class-fs-options.php 16.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
<?php
    /**
     * @package     Freemius
     * @copyright   Copyright (c) 2015, Freemius, Inc.
     * @license     https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License Version 3
     * @since       1.2.3
     */

    if ( ! defined( 'ABSPATH' ) ) {
        exit;
    }

    /**
     * Class FS_Options
     *
     * A wrapper class for handling network level and single site level options.
     */
    class FS_Options {
        /**
         * @var string
         */
        private $_id;

        /**
         * @var array[string]FS_Options {
         * @key   string
         * @value FS_Options
         * }
         */
        private static $_instances;

        /**
         * @var FS_Option_Manager Site level options.
         */
        private $_options;

        /**
         * @var FS_Option_Manager Network level options.
         */
        private $_network_options;

        /**
         * @var int The ID of the blog that is associated with the current site level options.
         */
        private $_blog_id = 0;

        /**
         * @var bool
         */
        private $_is_multisite;

        /**
         * @var string[] Lazy collection of params on the site level.
         */
        private static $_SITE_OPTIONS_MAP;

        /**
         * @author Leo Fajardo (@leorw)
         * @since  2.0.0
         *
         * @param string $id
         * @param bool   $load
         *
         * @return FS_Options
         */
        static function instance( $id, $load = false ) {
            if ( ! isset( self::$_instances[ $id ] ) ) {
                self::$_instances[ $id ] = new FS_Options( $id, $load );
            }

            return self::$_instances[ $id ];
        }

        /**
         * @author Leo Fajardo (@leorw)
         * @since  2.0.0
         *
         * @param string $id
         * @param bool   $load
         */
        private function __construct( $id, $load = false ) {
            $this->_id           = $id;
            $this->_is_multisite = is_multisite();

            if ( $this->_is_multisite ) {
                $this->_blog_id         = get_current_blog_id();
                $this->_network_options = FS_Option_Manager::get_manager( $id, $load, true );
            }

            $this->_options = FS_Option_Manager::get_manager( $id, $load, $this->_blog_id );
        }

        /**
         * Switch the context of the site level options manager.
         *
         * @author Vova Feldman (@svovaf)
         * @since  2.0.0
         *
         * @param $blog_id
         */
        function set_site_blog_context( $blog_id ) {
            $this->_blog_id = $blog_id;

            $this->_options = FS_Option_Manager::get_manager( $this->_id, false, $this->_blog_id );
        }

        /**
         * @author Leo Fajardo (@leorw)
         *
         * @param string        $option
         * @param mixed         $default
         * @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::$_SITE_LEVEL_PARAMS).
         *
         * @return mixed
         */
        function get_option( $option, $default = null, $network_level_or_blog_id = null ) {
            if ( $this->should_use_network_storage( $option, $network_level_or_blog_id ) ) {
                return $this->_network_options->get_option( $option, $default );
            }

            $site_options = $this->get_site_options( $network_level_or_blog_id );

            return $site_options->get_option( $option, $default );
        }

        /**
         * @author Leo Fajardo (@leorw)
         * @since  2.0.0
         *
         * @param string        $option
         * @param mixed         $value
         * @param bool          $flush
         * @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::$_SITE_LEVEL_PARAMS).
         */
        function set_option( $option, $value, $flush = false, $network_level_or_blog_id = null ) {
            if ( $this->should_use_network_storage( $option, $network_level_or_blog_id ) ) {
                $this->_network_options->set_option( $option, $value, $flush );
            } else {
                $site_options = $this->get_site_options( $network_level_or_blog_id );
                $site_options->set_option( $option, $value, $flush );
            }
        }

        /**
         * @author Vova Feldman (@svovaf)
         * @since  2.0.0
         *
         * @param string        $option
         * @param bool          $flush
         * @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::$_SITE_LEVEL_PARAMS).
         */
        function unset_option( $option, $flush = false, $network_level_or_blog_id = null ) {
            if ( $this->should_use_network_storage( $option, $network_level_or_blog_id ) ) {
                $this->_network_options->unset_option( $option, $flush );
            } else {
                $site_options = $this->get_site_options( $network_level_or_blog_id );
                $site_options->unset_option( $option, $flush );
            }
        }

        /**
         * @author Leo Fajardo (@leorw)
         * @since  2.0.0
         *
         * @param bool $flush
         * @param bool $network_level
         */
        function load( $flush = false, $network_level = true ) {
            if ( $this->_is_multisite && $network_level ) {
                $this->_network_options->load( $flush );
            } else {
                $this->_options->load( $flush );
            }
        }

        /**
         * @author Leo Fajardo (@leorw)
         * @since  2.0.0
         *
         * @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`, store both network storage and the current context blog storage.
         */
        function store( $network_level_or_blog_id = null ) {
            if ( ! $this->_is_multisite ||
                 false === $network_level_or_blog_id ||
                 0 == $network_level_or_blog_id ||
                 is_null( $network_level_or_blog_id )
            ) {
                $site_options = $this->get_site_options( $network_level_or_blog_id );
                $site_options->store();
            }

            if ( $this->_is_multisite &&
                 ( is_null( $network_level_or_blog_id ) || true === $network_level_or_blog_id )
            ) {
                $this->_network_options->store();
            }
        }

        /**
         * @author Vova Feldman (@svovaf)
         * @since  2.0.0
         *
         * @param int|null|bool $network_level_or_blog_id
         * @param bool          $flush
         */
        function clear( $network_level_or_blog_id = null, $flush = false ) {
            if ( ! $this->_is_multisite ||
                 false === $network_level_or_blog_id ||
                 is_null( $network_level_or_blog_id ) ||
                 is_numeric( $network_level_or_blog_id )
            ) {
                $site_options = $this->get_site_options( $network_level_or_blog_id );
                $site_options->clear( $flush );
            }

            if ( $this->_is_multisite &&
                 ( true === $network_level_or_blog_id || is_null( $network_level_or_blog_id ) )
            ) {
                $this->_network_options->clear( $flush );
            }
        }

        /**
         * Migration script to the new storage data structure that is network compatible.
         *
         * IMPORTANT:
         *      This method should be executed only after it is determined if this is a network
         *      level compatible product activation.
         *
         * @author Vova Feldman (@svovaf)
         * @since  2.0.0
         *
         * @param int $blog_id
         */
        function migrate_to_network( $blog_id = 0 ) {
            if ( ! $this->_is_multisite ) {
                return;
            }

            $updated = false;

            $site_options = $this->get_site_options( $blog_id );

            $keys = $site_options->get_options_keys();

            foreach ( $keys as $option ) {
                if ( $this->is_site_option( $option ) ||
                     // Don't move admin notices to the network storage.
                    in_array($option, array(
                        // Don't move admin notices to the network storage.
                        'admin_notices',
                        // Don't migrate the module specific data, it will be migrated by the FS_Storage.
                        'plugin_data',
                        'theme_data',
                    ))
                ) {
                    continue;
                }

                $option_updated = false;

                // Migrate option to the network storage.
                $site_option = $site_options->get_option( $option );

                if ( ! $this->_network_options->has_option( $option ) ) {
                    // Option not set on the network level, so just set it.
                    $this->_network_options->set_option( $option, $site_option, false );

                    $option_updated = true;
                } else {
                    // Option already set on the network level, so we need to merge it inelegantly.
                    $network_option = $this->_network_options->get_option( $option );

                    if ( is_array( $network_option ) && is_array( $site_option ) ) {
                        // Option is an array.
                        foreach ( $site_option as $key => $value ) {
                            if ( ! isset( $network_option[ $key ] ) ) {
                                $network_option[ $key ] = $value;

                                $option_updated = true;
                            } else if ( is_array( $network_option[ $key ] ) && is_array( $value ) ) {
                                if ( empty( $network_option[ $key ] ) ) {
                                    $network_option[ $key ] = $value;

                                    $option_updated = true;
                                } else if ( empty( $value ) ) {
                                    // Do nothing.
                                } else {
                                    reset($value);
                                    $first_key = key($value);
                                    if ( $value[$first_key] instanceof FS_Entity ) {
                                        // Merge entities by IDs.
                                        $network_entities_ids = array();
                                        foreach ( $network_option[ $key ] as $entity ) {
                                            $network_entities_ids[ $entity->id ] = true;
                                        }

                                        foreach ( $value as $entity ) {
                                            if ( ! isset( $network_entities_ids[ $entity->id ] ) ) {
                                                $network_option[ $key ][] = $entity;

                                                $option_updated = true;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }

                    if ( $option_updated ) {
                        $this->_network_options->set_option( $option, $network_option, false );
                    }
                }

                /**
                 * Remove the option from site level storage.
                 *
                 * IMPORTANT:
                 *      The line below is intentionally commented since we want to preserve the option
                 *      on the site storage level for "downgrade compatibility". Basically, if the user
                 *      will downgrade to an older version of the plugin with the prev storage structure,
                 *      it will continue working.
                 *
                 * @todo After a few releases we can remove this.
                 */
//                    $site_options->unset_option($option, false);

                if ( $option_updated ) {
                    $updated = true;
                }
            }

            if ( ! $updated ) {
                return;
            }

            // Update network level storage.
            $this->_network_options->store();
//            $site_options->store();
        }


        #--------------------------------------------------------------------------------
        #region Helper Methods
        #--------------------------------------------------------------------------------

        /**
         * We don't want to load the map right away since it's not even needed in a non-MS environment.
         *
         * @author Vova Feldman (@svovaf)
         * @since  2.0.0
         */
        private static function load_site_options_map() {
            self::$_SITE_OPTIONS_MAP = array(
                'sites'          => true,
                'theme_sites'    => true,
                'unique_id'      => true,
                'active_plugins' => true,
            );
        }

        /**
         * @author Vova Feldman (@svovaf)
         * @since  2.0.0
         *
         * @param string $option
         *
         * @return bool
         */
        private function is_site_option( $option ) {
            if ( WP_FS__ACCOUNTS_OPTION_NAME != $this->_id ) {
                return false;
            }

            if ( ! isset( self::$_SITE_OPTIONS_MAP ) ) {
                self::load_site_options_map();
            }

            return isset( self::$_SITE_OPTIONS_MAP[ $option ] );
        }

        /**
         * @author Vova Feldman (@svovaf)
         * @since  2.0.0
         *
         * @param int $blog_id
         *
         * @return FS_Option_Manager
         */
        private function get_site_options( $blog_id = 0 ) {
            if ( 0 == $blog_id || $blog_id == $this->_blog_id ) {
                return $this->_options;
            }

            return FS_Option_Manager::get_manager( $this->_id, true, $blog_id );
        }

        /**
         * Check if an option should be stored on the MS network storage.
         *
         * @author Vova Feldman (@svovaf)
         * @since  2.0.0
         *
         * @param string        $option
         * @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::$_SITE_LEVEL_PARAMS).
         *
         * @return bool
         */
        private function should_use_network_storage( $option, $network_level_or_blog_id = null ) {
            if ( ! $this->_is_multisite ) {
                // Not a multisite environment.
                return false;
            }

            if ( is_numeric( $network_level_or_blog_id ) ) {
                // Explicitly asked to use a specified blog storage.
                return false;
            }

            if ( is_bool( $network_level_or_blog_id ) ) {
                // Explicitly specified whether should use the network or blog level storage.
                return $network_level_or_blog_id;
            }

            // Determine which storage to use based on the option.
            return ! $this->is_site_option( $option );
        }

        #endregion
    }