| 1 |
<?php |
| 2 |
/** |
| 3 |
* MetaSync Otto Configuration Cache |
| 4 |
* |
| 5 |
* PERFORMANCE OPTIMIZATION: This class provides static caching of plugin options |
| 6 |
* to avoid repeated get_option() calls within the same request. |
| 7 |
* |
| 8 |
* Expected improvement: 75% reduction in option loading queries |
| 9 |
* |
| 10 |
* @package Metasync |
| 11 |
* @subpackage Otto |
| 12 |
* @since 2.5.16 |
| 13 |
*/ |
| 14 |
|
| 15 |
if (!defined('ABSPATH')) { |
| 16 |
exit; # Exit if accessed directly |
| 17 |
} |
| 18 |
|
| 19 |
class Metasync_Otto_Config { |
| 20 |
/** |
| 21 |
* Cached options array |
| 22 |
* @var array|null |
| 23 |
*/ |
| 24 |
private static $options = null; |
| 25 |
|
| 26 |
/** |
| 27 |
* Flag to track if options have been loaded |
| 28 |
* @var bool |
| 29 |
*/ |
| 30 |
private static $loaded = false; |
| 31 |
|
| 32 |
/** |
| 33 |
* Get all MetaSync options (cached) |
| 34 |
* |
| 35 |
* @return array The cached options array |
| 36 |
*/ |
| 37 |
public static function get_options() { |
| 38 |
if (!self::$loaded) { |
| 39 |
self::$options = get_option('metasync_options'); |
| 40 |
self::$loaded = true; |
| 41 |
} |
| 42 |
return self::$options; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Get Otto Pixel UUID |
| 47 |
* |
| 48 |
* @return string The Otto UUID or empty string |
| 49 |
*/ |
| 50 |
public static function get_otto_uuid() { |
| 51 |
$options = self::get_options(); |
| 52 |
return $options['general']['otto_pixel_uuid'] ?? ''; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Check if Otto is enabled |
| 57 |
* |
| 58 |
* @return bool True if Otto UUID is configured |
| 59 |
*/ |
| 60 |
public static function is_otto_enabled() { |
| 61 |
return !empty(self::get_otto_uuid()); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Check if Otto is disabled for logged-in users |
| 66 |
* |
| 67 |
* @return bool True if Otto should be disabled for logged-in users |
| 68 |
*/ |
| 69 |
public static function is_disabled_for_loggedin() { |
| 70 |
$options = self::get_options(); |
| 71 |
return !empty($options['general']['otto_disable_on_loggedin']) && |
| 72 |
filter_var($options['general']['otto_disable_on_loggedin'], FILTER_VALIDATE_BOOLEAN); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Get WP Rocket compatibility mode setting |
| 77 |
* |
| 78 |
* @return string Compatibility mode: 'auto', 'disabled', or 'forced' |
| 79 |
*/ |
| 80 |
public static function get_wp_rocket_compat_mode() { |
| 81 |
$options = self::get_options(); |
| 82 |
return $options['general']['otto_wp_rocket_compat'] ?? 'auto'; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Check if meta descriptions are enabled |
| 87 |
* |
| 88 |
* @return bool True if meta descriptions are enabled |
| 89 |
*/ |
| 90 |
public static function is_meta_description_enabled() { |
| 91 |
$options = self::get_options(); |
| 92 |
$enable_metadesc = $options['general']['enable_metadesc'] ?? ''; |
| 93 |
return $enable_metadesc === 'true' || $enable_metadesc === true; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Get a specific option value by path |
| 98 |
* |
| 99 |
* @param string $path Dot-notation path to option (e.g., 'general.otto_pixel_uuid') |
| 100 |
* @param mixed $default Default value if not found |
| 101 |
* @return mixed The option value or default |
| 102 |
*/ |
| 103 |
public static function get($path, $default = null) { |
| 104 |
$options = self::get_options(); |
| 105 |
$keys = explode('.', $path); |
| 106 |
|
| 107 |
$value = $options; |
| 108 |
foreach ($keys as $key) { |
| 109 |
if (!isset($value[$key])) { |
| 110 |
return $default; |
| 111 |
} |
| 112 |
$value = $value[$key]; |
| 113 |
} |
| 114 |
|
| 115 |
return $value; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Clear the cached options (useful for testing or when options change) |
| 120 |
* |
| 121 |
* @return void |
| 122 |
*/ |
| 123 |
public static function clear_cache() { |
| 124 |
self::$options = null; |
| 125 |
self::$loaded = false; |
| 126 |
} |
| 127 |
} |
| 128 |
|