Skip to content
This repository was archived by the owner on Jan 23, 2026. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions classes/class-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,35 @@ public function amp_admanager_menu_init() {

add_settings_field(
'dfp-network-id',
__( 'DFP Network ID', 'amp-admanager' ),
esc_html__( 'DFP Network ID', 'amp-admanager' ),
[ $this, 'get_text_field' ],
'amp-admanager-menu-page',
'amp-admanager-general-settings'
);

add_settings_field(
'load-amp-resources',
__( 'Load AMP Resources For Non AMP Site', 'amp-admanager' ),
esc_html__( 'Load AMP Resources For Non AMP Site', 'amp-admanager' ),
[ $this, 'get_checkbox_field' ],
'amp-admanager-menu-page',
'amp-admanager-general-settings'
);

add_settings_field(
'amp_admanager_enable_sticky_ads',
esc_html__( 'Enable Sticky Ads', 'amp-admanager' ),
[ $this, 'get_enable_sticky_ad_checkbox_field' ],
'amp-admanager-menu-page',
'amp-admanager-general-settings'
);

add_settings_field(
'amp_admanager_sticky_ad_unit',
esc_html__( 'Sticky adunit name', 'amp-admanager' ),
[ $this, 'get_sticky_adunit_name_field' ],
'amp-admanager-menu-page',
'amp-admanager-general-settings'
);
}

/**
Expand All @@ -118,4 +134,22 @@ public function get_checkbox_field() {
public function get_text_field() {
printf( '<input name="amp-admanager-menu-settings[dfp-network-id]" type="text" id="dfp-network-id" value="%s" class="regular-text">', esc_attr( $this->amp_settings['dfp-network-id'] ) );
}

/**
* Prints markup for enable sticky ad checkbox.
*
* @return void
*/
public function get_enable_sticky_ad_checkbox_field() {
Comment thread
deepaklalwani97 marked this conversation as resolved.
printf( '<input name="amp-admanager-menu-settings[amp_admanager_enable_sticky_ads]" type="checkbox" id="amp_admanager_enable_sticky_ads" value="1" %s>', checked( $this->amp_settings['amp_admanager_enable_sticky_ads'], '1', false ) );
}

/**
* Prints markup for adunit name text field.
*
* @return void
*/
public function get_sticky_adunit_name_field() {
Comment thread
deepaklalwani97 marked this conversation as resolved.
printf( '<input name="amp-admanager-menu-settings[amp_admanager_sticky_ad_unit]" type="text" id="amp_admanager_sticky_ad_unit" value="%s" class="regular-text">', esc_attr( $this->amp_settings['amp_admanager_sticky_ad_unit'] ) );
}
}
75 changes: 60 additions & 15 deletions classes/class-amp-admanager.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public function __construct() {
*/
add_action( 'wp_head', [ $this, 'load_amp_resources' ], 0 );

add_action( 'wp_footer', [ $this, 'add_sticky_amp_ad' ] );

}

/**
Expand Down Expand Up @@ -183,21 +185,38 @@ private static function get_amp_ad( $attr ) {
*/
$ad_arefresh_rate = ( isset( $attr['ad-refresh'] ) && (int) $attr['ad-refresh'] >= 30 ) ? (int) $attr['ad-refresh'] : false;

/**
* amp-ad markup.
*/
$ad_html = sprintf(
'<amp-ad width="%s" height="%s" media="%s" type="doubleclick" data-slot="%s" json=\'%s\' data-multi-size="%s" data-multi-size-validation="false" layout="%s" data-loading-strategy="%s" data-enable-refresh=%s></amp-ad>',
esc_attr( $attr['width'] ),
esc_attr( $attr['height'] ),
esc_attr( $media_query ),
esc_attr( $data_slot ),
esc_attr( $targeting_data_json ),
esc_attr( $attr['sizes'] ),
esc_attr( $layout ),
esc_attr( $data_loading_strategy ),
esc_attr( $ad_arefresh_rate )
);
if ( ! empty( $attr['sticky'] ) && true === $attr['sticky'] ) {
Comment thread
deepaklalwani97 marked this conversation as resolved.
/**
* Amp-sticky-ad markup.
*/
$ad_html = sprintf(
'<amp-sticky-ad layout="nodisplay"> <amp-ad width="%s" height="%s" media="%s" type="doubleclick" data-slot="%s" json=\'%s\' data-multi-size="%s" data-multi-size-validation="false" data-loading-strategy="%s" data-enable-refresh=%s></amp-ad> </amp-sticky-ad>',
esc_attr( $attr['width'] ),
esc_attr( $attr['height'] ),
esc_attr( $media_query ),
esc_attr( $data_slot ),
esc_attr( $targeting_data_json ),
esc_attr( $attr['sizes'] ),
esc_attr( $data_loading_strategy ),
esc_attr( $ad_arefresh_rate )
);
} else {
/**
* Amp-ad markup.
*/
$ad_html = sprintf(
'<amp-ad width="%s" height="%s" media="%s" type="doubleclick" data-slot="%s" json=\'%s\' data-multi-size="%s" data-multi-size-validation="false" layout="%s" data-loading-strategy="%s" data-enable-refresh=%s></amp-ad>',
esc_attr( $attr['width'] ),
esc_attr( $attr['height'] ),
esc_attr( $media_query ),
esc_attr( $data_slot ),
esc_attr( $targeting_data_json ),
esc_attr( $attr['sizes'] ),
esc_attr( $layout ),
esc_attr( $data_loading_strategy ),
esc_attr( $ad_arefresh_rate )
);
}

return $ad_html;
}
Expand Down Expand Up @@ -430,4 +449,30 @@ public function load_amp_resources() {

}
}

/**
* Add amp sticky ad to the footer.
*
* @return void
*/
public function add_sticky_amp_ad() {

if ( empty( self::$amp_settings['amp_admanager_enable_sticky_ads'] ) || '1' !== self::$amp_settings['amp_admanager_enable_sticky_ads'] ) {
return;
}

if ( empty( self::$amp_settings['amp_admanager_sticky_ad_unit'] ) ) {
return;
}

$ad_attr = [
'ad-unit' => self::$amp_settings['amp_admanager_sticky_ad_unit'],
'desktop-sizes' => '600x90,728x90',
'tablet-sizes' => '320x50,468x60',
'mobile-sizes' => '320x50,468x60',
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sizes should be dynamic instead of static, right? I see the discussion is going on #59
cc: @pradeep910

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes sizes should be dynamic, and override with these *-sizes parameters.

'sticky' => true,
];

self::get_ads( $ad_attr, true );
}
}
4 changes: 4 additions & 0 deletions template-parts/amp-resources.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@
</style>
</noscript>
<link rel="preload" as="script" href="https://cdn.ampproject.org/v0.js">

<?php //phpcs:disable ?>
<script type="text/javascript" src="https://cdn.ampproject.org/v0.js" async></script>
Comment thread
deepaklalwani97 marked this conversation as resolved.
<script async custom-element="amp-sticky-ad" src="https://cdn.ampproject.org/v0/amp-sticky-ad-1.0.js"></script>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be a check here if sticky ad is enabled, then only include this script.

<?php //phpcs:enable ?>