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