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