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