Skip to content

Commit

Permalink
implement watl placement for gutenberg ad formats
Browse files Browse the repository at this point in the history
  • Loading branch information
Alameen688 committed Nov 26, 2024
1 parent 64f1cf5 commit e679001
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 14 deletions.
34 changes: 30 additions & 4 deletions projects/plugins/jetpack/extensions/blocks/wordads/wordads.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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 );
}
}

Expand Down
17 changes: 15 additions & 2 deletions projects/plugins/jetpack/modules/wordads/class-wordads.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<div class=\"wordads-tag\" data-slot-type=\"$location\" style=\"display: none;\"></div>";
return self::get_watl_ad_html_tag( $location );
}

return $this->get_fallback_ad_snippet( $section_id, $form_factor, $location, $relocate, $id );
Expand Down Expand Up @@ -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 "<div class=\"wordads-tag\" data-slot-type=\"$slot_type\" style=\"display: none;\"></div>";
}

/**
* Activation hook actions
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
),
);
Expand Down

0 comments on commit e679001

Please sign in to comment.