PluginProbe
Advanced Custom Fields (ACF®) / 5.8.7
Advanced Custom Fields (ACF®) v5.8.7
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 / local-fields.php

local-fields.php in Advanced Custom Fields (ACF®) 5.8.7, at includes/local-fields.php

579 lines 11.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // Register notices stores.
4 acf_register_store( 'local-fields' );
5 acf_register_store( 'local-groups' );
6 acf_register_store( 'local-empty' );
7
8 // Register filter.
9 acf_enable_filter( 'local' );
10
11 /**
12 * acf_enable_local
13 *
14 * Enables the local filter.
15 *
16 * @date 22/1/19
17 * @since 5.7.10
18 *
19 * @param void
20 * @return void
21 */
22 function acf_enable_local() {
23 acf_enable_filter('local');
24 }
25
26 /**
27 * acf_disable_local
28 *
29 * Disables the local filter.
30 *
31 * @date 22/1/19
32 * @since 5.7.10
33 *
34 * @param void
35 * @return void
36 */
37 function acf_disable_local() {
38 acf_disable_filter('local');
39 }
40
41 /**
42 * acf_is_local_enabled
43 *
44 * Returns true if local fields are enabled.
45 *
46 * @date 23/1/19
47 * @since 5.7.10
48 *
49 * @param void
50 * @return bool
51 */
52 function acf_is_local_enabled() {
53 return ( acf_is_filter_enabled('local') && acf_get_setting('local') );
54 }
55
56 /**
57 * acf_get_local_store
58 *
59 * Returns either local store or a dummy store for the given name.
60 *
61 * @date 23/1/19
62 * @since 5.7.10
63 *
64 * @param string $name The store name (fields|groups).
65 * @return ACF_Data
66 */
67 function acf_get_local_store( $name = '' ) {
68
69 // Check if enabled.
70 if( acf_is_local_enabled() ) {
71 return acf_get_store( "local-$name" );
72
73 // Return dummy store if not enabled.
74 } else {
75 return acf_get_store( "local-empty" );
76 }
77 }
78
79 /**
80 * acf_reset_local
81 *
82 * Resets the local data.
83 *
84 * @date 22/1/19
85 * @since 5.7.10
86 *
87 * @param void
88 * @return void
89 */
90 function acf_reset_local() {
91 acf_get_local_store( 'fields' )->reset();
92 acf_get_local_store( 'groups' )->reset();
93 }
94
95 /**
96 * acf_get_local_field_groups
97 *
98 * Returns all local field groups.
99 *
100 * @date 22/1/19
101 * @since 5.7.10
102 *
103 * @param void
104 * @return array
105 */
106 function acf_get_local_field_groups() {
107 return acf_get_local_store( 'groups' )->get();
108 }
109
110 /**
111 * acf_have_local_field_groups
112 *
113 * description
114 *
115 * @date 22/1/19
116 * @since 5.7.10
117 *
118 * @param type $var Description. Default.
119 * @return type Description.
120 */
121 function acf_have_local_field_groups() {
122 return acf_get_local_store( 'groups' )->count() ? true : false;
123 }
124
125 /**
126 * acf_count_local_field_groups
127 *
128 * description
129 *
130 * @date 22/1/19
131 * @since 5.7.10
132 *
133 * @param type $var Description. Default.
134 * @return type Description.
135 */
136 function acf_count_local_field_groups() {
137 return acf_get_local_store( 'groups' )->count();
138 }
139
140 /**
141 * acf_add_local_field_group
142 *
143 * Adds a local field group.
144 *
145 * @date 22/1/19
146 * @since 5.7.10
147 *
148 * @param array $field_group The field group array.
149 * @return void
150 */
151 function acf_add_local_field_group( $field_group ) {
152
153 // Validate field group.
154 $field_group = acf_get_valid_field_group( $field_group );
155
156 // Bail early if field group already exists.
157 if( acf_is_local_field_group($field_group['key']) ) {
158 return;
159 }
160
161 // Extract fields from group.
162 $fields = acf_extract_var( $field_group, 'fields' );
163
164 // Add local reference (may be set to "json").
165 if( empty($field_group['local']) ) {
166 $field_group['local'] = 'php';
167 }
168
169 // Add to store
170 acf_get_local_store( 'groups' )->set( $field_group['key'], $field_group );
171
172 // Add fields
173 if( $fields ) {
174
175 // Add parent reference
176 foreach( $fields as $i => $field ) {
177 $fields[ $i ]['parent'] = $field_group['key'];
178 }
179
180 // Add fields.
181 acf_add_local_fields( $fields );
182 }
183 }
184
185 /**
186 * register_field_group
187 *
188 * See acf_add_local_field_group().
189 *
190 * @date 22/1/19
191 * @since 5.7.10
192 *
193 * @param array $field_group The field group array.
194 * @return void
195 */
196 function register_field_group( $field_group ) {
197 acf_add_local_field_group( $field_group );
198 }
199
200 /**
201 * acf_remove_local_field_group
202 *
203 * Removes a field group for the given key.
204 *
205 * @date 22/1/19
206 * @since 5.7.10
207 *
208 * @param string $key The field group key.
209 * @return bool
210 */
211 function acf_remove_local_field_group( $key = '' ) {
212 return acf_get_local_store( 'groups' )->remove( $key );
213 }
214
215 /**
216 * acf_is_local_field_group
217 *
218 * Returns true if a field group exists for the given key.
219 *
220 * @date 22/1/19
221 * @since 5.7.10
222 *
223 * @param string $key The field group key.
224 * @return bool
225 */
226 function acf_is_local_field_group( $key = '' ) {
227 return acf_get_local_store( 'groups' )->has( $key );
228 }
229
230 /**
231 * acf_is_local_field_group_key
232 *
233 * Returns true if a field group exists for the given key.
234 *
235 * @date 22/1/19
236 * @since 5.7.10
237 *
238 * @param string $key The field group group key.
239 * @return bool
240 */
241 function acf_is_local_field_group_key( $key = '' ) {
242 return acf_get_local_store( 'groups' )->is( $key );
243 }
244
245 /**
246 * acf_get_local_field_group
247 *
248 * Returns a field group for the given key.
249 *
250 * @date 22/1/19
251 * @since 5.7.10
252 *
253 * @param string $key The field group key.
254 * @return (array|null)
255 */
256 function acf_get_local_field_group( $key = '' ) {
257 return acf_get_local_store( 'groups' )->get( $key );
258 }
259
260 /**
261 * acf_add_local_fields
262 *
263 * Adds an array of local fields.
264 *
265 * @date 22/1/19
266 * @since 5.7.10
267 *
268 * @param array $fields An array of un prepared fields.
269 * @return array
270 */
271 function acf_add_local_fields( $fields = array() ) {
272
273 // Prepare for import (allows parent fields to offer up children).
274 $fields = acf_prepare_fields_for_import( $fields );
275
276 // Add each field.
277 foreach( $fields as $field ) {
278 acf_add_local_field( $field, true );
279 }
280 }
281
282 /**
283 * acf_get_local_fields
284 *
285 * Returns all local fields for the given parent.
286 *
287 * @date 22/1/19
288 * @since 5.7.10
289 *
290 * @param string $parent The parent key.
291 * @return array
292 */
293 function acf_get_local_fields( $parent = '' ) {
294
295 // Return children
296 if( $parent ) {
297 return acf_get_local_store( 'fields' )->query(array(
298 'parent' => $parent
299 ));
300
301 // Return all.
302 } else {
303 return acf_get_local_store( 'fields' )->get();
304 }
305 }
306
307 /**
308 * acf_have_local_fields
309 *
310 * Returns true if local fields exist.
311 *
312 * @date 22/1/19
313 * @since 5.7.10
314 *
315 * @param string $parent The parent key.
316 * @return bool
317 */
318 function acf_have_local_fields( $parent = '' ) {
319 return acf_get_local_fields($parent) ? true : false;
320 }
321
322 /**
323 * acf_count_local_fields
324 *
325 * Returns the number of local fields for the given parent.
326 *
327 * @date 22/1/19
328 * @since 5.7.10
329 *
330 * @param string $parent The parent key.
331 * @return int
332 */
333 function acf_count_local_fields( $parent = '' ) {
334 return count( acf_get_local_fields($parent) );
335 }
336
337 /**
338 * acf_add_local_field
339 *
340 * Adds a local field.
341 *
342 * @date 22/1/19
343 * @since 5.7.10
344 *
345 * @param array $field The field array.
346 * @param bool $prepared Whether or not the field has already been prepared for import.
347 * @return void
348 */
349 function acf_add_local_field( $field, $prepared = false ) {
350
351 // Apply default args needed for import.
352 $field = wp_parse_args($field, array(
353 'key' => '',
354 'name' => '',
355 'type' => '',
356 'parent' => ''
357 ));
358
359 // Ensure field has a key.
360 if( !$field['key'] ) {
361 $field['key'] = "field_{$field['name']}";
362 }
363
364 // If called directly, allow sub fields to be correctly prepared.
365 if( !$prepared ) {
366 return acf_add_local_fields( array( $field ) );
367 }
368
369 // Set menu order.
370 if( !isset($field['menu_order']) ) {
371 $field['menu_order'] = acf_count_local_fields( $field['parent'] );
372 }
373
374 // Extract attributes.
375 $key = $field['key'];
376 $name = $field['name'];
377
378 // Allow sub field to be added multipel times to different parents.
379 $store = acf_get_local_store( 'fields' );
380 if( $store->is($key) ) {
381 $old_key = _acf_generate_local_key( $store->get($key) );
382 $new_key = _acf_generate_local_key( $field );
383 if( $old_key !== $new_key ) {
384 $key = $new_key;
385 }
386 }
387
388 // Add field.
389 $store->set( $key, $field )->alias( $key, $name );
390 }
391
392 /**
393 * _acf_generate_local_key
394 *
395 * Generates a unique key based on the field's parent.
396 *
397 * @date 22/1/19
398 * @since 5.7.10
399 *
400 * @param string $key The field key.
401 * @return bool
402 */
403 function _acf_generate_local_key( $field ) {
404 return "{$field['key']}:{$field['parent']}";
405 }
406
407 /**
408 * acf_remove_local_field
409 *
410 * Removes a field for the given key.
411 *
412 * @date 22/1/19
413 * @since 5.7.10
414 *
415 * @param string $key The field key.
416 * @return bool
417 */
418 function acf_remove_local_field( $key = '' ) {
419 return acf_get_local_store( 'fields' )->remove( $key );
420 }
421
422 /**
423 * acf_is_local_field
424 *
425 * Returns true if a field exists for the given key or name.
426 *
427 * @date 22/1/19
428 * @since 5.7.10
429 *
430 * @param string $key The field group key.
431 * @return bool
432 */
433 function acf_is_local_field( $key = '' ) {
434 return acf_get_local_store( 'fields' )->has( $key );
435 }
436
437 /**
438 * acf_is_local_field_key
439 *
440 * Returns true if a field exists for the given key.
441 *
442 * @date 22/1/19
443 * @since 5.7.10
444 *
445 * @param string $key The field group key.
446 * @return bool
447 */
448 function acf_is_local_field_key( $key = '' ) {
449 return acf_get_local_store( 'fields' )->is( $key );
450 }
451
452 /**
453 * acf_get_local_field
454 *
455 * Returns a field for the given key.
456 *
457 * @date 22/1/19
458 * @since 5.7.10
459 *
460 * @param string $key The field group key.
461 * @return (array|null)
462 */
463 function acf_get_local_field( $key = '' ) {
464 return acf_get_local_store( 'fields' )->get( $key );
465 }
466
467 /**
468 * _acf_apply_get_local_field_groups
469 *
470 * Appends local field groups to the provided array.
471 *
472 * @date 23/1/19
473 * @since 5.7.10
474 *
475 * @param array $field_groups An array of field groups.
476 * @return array
477 */
478 function _acf_apply_get_local_field_groups( $groups = array() ) {
479
480 // Get local groups
481 $local = acf_get_local_field_groups();
482 if( $local ) {
483
484 // Generate map of "index" => "key" data.
485 $map = wp_list_pluck($groups, 'key');
486
487 // Loop over groups and update/append local.
488 foreach( $local as $group ) {
489
490 // Get group allowing cache and filters to run.
491 //$group = acf_get_field_group( $group['key'] );
492
493 // Update.
494 $i = array_search($group['key'], $map);
495 if( $i !== false ) {
496 unset($group['ID']);
497 $groups[ $i ] = array_merge($groups[ $i ], $group);
498
499 // Append
500 } else {
501 $groups[] = acf_get_field_group( $group['key'] );
502 }
503 }
504
505 // Sort list via menu_order and title.
506 $groups = wp_list_sort( $groups, array('menu_order' => 'ASC', 'title' => 'ASC') );
507 }
508
509 // Return groups.
510 return $groups;
511 }
512
513 // Hook into filter.
514 add_filter( 'acf/get_field_groups', '_acf_apply_get_local_field_groups', 20, 1 );
515
516 /**
517 * _acf_apply_is_local_field_key
518 *
519 * Returns true if is a local key.
520 *
521 * @date 23/1/19
522 * @since 5.7.10
523 *
524 * @param bool $bool The result.
525 * @param string $id The identifier.
526 * @return bool
527 */
528 function _acf_apply_is_local_field_key( $bool, $id ) {
529 return acf_is_local_field_key( $id );
530 }
531
532 // Hook into filter.
533 add_filter( 'acf/is_field_key', '_acf_apply_is_local_field_key', 20, 2 );
534
535 /**
536 * _acf_apply_is_local_field_group_key
537 *
538 * Returns true if is a local key.
539 *
540 * @date 23/1/19
541 * @since 5.7.10
542 *
543 * @param bool $bool The result.
544 * @param string $id The identifier.
545 * @return bool
546 */
547 function _acf_apply_is_local_field_group_key( $bool, $id ) {
548 return acf_is_local_field_group_key( $id );
549 }
550
551 // Hook into filter.
552 add_filter( 'acf/is_field_group_key', '_acf_apply_is_local_field_group_key', 20, 2 );
553
554 /**
555 * _acf_do_prepare_local_fields
556 *
557 * Local fields that are added too early will not be correctly prepared by the field type class.
558 *
559 * @date 23/1/19
560 * @since 5.7.10
561 *
562 * @param void
563 * @return void
564 */
565 function _acf_do_prepare_local_fields() {
566
567 // Get fields.
568 $fields = acf_get_local_fields();
569
570 // If fields have been registered early, re-add to correctly prepare them.
571 if( $fields ) {
572 acf_add_local_fields( $fields );
573 }
574 }
575
576 // Hook into action.
577 add_action( 'acf/include_fields', '_acf_do_prepare_local_fields', 0, 1 );
578
579 ?>