1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* acf_is_field_group_key |
5
|
|
|
* |
6
|
|
|
* This function will return true or false for the given $group_key parameter |
7
|
|
|
* |
8
|
|
|
* @type function |
9
|
|
|
* @date 6/12/2013 |
10
|
|
|
* @since 5.0.0 |
11
|
|
|
* |
12
|
|
|
* @param $group_key (string) |
13
|
|
|
* @return (boolean) |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
View Code Duplication |
function acf_is_field_group_key( $key = '' ) { |
|
|
|
|
17
|
|
|
|
18
|
|
|
// bail early if not string |
19
|
|
|
if( !is_string($key) ) return false; |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
// bail early if is numeric (could be numeric string '123') |
23
|
|
|
if( is_numeric($key) ) return false; |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
// look for 'field_' prefix |
27
|
|
|
if( substr($key, 0, 6) === 'group_' ) return true; |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
// allow local field group key to not start with prefix |
31
|
|
|
if( acf_is_local_field_group($key) ) return true; |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
// return |
35
|
|
|
return false; |
36
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/* |
41
|
|
|
* acf_get_valid_field_group |
42
|
|
|
* |
43
|
|
|
* This function will fill in any missing keys to the $field_group array making it valid |
44
|
|
|
* |
45
|
|
|
* @type function |
46
|
|
|
* @date 28/09/13 |
47
|
|
|
* @since 5.0.0 |
48
|
|
|
* |
49
|
|
|
* @param $field_group (array) |
50
|
|
|
* @return $field_group (array) |
51
|
|
|
*/ |
|
|
|
|
52
|
|
|
|
53
|
|
|
function acf_get_valid_field_group( $field_group = false ) { |
54
|
|
|
|
55
|
|
|
// parse in defaults |
56
|
|
|
$field_group = acf_parse_args( $field_group, array( |
57
|
|
|
'ID' => 0, |
58
|
|
|
'key' => '', |
59
|
|
|
'title' => '', |
60
|
|
|
'fields' => array(), |
61
|
|
|
'location' => array(), |
62
|
|
|
'menu_order' => 0, |
63
|
|
|
'position' => 'normal', |
64
|
|
|
'style' => 'default', |
65
|
|
|
'label_placement' => 'top', |
66
|
|
|
'instruction_placement' => 'label', |
67
|
|
|
'hide_on_screen' => array(), |
68
|
|
|
'active' => 1, // Added in 5.2.9 |
69
|
|
|
'description' => '' // Added in 5.2.9 |
70
|
|
|
)); |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
// translate |
74
|
|
|
acf_translate_keys( $field_group, acf_get_setting('l10n_field_group') ); |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
// filter |
78
|
|
|
$field_group = apply_filters('acf/get_valid_field_group', $field_group); |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
// return |
82
|
|
|
return $field_group; |
83
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
/* |
88
|
|
|
* acf_get_field_groups |
89
|
|
|
* |
90
|
|
|
* This function will return an array of field groupss for the given args. Similar to the WP get_posts function |
91
|
|
|
* |
92
|
|
|
* @type function |
93
|
|
|
* @date 30/09/13 |
94
|
|
|
* @since 5.0.0 |
95
|
|
|
* |
96
|
|
|
* @param $args (array) |
97
|
|
|
* @return $field_groups (array) |
98
|
|
|
*/ |
|
|
|
|
99
|
|
|
|
100
|
|
|
function acf_get_field_groups( $args = false ) { |
101
|
|
|
|
102
|
|
|
// vars |
103
|
|
|
$field_groups = array(); |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
// cache |
107
|
|
|
$found = false; |
108
|
|
|
$cache = wp_cache_get( 'get_field_groups', 'acf', false, $found ); |
109
|
|
|
|
110
|
|
|
if( $found ) { |
111
|
|
|
|
112
|
|
|
return acf_filter_field_groups( $cache, $args ); |
113
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
// load from DB |
118
|
|
|
$posts = get_posts(array( |
119
|
|
|
'post_type' => 'acf-field-group', |
120
|
|
|
'posts_per_page' => -1, |
121
|
|
|
'orderby' => 'menu_order title', |
122
|
|
|
'order' => 'asc', |
123
|
|
|
'suppress_filters' => false, // allow WPML to modify the query |
124
|
|
|
'post_status' => array('publish', 'acf-disabled'), |
125
|
|
|
'update_post_meta_cache' => false |
126
|
|
|
)); |
127
|
|
|
|
128
|
|
|
|
129
|
|
|
// loop through and load field groups |
130
|
|
|
if( $posts ) { |
131
|
|
|
|
132
|
|
|
foreach( $posts as $post ) { |
133
|
|
|
|
134
|
|
|
// add to return array |
135
|
|
|
$field_groups[] = acf_get_field_group( $post ); |
136
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
|
142
|
|
|
// filter |
143
|
|
|
$field_groups = apply_filters('acf/get_field_groups', $field_groups); |
144
|
|
|
|
145
|
|
|
|
146
|
|
|
// set cache |
147
|
|
|
wp_cache_set( 'get_field_groups', $field_groups, 'acf' ); |
148
|
|
|
|
149
|
|
|
|
150
|
|
|
// return |
151
|
|
|
return acf_filter_field_groups( $field_groups, $args ); |
152
|
|
|
|
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
|
156
|
|
|
/* |
157
|
|
|
* acf_filter_field_groups |
158
|
|
|
* |
159
|
|
|
* This function is used by acf_get_field_groups to filter out fields groups based on location rules |
160
|
|
|
* |
161
|
|
|
* @type function |
162
|
|
|
* @date 29/11/2013 |
163
|
|
|
* @since 5.0.0 |
164
|
|
|
* |
165
|
|
|
* @param $field_groups (array) |
166
|
|
|
* @param $args (array) |
167
|
|
|
* @return $field_groups (array) |
168
|
|
|
*/ |
|
|
|
|
169
|
|
|
|
170
|
|
|
function acf_filter_field_groups( $field_groups, $args = false ) { |
171
|
|
|
|
172
|
|
|
// bail early if empty sargs |
173
|
|
|
if( empty($args) || empty($field_groups) ) { |
174
|
|
|
|
175
|
|
|
return $field_groups; |
176
|
|
|
|
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
|
180
|
|
|
// vars |
181
|
|
|
$keys = array_keys( $field_groups ); |
182
|
|
|
|
183
|
|
|
|
184
|
|
|
// loop through keys |
185
|
|
|
foreach( $keys as $key ) { |
186
|
|
|
|
187
|
|
|
// get visibility |
188
|
|
|
$visibility = acf_get_field_group_visibility( $field_groups[ $key ], $args ); |
|
|
|
|
189
|
|
|
|
190
|
|
|
|
191
|
|
|
// unset |
192
|
|
|
if( !$visibility ) { |
193
|
|
|
|
194
|
|
|
unset($field_groups[ $key ]); |
195
|
|
|
|
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
|
201
|
|
|
// re assign index |
202
|
|
|
$field_groups = array_values( $field_groups ); |
203
|
|
|
|
204
|
|
|
|
205
|
|
|
// return |
206
|
|
|
return $field_groups; |
207
|
|
|
|
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
|
211
|
|
|
/* |
212
|
|
|
* acf_get_field_group |
213
|
|
|
* |
214
|
|
|
* This function will take either a post object, post ID or even null (for global $post), and |
215
|
|
|
* will then return a valid field group array |
216
|
|
|
* |
217
|
|
|
* @type function |
218
|
|
|
* @date 30/09/13 |
219
|
|
|
* @since 5.0.0 |
220
|
|
|
* |
221
|
|
|
* @param $selector (mixed) |
222
|
|
|
* @return $field_group (array) |
223
|
|
|
*/ |
|
|
|
|
224
|
|
|
|
225
|
|
|
function acf_get_field_group( $selector = false ) { |
226
|
|
|
|
227
|
|
|
// vars |
228
|
|
|
$field_group = false; |
|
|
|
|
229
|
|
|
$k = 'ID'; |
230
|
|
|
$v = 0; |
|
|
|
|
231
|
|
|
|
232
|
|
|
|
233
|
|
|
// $post_id or $key |
234
|
|
|
if( is_numeric($selector) ) { |
235
|
|
|
|
236
|
|
|
$v = $selector; |
237
|
|
|
|
238
|
|
|
} elseif( is_string($selector) ) { |
239
|
|
|
|
240
|
|
|
$k = 'key'; |
241
|
|
|
$v = $selector; |
242
|
|
|
|
243
|
|
|
} elseif( is_object($selector) ) { |
244
|
|
|
|
245
|
|
|
$v = $selector->ID; |
246
|
|
|
|
247
|
|
|
} else { |
248
|
|
|
|
249
|
|
|
return false; |
250
|
|
|
|
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
|
254
|
|
|
// get cache key |
255
|
|
|
$cache_key = "get_field_group/{$k}={$v}"; |
256
|
|
|
|
257
|
|
|
|
258
|
|
|
// get cache |
259
|
|
|
$found = false; |
260
|
|
|
$cache = wp_cache_get( $cache_key, 'acf', false, $found ); |
261
|
|
|
|
262
|
|
|
if( $found ) return $cache; |
263
|
|
|
|
264
|
|
|
|
265
|
|
|
// get field group from ID or key |
266
|
|
|
if( $k == 'ID' ) { |
267
|
|
|
|
268
|
|
|
$field_group = _acf_get_field_group_by_id( $v ); |
269
|
|
|
|
270
|
|
|
} else { |
271
|
|
|
|
272
|
|
|
$field_group = _acf_get_field_group_by_key( $v ); |
273
|
|
|
|
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
|
277
|
|
|
// filter for 3rd party customization |
278
|
|
|
$field_group = apply_filters('acf/get_field_group', $field_group); |
279
|
|
|
|
280
|
|
|
|
281
|
|
|
// set cache |
282
|
|
|
wp_cache_set( $cache_key, $field_group, 'acf' ); |
283
|
|
|
|
284
|
|
|
|
285
|
|
|
// return |
286
|
|
|
return $field_group; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
|
290
|
|
|
/* |
291
|
|
|
* _acf_get_field_group_by_id |
292
|
|
|
* |
293
|
|
|
* This function will get a field group by its ID |
294
|
|
|
* |
295
|
|
|
* @type function |
296
|
|
|
* @date 27/02/2014 |
297
|
|
|
* @since 5.0.0 |
298
|
|
|
* |
299
|
|
|
* @param $post_id (int) |
300
|
|
|
* @return $field_group (array) |
301
|
|
|
*/ |
|
|
|
|
302
|
|
|
|
303
|
|
|
function _acf_get_field_group_by_id( $post_id = 0 ) { |
304
|
|
|
|
305
|
|
|
// get post |
306
|
|
|
$post = get_post( $post_id ); |
307
|
|
|
|
308
|
|
|
|
309
|
|
|
// bail early if no post, or is not a field group |
310
|
|
|
if( empty($post) || $post->post_type != 'acf-field-group' ) { |
311
|
|
|
|
312
|
|
|
return false; |
313
|
|
|
|
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
|
317
|
|
|
// modify post_status (new field-group starts as auto-draft) |
318
|
|
|
if( $post->post_status == 'auto-draft' ) { |
319
|
|
|
|
320
|
|
|
$post->post_status = 'publish'; |
321
|
|
|
|
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
|
325
|
|
|
// unserialize data |
326
|
|
|
$field_group = maybe_unserialize( $post->post_content ); |
327
|
|
|
|
328
|
|
|
|
329
|
|
|
// update attributes |
330
|
|
|
$field_group['ID'] = $post->ID; |
331
|
|
|
$field_group['title'] = $post->post_title; |
332
|
|
|
$field_group['key'] = $post->post_name; |
333
|
|
|
$field_group['menu_order'] = $post->menu_order; |
334
|
|
|
$field_group['active'] = ($post->post_status === 'publish') ? 1 : 0; |
335
|
|
|
|
336
|
|
|
|
337
|
|
|
// is JSON |
338
|
|
|
if( acf_is_local_field_group( $field_group['key'] ) ) { |
339
|
|
|
|
340
|
|
|
// override |
341
|
|
|
$field_group = acf_get_local_field_group( $field_group['key'] ); |
342
|
|
|
|
343
|
|
|
|
344
|
|
|
// restore ID |
345
|
|
|
$field_group['ID'] = $post->ID; |
346
|
|
|
|
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
|
350
|
|
|
// validate |
351
|
|
|
$field_group = acf_get_valid_field_group( $field_group ); |
|
|
|
|
352
|
|
|
|
353
|
|
|
|
354
|
|
|
// return |
355
|
|
|
return $field_group; |
356
|
|
|
|
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
|
360
|
|
|
/* |
361
|
|
|
* _acf_get_field_group_by_key |
362
|
|
|
* |
363
|
|
|
* This function will get a field group by its key |
364
|
|
|
* |
365
|
|
|
* @type function |
366
|
|
|
* @date 27/02/2014 |
367
|
|
|
* @since 5.0.0 |
368
|
|
|
* |
369
|
|
|
* @param $key (string) |
370
|
|
|
* @return $field_group (array) |
371
|
|
|
*/ |
|
|
|
|
372
|
|
|
|
373
|
|
|
function _acf_get_field_group_by_key( $key = '' ) { |
374
|
|
|
|
375
|
|
|
// vars |
376
|
|
|
$field_group = false; |
377
|
|
|
|
378
|
|
|
|
379
|
|
|
// try JSON before DB to save query time |
380
|
|
|
if( acf_is_local_field_group( $key ) ) { |
381
|
|
|
|
382
|
|
|
$field_group = acf_get_local_field_group( $key ); |
383
|
|
|
|
384
|
|
|
// validate |
385
|
|
|
$field_group = acf_get_valid_field_group( $field_group ); |
386
|
|
|
|
387
|
|
|
// return |
388
|
|
|
return $field_group; |
389
|
|
|
|
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
|
393
|
|
|
// vars |
394
|
|
|
$args = array( |
395
|
|
|
'posts_per_page' => 1, |
396
|
|
|
'post_type' => 'acf-field-group', |
397
|
|
|
'orderby' => 'menu_order title', |
398
|
|
|
'order' => 'ASC', |
399
|
|
|
'suppress_filters' => false, |
400
|
|
|
'post_status' => array('publish', 'acf-disabled', 'trash'), |
401
|
|
|
'acf_group_key' => $key |
402
|
|
|
); |
403
|
|
|
|
404
|
|
|
|
405
|
|
|
// load posts |
406
|
|
|
$posts = get_posts( $args ); |
407
|
|
|
|
408
|
|
|
|
409
|
|
|
// validate |
410
|
|
|
if( empty($posts[0]) ) { |
411
|
|
|
|
412
|
|
|
return $field_group; |
413
|
|
|
|
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
|
417
|
|
|
// load from ID |
418
|
|
|
$field_group = _acf_get_field_group_by_id( $posts[0]->ID ); |
419
|
|
|
|
420
|
|
|
|
421
|
|
|
// return |
422
|
|
|
return $field_group; |
423
|
|
|
|
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
|
427
|
|
|
/* |
428
|
|
|
* acf_update_field_group |
429
|
|
|
* |
430
|
|
|
* This function will update a field group into the database. |
431
|
|
|
* The returned field group will always contain an ID |
432
|
|
|
* |
433
|
|
|
* @type function |
434
|
|
|
* @date 28/09/13 |
435
|
|
|
* @since 5.0.0 |
436
|
|
|
* |
437
|
|
|
* @param $field_group (array) |
438
|
|
|
* @return $field_group (array) |
439
|
|
|
*/ |
|
|
|
|
440
|
|
|
|
441
|
|
|
function acf_update_field_group( $field_group = array() ) { |
442
|
|
|
|
443
|
|
|
// validate |
444
|
|
|
$field_group = acf_get_valid_field_group( $field_group ); |
|
|
|
|
445
|
|
|
|
446
|
|
|
|
447
|
|
|
// may have been posted. Remove slashes |
448
|
|
|
$field_group = wp_unslash( $field_group ); |
449
|
|
|
|
450
|
|
|
|
451
|
|
|
// locations may contain 'uniquid' array keys |
452
|
|
|
$field_group['location'] = array_values( $field_group['location'] ); |
453
|
|
|
|
454
|
|
|
foreach( $field_group['location'] as $k => $v ) { |
455
|
|
|
|
456
|
|
|
$field_group['location'][ $k ] = array_values( $v ); |
457
|
|
|
|
458
|
|
|
} |
459
|
|
|
|
460
|
|
|
|
461
|
|
|
// store origional field group for return |
462
|
|
|
$data = $field_group; |
463
|
|
|
|
464
|
|
|
|
465
|
|
|
// extract some args |
466
|
|
|
$extract = acf_extract_vars($data, array( |
467
|
|
|
'ID', |
468
|
|
|
'key', |
469
|
|
|
'title', |
470
|
|
|
'menu_order', |
471
|
|
|
'fields', |
472
|
|
|
'active' |
473
|
|
|
)); |
474
|
|
|
|
475
|
|
|
|
476
|
|
|
// vars |
477
|
|
|
$data = maybe_serialize( $data ); |
478
|
|
|
$post_status = $extract['active'] ? 'publish' : 'acf-disabled'; |
479
|
|
|
|
480
|
|
|
|
481
|
|
|
// save |
482
|
|
|
$save = array( |
483
|
|
|
'ID' => $extract['ID'], |
484
|
|
|
'post_status' => $post_status, |
485
|
|
|
'post_type' => 'acf-field-group', |
486
|
|
|
'post_title' => $extract['title'], |
487
|
|
|
'post_name' => $extract['key'], |
488
|
|
|
'post_excerpt' => sanitize_title($extract['title']), |
489
|
|
|
'post_content' => $data, |
490
|
|
|
'menu_order' => $extract['menu_order'], |
491
|
|
|
); |
492
|
|
|
|
493
|
|
|
|
494
|
|
|
// allow field groups to contain the same name |
495
|
|
|
add_filter( 'wp_unique_post_slug', 'acf_update_field_group_wp_unique_post_slug', 100, 6 ); |
496
|
|
|
|
497
|
|
|
|
498
|
|
|
// update the field group and update the ID |
499
|
|
|
if( $field_group['ID'] ) { |
500
|
|
|
|
501
|
|
|
wp_update_post( $save ); |
502
|
|
|
|
503
|
|
|
} else { |
504
|
|
|
|
505
|
|
|
$field_group['ID'] = wp_insert_post( $save ); |
506
|
|
|
|
507
|
|
|
} |
508
|
|
|
|
509
|
|
|
|
510
|
|
|
// action for 3rd party customization |
511
|
|
|
do_action('acf/update_field_group', $field_group); |
512
|
|
|
|
513
|
|
|
|
514
|
|
|
// clear cache |
515
|
|
|
wp_cache_delete("get_field_group/ID={$field_group['ID']}", 'acf'); |
516
|
|
|
wp_cache_delete("get_field_group/key={$field_group['key']}", 'acf'); |
517
|
|
|
wp_cache_delete("get_field_groups", 'acf'); |
518
|
|
|
|
519
|
|
|
|
520
|
|
|
// return |
521
|
|
|
return $field_group; |
522
|
|
|
|
523
|
|
|
} |
524
|
|
|
|
525
|
|
|
function acf_update_field_group_wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug ) { |
|
|
|
|
526
|
|
|
|
527
|
|
|
if( $post_type == 'acf-field-group' ) { |
528
|
|
|
|
529
|
|
|
$slug = $original_slug; |
530
|
|
|
|
531
|
|
|
} |
532
|
|
|
|
533
|
|
|
return $slug; |
534
|
|
|
} |
535
|
|
|
|
536
|
|
|
|
537
|
|
|
/* |
538
|
|
|
* acf_duplicate_field_group |
539
|
|
|
* |
540
|
|
|
* This function will duplicate a field group into the database |
541
|
|
|
* |
542
|
|
|
* @type function |
543
|
|
|
* @date 28/09/13 |
544
|
|
|
* @since 5.0.0 |
545
|
|
|
* |
546
|
|
|
* @param $selector (mixed) |
547
|
|
|
* @param $new_post_id (int) allow specific ID to override (good for WPML translations) |
548
|
|
|
* @return $field_group (array) |
549
|
|
|
*/ |
|
|
|
|
550
|
|
|
|
551
|
|
|
function acf_duplicate_field_group( $selector = 0, $new_post_id = 0 ) { |
552
|
|
|
|
553
|
|
|
// disable JSON to avoid conflicts between DB and JSON |
554
|
|
|
acf_disable_local(); |
555
|
|
|
|
556
|
|
|
|
557
|
|
|
// load the origional field gorup |
558
|
|
|
$field_group = acf_get_field_group( $selector ); |
|
|
|
|
559
|
|
|
|
560
|
|
|
|
561
|
|
|
// bail early if field group did not load correctly |
562
|
|
|
if( empty($field_group) ) { |
563
|
|
|
|
564
|
|
|
return false; |
565
|
|
|
|
566
|
|
|
} |
567
|
|
|
|
568
|
|
|
|
569
|
|
|
// keep backup of field group |
570
|
|
|
$orig_field_group = $field_group; |
571
|
|
|
|
572
|
|
|
|
573
|
|
|
// update ID |
574
|
|
|
$field_group['ID'] = $new_post_id; |
575
|
|
|
$field_group['key'] = uniqid('group_'); |
576
|
|
|
|
577
|
|
|
|
578
|
|
|
// add (copy) |
579
|
|
|
if( !$new_post_id ) { |
580
|
|
|
|
581
|
|
|
$field_group['title'] .= ' (' . __("copy", 'acf') . ')'; |
582
|
|
|
|
583
|
|
|
} |
584
|
|
|
|
585
|
|
|
|
586
|
|
|
// save |
587
|
|
|
$field_group = acf_update_field_group( $field_group ); |
588
|
|
|
|
589
|
|
|
|
590
|
|
|
// get fields |
591
|
|
|
$fields = acf_get_fields( $orig_field_group ); |
592
|
|
|
|
593
|
|
|
|
594
|
|
|
// duplicate fields |
595
|
|
|
acf_duplicate_fields( $fields, $field_group['ID'] ); |
596
|
|
|
|
597
|
|
|
|
598
|
|
|
// action for 3rd party customization |
599
|
|
|
do_action('acf/duplicate_field_group', $field_group); |
600
|
|
|
|
601
|
|
|
|
602
|
|
|
// return |
603
|
|
|
return $field_group; |
604
|
|
|
|
605
|
|
|
} |
606
|
|
|
|
607
|
|
|
|
608
|
|
|
/* |
609
|
|
|
* acf_get_field_count |
610
|
|
|
* |
611
|
|
|
* This function will return the number of fields for the given field group |
612
|
|
|
* |
613
|
|
|
* @type function |
614
|
|
|
* @date 17/10/13 |
615
|
|
|
* @since 5.0.0 |
616
|
|
|
* |
617
|
|
|
* @param $field_group_id (int) |
618
|
|
|
* @return (int) |
619
|
|
|
*/ |
620
|
|
|
|
621
|
|
|
function acf_get_field_count( $field_group ) { |
622
|
|
|
|
623
|
|
|
// vars |
624
|
|
|
$count = 0; |
|
|
|
|
625
|
|
|
|
626
|
|
|
|
627
|
|
|
// local |
628
|
|
|
if( !$field_group['ID'] ) { |
629
|
|
|
|
630
|
|
|
$count = acf_count_local_fields( $field_group['key'] ); |
631
|
|
|
|
632
|
|
|
// DB |
633
|
|
|
} else { |
634
|
|
|
|
635
|
|
|
// load fields |
636
|
|
|
$posts = get_posts(array( |
637
|
|
|
'posts_per_page' => -1, |
638
|
|
|
'post_type' => 'acf-field', |
639
|
|
|
'orderby' => 'menu_order', |
640
|
|
|
'order' => 'ASC', |
641
|
|
|
'suppress_filters' => true, // DO NOT allow WPML to modify the query |
642
|
|
|
'post_parent' => $field_group['ID'], |
643
|
|
|
'fields' => 'ids', |
644
|
|
|
'post_status' => 'publish, trash' // 'any' won't get trashed fields |
645
|
|
|
)); |
646
|
|
|
|
647
|
|
|
$count = count($posts); |
648
|
|
|
|
649
|
|
|
} |
650
|
|
|
|
651
|
|
|
|
652
|
|
|
// filter for 3rd party customization |
653
|
|
|
$count = apply_filters('acf/get_field_count', $count, $field_group); |
654
|
|
|
|
655
|
|
|
|
656
|
|
|
// return |
657
|
|
|
return $count; |
658
|
|
|
|
659
|
|
|
} |
660
|
|
|
|
661
|
|
|
|
662
|
|
|
/* |
663
|
|
|
* acf_delete_field_group |
664
|
|
|
* |
665
|
|
|
* This function will delete the field group and its fields from the DB |
666
|
|
|
* |
667
|
|
|
* @type function |
668
|
|
|
* @date 5/12/2013 |
669
|
|
|
* @since 5.0.0 |
670
|
|
|
* |
671
|
|
|
* @param $selector (mixed) |
672
|
|
|
* @return (boolean) |
673
|
|
|
*/ |
674
|
|
|
|
675
|
|
View Code Duplication |
function acf_delete_field_group( $selector = 0 ) { |
|
|
|
|
676
|
|
|
|
677
|
|
|
// disable JSON to avoid conflicts between DB and JSON |
678
|
|
|
acf_disable_local(); |
679
|
|
|
|
680
|
|
|
|
681
|
|
|
// load the origional field gorup |
682
|
|
|
$field_group = acf_get_field_group( $selector ); |
|
|
|
|
683
|
|
|
|
684
|
|
|
|
685
|
|
|
// bail early if field group did not load correctly |
686
|
|
|
if( empty($field_group) ) return false; |
687
|
|
|
|
688
|
|
|
|
689
|
|
|
// get fields |
690
|
|
|
$fields = acf_get_fields($field_group); |
691
|
|
|
|
692
|
|
|
|
693
|
|
|
if( !empty($fields) ) { |
694
|
|
|
|
695
|
|
|
foreach( $fields as $field ) { |
696
|
|
|
|
697
|
|
|
acf_delete_field( $field['ID'] ); |
698
|
|
|
|
699
|
|
|
} |
700
|
|
|
|
701
|
|
|
} |
702
|
|
|
|
703
|
|
|
|
704
|
|
|
// delete |
705
|
|
|
wp_delete_post( $field_group['ID'] ); |
706
|
|
|
|
707
|
|
|
|
708
|
|
|
// action for 3rd party customization |
709
|
|
|
do_action('acf/delete_field_group', $field_group); |
710
|
|
|
|
711
|
|
|
|
712
|
|
|
// return |
713
|
|
|
return true; |
714
|
|
|
} |
715
|
|
|
|
716
|
|
|
|
717
|
|
|
/* |
718
|
|
|
* acf_trash_field_group |
719
|
|
|
* |
720
|
|
|
* This function will trash the field group and its fields |
721
|
|
|
* |
722
|
|
|
* @type function |
723
|
|
|
* @date 5/12/2013 |
724
|
|
|
* @since 5.0.0 |
725
|
|
|
* |
726
|
|
|
* @param $selector (mixed) |
727
|
|
|
* @return (boolean) |
728
|
|
|
*/ |
729
|
|
|
|
730
|
|
View Code Duplication |
function acf_trash_field_group( $selector = 0 ) { |
|
|
|
|
731
|
|
|
|
732
|
|
|
// disable JSON to avoid conflicts between DB and JSON |
733
|
|
|
acf_disable_local(); |
734
|
|
|
|
735
|
|
|
|
736
|
|
|
// load the origional field gorup |
737
|
|
|
$field_group = acf_get_field_group( $selector ); |
|
|
|
|
738
|
|
|
|
739
|
|
|
|
740
|
|
|
// bail early if field group did not load correctly |
741
|
|
|
if( empty($field_group) ) return false; |
742
|
|
|
|
743
|
|
|
|
744
|
|
|
// get fields |
745
|
|
|
$fields = acf_get_fields($field_group); |
746
|
|
|
|
747
|
|
|
|
748
|
|
|
if( !empty($fields) ) { |
749
|
|
|
|
750
|
|
|
foreach( $fields as $field ) { |
751
|
|
|
|
752
|
|
|
acf_trash_field( $field['ID'] ); |
753
|
|
|
|
754
|
|
|
} |
755
|
|
|
|
756
|
|
|
} |
757
|
|
|
|
758
|
|
|
|
759
|
|
|
// delete |
760
|
|
|
wp_trash_post( $field_group['ID'] ); |
761
|
|
|
|
762
|
|
|
|
763
|
|
|
// action for 3rd party customization |
764
|
|
|
do_action('acf/trash_field_group', $field_group); |
765
|
|
|
|
766
|
|
|
|
767
|
|
|
// return |
768
|
|
|
return true; |
769
|
|
|
} |
770
|
|
|
|
771
|
|
|
|
772
|
|
|
/* |
773
|
|
|
* acf_untrash_field_group |
774
|
|
|
* |
775
|
|
|
* This function will restore from trash the field group and its fields |
776
|
|
|
* |
777
|
|
|
* @type function |
778
|
|
|
* @date 5/12/2013 |
779
|
|
|
* @since 5.0.0 |
780
|
|
|
* |
781
|
|
|
* @param $selector (mixed) |
782
|
|
|
* @return (boolean) |
783
|
|
|
*/ |
784
|
|
|
|
785
|
|
View Code Duplication |
function acf_untrash_field_group( $selector = 0 ) { |
|
|
|
|
786
|
|
|
|
787
|
|
|
// disable JSON to avoid conflicts between DB and JSON |
788
|
|
|
acf_disable_local(); |
789
|
|
|
|
790
|
|
|
|
791
|
|
|
// load the origional field gorup |
792
|
|
|
$field_group = acf_get_field_group( $selector ); |
|
|
|
|
793
|
|
|
|
794
|
|
|
|
795
|
|
|
// bail early if field group did not load correctly |
796
|
|
|
if( empty($field_group) ) return false; |
797
|
|
|
|
798
|
|
|
|
799
|
|
|
// get fields |
800
|
|
|
$fields = acf_get_fields($field_group); |
801
|
|
|
|
802
|
|
|
|
803
|
|
|
if( !empty($fields) ) { |
804
|
|
|
|
805
|
|
|
foreach( $fields as $field ) { |
806
|
|
|
|
807
|
|
|
acf_untrash_field( $field['ID'] ); |
808
|
|
|
|
809
|
|
|
} |
810
|
|
|
|
811
|
|
|
} |
812
|
|
|
|
813
|
|
|
|
814
|
|
|
// delete |
815
|
|
|
wp_untrash_post( $field_group['ID'] ); |
816
|
|
|
|
817
|
|
|
|
818
|
|
|
// action for 3rd party customization |
819
|
|
|
do_action('acf/untrash_field_group', $field_group); |
820
|
|
|
|
821
|
|
|
|
822
|
|
|
// return |
823
|
|
|
return true; |
824
|
|
|
} |
825
|
|
|
|
826
|
|
|
|
827
|
|
|
|
828
|
|
|
/* |
829
|
|
|
* acf_get_field_group_style |
830
|
|
|
* |
831
|
|
|
* This function will render the CSS for a given field group |
832
|
|
|
* |
833
|
|
|
* @type function |
834
|
|
|
* @date 20/10/13 |
835
|
|
|
* @since 5.0.0 |
836
|
|
|
* |
837
|
|
|
* @param $field_group (array) |
838
|
|
|
* @return n/a |
839
|
|
|
*/ |
|
|
|
|
840
|
|
|
|
841
|
|
|
function acf_get_field_group_style( $field_group ) { |
842
|
|
|
|
843
|
|
|
// vars |
844
|
|
|
$e = ''; |
845
|
|
|
|
846
|
|
|
|
847
|
|
|
// validate |
848
|
|
|
if( !is_array($field_group['hide_on_screen']) ) |
849
|
|
|
{ |
850
|
|
|
return $e; |
851
|
|
|
} |
852
|
|
|
|
853
|
|
|
|
854
|
|
|
// add style to html |
855
|
|
|
if( in_array('permalink',$field_group['hide_on_screen']) ) |
856
|
|
|
{ |
857
|
|
|
$e .= '#edit-slug-box {display: none;} '; |
858
|
|
|
} |
859
|
|
|
|
860
|
|
|
if( in_array('the_content',$field_group['hide_on_screen']) ) |
861
|
|
|
{ |
862
|
|
|
$e .= '#postdivrich {display: none;} '; |
863
|
|
|
} |
864
|
|
|
|
865
|
|
|
if( in_array('excerpt',$field_group['hide_on_screen']) ) |
866
|
|
|
{ |
867
|
|
|
$e .= '#postexcerpt, #screen-meta label[for=postexcerpt-hide] {display: none;} '; |
868
|
|
|
} |
869
|
|
|
|
870
|
|
|
if( in_array('custom_fields',$field_group['hide_on_screen']) ) |
871
|
|
|
{ |
872
|
|
|
$e .= '#postcustom, #screen-meta label[for=postcustom-hide] { display: none; } '; |
873
|
|
|
} |
874
|
|
|
|
875
|
|
|
if( in_array('discussion',$field_group['hide_on_screen']) ) |
876
|
|
|
{ |
877
|
|
|
$e .= '#commentstatusdiv, #screen-meta label[for=commentstatusdiv-hide] {display: none;} '; |
878
|
|
|
} |
879
|
|
|
|
880
|
|
|
if( in_array('comments',$field_group['hide_on_screen']) ) |
881
|
|
|
{ |
882
|
|
|
$e .= '#commentsdiv, #screen-meta label[for=commentsdiv-hide] {display: none;} '; |
883
|
|
|
} |
884
|
|
|
|
885
|
|
|
if( in_array('slug',$field_group['hide_on_screen']) ) |
886
|
|
|
{ |
887
|
|
|
$e .= '#slugdiv, #screen-meta label[for=slugdiv-hide] {display: none;} '; |
888
|
|
|
} |
889
|
|
|
|
890
|
|
|
if( in_array('author',$field_group['hide_on_screen']) ) |
891
|
|
|
{ |
892
|
|
|
$e .= '#authordiv, #screen-meta label[for=authordiv-hide] {display: none;} '; |
893
|
|
|
} |
894
|
|
|
|
895
|
|
|
if( in_array('format',$field_group['hide_on_screen']) ) |
896
|
|
|
{ |
897
|
|
|
$e .= '#formatdiv, #screen-meta label[for=formatdiv-hide] {display: none;} '; |
898
|
|
|
} |
899
|
|
|
|
900
|
|
|
if( in_array('page_attributes',$field_group['hide_on_screen']) ) |
901
|
|
|
{ |
902
|
|
|
$e .= '#pageparentdiv {display: none;} '; |
903
|
|
|
} |
904
|
|
|
|
905
|
|
|
if( in_array('featured_image',$field_group['hide_on_screen']) ) |
906
|
|
|
{ |
907
|
|
|
$e .= '#postimagediv, #screen-meta label[for=postimagediv-hide] {display: none;} '; |
908
|
|
|
} |
909
|
|
|
|
910
|
|
|
if( in_array('revisions',$field_group['hide_on_screen']) ) |
911
|
|
|
{ |
912
|
|
|
$e .= '#revisionsdiv, #screen-meta label[for=revisionsdiv-hide] {display: none;} '; |
913
|
|
|
} |
914
|
|
|
|
915
|
|
|
if( in_array('categories',$field_group['hide_on_screen']) ) |
916
|
|
|
{ |
917
|
|
|
$e .= '#categorydiv, #screen-meta label[for=categorydiv-hide] {display: none;} '; |
918
|
|
|
} |
919
|
|
|
|
920
|
|
|
if( in_array('tags',$field_group['hide_on_screen']) ) |
921
|
|
|
{ |
922
|
|
|
$e .= '#tagsdiv-post_tag, #screen-meta label[for=tagsdiv-post_tag-hide] {display: none;} '; |
923
|
|
|
} |
924
|
|
|
|
925
|
|
|
if( in_array('send-trackbacks',$field_group['hide_on_screen']) ) |
926
|
|
|
{ |
927
|
|
|
$e .= '#trackbacksdiv, #screen-meta label[for=trackbacksdiv-hide] {display: none;} '; |
928
|
|
|
} |
929
|
|
|
|
930
|
|
|
|
931
|
|
|
// return |
932
|
|
|
return apply_filters('acf/get_field_group_style', $e, $field_group); |
933
|
|
|
} |
934
|
|
|
|
935
|
|
|
|
936
|
|
|
/* |
937
|
|
|
* acf_import_field_group |
938
|
|
|
* |
939
|
|
|
* This function will import a field group from JSON into the DB |
940
|
|
|
* |
941
|
|
|
* @type function |
942
|
|
|
* @date 10/12/2014 |
943
|
|
|
* @since 5.1.5 |
944
|
|
|
* |
945
|
|
|
* @param $field_group (array) |
946
|
|
|
* @return $id (int) |
947
|
|
|
*/ |
|
|
|
|
948
|
|
|
|
949
|
|
|
function acf_import_field_group( $field_group ) { |
950
|
|
|
|
951
|
|
|
// vars |
952
|
|
|
$ref = array(); |
953
|
|
|
$order = array(); |
954
|
|
|
|
955
|
|
|
|
956
|
|
|
// extract fields |
957
|
|
|
$fields = acf_extract_var($field_group, 'fields'); |
958
|
|
|
|
959
|
|
|
|
960
|
|
|
// format fields |
961
|
|
|
$fields = acf_prepare_fields_for_import( $fields ); |
962
|
|
|
|
963
|
|
|
|
964
|
|
|
// remove old fields |
965
|
|
|
if( $field_group['ID'] ) { |
966
|
|
|
|
967
|
|
|
// disable local - important as to avoid 'acf_get_fields_by_id' returning fields with ID = 0 |
968
|
|
|
acf_disable_local(); |
969
|
|
|
|
970
|
|
|
|
971
|
|
|
// load fields |
972
|
|
|
$db_fields = acf_get_fields_by_id( $field_group['ID'] ); |
973
|
|
|
$db_fields = acf_prepare_fields_for_import( $db_fields ); |
974
|
|
|
|
975
|
|
|
|
976
|
|
|
// get field keys |
977
|
|
|
$keys = array(); |
978
|
|
|
foreach( $fields as $field ) { |
979
|
|
|
|
980
|
|
|
$keys[] = $field['key']; |
981
|
|
|
|
982
|
|
|
} |
983
|
|
|
|
984
|
|
|
|
985
|
|
|
// loop over db fields |
986
|
|
|
foreach( $db_fields as $field ) { |
987
|
|
|
|
988
|
|
|
// add to ref |
989
|
|
|
$ref[ $field['key'] ] = $field['ID']; |
990
|
|
|
|
991
|
|
|
|
992
|
|
|
if( !in_array($field['key'], $keys) ) { |
993
|
|
|
|
994
|
|
|
acf_delete_field( $field['ID'] ); |
995
|
|
|
|
996
|
|
|
} |
997
|
|
|
|
998
|
|
|
} |
999
|
|
|
|
1000
|
|
|
|
1001
|
|
|
// enable local - important as to allow local to find new fields and save json file |
1002
|
|
|
acf_enable_local(); |
1003
|
|
|
|
1004
|
|
|
} |
1005
|
|
|
|
1006
|
|
|
|
1007
|
|
|
// save field group |
1008
|
|
|
$field_group = acf_update_field_group( $field_group ); |
1009
|
|
|
|
1010
|
|
|
|
1011
|
|
|
// add to ref |
1012
|
|
|
$ref[ $field_group['key'] ] = $field_group['ID']; |
1013
|
|
|
|
1014
|
|
|
|
1015
|
|
|
// add to order |
1016
|
|
|
$order[ $field_group['ID'] ] = 0; |
1017
|
|
|
|
1018
|
|
|
|
1019
|
|
|
// add fields |
1020
|
|
|
foreach( $fields as $field ) { |
1021
|
|
|
|
1022
|
|
|
// add ID |
1023
|
|
|
if( !$field['ID'] && isset($ref[ $field['key'] ]) ) { |
1024
|
|
|
|
1025
|
|
|
$field['ID'] = $ref[ $field['key'] ]; |
1026
|
|
|
|
1027
|
|
|
} |
1028
|
|
|
|
1029
|
|
|
|
1030
|
|
|
// add parent |
1031
|
|
View Code Duplication |
if( empty($field['parent']) ) { |
|
|
|
|
1032
|
|
|
|
1033
|
|
|
$field['parent'] = $field_group['ID']; |
1034
|
|
|
|
1035
|
|
|
} elseif( isset($ref[ $field['parent'] ]) ) { |
1036
|
|
|
|
1037
|
|
|
$field['parent'] = $ref[ $field['parent'] ]; |
1038
|
|
|
|
1039
|
|
|
} |
1040
|
|
|
|
1041
|
|
|
|
1042
|
|
|
// add field menu_order |
1043
|
|
|
if( !isset($order[ $field['parent'] ]) ) { |
1044
|
|
|
|
1045
|
|
|
$order[ $field['parent'] ] = 0; |
1046
|
|
|
|
1047
|
|
|
} |
1048
|
|
|
|
1049
|
|
|
$field['menu_order'] = $order[ $field['parent'] ]; |
1050
|
|
|
$order[ $field['parent'] ]++; |
1051
|
|
|
|
1052
|
|
|
|
1053
|
|
|
// save field |
1054
|
|
|
$field = acf_update_field( $field ); |
|
|
|
|
1055
|
|
|
|
1056
|
|
|
|
1057
|
|
|
// add to ref |
1058
|
|
|
$ref[ $field['key'] ] = $field['ID']; |
1059
|
|
|
|
1060
|
|
|
} |
1061
|
|
|
|
1062
|
|
|
|
1063
|
|
|
// return new field group |
1064
|
|
|
return $field_group; |
1065
|
|
|
|
1066
|
|
|
} |
1067
|
|
|
|
1068
|
|
|
|
1069
|
|
|
/* |
1070
|
|
|
* acf_prepare_field_group_for_export |
1071
|
|
|
* |
1072
|
|
|
* description |
1073
|
|
|
* |
1074
|
|
|
* @type function |
1075
|
|
|
* @date 4/12/2015 |
1076
|
|
|
* @since 5.3.2 |
1077
|
|
|
* |
1078
|
|
|
* @param $post_id (int) |
1079
|
|
|
* @return $post_id (int) |
1080
|
|
|
*/ |
|
|
|
|
1081
|
|
|
|
1082
|
|
|
function acf_prepare_field_group_for_export( $field_group ) { |
1083
|
|
|
|
1084
|
|
|
// extract field group ID |
1085
|
|
|
$id = acf_extract_var( $field_group, 'ID' ); |
|
|
|
|
1086
|
|
|
|
1087
|
|
|
|
1088
|
|
|
// prepare fields |
1089
|
|
|
$field_group['fields'] = acf_prepare_fields_for_export( $field_group['fields'] ); |
1090
|
|
|
|
1091
|
|
|
|
1092
|
|
|
// filter for 3rd party customization |
1093
|
|
|
$field_group = apply_filters('acf/prepare_field_group_for_export', $field_group); |
1094
|
|
|
|
1095
|
|
|
|
1096
|
|
|
// return |
1097
|
|
|
return $field_group; |
1098
|
|
|
} |
1099
|
|
|
|
1100
|
|
|
|
1101
|
|
|
?> |
|
|
|
|
1102
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.