Skip to content

Commit

Permalink
[WordAds]: Render legacy sidebar widget with the WATL (#40462)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
diogoca authored and Alameen688 committed Dec 10, 2024
1 parent 1d0839f commit a9933e9
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ Significance: patch
Type: other

Migration from IPONWEB to WATL
* Render legacy sidebar widget with the WATL
2 changes: 1 addition & 1 deletion projects/plugins/jetpack/modules/wordads/class-wordads.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -74,15 +85,57 @@ 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'
$section_id = 0 === $wordads->params->blog_id ?
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 = <<<HTML
<script type="text/javascript">
var sas_fallback = sas_fallback || [];
sas_fallback.push(
{ tag: "$tag", type: '$smart_format' }
);
</script>
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 ) {
Expand All @@ -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 <<<HTML
<div class="wpcnt">
<div class="wpa">
<span class="wpa-about"><?php echo esc_html( $about ); ?></span>
<div class="u <?php echo esc_attr( $instance['unit'] ); ?>">
<?php echo $snippet; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<span class="wpa-about">$about</span>
<div class="u $unit">
$snippet
</div>
</div>
</div>
<?php
HTML;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,35 +39,45 @@ class WordAds_Smart {

/**
* Supported formats.
* sidebar_widget formats represents the legacy Jetpack sidebar widget.
*
* @var array
*/
private $formats = array(
'top' => 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,
),
);
Expand Down

0 comments on commit a9933e9

Please sign in to comment.