PluginProbe
Advanced Custom Fields (ACF®) / 6.0.7
Advanced Custom Fields (ACF®) v6.0.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
597 lines 11.7 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 bool
150 */
151 function acf_add_local_field_group( $field_group ) {
152
153 // Apply default properties needed for import.
154 $field_group = wp_parse_args(
155 $field_group,
156 array(
157 'key' => '',
158 'title' => '',
159 'fields' => array(),
160 'local' => 'php',
161 )
162 );
163
164 // Generate key if only name is provided.
165 if ( ! $field_group['key'] ) {
166 $field_group_key = 'group_' . acf_slugify( $field_group['title'], '_' );
167 if ( $field_group_key === 'group_' ) {
168 $field_group_key = 'group_' . md5( $field_group['title'] );
169 }
170 $field_group['key'] = $field_group_key;
171 }
172
173 // Bail early if field group already exists.
174 if ( acf_is_local_field_group( $field_group['key'] ) ) {
175 return false;
176 }
177
178 // Prepare field group for import (adds menu_order and parent properties to fields).
179 $field_group = acf_prepare_field_group_for_import( $field_group );
180
181 // Extract fields from group.
182 $fields = acf_extract_var( $field_group, 'fields' );
183
184 // Add to store
185 acf_get_local_store( 'groups' )->set( $field_group['key'], $field_group );
186
187 // Add fields
188 if ( $fields ) {
189 acf_add_local_fields( $fields );
190 }
191
192 // Return true on success.
193 return true;
194 }
195
196 /**
197 * register_field_group
198 *
199 * See acf_add_local_field_group().
200 *
201 * @date 22/1/19
202 * @since 5.7.10
203 *
204 * @param array $field_group The field group array.
205 * @return void
206 */
207 function register_field_group( $field_group ) {
208 acf_add_local_field_group( $field_group );
209 }
210
211 /**
212 * acf_remove_local_field_group
213 *
214 * Removes a field group for the given key.
215 *
216 * @date 22/1/19
217 * @since 5.7.10
218 *
219 * @param string $key The field group key.
220 * @return bool
221 */
222 function acf_remove_local_field_group( $key = '' ) {
223 return acf_get_local_store( 'groups' )->remove( $key );
224 }
225
226 /**
227 * acf_is_local_field_group
228 *
229 * Returns true if a field group exists for the given key.
230 *
231 * @date 22/1/19
232 * @since 5.7.10
233 *
234 * @param string $key The field group key.
235 * @return bool
236 */
237 function acf_is_local_field_group( $key = '' ) {
238 return acf_get_local_store( 'groups' )->has( $key );
239 }
240
241 /**
242 * acf_is_local_field_group_key
243 *
244 * Returns true if a field group exists for the given key.
245 *
246 * @date 22/1/19
247 * @since 5.7.10
248 *
249 * @param string $key The field group group key.
250 * @return bool
251 */
252 function acf_is_local_field_group_key( $key = '' ) {
253 return acf_get_local_store( 'groups' )->is( $key );
254 }
255
256 /**
257 * acf_get_local_field_group
258 *
259 * Returns a field group for the given key.
260 *
261 * @date 22/1/19
262 * @since 5.7.10
263 *
264 * @param string $key The field group key.
265 * @return (array|null)
266 */
267 function acf_get_local_field_group( $key = '' ) {
268 return acf_get_local_store( 'groups' )->get( $key );
269 }
270
271 /**
272 * acf_add_local_fields
273 *
274 * Adds an array of local fields.
275 *
276 * @date 22/1/19
277 * @since 5.7.10
278 *
279 * @param array $fields An array of un prepared fields.
280 * @return array
281 */
282 function acf_add_local_fields( $fields = array() ) {
283
284 // Prepare for import (allows parent fields to offer up children).
285 $fields = acf_prepare_fields_for_import( $fields );
286
287 // Add each field.
288 foreach ( $fields as $field ) {
289 acf_add_local_field( $field, true );
290 }
291 }
292
293 /**
294 * acf_get_local_fields
295 *
296 * Returns all local fields for the given parent.
297 *
298 * @date 22/1/19
299 * @since 5.7.10
300 *
301 * @param string $parent The parent key.
302 * @return array
303 */
304 function acf_get_local_fields( $parent = '' ) {
305
306 // Return children
307 if ( $parent ) {
308 return acf_get_local_store( 'fields' )->query(
309 array(
310 'parent' => $parent,
311 )
312 );
313
314 // Return all.
315 } else {
316 return acf_get_local_store( 'fields' )->get();
317 }
318 }
319
320 /**
321 * acf_have_local_fields
322 *
323 * Returns true if local fields exist.
324 *
325 * @date 22/1/19
326 * @since 5.7.10
327 *
328 * @param string $parent The parent key.
329 * @return bool
330 */
331 function acf_have_local_fields( $parent = '' ) {
332 return acf_get_local_fields( $parent ) ? true : false;
333 }
334
335 /**
336 * acf_count_local_fields
337 *
338 * Returns the number of local fields for the given parent.
339 *
340 * @date 22/1/19
341 * @since 5.7.10
342 *
343 * @param string $parent The parent key.
344 * @return int
345 */
346 function acf_count_local_fields( $parent = '' ) {
347 return count( acf_get_local_fields( $parent ) );
348 }
349
350 /**
351 * acf_add_local_field
352 *
353 * Adds a local field.
354 *
355 * @date 22/1/19
356 * @since 5.7.10
357 *
358 * @param array $field The field array.
359 * @param bool $prepared Whether or not the field has already been prepared for import.
360 * @return void
361 */
362 function acf_add_local_field( $field, $prepared = false ) {
363
364 // Apply default properties needed for import.
365 $field = wp_parse_args(
366 $field,
367 array(
368 'key' => '',
369 'name' => '',
370 'type' => '',
371 'parent' => '',
372 )
373 );
374
375 // Generate key if only name is provided.
376 if ( ! $field['key'] ) {
377 $field['key'] = 'field_' . $field['name'];
378 }
379
380 // If called directly, allow sub fields to be correctly prepared.
381 if ( ! $prepared ) {
382 return acf_add_local_fields( array( $field ) );
383 }
384
385 // Extract attributes.
386 $key = $field['key'];
387 $name = $field['name'];
388
389 // Allow sub field to be added multipel times to different parents.
390 $store = acf_get_local_store( 'fields' );
391 if ( $store->is( $key ) ) {
392 $old_key = _acf_generate_local_key( $store->get( $key ) );
393 $new_key = _acf_generate_local_key( $field );
394 if ( $old_key !== $new_key ) {
395 $key = $new_key;
396 }
397 }
398
399 // Add field.
400 $store->set( $key, $field )->alias( $key, $name );
401 }
402
403 /**
404 * _acf_generate_local_key
405 *
406 * Generates a unique key based on the field's parent.
407 *
408 * @date 22/1/19
409 * @since 5.7.10
410 *
411 * @param string $key The field key.
412 * @return bool
413 */
414 function _acf_generate_local_key( $field ) {
415 return "{$field['key']}:{$field['parent']}";
416 }
417
418 /**
419 * acf_remove_local_field
420 *
421 * Removes a field for the given key.
422 *
423 * @date 22/1/19
424 * @since 5.7.10
425 *
426 * @param string $key The field key.
427 * @return bool
428 */
429 function acf_remove_local_field( $key = '' ) {
430 return acf_get_local_store( 'fields' )->remove( $key );
431 }
432
433 /**
434 * acf_is_local_field
435 *
436 * Returns true if a field exists for the given key or name.
437 *
438 * @date 22/1/19
439 * @since 5.7.10
440 *
441 * @param string $key The field group key.
442 * @return bool
443 */
444 function acf_is_local_field( $key = '' ) {
445 return acf_get_local_store( 'fields' )->has( $key );
446 }
447
448 /**
449 * acf_is_local_field_key
450 *
451 * Returns true if a field exists for the given key.
452 *
453 * @date 22/1/19
454 * @since 5.7.10
455 *
456 * @param string $key The field group key.
457 * @return bool
458 */
459 function acf_is_local_field_key( $key = '' ) {
460 return acf_get_local_store( 'fields' )->is( $key );
461 }
462
463 /**
464 * acf_get_local_field
465 *
466 * Returns a field for the given key.
467 *
468 * @date 22/1/19
469 * @since 5.7.10
470 *
471 * @param string $key The field group key.
472 * @return (array|null)
473 */
474 function acf_get_local_field( $key = '' ) {
475 return acf_get_local_store( 'fields' )->get( $key );
476 }
477
478 /**
479 * _acf_apply_get_local_field_groups
480 *
481 * Appends local field groups to the provided array.
482 *
483 * @date 23/1/19
484 * @since 5.7.10
485 *
486 * @param array $field_groups An array of field groups.
487 * @return array
488 */
489 function _acf_apply_get_local_field_groups( $groups = array() ) {
490
491 // Get local groups
492 $local = acf_get_local_field_groups();
493 if ( $local ) {
494
495 // Generate map of "index" => "key" data.
496 $map = wp_list_pluck( $groups, 'key' );
497
498 // Loop over groups and update/append local.
499 foreach ( $local as $group ) {
500
501 // Get group allowing cache and filters to run.
502 // $group = acf_get_field_group( $group['key'] );
503
504 // Update.
505 $i = array_search( $group['key'], $map );
506 if ( $i !== false ) {
507 unset( $group['ID'] );
508 $groups[ $i ] = array_merge( $groups[ $i ], $group );
509
510 // Append
511 } else {
512 $groups[] = acf_get_field_group( $group['key'] );
513 }
514 }
515
516 // Sort list via menu_order and title.
517 $groups = wp_list_sort(
518 $groups,
519 array(
520 'menu_order' => 'ASC',
521 'title' => 'ASC',
522 )
523 );
524 }
525
526 // Return groups.
527 return $groups;
528 }
529
530 // Hook into filter.
531 add_filter( 'acf/load_field_groups', '_acf_apply_get_local_field_groups', 20, 1 );
532
533 /**
534 * _acf_apply_is_local_field_key
535 *
536 * Returns true if is a local key.
537 *
538 * @date 23/1/19
539 * @since 5.7.10
540 *
541 * @param bool $bool The result.
542 * @param string $id The identifier.
543 * @return bool
544 */
545 function _acf_apply_is_local_field_key( $bool, $id ) {
546 return acf_is_local_field_key( $id );
547 }
548
549 // Hook into filter.
550 add_filter( 'acf/is_field_key', '_acf_apply_is_local_field_key', 20, 2 );
551
552 /**
553 * _acf_apply_is_local_field_group_key
554 *
555 * Returns true if is a local key.
556 *
557 * @date 23/1/19
558 * @since 5.7.10
559 *
560 * @param bool $bool The result.
561 * @param string $id The identifier.
562 * @return bool
563 */
564 function _acf_apply_is_local_field_group_key( $bool, $id ) {
565 return acf_is_local_field_group_key( $id );
566 }
567
568 // Hook into filter.
569 add_filter( 'acf/is_field_group_key', '_acf_apply_is_local_field_group_key', 20, 2 );
570
571 /**
572 * _acf_do_prepare_local_fields
573 *
574 * Local fields that are added too early will not be correctly prepared by the field type class.
575 *
576 * @date 23/1/19
577 * @since 5.7.10
578 *
579 * @param void
580 * @return void
581 */
582 function _acf_do_prepare_local_fields() {
583
584 // Get fields.
585 $fields = acf_get_local_fields();
586
587 // If fields have been registered early, re-add to correctly prepare them.
588 if ( $fields ) {
589 acf_add_local_fields( $fields );
590 }
591 }
592
593 // Hook into action.
594 add_action( 'acf/include_fields', '_acf_do_prepare_local_fields', 0, 1 );
595
596
597