Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for sticky ad #62

Open
wants to merge 16 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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() {
deepaklalwani97 marked this conversation as resolved.
Show resolved Hide 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() {
deepaklalwani97 marked this conversation as resolved.
Show resolved Hide 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'] ) );
}
}
79 changes: 64 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'] ) {
deepaklalwani97 marked this conversation as resolved.
Show resolved Hide 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,34 @@ 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;
}

?>
<script async custom-element="amp-sticky-ad" src="https://cdn.ampproject.org/v0/amp-sticky-ad-1.0.js"></script> // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
deepaklalwani97 marked this conversation as resolved.
Show resolved Hide resolved
<?php

$ad_attr = [
'ad-unit' => self::$amp_settings['amp_admanager_sticky_ad_unit'],
'mobile-sizes' => '320x50',
'tablet-sizes' => '320x50',
'desktop-sizes' => '320x50',
'sticky' => true,
];

self::get_ads( $ad_attr, true );
}
}