PluginProbe ʕ •ᴥ•ʔ
Image Widget / 4.2
Image Widget v4.2
trunk 1.0 2.0 2.1 2.2 2.2.1 2.2.2 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.2 3.2.1 3.2.10 3.2.11 3.2.2 3.2.3 3.2.4 3.2.5 3.2.7 3.2.8 3.2.9 3.3 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 4.0 4.0.1 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.0.7 4.0.8 4.0.9 4.1 4.1.1 4.1.2 4.2 4.2.1 4.2.2 4.3 4.3.1 4.4 4.4.1 4.4.11 4.4.12 4.4.2 4.4.3 4.4.4 4.4.5 4.4.6 4.4.7 4.4.8 4.4.9
image-widget / freemius / includes / managers / class-fs-key-value-storage.php
image-widget / freemius / includes / managers Last commit date
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-key-value-storage.php
289 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.7
7 */
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit;
11 }
12
13 class FS_Key_Value_Storage implements ArrayAccess, Iterator, Countable {
14 /**
15 * @var string
16 */
17 protected $_id;
18 /**
19 * @var string
20 */
21 protected $_slug;
22 /**
23 * @var array
24 */
25 protected $_data;
26
27 /**
28 * @var FS_Plugin_Manager[]
29 */
30 private static $_instances = array();
31 /**
32 * @var FS_Logger
33 */
34 protected $_logger;
35
36 /**
37 * @param string $id
38 * @param string $slug
39 *
40 * @return FS_Key_Value_Storage
41 */
42 static function instance( $id, $slug ) {
43 $key = $id . ':' . $slug;
44 if ( ! isset( self::$_instances[ $key ] ) ) {
45 self::$_instances[ $key ] = new FS_Key_Value_Storage( $id, $slug );
46 }
47
48 return self::$_instances[ $key ];
49 }
50
51 protected function __construct( $id, $slug ) {
52 $this->_logger = FS_Logger::get_logger( WP_FS__SLUG . '_' . $slug . '_' . $id, WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK );
53
54 $this->_slug = $slug;
55 $this->_id = $id;
56 $this->load();
57 }
58
59 protected function get_option_manager() {
60 return FS_Option_Manager::get_manager( WP_FS__ACCOUNTS_OPTION_NAME, true );
61 }
62
63 protected function get_all_data() {
64 return $this->get_option_manager()->get_option( $this->_id, array() );
65 }
66
67 /**
68 * Load plugin data from local DB.
69 *
70 * @author Vova Feldman (@svovaf)
71 * @since 1.0.7
72 */
73 function load() {
74 $all_plugins_data = $this->get_all_data();
75 $this->_data = isset( $all_plugins_data[ $this->_slug ] ) ?
76 $all_plugins_data[ $this->_slug ] :
77 array();
78 }
79
80 /**
81 * @author Vova Feldman (@svovaf)
82 * @since 1.0.7
83 *
84 * @param string $key
85 * @param mixed $value
86 * @param bool $flush
87 */
88 function store( $key, $value, $flush = true ) {
89 if ( array_key_exists( $key, $this->_data ) && $value === $this->_data[ $key ] ) {
90 // No need to store data if the value wasn't changed.
91 return;
92 }
93
94 $all_data = $this->get_all_data();
95
96 $this->_data[ $key ] = $value;
97
98 $all_data[ $this->_slug ] = $this->_data;
99
100 $options_manager = $this->get_option_manager();
101 $options_manager->set_option( $this->_id, $all_data, $flush );
102 }
103
104 /**
105 * @author Vova Feldman (@svovaf)
106 * @since 1.0.7
107 *
108 * @param bool $store
109 * @param string[] $exceptions Set of keys to keep and not clear.
110 */
111 function clear_all( $store = true, $exceptions = array() ) {
112 $new_data = array();
113 foreach ( $exceptions as $key ) {
114 $new_data[ $key ] = $this->_data[ $key ];
115 }
116
117 $this->_data = $new_data;
118
119 if ( $store ) {
120 $all_data = $this->get_all_data();
121 $all_data[ $this->_slug ] = $this->_data;
122 $options_manager = $this->get_option_manager();
123 $options_manager->set_option( $this->_id, $all_data, true );
124 }
125 }
126
127 /**
128 * Delete key-value storage.
129 *
130 * @author Vova Feldman (@svovaf)
131 * @since 1.0.9
132 */
133 function delete() {
134 $this->_data = array();
135
136 $all_data = $this->get_all_data();
137 unset( $all_data[ $this->_slug ] );
138 $options_manager = $this->get_option_manager();
139 $options_manager->set_option( $this->_id, $all_data, true );
140 }
141
142 /**
143 * @author Vova Feldman (@svovaf)
144 * @since 1.0.7
145 *
146 * @param string $key
147 * @param bool $store
148 */
149 function remove( $key, $store = true ) {
150 if ( ! array_key_exists( $key, $this->_data ) ) {
151 return;
152 }
153
154 unset( $this->_data[ $key ] );
155
156 if ( $store ) {
157 $all_data = $this->get_all_data();
158 $all_data[ $this->_slug ] = $this->_data;
159 $options_manager = $this->get_option_manager();
160 $options_manager->set_option( $this->_id, $all_data, true );
161 }
162 }
163
164 /**
165 * @author Vova Feldman (@svovaf)
166 * @since 1.0.7
167 *
168 * @param string $key
169 * @param mixed $default
170 *
171 * @return bool|\FS_Plugin
172 */
173 function get( $key, $default = false ) {
174 return array_key_exists( $key, $this->_data ) ?
175 $this->_data[ $key ] :
176 $default;
177 }
178
179
180 /* ArrayAccess + Magic Access (better for refactoring)
181 -----------------------------------------------------------------------------------*/
182 function __set( $k, $v ) {
183 $this->store( $k, $v );
184 }
185
186 function __isset( $k ) {
187 return array_key_exists( $k, $this->_data );
188 }
189
190 function __unset( $k ) {
191 $this->remove( $k );
192 }
193
194 function __get( $k ) {
195 return $this->get( $k, null );
196 }
197
198 function offsetSet( $k, $v ) {
199 if ( is_null( $k ) ) {
200 throw new Exception( 'Can\'t append value to request params.' );
201 } else {
202 $this->{$k} = $v;
203 }
204 }
205
206 function offsetExists( $k ) {
207 return array_key_exists( $k, $this->_data );
208 }
209
210 function offsetUnset( $k ) {
211 unset( $this->$k );
212 }
213
214 function offsetGet( $k ) {
215 return $this->get( $k, null );
216 }
217
218 /**
219 * (PHP 5 &gt;= 5.0.0)<br/>
220 * Return the current element
221 *
222 * @link http://php.net/manual/en/iterator.current.php
223 * @return mixed Can return any type.
224 */
225 public function current() {
226 return current( $this->_data );
227 }
228
229 /**
230 * (PHP 5 &gt;= 5.0.0)<br/>
231 * Move forward to next element
232 *
233 * @link http://php.net/manual/en/iterator.next.php
234 * @return void Any returned value is ignored.
235 */
236 public function next() {
237 return next( $this->_data );
238 }
239
240 /**
241 * (PHP 5 &gt;= 5.0.0)<br/>
242 * Return the key of the current element
243 *
244 * @link http://php.net/manual/en/iterator.key.php
245 * @return mixed scalar on success, or null on failure.
246 */
247 public function key() {
248 return key( $this->_data );
249 }
250
251 /**
252 * (PHP 5 &gt;= 5.0.0)<br/>
253 * Checks if current position is valid
254 *
255 * @link http://php.net/manual/en/iterator.valid.php
256 * @return boolean The return value will be casted to boolean and then evaluated.
257 * Returns true on success or false on failure.
258 */
259 public function valid() {
260 $key = key( $this->_data );
261
262 return ( $key !== null && $key !== false );
263 }
264
265 /**
266 * (PHP 5 &gt;= 5.0.0)<br/>
267 * Rewind the Iterator to the first element
268 *
269 * @link http://php.net/manual/en/iterator.rewind.php
270 * @return void Any returned value is ignored.
271 */
272 public function rewind() {
273 reset( $this->_data );
274 }
275
276 /**
277 * (PHP 5 &gt;= 5.1.0)<br/>
278 * Count elements of an object
279 *
280 * @link http://php.net/manual/en/countable.count.php
281 * @return int The custom count as an integer.
282 * </p>
283 * <p>
284 * The return value is cast to an integer.
285 */
286 public function count() {
287 return count( $this->_data );
288 }
289 }