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

[WordAds]: Ad formats migration to WATL #40308

Draft
wants to merge 15 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: other

Migration from IPONWEB to WATL
45 changes: 41 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,35 @@ 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;
}

// Remove linebreaks and sanitize.
$snippet = esc_js( str_replace( array( "\n", "\t", "\r" ), '', $ad_div ) );

// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
$fallback_snippet = <<<HTML
<script>
var sas_fallback = sas_fallback || [];
sas_fallback.push(
{ tag: "$snippet", type: '$smart_format' }
);
</script>
HTML;

return $fallback_snippet . $wordads->get_watl_ad_html_tag( $smart_format );
}
}

Expand Down
59 changes: 58 additions & 1 deletion projects/plugins/jetpack/modules/wordads/class-wordads.php
Original file line number Diff line number Diff line change
Expand Up @@ -381,18 +381,34 @@ public function insert_head_meta() {
</script>
<?php

$section_id = $this->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_dynamic_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 ) );

// 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 <<<HTML
<script>
var sas_fallback = sas_fallback || [];
sas_fallback.push(
{ tag: "$tag_inline", type: 'inline' }
{ tag: "$tag_belowpost", type: 'belowpost' }
{ tag: "$tag_top", type: 'top' }
);
</script>
HTML;
Expand Down Expand Up @@ -703,6 +719,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 );
}

Expand All @@ -720,6 +737,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 ) && $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 );
}

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 );

Expand Down Expand Up @@ -868,6 +912,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 @@ -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.
*/
Expand Down Expand Up @@ -83,12 +118,14 @@ 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;
}
if ( $this->is_inline_enabled ) {
// 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'];

// 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();
}
Expand Down Expand Up @@ -178,10 +215,7 @@ public function insert_config() {
'title' => __( 'Advertisement', 'jetpack' ),
'privacy_settings' => __( 'Privacy Settings', 'jetpack' ),
),
'inline' => array(
'enabled' => $this->is_inline_enabled,
),
);
) + $this->formats;

// Do conversion.
$js_config = WordAds_Array_Utils::array_to_js_object( $config );
Expand All @@ -202,6 +236,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.
*
Expand Down
Loading