Skip to content

Commit 5e3a659

Browse files
fix: reset site settings cache on update
1 parent 539b5d8 commit 5e3a659

1 file changed

Lines changed: 27 additions & 5 deletions

File tree

inc/settings.php

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@ class Optml_Settings {
119119
*/
120120
private static $options;
121121

122+
/**
123+
* Cache for site settings to avoid multiple calls to get_option on the same request.
124+
*
125+
* @var array<string, mixed>|null Cached site settings.
126+
*/
127+
private static $site_settings_cache = null;
128+
122129
/**
123130
* Optml_Settings constructor.
124131
*/
@@ -440,6 +447,7 @@ public function update_frontend_banner_from_remote( $value ) {
440447

441448
if ( $update ) {
442449
self::$options = $opts;
450+
$this->set_settings_cache( 'banner_frontend', $opts['banner_frontend'] );
443451
}
444452

445453
return $update;
@@ -476,6 +484,7 @@ public function update( $key, $value ) {
476484

477485
if ( $update ) {
478486
self::$options = $opt;
487+
$this->set_settings_cache( $key, $value );
479488
}
480489
if ( apply_filters( 'optml_dont_trigger_settings_updated', false ) === false ) {
481490
/**
@@ -527,9 +536,8 @@ private function is_main_mu_site() {
527536
* @return array Site settings.
528537
*/
529538
public function get_site_settings() {
530-
static $cache = null;
531-
if ( $cache !== null ) {
532-
return $cache;
539+
if ( self::$site_settings_cache !== null ) {
540+
return self::$site_settings_cache;
533541
}
534542

535543
$service_data = $this->get( 'service_data' );
@@ -538,7 +546,7 @@ public function get_site_settings() {
538546
$whitelist = $service_data['whitelist'];
539547
}
540548

541-
$cache = [
549+
self::$site_settings_cache = [
542550
'quality' => $this->get_quality(),
543551
'admin_bar_item' => $this->get( 'admin_bar_item' ),
544552
'lazyload' => $this->get( 'lazyload' ),
@@ -583,7 +591,7 @@ public function get_site_settings() {
583591
'show_badge_icon' => $this->get( 'show_badge_icon' ),
584592
'badge_position' => $this->get( 'badge_position' ),
585593
];
586-
return $cache;
594+
return self::$site_settings_cache;
587595
}
588596

589597
/**
@@ -753,6 +761,7 @@ public function reset() {
753761
$update = update_option( $this->namespace, $reset_schema );
754762
if ( $update ) {
755763
self::$options = $reset_schema;
764+
$this->set_settings_cache( 'filters', $reset_schema['filters'] );
756765
}
757766
wp_unschedule_hook( Optml_Admin::SYNC_CRON );
758767
wp_unschedule_hook( Optml_Admin::ENRICH_CRON );
@@ -878,4 +887,17 @@ public function get_cloud_sites_whitelist_default() {
878887
$site_url => 'true',
879888
];
880889
}
890+
891+
/**
892+
* Set settings cache value.
893+
*
894+
* @param string $key Cache key.
895+
* @param mixed $value Cache value.
896+
* @return void
897+
*/
898+
private function set_settings_cache( $key, $value ) {
899+
if ( self::$site_settings_cache !== null && isset( self::$site_settings_cache[ $key ] ) ) {
900+
self::$site_settings_cache[ $key ] = $value;
901+
}
902+
}
881903
}

0 commit comments

Comments
 (0)