From 1f3c708052b5b795ef66a84b194f55cae7bca8c6 Mon Sep 17 00:00:00 2001 From: Al-Ameen Ogundiran Date: Thu, 14 Nov 2024 09:26:47 +0000 Subject: [PATCH 01/22] add test log --- projects/plugins/jetpack/modules/wordads/js/adflow-loader.js | 1 + 1 file changed, 1 insertion(+) diff --git a/projects/plugins/jetpack/modules/wordads/js/adflow-loader.js b/projects/plugins/jetpack/modules/wordads/js/adflow-loader.js index 67d51d14f3a3e..321d9a2d8f367 100644 --- a/projects/plugins/jetpack/modules/wordads/js/adflow-loader.js +++ b/projects/plugins/jetpack/modules/wordads/js/adflow-loader.js @@ -1,4 +1,5 @@ function a8c_adflow_callback( data ) { + console.log( 'a8c_adflow_callback', data ); if ( data && data.scripts && Array.isArray( data.scripts ) ) { if ( data.config ) { let configurationScript = document.createElement( 'script' ); From 24a89c8bf5d72ac48e06c2b2e6da2850527f6b31 Mon Sep 17 00:00:00 2001 From: Al-Ameen Ogundiran Date: Thu, 14 Nov 2024 09:27:47 +0000 Subject: [PATCH 02/22] changelog --- projects/plugins/jetpack/changelog/try-ipw-migration-spike | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 projects/plugins/jetpack/changelog/try-ipw-migration-spike diff --git a/projects/plugins/jetpack/changelog/try-ipw-migration-spike b/projects/plugins/jetpack/changelog/try-ipw-migration-spike new file mode 100644 index 0000000000000..2b80a25cafa31 --- /dev/null +++ b/projects/plugins/jetpack/changelog/try-ipw-migration-spike @@ -0,0 +1,4 @@ +Significance: patch +Type: other + +debugging wordads for ipw migration spike From 434279288226907665120d97b59fc6ae1fdb650f Mon Sep 17 00:00:00 2001 From: Al-Ameen Ogundiran Date: Thu, 14 Nov 2024 09:30:50 +0000 Subject: [PATCH 03/22] update log --- projects/plugins/jetpack/modules/wordads/js/adflow-loader.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projects/plugins/jetpack/modules/wordads/js/adflow-loader.js b/projects/plugins/jetpack/modules/wordads/js/adflow-loader.js index 321d9a2d8f367..94cff4e47fc21 100644 --- a/projects/plugins/jetpack/modules/wordads/js/adflow-loader.js +++ b/projects/plugins/jetpack/modules/wordads/js/adflow-loader.js @@ -1,5 +1,5 @@ function a8c_adflow_callback( data ) { - console.log( 'a8c_adflow_callback', data ); + console.log( 'watl call, a8c_adflow_callback', data ); if ( data && data.scripts && Array.isArray( data.scripts ) ) { if ( data.config ) { let configurationScript = document.createElement( 'script' ); From 13d113a08123a16fb68bd52682512717f47ea51d Mon Sep 17 00:00:00 2001 From: Al-Ameen Ogundiran Date: Thu, 21 Nov 2024 06:48:57 +0000 Subject: [PATCH 04/22] enable bottom sticky --- .../modules/wordads/php/class-wordads-smart.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php b/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php index be78d6e874fbb..03cc6353fb830 100644 --- a/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php +++ b/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php @@ -170,17 +170,20 @@ public function insert_config() { global $post; $config = array( - 'blog_id' => $this->get_blog_id(), - 'post_id' => ( $post instanceof WP_Post ) && is_singular( 'post' ) ? $post->ID : null, - 'theme' => $this->theme, - 'target' => $this->target_keywords(), - '_' => array( + 'blog_id' => $this->get_blog_id(), + 'post_id' => ( $post instanceof WP_Post ) && is_singular( 'post' ) ? $post->ID : null, + 'theme' => $this->theme, + 'target' => $this->target_keywords(), + '_' => array( 'title' => __( 'Advertisement', 'jetpack' ), 'privacy_settings' => __( 'Privacy Settings', 'jetpack' ), ), - 'inline' => array( + 'inline' => array( 'enabled' => $this->is_inline_enabled, ), + 'bottom_sticky' => array( + 'enabled' => true, + ), ); // Do conversion. From 107f6d8dfa427955604c20b1f8ca0653bb262854 Mon Sep 17 00:00:00 2001 From: Al-Ameen Ogundiran Date: Thu, 21 Nov 2024 11:48:04 +0000 Subject: [PATCH 05/22] add method to insert wordads-tag html to be used by watl.js --- .../jetpack/modules/wordads/class-wordads.php | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/projects/plugins/jetpack/modules/wordads/class-wordads.php b/projects/plugins/jetpack/modules/wordads/class-wordads.php index 851b093d750b3..5be2917ed3a45 100644 --- a/projects/plugins/jetpack/modules/wordads/class-wordads.php +++ b/projects/plugins/jetpack/modules/wordads/class-wordads.php @@ -382,7 +382,7 @@ public function insert_head_meta() { get_dynamic_ad_snippet( $this->params->blog_id . 5, 'square', 'inline', '', '{{unique_id}}' ); + $tag_inline = $this->get_fallback_ad_snippet( $this->params->blog_id . 5, 'square', 'inline', '', '{{unique_id}}' ); // Remove linebreaks and sanitize. $tag_inline = esc_js( str_replace( array( "\n", "\t", "\r" ), '', $tag_inline ) ); @@ -703,6 +703,7 @@ public function get_ad_snippet( $section_id, $height, $width, $location = '', $c $form_factor = 'leaderboard'; } + // TODO: Still investigating required change. Potentially update with respect with what get_ad_div on WPCOM for gutenberg return $this->get_dynamic_ad_snippet( $section_id, $form_factor, $location ); } @@ -720,6 +721,33 @@ public function get_ad_snippet( $section_id, $height, $width, $location = '', $c * @since 8.7 */ public function get_dynamic_ad_snippet( $section_id, $form_factor = 'square', $location = '', $relocate = '', $id = null ) { + + // Allow overriding and printing of the tag parsed by the WATL. + // phpcs:disable WordPress.Security.NonceVerification.Recommended + $is_location_enabled = ( isset( $_GET[ $location ] ) && 'true' === $_GET[ $location ] ); + + if ( ( 'top' === $location || 'belowpost' === $location || 'sidebar' === $location ) && $is_location_enabled ) { + // TODO: Confirm if it's best here or there is a way to get it via the adflow config endpoint + return "
"; + } + + return $this->get_fallback_ad_snippet( $section_id, $form_factor, $location, $relocate, $id ); + } + + /** + * Returns the fallback dynamic snippet to be inserted into the ad unit + * + * @param int $section_id section_id. + * @param string $form_factor form_factor. + * @param string $location location. + * @param string $relocate location to be moved after the fact for themes without required hook. + * @param string | null $id A unique string ID or placeholder. + * + * @return string + * + * @since 8.7 + */ + public function get_fallback_ad_snippet( $section_id, $form_factor = 'square', $location = '', $relocate = '', $id = null ) { $div_id = 'atatags-' . $section_id . '-' . ( $id ?? uniqid() ); $div_id = esc_attr( $div_id ); From 5e2ef5aae86737c003e76c2fed035fab4b3300fb Mon Sep 17 00:00:00 2001 From: Al-Ameen Ogundiran Date: Thu, 21 Nov 2024 11:58:52 +0000 Subject: [PATCH 06/22] enable belowpost to use adflow with fallback support --- .../plugins/jetpack/modules/wordads/class-wordads.php | 11 ++++++++++- .../modules/wordads/php/class-wordads-smart.php | 5 ++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/projects/plugins/jetpack/modules/wordads/class-wordads.php b/projects/plugins/jetpack/modules/wordads/class-wordads.php index 5be2917ed3a45..a47d09de45c67 100644 --- a/projects/plugins/jetpack/modules/wordads/class-wordads.php +++ b/projects/plugins/jetpack/modules/wordads/class-wordads.php @@ -381,8 +381,16 @@ public function insert_head_meta() { params->blog_id . 5; + + // Get below post tag. + $tag_belowpost = $this->get_fallback_ad_snippet( $section_id, 'square', 'belowpost', '', '{{unique_id}}' ); + + // Remove linebreaks and sanitize. + $tag_belowpost = esc_js( str_replace( array( "\n", "\t", "\r" ), '', $tag_belowpost ) ); + // Get an inline tag with a macro as id handled on JS side to use as a fallback. - $tag_inline = $this->get_fallback_ad_snippet( $this->params->blog_id . 5, 'square', 'inline', '', '{{unique_id}}' ); + $tag_inline = $this->get_fallback_ad_snippet( $section_id, 'square', 'inline', '', '{{unique_id}}' ); // Remove linebreaks and sanitize. $tag_inline = esc_js( str_replace( array( "\n", "\t", "\r" ), '', $tag_inline ) ); @@ -393,6 +401,7 @@ public function insert_head_meta() { var sas_fallback = sas_fallback || []; sas_fallback.push( { tag: "$tag_inline", type: 'inline' } + { tag: "$tag_belowpost", type: 'belowpost' } ); HTML; diff --git a/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php b/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php index 03cc6353fb830..b879f99bbb0e9 100644 --- a/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php +++ b/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php @@ -182,7 +182,10 @@ public function insert_config() { 'enabled' => $this->is_inline_enabled, ), 'bottom_sticky' => array( - 'enabled' => true, + 'enabled' => true, // TODO: this will not be true by default + ), + 'belowpost' => array( + 'enabled' => true, // TODO: this will not be true by default ), ); From e8c12efd40e13d132f8c913fe4f83da0a4cde9a6 Mon Sep 17 00:00:00 2001 From: Al-Ameen Ogundiran Date: Thu, 21 Nov 2024 12:04:15 +0000 Subject: [PATCH 07/22] update changelog --- projects/plugins/jetpack/changelog/try-ipw-migration-spike | 4 ---- .../jetpack/changelog/update-iponweb-to-watl-migration | 4 ++++ 2 files changed, 4 insertions(+), 4 deletions(-) delete mode 100644 projects/plugins/jetpack/changelog/try-ipw-migration-spike create mode 100644 projects/plugins/jetpack/changelog/update-iponweb-to-watl-migration diff --git a/projects/plugins/jetpack/changelog/try-ipw-migration-spike b/projects/plugins/jetpack/changelog/try-ipw-migration-spike deleted file mode 100644 index 2b80a25cafa31..0000000000000 --- a/projects/plugins/jetpack/changelog/try-ipw-migration-spike +++ /dev/null @@ -1,4 +0,0 @@ -Significance: patch -Type: other - -debugging wordads for ipw migration spike diff --git a/projects/plugins/jetpack/changelog/update-iponweb-to-watl-migration b/projects/plugins/jetpack/changelog/update-iponweb-to-watl-migration new file mode 100644 index 0000000000000..646c60484e2bd --- /dev/null +++ b/projects/plugins/jetpack/changelog/update-iponweb-to-watl-migration @@ -0,0 +1,4 @@ +Significance: patch +Type: other + +Migration from IPONWEB to WATL From f98fd36bff7288063a9255f878e6cab1c07c693c Mon Sep 17 00:00:00 2001 From: Al-Ameen Ogundiran Date: Tue, 26 Nov 2024 12:01:50 +0000 Subject: [PATCH 08/22] implement watl placement for gutenberg ad formats --- .../extensions/blocks/wordads/wordads.php | 34 ++++++++++++++++--- .../jetpack/modules/wordads/class-wordads.php | 17 ++++++++-- .../wordads/php/class-wordads-smart.php | 28 ++++++++++----- 3 files changed, 65 insertions(+), 14 deletions(-) diff --git a/projects/plugins/jetpack/extensions/blocks/wordads/wordads.php b/projects/plugins/jetpack/extensions/blocks/wordads/wordads.php index 511b6a2c27015..82790e1c8e9f3 100644 --- a/projects/plugins/jetpack/extensions/blocks/wordads/wordads.php +++ b/projects/plugins/jetpack/extensions/blocks/wordads/wordads.php @@ -20,6 +20,18 @@ * @since 7.1.0 */ class WordAds { + /** + * Mapping array of gutenberg ad snippet with the WordAds_Smart formats. + * + * @var array + */ + public static $gutenberg_ad_snippet_x_smart_format = array( + 'gutenberg_300x250' => 'gutenberg_rectangle', + 'gutenberg_728x90' => 'gutenberg_leaderboard', + 'gutenberg_320x50' => 'gutenberg_mobile_leaderboard', + 'gutenberg_160x600' => 'gutenberg_skyscraper', + ); + /** * Check if site is on WP.com Simple. * @@ -127,10 +139,24 @@ public static function gutenblock_render( $attr ) { $format = $attr['format']; } - $height = $ad_tag_ids[ $format ]['height']; - $width = $ad_tag_ids[ $format ]['width']; - $snippet = $wordads->get_ad_snippet( $section_id, $height, $width, 'gutenberg', $wordads->get_solo_unit_css() ); - return $wordads->get_ad_div( 'inline', $snippet, array( $align ) ); + $height = $ad_tag_ids[ $format ]['height']; + $width = $ad_tag_ids[ $format ]['width']; + $gutenberg_location = 'gutenberg'; + $snippet = $wordads->get_ad_snippet( $section_id, $height, $width, $gutenberg_location, $wordads->get_solo_unit_css() ); + + $key = "{$gutenberg_location}_{$width}x{$height}"; + $smart_format = self::$gutenberg_ad_snippet_x_smart_format[ $key ] ?? null; + // phpcs:disable WordPress.Security.NonceVerification.Recommended + $is_watl_enabled = $smart_format && ( isset( $_GET[ $smart_format ] ) && 'true' === $_GET[ $smart_format ] ); + + $ad_div = $wordads->get_ad_div( 'inline', $snippet, array( $align ) ); + // Render IPW div if WATL is not enabled. + if ( ! $is_watl_enabled ) { + return $ad_div; + } + + // TODO: Add sas_fallback to the ad_div. + return $wordads->get_watl_ad_html_tag( $smart_format ); } } diff --git a/projects/plugins/jetpack/modules/wordads/class-wordads.php b/projects/plugins/jetpack/modules/wordads/class-wordads.php index a47d09de45c67..e56b8ca15c86c 100644 --- a/projects/plugins/jetpack/modules/wordads/class-wordads.php +++ b/projects/plugins/jetpack/modules/wordads/class-wordads.php @@ -735,9 +735,9 @@ public function get_dynamic_ad_snippet( $section_id, $form_factor = 'square', $l // phpcs:disable WordPress.Security.NonceVerification.Recommended $is_location_enabled = ( isset( $_GET[ $location ] ) && 'true' === $_GET[ $location ] ); - if ( ( 'top' === $location || 'belowpost' === $location || 'sidebar' === $location ) && $is_location_enabled ) { + if ( ( 'belowpost' === $location ) && $is_location_enabled ) { // TODO: Confirm if it's best here or there is a way to get it via the adflow config endpoint - return "
"; + return self::get_watl_ad_html_tag( $location ); } return $this->get_fallback_ad_snippet( $section_id, $form_factor, $location, $relocate, $id ); @@ -905,6 +905,19 @@ public function get_house_ad( $unit = 'mrec' ) { HTML; } + /** + * Returns the html ad tag used by WordAds Tag Library + * + * @param string $slot_type e.g belowpost, gutenberg_rectangle. + * + * @return string + * + * @since 8.7 + */ + public static function get_watl_ad_html_tag( string $slot_type ): string { + return "
"; + } + /** * Activation hook actions * diff --git a/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php b/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php index b879f99bbb0e9..9ab199bddf64a 100644 --- a/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php +++ b/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php @@ -170,21 +170,33 @@ public function insert_config() { global $post; $config = array( - 'blog_id' => $this->get_blog_id(), - 'post_id' => ( $post instanceof WP_Post ) && is_singular( 'post' ) ? $post->ID : null, - 'theme' => $this->theme, - 'target' => $this->target_keywords(), - '_' => array( + 'blog_id' => $this->get_blog_id(), + 'post_id' => ( $post instanceof WP_Post ) && is_singular( 'post' ) ? $post->ID : null, + 'theme' => $this->theme, + 'target' => $this->target_keywords(), + '_' => array( 'title' => __( 'Advertisement', 'jetpack' ), 'privacy_settings' => __( 'Privacy Settings', 'jetpack' ), ), - 'inline' => array( + 'inline' => array( 'enabled' => $this->is_inline_enabled, ), - 'bottom_sticky' => array( + 'bottom_sticky' => array( 'enabled' => true, // TODO: this will not be true by default ), - 'belowpost' => array( + 'belowpost' => array( + 'enabled' => true, // TODO: this will not be true by default + ), + 'gutenberg_rectangle' => array( + 'enabled' => true, // TODO: this will not be true by default + ), + 'gutenberg_leaderboard' => array( + 'enabled' => true, // TODO: this will not be true by default + ), + 'gutenberg_mobile_leaderboard' => array( + 'enabled' => true, // TODO: this will not be true by default + ), + 'gutenberg_skyscraper' => array( 'enabled' => true, // TODO: this will not be true by default ), ); From 7f5350d60726eb2a76e856d69e968a59a109b26b Mon Sep 17 00:00:00 2001 From: Al-Ameen Ogundiran Date: Tue, 26 Nov 2024 12:05:55 +0000 Subject: [PATCH 09/22] implement fallback to ipw on gutenberg watl/smart ad --- .../extensions/blocks/wordads/wordads.php | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/projects/plugins/jetpack/extensions/blocks/wordads/wordads.php b/projects/plugins/jetpack/extensions/blocks/wordads/wordads.php index 82790e1c8e9f3..e19ccc8ad5399 100644 --- a/projects/plugins/jetpack/extensions/blocks/wordads/wordads.php +++ b/projects/plugins/jetpack/extensions/blocks/wordads/wordads.php @@ -148,15 +148,26 @@ public static function gutenblock_render( $attr ) { $smart_format = self::$gutenberg_ad_snippet_x_smart_format[ $key ] ?? null; // phpcs:disable WordPress.Security.NonceVerification.Recommended $is_watl_enabled = $smart_format && ( isset( $_GET[ $smart_format ] ) && 'true' === $_GET[ $smart_format ] ); - - $ad_div = $wordads->get_ad_div( 'inline', $snippet, array( $align ) ); + $ad_div = $wordads->get_ad_div( 'inline', $snippet, array( $align ) ); // Render IPW div if WATL is not enabled. if ( ! $is_watl_enabled ) { return $ad_div; } - // TODO: Add sas_fallback to the ad_div. - return $wordads->get_watl_ad_html_tag( $smart_format ); + // Remove linebreaks and sanitize. + $snippet = esc_js( str_replace( array( "\n", "\t", "\r" ), '', $ad_div ) ); + + // phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped + $fallback_snippet = << + var sas_fallback = sas_fallback || []; + sas_fallback.push( + { tag: "$snippet", type: '$smart_format' } + ); + +HTML; + + return $fallback_snippet . $wordads->get_watl_ad_html_tag( $smart_format ); } } From 85086afd7c9b9ee908f84b6a252052953cd3b211 Mon Sep 17 00:00:00 2001 From: Al-Ameen Ogundiran Date: Tue, 26 Nov 2024 12:08:48 +0000 Subject: [PATCH 10/22] remove debug console log --- projects/plugins/jetpack/modules/wordads/js/adflow-loader.js | 1 - 1 file changed, 1 deletion(-) diff --git a/projects/plugins/jetpack/modules/wordads/js/adflow-loader.js b/projects/plugins/jetpack/modules/wordads/js/adflow-loader.js index 94cff4e47fc21..67d51d14f3a3e 100644 --- a/projects/plugins/jetpack/modules/wordads/js/adflow-loader.js +++ b/projects/plugins/jetpack/modules/wordads/js/adflow-loader.js @@ -1,5 +1,4 @@ function a8c_adflow_callback( data ) { - console.log( 'watl call, a8c_adflow_callback', data ); if ( data && data.scripts && Array.isArray( data.scripts ) ) { if ( data.config ) { let configurationScript = document.createElement( 'script' ); From 8ac93cf1e355cc01a79b3704a3a06645db67d623 Mon Sep 17 00:00:00 2001 From: Al-Ameen Ogundiran Date: Tue, 26 Nov 2024 13:59:12 +0000 Subject: [PATCH 11/22] enable sidebar sticky right --- .../jetpack/modules/wordads/php/class-wordads-smart.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php b/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php index 9ab199bddf64a..84bc769a22e25 100644 --- a/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php +++ b/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php @@ -187,6 +187,9 @@ public function insert_config() { 'belowpost' => array( 'enabled' => true, // TODO: this will not be true by default ), + 'sidebar_sticky_right' => array( + 'enabled' => true, // TODO: this will not be true by default + ), 'gutenberg_rectangle' => array( 'enabled' => true, // TODO: this will not be true by default ), From c41932c58b60e001571fa32590c2ea232998071f Mon Sep 17 00:00:00 2001 From: Al-Ameen Ogundiran Date: Tue, 26 Nov 2024 14:27:26 +0000 Subject: [PATCH 12/22] enable top leaderboard with fallback support --- .../plugins/jetpack/modules/wordads/class-wordads.php | 9 ++++++++- .../jetpack/modules/wordads/php/class-wordads-smart.php | 7 +++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/projects/plugins/jetpack/modules/wordads/class-wordads.php b/projects/plugins/jetpack/modules/wordads/class-wordads.php index e56b8ca15c86c..db618038ef95c 100644 --- a/projects/plugins/jetpack/modules/wordads/class-wordads.php +++ b/projects/plugins/jetpack/modules/wordads/class-wordads.php @@ -395,6 +395,12 @@ public function insert_head_meta() { // Remove linebreaks and sanitize. $tag_inline = esc_js( str_replace( array( "\n", "\t", "\r" ), '', $tag_inline ) ); + // Get top tag. + $tag_top = $this->get_fallback_ad_snippet( $section_id, 'leaderboard', 'top', '', '{{unique_id}}' ); + + // Remove linebreaks and sanitize. + $tag_top = esc_js( str_replace( array( "\n", "\t", "\r" ), '', $tag_top ) ); + // phpcs:disable WordPress.Security.EscapeOutput.HeredocOutputNotEscaped echo << @@ -402,6 +408,7 @@ public function insert_head_meta() { sas_fallback.push( { tag: "$tag_inline", type: 'inline' } { tag: "$tag_belowpost", type: 'belowpost' } + { tag: "$tag_top", type: 'top' } ); HTML; @@ -735,7 +742,7 @@ public function get_dynamic_ad_snippet( $section_id, $form_factor = 'square', $l // phpcs:disable WordPress.Security.NonceVerification.Recommended $is_location_enabled = ( isset( $_GET[ $location ] ) && 'true' === $_GET[ $location ] ); - if ( ( 'belowpost' === $location ) && $is_location_enabled ) { + if ( ( 'top' === $location || 'belowpost' === $location ) && $is_location_enabled ) { // TODO: Confirm if it's best here or there is a way to get it via the adflow config endpoint return self::get_watl_ad_html_tag( $location ); } diff --git a/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php b/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php index 84bc769a22e25..405b5a4498973 100644 --- a/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php +++ b/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php @@ -178,13 +178,16 @@ public function insert_config() { 'title' => __( 'Advertisement', 'jetpack' ), 'privacy_settings' => __( 'Privacy Settings', 'jetpack' ), ), + 'top' => array( + 'enabled' => true, // TODO: this will not be true by default + ), 'inline' => array( 'enabled' => $this->is_inline_enabled, ), - 'bottom_sticky' => array( + 'belowpost' => array( 'enabled' => true, // TODO: this will not be true by default ), - 'belowpost' => array( + 'bottom_sticky' => array( 'enabled' => true, // TODO: this will not be true by default ), 'sidebar_sticky_right' => array( From 742dcddd79faaea7d008ee29b2f061e6c9ddcb85 Mon Sep 17 00:00:00 2001 From: Al-Ameen Ogundiran Date: Thu, 28 Nov 2024 15:11:16 +0000 Subject: [PATCH 13/22] disable formats by default and override with query string --- .../wordads/php/class-wordads-smart.php | 98 ++++++++++++------- 1 file changed, 60 insertions(+), 38 deletions(-) diff --git a/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php b/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php index 405b5a4498973..10a90cf41c3be 100644 --- a/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php +++ b/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php @@ -51,6 +51,41 @@ class WordAds_Smart { */ private $is_inline_enabled; + /** + * Supported formats. + * + * @var array + */ + private $formats = array( + 'top' => array( + 'enabled' => false, + ), + 'inline' => array( + 'enabled' => false, + ), + 'belowpost' => array( + 'enabled' => false, + ), + 'bottom_sticky' => array( + 'enabled' => false, + ), + 'sidebar_sticky_right' => array( + 'enabled' => false, + ), + 'gutenberg_rectangle' => array( + 'enabled' => false, + ), + 'gutenberg_leaderboard' => array( + 'enabled' => false, + ), + 'gutenberg_mobile_leaderboard' => array( + 'enabled' => false, + ), + 'gutenberg_skyscraper' => array( + 'enabled' => false, + ), + ); + /** * Private constructor. */ @@ -83,11 +118,11 @@ public function init( WordAds_Params $params ) { $this->theme = get_stylesheet(); $this->is_inline_enabled = is_singular( 'post' ) && $params->options['wordads_inline_enabled']; - // Allow override. - // phpcs:disable WordPress.Security.NonceVerification.Recommended - if ( isset( $_GET['inline'] ) && 'true' === $_GET['inline'] ) { - $this->is_inline_enabled = true; - } + // Allow enabled format override by query string. + $this->override_formats_from_query_string(); + // TODO: refactor to remove the need to do this + $this->is_inline_enabled = $this->formats['inline']['enabled']; + if ( $this->is_inline_enabled ) { // Insert ads. $this->insert_ads(); @@ -170,42 +205,15 @@ public function insert_config() { global $post; $config = array( - 'blog_id' => $this->get_blog_id(), - 'post_id' => ( $post instanceof WP_Post ) && is_singular( 'post' ) ? $post->ID : null, - 'theme' => $this->theme, - 'target' => $this->target_keywords(), - '_' => array( + 'blog_id' => $this->get_blog_id(), + 'post_id' => ( $post instanceof WP_Post ) && is_singular( 'post' ) ? $post->ID : null, + 'theme' => $this->theme, + 'target' => $this->target_keywords(), + '_' => array( 'title' => __( 'Advertisement', 'jetpack' ), 'privacy_settings' => __( 'Privacy Settings', 'jetpack' ), ), - 'top' => array( - 'enabled' => true, // TODO: this will not be true by default - ), - 'inline' => array( - 'enabled' => $this->is_inline_enabled, - ), - 'belowpost' => array( - 'enabled' => true, // TODO: this will not be true by default - ), - 'bottom_sticky' => array( - 'enabled' => true, // TODO: this will not be true by default - ), - 'sidebar_sticky_right' => array( - 'enabled' => true, // TODO: this will not be true by default - ), - 'gutenberg_rectangle' => array( - 'enabled' => true, // TODO: this will not be true by default - ), - 'gutenberg_leaderboard' => array( - 'enabled' => true, // TODO: this will not be true by default - ), - 'gutenberg_mobile_leaderboard' => array( - 'enabled' => true, // TODO: this will not be true by default - ), - 'gutenberg_skyscraper' => array( - 'enabled' => true, // TODO: this will not be true by default - ), - ); + ) + $this->formats; // Do conversion. $js_config = WordAds_Array_Utils::array_to_js_object( $config ); @@ -226,6 +234,20 @@ private function get_config_url(): string { ); } + /** + * Allow format enabled override from query string, eg. ?inline=true. + * + * @return void + */ + private function override_formats_from_query_string(): void { + foreach ( $this->formats as $format_type => $_ ) { + // phpcs:disable WordPress.Security.NonceVerification.Recommended + if ( isset( $_GET[ $format_type ] ) && 'true' === $_GET[ $format_type ] ) { + $this->formats[ $format_type ]['enabled'] = true; + } + } + } + /** * Places marker at the end of the content so inline can identify the post content container. * From 0137ca3b461001f3a59aef309880eba76cd1cf66 Mon Sep 17 00:00:00 2001 From: Al-Ameen Ogundiran Date: Thu, 28 Nov 2024 16:04:54 +0000 Subject: [PATCH 14/22] always run smart ad --- .../jetpack/modules/wordads/php/class-wordads-smart.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php b/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php index 10a90cf41c3be..7b846b309dd06 100644 --- a/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php +++ b/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php @@ -123,10 +123,8 @@ public function init( WordAds_Params $params ) { // TODO: refactor to remove the need to do this $this->is_inline_enabled = $this->formats['inline']['enabled']; - if ( $this->is_inline_enabled ) { - // Insert ads. - $this->insert_ads(); - } + // Insert ads. + $this->insert_ads(); } /** From 83fca79736fc4d44126bd1c45067ea1d05e3b4dc Mon Sep 17 00:00:00 2001 From: Al-Ameen Ogundiran Date: Thu, 28 Nov 2024 16:09:20 +0000 Subject: [PATCH 15/22] replace inline ad condition with any ad format enabled check --- .../jetpack/modules/wordads/php/class-wordads-smart.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php b/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php index 7b846b309dd06..7852d599d838b 100644 --- a/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php +++ b/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php @@ -123,8 +123,12 @@ public function init( WordAds_Params $params ) { // TODO: refactor to remove the need to do this $this->is_inline_enabled = $this->formats['inline']['enabled']; - // Insert ads. - $this->insert_ads(); + // TODO: Is this necessary? if we want to always run Smart insert ads logic after IPW migrations + $has_any_format_enabled = in_array( true, array_column( $this->formats, 'enabled' ), true ); + if ( $has_any_format_enabled ) { + // Insert ads. + $this->insert_ads(); + } } /** From 3cb85749c0917be6d965543c755ea495cea2d7da Mon Sep 17 00:00:00 2001 From: Al-Ameen Ogundiran Date: Tue, 3 Dec 2024 13:52:58 +0000 Subject: [PATCH 16/22] Disable IPW sticky format and fix fallback (#40426) --- .../update-iponweb-to-watl-migration | 2 +- .../jetpack/modules/wordads/class-wordads.php | 42 +++++++++++++++++-- .../wordads/php/class-wordads-smart.php | 10 +++++ 3 files changed, 49 insertions(+), 5 deletions(-) diff --git a/projects/plugins/jetpack/changelog/update-iponweb-to-watl-migration b/projects/plugins/jetpack/changelog/update-iponweb-to-watl-migration index 646c60484e2bd..bd85e9ccf4478 100644 --- a/projects/plugins/jetpack/changelog/update-iponweb-to-watl-migration +++ b/projects/plugins/jetpack/changelog/update-iponweb-to-watl-migration @@ -1,4 +1,4 @@ Significance: patch Type: other -Migration from IPONWEB to WATL +Migration from IPONWEB to WATL diff --git a/projects/plugins/jetpack/modules/wordads/class-wordads.php b/projects/plugins/jetpack/modules/wordads/class-wordads.php index db618038ef95c..0d389e87c806e 100644 --- a/projects/plugins/jetpack/modules/wordads/class-wordads.php +++ b/projects/plugins/jetpack/modules/wordads/class-wordads.php @@ -371,9 +371,43 @@ public function insert_head_meta() { $site_id = $this->params->blog_id; $consent = (int) isset( $_COOKIE['personalized-ads-consent'] ); $is_logged_in = is_user_logged_in() ? '1' : '0'; + + $disabled_slot_formats = apply_filters( 'wordads_disabled_slot_formats', array() ); + + if ( apply_filters( 'wordads_iponweb_bottom_sticky_ad_disable', false ) ) { + $disabled_slot_formats[] = 'MTS'; + } + + if ( apply_filters( 'wordads_iponweb_sidebar_sticky_right_ad_disable', false ) ) { + $disabled_slot_formats[] = 'DPR'; + } + + $config = array( + 'pt' => $pagetype, + 'ht' => $hosting_type, + 'tn' => get_stylesheet(), + 'uloggedin' => $is_logged_in, + 'amp' => false, + 'siteid' => $site_id, + 'consent' => $consent, + 'ad' => array( + 'label' => array( + 'text' => __( 'Advertisements', 'jetpack' ), + ), + 'reportAd' => array( + 'text' => __( 'Report this ad', 'jetpack' ), + ), + 'privacySettings' => array( + 'text' => __( 'Privacy', 'jetpack' ), + 'onClick' => 'js:function() { window.__tcfapi && window.__tcfapi(\'showUi\'); }', + ), + ), + 'disabled_slot_formats' => $disabled_slot_formats, + ); + $js_config = WordAds_Array_Utils::array_to_js_object( $config ); ?> type="text/javascript"> - var __ATA_PP = { pt: , ht: , tn: '', uloggedin: , amp: false, siteid: , consent: , ad: { label: { text: '' }, reportAd: { text: '' }, privacySettings: { text: '', onClick: function() { window.__tcfapi && window.__tcfapi('showUi'); } } } }; + var __ATA_PP = ; var __ATA = __ATA || {}; __ATA.cmd = __ATA.cmd || []; __ATA.criteo = __ATA.criteo || {}; @@ -403,11 +437,11 @@ public function insert_head_meta() { // phpcs:disable WordPress.Security.EscapeOutput.HeredocOutputNotEscaped echo << + diff --git a/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php b/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php index 7852d599d838b..a60da8234ef77 100644 --- a/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php +++ b/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php @@ -196,6 +196,16 @@ private function insert_ads() { ); } } + + if ( $this->formats['bottom_sticky']['enabled'] ) { + // Disable IPW slot. + add_filter( 'wordads_iponweb_bottom_sticky_ad_disable', '__return_true', 10 ); + } + + if ( $this->formats['sidebar_sticky_right']['enabled'] ) { + // Disable IPW slot. + add_filter( 'wordads_iponweb_sidebar_sticky_right_ad_disable', '__return_true', 10 ); + } } /** From e085e59f1fbf6219b538a40629d671da7462c1bc Mon Sep 17 00:00:00 2001 From: Diogo Alves Date: Wed, 4 Dec 2024 09:41:55 -0300 Subject: [PATCH 17/22] [WordAds]: Update Smart class to handle new formats (#40431) * Update Smart class to handle new formats * Fix sas_fallback variable and add sidebar tag * Add sidebar on Smart formats to support the legacy sidebar widget * Add resource hints * Reorder methods * Fix enabled check for top format * Undo the support for sidebar format and minor fixes --- .../wordads/php/class-wordads-params.php | 42 +++--- .../wordads/php/class-wordads-smart.php | 132 ++++++++++-------- 2 files changed, 97 insertions(+), 77 deletions(-) diff --git a/projects/plugins/jetpack/modules/wordads/php/class-wordads-params.php b/projects/plugins/jetpack/modules/wordads/php/class-wordads-params.php index ece3e6488bc51..8707b1d43d871 100644 --- a/projects/plugins/jetpack/modules/wordads/php/class-wordads-params.php +++ b/projects/plugins/jetpack/modules/wordads/php/class-wordads-params.php @@ -71,6 +71,13 @@ class WordAds_Params { */ public $page_type_ipw; + /** + * Is this an AMP request? + * + * @var bool + */ + public $is_amp; + /** * Setup parameters for serving the ads * @@ -79,22 +86,24 @@ class WordAds_Params { public function __construct() { // WordAds setting => default. $settings = array( - 'wordads_approved' => false, - 'wordads_active' => false, - 'wordads_house' => true, - 'wordads_unsafe' => false, - 'enable_header_ad' => true, - 'wordads_second_belowpost' => true, - 'wordads_inline_enabled' => true, - 'wordads_display_front_page' => true, - 'wordads_display_post' => true, - 'wordads_display_page' => true, - 'wordads_display_archive' => true, - 'wordads_custom_adstxt' => '', - 'wordads_custom_adstxt_enabled' => false, - 'wordads_ccpa_enabled' => false, - 'wordads_ccpa_privacy_policy_url' => get_option( 'wp_page_for_privacy_policy' ) ? get_permalink( (int) get_option( 'wp_page_for_privacy_policy' ) ) : '', - 'wordads_cmp_enabled' => false, + 'wordads_approved' => false, + 'wordads_active' => false, + 'wordads_house' => true, + 'wordads_unsafe' => false, + 'enable_header_ad' => true, + 'wordads_second_belowpost' => true, + 'wordads_inline_enabled' => true, + 'wordads_bottom_sticky_enabled' => false, + 'wordads_sidebar_sticky_right_enabled' => false, + 'wordads_display_front_page' => true, + 'wordads_display_post' => true, + 'wordads_display_page' => true, + 'wordads_display_archive' => true, + 'wordads_custom_adstxt' => '', + 'wordads_custom_adstxt_enabled' => false, + 'wordads_ccpa_enabled' => false, + 'wordads_ccpa_privacy_policy_url' => get_option( 'wp_page_for_privacy_policy' ) ? get_permalink( (int) get_option( 'wp_page_for_privacy_policy' ) ) : '', + 'wordads_cmp_enabled' => false, ); // Grab settings, or set as default if it doesn't exist. @@ -136,6 +145,7 @@ public function __construct() { 'LangId' => str_contains( get_bloginfo( 'language' ), 'en' ) ? 1 : 0, // TODO something else? 'AdSafe' => 1, // TODO. ); + $this->is_amp = function_exists( 'amp_is_request' ) && amp_is_request(); } /** diff --git a/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php b/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php index a60da8234ef77..c884bd6d719f5 100644 --- a/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php +++ b/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php @@ -24,18 +24,11 @@ class WordAds_Smart { protected static $instance = null; /** - * Is this an AMP request? + * The parameters for WordAds. * - * @var bool + * @var WordAds_Params */ - private $is_amp; - - /** - * Current blog theme from get_stylesheet(). - * - * @var string - */ - private $theme; + private $params; /** * Has Smart asset been enqueued? @@ -44,13 +37,6 @@ class WordAds_Smart { */ private $is_asset_enqueued = false; - /** - * Toggle for inline ads. - * - * @var bool True if inline ads are enabled. - */ - private $is_inline_enabled; - /** * Supported formats. * @@ -99,8 +85,8 @@ private function __construct() { * * @return WordAds_Smart */ - public static function instance() { - if ( self::$instance === null ) { + public static function instance(): self { + if ( null === self::$instance ) { self::$instance = new self(); } return self::$instance; @@ -114,19 +100,12 @@ public static function instance() { * @return void */ public function init( WordAds_Params $params ) { - $this->is_amp = function_exists( 'amp_is_request' ) && amp_is_request(); - $this->theme = get_stylesheet(); - $this->is_inline_enabled = is_singular( 'post' ) && $params->options['wordads_inline_enabled']; + $this->params = $params; - // Allow enabled format override by query string. + $this->enable_formats(); $this->override_formats_from_query_string(); - // TODO: refactor to remove the need to do this - $this->is_inline_enabled = $this->formats['inline']['enabled']; - // TODO: Is this necessary? if we want to always run Smart insert ads logic after IPW migrations - $has_any_format_enabled = in_array( true, array_column( $this->formats, 'enabled' ), true ); - if ( $has_any_format_enabled ) { - // Insert ads. + if ( $this->has_any_format_enabled() ) { $this->insert_ads(); } } @@ -173,7 +152,7 @@ public function enqueue_assets() { * @return void */ private function insert_ads() { - if ( $this->is_amp ) { + if ( $this->params->is_amp ) { return; } @@ -182,13 +161,16 @@ private function insert_ads() { return; } + // Add the resource hints. + add_filter( 'wp_resource_hints', array( $this, 'resource_hints' ), 10, 2 ); + // Enqueue JS assets. $this->enqueue_assets(); $is_static_front_page = is_front_page() && 'page' === get_option( 'show_on_front' ); if ( ! ( $is_static_front_page || is_home() ) ) { - if ( $this->is_inline_enabled ) { + if ( $this->formats['inline']['enabled'] ) { add_filter( 'the_content', array( $this, 'insert_inline_marker' ), @@ -217,14 +199,9 @@ public function insert_config() { global $post; $config = array( - 'blog_id' => $this->get_blog_id(), 'post_id' => ( $post instanceof WP_Post ) && is_singular( 'post' ) ? $post->ID : null, - 'theme' => $this->theme, + 'theme' => get_stylesheet(), 'target' => $this->target_keywords(), - '_' => array( - 'title' => __( 'Advertisement', 'jetpack' ), - 'privacy_settings' => __( 'Privacy Settings', 'jetpack' ), - ), ) + $this->formats; // Do conversion. @@ -234,6 +211,22 @@ public function insert_config() { wp_print_inline_script_tag( "var wa_smart = $js_config; wa_smart.cmd = [];" ); } + /** + * Add the Smart resource hints. + * + * @param array $hints Domains for hinting. + * @param string $relation_type Resource type. + * + * @return array Domains for hinting. + */ + public function resource_hints( $hints, $relation_type ) { + if ( 'dns-prefetch' === $relation_type ) { + $hints[] = '//af.pubmine.com'; + } + + return $hints; + } + /** * Gets the URL to a JSONP endpoint with configuration data. * @@ -242,33 +235,19 @@ public function insert_config() { private function get_config_url(): string { return sprintf( 'https://public-api.wordpress.com/wpcom/v2/sites/%1$d/adflow/conf/?_jsonp=a8c_adflow_callback', - $this->get_blog_id() + $this->params->blog_id ); } - /** - * Allow format enabled override from query string, eg. ?inline=true. - * - * @return void - */ - private function override_formats_from_query_string(): void { - foreach ( $this->formats as $format_type => $_ ) { - // phpcs:disable WordPress.Security.NonceVerification.Recommended - if ( isset( $_GET[ $format_type ] ) && 'true' === $_GET[ $format_type ] ) { - $this->formats[ $format_type ]['enabled'] = true; - } - } - } - /** * Places marker at the end of the content so inline can identify the post content container. * * @param string|null $content The post content. * @return string|null The post content with the marker appended. */ - public function insert_inline_marker( $content ) { - if ( $content === null ) { - return $content; + public function insert_inline_marker( ?string $content ): ?string { + if ( null === $content ) { + return null; } $inline_ad_marker = ''; @@ -285,7 +264,6 @@ private function target_keywords(): string { $target_keywords = array_merge( $this->get_blog_keywords(), $this->get_language_keywords() - // TODO: Include categorization. ); return implode( ';', $target_keywords ); @@ -297,7 +275,7 @@ private function target_keywords(): string { * @return array The list of blog keywords. */ private function get_blog_keywords(): array { - return array( 'wp_blog_id=' . $this->get_blog_id() ); + return array( 'wp_blog_id=' . $this->params->blog_id ); } /** @@ -310,11 +288,43 @@ private function get_language_keywords(): array { } /** - * Gets the blog's ID. + * Enable formats by post types and the display options. + * + * @return void + */ + private function enable_formats(): void { + $this->formats['top']['enabled'] = $this->params->options['enable_header_ad']; + $this->formats['inline']['enabled'] = is_singular( 'post' ) && $this->params->options['wordads_inline_enabled']; + $this->formats['belowpost']['enabled'] = $this->params->should_show(); + $this->formats['bottom_sticky']['enabled'] = $this->params->options['wordads_bottom_sticky_enabled']; + $this->formats['sidebar_sticky_right']['enabled'] = $this->params->options['wordads_sidebar_sticky_right_enabled']; + } + + /** + * Allow format enabled override from query string, eg. ?inline=true. + * + * @return void + */ + private function override_formats_from_query_string(): void { + // phpcs:disable WordPress.Security.NonceVerification.Recommended + if ( ! isset( $_GET['wordads-logging'] ) ) { + return; + } + + foreach ( $this->formats as $format_type => $_ ) { + // phpcs:disable WordPress.Security.NonceVerification.Recommended + if ( isset( $_GET[ $format_type ] ) && 'true' === $_GET[ $format_type ] ) { + $this->formats[ $format_type ]['enabled'] = true; + } + } + } + + /** + * Check if has any format enabled. * - * @return int The blog's ID. + * @return bool True if enabled, false otherwise. */ - private function get_blog_id(): int { - return Jetpack::get_option( 'id', 0 ); + private function has_any_format_enabled(): bool { + return in_array( true, array_column( $this->formats, 'enabled' ), true ); } } From 228aa66d40658d5d67a4136d1f78b9e95025c1a4 Mon Sep 17 00:00:00 2001 From: Diogo Alves Date: Fri, 6 Dec 2024 16:03:25 -0300 Subject: [PATCH 18/22] [WordAds]: Render legacy sidebar widget with the WATL (#40462) * Render legacy sidebar widget with the WATL * Replace null with empty as the right hand side * Use the new Smarts for the sidebar widget * Remove redundant check * Move mapping array to constant * Update changelog --- .../update-iponweb-to-watl-migration | 1 + .../jetpack/modules/wordads/class-wordads.php | 2 +- .../php/class-wordads-sidebar-widget.php | 74 ++++++++++++++++--- .../wordads/php/class-wordads-smart.php | 28 ++++--- 4 files changed, 85 insertions(+), 20 deletions(-) diff --git a/projects/plugins/jetpack/changelog/update-iponweb-to-watl-migration b/projects/plugins/jetpack/changelog/update-iponweb-to-watl-migration index bd85e9ccf4478..57bf248955ab6 100644 --- a/projects/plugins/jetpack/changelog/update-iponweb-to-watl-migration +++ b/projects/plugins/jetpack/changelog/update-iponweb-to-watl-migration @@ -2,3 +2,4 @@ Significance: patch Type: other Migration from IPONWEB to WATL +* Render legacy sidebar widget with the WATL diff --git a/projects/plugins/jetpack/modules/wordads/class-wordads.php b/projects/plugins/jetpack/modules/wordads/class-wordads.php index 0d389e87c806e..9d796910a78aa 100644 --- a/projects/plugins/jetpack/modules/wordads/class-wordads.php +++ b/projects/plugins/jetpack/modules/wordads/class-wordads.php @@ -774,7 +774,7 @@ public function get_dynamic_ad_snippet( $section_id, $form_factor = 'square', $l // Allow overriding and printing of the tag parsed by the WATL. // phpcs:disable WordPress.Security.NonceVerification.Recommended - $is_location_enabled = ( isset( $_GET[ $location ] ) && 'true' === $_GET[ $location ] ); + $is_location_enabled = isset( $_GET['wordads-logging'] ) && isset( $_GET[ $location ] ) && 'true' === $_GET[ $location ]; if ( ( 'top' === $location || 'belowpost' === $location ) && $is_location_enabled ) { // TODO: Confirm if it's best here or there is a way to get it via the adflow config endpoint diff --git a/projects/plugins/jetpack/modules/wordads/php/class-wordads-sidebar-widget.php b/projects/plugins/jetpack/modules/wordads/php/class-wordads-sidebar-widget.php index 7b1b7b5f6de77..63cb75ceebe3b 100644 --- a/projects/plugins/jetpack/modules/wordads/php/class-wordads-sidebar-widget.php +++ b/projects/plugins/jetpack/modules/wordads/php/class-wordads-sidebar-widget.php @@ -19,6 +19,17 @@ class WordAds_Sidebar_Widget extends WP_Widget { */ private static $allowed_tags = array( 'mrec', 'wideskyscraper', 'leaderboard' ); + /** + * Mapping array of widget sizes with the WordAds_Smart formats. + * + * @var string[] + */ + private static $sizes_x_smart_format = array( + '300x250' => 'sidebar_widget_mediumrectangle', + '728x90' => 'sidebar_widget_leaderboard', + '160x600' => 'sidebar_widget_wideskyscraper', + ); + /** * Number of widgets. * @@ -74,7 +85,6 @@ public function widget( $args, $instance ) { } ++self::$num_widgets; - $about = __( 'Advertisements', 'jetpack' ); $width = WordAds::$ad_tag_ids[ $instance['unit'] ]['width']; $height = WordAds::$ad_tag_ids[ $instance['unit'] ]['height']; $unit_id = 1 === self::$num_widgets ? 3 : self::$num_widgets + 3; // 2nd belowpost is '4' @@ -82,7 +92,50 @@ public function widget( $args, $instance ) { WORDADS_API_TEST_ID : $wordads->params->blog_id . $unit_id; - $snippet = ''; + $smart_format = self::$sizes_x_smart_format[ "{$width}x{$height}" ]; + // phpcs:disable WordPress.Security.NonceVerification.Recommended + $is_watl_enabled = isset( $_GET['wordads-logging'] ) && isset( $_GET[ $smart_format ] ) && 'true' === $_GET[ $smart_format ]; + + // Get the widget snippet. + $widget_snippet = $this->get_widget_snippet( $instance, $section_id, $height, $width ); + + // Render the IPW or house ad if WATL is disabled. + if ( ! $is_watl_enabled ) { + // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped + echo $widget_snippet; + return; + } + + // Remove linebreaks and sanitize. + $tag = esc_js( str_replace( array( "\n", "\t", "\r" ), '', $widget_snippet ) ); + + // Add the fallback to be processed by WATL. + $fallback_snippet = << + var sas_fallback = sas_fallback || []; + sas_fallback.push( + { tag: "$tag", type: '$smart_format' } + ); + +HTML; + + // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped + echo $fallback_snippet . $wordads::get_watl_ad_html_tag( $smart_format ); + } + + /** + * The widget snippet. + * + * @param array $instance The widget instance. + * @param string $section_id The section id. + * @param int $height The ad height. + * @param int $width The ad width. + * + * @return string + */ + private function get_widget_snippet( $instance, $section_id, $height, $width ) { + global $wordads; + if ( $wordads->option( 'wordads_house', true ) ) { $unit = 'mrec'; if ( 'leaderboard' === $instance['unit'] && ! $this->params->mobile_device ) { @@ -93,21 +146,22 @@ public function widget( $args, $instance ) { $snippet = $wordads->get_house_ad( $unit ); } else { - // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped - echo $wordads->get_ad_snippet( $section_id, $height, $width, 'widget' ); - return; + return $wordads->get_ad_snippet( $section_id, $height, $width, 'widget' ); } - ?> + $about = __( 'Advertisements', 'jetpack' ); + $unit = esc_attr( $instance['unit'] ); + + return <<
- -
- + $about +
+ $snippet
- array( + 'top' => array( 'enabled' => false, ), - 'inline' => array( + 'inline' => array( 'enabled' => false, ), - 'belowpost' => array( + 'belowpost' => array( 'enabled' => false, ), - 'bottom_sticky' => array( + 'bottom_sticky' => array( 'enabled' => false, ), - 'sidebar_sticky_right' => array( + 'sidebar_sticky_right' => array( 'enabled' => false, ), - 'gutenberg_rectangle' => array( + 'gutenberg_rectangle' => array( 'enabled' => false, ), - 'gutenberg_leaderboard' => array( + 'gutenberg_leaderboard' => array( 'enabled' => false, ), - 'gutenberg_mobile_leaderboard' => array( + 'gutenberg_mobile_leaderboard' => array( 'enabled' => false, ), - 'gutenberg_skyscraper' => array( + 'gutenberg_skyscraper' => array( + 'enabled' => false, + ), + 'sidebar_widget_mediumrectangle' => array( + 'enabled' => false, + ), + 'sidebar_widget_leaderboard' => array( + 'enabled' => false, + ), + 'sidebar_widget_wideskyscraper' => array( 'enabled' => false, ), ); From c307dae4211f2b58191f2e463cc62c0caa64912e Mon Sep 17 00:00:00 2001 From: Al-Ameen Ogundiran Date: Wed, 18 Dec 2024 12:45:00 +0000 Subject: [PATCH 19/22] change gutenberg_ad_snippet_x_smart_format property type from public to private --- projects/plugins/jetpack/extensions/blocks/wordads/wordads.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projects/plugins/jetpack/extensions/blocks/wordads/wordads.php b/projects/plugins/jetpack/extensions/blocks/wordads/wordads.php index e19ccc8ad5399..9107adf5cd600 100644 --- a/projects/plugins/jetpack/extensions/blocks/wordads/wordads.php +++ b/projects/plugins/jetpack/extensions/blocks/wordads/wordads.php @@ -25,7 +25,7 @@ class WordAds { * * @var array */ - public static $gutenberg_ad_snippet_x_smart_format = array( + private static $gutenberg_ad_snippet_x_smart_format = array( 'gutenberg_300x250' => 'gutenberg_rectangle', 'gutenberg_728x90' => 'gutenberg_leaderboard', 'gutenberg_320x50' => 'gutenberg_mobile_leaderboard', From c59410b320a0df734674ed510fa578e0ca4f37d5 Mon Sep 17 00:00:00 2001 From: Al-Ameen Ogundiran Date: Thu, 19 Dec 2024 21:42:58 +0000 Subject: [PATCH 20/22] [WordAds] Migrate wordads shortcode to WATL (#40599) --- projects/plugins/jetpack/.phan/baseline.php | 1 - .../update-iponweb-to-watl-migration | 4 +-- .../extensions/blocks/wordads/wordads.php | 10 +++--- .../jetpack/modules/wordads/class-wordads.php | 17 ++++++++-- .../php/class-wordads-shortcode.php} | 33 +++++-------------- .../wordads/php/class-wordads-smart.php | 4 +++ 6 files changed, 35 insertions(+), 34 deletions(-) rename projects/plugins/jetpack/modules/{shortcodes/wordads.php => wordads/php/class-wordads-shortcode.php} (57%) diff --git a/projects/plugins/jetpack/.phan/baseline.php b/projects/plugins/jetpack/.phan/baseline.php index 741c58a413969..26ebb777f9d7f 100644 --- a/projects/plugins/jetpack/.phan/baseline.php +++ b/projects/plugins/jetpack/.phan/baseline.php @@ -380,7 +380,6 @@ 'modules/shortcodes/ustream.php' => ['PhanTypeMismatchArgument'], 'modules/shortcodes/vimeo.php' => ['PhanPluginDuplicateConditionalNullCoalescing', 'PhanTypeMismatchArgument'], 'modules/shortcodes/vr.php' => ['PhanPluginDuplicateConditionalNullCoalescing'], - 'modules/shortcodes/wordads.php' => ['PhanNoopNew'], 'modules/shortcodes/youtube.php' => ['PhanRedefineFunction'], 'modules/shortlinks.php' => ['PhanPluginDuplicateExpressionAssignmentOperation', 'PhanTypeMismatchArgumentInternal', 'PhanTypeMismatchArgumentProbablyReal'], 'modules/simple-payments/simple-payments.php' => ['PhanTypeMismatchArgument', 'PhanTypeMismatchReturn'], diff --git a/projects/plugins/jetpack/changelog/update-iponweb-to-watl-migration b/projects/plugins/jetpack/changelog/update-iponweb-to-watl-migration index 57bf248955ab6..c66b2abb063ef 100644 --- a/projects/plugins/jetpack/changelog/update-iponweb-to-watl-migration +++ b/projects/plugins/jetpack/changelog/update-iponweb-to-watl-migration @@ -1,5 +1,5 @@ Significance: patch Type: other -Migration from IPONWEB to WATL -* Render legacy sidebar widget with the WATL +Migration of ad formats from IPONWEB to WATL + diff --git a/projects/plugins/jetpack/extensions/blocks/wordads/wordads.php b/projects/plugins/jetpack/extensions/blocks/wordads/wordads.php index 9107adf5cd600..e1d4b6ce3b8cc 100644 --- a/projects/plugins/jetpack/extensions/blocks/wordads/wordads.php +++ b/projects/plugins/jetpack/extensions/blocks/wordads/wordads.php @@ -139,12 +139,12 @@ public static function gutenblock_render( $attr ) { $format = $attr['format']; } - $height = $ad_tag_ids[ $format ]['height']; - $width = $ad_tag_ids[ $format ]['width']; - $gutenberg_location = 'gutenberg'; - $snippet = $wordads->get_ad_snippet( $section_id, $height, $width, $gutenberg_location, $wordads->get_solo_unit_css() ); + $height = $ad_tag_ids[ $format ]['height']; + $width = $ad_tag_ids[ $format ]['width']; + $location = 'gutenberg'; + $snippet = $wordads->get_ad_snippet( $section_id, $height, $width, $location, $wordads->get_solo_unit_css() ); - $key = "{$gutenberg_location}_{$width}x{$height}"; + $key = "{$location}_{$width}x{$height}"; $smart_format = self::$gutenberg_ad_snippet_x_smart_format[ $key ] ?? null; // phpcs:disable WordPress.Security.NonceVerification.Recommended $is_watl_enabled = $smart_format && ( isset( $_GET[ $smart_format ] ) && 'true' === $_GET[ $smart_format ] ); diff --git a/projects/plugins/jetpack/modules/wordads/class-wordads.php b/projects/plugins/jetpack/modules/wordads/class-wordads.php index 9d796910a78aa..81e0ed53afd3e 100644 --- a/projects/plugins/jetpack/modules/wordads/class-wordads.php +++ b/projects/plugins/jetpack/modules/wordads/class-wordads.php @@ -21,6 +21,7 @@ require_once WORDADS_ROOT . '/php/class-wordads-ccpa-do-not-sell-link-widget.php'; require_once WORDADS_ROOT . '/php/class-wordads-consent-management-provider.php'; require_once WORDADS_ROOT . '/php/class-wordads-smart.php'; +require_once WORDADS_ROOT . '/php/class-wordads-shortcode.php'; /** * Primary WordAds class. @@ -215,6 +216,9 @@ public function init() { WordAds_Consent_Management_Provider::init(); } + // Initialize [wordads] shortcode. + WordAds_Shortcode::init(); + // Initialize Smart. WordAds_Smart::instance()->init( $this->params ); @@ -527,8 +531,17 @@ public function insert_inline_ad( $content ) { } $ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb'; - $content .= $this->get_ad( 'inline', $ad_type ); - return $content; + $location = 'shortcode'; + // not house ad and watl enabled + // phpcs:disable WordPress.Security.NonceVerification.Recommended + if ( 'house' !== $ad_type && ( isset( $_GET['wordads-logging'] ) && isset( $_GET[ $location ] ) && 'true' === $_GET[ $location ] ) ) { + return $content . $this->get_watl_ad_html_tag( $location ); + } + + return $content .= sprintf( + '
%s
', + $this->get_ad( 'inline', $ad_type ) + ); } /** diff --git a/projects/plugins/jetpack/modules/shortcodes/wordads.php b/projects/plugins/jetpack/modules/wordads/php/class-wordads-shortcode.php similarity index 57% rename from projects/plugins/jetpack/modules/shortcodes/wordads.php rename to projects/plugins/jetpack/modules/wordads/php/class-wordads-shortcode.php index 5f91213e3a241..8a291771da916 100644 --- a/projects/plugins/jetpack/modules/shortcodes/wordads.php +++ b/projects/plugins/jetpack/modules/wordads/php/class-wordads-shortcode.php @@ -1,4 +1,4 @@ -' . __( 'The WordAds module is not active', 'jetpack' ) . ''; } - $html = '
'; - $html = $wordads->insert_inline_ad( $html ); + $html = $wordads->insert_inline_ad( '' ); return $html; } } - -new Jetpack_WordAds_Shortcode(); diff --git a/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php b/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php index 281b57cc6b3bc..3d1be487365d2 100644 --- a/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php +++ b/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php @@ -80,6 +80,9 @@ class WordAds_Smart { 'sidebar_widget_wideskyscraper' => array( 'enabled' => false, ), + 'shortcode' => array( + 'enabled' => false, + ), ); /** @@ -210,6 +213,7 @@ public function insert_config() { $config = array( 'post_id' => ( $post instanceof WP_Post ) && is_singular( 'post' ) ? $post->ID : null, + 'origin' => 'jetpack', 'theme' => get_stylesheet(), 'target' => $this->target_keywords(), ) + $this->formats; From 2969577144b6a7986e7f311e72d29efc122ca59c Mon Sep 17 00:00:00 2001 From: Al-Ameen Ogundiran Date: Thu, 26 Dec 2024 12:37:11 +0000 Subject: [PATCH 21/22] remove todo text --- projects/plugins/jetpack/modules/wordads/class-wordads.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/projects/plugins/jetpack/modules/wordads/class-wordads.php b/projects/plugins/jetpack/modules/wordads/class-wordads.php index 81e0ed53afd3e..d148628e0d46c 100644 --- a/projects/plugins/jetpack/modules/wordads/class-wordads.php +++ b/projects/plugins/jetpack/modules/wordads/class-wordads.php @@ -766,7 +766,6 @@ public function get_ad_snippet( $section_id, $height, $width, $location = '', $c $form_factor = 'leaderboard'; } - // TODO: Still investigating required change. Potentially update with respect with what get_ad_div on WPCOM for gutenberg return $this->get_dynamic_ad_snippet( $section_id, $form_factor, $location ); } @@ -790,7 +789,6 @@ public function get_dynamic_ad_snippet( $section_id, $form_factor = 'square', $l $is_location_enabled = isset( $_GET['wordads-logging'] ) && isset( $_GET[ $location ] ) && 'true' === $_GET[ $location ]; if ( ( 'top' === $location || 'belowpost' === $location ) && $is_location_enabled ) { - // TODO: Confirm if it's best here or there is a way to get it via the adflow config endpoint return self::get_watl_ad_html_tag( $location ); } From 664e77c74437f7dfd58655975f41a05aeae58e7e Mon Sep 17 00:00:00 2001 From: Diogo Alves Date: Thu, 26 Dec 2024 15:19:41 -0300 Subject: [PATCH 22/22] [WordAds]: Clean-up the wordads shortcode class and restore the old behavior (#40733) * Add missing period * Remove unnecessary attribution * Clean up when handle wa shortcode * Restore the old behavior when printing the div tag --- .../jetpack/modules/wordads/class-wordads.php | 8 +++---- .../wordads/php/class-wordads-shortcode.php | 23 +++---------------- 2 files changed, 6 insertions(+), 25 deletions(-) diff --git a/projects/plugins/jetpack/modules/wordads/class-wordads.php b/projects/plugins/jetpack/modules/wordads/class-wordads.php index d148628e0d46c..c02922d44d055 100644 --- a/projects/plugins/jetpack/modules/wordads/class-wordads.php +++ b/projects/plugins/jetpack/modules/wordads/class-wordads.php @@ -532,16 +532,14 @@ public function insert_inline_ad( $content ) { $ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb'; $location = 'shortcode'; - // not house ad and watl enabled + + // Not house ad and WATL enabled. // phpcs:disable WordPress.Security.NonceVerification.Recommended if ( 'house' !== $ad_type && ( isset( $_GET['wordads-logging'] ) && isset( $_GET[ $location ] ) && 'true' === $_GET[ $location ] ) ) { return $content . $this->get_watl_ad_html_tag( $location ); } - return $content .= sprintf( - '
%s
', - $this->get_ad( 'inline', $ad_type ) - ); + return $content . $this->get_ad( 'inline', $ad_type ); } /** diff --git a/projects/plugins/jetpack/modules/wordads/php/class-wordads-shortcode.php b/projects/plugins/jetpack/modules/wordads/php/class-wordads-shortcode.php index 8a291771da916..0f7a9a5d48ec5 100644 --- a/projects/plugins/jetpack/modules/wordads/php/class-wordads-shortcode.php +++ b/projects/plugins/jetpack/modules/wordads/php/class-wordads-shortcode.php @@ -32,34 +32,17 @@ public static function init() { * Our [wordads] shortcode. * Prints a WordAds Ad. * - * @param array $atts Array of shortcode attributes. - * @param string $content Post content. - * * @return string HTML for WordAds shortcode. */ - public static function handle_wordads_shortcode( $atts, $content = '' ) { - $atts = shortcode_atts( array(), $atts, 'wordads' ); - - return self::wordads_shortcode_html( $atts, $content ); - } - - /** - * The shortcode output - * - * @param array $atts Array of shortcode attributes. - * @param string $content Post content. - * - * @return string HTML output - */ - private static function wordads_shortcode_html( $atts, $content = '' ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable + public static function handle_wordads_shortcode() { global $wordads; if ( empty( $wordads ) ) { return '
' . __( 'The WordAds module is not active', 'jetpack' ) . '
'; } - $html = $wordads->insert_inline_ad( '' ); + $html = '
'; - return $html; + return $wordads->insert_inline_ad( $html ); } }