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