From e67900146c5c32a1923ce75398a0d6fd0e23371b Mon Sep 17 00:00:00 2001 From: Al-Ameen Ogundiran Date: Tue, 26 Nov 2024 12:01:50 +0000 Subject: [PATCH] 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 ), );