PluginProbe
Advanced Custom Fields (ACF®) / 5.9.2
Advanced Custom Fields (ACF®) v5.9.2
6.8.9 6.8.8 6.8.7 6.8.6 6.8.5 6.8.4 6.8.3 6.8.2 6.8.1 5.8.5 5.8.6 5.8.7 5.8.8 5.8.9 5.9.0 5.9.1 5.9.2 5.9.3 5.9.4 5.9.5 5.9.6 5.9.7 5.9.8 5.9.9 6.0.0 All 230 releases
advanced-custom-fields / includes / class-acf-data.php
class-acf-data.php
355 lines 6.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly.
4
5 if( ! class_exists('ACF_Data') ) :
6
7 class ACF_Data {
8
9 /** @var string Unique identifier. */
10 var $cid = '';
11
12 /** @var array Storage for data. */
13 var $data = array();
14
15 /** @var array Storage for data aliases. */
16 var $aliases = array();
17
18 /** @var bool Enables unique data per site. */
19 var $multisite = false;
20
21 /**
22 * __construct
23 *
24 * Sets up the class functionality.
25 *
26 * @date 9/1/19
27 * @since 5.7.10
28 *
29 * @param array $data Optional data to set.
30 * @return void
31 */
32 function __construct( $data = false ) {
33
34 // Set cid.
35 $this->cid = acf_uniqid();
36
37 // Set data.
38 if( $data ) {
39 $this->set( $data );
40 }
41
42 // Initialize.
43 $this->initialize();
44 }
45
46 /**
47 * initialize
48 *
49 * Called during constructor to setup class functionality.
50 *
51 * @date 9/1/19
52 * @since 5.7.10
53 *
54 * @param void
55 * @return void
56 */
57 function initialize() {
58 // Do nothing.
59 }
60
61 /**
62 * prop
63 *
64 * Sets a property for the given name and returns $this for chaining.
65 *
66 * @date 9/1/19
67 * @since 5.7.10
68 *
69 * @param (string|array) $name The data name or an array of data.
70 * @param mixed $value The data value.
71 * @return ACF_Data
72 */
73 function prop( $name = '', $value = null ) {
74
75 // Update property.
76 $this->{$name} = $value;
77
78 // Return this for chaining.
79 return $this;
80 }
81
82 /**
83 * _key
84 *
85 * Returns a key for the given name allowing aliasses to work.
86 *
87 * @date 18/1/19
88 * @since 5.7.10
89 *
90 * @param type $var Description. Default.
91 * @return type Description.
92 */
93 function _key( $name = '' ) {
94 return isset($this->aliases[ $name ]) ? $this->aliases[ $name ] : $name;
95 }
96
97 /**
98 * has
99 *
100 * Returns true if this has data for the given name.
101 *
102 * @date 9/1/19
103 * @since 5.7.10
104 *
105 * @param string $name The data name.
106 * @return boolean
107 */
108 function has( $name = '' ) {
109 $key = $this->_key($name);
110 return isset($this->data[ $key ]);
111 }
112
113 /**
114 * is
115 *
116 * Similar to has() but does not check aliases.
117 *
118 * @date 7/2/19
119 * @since 5.7.11
120 *
121 * @param type $var Description. Default.
122 * @return type Description.
123 */
124 function is( $key = '' ) {
125 return isset($this->data[ $key ]);
126 }
127
128 /**
129 * get
130 *
131 * Returns data for the given name of null if doesn't exist.
132 *
133 * @date 9/1/19
134 * @since 5.7.10
135 *
136 * @param string $name The data name.
137 * @return mixed
138 */
139 function get( $name = false ) {
140
141 // Get all.
142 if( $name === false ) {
143 return $this->data;
144
145 // Get specific.
146 } else {
147 $key = $this->_key($name);
148 return isset($this->data[ $key ]) ? $this->data[ $key ] : null;
149 }
150 }
151
152 /**
153 * get_data
154 *
155 * Returns an array of all data.
156 *
157 * @date 9/1/19
158 * @since 5.7.10
159 *
160 * @param void
161 * @return array
162 */
163 function get_data() {
164 return $this->data;
165 }
166
167 /**
168 * set
169 *
170 * Sets data for the given name and returns $this for chaining.
171 *
172 * @date 9/1/19
173 * @since 5.7.10
174 *
175 * @param (string|array) $name The data name or an array of data.
176 * @param mixed $value The data value.
177 * @return ACF_Data
178 */
179 function set( $name = '', $value = null ) {
180
181 // Set multiple.
182 if( is_array($name) ) {
183 $this->data = array_merge($this->data, $name);
184
185 // Set single.
186 } else {
187 $this->data[ $name ] = $value;
188 }
189
190 // Return this for chaining.
191 return $this;
192 }
193
194 /**
195 * append
196 *
197 * Appends data for the given name and returns $this for chaining.
198 *
199 * @date 9/1/19
200 * @since 5.7.10
201 *
202 * @param mixed $value The data value.
203 * @return ACF_Data
204 */
205 function append( $value = null ) {
206
207 // Append.
208 $this->data[] = $value;
209
210 // Return this for chaining.
211 return $this;
212 }
213
214 /**
215 * remove
216 *
217 * Removes data for the given name.
218 *
219 * @date 9/1/19
220 * @since 5.7.10
221 *
222 * @param string $name The data name.
223 * @return ACF_Data
224 */
225 function remove( $name = '' ) {
226
227 // Remove data.
228 unset( $this->data[ $name ] );
229
230 // Return this for chaining.
231 return $this;
232 }
233
234 /**
235 * reset
236 *
237 * Resets the data.
238 *
239 * @date 22/1/19
240 * @since 5.7.10
241 *
242 * @param void
243 * @return void
244 */
245 function reset() {
246 $this->data = array();
247 $this->aliases = array();
248 }
249
250 /**
251 * count
252 *
253 * Returns the data count.
254 *
255 * @date 23/1/19
256 * @since 5.7.10
257 *
258 * @param void
259 * @return int
260 */
261 function count() {
262 return count( $this->data );
263 }
264
265 /**
266 * query
267 *
268 * Returns a filtered array of data based on the set of key => value arguments.
269 *
270 * @date 23/1/19
271 * @since 5.7.10
272 *
273 * @param void
274 * @return int
275 */
276 function query( $args, $operator = 'AND' ) {
277 return wp_list_filter( $this->data, $args, $operator );
278 }
279
280 /**
281 * alias
282 *
283 * Sets an alias for the given name allowing data to be found via multiple identifiers.
284 *
285 * @date 18/1/19
286 * @since 5.7.10
287 *
288 * @param type $var Description. Default.
289 * @return type Description.
290 */
291 function alias( $name = '' /*, $alias, $alias2, etc */ ) {
292
293 // Get all aliases.
294 $args = func_get_args();
295 array_shift( $args );
296
297 // Loop over aliases and add to data.
298 foreach( $args as $alias ) {
299 $this->aliases[ $alias ] = $name;
300 }
301
302 // Return this for chaining.
303 return $this;
304 }
305
306 /**
307 * switch_site
308 *
309 * Triggered when switching between sites on a multisite installation.
310 *
311 * @date 13/2/19
312 * @since 5.7.11
313 *
314 * @param int $site_id New blog ID.
315 * @param int prev_blog_id Prev blog ID.
316 * @return void
317 */
318 function switch_site( $site_id, $prev_site_id ) {
319
320 // Bail early if not multisite compatible.
321 if( !$this->multisite ) {
322 return;
323 }
324
325 // Bail early if no change in blog ID.
326 if( $site_id === $prev_site_id ) {
327 return;
328 }
329
330 // Create storage.
331 if( !isset($this->site_data) ) {
332 $this->site_data = array();
333 $this->site_aliases = array();
334 }
335
336 // Save state.
337 $this->site_data[ $prev_site_id ] = $this->data;
338 $this->site_aliases[ $prev_site_id ] = $this->aliases;
339
340 // Reset state.
341 $this->data = array();
342 $this->aliases = array();
343
344 // Load state.
345 if( isset($this->site_data[ $site_id ]) ) {
346 $this->data = $this->site_data[ $site_id ];
347 $this->aliases = $this->site_aliases[ $site_id ];
348 unset( $this->site_data[ $site_id ] );
349 unset( $this->site_aliases[ $site_id ] );
350 }
351 }
352 }
353
354 endif; // class_exists check
355