PluginProbe
Buttonizer – Floating Menus, Sticky Buttons, & Popup Builder / 1.4.4
Buttonizer – Floating Menus, Sticky Buttons, & Popup Builder v1.4.4
3.6.0 3.5.0 trunk 1.0.10 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.6.1 1.0.7 1.0.8 1.0.9 1.1 1.1.1 1.2 1.3 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.5 1.5.1 All 110 releases
buttonizer-multifunctional-button / freemius / includes / managers / class-fs-option-manager.php

class-fs-option-manager.php in Buttonizer – Floating Menus, Sticky Buttons, & Popup Builder 1.4.4, at freemius/includes/managers/class-fs-option-manager.php

353 lines 8.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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.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 ( isset( $this->_options ) ) {
104 // Clear prev options.
105 $this->clear();
106 }
107
108 if ( ! WP_FS__DEBUG_SDK ) {
109 $this->_options = wp_cache_get( $option_name, WP_FS__SLUG );
110 }
111
112 // $this->_logger->info('wp_cache_get = ' . var_export($this->_options, true));
113
114 // if ( is_array( $this->_options ) ) {
115 // $this->clear();
116 // }
117
118 $cached = true;
119
120 if ( empty( $this->_options ) ) {
121 $this->_options = get_option( $option_name );
122
123 if ( is_string( $this->_options ) ) {
124 $this->_options = json_decode( $this->_options );
125 }
126
127 // $this->_logger->info('get_option = ' . var_export($this->_options, true));
128
129 if ( false === $this->_options ) {
130 $this->clear();
131 }
132
133 $cached = false;
134 }
135
136 if ( ! WP_FS__DEBUG_SDK && ! $cached ) // Set non encoded cache.
137 {
138 wp_cache_set( $option_name, $this->_options, WP_FS__SLUG );
139 }
140 }
141 }
142
143 /**
144 * @author Vova Feldman (@svovaf)
145 * @since 1.0.3
146 *
147 * @return bool
148 */
149 function is_loaded() {
150 return isset( $this->_options );
151 }
152
153 /**
154 * @author Vova Feldman (@svovaf)
155 * @since 1.0.3
156 *
157 * @return bool
158 */
159 function is_empty() {
160 return ( $this->is_loaded() && false === $this->_options );
161 }
162
163 /**
164 * @author Vova Feldman (@svovaf)
165 * @since 1.0.6
166 *
167 * @param bool $flush
168 */
169 function clear( $flush = false ) {
170 $this->_logger->entrance();
171
172 $this->_options = array();
173
174 if ( $flush ) {
175 $this->store();
176 }
177 }
178
179 /**
180 * Delete options manager from DB.
181 *
182 * @author Vova Feldman (@svovaf)
183 * @since 1.0.9
184 */
185 function delete() {
186 delete_option( $this->_get_option_manager_name() );
187 }
188
189 /**
190 * @author Vova Feldman (@svovaf)
191 * @since 1.0.6
192 *
193 * @param string $option
194 *
195 * @return bool
196 */
197 function has_option( $option ) {
198 return array_key_exists( $option, $this->_options );
199 }
200
201 /**
202 * @author Vova Feldman (@svovaf)
203 * @since 1.0.3
204 *
205 * @param string $option
206 * @param mixed $default
207 *
208 * @return mixed
209 */
210 function get_option( $option, $default = null ) {
211 $this->_logger->entrance( 'option = ' . $option );
212
213 if ( is_array( $this->_options ) ) {
214 $value = isset( $this->_options[ $option ] ) ?
215 $this->_options[ $option ] :
216 $default;
217 } else if ( is_object( $this->_options ) ) {
218 $value = isset( $this->_options->{$option} ) ?
219 $this->_options->{$option} :
220 $default;
221 } else {
222 $value = $default;
223 }
224
225 /**
226 * If it's an object, return a clone of the object, otherwise,
227 * external changes of the object will actually change the value
228 * of the object in the options manager which may lead to an unexpected
229 * behaviour and data integrity when a store() call is triggered.
230 *
231 * Example:
232 * $object1 = $options->get_option( 'object1' );
233 * $object1->x = 123;
234 *
235 * $object2 = $options->get_option( 'object2' );
236 * $object2->y = 'dummy';
237 *
238 * $options->set_option( 'object2', $object2, true );
239 *
240 * If we don't return a clone of option 'object1', setting 'object2'
241 * will also store the updated value of 'object1' which is quite not
242 * an expected behaviour.
243 *
244 * @author Vova Feldman
245 */
246 return is_object($value) ? clone $value : $value;
247 }
248
249 /**
250 * @author Vova Feldman (@svovaf)
251 * @since 1.0.3
252 *
253 * @param string $option
254 * @param mixed $value
255 * @param bool $flush
256 */
257 function set_option( $option, $value, $flush = false ) {
258 $this->_logger->entrance( 'option = ' . $option );
259
260 if ( ! $this->is_loaded() ) {
261 $this->clear();
262 }
263
264 /**
265 * If it's an object, store a clone of the object, otherwise,
266 * external changes of the object will actually change the value
267 * of the object in the options manager which may lead to an unexpected
268 * behaviour and data integrity when a store() call is triggered.
269 *
270 * Example:
271 * $object1 = new stdClass();
272 * $object1->x = 123;
273 *
274 * $options->set_option( 'object1', $object1 );
275 *
276 * $object1->x = 456;
277 *
278 * $options->set_option( 'object2', $object2, true );
279 *
280 * If we don't set the option as a clone of option 'object1', setting 'object2'
281 * will also store the updated value of 'object1' ($object1->x = 456 instead of
282 * $object1->x = 123) which is quite not an expected behaviour.
283 *
284 * @author Vova Feldman
285 */
286 $copy = is_object($value) ? clone $value : $value;
287
288 if ( is_array( $this->_options ) ) {
289 $this->_options[ $option ] = $copy;
290 } else if ( is_object( $this->_options ) ) {
291 $this->_options->{$option} = $copy;
292 }
293
294 if ( $flush ) {
295 $this->store();
296 }
297 }
298
299 /**
300 * Unset option.
301 *
302 * @author Vova Feldman (@svovaf)
303 * @since 1.0.3
304 *
305 * @param string $option
306 * @param bool $flush
307 */
308 function unset_option( $option, $flush = false ) {
309 $this->_logger->entrance( 'option = ' . $option );
310
311 if ( is_array( $this->_options ) ) {
312 if ( ! isset( $this->_options[ $option ] ) ) {
313 return;
314 }
315
316 unset( $this->_options[ $option ] );
317
318 } else if ( is_object( $this->_options ) ) {
319 if ( ! isset( $this->_options->{$option} ) ) {
320 return;
321 }
322
323 unset( $this->_options->{$option} );
324 }
325
326 if ( $flush ) {
327 $this->store();
328 }
329 }
330
331 /**
332 * Dump options to database.
333 *
334 * @author Vova Feldman (@svovaf)
335 * @since 1.0.3
336 */
337 function store() {
338 $this->_logger->entrance();
339
340 $option_name = $this->_get_option_manager_name();
341
342 if ( $this->_logger->is_on() ) {
343 $this->_logger->info( $option_name . ' = ' . var_export( $this->_options, true ) );
344 }
345
346 // Update DB.
347 update_option( $option_name, $this->_options );
348
349 if ( ! WP_FS__DEBUG_SDK ) {
350 wp_cache_set( $option_name, $this->_options, WP_FS__SLUG );
351 }
352 }
353 }