forked from WordPress/secure-custom-fields
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacf-field-group-functions.php
542 lines (492 loc) · 13.7 KB
/
acf-field-group-functions.php
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
<?php
/**
* acf_get_field_group
*
* Retrieves a field group for the given identifier.
*
* @date 30/09/13
* @since ACF 5.0.0
*
* @param (int|string) $id The field group ID, key or name.
* @return (array|false) The field group array.
*/
function acf_get_field_group( $id = 0 ) {
return acf_get_internal_post_type( $id, 'acf-field-group' );
}
/**
* acf_get_raw_field_group
*
* Retrieves raw field group data for the given identifier.
*
* @date 18/1/19
* @since ACF 5.7.10
*
* @param (int|string) $id The field ID, key or name.
* @return (array|false) The field group array.
*/
function acf_get_raw_field_group( $id = 0 ) {
return acf_get_raw_internal_post_type( $id, 'acf-field-group' );
}
/**
* acf_get_field_group_post
*
* Retrieves the field group's WP_Post object.
*
* @date 18/1/19
* @since ACF 5.7.10
*
* @param (int|string) $id The field group's ID, key or name.
* @return (array|false) The field group's array.
*/
function acf_get_field_group_post( $id = 0 ) {
return acf_get_internal_post_type_post( $id, 'acf-field-group' );
}
/**
* acf_is_field_group_key
*
* Returns true if the given identifier is a field group key.
*
* @date 6/12/2013
* @since ACF 5.0.0
*
* @param string $id The identifier.
* @return boolean
*/
function acf_is_field_group_key( $id = '' ) {
return acf_is_internal_post_type_key( $id, 'acf-field-group' );
}
/**
* Ensures the given field group is valid.
*
* @date 18/1/19
* @since ACF 5.7.10
*
* @param array $field_group The field group array.
* @return array
*/
function acf_validate_field_group( $field_group = array() ) {
return acf_validate_internal_post_type( $field_group, 'acf-field-group' );
}
/**
* acf_get_valid_field_group
*
* Ensures the given field group is valid.
*
* @date 28/09/13
* @since ACF 5.0.0
*
* @param array $field_group The field group array.
* @return array
*/
function acf_get_valid_field_group( $field_group = false ) {
return acf_validate_field_group( $field_group );
}
/**
* acf_translate_field_group
*
* Translates a field group's settings.
*
* @date 8/03/2016
* @since ACF 5.3.2
*
* @param array $field_group The field group array.
* @return array
*/
function acf_translate_field_group( $field_group = array() ) {
return acf_translate_internal_post_type( $field_group, 'acf-field-group' );
}
/**
* acf_get_field_groups
*
* Returns and array of field_groups for the given $filter.
*
* @date 30/09/13
* @since ACF 5.0.0
*
* @param array $filter An array of args to filter results by.
* @return array
*/
function acf_get_field_groups( $filter = array() ) {
return acf_get_internal_post_type_posts( 'acf-field-group', $filter );
}
/**
* acf_get_raw_field_groups
*
* Returns and array of raw field_group data.
*
* @date 18/1/19
* @since ACF 5.7.10
*
* @return array
*/
function acf_get_raw_field_groups() {
return acf_get_raw_internal_post_type_posts( 'acf-field-group' );
}
/**
* acf_filter_field_groups
*
* Returns a filtered array of field groups based on the given $args.
*
* @date 29/11/2013
* @since ACF 5.0.0
*
* @param array $field_groups An array of field groups.
* @param array $args An array of location args.
* @return array
*/
function acf_filter_field_groups( $field_groups, $args = array() ) {
return acf_filter_internal_post_type_posts( $field_groups, $args, 'acf-field-group' );
}
/**
* acf_get_field_group_visibility
*
* Returns true if the given field group's location rules match the given $args.
*
* @date 7/10/13
* @since ACF 5.0.0
*
* @param array $field_groups An array of field groups.
* @param array $args An array of location args.
* @return boolean
*/
function acf_get_field_group_visibility( $field_group, $args = array() ) {
// Check if active.
if ( ! $field_group['active'] ) {
return false;
}
// Check if location rules exist
if ( $field_group['location'] ) {
// Get the current screen.
$screen = acf_get_location_screen( $args );
// Loop through location groups.
foreach ( $field_group['location'] as $group ) {
// ignore group if no rules.
if ( empty( $group ) ) {
continue;
}
// Loop over rules and determine if all rules match.
$match_group = true;
foreach ( $group as $rule ) {
if ( ! acf_match_location_rule( $rule, $screen, $field_group ) ) {
$match_group = false;
break;
}
}
// If this group matches, show the field group.
if ( $match_group ) {
return true;
}
}
}
// Return default.
return false;
}
/**
* acf_update_field_group
*
* Updates a field group in the database.
*
* @date 21/1/19
* @since ACF 5.7.10
*
* @param array $field_group The field group array.
* @return array
*/
function acf_update_field_group( $field_group ) {
return acf_update_internal_post_type( $field_group, 'acf-field-group' );
}
/**
* _acf_apply_unique_field_group_slug
*
* Allows full control over 'acf-field-group' slugs.
*
* @date 21/1/19
* @since ACF 5.7.10
*
* @param string $slug The post slug.
* @param integer $post_ID Post ID.
* @param string $post_status The post status.
* @param string $post_type Post type.
* @param integer $post_parent Post parent ID
* @param string $original_slug The original post slug.
*/
function _acf_apply_unique_field_group_slug( $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug ) {
// Check post type and reset to original value.
if ( $post_type === 'acf-field-group' ) {
return $original_slug;
}
// Return slug.
return $slug;
}
/**
* acf_flush_field_group_cache
*
* Deletes all caches for this field group.
*
* @date 22/1/19
* @since ACF 5.7.10
*
* @param array $field_group The field group array.
* @return void
*/
function acf_flush_field_group_cache( $field_group ) {
acf_flush_internal_post_type_cache( $field_group, 'acf-field-group' );
}
/**
* acf_delete_field_group
*
* Deletes a field group from the database.
*
* @date 21/1/19
* @since ACF 5.7.10
*
* @param (int|string) $id The field group ID, key or name.
* @return boolean True if field group was deleted.
*/
function acf_delete_field_group( $id = 0 ) {
return acf_delete_internal_post_type( $id, 'acf-field-group' );
}
/**
* acf_trash_field_group
*
* Trashes a field group from the database.
*
* @date 2/10/13
* @since ACF 5.0.0
*
* @param (int|string) $id The field group ID, key or name.
* @return boolean True if field group was trashed.
*/
function acf_trash_field_group( $id = 0 ) {
return acf_trash_internal_post_type( $id, 'acf-field-group' );
}
/**
* acf_untrash_field_group
*
* Restores a field_group from the trash.
*
* @date 2/10/13
* @since ACF 5.0.0
*
* @param (int|string) $id The field_group ID, key or name.
* @return boolean True if field_group was trashed.
*/
function acf_untrash_field_group( $id = 0 ) {
return acf_untrash_internal_post_type( $id, 'acf-field-group' );
}
/**
* Filter callback which returns the previous post_status instead of "draft" for the "acf-field-group" post type.
*
* Prior to WordPress 5.6.0, this filter was not needed as restored posts were always assigned their original status.
*
* @since ACF 5.9.5
*
* @param string $new_status The new status of the post being restored.
* @param integer $post_id The ID of the post being restored.
* @param string $previous_status The status of the post at the point where it was trashed.
* @return string.
*/
function _acf_untrash_field_group_post_status( $new_status, $post_id, $previous_status ) {
return ( get_post_type( $post_id ) === 'acf-field-group' ) ? $previous_status : $new_status;
}
/**
* acf_is_field_group
*
* Returns true if the given params match a field group.
*
* @date 21/1/19
* @since ACF 5.7.10
*
* @param array $field_group The field group array.
* @param mixed $id An optional identifier to search for.
* @return boolean
*/
function acf_is_field_group( $field_group = false ) {
return acf_is_internal_post_type( $field_group, 'acf-field-group' );
}
/**
* acf_duplicate_field_group
*
* Duplicates a field group.
*
* @date 16/06/2014
* @since ACF 5.0.0
*
* @param (int|string) $id The field_group ID, key or name.
* @param integer $new_post_id Optional post ID to override.
* @return array The new field group.
*/
function acf_duplicate_field_group( $id = 0, $new_post_id = 0 ) {
return acf_duplicate_internal_post_type( $id, $new_post_id, 'acf-field-group' );
}
/**
* Activates or deactivates a field group.
*
* @param integer|string $id The field_group ID, key or name.
* @param boolean $activate True if the post should be activated.
* @return boolean
*/
function acf_update_field_group_active_status( $id, $activate = true ) {
return acf_update_internal_post_type_active_status( $id, $activate, 'acf-field-group' );
}
/**
* acf_get_field_group_style
*
* Returns the CSS styles generated from field group settings.
*
* @date 20/10/13
* @since ACF 5.0.0
*
* @param array $field_group The field group array.
* @return string.
*/
function acf_get_field_group_style( $field_group ) {
// Vars.
$style = '';
$elements = array(
'permalink' => '#edit-slug-box',
'the_content' => '#postdivrich',
'excerpt' => '#postexcerpt',
'custom_fields' => '#postcustom',
'discussion' => '#commentstatusdiv',
'comments' => '#commentsdiv',
'slug' => '#slugdiv',
'author' => '#authordiv',
'format' => '#formatdiv',
'page_attributes' => '#pageparentdiv',
'featured_image' => '#postimagediv',
'revisions' => '#revisionsdiv',
'categories' => '#categorydiv',
'tags' => '#tagsdiv-post_tag',
'send-trackbacks' => '#trackbacksdiv',
);
// Loop over field group settings and generate list of selectors to hide.
if ( is_array( $field_group['hide_on_screen'] ) ) {
$hide = array();
foreach ( $field_group['hide_on_screen'] as $k ) {
if ( isset( $elements[ $k ] ) ) {
$id = $elements[ $k ];
$hide[] = $id;
$hide[] = '#screen-meta label[for=' . substr( $id, 1 ) . '-hide]';
}
}
$style = implode( ', ', $hide ) . ' {display: none;}';
}
/**
* Filters the generated CSS styles.
*
* @date 12/02/2014
* @since ACF 5.0.0
*
* @param string $style The CSS styles.
* @param array $field_group The field group array.
*/
return apply_filters( 'acf/get_field_group_style', $style, $field_group );
}
/**
* acf_get_field_group_edit_link
*
* Checks if the current user can edit the field group and returns the edit url.
*
* @date 23/9/18
* @since ACF 5.7.7
*
* @param integer $post_id The field group ID.
* @return string
*/
function acf_get_field_group_edit_link( $post_id ) {
return acf_get_internal_post_type_edit_link( $post_id, 'acf-field-group' );
}
/**
* acf_prepare_field_group_for_export
*
* Returns a modified field group ready for export.
*
* @date 11/03/2014
* @since ACF 5.0.0
*
* @param array $field_group The field group array.
* @return array
*/
function acf_prepare_field_group_for_export( $field_group = array() ) {
return acf_prepare_internal_post_type_for_export( $field_group, 'acf-field-group' );
}
/**
* acf_prepare_field_group_for_import
*
* Prepares a field group for the import process.
*
* @date 21/11/19
* @since ACF 5.8.8
*
* @param array $field_group The field group array.
* @return array
*/
function acf_prepare_field_group_for_import( $field_group ) {
return acf_prepare_internal_post_type_for_import( $field_group, 'acf-field-group' );
}
/**
* acf_import_field_group
*
* Imports a field group into the database.
*
* @date 11/03/2014
* @since ACF 5.0.0
*
* @param array $field_group The field group array.
* @return array The new field group.
*/
function acf_import_field_group( $field_group ) {
return acf_import_internal_post_type( $field_group, 'acf-field-group' );
}
/**
* Returns an array of tabs for the field group settings.
* We combine a list of default tabs with filtered tabs.
* I.E. Default tabs should be static and should not be changed by the
* filtered tabs.
*
* @since ACF 6.1
*
* @return array Key/value array of the default settings tabs for field group settings.
*/
function acf_get_combined_field_group_settings_tabs() {
$default_field_group_settings_tabs = array(
'location_rules' => __( 'Location Rules', 'secure-custom-fields' ),
'presentation' => __( 'Presentation', 'secure-custom-fields' ),
'group_settings' => __( 'Group Settings', 'secure-custom-fields' ),
);
$field_group_settings_tabs = (array) apply_filters( 'acf/field_group/additional_group_settings_tabs', array() );
// remove any default tab values from the filter tabs.
foreach ( $field_group_settings_tabs as $key => $tab ) {
if ( isset( $default_field_group_settings_tabs[ $key ] ) ) {
unset( $field_group_settings_tabs[ $key ] );
}
}
$combined_field_group_settings_tabs = array_merge( $default_field_group_settings_tabs, $field_group_settings_tabs );
return $combined_field_group_settings_tabs;
}
/**
* Checks if a field group has the provided location rule.
*
* @since ACF 6.2.8
*
* @param integer $post_id The post ID of the field group.
* @param string $location The location type to check for.
* @return boolean
*/
function acf_field_group_has_location_type( int $post_id, string $location ) {
if ( empty( $post_id ) || empty( $location ) ) {
return false;
}
$field_group = acf_get_field_group( (int) $post_id );
if ( empty( $field_group['location'] ) ) {
return false;
}
foreach ( $field_group['location'] as $rule_group ) {
$params = array_column( $rule_group, 'param' );
if ( in_array( $location, $params, true ) ) {
return true;
}
}
return false;
}