From cf55949705bfc55ba1b092965c2d043f8ada1124 Mon Sep 17 00:00:00 2001 From: Collyn Philleo Date: Mon, 29 Apr 2024 17:40:17 -0700 Subject: [PATCH 1/5] Add support for inline Blaze ads with IPONWEB fallback --- projects/packages/sync/src/class-defaults.php | 1 + .../plugins/jetpack/_inc/client/earn/ads.jsx | 15 ++ .../lib/class.core-rest-api-endpoints.php | 7 + .../jetpack/modules/wordads/class-wordads.php | 34 ++- .../modules/wordads/js/adflow-loader.js | 21 ++ .../wordads/php/class-wordads-array-utils.php | 67 +++++ .../wordads/php/class-wordads-params.php | 1 + .../wordads/php/class-wordads-smart.php | 255 ++++++++++++++++++ .../sync/test_class.jetpack-sync-options.php | 3 +- 9 files changed, 397 insertions(+), 7 deletions(-) create mode 100644 projects/plugins/jetpack/modules/wordads/js/adflow-loader.js create mode 100644 projects/plugins/jetpack/modules/wordads/php/class-wordads-array-utils.php create mode 100644 projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php diff --git a/projects/packages/sync/src/class-defaults.php b/projects/packages/sync/src/class-defaults.php index dfbe6939d1d12..eaa3354fb954b 100644 --- a/projects/packages/sync/src/class-defaults.php +++ b/projects/packages/sync/src/class-defaults.php @@ -177,6 +177,7 @@ class Defaults { 'wordads_display_page', 'wordads_display_post', 'wordads_second_belowpost', + 'wordads_inline_enabled', 'woocommerce_custom_orders_table_enabled', 'wp_mobile_app_promos', 'wp_mobile_excerpt', diff --git a/projects/plugins/jetpack/_inc/client/earn/ads.jsx b/projects/plugins/jetpack/_inc/client/earn/ads.jsx index 4aeafe1018dd5..4a43e6ae1165e 100644 --- a/projects/plugins/jetpack/_inc/client/earn/ads.jsx +++ b/projects/plugins/jetpack/_inc/client/earn/ads.jsx @@ -123,6 +123,10 @@ export const Ads = withModuleSettingsFormHelpers( 'wordads_second_belowpost', 'wordads' ); + const wordads_inline_enabled = this.props.getOptionValue( + 'wordads_inline_enabled', + 'wordads' + ); const wordads_display_front_page = this.props.getOptionValue( 'wordads_display_front_page', 'wordads' @@ -270,6 +274,17 @@ export const Ads = withModuleSettingsFormHelpers( onChange={ this.handleChange( 'wordads_second_belowpost' ) } label={ __( 'Second ad below post', 'jetpack' ) } /> + __CLASS__ . '::validate_boolean', 'jp_group' => 'wordads', ), + 'wordads_inline_enabled' => array( + 'description' => esc_html__( 'Display inline ad within post content?', 'jetpack' ), + 'type' => 'boolean', + 'default' => 1, + 'validate_callback' => __CLASS__ . '::validate_boolean', + 'jp_group' => 'wordads', + ), 'wordads_display_front_page' => array( 'description' => esc_html__( 'Display ads on the front page?', 'jetpack' ), 'type' => 'boolean', diff --git a/projects/plugins/jetpack/modules/wordads/class-wordads.php b/projects/plugins/jetpack/modules/wordads/class-wordads.php index 0fbf5f8021dff..5e2e169387725 100644 --- a/projects/plugins/jetpack/modules/wordads/class-wordads.php +++ b/projects/plugins/jetpack/modules/wordads/class-wordads.php @@ -20,6 +20,7 @@ require_once WORDADS_ROOT . '/php/class-wordads-california-privacy.php'; require_once WORDADS_ROOT . '/php/class-wordads-ccpa-do-not-sell-link-widget.php'; require_once WORDADS_ROOT . '/php/class-wordads-consent-management-provider.php'; +require_once WORDADS_ROOT . '/php/class-wordads-smart.php'; /** * Primary WordAds class. @@ -214,6 +215,9 @@ public function init() { WordAds_Consent_Management_Provider::init(); } + // Initialize Smart. + new WordAds_Smart( $this->params ); + if ( ( isset( $_SERVER['REQUEST_URI'] ) && '/ads.txt' === $_SERVER['REQUEST_URI'] ) || ( site_url( 'ads.txt', 'relative' ) === $_SERVER['REQUEST_URI'] ) ) { @@ -376,6 +380,22 @@ public function insert_head_meta() { __ATA.criteo.cmd = __ATA.criteo.cmd || []; get_dynamic_ad_snippet( $this->params->blog_id . 5, 'square', 'inline', '', '{{unique_id}}' ); + + // Remove linebreaks and sanitize. + $tag_inline = esc_js( str_replace( array( "\n", "\t", "\r" ), '', $tag_inline ) ); + + // phpcs:disable WordPress.Security.EscapeOutput.HeredocOutputNotEscaped + echo << + var sas_fallback = sas_fallback || []; + sas_fallback.push( + { tag: "$tag_inline", type: 'inline' } + ); + +HTML; } /** @@ -689,16 +709,18 @@ public function get_ad_snippet( $section_id, $height, $width, $location = '', $c /** * Returns the 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 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_dynamic_ad_snippet( $section_id, $form_factor = 'square', $location = '', $relocate = '' ) { - $div_id = 'atatags-' . $section_id . '-' . uniqid(); + public function get_dynamic_ad_snippet( $section_id, $form_factor = 'square', $location = '', $relocate = '', $id = null ) { + $div_id = 'atatags-' . $section_id . '-' . ( $id ?? uniqid() ); $div_id = esc_attr( $div_id ); // Default form factor. diff --git a/projects/plugins/jetpack/modules/wordads/js/adflow-loader.js b/projects/plugins/jetpack/modules/wordads/js/adflow-loader.js new file mode 100644 index 0000000000000..67d51d14f3a3e --- /dev/null +++ b/projects/plugins/jetpack/modules/wordads/js/adflow-loader.js @@ -0,0 +1,21 @@ +function a8c_adflow_callback( data ) { + if ( data && data.scripts && Array.isArray( data.scripts ) ) { + if ( data.config ) { + let configurationScript = document.createElement( 'script' ); + configurationScript.id = 'adflow-configuration'; + configurationScript.type = 'application/configuration'; + configurationScript.innerHTML = JSON.stringify( data.config ); + + // Add the adflow-configuration script element to the document's body. + document.head.appendChild( configurationScript ); + } + + // Load each adflow script. + data.scripts.forEach( function ( scriptUrl ) { + let script = document.createElement( 'script' ); + script.src = scriptUrl; + document.head.appendChild( script ); + } ); + } +} +window.a8c_adflow_callback = a8c_adflow_callback; diff --git a/projects/plugins/jetpack/modules/wordads/php/class-wordads-array-utils.php b/projects/plugins/jetpack/modules/wordads/php/class-wordads-array-utils.php new file mode 100644 index 0000000000000..fddf72b1b8626 --- /dev/null +++ b/projects/plugins/jetpack/modules/wordads/php/class-wordads-array-utils.php @@ -0,0 +1,67 @@ + $v ) { + // Don't set property key for values from non-associative array. + $property_key = $in_list ? '' : "'$k': "; + + if ( is_array( $v ) ) { + // Check for empty array. + if ( array() === $v ) { + $properties[] = "'$k': []"; + continue; + } + + // Check if this is a list and not an associative array. + if ( array_keys( $v ) === range( 0, count( $v ) - 1 ) ) { + // Apply recursively. + $properties[] = $property_key . '[ ' . self::array_to_js_object( $v, true ) . ' ]'; + } else { + // Apply recursively. + $properties[] = $property_key . self::array_to_js_object( $v ); + } + } elseif ( is_string( $v ) && strpos( $v, 'js:' ) === 0 ) { + // JS code. Strip the 'js:' prefix. + $properties[] = $property_key . substr( $v, 3 ); + } elseif ( is_string( $v ) ) { + $properties[] = $property_key . "'" . addcslashes( $v, "'" ) . "'"; + } elseif ( is_bool( $v ) ) { + $properties[] = $property_key . ( $v ? 'true' : 'false' ); + } elseif ( $v === null ) { + $properties[] = $property_key . 'null'; + } else { + $properties[] = $property_key . $v; + } + } + + $output = implode( ', ', $properties ); + + if ( ! $in_list ) { + $output = '{ ' . $output . ' }'; + } + + return $output; + } +} diff --git a/projects/plugins/jetpack/modules/wordads/php/class-wordads-params.php b/projects/plugins/jetpack/modules/wordads/php/class-wordads-params.php index feddfda80d51a..ece3e6488bc51 100644 --- a/projects/plugins/jetpack/modules/wordads/php/class-wordads-params.php +++ b/projects/plugins/jetpack/modules/wordads/php/class-wordads-params.php @@ -85,6 +85,7 @@ public function __construct() { '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, diff --git a/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php b/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php new file mode 100644 index 0000000000000..a4abe0684cfef --- /dev/null +++ b/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php @@ -0,0 +1,255 @@ +params = $params; + $this->is_amp = function_exists( 'is_amp_endpoint' ) && is_amp_endpoint(); + $this->theme = get_stylesheet(); + $this->is_inline_enabled = is_singular( 'post' ) && $this->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 ) { + // Insert ads. + $this->insert_ads(); + } + } + + /** + * Enqueue any front-end CSS and JS. + * + * @return void + */ + public function enqueue_assets() { + + if ( $this->is_asset_enqueued ) { + return; + } + + add_action( 'wp_head', array( $this, 'insert_config' ) ); + + Assets::register_script( + 'adflow_script_loader', + '_inc/build/wordads/js/adflow-loader.min.js', + JETPACK__PLUGIN_FILE, + array( + 'nonmin_path' => 'modules/wordads/js/adflow-loader.js', + 'dependencies' => array(), + 'enqueue' => true, + 'version' => JETPACK__VERSION, + ) + ); + + wp_enqueue_script( + 'adflow_config', + esc_url( $this->get_config_url() ), + array( 'adflow_script_loader' ), + JETPACK__VERSION, + false + ); + + $this->is_asset_enqueued = true; + } + + /** + * Inserts ad tags on the page. + * + * @return void + */ + public function insert_ads() { + if ( $this->is_amp ) { + return; + } + + // Don't run on not found pages. + if ( is_404() ) { + return; + } + + // 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 ) { + add_filter( + 'the_content', + function ( $content ) { + return $this->insert_inline_marker( $content ); + }, + 10 + ); + } + } + } + + /** + * Inserts JS configuration used by watl.js. + * + * @return void + */ + 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( + 'title' => __( 'Advertisement', 'jetpack' ), + 'privacy_settings' => __( 'Privacy Settings', 'jetpack' ), + ), + 'inline' => array( + 'enabled' => $this->is_inline_enabled, + ), + ); + + // Do conversion. + $js_config = WordAds_Array_Utils::array_to_js_object( $config ); + + // Output script. + wp_print_inline_script_tag( "var wa_smart = $js_config; wa_smart.cmd = [];" ); + } + + /** + * Gets the URL to a JSONP endpoint with configuration data. + * + * @return string The URL. + */ + private function get_config_url(): string { + return sprintf( + 'https://public-api.wordpress.com/wpcom/v2/sites/%1$d/adflow/conf/?_jsonp=a8c_adflow_callback', + (int) Jetpack_Options::get_option( 'id' ) + ); + } + + /** + * Places marker at the end of the content so inline can identify the post content container. + * + * @param string $content The post content. + * @return string The post content with the marker appended. + */ + private function insert_inline_marker( string $content ): string { + $inline_ad_marker = ''; + + // Append the ad to the post content. + return $content . $inline_ad_marker; + } + + /** + * Gets a formatted list of target keywords. + * + * @return string Formatted list of target keywords. + */ + private function target_keywords(): string { + $target_keywords = array_merge( + $this->get_blog_keywords(), + $this->get_language_keywords(), + $this->get_feature_keywords() + // TODO: Include categorization. + ); + + return implode( ';', $target_keywords ); + } + + /** + * Gets a formatted list of blog keywords. + * + * @return array The list of blog keywords. + */ + private function get_blog_keywords(): array { + return array( 'wp_blog_id=' . $this->get_blog_id() ); + } + + /** + * Gets the site language formatted as a keyword. + * + * @return array The language as a keyword. + */ + private function get_language_keywords(): array { + return array( 'language=' . explode( '-', get_locale() )[0] ); + } + + /** + * Gets a formatted list of feature keywords. + * + * @return array The list of feature keywords. + */ + private function get_feature_keywords(): array { + $feature_keywords = array(); + + $feature_keywords[] = 'adflow=true'; + + return $feature_keywords; + } + + /** + * Gets the blog's ID. + * + * @return int The blog's ID. + */ + private function get_blog_id(): int { + return Jetpack::get_option( 'id', 0 ); + } +} diff --git a/projects/plugins/jetpack/tests/php/sync/test_class.jetpack-sync-options.php b/projects/plugins/jetpack/tests/php/sync/test_class.jetpack-sync-options.php index 1cece20389f61..8214bc97240d8 100644 --- a/projects/plugins/jetpack/tests/php/sync/test_class.jetpack-sync-options.php +++ b/projects/plugins/jetpack/tests/php/sync/test_class.jetpack-sync-options.php @@ -206,6 +206,8 @@ public function test_sync_default_options() { 'wp_page_for_privacy_policy' => false, 'enable_header_ad' => '1', 'wordads_second_belowpost' => '1', + 'wordads_inline_enabled' => true, + 'wordads_cmp_enabled' => false, 'wordads_display_front_page' => '1', 'wordads_display_post' => '1', 'wordads_display_page' => '1', @@ -214,7 +216,6 @@ public function test_sync_default_options() { 'wordads_custom_adstxt_enabled' => false, 'wordads_ccpa_enabled' => false, 'wordads_ccpa_privacy_policy_url' => 'pineapple', - 'wordads_cmp_enabled' => false, 'woocommerce_custom_orders_table_enabled' => false, 'site_user_type' => wp_json_encode( array( 1 => 'pineapple' ) ), 'site_segment' => 'pineapple', From 0977215f7a92b10ef1348290759cbbefa53fb18f Mon Sep 17 00:00:00 2001 From: Collyn Philleo Date: Wed, 1 May 2024 17:52:40 -0700 Subject: [PATCH 2/5] changelog --- projects/packages/sync/changelog/add-wordads-blaze | 4 ++++ projects/plugins/jetpack/changelog/add-wordads-blaze | 4 ++++ 2 files changed, 8 insertions(+) create mode 100644 projects/packages/sync/changelog/add-wordads-blaze create mode 100644 projects/plugins/jetpack/changelog/add-wordads-blaze diff --git a/projects/packages/sync/changelog/add-wordads-blaze b/projects/packages/sync/changelog/add-wordads-blaze new file mode 100644 index 0000000000000..771c0d8f8ded2 --- /dev/null +++ b/projects/packages/sync/changelog/add-wordads-blaze @@ -0,0 +1,4 @@ +Significance: minor +Type: added + +Options: sync WordAds inline ads toggle option diff --git a/projects/plugins/jetpack/changelog/add-wordads-blaze b/projects/plugins/jetpack/changelog/add-wordads-blaze new file mode 100644 index 0000000000000..06330ceffb4e3 --- /dev/null +++ b/projects/plugins/jetpack/changelog/add-wordads-blaze @@ -0,0 +1,4 @@ +Significance: minor +Type: enhancement + +WordAds: add inline ads within post content. From 82ee8494516465f67d9bd7d5990dbbbe8215afb6 Mon Sep 17 00:00:00 2001 From: Collyn Philleo Date: Thu, 2 May 2024 12:00:54 -0700 Subject: [PATCH 3/5] Minor fixes --- .../wordads/php/class-wordads-smart.php | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) 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 a4abe0684cfef..d7048d69559f2 100644 --- a/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php +++ b/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php @@ -180,7 +180,7 @@ 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', - (int) Jetpack_Options::get_option( 'id' ) + $this->get_blog_id() ); } @@ -205,8 +205,7 @@ private function insert_inline_marker( string $content ): string { private function target_keywords(): string { $target_keywords = array_merge( $this->get_blog_keywords(), - $this->get_language_keywords(), - $this->get_feature_keywords() + $this->get_language_keywords() // TODO: Include categorization. ); @@ -231,19 +230,6 @@ private function get_language_keywords(): array { return array( 'language=' . explode( '-', get_locale() )[0] ); } - /** - * Gets a formatted list of feature keywords. - * - * @return array The list of feature keywords. - */ - private function get_feature_keywords(): array { - $feature_keywords = array(); - - $feature_keywords[] = 'adflow=true'; - - return $feature_keywords; - } - /** * Gets the blog's ID. * From cf73824e520daef0003d707d2d08dc29f40a98be Mon Sep 17 00:00:00 2001 From: Collyn Philleo Date: Thu, 2 May 2024 14:51:14 -0700 Subject: [PATCH 4/5] Run fixup-project-versions.sh --- projects/packages/sync/composer.json | 2 +- projects/packages/sync/src/class-package-version.php | 2 +- .../changelog/add-wordads-blaze | 5 +++++ .../plugins/automattic-for-agencies-client/composer.lock | 4 ++-- projects/plugins/backup/changelog/add-wordads-blaze | 5 +++++ projects/plugins/backup/composer.lock | 4 ++-- projects/plugins/boost/changelog/add-wordads-blaze | 5 +++++ projects/plugins/boost/composer.lock | 4 ++-- projects/plugins/jetpack/changelog/add-wordads-blaze#2 | 5 +++++ projects/plugins/jetpack/composer.lock | 4 ++-- projects/plugins/migration/changelog/add-wordads-blaze | 5 +++++ projects/plugins/migration/composer.lock | 4 ++-- projects/plugins/mu-wpcom-plugin/changelog/add-wordads-blaze | 5 +++++ projects/plugins/mu-wpcom-plugin/composer.lock | 4 ++-- projects/plugins/protect/changelog/add-wordads-blaze | 5 +++++ projects/plugins/protect/composer.lock | 4 ++-- projects/plugins/search/changelog/add-wordads-blaze | 5 +++++ projects/plugins/search/composer.lock | 4 ++-- projects/plugins/social/changelog/add-wordads-blaze | 5 +++++ projects/plugins/social/composer.lock | 4 ++-- projects/plugins/starter-plugin/changelog/add-wordads-blaze | 5 +++++ projects/plugins/starter-plugin/composer.lock | 4 ++-- projects/plugins/videopress/changelog/add-wordads-blaze | 5 +++++ projects/plugins/videopress/composer.lock | 4 ++-- 24 files changed, 79 insertions(+), 24 deletions(-) create mode 100644 projects/plugins/automattic-for-agencies-client/changelog/add-wordads-blaze create mode 100644 projects/plugins/backup/changelog/add-wordads-blaze create mode 100644 projects/plugins/boost/changelog/add-wordads-blaze create mode 100644 projects/plugins/jetpack/changelog/add-wordads-blaze#2 create mode 100644 projects/plugins/migration/changelog/add-wordads-blaze create mode 100644 projects/plugins/mu-wpcom-plugin/changelog/add-wordads-blaze create mode 100644 projects/plugins/protect/changelog/add-wordads-blaze create mode 100644 projects/plugins/search/changelog/add-wordads-blaze create mode 100644 projects/plugins/social/changelog/add-wordads-blaze create mode 100644 projects/plugins/starter-plugin/changelog/add-wordads-blaze create mode 100644 projects/plugins/videopress/changelog/add-wordads-blaze diff --git a/projects/packages/sync/composer.json b/projects/packages/sync/composer.json index b07158ece2d27..bb60c6383560f 100644 --- a/projects/packages/sync/composer.json +++ b/projects/packages/sync/composer.json @@ -60,7 +60,7 @@ "link-template": "https://github.com/Automattic/jetpack-sync/compare/v${old}...v${new}" }, "branch-alias": { - "dev-trunk": "2.15.x-dev" + "dev-trunk": "2.16.x-dev" }, "dependencies": { "test-only": [ diff --git a/projects/packages/sync/src/class-package-version.php b/projects/packages/sync/src/class-package-version.php index dd903b261717f..9005586a9f91f 100644 --- a/projects/packages/sync/src/class-package-version.php +++ b/projects/packages/sync/src/class-package-version.php @@ -12,7 +12,7 @@ */ class Package_Version { - const PACKAGE_VERSION = '2.15.1'; + const PACKAGE_VERSION = '2.16.0-alpha'; const PACKAGE_SLUG = 'sync'; diff --git a/projects/plugins/automattic-for-agencies-client/changelog/add-wordads-blaze b/projects/plugins/automattic-for-agencies-client/changelog/add-wordads-blaze new file mode 100644 index 0000000000000..9aa70e3ec1f75 --- /dev/null +++ b/projects/plugins/automattic-for-agencies-client/changelog/add-wordads-blaze @@ -0,0 +1,5 @@ +Significance: patch +Type: changed +Comment: Updated composer.lock. + + diff --git a/projects/plugins/automattic-for-agencies-client/composer.lock b/projects/plugins/automattic-for-agencies-client/composer.lock index b4c5681627cb9..5ad8e0bfb8dfa 100644 --- a/projects/plugins/automattic-for-agencies-client/composer.lock +++ b/projects/plugins/automattic-for-agencies-client/composer.lock @@ -1043,7 +1043,7 @@ "dist": { "type": "path", "url": "../../packages/sync", - "reference": "6c08210f41958cbad899f8cf8f92435b91eaa932" + "reference": "4415298e32ada09dc9880eb8d4ec5b979efa4889" }, "require": { "automattic/jetpack-connection": "@dev", @@ -1077,7 +1077,7 @@ "link-template": "https://github.com/Automattic/jetpack-sync/compare/v${old}...v${new}" }, "branch-alias": { - "dev-trunk": "2.15.x-dev" + "dev-trunk": "2.16.x-dev" }, "dependencies": { "test-only": [ diff --git a/projects/plugins/backup/changelog/add-wordads-blaze b/projects/plugins/backup/changelog/add-wordads-blaze new file mode 100644 index 0000000000000..9aa70e3ec1f75 --- /dev/null +++ b/projects/plugins/backup/changelog/add-wordads-blaze @@ -0,0 +1,5 @@ +Significance: patch +Type: changed +Comment: Updated composer.lock. + + diff --git a/projects/plugins/backup/composer.lock b/projects/plugins/backup/composer.lock index ccd81132b1d2b..a3d9e0a269ad2 100644 --- a/projects/plugins/backup/composer.lock +++ b/projects/plugins/backup/composer.lock @@ -1605,7 +1605,7 @@ "dist": { "type": "path", "url": "../../packages/sync", - "reference": "6c08210f41958cbad899f8cf8f92435b91eaa932" + "reference": "4415298e32ada09dc9880eb8d4ec5b979efa4889" }, "require": { "automattic/jetpack-connection": "@dev", @@ -1639,7 +1639,7 @@ "link-template": "https://github.com/Automattic/jetpack-sync/compare/v${old}...v${new}" }, "branch-alias": { - "dev-trunk": "2.15.x-dev" + "dev-trunk": "2.16.x-dev" }, "dependencies": { "test-only": [ diff --git a/projects/plugins/boost/changelog/add-wordads-blaze b/projects/plugins/boost/changelog/add-wordads-blaze new file mode 100644 index 0000000000000..9aa70e3ec1f75 --- /dev/null +++ b/projects/plugins/boost/changelog/add-wordads-blaze @@ -0,0 +1,5 @@ +Significance: patch +Type: changed +Comment: Updated composer.lock. + + diff --git a/projects/plugins/boost/composer.lock b/projects/plugins/boost/composer.lock index d04d69b415ad1..2a87ddf187057 100644 --- a/projects/plugins/boost/composer.lock +++ b/projects/plugins/boost/composer.lock @@ -1588,7 +1588,7 @@ "dist": { "type": "path", "url": "../../packages/sync", - "reference": "6c08210f41958cbad899f8cf8f92435b91eaa932" + "reference": "4415298e32ada09dc9880eb8d4ec5b979efa4889" }, "require": { "automattic/jetpack-connection": "@dev", @@ -1622,7 +1622,7 @@ "link-template": "https://github.com/Automattic/jetpack-sync/compare/v${old}...v${new}" }, "branch-alias": { - "dev-trunk": "2.15.x-dev" + "dev-trunk": "2.16.x-dev" }, "dependencies": { "test-only": [ diff --git a/projects/plugins/jetpack/changelog/add-wordads-blaze#2 b/projects/plugins/jetpack/changelog/add-wordads-blaze#2 new file mode 100644 index 0000000000000..a1c1831fa1ef7 --- /dev/null +++ b/projects/plugins/jetpack/changelog/add-wordads-blaze#2 @@ -0,0 +1,5 @@ +Significance: patch +Type: other +Comment: Updated composer.lock. + + diff --git a/projects/plugins/jetpack/composer.lock b/projects/plugins/jetpack/composer.lock index 9bf7296164a46..2e67264c72feb 100644 --- a/projects/plugins/jetpack/composer.lock +++ b/projects/plugins/jetpack/composer.lock @@ -2571,7 +2571,7 @@ "dist": { "type": "path", "url": "../../packages/sync", - "reference": "6c08210f41958cbad899f8cf8f92435b91eaa932" + "reference": "4415298e32ada09dc9880eb8d4ec5b979efa4889" }, "require": { "automattic/jetpack-connection": "@dev", @@ -2605,7 +2605,7 @@ "link-template": "https://github.com/Automattic/jetpack-sync/compare/v${old}...v${new}" }, "branch-alias": { - "dev-trunk": "2.15.x-dev" + "dev-trunk": "2.16.x-dev" }, "dependencies": { "test-only": [ diff --git a/projects/plugins/migration/changelog/add-wordads-blaze b/projects/plugins/migration/changelog/add-wordads-blaze new file mode 100644 index 0000000000000..9aa70e3ec1f75 --- /dev/null +++ b/projects/plugins/migration/changelog/add-wordads-blaze @@ -0,0 +1,5 @@ +Significance: patch +Type: changed +Comment: Updated composer.lock. + + diff --git a/projects/plugins/migration/composer.lock b/projects/plugins/migration/composer.lock index 7935bc6fd45cb..65c35c742db1c 100644 --- a/projects/plugins/migration/composer.lock +++ b/projects/plugins/migration/composer.lock @@ -1605,7 +1605,7 @@ "dist": { "type": "path", "url": "../../packages/sync", - "reference": "6c08210f41958cbad899f8cf8f92435b91eaa932" + "reference": "4415298e32ada09dc9880eb8d4ec5b979efa4889" }, "require": { "automattic/jetpack-connection": "@dev", @@ -1639,7 +1639,7 @@ "link-template": "https://github.com/Automattic/jetpack-sync/compare/v${old}...v${new}" }, "branch-alias": { - "dev-trunk": "2.15.x-dev" + "dev-trunk": "2.16.x-dev" }, "dependencies": { "test-only": [ diff --git a/projects/plugins/mu-wpcom-plugin/changelog/add-wordads-blaze b/projects/plugins/mu-wpcom-plugin/changelog/add-wordads-blaze new file mode 100644 index 0000000000000..9aa70e3ec1f75 --- /dev/null +++ b/projects/plugins/mu-wpcom-plugin/changelog/add-wordads-blaze @@ -0,0 +1,5 @@ +Significance: patch +Type: changed +Comment: Updated composer.lock. + + diff --git a/projects/plugins/mu-wpcom-plugin/composer.lock b/projects/plugins/mu-wpcom-plugin/composer.lock index 26e732d7bf0fe..0a3b6d6c6b9e5 100644 --- a/projects/plugins/mu-wpcom-plugin/composer.lock +++ b/projects/plugins/mu-wpcom-plugin/composer.lock @@ -1185,7 +1185,7 @@ "dist": { "type": "path", "url": "../../packages/sync", - "reference": "6c08210f41958cbad899f8cf8f92435b91eaa932" + "reference": "4415298e32ada09dc9880eb8d4ec5b979efa4889" }, "require": { "automattic/jetpack-connection": "@dev", @@ -1219,7 +1219,7 @@ "link-template": "https://github.com/Automattic/jetpack-sync/compare/v${old}...v${new}" }, "branch-alias": { - "dev-trunk": "2.15.x-dev" + "dev-trunk": "2.16.x-dev" }, "dependencies": { "test-only": [ diff --git a/projects/plugins/protect/changelog/add-wordads-blaze b/projects/plugins/protect/changelog/add-wordads-blaze new file mode 100644 index 0000000000000..9aa70e3ec1f75 --- /dev/null +++ b/projects/plugins/protect/changelog/add-wordads-blaze @@ -0,0 +1,5 @@ +Significance: patch +Type: changed +Comment: Updated composer.lock. + + diff --git a/projects/plugins/protect/composer.lock b/projects/plugins/protect/composer.lock index 9ced4ec062b7c..60e9ee8409622 100644 --- a/projects/plugins/protect/composer.lock +++ b/projects/plugins/protect/composer.lock @@ -1517,7 +1517,7 @@ "dist": { "type": "path", "url": "../../packages/sync", - "reference": "6c08210f41958cbad899f8cf8f92435b91eaa932" + "reference": "4415298e32ada09dc9880eb8d4ec5b979efa4889" }, "require": { "automattic/jetpack-connection": "@dev", @@ -1551,7 +1551,7 @@ "link-template": "https://github.com/Automattic/jetpack-sync/compare/v${old}...v${new}" }, "branch-alias": { - "dev-trunk": "2.15.x-dev" + "dev-trunk": "2.16.x-dev" }, "dependencies": { "test-only": [ diff --git a/projects/plugins/search/changelog/add-wordads-blaze b/projects/plugins/search/changelog/add-wordads-blaze new file mode 100644 index 0000000000000..9aa70e3ec1f75 --- /dev/null +++ b/projects/plugins/search/changelog/add-wordads-blaze @@ -0,0 +1,5 @@ +Significance: patch +Type: changed +Comment: Updated composer.lock. + + diff --git a/projects/plugins/search/composer.lock b/projects/plugins/search/composer.lock index 2cbf046a868b3..4f4c36760997c 100644 --- a/projects/plugins/search/composer.lock +++ b/projects/plugins/search/composer.lock @@ -1609,7 +1609,7 @@ "dist": { "type": "path", "url": "../../packages/sync", - "reference": "6c08210f41958cbad899f8cf8f92435b91eaa932" + "reference": "4415298e32ada09dc9880eb8d4ec5b979efa4889" }, "require": { "automattic/jetpack-connection": "@dev", @@ -1643,7 +1643,7 @@ "link-template": "https://github.com/Automattic/jetpack-sync/compare/v${old}...v${new}" }, "branch-alias": { - "dev-trunk": "2.15.x-dev" + "dev-trunk": "2.16.x-dev" }, "dependencies": { "test-only": [ diff --git a/projects/plugins/social/changelog/add-wordads-blaze b/projects/plugins/social/changelog/add-wordads-blaze new file mode 100644 index 0000000000000..9aa70e3ec1f75 --- /dev/null +++ b/projects/plugins/social/changelog/add-wordads-blaze @@ -0,0 +1,5 @@ +Significance: patch +Type: changed +Comment: Updated composer.lock. + + diff --git a/projects/plugins/social/composer.lock b/projects/plugins/social/composer.lock index 80f0abd5e3246..57095178f428d 100644 --- a/projects/plugins/social/composer.lock +++ b/projects/plugins/social/composer.lock @@ -1599,7 +1599,7 @@ "dist": { "type": "path", "url": "../../packages/sync", - "reference": "6c08210f41958cbad899f8cf8f92435b91eaa932" + "reference": "4415298e32ada09dc9880eb8d4ec5b979efa4889" }, "require": { "automattic/jetpack-connection": "@dev", @@ -1633,7 +1633,7 @@ "link-template": "https://github.com/Automattic/jetpack-sync/compare/v${old}...v${new}" }, "branch-alias": { - "dev-trunk": "2.15.x-dev" + "dev-trunk": "2.16.x-dev" }, "dependencies": { "test-only": [ diff --git a/projects/plugins/starter-plugin/changelog/add-wordads-blaze b/projects/plugins/starter-plugin/changelog/add-wordads-blaze new file mode 100644 index 0000000000000..9aa70e3ec1f75 --- /dev/null +++ b/projects/plugins/starter-plugin/changelog/add-wordads-blaze @@ -0,0 +1,5 @@ +Significance: patch +Type: changed +Comment: Updated composer.lock. + + diff --git a/projects/plugins/starter-plugin/composer.lock b/projects/plugins/starter-plugin/composer.lock index 3eedbfb5de947..d4450e8980e09 100644 --- a/projects/plugins/starter-plugin/composer.lock +++ b/projects/plugins/starter-plugin/composer.lock @@ -1460,7 +1460,7 @@ "dist": { "type": "path", "url": "../../packages/sync", - "reference": "6c08210f41958cbad899f8cf8f92435b91eaa932" + "reference": "4415298e32ada09dc9880eb8d4ec5b979efa4889" }, "require": { "automattic/jetpack-connection": "@dev", @@ -1494,7 +1494,7 @@ "link-template": "https://github.com/Automattic/jetpack-sync/compare/v${old}...v${new}" }, "branch-alias": { - "dev-trunk": "2.15.x-dev" + "dev-trunk": "2.16.x-dev" }, "dependencies": { "test-only": [ diff --git a/projects/plugins/videopress/changelog/add-wordads-blaze b/projects/plugins/videopress/changelog/add-wordads-blaze new file mode 100644 index 0000000000000..9aa70e3ec1f75 --- /dev/null +++ b/projects/plugins/videopress/changelog/add-wordads-blaze @@ -0,0 +1,5 @@ +Significance: patch +Type: changed +Comment: Updated composer.lock. + + diff --git a/projects/plugins/videopress/composer.lock b/projects/plugins/videopress/composer.lock index 7f0c21fd854af..3444cfdedc1c9 100644 --- a/projects/plugins/videopress/composer.lock +++ b/projects/plugins/videopress/composer.lock @@ -1460,7 +1460,7 @@ "dist": { "type": "path", "url": "../../packages/sync", - "reference": "6c08210f41958cbad899f8cf8f92435b91eaa932" + "reference": "4415298e32ada09dc9880eb8d4ec5b979efa4889" }, "require": { "automattic/jetpack-connection": "@dev", @@ -1494,7 +1494,7 @@ "link-template": "https://github.com/Automattic/jetpack-sync/compare/v${old}...v${new}" }, "branch-alias": { - "dev-trunk": "2.15.x-dev" + "dev-trunk": "2.16.x-dev" }, "dependencies": { "test-only": [ From 8f55e834a13b2c90504b794ca887c4a2be95a05f Mon Sep 17 00:00:00 2001 From: Collyn Philleo Date: Thu, 2 May 2024 15:57:51 -0700 Subject: [PATCH 5/5] Make WordAds_Smart a singleton --- .../jetpack/modules/wordads/class-wordads.php | 2 +- .../wordads/php/class-wordads-smart.php | 40 ++++++++++++++----- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/projects/plugins/jetpack/modules/wordads/class-wordads.php b/projects/plugins/jetpack/modules/wordads/class-wordads.php index 5e2e169387725..851b093d750b3 100644 --- a/projects/plugins/jetpack/modules/wordads/class-wordads.php +++ b/projects/plugins/jetpack/modules/wordads/class-wordads.php @@ -216,7 +216,7 @@ public function init() { } // Initialize Smart. - new WordAds_Smart( $this->params ); + WordAds_Smart::instance()->init( $this->params ); if ( ( isset( $_SERVER['REQUEST_URI'] ) && '/ads.txt' === $_SERVER['REQUEST_URI'] ) || ( site_url( 'ads.txt', 'relative' ) === $_SERVER['REQUEST_URI'] ) ) { 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 d7048d69559f2..2b0a2ec1c1068 100644 --- a/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php +++ b/projects/plugins/jetpack/modules/wordads/php/class-wordads-smart.php @@ -17,11 +17,11 @@ class WordAds_Smart { /** - * WordAds Parameters. + * The single instance of the class. * - * @var WordAds_Params + * @var WordAds_Smart */ - private $params; + protected static $instance = null; /** * Is this an AMP request? @@ -52,22 +52,42 @@ class WordAds_Smart { private $is_inline_enabled; /** - * Our constructor. + * Private constructor. + */ + private function __construct() { + } + + /** + * Main Class Instance. + * + * Ensures only one instance of WordAds_Smart is loaded or can be loaded. + * + * @return WordAds_Smart + */ + public static function instance() { + if ( self::$instance === null ) { + self::$instance = new self(); + } + return self::$instance; + } + + /** + * Initialize the ads. * * @param WordAds_Params $params Object containing WordAds settings. + * + * @return void */ - public function __construct( WordAds_Params $params ) { - $this->params = $params; - $this->is_amp = function_exists( 'is_amp_endpoint' ) && is_amp_endpoint(); + 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' ) && $this->params->options['wordads_inline_enabled']; + $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 ) { // Insert ads. $this->insert_ads(); @@ -115,7 +135,7 @@ public function enqueue_assets() { * * @return void */ - public function insert_ads() { + private function insert_ads() { if ( $this->is_amp ) { return; }