Skip to content

Commit

Permalink
[WordAds]: Update Smart class to handle new formats (#40431)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
diogoca authored and Alameen688 committed Dec 10, 2024
1 parent 663209b commit 1d0839f
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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.
Expand Down Expand Up @@ -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();
}

/**
Expand Down
132 changes: 71 additions & 61 deletions projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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.
*
Expand Down Expand Up @@ -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;
Expand All @@ -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();
}
}
Expand Down Expand Up @@ -173,7 +152,7 @@ public function enqueue_assets() {
* @return void
*/
private function insert_ads() {
if ( $this->is_amp ) {
if ( $this->params->is_amp ) {
return;
}

Expand All @@ -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' ),
Expand Down Expand Up @@ -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.
Expand All @@ -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.
*
Expand All @@ -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 = '<span id="wordads-inline-marker" style="display: none;"></span>';

Expand All @@ -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 );
Expand All @@ -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 );
}

/**
Expand All @@ -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 );
}
}

0 comments on commit 1d0839f

Please sign in to comment.