Skip to content

Commit 239d537

Browse files
authored
Merge pull request #1033 from Codeinwp/bugfix/1031
Memoized result within request
2 parents 70da716 + 5e3a659 commit 239d537

1 file changed

Lines changed: 29 additions & 1 deletion

File tree

inc/settings.php

Lines changed: 29 additions & 1 deletion
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,13 +536,17 @@ private function is_main_mu_site() {
527536
* @return array Site settings.
528537
*/
529538
public function get_site_settings() {
539+
if ( self::$site_settings_cache !== null ) {
540+
return self::$site_settings_cache;
541+
}
542+
530543
$service_data = $this->get( 'service_data' );
531544
$whitelist = [];
532545
if ( isset( $service_data['whitelist'] ) ) {
533546
$whitelist = $service_data['whitelist'];
534547
}
535548

536-
return [
549+
self::$site_settings_cache = [
537550
'quality' => $this->get_quality(),
538551
'admin_bar_item' => $this->get( 'admin_bar_item' ),
539552
'lazyload' => $this->get( 'lazyload' ),
@@ -578,6 +591,7 @@ public function get_site_settings() {
578591
'show_badge_icon' => $this->get( 'show_badge_icon' ),
579592
'badge_position' => $this->get( 'badge_position' ),
580593
];
594+
return self::$site_settings_cache;
581595
}
582596

583597
/**
@@ -747,6 +761,7 @@ public function reset() {
747761
$update = update_option( $this->namespace, $reset_schema );
748762
if ( $update ) {
749763
self::$options = $reset_schema;
764+
$this->set_settings_cache( 'filters', $reset_schema['filters'] );
750765
}
751766
wp_unschedule_hook( Optml_Admin::SYNC_CRON );
752767
wp_unschedule_hook( Optml_Admin::ENRICH_CRON );
@@ -872,4 +887,17 @@ public function get_cloud_sites_whitelist_default() {
872887
$site_url => 'true',
873888
];
874889
}
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+
}
875903
}

0 commit comments

Comments
 (0)