Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion inc/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ class Optml_Settings {
*/
private static $options;

/**
* Cache for site settings to avoid multiple calls to get_option on the same request.
*
* @var array<string, mixed>|null Cached site settings.
*/
private static $site_settings_cache = null;

/**
* Optml_Settings constructor.
*/
Expand Down Expand Up @@ -440,6 +447,7 @@ public function update_frontend_banner_from_remote( $value ) {

if ( $update ) {
self::$options = $opts;
$this->set_settings_cache( 'banner_frontend', $opts['banner_frontend'] );
}

return $update;
Expand Down Expand Up @@ -476,6 +484,7 @@ public function update( $key, $value ) {

if ( $update ) {
self::$options = $opt;
$this->set_settings_cache( $key, $value );
}
if ( apply_filters( 'optml_dont_trigger_settings_updated', false ) === false ) {
/**
Expand Down Expand Up @@ -527,13 +536,17 @@ private function is_main_mu_site() {
* @return array Site settings.
*/
public function get_site_settings() {
if ( self::$site_settings_cache !== null ) {
return self::$site_settings_cache;
}

$service_data = $this->get( 'service_data' );
$whitelist = [];
if ( isset( $service_data['whitelist'] ) ) {
$whitelist = $service_data['whitelist'];
}

return [
self::$site_settings_cache = [
'quality' => $this->get_quality(),
'admin_bar_item' => $this->get( 'admin_bar_item' ),
'lazyload' => $this->get( 'lazyload' ),
Expand Down Expand Up @@ -578,6 +591,7 @@ public function get_site_settings() {
'show_badge_icon' => $this->get( 'show_badge_icon' ),
'badge_position' => $this->get( 'badge_position' ),
];
return self::$site_settings_cache;
}

/**
Expand Down Expand Up @@ -747,6 +761,7 @@ public function reset() {
$update = update_option( $this->namespace, $reset_schema );
if ( $update ) {
self::$options = $reset_schema;
$this->set_settings_cache( 'filters', $reset_schema['filters'] );
}
wp_unschedule_hook( Optml_Admin::SYNC_CRON );
wp_unschedule_hook( Optml_Admin::ENRICH_CRON );
Expand Down Expand Up @@ -872,4 +887,17 @@ public function get_cloud_sites_whitelist_default() {
$site_url => 'true',
];
}

/**
* Set settings cache value.
*
* @param string $key Cache key.
* @param mixed $value Cache value.
* @return void
*/
private function set_settings_cache( $key, $value ) {
if ( self::$site_settings_cache !== null && isset( self::$site_settings_cache[ $key ] ) ) {
self::$site_settings_cache[ $key ] = $value;
}
}
}
Loading