PluginProbe
BlockSpare – Gutenberg Blocks, AI Content Generator & Site Builder for News, Magazine & Blogs / 2.6.6
BlockSpare – Gutenberg Blocks, AI Content Generator & Site Builder for News, Magazine & Blogs v2.6.6
5.0.1 4.1.0 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.1.0 1.1.1 1.1.2 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 2.1.2 2.5.0 2.5.3 All 39 releases
← All changes | freemius/includes/class-fs-storage.php +558 -532 1.3.5 → 2.6.6 View file →
@@ -1,532 +1,558 @@
1 -<?php
2 - /**
3 - * @package Freemius
4 - * @copyright Copyright (c) 2015, Freemius, Inc.
5 - * @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License Version 3
6 - * @since 1.2.3
7 - */
8 -
9 - if ( ! defined( 'ABSPATH' ) ) {
10 - exit;
11 - }
12 -
13 - /**
14 - * Class FS_Storage
15 - *
16 - * A wrapper class for handling network level and single site level storage.
17 - *
18 - * @property bool $is_network_activation
19 - * @property int $network_install_blog_id
20 - * @property object $sync_cron
21 - */
22 - class FS_Storage {
23 - /**
24 - * @var FS_Storage[]
25 - */
26 - private static $_instances = array();
27 - /**
28 - * @var FS_Key_Value_Storage Site level storage.
29 - */
30 - private $_storage;
31 -
32 - /**
33 - * @var FS_Key_Value_Storage Network level storage.
34 - */
35 - private $_network_storage;
36 -
37 - /**
38 - * @var string
39 - */
40 - private $_module_type;
41 -
42 - /**
43 - * @var string
44 - */
45 - private $_module_slug;
46 -
47 - /**
48 - * @var int The ID of the blog that is associated with the current site level options.
49 - */
50 - private $_blog_id = 0;
51 -
52 - /**
53 - * @var bool
54 - */
55 - private $_is_multisite;
56 -
57 - /**
58 - * @var bool
59 - */
60 - private $_is_network_active = false;
61 -
62 - /**
63 - * @var bool
64 - */
65 - private $_is_delegated_connection = false;
66 -
67 - /**
68 - * @var array {
69 - * @key string Option name.
70 - * @value int If 0 store on the network level. If 1, store on the network level only if module was network level activated. If 2, store on the network level only if network activated and NOT delegated the connection.
71 - * }
72 - */
73 - private static $_NETWORK_OPTIONS_MAP;
74 -
75 - /**
76 - * @author Leo Fajardo (@leorw)
77 - *
78 - * @param string $module_type
79 - * @param string $slug
80 - *
81 - * @return FS_Storage
82 - */
83 - static function instance( $module_type, $slug ) {
84 - $key = $module_type . ':' . $slug;
85 -
86 - if ( ! isset( self::$_instances[ $key ] ) ) {
87 - self::$_instances[ $key ] = new FS_Storage( $module_type, $slug );
88 - }
89 -
90 - return self::$_instances[ $key ];
91 - }
92 -
93 - /**
94 - * @author Leo Fajardo (@leorw)
95 - *
96 - * @param string $module_type
97 - * @param string $slug
98 - */
99 - private function __construct( $module_type, $slug ) {
100 - $this->_module_type = $module_type;
101 - $this->_module_slug = $slug;
102 - $this->_is_multisite = is_multisite();
103 -
104 - if ( $this->_is_multisite ) {
105 - $this->_blog_id = get_current_blog_id();
106 - $this->_network_storage = FS_Key_Value_Storage::instance( $module_type . '_data', $slug, true );
107 - }
108 -
109 - $this->_storage = FS_Key_Value_Storage::instance( $module_type . '_data', $slug, $this->_blog_id );
110 - }
111 -
112 - /**
113 - * Tells this storage wrapper class that the context plugin is network active. This flag will affect how values
114 - * are retrieved/stored from/into the storage.
115 - *
116 - * @author Leo Fajardo (@leorw)
117 - *
118 - * @param bool $is_network_active
119 - * @param bool $is_delegated_connection
120 - */
121 - function set_network_active( $is_network_active = true, $is_delegated_connection = false ) {
122 - $this->_is_network_active = $is_network_active;
123 - $this->_is_delegated_connection = $is_delegated_connection;
124 - }
125 -
126 - /**
127 - * Switch the context of the site level storage manager.
128 - *
129 - * @author Vova Feldman (@svovaf)
130 - * @since 2.0.0
131 - *
132 - * @param int $blog_id
133 - */
134 - function set_site_blog_context( $blog_id ) {
135 - $this->_storage = $this->get_site_storage( $blog_id );
136 - $this->_blog_id = $blog_id;
137 - }
138 -
139 - /**
140 - * @author Leo Fajardo (@leorw)
141 - *
142 - * @param string $key
143 - * @param mixed $value
144 - * @param null|bool|int $network_level_or_blog_id When an integer, use the given blog storage. When `true` use the multisite storage (if there's a network). When `false`, use the current context blog storage. When `null`, the decision which storage to use (MS vs. Current S) will be handled internally and determined based on the $option (based on self::$_BINARY_MAP).
145 - * @param bool $flush
146 - */
147 - function store( $key, $value, $network_level_or_blog_id = null, $flush = true ) {
148 - if ( $this->should_use_network_storage( $key, $network_level_or_blog_id ) ) {
149 - $this->_network_storage->store( $key, $value, $flush );
150 - } else {
151 - $storage = $this->get_site_storage( $network_level_or_blog_id );
152 - $storage->store( $key, $value, $flush );
153 - }
154 - }
155 -
156 - /**
157 - * @author Leo Fajardo (@leorw)
158 - *
159 - * @param bool $store
160 - * @param string[] $exceptions Set of keys to keep and not clear.
161 - * @param int|null|bool $network_level_or_blog_id
162 - */
163 - function clear_all( $store = true, $exceptions = array(), $network_level_or_blog_id = null ) {
164 - if ( ! $this->_is_multisite ||
165 - false === $network_level_or_blog_id ||
166 - is_null( $network_level_or_blog_id ) ||
167 - is_numeric( $network_level_or_blog_id )
168 - ) {
169 - $storage = $this->get_site_storage( $network_level_or_blog_id );
170 - $storage->clear_all( $store, $exceptions );
171 - }
172 -
173 - if ( $this->_is_multisite &&
174 - ( true === $network_level_or_blog_id || is_null( $network_level_or_blog_id ) )
175 - ) {
176 - $this->_network_storage->clear_all( $store, $exceptions );
177 - }
178 - }
179 -
180 - /**
181 - * @author Leo Fajardo (@leorw)
182 - *
183 - * @param string $key
184 - * @param bool $store
185 - * @param null|bool|int $network_level_or_blog_id When an integer, use the given blog storage. When `true` use the multisite storage (if there's a network). When `false`, use the current context blog storage. When `null`, the decision which storage to use (MS vs. Current S) will be handled internally and determined based on the $option (based on self::$_BINARY_MAP).
186 - */
187 - function remove( $key, $store = true, $network_level_or_blog_id = null ) {
188 - if ( $this->should_use_network_storage( $key, $network_level_or_blog_id ) ) {
189 - $this->_network_storage->remove( $key, $store );
190 - } else {
191 - $storage = $this->get_site_storage( $network_level_or_blog_id );
192 - $storage->remove( $key, $store );
193 - }
194 - }
195 -
196 - /**
197 - * @author Leo Fajardo (@leorw)
198 - *
199 - * @param string $key
200 - * @param mixed $default
201 - * @param null|bool|int $network_level_or_blog_id When an integer, use the given blog storage. When `true` use the multisite storage (if there's a network). When `false`, use the current context blog storage. When `null`, the decision which storage to use (MS vs. Current S) will be handled internally and determined based on the $option (based on self::$_BINARY_MAP).
202 - *
203 - * @return mixed
204 - */
205 - function get( $key, $default = false, $network_level_or_blog_id = null ) {
206 - if ( $this->should_use_network_storage( $key, $network_level_or_blog_id ) ) {
207 - return $this->_network_storage->get( $key, $default );
208 - } else {
209 - $storage = $this->get_site_storage( $network_level_or_blog_id );
210 -
211 - return $storage->get( $key, $default );
212 - }
213 - }
214 -
215 - /**
216 - * Multisite activated:
217 - * true: Save network storage.
218 - * int: Save site specific storage.
219 - * false|0: Save current site storage.
220 - * null: Save network and current site storage.
221 - * Site level activated:
222 - * Save site storage.
223 - *
224 - * @author Vova Feldman (@svovaf)
225 - * @since 2.0.0
226 - *
227 - * @param bool|int|null $network_level_or_blog_id
228 - */
229 - function save( $network_level_or_blog_id = null ) {
230 - if ( $this->_is_network_active &&
231 - ( true === $network_level_or_blog_id || is_null( $network_level_or_blog_id ) )
232 - ) {
233 - $this->_network_storage->save();
234 - }
235 -
236 - if ( ! $this->_is_network_active || true !== $network_level_or_blog_id ) {
237 - $storage = $this->get_site_storage( $network_level_or_blog_id );
238 - $storage->save();
239 - }
240 - }
241 -
242 - /**
243 - * @author Vova Feldman (@svovaf)
244 - * @since 2.0.0
245 - *
246 - * @return string
247 - */
248 - function get_module_slug() {
249 - return $this->_module_slug;
250 - }
251 -
252 - /**
253 - * @author Vova Feldman (@svovaf)
254 - * @since 2.0.0
255 - *
256 - * @return string
257 - */
258 - function get_module_type() {
259 - return $this->_module_type;
260 - }
261 -
262 - /**
263 - * Migration script to the new storage data structure that is network compatible.
264 - *
265 - * IMPORTANT:
266 - * This method should be executed only after it is determined if this is a network
267 - * level compatible product activation.
268 - *
269 - * @author Vova Feldman (@svovaf)
270 - * @since 2.0.0
271 - */
272 - function migrate_to_network() {
273 - if ( ! $this->_is_multisite ) {
274 - return;
275 - }
276 -
277 - $updated = false;
278 -
279 - if ( ! isset( self::$_NETWORK_OPTIONS_MAP ) ) {
280 - self::load_network_options_map();
281 - }
282 -
283 - foreach ( self::$_NETWORK_OPTIONS_MAP as $option => $storage_level ) {
284 - if ( ! $this->is_multisite_option( $option ) ) {
285 - continue;
286 - }
287 -
288 - if ( isset( $this->_storage->{$option} ) && ! isset( $this->_network_storage->{$option} ) ) {
289 - // Migrate option to the network storage.
290 - $this->_network_storage->store( $option, $this->_storage->{$option}, false );
291 -
292 - /**
293 - * Remove the option from site level storage.
294 - *
295 - * IMPORTANT:
296 - * The line below is intentionally commented since we want to preserve the option
297 - * on the site storage level for "downgrade compatibility". Basically, if the user
298 - * will downgrade to an older version of the plugin with the prev storage structure,
299 - * it will continue working.
300 - *
301 - * @todo After a few releases we can remove this.
302 - */
303 -// $this->_storage->remove($option, false);
304 -
305 - $updated = true;
306 - }
307 - }
308 -
309 - if ( ! $updated ) {
310 - return;
311 - }
312 -
313 - // Update network level storage.
314 - $this->_network_storage->save();
315 -// $this->_storage->save();
316 - }
317 -
318 - #--------------------------------------------------------------------------------
319 - #region Helper Methods
320 - #--------------------------------------------------------------------------------
321 -
322 - /**
323 - * We don't want to load the map right away since it's not even needed in a non-MS environment.
324 - *
325 - * Example:
326 - * array(
327 - * 'option1' => 0, // Means that the option should always be stored on the network level.
328 - * 'option2' => 1, // Means that the option should be stored on the network level only when the module was network level activated.
329 - * 'option2' => 2, // Means that the option should be stored on the network level only when the module was network level activated AND the connection was NOT delegated.
330 - * 'option3' => 3, // Means that the option should always be stored on the site level.
331 - * )
332 - *
333 - * @author Vova Feldman (@svovaf)
334 - * @since 2.0.0
335 - */
336 - private static function load_network_options_map() {
337 - self::$_NETWORK_OPTIONS_MAP = array(
338 - // Network level options.
339 - 'affiliate_application_data' => 0,
340 - 'beta_data' => 0,
341 - 'connectivity_test' => 0,
342 - 'handle_gdpr_admin_notice' => 0,
343 - 'has_trial_plan' => 0,
344 - 'install_sync_timestamp' => 0,
345 - 'install_sync_cron' => 0,
346 - 'is_anonymous_ms' => 0,
347 - 'is_network_activated' => 0,
348 - 'is_on' => 0,
349 - 'is_plugin_new_install' => 0,
350 - 'network_install_blog_id' => 0,
351 - 'pending_sites_info' => 0,
352 - 'plugin_last_version' => 0,
353 - 'plugin_main_file' => 0,
354 - 'plugin_version' => 0,
355 - 'sdk_downgrade_mode' => 0,
356 - 'sdk_last_version' => 0,
357 - 'sdk_upgrade_mode' => 0,
358 - 'sdk_version' => 0,
359 - 'sticky_optin_added_ms' => 0,
360 - 'subscriptions' => 0,
361 - 'sync_timestamp' => 0,
362 - 'sync_cron' => 0,
363 - 'was_plugin_loaded' => 0,
364 - 'network_user_id' => 0,
365 - 'plugin_upgrade_mode' => 0,
366 - 'plugin_downgrade_mode' => 0,
367 - 'is_network_connected' => 0,
368 - /**
369 - * Special flag that is used when a super-admin upgrades to the new version of the SDK that
370 - * supports network level integration, when the connection decision wasn't made for all of the
371 - * sites in the network.
372 - */
373 - 'is_network_activation' => 0,
374 - 'license_migration' => 0,
375 -
376 - // When network activated, then network level.
377 - 'install_timestamp' => 1,
378 - 'prev_is_premium' => 1,
379 - 'require_license_activation' => 1,
380 -
381 - // If not network activated OR delegated, then site level.
382 - 'activation_timestamp' => 2,
383 - 'expired_license_notice_shown' => 2,
384 - 'is_whitelabeled' => 2,
385 - 'last_license_key' => 2,
386 - 'last_license_user_id' => 2,
387 - 'prev_user_id' => 2,
388 - 'sticky_optin_added' => 2,
389 - 'uninstall_reason' => 2,
390 - 'is_pending_activation' => 2,
391 - 'pending_license_key' => 2,
392 - 'is_extensions_tracking_allowed' => 2,
393 -
394 - // Site level options.
395 - 'is_anonymous' => 3,
396 - );
397 - }
398 -
399 - /**
400 - * This method will and should only be executed when is_multisite() is true.
401 - *
402 - * @author Vova Feldman (@svovaf)
403 - * @since 2.0.0
404 - *
405 - * @param string $key
406 - *
407 - * @return bool|mixed
408 - */
409 - private function is_multisite_option( $key ) {
410 - if ( ! isset( self::$_NETWORK_OPTIONS_MAP ) ) {
411 - self::load_network_options_map();
412 - }
413 -
414 - if ( ! isset( self::$_NETWORK_OPTIONS_MAP[ $key ] ) ) {
415 - // Option not found -> use site level storage.
416 - return false;
417 - }
418 -
419 - if ( 0 === self::$_NETWORK_OPTIONS_MAP[ $key ] ) {
420 - // Option found and set to always use the network level storage on a multisite.
421 - return true;
422 - }
423 -
424 - if ( 3 === self::$_NETWORK_OPTIONS_MAP[ $key ] ) {
425 - // Option found and set to always use the site level storage on a multisite.
426 - return false;
427 - }
428 -
429 - if ( ! $this->_is_network_active ) {
430 - return false;
431 - }
432 -
433 - if ( 1 === self::$_NETWORK_OPTIONS_MAP[ $key ] ) {
434 - // Network activated.
435 - return true;
436 - }
437 -
438 - if ( 2 === self::$_NETWORK_OPTIONS_MAP[ $key ] && ! $this->_is_delegated_connection ) {
439 - // Network activated and not delegated.
440 - return true;
441 - }
442 -
443 - return false;
444 - }
445 -
446 - /**
447 - * @author Leo Fajardo
448 - *
449 - * @param string $key
450 - * @param null|bool|int $network_level_or_blog_id When an integer, use the given blog storage. When `true` use the multisite storage (if there's a network). When `false`, use the current context blog storage. When `null`, the decision which storage to use (MS vs. Current S) will be handled internally and determined based on the $option (based on self::$_BINARY_MAP).
451 - *
452 - * @return bool
453 - */
454 - private function should_use_network_storage( $key, $network_level_or_blog_id = null ) {
455 - if ( ! $this->_is_multisite ) {
456 - // Not a multisite environment.
457 - return false;
458 - }
459 -
460 - if ( is_numeric( $network_level_or_blog_id ) ) {
461 - // Explicitly asked to use a specified blog storage.
462 - return false;
463 - }
464 -
465 - if ( is_bool( $network_level_or_blog_id ) ) {
466 - // Explicitly specified whether should use the network or blog level storage.
467 - return $network_level_or_blog_id;
468 - }
469 -
470 - // Determine which storage to use based on the option.
471 - return $this->is_multisite_option( $key );
472 - }
473 -
474 - /**
475 - * @author Vova Feldman (@svovaf)
476 - * @since 2.0.0
477 - *
478 - * @param int $blog_id
479 - *
480 - * @return \FS_Key_Value_Storage
481 - */
482 - private function get_site_storage( $blog_id = 0 ) {
483 - if ( ! is_numeric( $blog_id ) ||
484 - $blog_id == $this->_blog_id ||
485 - 0 == $blog_id
486 - ) {
487 - return $this->_storage;
488 - }
489 -
490 - return FS_Key_Value_Storage::instance(
491 - $this->_module_type . '_data',
492 - $this->_storage->get_secondary_id(),
493 - $blog_id
494 - );
495 - }
496 -
497 - #endregion
498 -
499 - #--------------------------------------------------------------------------------
500 - #region Magic methods
501 - #--------------------------------------------------------------------------------
502 -
503 - function __set( $k, $v ) {
504 - if ( $this->should_use_network_storage( $k ) ) {
505 - $this->_network_storage->{$k} = $v;
506 - } else {
507 - $this->_storage->{$k} = $v;
508 - }
509 - }
510 -
511 - function __isset( $k ) {
512 - return $this->should_use_network_storage( $k ) ?
513 - isset( $this->_network_storage->{$k} ) :
514 - isset( $this->_storage->{$k} );
515 - }
516 -
517 - function __unset( $k ) {
518 - if ( $this->should_use_network_storage( $k ) ) {
519 - unset( $this->_network_storage->{$k} );
520 - } else {
521 - unset( $this->_storage->{$k} );
522 - }
523 - }
524 -
525 - function __get( $k ) {
526 - return $this->should_use_network_storage( $k ) ?
527 - $this->_network_storage->{$k} :
528 - $this->_storage->{$k};
529 - }
530 -
531 - #endregion
532 - }
1 +<?php
2 + /**
3 + * @package Freemius
4 + * @copyright Copyright (c) 2015, Freemius, Inc.
5 + * @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License Version 3
6 + * @since 1.2.3
7 + */
8 +
9 + if ( ! defined( 'ABSPATH' ) ) {
10 + exit;
11 + }
12 +
13 + /**
14 + * Class FS_Storage
15 + *
16 + * A wrapper class for handling network level and single site level storage.
17 + *
18 + * @property bool $is_network_activation
19 + * @property int $network_install_blog_id
20 + * @property bool|null $is_extensions_tracking_allowed
21 + * @property bool|null $is_diagnostic_tracking_allowed
22 + * @property object $sync_cron
23 + */
24 + class FS_Storage {
25 + /**
26 + * @var FS_Storage[]
27 + */
28 + private static $_instances = array();
29 + /**
30 + * @var FS_Key_Value_Storage Site level storage.
31 + */
32 + private $_storage;
33 +
34 + /**
35 + * @var FS_Key_Value_Storage Network level storage.
36 + */
37 + private $_network_storage;
38 +
39 + /**
40 + * @var string
41 + */
42 + private $_module_type;
43 +
44 + /**
45 + * @var string
46 + */
47 + private $_module_slug;
48 +
49 + /**
50 + * @var int The ID of the blog that is associated with the current site level options.
51 + */
52 + private $_blog_id = 0;
53 +
54 + /**
55 + * @var bool
56 + */
57 + private $_is_multisite;
58 +
59 + /**
60 + * @var bool
61 + */
62 + private $_is_network_active = false;
63 +
64 + /**
65 + * @var bool
66 + */
67 + private $_is_delegated_connection = false;
68 +
69 + /**
70 + * @var array {
71 + * @key string Option name.
72 + * @value int If 0 store on the network level. If 1, store on the network level only if module was network level activated. If 2, store on the network level only if network activated and NOT delegated the connection.
73 + * }
74 + */
75 + private static $_NETWORK_OPTIONS_MAP;
76 +
77 + const OPTION_LEVEL_UNDEFINED = -1;
78 + // The option should be stored on the network level.
79 + const OPTION_LEVEL_NETWORK = 0;
80 + // The option should be stored on the network level when the plugin is network-activated.
81 + const OPTION_LEVEL_NETWORK_ACTIVATED = 1;
82 + // The option should be stored on the network level when the plugin is network-activated and the opt-in connection was NOT delegated to the sub-site admin.
83 + const OPTION_LEVEL_NETWORK_ACTIVATED_NOT_DELEGATED = 2;
84 + // The option should be stored on the site level.
85 + const OPTION_LEVEL_SITE = 3;
86 +
87 + /**
88 + * @author Leo Fajardo (@leorw)
89 + *
90 + * @param string $module_type
91 + * @param string $slug
92 + *
93 + * @return FS_Storage
94 + */
95 + static function instance( $module_type, $slug ) {
96 + $key = $module_type . ':' . $slug;
97 +
98 + if ( ! isset( self::$_instances[ $key ] ) ) {
99 + self::$_instances[ $key ] = new FS_Storage( $module_type, $slug );
100 + }
101 +
102 + return self::$_instances[ $key ];
103 + }
104 +
105 + /**
106 + * @author Leo Fajardo (@leorw)
107 + *
108 + * @param string $module_type
109 + * @param string $slug
110 + */
111 + private function __construct( $module_type, $slug ) {
112 + $this->_module_type = $module_type;
113 + $this->_module_slug = $slug;
114 + $this->_is_multisite = is_multisite();
115 +
116 + if ( $this->_is_multisite ) {
117 + $this->_blog_id = get_current_blog_id();
118 + $this->_network_storage = FS_Key_Value_Storage::instance( $module_type . '_data', $slug, true );
119 + }
120 +
121 + $this->_storage = FS_Key_Value_Storage::instance( $module_type . '_data', $slug, $this->_blog_id );
122 + }
123 +
124 + /**
125 + * Tells this storage wrapper class that the context plugin is network active. This flag will affect how values
126 + * are retrieved/stored from/into the storage.
127 + *
128 + * @author Leo Fajardo (@leorw)
129 + *
130 + * @param bool $is_network_active
131 + * @param bool $is_delegated_connection
132 + */
133 + function set_network_active( $is_network_active = true, $is_delegated_connection = false ) {
134 + $this->_is_network_active = $is_network_active;
135 + $this->_is_delegated_connection = $is_delegated_connection;
136 + }
137 +
138 + /**
139 + * Switch the context of the site level storage manager.
140 + *
141 + * @author Vova Feldman (@svovaf)
142 + * @since 2.0.0
143 + *
144 + * @param int $blog_id
145 + */
146 + function set_site_blog_context( $blog_id ) {
147 + $this->_storage = $this->get_site_storage( $blog_id );
148 + $this->_blog_id = $blog_id;
149 + }
150 +
151 + /**
152 + * @author Leo Fajardo (@leorw)
153 + *
154 + * @param string $key
155 + * @param mixed $value
156 + * @param null|bool|int $network_level_or_blog_id When an integer, use the given blog storage. When `true` use the multisite storage (if there's a network). When `false`, use the current context blog storage. When `null`, the decision which storage to use (MS vs. Current S) will be handled internally and determined based on the $option (based on self::$_BINARY_MAP).
157 + * @param int $option_level Since 2.5.1
158 + * @param bool $flush
159 + */
160 + function store(
161 + $key,
162 + $value,
163 + $network_level_or_blog_id = null,
164 + $option_level = self::OPTION_LEVEL_UNDEFINED,
165 + $flush = true
166 + ) {
167 + if ( $this->should_use_network_storage( $key, $network_level_or_blog_id, $option_level ) ) {
168 + $this->_network_storage->store( $key, $value, $flush );
169 + } else {
170 + $storage = $this->get_site_storage( $network_level_or_blog_id );
171 + $storage->store( $key, $value, $flush );
172 + }
173 + }
174 +
175 + /**
176 + * @author Leo Fajardo (@leorw)
177 + *
178 + * @param bool $store
179 + * @param string[] $exceptions Set of keys to keep and not clear.
180 + * @param int|null|bool $network_level_or_blog_id
181 + */
182 + function clear_all( $store = true, $exceptions = array(), $network_level_or_blog_id = null ) {
183 + if ( ! $this->_is_multisite ||
184 + false === $network_level_or_blog_id ||
185 + is_null( $network_level_or_blog_id ) ||
186 + is_numeric( $network_level_or_blog_id )
187 + ) {
188 + $storage = $this->get_site_storage( $network_level_or_blog_id );
189 + $storage->clear_all( $store, $exceptions );
190 + }
191 +
192 + if ( $this->_is_multisite &&
193 + ( true === $network_level_or_blog_id || is_null( $network_level_or_blog_id ) )
194 + ) {
195 + $this->_network_storage->clear_all( $store, $exceptions );
196 + }
197 + }
198 +
199 + /**
200 + * @author Leo Fajardo (@leorw)
201 + *
202 + * @param string $key
203 + * @param bool $store
204 + * @param null|bool|int $network_level_or_blog_id When an integer, use the given blog storage. When `true` use the multisite storage (if there's a network). When `false`, use the current context blog storage. When `null`, the decision which storage to use (MS vs. Current S) will be handled internally and determined based on the $option (based on self::$_BINARY_MAP).
205 + */
206 + function remove( $key, $store = true, $network_level_or_blog_id = null ) {
207 + if ( $this->should_use_network_storage( $key, $network_level_or_blog_id ) ) {
208 + $this->_network_storage->remove( $key, $store );
209 + } else {
210 + $storage = $this->get_site_storage( $network_level_or_blog_id );
211 + $storage->remove( $key, $store );
212 + }
213 + }
214 +
215 + /**
216 + * @author Leo Fajardo (@leorw)
217 + *
218 + * @param string $key
219 + * @param mixed $default
220 + * @param null|bool|int $network_level_or_blog_id When an integer, use the given blog storage. When `true` use the multisite storage (if there's a network). When `false`, use the current context blog storage. When `null`, the decision which storage to use (MS vs. Current S) will be handled internally and determined based on the $option (based on self::$_BINARY_MAP).
221 + * @param int $option_level Since 2.5.1
222 + *
223 + * @return mixed
224 + */
225 + function get(
226 + $key,
227 + $default = false,
228 + $network_level_or_blog_id = null,
229 + $option_level = self::OPTION_LEVEL_UNDEFINED
230 + ) {
231 + if ( $this->should_use_network_storage( $key, $network_level_or_blog_id, $option_level ) ) {
232 + return $this->_network_storage->get( $key, $default );
233 + } else {
234 + $storage = $this->get_site_storage( $network_level_or_blog_id );
235 +
236 + return $storage->get( $key, $default );
237 + }
238 + }
239 +
240 + /**
241 + * Multisite activated:
242 + * true: Save network storage.
243 + * int: Save site specific storage.
244 + * false|0: Save current site storage.
245 + * null: Save network and current site storage.
246 + * Site level activated:
247 + * Save site storage.
248 + *
249 + * @author Vova Feldman (@svovaf)
250 + * @since 2.0.0
251 + *
252 + * @param bool|int|null $network_level_or_blog_id
253 + */
254 + function save( $network_level_or_blog_id = null ) {
255 + if ( $this->_is_network_active &&
256 + ( true === $network_level_or_blog_id || is_null( $network_level_or_blog_id ) )
257 + ) {
258 + $this->_network_storage->save();
259 + }
260 +
261 + if ( ! $this->_is_network_active || true !== $network_level_or_blog_id ) {
262 + $storage = $this->get_site_storage( $network_level_or_blog_id );
263 + $storage->save();
264 + }
265 + }
266 +
267 + /**
268 + * @author Vova Feldman (@svovaf)
269 + * @since 2.0.0
270 + *
271 + * @return string
272 + */
273 + function get_module_slug() {
274 + return $this->_module_slug;
275 + }
276 +
277 + /**
278 + * @author Vova Feldman (@svovaf)
279 + * @since 2.0.0
280 + *
281 + * @return string
282 + */
283 + function get_module_type() {
284 + return $this->_module_type;
285 + }
286 +
287 + /**
288 + * Migration script to the new storage data structure that is network compatible.
289 + *
290 + * IMPORTANT:
291 + * This method should be executed only after it is determined if this is a network
292 + * level compatible product activation.
293 + *
294 + * @author Vova Feldman (@svovaf)
295 + * @since 2.0.0
296 + */
297 + function migrate_to_network() {
298 + if ( ! $this->_is_multisite ) {
299 + return;
300 + }
301 +
302 + $updated = false;
303 +
304 + if ( ! isset( self::$_NETWORK_OPTIONS_MAP ) ) {
305 + self::load_network_options_map();
306 + }
307 +
308 + foreach ( self::$_NETWORK_OPTIONS_MAP as $option => $storage_level ) {
309 + if ( ! $this->is_multisite_option( $option ) ) {
310 + continue;
311 + }
312 +
313 + if ( isset( $this->_storage->{$option} ) && ! isset( $this->_network_storage->{$option} ) ) {
314 + // Migrate option to the network storage.
315 + $this->_network_storage->store( $option, $this->_storage->{$option}, false );
316 +
317 + $updated = true;
318 + }
319 + }
320 +
321 + if ( ! $updated ) {
322 + return;
323 + }
324 +
325 + // Update network level storage.
326 + $this->_network_storage->save();
327 +// $this->_storage->save();
328 + }
329 +
330 + #--------------------------------------------------------------------------------
331 + #region Helper Methods
332 + #--------------------------------------------------------------------------------
333 +
334 + /**
335 + * We don't want to load the map right away since it's not even needed in a non-MS environment.
336 + *
337 + * Example:
338 + * array(
339 + * 'option1' => 0, // Means that the option should always be stored on the network level.
340 + * 'option2' => 1, // Means that the option should be stored on the network level only when the module was network level activated.
341 + * 'option2' => 2, // Means that the option should be stored on the network level only when the module was network level activated AND the connection was NOT delegated.
342 + * 'option3' => 3, // Means that the option should always be stored on the site level.
343 + * )
344 + *
345 + * @author Vova Feldman (@svovaf)
346 + * @since 2.0.0
347 + */
348 + private static function load_network_options_map() {
349 + self::$_NETWORK_OPTIONS_MAP = array(
350 + // Network level options.
351 + 'affiliate_application_data' => self::OPTION_LEVEL_NETWORK,
352 + 'beta_data' => self::OPTION_LEVEL_NETWORK,
353 + 'connectivity_test' => self::OPTION_LEVEL_NETWORK,
354 + 'handle_gdpr_admin_notice' => self::OPTION_LEVEL_NETWORK,
355 + 'has_trial_plan' => self::OPTION_LEVEL_NETWORK,
356 + 'install_sync_timestamp' => self::OPTION_LEVEL_NETWORK,
357 + 'install_sync_cron' => self::OPTION_LEVEL_NETWORK,
358 + 'is_anonymous_ms' => self::OPTION_LEVEL_NETWORK,
359 + 'is_network_activated' => self::OPTION_LEVEL_NETWORK,
360 + 'is_on' => self::OPTION_LEVEL_NETWORK,
361 + 'is_plugin_new_install' => self::OPTION_LEVEL_NETWORK,
362 + 'network_install_blog_id' => self::OPTION_LEVEL_NETWORK,
363 + 'pending_sites_info' => self::OPTION_LEVEL_NETWORK,
364 + 'plugin_last_version' => self::OPTION_LEVEL_NETWORK,
365 + 'plugin_main_file' => self::OPTION_LEVEL_NETWORK,
366 + 'plugin_version' => self::OPTION_LEVEL_NETWORK,
367 + 'sdk_downgrade_mode' => self::OPTION_LEVEL_NETWORK,
368 + 'sdk_last_version' => self::OPTION_LEVEL_NETWORK,
369 + 'sdk_upgrade_mode' => self::OPTION_LEVEL_NETWORK,
370 + 'sdk_version' => self::OPTION_LEVEL_NETWORK,
371 + 'sticky_optin_added_ms' => self::OPTION_LEVEL_NETWORK,
372 + 'subscriptions' => self::OPTION_LEVEL_NETWORK,
373 + 'sync_timestamp' => self::OPTION_LEVEL_NETWORK,
374 + 'sync_cron' => self::OPTION_LEVEL_NETWORK,
375 + 'was_plugin_loaded' => self::OPTION_LEVEL_NETWORK,
376 + 'network_user_id' => self::OPTION_LEVEL_NETWORK,
377 + 'plugin_upgrade_mode' => self::OPTION_LEVEL_NETWORK,
378 + 'plugin_downgrade_mode' => self::OPTION_LEVEL_NETWORK,
379 + 'is_network_connected' => self::OPTION_LEVEL_NETWORK,
380 + /**
381 + * Special flag that is used when a super-admin upgrades to the new version of the SDK that supports network level integration, when the connection decision wasn't made for all the sites in the network.
382 + */
383 + 'is_network_activation' => self::OPTION_LEVEL_NETWORK,
384 + 'license_migration' => self::OPTION_LEVEL_NETWORK,
385 +
386 + // When network activated, then network level.
387 + 'install_timestamp' => self::OPTION_LEVEL_NETWORK_ACTIVATED,
388 + 'prev_is_premium' => self::OPTION_LEVEL_NETWORK_ACTIVATED,
389 + 'require_license_activation' => self::OPTION_LEVEL_NETWORK_ACTIVATED,
390 +
391 + // If not network activated OR delegated, then site level.
392 + 'activation_timestamp' => self::OPTION_LEVEL_NETWORK_ACTIVATED_NOT_DELEGATED,
393 + 'expired_license_notice_shown' => self::OPTION_LEVEL_NETWORK_ACTIVATED_NOT_DELEGATED,
394 + 'is_whitelabeled' => self::OPTION_LEVEL_NETWORK_ACTIVATED_NOT_DELEGATED,
395 + 'last_license_key' => self::OPTION_LEVEL_NETWORK_ACTIVATED_NOT_DELEGATED,
396 + 'last_license_user_id' => self::OPTION_LEVEL_NETWORK_ACTIVATED_NOT_DELEGATED,
397 + 'prev_user_id' => self::OPTION_LEVEL_NETWORK_ACTIVATED_NOT_DELEGATED,
398 + 'sticky_optin_added' => self::OPTION_LEVEL_NETWORK_ACTIVATED_NOT_DELEGATED,
399 + 'uninstall_reason' => self::OPTION_LEVEL_NETWORK_ACTIVATED_NOT_DELEGATED,
400 + 'is_pending_activation' => self::OPTION_LEVEL_NETWORK_ACTIVATED_NOT_DELEGATED,
401 + 'pending_license_key' => self::OPTION_LEVEL_NETWORK_ACTIVATED_NOT_DELEGATED,
402 +
403 + // Site level options.
404 + 'is_anonymous' => self::OPTION_LEVEL_SITE,
405 + 'clone_id' => self::OPTION_LEVEL_SITE,
406 + );
407 + }
408 +
409 + /**
410 + * This method will and should only be executed when is_multisite() is true.
411 + *
412 + * @author Vova Feldman (@svovaf)
413 + * @since 2.0.0
414 + *
415 + * @param string $key
416 + * @param int $option_level Since 2.5.1
417 + *
418 + * @return bool
419 + */
420 + private function is_multisite_option( $key, $option_level = self::OPTION_LEVEL_UNDEFINED ) {
421 + if ( ! isset( self::$_NETWORK_OPTIONS_MAP ) ) {
422 + self::load_network_options_map();
423 + }
424 +
425 + if (
426 + self::OPTION_LEVEL_UNDEFINED === $option_level &&
427 + isset( self::$_NETWORK_OPTIONS_MAP[ $key ] )
428 + ) {
429 + $option_level = self::$_NETWORK_OPTIONS_MAP[ $key ];
430 + }
431 +
432 + if ( self::OPTION_LEVEL_UNDEFINED === $option_level ) {
433 + // Option not found -> use site level storage.
434 + return false;
435 + }
436 +
437 + if ( self::OPTION_LEVEL_NETWORK === $option_level ) {
438 + // Option found and set to always use the network level storage on a multisite.
439 + return true;
440 + }
441 +
442 + if ( self::OPTION_LEVEL_SITE === $option_level ) {
443 + // Option found and set to always use the site level storage on a multisite.
444 + return false;
445 + }
446 +
447 + if ( ! $this->_is_network_active ) {
448 + return false;
449 + }
450 +
451 + if ( self::OPTION_LEVEL_NETWORK_ACTIVATED === $option_level ) {
452 + // Network activated.
453 + return true;
454 + }
455 +
456 + if (
457 + self::OPTION_LEVEL_NETWORK_ACTIVATED_NOT_DELEGATED === $option_level &&
458 + ! $this->_is_delegated_connection
459 + ) {
460 + // Network activated and not delegated.
461 + return true;
462 + }
463 +
464 + return false;
465 + }
466 +
467 + /**
468 + * @author Leo Fajardo
469 + *
470 + * @param string $key
471 + * @param null|bool|int $network_level_or_blog_id When an integer, use the given blog storage. When `true` use the multisite storage (if there's a network). When `false`, use the current context blog storage. When `null`, the decision which storage to use (MS vs. Current S) will be handled internally and determined based on the $option (based on self::$_BINARY_MAP).
472 + * @param int $option_level Since 2.5.1
473 + *
474 + * @return bool
475 + */
476 + private function should_use_network_storage(
477 + $key,
478 + $network_level_or_blog_id = null,
479 + $option_level = self::OPTION_LEVEL_UNDEFINED
480 + ) {
481 + if ( ! $this->_is_multisite ) {
482 + // Not a multisite environment.
483 + return false;
484 + }
485 +
486 + if ( is_numeric( $network_level_or_blog_id ) ) {
487 + // Explicitly asked to use a specified blog storage.
488 + return false;
489 + }
490 +
491 + if ( is_bool( $network_level_or_blog_id ) ) {
492 + // Explicitly specified whether it should use the network or blog level storage.
493 + return $network_level_or_blog_id;
494 + }
495 +
496 + // Determine which storage to use based on the option.
497 + return $this->is_multisite_option( $key, $option_level );
498 + }
499 +
500 + /**
501 + * @author Vova Feldman (@svovaf)
502 + * @since 2.0.0
503 + *
504 + * @param int $blog_id
505 + *
506 + * @return \FS_Key_Value_Storage
507 + */
508 + private function get_site_storage( $blog_id = 0 ) {
509 + if ( ! is_numeric( $blog_id ) ||
510 + $blog_id == $this->_blog_id ||
511 + 0 == $blog_id
512 + ) {
513 + return $this->_storage;
514 + }
515 +
516 + return FS_Key_Value_Storage::instance(
517 + $this->_module_type . '_data',
518 + $this->_storage->get_secondary_id(),
519 + $blog_id
520 + );
521 + }
522 +
523 + #endregion
524 +
525 + #--------------------------------------------------------------------------------
526 + #region Magic methods
527 + #--------------------------------------------------------------------------------
528 +
529 + function __set( $k, $v ) {
530 + if ( $this->should_use_network_storage( $k ) ) {
531 + $this->_network_storage->{$k} = $v;
532 + } else {
533 + $this->_storage->{$k} = $v;
534 + }
535 + }
536 +
537 + function __isset( $k ) {
538 + return $this->should_use_network_storage( $k ) ?
539 + isset( $this->_network_storage->{$k} ) :
540 + isset( $this->_storage->{$k} );
541 + }
542 +
543 + function __unset( $k ) {
544 + if ( $this->should_use_network_storage( $k ) ) {
545 + unset( $this->_network_storage->{$k} );
546 + } else {
547 + unset( $this->_storage->{$k} );
548 + }
549 + }
550 +
551 + function __get( $k ) {
552 + return $this->should_use_network_storage( $k ) ?
553 + $this->_network_storage->{$k} :
554 + $this->_storage->{$k};
555 + }
556 +
557 + #endregion
558 + }