Skip to content

Commit

Permalink
Make WordAds_Smart a singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
cphilleo committed May 2, 2024
1 parent 35dcd2e commit 5ea9fef
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
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 @@ -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'] ) ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -115,7 +135,7 @@ public function enqueue_assets() {
*
* @return void
*/
public function insert_ads() {
private function insert_ads() {
if ( $this->is_amp ) {
return;
}
Expand Down

0 comments on commit 5ea9fef

Please sign in to comment.