← All changes
|
freemius/includes/managers/class-fs-option-manager.php
+85
-71
8.0.2
→
2.1.2
View file →
| @@ -10,13 +10,16 @@ | ||
| 10 | 10 | exit; |
| 11 | 11 | } |
| 12 | 12 | |
| 13 | 13 | /** |
| 14 | - * 2-layer lazy options manager. | |
| 15 | - * layer 2: Memory | |
| 16 | - * layer 1: Database (options table). All options stored as one option record in the DB to reduce number of DB queries. | |
| 14 | + * 3-layer lazy options manager. | |
| 15 | + * layer 3: Memory | |
| 16 | + * layer 2: Cache (if there's any caching plugin and if WP_FS__DEBUG_SDK is FALSE) | |
| 17 | + * layer 1: Database (options table). All options stored as one option record in the DB to reduce number of DB | |
| 18 | + * queries. | |
| 17 | 19 | * |
| 18 | - * If load() is not explicitly called, starts as empty manager. Same thing about saving the data - you have to explicitly call store(). | |
| 20 | + * If load() is not explicitly called, starts as empty manager. Same thing about saving the data - you have to | |
| 21 | + * explicitly call store(). | |
| 19 | 22 | * |
| 20 | 23 | * Class Freemius_Option_Manager |
| 21 | 24 | */ |
| 22 | 25 | class FS_Option_Manager { |
| @@ -45,13 +48,8 @@ | ||
| 45 | 48 | */ |
| 46 | 49 | private $_is_network_storage; |
| 47 | 50 | |
| 48 | 51 | /** |
| 49 | - * @var bool|null | |
| 50 | - */ | |
| 51 | - private $_autoload; | |
| 52 | - | |
| 53 | - /** | |
| 54 | 52 | * @var array[string]FS_Option_Manager { |
| 55 | 53 | * @key string |
| 56 | 54 | * @value FS_Option_Manager |
| 57 | 55 | * } |
| @@ -61,19 +59,13 @@ | ||
| 61 | 59 | /** |
| 62 | 60 | * @author Vova Feldman (@svovaf) |
| 63 | 61 | * @since 1.0.3 |
| 64 | 62 | * |
| 65 | - * @param string $id | |
| 66 | - * @param bool $load | |
| 67 | - * @param bool|int $network_level_or_blog_id Since 2.0.0 | |
| 68 | - * @param bool|null $autoload | |
| 63 | + * @param string $id | |
| 64 | + * @param bool $load | |
| 65 | + * @param bool|int $network_level_or_blog_id Since 2.0.0 | |
| 69 | 66 | */ |
| 70 | - private function __construct( | |
| 71 | - $id, | |
| 72 | - $load = false, | |
| 73 | - $network_level_or_blog_id = false, | |
| 74 | - $autoload = null | |
| 75 | - ) { | |
| 67 | + private function __construct( $id, $load = false, $network_level_or_blog_id = false ) { | |
| 76 | 68 | $id = strtolower( $id ); |
| 77 | 69 | |
| 78 | 70 | $this->_logger = FS_Logger::get_logger( WP_FS__SLUG . '_opt_mngr_' . $id, WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK ); |
| 79 | 71 | |
| @@ -81,10 +73,8 @@ | ||
| 81 | 73 | $this->_logger->log( 'id = ' . $id ); |
| 82 | 74 | |
| 83 | 75 | $this->_id = $id; |
| 84 | 76 | |
| 85 | - $this->_autoload = $autoload; | |
| 86 | - | |
| 87 | 77 | if ( is_multisite() ) { |
| 88 | 78 | $this->_is_network_storage = ( true === $network_level_or_blog_id ); |
| 89 | 79 | |
| 90 | 80 | if ( is_numeric( $network_level_or_blog_id ) ) { |
| @@ -102,21 +92,15 @@ | ||
| 102 | 92 | /** |
| 103 | 93 | * @author Vova Feldman (@svovaf) |
| 104 | 94 | * @since 1.0.3 |
| 105 | 95 | * |
| 106 | - * @param string $id | |
| 107 | - * @param bool $load | |
| 108 | - * @param bool|int $network_level_or_blog_id Since 2.0.0 | |
| 109 | - * @param bool|null $autoload | |
| 96 | + * @param string $id | |
| 97 | + * @param bool $load | |
| 98 | + * @param bool|int $network_level_or_blog_id Since 2.0.0 | |
| 110 | 99 | * |
| 111 | - * @return \FS_Option_Manager | |
| 100 | + * @return FS_Option_Manager | |
| 112 | 101 | */ |
| 113 | - static function get_manager( | |
| 114 | - $id, | |
| 115 | - $load = false, | |
| 116 | - $network_level_or_blog_id = false, | |
| 117 | - $autoload = null | |
| 118 | - ) { | |
| 102 | + static function get_manager( $id, $load = false, $network_level_or_blog_id = false ) { | |
| 119 | 103 | $key = strtolower( $id ); |
| 120 | 104 | |
| 121 | 105 | if ( is_multisite() ) { |
| 122 | 106 | if ( true === $network_level_or_blog_id ) { |
| @@ -130,14 +114,9 @@ | ||
| 130 | 114 | } |
| 131 | 115 | } |
| 132 | 116 | |
| 133 | 117 | if ( ! isset( self::$_MANAGERS[ $key ] ) ) { |
| 134 | - self::$_MANAGERS[ $key ] = new FS_Option_Manager( | |
| 135 | - $id, | |
| 136 | - $load, | |
| 137 | - $network_level_or_blog_id, | |
| 138 | - $autoload | |
| 139 | - ); | |
| 118 | + self::$_MANAGERS[ $key ] = new FS_Option_Manager( $id, $load, $network_level_or_blog_id ); | |
| 140 | 119 | } // If load required but not yet loaded, load. |
| 141 | 120 | else if ( $load && ! self::$_MANAGERS[ $key ]->is_loaded() ) { |
| 142 | 121 | self::$_MANAGERS[ $key ]->load(); |
| 143 | 122 | } |
| @@ -153,35 +132,54 @@ | ||
| 153 | 132 | */ |
| 154 | 133 | function load( $flush = false ) { |
| 155 | 134 | $this->_logger->entrance(); |
| 156 | 135 | |
| 157 | - if ( ! $flush && isset( $this->_options ) ) { | |
| 158 | - return; | |
| 159 | - } | |
| 136 | + $option_name = $this->get_option_manager_name(); | |
| 160 | 137 | |
| 161 | - if ( isset( $this->_options ) ) { | |
| 162 | - // Clear prev options. | |
| 163 | - $this->clear(); | |
| 164 | - } | |
| 138 | + if ( $flush || ! isset( $this->_options ) ) { | |
| 139 | + if ( isset( $this->_options ) ) { | |
| 140 | + // Clear prev options. | |
| 141 | + $this->clear(); | |
| 142 | + } | |
| 165 | 143 | |
| 166 | - $option_name = $this->get_option_manager_name(); | |
| 144 | + $cache_group = $this->get_cache_group(); | |
| 167 | 145 | |
| 168 | - if ( $this->_is_network_storage ) { | |
| 169 | - $this->_options = get_site_option( $option_name ); | |
| 170 | - } else if ( $this->_blog_id > 0 ) { | |
| 171 | - $this->_options = get_blog_option( $this->_blog_id, $option_name ); | |
| 172 | - } else { | |
| 173 | - $this->_options = get_option( $option_name ); | |
| 174 | - } | |
| 146 | + if ( ! WP_FS__DEBUG_SDK ) { | |
| 175 | 147 | |
| 176 | - if ( is_string( $this->_options ) ) { | |
| 177 | - $this->_options = json_decode( $this->_options ); | |
| 178 | - } | |
| 148 | + $this->_options = wp_cache_get( | |
| 149 | + $option_name, | |
| 150 | + $cache_group | |
| 151 | + ); | |
| 152 | + } | |
| 179 | 153 | |
| 154 | + $cached = true; | |
| 155 | + | |
| 156 | + if ( empty( $this->_options ) ) { | |
| 157 | + if ( $this->_is_network_storage ) { | |
| 158 | + $this->_options = get_site_option( $option_name ); | |
| 159 | + } else if ( $this->_blog_id > 0 ) { | |
| 160 | + $this->_options = get_blog_option( $this->_blog_id, $option_name ); | |
| 161 | + } else { | |
| 162 | + $this->_options = get_option( $option_name ); | |
| 163 | + } | |
| 164 | + | |
| 165 | + if ( is_string( $this->_options ) ) { | |
| 166 | + $this->_options = json_decode( $this->_options ); | |
| 167 | + } | |
| 168 | + | |
| 180 | 169 | // $this->_logger->info('get_option = ' . var_export($this->_options, true)); |
| 181 | 170 | |
| 182 | - if ( false === $this->_options ) { | |
| 183 | - $this->clear(); | |
| 171 | + if ( false === $this->_options ) { | |
| 172 | + $this->clear(); | |
| 173 | + } | |
| 174 | + | |
| 175 | + $cached = false; | |
| 176 | + } | |
| 177 | + | |
| 178 | + if ( ! WP_FS__DEBUG_SDK && ! $cached ) { | |
| 179 | + // Set non encoded cache. | |
| 180 | + wp_cache_set( $option_name, $this->_options, $cache_group ); | |
| 181 | + } | |
| 184 | 182 | } |
| 185 | 183 | } |
| 186 | 184 | |
| 187 | 185 | /** |
| @@ -242,17 +240,12 @@ | ||
| 242 | 240 | * @author Vova Feldman (@svovaf) |
| 243 | 241 | * @since 1.0.6 |
| 244 | 242 | * |
| 245 | 243 | * @param string $option |
| 246 | - * @param bool $flush | |
| 247 | 244 | * |
| 248 | 245 | * @return bool |
| 249 | 246 | */ |
| 250 | - function has_option( $option, $flush = false ) { | |
| 251 | - if ( ! $this->is_loaded() || $flush ) { | |
| 252 | - $this->load( $flush ); | |
| 253 | - } | |
| 254 | - | |
| 247 | + function has_option( $option ) { | |
| 255 | 248 | return array_key_exists( $option, $this->_options ); |
| 256 | 249 | } |
| 257 | 250 | |
| 258 | 251 | /** |
| @@ -260,17 +253,16 @@ | ||
| 260 | 253 | * @since 1.0.3 |
| 261 | 254 | * |
| 262 | 255 | * @param string $option |
| 263 | 256 | * @param mixed $default |
| 264 | - * @param bool $flush | |
| 265 | 257 | * |
| 266 | 258 | * @return mixed |
| 267 | 259 | */ |
| 268 | - function get_option( $option, $default = null, $flush = false ) { | |
| 260 | + function get_option( $option, $default = null ) { | |
| 269 | 261 | $this->_logger->entrance( 'option = ' . $option ); |
| 270 | 262 | |
| 271 | - if ( ! $this->is_loaded() || $flush ) { | |
| 272 | - $this->load( $flush ); | |
| 263 | + if ( ! $this->is_loaded() ) { | |
| 264 | + $this->load(); | |
| 273 | 265 | } |
| 274 | 266 | |
| 275 | 267 | if ( is_array( $this->_options ) ) { |
| 276 | 268 | $value = isset( $this->_options[ $option ] ) ? |
| @@ -286,9 +278,9 @@ | ||
| 286 | 278 | |
| 287 | 279 | /** |
| 288 | 280 | * If it's an object, return a clone of the object, otherwise, |
| 289 | 281 | * external changes of the object will actually change the value |
| 290 | - * of the object in the option manager which may lead to an unexpected | |
| 282 | + * of the object in the options manager which may lead to an unexpected | |
| 291 | 283 | * behaviour and data integrity when a store() call is triggered. |
| 292 | 284 | * |
| 293 | 285 | * Example: |
| 294 | 286 | * $object1 = $options->get_option( 'object1' ); |
| @@ -410,10 +402,14 @@ | ||
| 410 | 402 | update_site_option( $option_name, $this->_options ); |
| 411 | 403 | } else if ( $this->_blog_id > 0 ) { |
| 412 | 404 | update_blog_option( $this->_blog_id, $option_name, $this->_options ); |
| 413 | 405 | } else { |
| 414 | - update_option( $option_name, $this->_options, $this->_autoload ); | |
| 406 | + update_option( $option_name, $this->_options ); | |
| 415 | 407 | } |
| 408 | + | |
| 409 | + if ( ! WP_FS__DEBUG_SDK ) { | |
| 410 | + wp_cache_set( $option_name, $this->_options, $this->get_cache_group() ); | |
| 411 | + } | |
| 416 | 412 | } |
| 417 | 413 | |
| 418 | 414 | /** |
| 419 | 415 | * Get options keys. |
| @@ -471,6 +467,24 @@ | ||
| 471 | 467 | private function get_option_manager_name() { |
| 472 | 468 | return $this->_id; |
| 473 | 469 | } |
| 474 | 470 | |
| 471 | + /** | |
| 472 | + * @author Vova Feldman (@svovaf) | |
| 473 | + * @since 2.0.0 | |
| 474 | + * | |
| 475 | + * @return string | |
| 476 | + */ | |
| 477 | + private function get_cache_group() { | |
| 478 | + $group = WP_FS__SLUG; | |
| 479 | + | |
| 480 | + if ( $this->_is_network_storage ) { | |
| 481 | + $group .= '_ms'; | |
| 482 | + } else if ( $this->_blog_id > 0 ) { | |
| 483 | + $group .= "_s{$this->_blog_id}"; | |
| 484 | + } | |
| 485 | + | |
| 486 | + return $group; | |
| 487 | + } | |
| 488 | + | |
| 475 | 489 | #endregion |
| 476 | - } | |
| 490 | + } | |