diff --git a/classes/class-admin.php b/classes/class-admin.php index acf4ccf..75c309c 100644 --- a/classes/class-admin.php +++ b/classes/class-admin.php @@ -84,9 +84,16 @@ public function amp_admanager_menu_init() { 'amp-admanager-menu-page' ); + add_settings_section( + 'amp-admanager-sticky-ad-settings', + __( 'Sticky ad Settings', 'amp-admanager' ), + '__return_empty_string', + 'amp-admanager-menu-page' + ); + 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' @@ -94,11 +101,43 @@ public function amp_admanager_menu_init() { 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-sticky-ad-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-sticky-ad-settings' + ); + + add_settings_field( + 'amp_admanager_sticky_ad_height', + esc_html__( 'Sticky Ad height', 'amp-admanager' ), + [ $this, 'get_sticky_adunit_height_field' ], + 'amp-admanager-menu-page', + 'amp-admanager-sticky-ad-settings' + ); + + add_settings_field( + 'amp_admanager_sticky_ad_width', + esc_html__( 'Sticky Ad width', 'amp-admanager' ), + [ $this, 'get_sticky_adunit_width_field' ], + 'amp-admanager-menu-page', + 'amp-admanager-sticky-ad-settings' + ); } /** @@ -118,4 +157,40 @@ public function get_checkbox_field() { public function get_text_field() { printf( '', 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() { + printf( '', 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() { + printf( '', esc_attr( $this->amp_settings['amp_admanager_sticky_ad_unit'] ) ); + } + + /** + * Prints markup for sticky ad width field. + * + * @return void + */ + public function get_sticky_adunit_width_field() { + printf( '', esc_attr( $this->amp_settings['amp_admanager_sticky_ad_width'] ) ); + } + + /** + * Prints markup for sticky ad height field. + * + * @return void + */ + public function get_sticky_adunit_height_field() { + printf( '', esc_attr( $this->amp_settings['amp_admanager_sticky_ad_height'] ) ); + } } diff --git a/classes/class-amp-admanager.php b/classes/class-amp-admanager.php index 622f6fa..fe61f2b 100644 --- a/classes/class-amp-admanager.php +++ b/classes/class-amp-admanager.php @@ -153,7 +153,12 @@ private static function get_amp_ad( $attr ) { $network_id = ( ! empty( $attr['network-id'] ) ) ? $attr['network-id'] : self::$amp_settings['dfp-network-id']; $data_slot = sprintf( '/%s/%s', $network_id, $attr['ad-unit'] ); - $media_query = self::get_slot_media_query( $attr['min'], $attr['max'] ); + // Add media query only non sticky ads. + if ( empty( $attr['sticky'] ) ) { + $media_query = self::get_slot_media_query( $attr['min'], $attr['max'] ); + } else { + $media_query = ''; + } /** * Encode targeting data for json attribute in amp-ad. @@ -183,22 +188,29 @@ 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. - */ + if ( ! empty( $attr['sticky'] ) && true === $attr['sticky'] ) { + $layout = ''; + } else { + $layout = sprintf( 'layout=%s', $layout ); + } + $ad_html = sprintf( - '', + '', 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( $attr['sizes'] ?? '' ), + $layout, esc_attr( $data_loading_strategy ), esc_attr( $ad_arefresh_rate ) ); + if ( ! empty( $attr['sticky'] ) && true === $attr['sticky'] ) { + $ad_html = '' . $ad_html . ''; + } + return $ad_html; } @@ -265,6 +277,39 @@ public static function get_ads( $attr = [], $echo = false ) { return $ad_html; } + /** + * Get amp sticky ad html for all the sizes. + * + * @param array $attr shortcode attributes. + * @param boolean $echo whether to echo or return html code. + * + * @return string + */ + public static function get_sticky_ad( $attr = [], $echo = false ) { + + if ( empty( self::$amp_settings['amp_admanager_sticky_ad_unit'] ) || empty( self::$amp_settings['amp_admanager_sticky_ad_width'] ) || empty( self::$amp_settings['amp_admanager_sticky_ad_height'] ) ) { + return; + } + + $default_attr = [ + 'ad-unit' => esc_attr( self::$amp_settings['amp_admanager_sticky_ad_unit'] ), + 'width' => esc_attr( self::$amp_settings['amp_admanager_sticky_ad_width'] ), + 'height' => esc_attr( self::$amp_settings['amp_admanager_sticky_ad_height'] ), + 'layout' => 'fixed', + + ]; + + $attr = wp_parse_args( $attr, $default_attr ); + + $attr['sticky'] = true; + + if ( true === $echo ) { + echo self::get_amp_ad( $attr, $echo ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped + } else { + return self::get_amp_ad( $attr, $echo ); + } + } + /** * Filter sizes for all 3 device type breakpoints. * @@ -410,6 +455,10 @@ public function load_amp_resources() { '; // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript + } + // Check if current page is amp page. if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) { return; @@ -430,4 +479,5 @@ public function load_amp_resources() { } } + }