PluginProbe
Tiered Pricing Table for WooCommerce / 8.0.2
Tiered Pricing Table for WooCommerce v8.0.2
8.0.2 7.1.7 7.1.5 7.1.4 7.1.1 6.5.0 6.4.0 6.1.0 trunk 1.0 1.1 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2 2.2.3 2.3.0 2.3.1 2.3.2 2.3.3 All 91 releases
← All changes | freemius/includes/managers/class-fs-option-manager.php +71 -85 2.0 → 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,54 +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 - $this->_options = wp_cache_get(
149 - $option_name,
150 - $cache_group
151 - );
152 - }
176 + if ( is_string( $this->_options ) ) {
177 + $this->_options = json_decode( $this->_options );
178 + }
153 179
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 -
169 180 // $this->_logger->info('get_option = ' . var_export($this->_options, true));
170 181
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 - }
182 + if ( false === $this->_options ) {
183 + $this->clear();
182 184 }
183 185 }
184 186
185 187 /**
@@ -240,12 +242,17 @@
240 242 * @author Vova Feldman (@svovaf)
241 243 * @since 1.0.6
242 244 *
243 245 * @param string $option
246 + * @param bool $flush
244 247 *
245 248 * @return bool
246 249 */
247 - function has_option( $option ) {
250 + function has_option( $option, $flush = false ) {
251 + if ( ! $this->is_loaded() || $flush ) {
252 + $this->load( $flush );
253 + }
254 +
248 255 return array_key_exists( $option, $this->_options );
249 256 }
250 257
251 258 /**
@@ -253,16 +260,17 @@
253 260 * @since 1.0.3
254 261 *
255 262 * @param string $option
256 263 * @param mixed $default
264 + * @param bool $flush
257 265 *
258 266 * @return mixed
259 267 */
260 - function get_option( $option, $default = null ) {
268 + function get_option( $option, $default = null, $flush = false ) {
261 269 $this->_logger->entrance( 'option = ' . $option );
262 270
263 - if ( ! $this->is_loaded() ) {
264 - $this->load();
271 + if ( ! $this->is_loaded() || $flush ) {
272 + $this->load( $flush );
265 273 }
266 274
267 275 if ( is_array( $this->_options ) ) {
268 276 $value = isset( $this->_options[ $option ] ) ?
@@ -278,9 +286,9 @@
278 286
279 287 /**
280 288 * If it's an object, return a clone of the object, otherwise,
281 289 * external changes of the object will actually change the value
282 - * 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
283 291 * behaviour and data integrity when a store() call is triggered.
284 292 *
285 293 * Example:
286 294 * $object1 = $options->get_option( 'object1' );
@@ -402,14 +410,10 @@
402 410 update_site_option( $option_name, $this->_options );
403 411 } else if ( $this->_blog_id > 0 ) {
404 412 update_blog_option( $this->_blog_id, $option_name, $this->_options );
405 413 } else {
406 - update_option( $option_name, $this->_options );
414 + update_option( $option_name, $this->_options, $this->_autoload );
407 415 }
408 -
409 - if ( ! WP_FS__DEBUG_SDK ) {
410 - wp_cache_set( $option_name, $this->_options, $this->get_cache_group() );
411 - }
412 416 }
413 417
414 418 /**
415 419 * Get options keys.
@@ -467,24 +471,6 @@
467 471 private function get_option_manager_name() {
468 472 return $this->_id;
469 473 }
470 474
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 -
489 475 #endregion
490 - }
476 + }