From 5ea9fefc2c7f70f07f5206ffebaa35c304803c88 Mon Sep 17 00:00:00 2001 From: Collyn Philleo Date: Thu, 2 May 2024 15:57:51 -0700 Subject: [PATCH] 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; }