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