From a87297d2dee45f99b2d7cb2e770dc5c10dbd0802 Mon Sep 17 00:00:00 2001 From: Swanand01 Date: Wed, 22 May 2024 16:42:09 +0530 Subject: [PATCH] Add `prime_option_caches` and call it on `init` --- includes/Settings.php | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/includes/Settings.php b/includes/Settings.php index f1af2d1bc20c..c5904103d913 100644 --- a/includes/Settings.php +++ b/includes/Settings.php @@ -163,6 +163,19 @@ class Settings implements Service, Registerable, PluginUninstallAware { */ public function __construct( Shopping_Vendors $shopping_vendors ) { $this->shopping_vendors = $shopping_vendors; + add_action( 'init', [ $this, 'prime_option_caches' ] ); + } + + /** + * Primes option caches for specified groups if the function exists. + * + * @since 1.37.0 + */ + public function prime_option_caches(): void { + if ( \function_exists( 'wp_prime_option_caches_by_group' ) ) { + wp_prime_option_caches_by_group( self::SETTING_GROUP ); + wp_prime_option_caches_by_group( self::SETTING_GROUP_EXPERIMENTS ); + } } /** @@ -438,11 +451,6 @@ public function register(): void { * @return string|array|bool|int Setting value. */ public function get_setting( string $key, $default_value = false ) { - if ( \function_exists( 'wp_prime_option_caches_by_group' ) ) { - wp_prime_option_caches_by_group( self::SETTING_GROUP ); - wp_prime_option_caches_by_group( self::SETTING_GROUP_EXPERIMENTS ); - } - // Distinguish between `false` as a default, and not passing one, just like WordPress. $passed_default = \func_num_args() > 1;