PluginProbe ʕ •ᴥ•ʔ
Secure Custom Fields / trunk
Secure Custom Fields vtrunk
6.9.1 6.9.0 6.8.9 6.8.7 6.8.8 6.8.6 6.8.4 6.8.5 trunk 6.4.0-beta1 6.4.0-beta2 6.4.1 6.4.1-beta3 6.4.1-beta4 6.4.1-beta5 6.4.1-beta6 6.4.1-beta7 6.4.2 6.5.0 6.5.1 6.5.2 6.5.3 6.5.4 6.5.5 6.5.6 6.5.7 6.6.0 6.7.0 6.7.1 6.8.0 6.8.1 6.8.2 6.8.3
secure-custom-fields / includes / class-acf-data.php
secure-custom-fields / includes Last commit date
Blocks 1 week ago Datastore 1 month ago Meta 1 year ago abilities 1 week ago admin 1 week ago ajax 1 month ago api 8 hours ago fields 8 hours ago forms 8 hours ago legacy 1 year ago locations 1 year ago post-types 2 months ago rest-api 1 week ago walkers 1 year ago acf-bidirectional-functions.php 1 year ago acf-field-functions.php 2 months ago acf-field-group-functions.php 7 months ago acf-form-functions.php 1 year ago acf-helper-functions.php 1 year ago acf-hook-functions.php 1 year ago acf-input-functions.php 7 months ago acf-internal-post-type-functions.php 7 months ago acf-meta-functions.php 2 weeks ago acf-post-functions.php 1 year ago acf-post-type-functions.php 1 year ago acf-taxonomy-functions.php 1 year ago acf-user-functions.php 1 week ago acf-utility-functions.php 1 year ago acf-value-functions.php 1 year ago acf-wp-functions.php 8 hours ago assets.php 1 week ago blocks-auto-inline-editing.php 2 months ago blocks.php 3 weeks ago class-acf-data.php 10 months ago class-acf-internal-post-type.php 1 week ago class-acf-options-page.php 1 year ago class-acf-site-health.php 3 months ago class-scf-json-schema-validator.php 6 months ago class-scf-schema-builder.php 2 months ago compatibility.php 1 year ago datastore.php 1 month ago deprecated.php 1 year ago fields.php 10 months ago index.php 1 year ago l10n.php 1 year ago local-fields.php 1 year ago local-json.php 1 month ago local-meta.php 1 year ago locations.php 1 year ago loop.php 10 months ago media.php 1 year ago rest-api.php 10 months ago revisions.php 1 month ago scf-ui-options-page-functions.php 1 year ago third-party.php 7 months ago upgrades.php 2 weeks ago validation.php 10 months ago wpml.php 1 year ago
class-acf-data.php
393 lines
1 <?php
2 /**
3 * ACF Data Class
4 *
5 * Handles data storage and retrieval with support for aliases and multisite.
6 *
7 * @package wordpress/secure-custom-fields
8 */
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit; // Exit if accessed directly.
12 }
13
14 if ( ! class_exists( 'ACF_Data' ) ) :
15 /**
16 * ACF Data Class
17 *
18 * Provides a flexible data storage system with support for aliases and multisite.
19 *
20 * @since ACF 5.7.10
21 */
22 class ACF_Data {
23
24 /**
25 * Unique identifier.
26 *
27 * @var string Unique identifier for this instance.
28 */
29 public $cid = '';
30
31 /**
32 * Data storage.
33 *
34 * @var array Storage for data values.
35 */
36 public $data = array();
37
38 /**
39 * Data aliases.
40 *
41 * @var array Storage for data key aliases.
42 */
43 public $aliases = array();
44
45 /**
46 * Site-specific data storage.
47 *
48 * @var array Storage for data values per site.
49 */
50 public $site_data = array();
51
52 /**
53 * Site-specific aliases storage.
54 *
55 * @var array Storage for data key aliases per site.
56 */
57 public $site_aliases = array();
58
59 /**
60 * Multisite support.
61 *
62 * @var boolean Whether to enable unique data per site.
63 */
64 public $multisite = false;
65
66 /**
67 * __construct
68 *
69 * Sets up the class functionality.
70 *
71 * @date 9/1/19
72 * @since ACF 5.7.10
73 *
74 * @param array $data Optional data to set.
75 * @return void
76 */
77 public function __construct( $data = false ) {
78
79 // Set cid.
80 $this->cid = acf_uniqid();
81
82 // Set data.
83 if ( $data ) {
84 $this->set( $data );
85 }
86
87 // Initialize.
88 $this->initialize();
89 }
90
91 /**
92 * Initialize
93 *
94 * Called during constructor to setup class functionality.
95 *
96 * @date 9/1/19
97 * @since ACF 5.7.10
98 *
99 * @return void
100 */
101 public function initialize() {
102 // Do nothing.
103 }
104
105 /**
106 * Prop
107 *
108 * Sets a property for the given name and returns $this for chaining.
109 *
110 * @date 9/1/19
111 * @since ACF 5.7.10
112 *
113 * @param (string|array) $name The data name or an array of data.
114 * @param mixed $value The data value.
115 * @return ACF_Data
116 */
117 public function prop( $name = '', $value = null ) {
118
119 // Update property.
120 $this->{$name} = $value;
121
122 // Return this for chaining.
123 return $this;
124 }
125
126 /**
127 * Key
128 *
129 * Returns a key for the given name allowing aliases to work.
130 *
131 * @date 18/1/19
132 * @since ACF 5.7.10
133 *
134 * @param string $name The name to get key for.
135 * @return string The key for the given name.
136 */
137 public function _key( $name = '' ) { // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore
138 return isset( $this->aliases[ $name ] ) ? $this->aliases[ $name ] : $name;
139 }
140
141 /**
142 * Has
143 *
144 * Returns true if this has data for the given name.
145 *
146 * @date 9/1/19
147 * @since ACF 5.7.10
148 *
149 * @param string $name The data name.
150 * @return boolean
151 */
152 public function has( $name = '' ) {
153 $key = $this->_key( $name );
154 return isset( $this->data[ $key ] );
155 }
156
157 /**
158 * Is
159 *
160 * Similar to has() but does not check aliases.
161 *
162 * @date 7/2/19
163 * @since ACF 5.7.11
164 *
165 * @param string $key The key to check.
166 * @return boolean True if key exists.
167 */
168 public function is( $key = '' ) {
169 return isset( $this->data[ $key ] );
170 }
171
172 /**
173 * Get
174 *
175 * Returns data for the given name of null if doesn't exist.
176 *
177 * @date 9/1/19
178 * @since ACF 5.7.10
179 *
180 * @param string $name The data name.
181 * @return mixed
182 */
183 public function get( $name = false ) {
184
185 // Get all.
186 if ( false === $name ) {
187 return $this->data;
188
189 // Get specific.
190 } else {
191 $key = $this->_key( $name );
192 return isset( $this->data[ $key ] ) ? $this->data[ $key ] : null;
193 }
194 }
195
196 /**
197 * Get Data
198 *
199 * Returns an array of all data.
200 *
201 * @date 9/1/19
202 * @since ACF 5.7.10
203 *
204 * @return array
205 */
206 public function get_data() {
207 return $this->data;
208 }
209
210 /**
211 * Set
212 *
213 * Sets data for the given name and returns $this for chaining.
214 *
215 * @date 9/1/19
216 * @since ACF 5.7.10
217 *
218 * @param (string|array) $name The data name or an array of data.
219 * @param mixed $value The data value.
220 * @return ACF_Data
221 */
222 public function set( $name = '', $value = null ) {
223
224 // Set multiple.
225 if ( is_array( $name ) ) {
226 $this->data = array_merge( $this->data, $name );
227
228 // Set single.
229 } else {
230 $this->data[ $name ] = $value;
231 }
232
233 // Return this for chaining.
234 return $this;
235 }
236
237 /**
238 * Append
239 *
240 * Appends data for the given name and returns $this for chaining.
241 *
242 * @date 9/1/19
243 * @since ACF 5.7.10
244 *
245 * @param mixed $value The data value.
246 * @return ACF_Data
247 */
248 public function append( $value = null ) {
249
250 // Append.
251 $this->data[] = $value;
252
253 // Return this for chaining.
254 return $this;
255 }
256
257 /**
258 * Remove
259 *
260 * Removes data for the given name.
261 *
262 * @date 9/1/19
263 * @since ACF 5.7.10
264 *
265 * @param string $name The data name.
266 * @return ACF_Data
267 */
268 public function remove( $name = '' ) {
269
270 // Remove data.
271 unset( $this->data[ $name ] );
272
273 // Return this for chaining.
274 return $this;
275 }
276
277 /**
278 * Reset
279 *
280 * Resets the data.
281 *
282 * @date 22/1/19
283 * @since ACF 5.7.10
284 *
285 * @return void
286 */
287 public function reset() {
288 $this->data = array();
289 $this->aliases = array();
290 }
291
292 /**
293 * Count
294 *
295 * Returns the data count.
296 *
297 * @date 23/1/19
298 * @since ACF 5.7.10
299 *
300 * @return integer
301 */
302 public function count() {
303 return count( $this->data );
304 }
305
306 /**
307 * Query
308 *
309 * Returns a filtered array of data based on the set of key => value arguments.
310 *
311 * @date 23/1/19
312 * @since ACF 5.7.10
313 *
314 * @param array $args The query arguments.
315 * @param string $operator The logical operator. Accepts 'AND' or 'OR'.
316 * @return array
317 */
318 public function query( $args, $operator = 'AND' ) {
319 return wp_list_filter( $this->data, $args, $operator );
320 }
321
322 /**
323 * Alias
324 *
325 * Sets an alias for the given name allowing data to be found via multiple identifiers.
326 *
327 * @date 18/1/19
328 * @since ACF 5.7.10
329 *
330 * @param string $name The name to create aliases for.
331 * @param string ...$args Additional aliases to map to the name.
332 * @return ACF_Data
333 */
334 public function alias( $name = '', ...$args ) {
335 // Loop over aliases and add to data.
336 foreach ( $args as $alias ) {
337 $this->aliases[ $alias ] = $name;
338 }
339
340 // Return this for chaining.
341 return $this;
342 }
343
344 /**
345 * Switch Site
346 *
347 * Triggered when switching between sites on a multisite installation.
348 *
349 * @date 13/2/19
350 * @since ACF 5.7.11
351 *
352 * @param integer $site_id New blog ID.
353 * @param integer $prev_site_id Previous blog ID.
354 * @return void
355 */
356 public function switch_site( $site_id, $prev_site_id ) {
357
358 // Bail early if not multisite compatible.
359 if ( ! $this->multisite ) {
360 return;
361 }
362
363 // Bail early if no change in blog ID.
364 if ( $site_id === $prev_site_id ) {
365 return;
366 }
367
368 // Create storage.
369 if ( ! isset( $this->site_data ) ) {
370 $this->site_data = array();
371 $this->site_aliases = array();
372 }
373
374 // Save state.
375 $this->site_data[ $prev_site_id ] = $this->data;
376 $this->site_aliases[ $prev_site_id ] = $this->aliases;
377
378 // Reset state.
379 $this->data = array();
380 $this->aliases = array();
381
382 // Load state.
383 if ( isset( $this->site_data[ $site_id ] ) ) {
384 $this->data = $this->site_data[ $site_id ];
385 $this->aliases = $this->site_aliases[ $site_id ];
386 unset( $this->site_data[ $site_id ] );
387 unset( $this->site_aliases[ $site_id ] );
388 }
389 }
390 }
391
392 endif; // class_exists check
393