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-key-value-storage.php

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

328 lines 8.1 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.7
7 */
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit;
11 }
12
13 /**
14 * Class FS_Key_Value_Storage
15 *
16 * @property int $install_timestamp
17 * @property int $activation_timestamp
18 * @property int $sync_timestamp
19 * @property object $sync_cron
20 * @property int $install_sync_timestamp
21 * @property array $connectivity_test
22 * @property array $is_on
23 * @property object $trial_plan
24 * @property bool $has_trial_plan
25 * @property bool $trial_promotion_shown
26 * @property string $sdk_version
27 * @property string $sdk_last_version
28 * @property bool $sdk_upgrade_mode
29 * @property bool $sdk_downgrade_mode
30 * @property bool $plugin_upgrade_mode
31 * @property bool $plugin_downgrade_mode
32 * @property string $plugin_version
33 * @property string $plugin_last_version
34 * @property bool $is_plugin_new_install
35 * @property bool $was_plugin_loaded
36 * @property object $plugin_main_file
37 * @property bool $prev_is_premium
38 * @property array $is_anonymous
39 * @property bool $is_pending_activation
40 * @property bool $sticky_optin_added
41 * @property object $uninstall_reason
42 * @property object $subscription
43 */
44 class FS_Key_Value_Storage implements ArrayAccess, Iterator, Countable {
45 /**
46 * @var string
47 */
48 protected $_id;
49 /**
50 * @since 1.2.2
51 *
52 * @var string
53 */
54 protected $_secondary_id;
55 /**
56 * @var array
57 */
58 protected $_data;
59
60 /**
61 * @var FS_Plugin_Manager[]
62 */
63 private static $_instances = array();
64 /**
65 * @var FS_Logger
66 */
67 protected $_logger;
68
69 /**
70 * @param string $id
71 * @param string $secondary_id
72 *
73 * @return FS_Key_Value_Storage
74 */
75 static function instance( $id, $secondary_id ) {
76 $key = $id . ':' . $secondary_id;
77 if ( ! isset( self::$_instances[ $key ] ) ) {
78 self::$_instances[ $key ] = new FS_Key_Value_Storage( $id, $secondary_id );
79 }
80
81 return self::$_instances[ $key ];
82 }
83
84 protected function __construct( $id, $secondary_id ) {
85 $this->_logger = FS_Logger::get_logger( WP_FS__SLUG . '_' . $secondary_id . '_' . $id, WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK );
86
87 $this->_secondary_id = $secondary_id;
88 $this->_id = $id;
89 $this->load();
90 }
91
92 protected function get_option_manager() {
93 return FS_Option_Manager::get_manager( WP_FS__ACCOUNTS_OPTION_NAME, true );
94 }
95
96 protected function get_all_data() {
97 return $this->get_option_manager()->get_option( $this->_id, array() );
98 }
99
100 /**
101 * Load plugin data from local DB.
102 *
103 * @author Vova Feldman (@svovaf)
104 * @since 1.0.7
105 */
106 function load() {
107 $all_plugins_data = $this->get_all_data();
108 $this->_data = isset( $all_plugins_data[ $this->_secondary_id ] ) ?
109 $all_plugins_data[ $this->_secondary_id ] :
110 array();
111 }
112
113 /**
114 * @author Vova Feldman (@svovaf)
115 * @since 1.0.7
116 *
117 * @param string $key
118 * @param mixed $value
119 * @param bool $flush
120 */
121 function store( $key, $value, $flush = true ) {
122 if ( $this->_logger->is_on() ) {
123 $this->_logger->entrance( $key . ' = ' . var_export( $value, true ) );
124 }
125
126 if ( array_key_exists( $key, $this->_data ) && $value === $this->_data[ $key ] ) {
127 // No need to store data if the value wasn't changed.
128 return;
129 }
130
131 $all_data = $this->get_all_data();
132
133 $this->_data[ $key ] = $value;
134
135 $all_data[ $this->_secondary_id ] = $this->_data;
136
137 $options_manager = $this->get_option_manager();
138 $options_manager->set_option( $this->_id, $all_data, $flush );
139 }
140
141 /**
142 * @author Vova Feldman (@svovaf)
143 * @since 1.0.7
144 *
145 * @param bool $store
146 * @param string[] $exceptions Set of keys to keep and not clear.
147 */
148 function clear_all( $store = true, $exceptions = array() ) {
149 $new_data = array();
150 foreach ( $exceptions as $key ) {
151 if ( isset( $this->_data[ $key ] ) ) {
152 $new_data[ $key ] = $this->_data[ $key ];
153 }
154 }
155
156 $this->_data = $new_data;
157
158 if ( $store ) {
159 $all_data = $this->get_all_data();
160 $all_data[ $this->_secondary_id ] = $this->_data;
161 $options_manager = $this->get_option_manager();
162 $options_manager->set_option( $this->_id, $all_data, true );
163 }
164 }
165
166 /**
167 * Delete key-value storage.
168 *
169 * @author Vova Feldman (@svovaf)
170 * @since 1.0.9
171 */
172 function delete() {
173 $this->_data = array();
174
175 $all_data = $this->get_all_data();
176 unset( $all_data[ $this->_secondary_id ] );
177 $options_manager = $this->get_option_manager();
178 $options_manager->set_option( $this->_id, $all_data, true );
179 }
180
181 /**
182 * @author Vova Feldman (@svovaf)
183 * @since 1.0.7
184 *
185 * @param string $key
186 * @param bool $store
187 */
188 function remove( $key, $store = true ) {
189 if ( ! array_key_exists( $key, $this->_data ) ) {
190 return;
191 }
192
193 unset( $this->_data[ $key ] );
194
195 if ( $store ) {
196 $all_data = $this->get_all_data();
197 $all_data[ $this->_secondary_id ] = $this->_data;
198 $options_manager = $this->get_option_manager();
199 $options_manager->set_option( $this->_id, $all_data, true );
200 }
201 }
202
203 /**
204 * @author Vova Feldman (@svovaf)
205 * @since 1.0.7
206 *
207 * @param string $key
208 * @param mixed $default
209 *
210 * @return bool|\FS_Plugin
211 */
212 function get( $key, $default = false ) {
213 return array_key_exists( $key, $this->_data ) ?
214 $this->_data[ $key ] :
215 $default;
216 }
217
218
219 /* ArrayAccess + Magic Access (better for refactoring)
220 -----------------------------------------------------------------------------------*/
221 function __set( $k, $v ) {
222 $this->store( $k, $v );
223 }
224
225 function __isset( $k ) {
226 return array_key_exists( $k, $this->_data );
227 }
228
229 function __unset( $k ) {
230 $this->remove( $k );
231 }
232
233 function __get( $k ) {
234 return $this->get( $k, null );
235 }
236
237 function offsetSet( $k, $v ) {
238 if ( is_null( $k ) ) {
239 throw new Exception( 'Can\'t append value to request params.' );
240 } else {
241 $this->{$k} = $v;
242 }
243 }
244
245 function offsetExists( $k ) {
246 return array_key_exists( $k, $this->_data );
247 }
248
249 function offsetUnset( $k ) {
250 unset( $this->$k );
251 }
252
253 function offsetGet( $k ) {
254 return $this->get( $k, null );
255 }
256
257 /**
258 * (PHP 5 &gt;= 5.0.0)<br/>
259 * Return the current element
260 *
261 * @link http://php.net/manual/en/iterator.current.php
262 * @return mixed Can return any type.
263 */
264 public function current() {
265 return current( $this->_data );
266 }
267
268 /**
269 * (PHP 5 &gt;= 5.0.0)<br/>
270 * Move forward to next element
271 *
272 * @link http://php.net/manual/en/iterator.next.php
273 * @return void Any returned value is ignored.
274 */
275 public function next() {
276 next( $this->_data );
277 }
278
279 /**
280 * (PHP 5 &gt;= 5.0.0)<br/>
281 * Return the key of the current element
282 *
283 * @link http://php.net/manual/en/iterator.key.php
284 * @return mixed scalar on success, or null on failure.
285 */
286 public function key() {
287 return key( $this->_data );
288 }
289
290 /**
291 * (PHP 5 &gt;= 5.0.0)<br/>
292 * Checks if current position is valid
293 *
294 * @link http://php.net/manual/en/iterator.valid.php
295 * @return boolean The return value will be casted to boolean and then evaluated.
296 * Returns true on success or false on failure.
297 */
298 public function valid() {
299 $key = key( $this->_data );
300
301 return ( $key !== null && $key !== false );
302 }
303
304 /**
305 * (PHP 5 &gt;= 5.0.0)<br/>
306 * Rewind the Iterator to the first element
307 *
308 * @link http://php.net/manual/en/iterator.rewind.php
309 * @return void Any returned value is ignored.
310 */
311 public function rewind() {
312 reset( $this->_data );
313 }
314
315 /**
316 * (PHP 5 &gt;= 5.1.0)<br/>
317 * Count elements of an object
318 *
319 * @link http://php.net/manual/en/countable.count.php
320 * @return int The custom count as an integer.
321 * </p>
322 * <p>
323 * The return value is cast to an integer.
324 */
325 public function count() {
326 return count( $this->_data );
327 }
328 }