← All changes
|
freemius/includes/managers/class-fs-option-manager.php
+35
-80
1.3.3
→
5.0.1
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 { |
| @@ -156,61 +153,35 @@ | ||
| 156 | 153 | */ |
| 157 | 154 | function load( $flush = false ) { |
| 158 | 155 | $this->_logger->entrance(); |
| 159 | 156 | |
| 160 | - $option_name = $this->get_option_manager_name(); | |
| 157 | + if ( ! $flush && isset( $this->_options ) ) { | |
| 158 | + return; | |
| 159 | + } | |
| 161 | 160 | |
| 162 | - if ( $flush || ! isset( $this->_options ) ) { | |
| 163 | - if ( isset( $this->_options ) ) { | |
| 164 | - // Clear prev options. | |
| 165 | - $this->clear(); | |
| 166 | - } | |
| 161 | + if ( isset( $this->_options ) ) { | |
| 162 | + // Clear prev options. | |
| 163 | + $this->clear(); | |
| 164 | + } | |
| 167 | 165 | |
| 168 | - $cache_group = $this->get_cache_group(); | |
| 166 | + $option_name = $this->get_option_manager_name(); | |
| 169 | 167 | |
| 170 | - 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 | + } | |
| 171 | 175 | |
| 172 | - // Don't use cache layer in DEBUG mode. | |
| 173 | - $load_options = empty( $this->_options ); | |
| 176 | + if ( is_string( $this->_options ) ) { | |
| 177 | + $this->_options = json_decode( $this->_options ); | |
| 178 | + } | |
| 174 | 179 | |
| 175 | - } else { | |
| 176 | - | |
| 177 | - $this->_options = wp_cache_get( | |
| 178 | - $option_name, | |
| 179 | - $cache_group | |
| 180 | - ); | |
| 181 | - | |
| 182 | - $load_options = ( false === $this->_options ); | |
| 183 | - } | |
| 184 | - | |
| 185 | - $cached = true; | |
| 186 | - | |
| 187 | - if ( $load_options ) { | |
| 188 | - if ( $this->_is_network_storage ) { | |
| 189 | - $this->_options = get_site_option( $option_name ); | |
| 190 | - } else if ( $this->_blog_id > 0 ) { | |
| 191 | - $this->_options = get_blog_option( $this->_blog_id, $option_name ); | |
| 192 | - } else { | |
| 193 | - $this->_options = get_option( $option_name ); | |
| 194 | - } | |
| 195 | - | |
| 196 | - if ( is_string( $this->_options ) ) { | |
| 197 | - $this->_options = json_decode( $this->_options ); | |
| 198 | - } | |
| 199 | - | |
| 200 | 180 | // $this->_logger->info('get_option = ' . var_export($this->_options, true)); |
| 201 | 181 | |
| 202 | - if ( false === $this->_options ) { | |
| 203 | - $this->clear(); | |
| 204 | - } | |
| 205 | - | |
| 206 | - $cached = false; | |
| 207 | - } | |
| 208 | - | |
| 209 | - if ( ! WP_FS__DEBUG_SDK && ! $cached ) { | |
| 210 | - // Set non encoded cache. | |
| 211 | - wp_cache_set( $option_name, $this->_options, $cache_group ); | |
| 212 | - } | |
| 182 | + if ( false === $this->_options ) { | |
| 183 | + $this->clear(); | |
| 213 | 184 | } |
| 214 | 185 | } |
| 215 | 186 | |
| 216 | 187 | /** |
| @@ -271,12 +242,17 @@ | ||
| 271 | 242 | * @author Vova Feldman (@svovaf) |
| 272 | 243 | * @since 1.0.6 |
| 273 | 244 | * |
| 274 | 245 | * @param string $option |
| 246 | + * @param bool $flush | |
| 275 | 247 | * |
| 276 | 248 | * @return bool |
| 277 | 249 | */ |
| 278 | - function has_option( $option ) { | |
| 250 | + function has_option( $option, $flush = false ) { | |
| 251 | + if ( ! $this->is_loaded() || $flush ) { | |
| 252 | + $this->load( $flush ); | |
| 253 | + } | |
| 254 | + | |
| 279 | 255 | return array_key_exists( $option, $this->_options ); |
| 280 | 256 | } |
| 281 | 257 | |
| 282 | 258 | /** |
| @@ -284,16 +260,17 @@ | ||
| 284 | 260 | * @since 1.0.3 |
| 285 | 261 | * |
| 286 | 262 | * @param string $option |
| 287 | 263 | * @param mixed $default |
| 264 | + * @param bool $flush | |
| 288 | 265 | * |
| 289 | 266 | * @return mixed |
| 290 | 267 | */ |
| 291 | - function get_option( $option, $default = null ) { | |
| 268 | + function get_option( $option, $default = null, $flush = false ) { | |
| 292 | 269 | $this->_logger->entrance( 'option = ' . $option ); |
| 293 | 270 | |
| 294 | - if ( ! $this->is_loaded() ) { | |
| 295 | - $this->load(); | |
| 271 | + if ( ! $this->is_loaded() || $flush ) { | |
| 272 | + $this->load( $flush ); | |
| 296 | 273 | } |
| 297 | 274 | |
| 298 | 275 | if ( is_array( $this->_options ) ) { |
| 299 | 276 | $value = isset( $this->_options[ $option ] ) ? |
| @@ -309,9 +286,9 @@ | ||
| 309 | 286 | |
| 310 | 287 | /** |
| 311 | 288 | * If it's an object, return a clone of the object, otherwise, |
| 312 | 289 | * external changes of the object will actually change the value |
| 313 | - * 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 | |
| 314 | 291 | * behaviour and data integrity when a store() call is triggered. |
| 315 | 292 | * |
| 316 | 293 | * Example: |
| 317 | 294 | * $object1 = $options->get_option( 'object1' ); |
| @@ -435,12 +412,8 @@ | ||
| 435 | 412 | update_blog_option( $this->_blog_id, $option_name, $this->_options ); |
| 436 | 413 | } else { |
| 437 | 414 | update_option( $option_name, $this->_options, $this->_autoload ); |
| 438 | 415 | } |
| 439 | - | |
| 440 | - if ( ! WP_FS__DEBUG_SDK ) { | |
| 441 | - wp_cache_set( $option_name, $this->_options, $this->get_cache_group() ); | |
| 442 | - } | |
| 443 | 416 | } |
| 444 | 417 | |
| 445 | 418 | /** |
| 446 | 419 | * Get options keys. |
| @@ -496,26 +469,8 @@ | ||
| 496 | 469 | * @return string |
| 497 | 470 | */ |
| 498 | 471 | private function get_option_manager_name() { |
| 499 | 472 | return $this->_id; |
| 500 | - } | |
| 501 | - | |
| 502 | - /** | |
| 503 | - * @author Vova Feldman (@svovaf) | |
| 504 | - * @since 2.0.0 | |
| 505 | - * | |
| 506 | - * @return string | |
| 507 | - */ | |
| 508 | - private function get_cache_group() { | |
| 509 | - $group = WP_FS__SLUG; | |
| 510 | - | |
| 511 | - if ( $this->_is_network_storage ) { | |
| 512 | - $group .= '_ms'; | |
| 513 | - } else if ( $this->_blog_id > 0 ) { | |
| 514 | - $group .= "_s{$this->_blog_id}"; | |
| 515 | - } | |
| 516 | - | |
| 517 | - return $group; | |
| 518 | 473 | } |
| 519 | 474 | |
| 520 | 475 | #endregion |
| 521 | 476 | } |