class-fs-admin-menu-manager.php
10 years ago
class-fs-admin-notice-manager.php
10 years ago
class-fs-key-value-storage.php
10 years ago
class-fs-license-manager.php
10 years ago
class-fs-option-manager.php
10 years ago
class-fs-plan-manager.php
10 years ago
class-fs-plugin-manager.php
10 years ago
class-fs-option-manager.php
297 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package Freemius |
| 4 | * @copyright Copyright (c) 2015, Freemius, Inc. |
| 5 | * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 6 | * @since 1.0.3 |
| 7 | */ |
| 8 | |
| 9 | if ( ! defined( 'ABSPATH' ) ) { |
| 10 | exit; |
| 11 | } |
| 12 | |
| 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. |
| 19 | * |
| 20 | * If load() is not explicitly called, starts as empty manager. Same thing about saving the data - you have to |
| 21 | * explicitly call store(). |
| 22 | * |
| 23 | * Class Freemius_Option_Manager |
| 24 | */ |
| 25 | class FS_Option_Manager { |
| 26 | /** |
| 27 | * @var string |
| 28 | */ |
| 29 | private $_id; |
| 30 | /** |
| 31 | * @var array |
| 32 | */ |
| 33 | private $_options; |
| 34 | /** |
| 35 | * @var FS_Logger |
| 36 | */ |
| 37 | private $_logger; |
| 38 | |
| 39 | /** |
| 40 | * @var FS_Option_Manager[] |
| 41 | */ |
| 42 | private static $_MANAGERS = array(); |
| 43 | |
| 44 | /** |
| 45 | * @author Vova Feldman (@svovaf) |
| 46 | * @since 1.0.3 |
| 47 | * |
| 48 | * @param string $id |
| 49 | * @param bool $load |
| 50 | */ |
| 51 | private function __construct( $id, $load = false ) { |
| 52 | $this->_logger = FS_Logger::get_logger( WP_FS__SLUG . '_opt_mngr_' . $id, WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK ); |
| 53 | |
| 54 | $this->_logger->entrance(); |
| 55 | $this->_logger->log( 'id = ' . $id ); |
| 56 | |
| 57 | $this->_id = $id; |
| 58 | |
| 59 | if ( $load ) { |
| 60 | $this->load(); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * @author Vova Feldman (@svovaf) |
| 66 | * @since 1.0.3 |
| 67 | * |
| 68 | * @param $id |
| 69 | * @param $load |
| 70 | * |
| 71 | * @return FS_Option_Manager |
| 72 | */ |
| 73 | static function get_manager( $id, $load = false ) { |
| 74 | $id = strtolower( $id ); |
| 75 | |
| 76 | if ( ! isset( self::$_MANAGERS[ $id ] ) ) { |
| 77 | self::$_MANAGERS[ $id ] = new FS_Option_Manager( $id, $load ); |
| 78 | } // If load required but not yet loaded, load. |
| 79 | else if ( $load && ! self::$_MANAGERS[ $id ]->is_loaded() ) { |
| 80 | self::$_MANAGERS[ $id ]->load(); |
| 81 | } |
| 82 | |
| 83 | return self::$_MANAGERS[ $id ]; |
| 84 | } |
| 85 | |
| 86 | private function _get_option_manager_name() { |
| 87 | // return WP_FS__SLUG . '_' . $this->_id; |
| 88 | return $this->_id; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * @author Vova Feldman (@svovaf) |
| 93 | * @since 1.0.3 |
| 94 | * |
| 95 | * @param bool $flush |
| 96 | */ |
| 97 | function load( $flush = false ) { |
| 98 | $this->_logger->entrance(); |
| 99 | |
| 100 | $option_name = $this->_get_option_manager_name(); |
| 101 | |
| 102 | if ( $flush || ! isset( $this->_options ) ) { |
| 103 | if ( ! WP_FS__DEBUG_SDK ) { |
| 104 | $this->_options = wp_cache_get( $option_name, WP_FS__SLUG ); |
| 105 | } |
| 106 | |
| 107 | // $this->_logger->info('wp_cache_get = ' . var_export($this->_options, true)); |
| 108 | |
| 109 | // if ( is_array( $this->_options ) ) { |
| 110 | // $this->clear(); |
| 111 | // } |
| 112 | |
| 113 | $cached = true; |
| 114 | |
| 115 | if ( empty( $this->_options ) ) { |
| 116 | $this->_options = get_option( $option_name ); |
| 117 | |
| 118 | if ( is_string( $this->_options ) ) { |
| 119 | $this->_options = json_decode( $this->_options ); |
| 120 | } |
| 121 | |
| 122 | // $this->_logger->info('get_option = ' . var_export($this->_options, true)); |
| 123 | |
| 124 | if ( false === $this->_options ) { |
| 125 | $this->clear(); |
| 126 | } |
| 127 | |
| 128 | $cached = false; |
| 129 | } |
| 130 | |
| 131 | if ( ! WP_FS__DEBUG_SDK && ! $cached ) // Set non encoded cache. |
| 132 | { |
| 133 | wp_cache_set( $option_name, $this->_options, WP_FS__SLUG ); |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * @author Vova Feldman (@svovaf) |
| 140 | * @since 1.0.3 |
| 141 | * |
| 142 | * @return bool |
| 143 | */ |
| 144 | function is_loaded() { |
| 145 | return isset( $this->_options ); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * @author Vova Feldman (@svovaf) |
| 150 | * @since 1.0.3 |
| 151 | * |
| 152 | * @return bool |
| 153 | */ |
| 154 | function is_empty() { |
| 155 | return ( $this->is_loaded() && false === $this->_options ); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * @author Vova Feldman (@svovaf) |
| 160 | * @since 1.0.6 |
| 161 | * |
| 162 | * @param bool $flush |
| 163 | */ |
| 164 | function clear( $flush = false ) { |
| 165 | $this->_logger->entrance(); |
| 166 | |
| 167 | $this->_options = array(); |
| 168 | |
| 169 | if ( $flush ) { |
| 170 | $this->store(); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Delete options manager from DB. |
| 176 | * |
| 177 | * @author Vova Feldman (@svovaf) |
| 178 | * @since 1.0.9 |
| 179 | */ |
| 180 | function delete() { |
| 181 | delete_option( $this->_get_option_manager_name() ); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * @author Vova Feldman (@svovaf) |
| 186 | * @since 1.0.6 |
| 187 | * |
| 188 | * @param string $option |
| 189 | * |
| 190 | * @return bool |
| 191 | */ |
| 192 | function has_option( $option ) { |
| 193 | return array_key_exists( $option, $this->_options ); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * @author Vova Feldman (@svovaf) |
| 198 | * @since 1.0.3 |
| 199 | * |
| 200 | * @param string $option |
| 201 | * @param mixed $default |
| 202 | * |
| 203 | * @return mixed |
| 204 | */ |
| 205 | function get_option( $option, $default = null ) { |
| 206 | $this->_logger->entrance( 'option = ' . $option ); |
| 207 | |
| 208 | if ( is_array( $this->_options ) ) { |
| 209 | return isset( $this->_options[ $option ] ) ? $this->_options[ $option ] : $default; |
| 210 | } else if ( is_object( $this->_options ) ) { |
| 211 | return isset( $this->_options->{$option} ) ? $this->_options->{$option} : $default; |
| 212 | } |
| 213 | |
| 214 | return $default; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * @author Vova Feldman (@svovaf) |
| 219 | * @since 1.0.3 |
| 220 | * |
| 221 | * @param string $option |
| 222 | * @param mixed $value |
| 223 | * @param bool $flush |
| 224 | */ |
| 225 | function set_option( $option, $value, $flush = false ) { |
| 226 | $this->_logger->entrance( 'option = ' . $option ); |
| 227 | |
| 228 | if ( ! $this->is_loaded() ) { |
| 229 | $this->clear(); |
| 230 | } |
| 231 | |
| 232 | if ( is_array( $this->_options ) ) { |
| 233 | $this->_options[ $option ] = $value; |
| 234 | } else if ( is_object( $this->_options ) ) { |
| 235 | $this->_options->{$option} = $value; |
| 236 | } |
| 237 | |
| 238 | if ( $flush ) { |
| 239 | $this->store(); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Unset option. |
| 245 | * |
| 246 | * @author Vova Feldman (@svovaf) |
| 247 | * @since 1.0.3 |
| 248 | * |
| 249 | * @param string $option |
| 250 | * @param bool $flush |
| 251 | */ |
| 252 | function unset_option( $option, $flush = false ) { |
| 253 | $this->_logger->entrance( 'option = ' . $option ); |
| 254 | |
| 255 | if ( is_array( $this->_options ) ) { |
| 256 | if ( ! isset( $this->_options[ $option ] ) ) { |
| 257 | return; |
| 258 | } |
| 259 | |
| 260 | unset( $this->_options[ $option ] ); |
| 261 | |
| 262 | } else if ( is_object( $this->_options ) ) { |
| 263 | if ( ! isset( $this->_options->{$option} ) ) { |
| 264 | return; |
| 265 | } |
| 266 | |
| 267 | unset( $this->_options->{$option} ); |
| 268 | } |
| 269 | |
| 270 | if ( $flush ) { |
| 271 | $this->store(); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Dump options to database. |
| 277 | * |
| 278 | * @author Vova Feldman (@svovaf) |
| 279 | * @since 1.0.3 |
| 280 | */ |
| 281 | function store() { |
| 282 | $this->_logger->entrance(); |
| 283 | |
| 284 | $option_name = $this->_get_option_manager_name(); |
| 285 | |
| 286 | if ( $this->_logger->is_on() ) { |
| 287 | $this->_logger->info( $option_name . ' = ' . var_export( $this->_options, true ) ); |
| 288 | } |
| 289 | |
| 290 | // Update DB. |
| 291 | update_option( $option_name, $this->_options ); |
| 292 | |
| 293 | if ( ! WP_FS__DEBUG_SDK ) { |
| 294 | wp_cache_set( $option_name, $this->_options, WP_FS__SLUG ); |
| 295 | } |
| 296 | } |
| 297 | } |