PluginProbe
Advanced Custom Fields (ACF®) / 6.1.0
Advanced Custom Fields (ACF®) v6.1.0
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
733 lines 15.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // Register local stores.
4 acf_register_store( 'local-fields' );
5 acf_register_store( 'local-groups' );
6 acf_register_store( 'local-empty' );
7 acf_register_store( 'local-post-types' );
8 acf_register_store( 'local-taxonomies' );
9
10 // Register filter.
11 acf_enable_filter( 'local' );
12
13 /**
14 * acf_enable_local
15 *
16 * Enables the local filter.
17 *
18 * @date 22/1/19
19 * @since 5.7.10
20 *
21 * @param void
22 * @return void
23 */
24 function acf_enable_local() {
25 acf_enable_filter( 'local' );
26 }
27
28 /**
29 * acf_disable_local
30 *
31 * Disables the local filter.
32 *
33 * @date 22/1/19
34 * @since 5.7.10
35 *
36 * @param void
37 * @return void
38 */
39 function acf_disable_local() {
40 acf_disable_filter( 'local' );
41 }
42
43 /**
44 * acf_is_local_enabled
45 *
46 * Returns true if local fields are enabled.
47 *
48 * @date 23/1/19
49 * @since 5.7.10
50 *
51 * @param void
52 * @return bool
53 */
54 function acf_is_local_enabled() {
55 return ( acf_is_filter_enabled( 'local' ) && acf_get_setting( 'local' ) );
56 }
57
58 /**
59 * Returns either local store or a dummy store for the given name or post type.
60 *
61 * @date 23/1/19
62 * @since 5.7.10
63 *
64 * @param string $name The store name.
65 * @param string $post_type The post type for the desired store.
66 * @return ACF_Data
67 */
68 function acf_get_local_store( $name = '', $post_type = '' ) {
69 if ( '' !== $post_type ) {
70 switch ( $post_type ) {
71 case 'acf-post-type':
72 $name = 'post-types';
73 break;
74 case 'acf-taxonomy':
75 $name = 'taxonomies';
76 break;
77 case 'acf-field-group':
78 $name = 'groups';
79 break;
80 case 'acf-field':
81 $name = 'fields';
82 break;
83 }
84 }
85
86 if ( acf_is_local_enabled() && '' !== $name ) {
87 return acf_get_store( "local-$name" );
88 } else {
89 // Return dummy store if not enabled or no name provided.
90 return acf_get_store( 'local-empty' );
91 }
92 }
93
94 /**
95 * acf_reset_local
96 *
97 * Resets the local data.
98 *
99 * @date 22/1/19
100 * @since 5.7.10
101 *
102 * @param void
103 * @return void
104 */
105 function acf_reset_local() {
106 acf_get_local_store( 'fields' )->reset();
107 acf_get_local_store( 'groups' )->reset();
108 acf_get_local_store( 'post-types' )->reset();
109 acf_get_local_store( 'taxonomies' )->reset();
110 }
111
112 /**
113 * acf_get_local_field_groups
114 *
115 * Returns all local field groups.
116 *
117 * @date 22/1/19
118 * @since 5.7.10
119 *
120 * @param void
121 * @return array
122 */
123 function acf_get_local_field_groups() {
124 return acf_get_local_store( 'groups' )->get();
125 }
126
127 /**
128 * Returns local ACF posts with the provided post type.
129 *
130 * @since 6.1
131 *
132 * @param string $post_type The post type to check for.
133 * @return array|mixed
134 */
135 function acf_get_local_internal_posts( $post_type = 'acf-field-group' ) {
136 return acf_get_local_store( '', $post_type )->get();
137 }
138
139 /**
140 * acf_have_local_field_groups
141 *
142 * description
143 *
144 * @date 22/1/19
145 * @since 5.7.10
146 *
147 * @param type $var Description. Default.
148 * @return type Description.
149 */
150 function acf_have_local_field_groups() {
151 return acf_get_local_store( 'groups' )->count() ? true : false;
152 }
153
154 /**
155 * acf_count_local_field_groups
156 *
157 * description
158 *
159 * @date 22/1/19
160 * @since 5.7.10
161 *
162 * @param type $var Description. Default.
163 * @return type Description.
164 */
165 function acf_count_local_field_groups() {
166 return acf_get_local_store( 'groups' )->count();
167 }
168
169 /**
170 * acf_add_local_field_group
171 *
172 * Adds a local field group.
173 *
174 * @date 22/1/19
175 * @since 5.7.10
176 *
177 * @param array $field_group The field group array.
178 * @return bool
179 */
180 function acf_add_local_field_group( $field_group ) {
181
182 // Apply default properties needed for import.
183 $field_group = wp_parse_args(
184 $field_group,
185 array(
186 'key' => '',
187 'title' => '',
188 'fields' => array(),
189 'local' => 'php',
190 )
191 );
192
193 // Generate key if only name is provided.
194 if ( ! $field_group['key'] ) {
195 $field_group_key = 'group_' . acf_slugify( $field_group['title'], '_' );
196 if ( $field_group_key === 'group_' ) {
197 $field_group_key = 'group_' . md5( $field_group['title'] );
198 }
199 $field_group['key'] = $field_group_key;
200 }
201
202 // Bail early if field group already exists.
203 if ( acf_is_local_field_group( $field_group['key'] ) ) {
204 return false;
205 }
206
207 // Prepare field group for import (adds menu_order and parent properties to fields).
208 $field_group = acf_prepare_field_group_for_import( $field_group );
209
210 // Extract fields from group.
211 $fields = acf_extract_var( $field_group, 'fields' );
212
213 // Add to store
214 acf_get_local_store( 'groups' )->set( $field_group['key'], $field_group );
215
216 // Add fields
217 if ( $fields ) {
218 acf_add_local_fields( $fields );
219 }
220
221 // Return true on success.
222 return true;
223 }
224
225 /**
226 * Adds a local ACF internal post type.
227 *
228 * @since 6.1
229 *
230 * @param array $post The main ACF post array.
231 * @param string $post_type The post type being added.
232 * @return bool
233 */
234 function acf_add_local_internal_post_type( $post, $post_type ) {
235 // Apply default properties needed for import.
236 $post = wp_parse_args(
237 $post,
238 array(
239 'key' => '',
240 'title' => '',
241 'local' => 'json',
242 )
243 );
244
245 // Bail early if field group already exists.
246 if ( acf_is_local_internal_post_type( $post['key'], $post_type ) ) {
247 return false;
248 }
249
250 // Prepare field group for import (adds menu_order and parent properties to fields).
251 $post = acf_prepare_internal_post_type_for_import( $post, $post_type );
252
253 // Add to store.
254 acf_get_local_store( '', $post_type )->set( $post['key'], $post );
255
256 return true;
257 }
258
259 /**
260 * register_field_group
261 *
262 * See acf_add_local_field_group().
263 *
264 * @date 22/1/19
265 * @since 5.7.10
266 *
267 * @param array $field_group The field group array.
268 * @return void
269 */
270 function register_field_group( $field_group ) {
271 acf_add_local_field_group( $field_group );
272 }
273
274 /**
275 * acf_remove_local_field_group
276 *
277 * Removes a field group for the given key.
278 *
279 * @date 22/1/19
280 * @since 5.7.10
281 *
282 * @param string $key The field group key.
283 * @return bool
284 */
285 function acf_remove_local_field_group( $key = '' ) {
286 return acf_remove_local_internal_post_type( $key, 'acf-field-group' );
287 }
288
289 /**
290 * Removes a local ACF post with the given key and post type.
291 *
292 * @since 6.1
293 *
294 * @param string $key The ACF key.
295 * @param string $post_type The ACF post type.
296 * @return bool
297 */
298 function acf_remove_local_internal_post_type( $key = '', $post_type = 'acf-field-group' ) {
299 return acf_get_local_store( '', $post_type )->remove( $key );
300 }
301
302 /**
303 * acf_is_local_field_group
304 *
305 * Returns true if a field group exists for the given key.
306 *
307 * @date 22/1/19
308 * @since 5.7.10
309 *
310 * @param string $key The field group key.
311 * @return bool
312 */
313 function acf_is_local_field_group( $key = '' ) {
314 return acf_get_local_store( 'groups' )->has( $key );
315 }
316
317
318 /**
319 * Returns true if an ACF post exists for the given key.
320 *
321 * @since 6.1
322 *
323 * @param string $key The ACF key.
324 * @param string $post_type The ACF post type.
325 * @return bool
326 */
327 function acf_is_local_internal_post_type( $key = '', $post_type = 'acf-field-group' ) {
328 return acf_get_local_store( '', $post_type )->has( $key );
329 }
330
331 /**
332 * acf_is_local_field_group_key
333 *
334 * Returns true if a field group exists for the given key.
335 *
336 * @date 22/1/19
337 * @since 5.7.10
338 *
339 * @param string $key The field group key.
340 * @return bool
341 */
342 function acf_is_local_field_group_key( $key = '' ) {
343 return acf_is_local_internal_post_type_key( $key, 'acf-field-group' );
344 }
345
346 /**
347 * Returns true if a local ACF post exists for the given key.
348 *
349 * @since 6.1
350 *
351 * @param string $key The ACF post key.
352 * @param string $post_type The post type to check.
353 * @return bool
354 */
355 function acf_is_local_internal_post_type_key( $key = '', $post_type = '' ) {
356 return acf_get_local_store( '', $post_type )->is( $key );
357 }
358
359 /**
360 * acf_get_local_field_group
361 *
362 * Returns a field group for the given key.
363 *
364 * @date 22/1/19
365 * @since 5.7.10
366 *
367 * @param string $key The field group key.
368 * @return (array|null)
369 */
370 function acf_get_local_field_group( $key = '' ) {
371 return acf_get_local_store( 'groups' )->get( $key );
372 }
373
374 /**
375 * Returns an ACF post for the given key.
376 *
377 * @since 6.1
378 *
379 * @param string $key The field group key.
380 * @param string $post_type The ACF post type.
381 * @return array|null
382 */
383 function acf_get_local_internal_post_type( $key = '', $post_type = 'acf-field-group' ) {
384 return acf_get_local_store( '', $post_type )->get( $key );
385 }
386
387 /**
388 * acf_add_local_fields
389 *
390 * Adds an array of local fields.
391 *
392 * @date 22/1/19
393 * @since 5.7.10
394 *
395 * @param array $fields An array of un prepared fields.
396 * @return array
397 */
398 function acf_add_local_fields( $fields = array() ) {
399
400 // Prepare for import (allows parent fields to offer up children).
401 $fields = acf_prepare_fields_for_import( $fields );
402
403 // Add each field.
404 foreach ( $fields as $field ) {
405 acf_add_local_field( $field, true );
406 }
407 }
408
409 /**
410 * acf_get_local_fields
411 *
412 * Returns all local fields for the given parent.
413 *
414 * @date 22/1/19
415 * @since 5.7.10
416 *
417 * @param string $parent The parent key.
418 * @return array
419 */
420 function acf_get_local_fields( $parent = '' ) {
421
422 // Return children
423 if ( $parent ) {
424 return acf_get_local_store( 'fields' )->query(
425 array(
426 'parent' => $parent,
427 )
428 );
429
430 // Return all.
431 } else {
432 return acf_get_local_store( 'fields' )->get();
433 }
434 }
435
436 /**
437 * acf_have_local_fields
438 *
439 * Returns true if local fields exist.
440 *
441 * @date 22/1/19
442 * @since 5.7.10
443 *
444 * @param string $parent The parent key.
445 * @return bool
446 */
447 function acf_have_local_fields( $parent = '' ) {
448 return acf_get_local_fields( $parent ) ? true : false;
449 }
450
451 /**
452 * acf_count_local_fields
453 *
454 * Returns the number of local fields for the given parent.
455 *
456 * @date 22/1/19
457 * @since 5.7.10
458 *
459 * @param string $parent The parent key.
460 * @return int
461 */
462 function acf_count_local_fields( $parent = '' ) {
463 return count( acf_get_local_fields( $parent ) );
464 }
465
466 /**
467 * acf_add_local_field
468 *
469 * Adds a local field.
470 *
471 * @date 22/1/19
472 * @since 5.7.10
473 *
474 * @param array $field The field array.
475 * @param bool $prepared Whether or not the field has already been prepared for import.
476 * @return void
477 */
478 function acf_add_local_field( $field, $prepared = false ) {
479
480 // Apply default properties needed for import.
481 $field = wp_parse_args(
482 $field,
483 array(
484 'key' => '',
485 'name' => '',
486 'type' => '',
487 'parent' => '',
488 )
489 );
490
491 // Generate key if only name is provided.
492 if ( ! $field['key'] ) {
493 $field['key'] = 'field_' . $field['name'];
494 }
495
496 // If called directly, allow sub fields to be correctly prepared.
497 if ( ! $prepared ) {
498 return acf_add_local_fields( array( $field ) );
499 }
500
501 // Extract attributes.
502 $key = $field['key'];
503 $name = $field['name'];
504
505 // Allow sub field to be added multipel times to different parents.
506 $store = acf_get_local_store( 'fields' );
507 if ( $store->is( $key ) ) {
508 $old_key = _acf_generate_local_key( $store->get( $key ) );
509 $new_key = _acf_generate_local_key( $field );
510 if ( $old_key !== $new_key ) {
511 $key = $new_key;
512 }
513 }
514
515 // Add field.
516 $store->set( $key, $field )->alias( $key, $name );
517 }
518
519 /**
520 * _acf_generate_local_key
521 *
522 * Generates a unique key based on the field's parent.
523 *
524 * @date 22/1/19
525 * @since 5.7.10
526 *
527 * @param string $key The field key.
528 * @return bool
529 */
530 function _acf_generate_local_key( $field ) {
531 return "{$field['key']}:{$field['parent']}";
532 }
533
534 /**
535 * acf_remove_local_field
536 *
537 * Removes a field for the given key.
538 *
539 * @date 22/1/19
540 * @since 5.7.10
541 *
542 * @param string $key The field key.
543 * @return bool
544 */
545 function acf_remove_local_field( $key = '' ) {
546 return acf_get_local_store( 'fields' )->remove( $key );
547 }
548
549 /**
550 * acf_is_local_field
551 *
552 * Returns true if a field exists for the given key or name.
553 *
554 * @date 22/1/19
555 * @since 5.7.10
556 *
557 * @param string $key The field group key.
558 * @return bool
559 */
560 function acf_is_local_field( $key = '' ) {
561 return acf_get_local_store( 'fields' )->has( $key );
562 }
563
564 /**
565 * acf_is_local_field_key
566 *
567 * Returns true if a field exists for the given key.
568 *
569 * @date 22/1/19
570 * @since 5.7.10
571 *
572 * @param string $key The field group key.
573 * @return bool
574 */
575 function acf_is_local_field_key( $key = '' ) {
576 return acf_get_local_store( 'fields' )->is( $key );
577 }
578
579 /**
580 * acf_get_local_field
581 *
582 * Returns a field for the given key.
583 *
584 * @date 22/1/19
585 * @since 5.7.10
586 *
587 * @param string $key The field group key.
588 * @return (array|null)
589 */
590 function acf_get_local_field( $key = '' ) {
591 return acf_get_local_store( 'fields' )->get( $key );
592 }
593
594 /**
595 * _acf_apply_get_local_field_groups
596 *
597 * Appends local field groups to the provided array.
598 *
599 * @date 23/1/19
600 * @since 5.7.10
601 *
602 * @param array $field_groups An array of field groups.
603 * @return array
604 */
605 function _acf_apply_get_local_field_groups( $groups = array() ) {
606 return _acf_apply_get_local_internal_posts( $groups, 'acf-field-group' );
607 }
608
609 /**
610 * Appends local ACF internal post types to the provided array.
611 *
612 * @since 6.1
613 *
614 * @param array $posts An array of ACF posts.
615 * @param string $post_type The ACF internal post type being loaded.
616 * @return array
617 */
618 function _acf_apply_get_local_internal_posts( $posts = array(), $post_type = 'acf-field-group' ) {
619 // Get local posts.
620 $local_posts = acf_get_local_internal_posts( $post_type );
621
622 if ( ! $local_posts ) {
623 return $posts;
624 }
625
626 // Generate map of "index" => "key" data.
627 $map = wp_list_pluck( $posts, 'key' );
628
629 // Loop over local posts and update/append local.
630 foreach ( $local_posts as $post ) {
631 $i = array_search( $post['key'], $map, true );
632 if ( $i !== false ) {
633 unset( $post['ID'] );
634 $posts[ $i ] = array_merge( $posts[ $i ], $post );
635 } else {
636 $posts[] = acf_get_internal_post_type( $post['key'], $post_type );
637 }
638 }
639
640 // Sort list via menu_order and title.
641 return wp_list_sort(
642 $posts,
643 array(
644 'menu_order' => 'ASC',
645 'title' => 'ASC',
646 )
647 );
648 }
649 add_filter( 'acf/load_field_groups', '_acf_apply_get_local_internal_posts', 20, 2 );
650 add_filter( 'acf/load_post_types', '_acf_apply_get_local_internal_posts', 20, 2 );
651 add_filter( 'acf/load_taxonomies', '_acf_apply_get_local_internal_posts', 20, 2 );
652
653 /**
654 * _acf_apply_is_local_field_key
655 *
656 * Returns true if is a local key.
657 *
658 * @date 23/1/19
659 * @since 5.7.10
660 *
661 * @param bool $bool The result.
662 * @param string $id The identifier.
663 * @return bool
664 */
665 function _acf_apply_is_local_field_key( $bool, $id ) {
666 return acf_is_local_field_key( $id );
667 }
668
669 // Hook into filter.
670 add_filter( 'acf/is_field_key', '_acf_apply_is_local_field_key', 20, 2 );
671
672 /**
673 * _acf_apply_is_local_field_group_key
674 *
675 * Returns true if is a local key.
676 *
677 * @date 23/1/19
678 * @since 5.7.10
679 *
680 * @param bool $bool The result.
681 * @param string $id The identifier.
682 * @return bool
683 */
684 function _acf_apply_is_local_field_group_key( $bool, $id ) {
685 return acf_is_local_field_group_key( $id );
686 }
687
688 /**
689 * Returns true if is a local key.
690 *
691 * @since 6.1
692 *
693 * @param bool $bool The result.
694 * @param string $id The identifier.
695 * @param string $post_type The post type.
696 * @return bool
697 */
698 function _acf_apply_is_local_internal_post_type_key( $bool, $id, $post_type = 'acf-field-group' ) {
699 return acf_is_local_internal_post_type_key( $id, $post_type );
700 }
701
702 // Hook into filter.
703 add_filter( 'acf/is_field_group_key', '_acf_apply_is_local_internal_post_type_key', 20, 3 );
704 add_filter( 'acf/is_post_type_key', '_acf_apply_is_local_internal_post_type_key', 20, 3 );
705 add_filter( 'acf/is_taxonomy_key', '_acf_apply_is_local_internal_post_type_key', 20, 3 );
706
707 /**
708 * _acf_do_prepare_local_fields
709 *
710 * Local fields that are added too early will not be correctly prepared by the field type class.
711 *
712 * @date 23/1/19
713 * @since 5.7.10
714 *
715 * @param void
716 * @return void
717 */
718 function _acf_do_prepare_local_fields() {
719
720 // Get fields.
721 $fields = acf_get_local_fields();
722
723 // If fields have been registered early, re-add to correctly prepare them.
724 if ( $fields ) {
725 acf_add_local_fields( $fields );
726 }
727 }
728
729 // Hook into action.
730 add_action( 'acf/include_fields', '_acf_do_prepare_local_fields', 0, 1 );
731
732
733