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