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