acf-field-functions.php
37.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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
<?php
// Register store.
acf_register_store( 'fields' )->prop( 'multisite', true );
/**
* acf_get_field
*
* Retrieves a field for the given identifier.
*
* @date 17/1/19
* @since 5.7.10
*
* @param (int|string) $id The field ID, key or name.
* @return (array|false) The field array.
*/
function acf_get_field( $id = 0 ) {
// Allow WP_Post to be passed.
if ( is_object( $id ) ) {
$id = $id->ID;
}
// Check store.
$store = acf_get_store( 'fields' );
if ( $store->has( $id ) ) {
return $store->get( $id );
}
// Check local fields first.
if ( acf_is_local_field( $id ) ) {
$field = acf_get_local_field( $id );
// Then check database.
} else {
$field = acf_get_raw_field( $id );
}
// Bail early if no field.
if ( ! $field ) {
return false;
}
// Validate field.
$field = acf_validate_field( $field );
// Set input prefix.
$field['prefix'] = 'acf';
/**
* Filters the $field array after it has been loaded.
*
* @date 12/02/2014
* @since 5.0.0
*
* @param array The field array.
*/
$field = apply_filters( 'acf/load_field', $field );
// Store field using aliasses to also find via key, ID and name.
$store->set( $field['key'], $field );
$store->alias( $field['key'], $field['ID'], $field['name'] );
// Return.
return $field;
}
// Register variation.
acf_add_filter_variations( 'acf/load_field', array( 'type', 'name', 'key' ), 0 );
/**
* acf_get_raw_field
*
* Retrieves raw field data for the given identifier.
*
* @date 18/1/19
* @since 5.7.10
*
* @param (int|string) $id The field ID, key or name.
* @return (array|false) The field array.
*/
function acf_get_raw_field( $id = 0 ) {
// Get raw field from database.
$post = acf_get_field_post( $id );
if ( ! $post ) {
return false;
}
// Bail early if incorrect post type.
if ( $post->post_type !== 'acf-field' ) {
return false;
}
// Unserialize post_content.
$field = (array) maybe_unserialize( $post->post_content );
// update attributes
$field['ID'] = $post->ID;
$field['key'] = $post->post_name;
$field['label'] = $post->post_title;
$field['name'] = $post->post_excerpt;
$field['menu_order'] = $post->menu_order;
$field['parent'] = $post->post_parent;
// Return field.
return $field;
}
/**
* acf_get_field_post
*
* Retrieves the field's WP_Post object.
*
* @date 18/1/19
* @since 5.7.10
*
* @param (int|string) $id The field ID, key or name.
* @return (array|false) The field array.
*/
function acf_get_field_post( $id = 0 ) {
// Get post if numeric.
if ( is_numeric( $id ) ) {
return get_post( $id );
// Search posts if is string.
} elseif ( is_string( $id ) ) {
// Determine id type.
$type = acf_is_field_key( $id ) ? 'key' : 'name';
// Try cache.
$cache_key = acf_cache_key( "acf_get_field_post:$type:$id" );
$post_id = wp_cache_get( $cache_key, 'acf' );
if ( $post_id === false ) {
// Query posts.
$posts = get_posts(
array(
'posts_per_page' => 1,
'post_type' => 'acf-field',
'orderby' => 'menu_order title',
'order' => 'ASC',
'suppress_filters' => false,
'cache_results' => true,
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
"acf_field_$type" => $id,
)
);
// Update $post_id with a non false value.
$post_id = $posts ? $posts[0]->ID : 0;
// Update cache.
wp_cache_set( $cache_key, $post_id, 'acf' );
}
// Check $post_id and return the post when possible.
if ( $post_id ) {
return get_post( $post_id );
}
}
// Return false by default.
return false;
}
/**
* acf_is_field_key
*
* Returns true if the given identifier is a field key.
*
* @date 6/12/2013
* @since 5.0.0
*
* @param string $id The identifier.
* @return bool
*/
function acf_is_field_key( $id = '' ) {
// Check if $id is a string starting with "field_".
if ( is_string( $id ) && substr( $id, 0, 6 ) === 'field_' ) {
return true;
}
/**
* Filters whether the $id is a field key.
*
* @date 23/1/19
* @since 5.7.10
*
* @param bool $bool The result.
* @param string $id The identifier.
*/
return apply_filters( 'acf/is_field_key', false, $id );
}
/**
* acf_validate_field
*
* Ensures the given field valid.
*
* @date 18/1/19
* @since 5.7.10
*
* @param array $field The field array.
* @return array
*/
function acf_validate_field( $field = array() ) {
// Bail early if already valid.
if ( is_array( $field ) && ! empty( $field['_valid'] ) ) {
return $field;
}
// Apply defaults.
$field = wp_parse_args(
$field,
array(
'ID' => 0,
'key' => '',
'label' => '',
'name' => '',
'aria-label' => '',
'prefix' => '',
'type' => 'text',
'value' => null,
'menu_order' => 0,
'instructions' => '',
'required' => false,
'id' => '',
'class' => '',
'conditional_logic' => false,
'parent' => 0,
'wrapper' => array(),
// 'attributes' => array()
)
);
// Convert types.
$field['ID'] = (int) $field['ID'];
$field['menu_order'] = (int) $field['menu_order'];
// Add backwards compatibility for wrapper attributes.
// Todo: Remove need for this.
$field['wrapper'] = wp_parse_args(
$field['wrapper'],
array(
'width' => '',
'class' => '',
'id' => '',
)
);
// Store backups.
$field['_name'] = $field['name'];
$field['_valid'] = 1;
/**
* Filters the $field array to validate settings.
*
* @date 12/02/2014
* @since 5.0.0
*
* @param array $field The field array.
*/
$field = apply_filters( 'acf/validate_field', $field );
// return
return $field;
}
// Register variation.
acf_add_filter_variations( 'acf/validate_field', array( 'type' ), 0 );
/**
* acf_get_valid_field
*
* Ensures the given field valid.
*
* @date 28/09/13
* @since 5.0.0
*
* @param array $field The field array.
* @return array
*/
function acf_get_valid_field( $field = false ) {
return acf_validate_field( $field );
}
/**
* acf_translate_field
*
* Translates a field's settings.
*
* @date 8/03/2016
* @since 5.3.2
*
* @param array $field The field array.
* @return array
*/
function acf_translate_field( $field = array() ) {
// Get settings.
$l10n = acf_get_setting( 'l10n' );
$l10n_textdomain = acf_get_setting( 'l10n_textdomain' );
// Translate field settings if textdomain is set.
if ( $l10n && $l10n_textdomain ) {
$field['label'] = acf_translate( $field['label'] );
$field['instructions'] = acf_translate( $field['instructions'] );
/**
* Filters the $field array to translate strings.
*
* @date 12/02/2014
* @since 5.0.0
*
* @param array $field The field array.
*/
$field = apply_filters( 'acf/translate_field', $field );
}
// Return field.
return $field;
}
// Register variation.
acf_add_filter_variations( 'acf/translate_field', array( 'type' ), 0 );
// Translate fields passing through validation.
add_action( 'acf/validate_field', 'acf_translate_field' );
/**
* acf_get_fields
*
* Returns and array of fields for the given $parent.
*
* @date 30/09/13
* @since 5.0.0
*
* @param (int|string|array) $parent The field group or field settings. Also accepts the field group ID or key.
* @return array
*/
function acf_get_fields( $parent ) {
// Allow field group selector as $parent.
if ( ! is_array( $parent ) ) {
$parent = acf_get_field_group( $parent );
if ( ! $parent ) {
return array();
}
}
// Vars.
$fields = array();
// Check local fields first.
if ( acf_have_local_fields( $parent['key'] ) ) {
$raw_fields = acf_get_local_fields( $parent['key'] );
foreach ( $raw_fields as $raw_field ) {
$fields[] = acf_get_field( $raw_field['key'] );
}
// Then check database.
} else {
$raw_fields = acf_get_raw_fields( $parent['ID'] );
foreach ( $raw_fields as $raw_field ) {
$fields[] = acf_get_field( $raw_field['ID'] );
}
}
/**
* Filters the $fields array.
*
* @date 12/02/2014
* @since 5.0.0
*
* @param array $fields The array of fields.
*/
$fields = apply_filters( 'acf/load_fields', $fields, $parent );
// Return fields
return $fields;
}
/**
* acf_get_raw_fields
*
* Returns and array of raw field data for the given parent id.
*
* @date 18/1/19
* @since 5.7.10
*
* @param int $id The field group or field id.
* @return array
*/
function acf_get_raw_fields( $id = 0 ) {
// Try cache.
$cache_key = acf_cache_key( "acf_get_field_posts:$id" );
$post_ids = wp_cache_get( $cache_key, 'acf' );
if ( $post_ids === false ) {
// Query posts.
$posts = get_posts(
array(
'posts_per_page' => -1,
'post_type' => 'acf-field',
'orderby' => 'menu_order',
'order' => 'ASC',
'suppress_filters' => true, // DO NOT allow WPML to modify the query
'cache_results' => true,
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
'post_parent' => $id,
'post_status' => array( 'publish', 'trash' ),
)
);
// Update $post_ids with a non false value.
$post_ids = array();
foreach ( $posts as $post ) {
$post_ids[] = $post->ID;
}
// Update cache.
wp_cache_set( $cache_key, $post_ids, 'acf' );
}
// Loop over ids and populate array of fields.
$fields = array();
foreach ( $post_ids as $post_id ) {
$fields[] = acf_get_raw_field( $post_id );
}
// Return fields.
return $fields;
}
/**
* acf_get_field_count
*
* Return the number of fields for the given field group.
*
* @date 17/10/13
* @since 5.0.0
*
* @param array $parent The field group or field array.
* @return int
*/
function acf_get_field_count( $parent ) {
// Check local fields first.
if ( acf_have_local_fields( $parent['key'] ) ) {
$raw_fields = acf_get_local_fields( $parent['key'] );
// Then check database.
} else {
$raw_fields = acf_get_raw_fields( $parent['ID'] );
}
/**
* Filters the counted number of fields.
*
* @date 12/02/2014
* @since 5.0.0
*
* @param int $count The number of fields.
* @param array $parent The field group or field array.
*/
return apply_filters( 'acf/get_field_count', count( $raw_fields ), $parent );
}
/**
* acf_clone_field
*
* Allows customization to a field when it is cloned. Used by the clone field.
*
* @date 8/03/2016
* @since 5.3.2
*
* @param array $field The field being cloned.
* @param array $clone_field The clone field.
* @return array
*/
function acf_clone_field( $field, $clone_field ) {
// Add reference to the clone field.
$field['_clone'] = $clone_field['key'];
/**
* Filters the $field array when it is being cloned.
*
* @date 12/02/2014
* @since 5.0.0
*
* @param array $field The field array.
* @param array $clone_field The clone field array.
*/
$field = apply_filters( 'acf/clone_field', $field, $clone_field );
// Return field.
return $field;
}
// Register variation.
acf_add_filter_variations( 'acf/clone_field', array( 'type' ), 0 );
/**
* acf_prepare_field
*
* Prepare a field for input.
*
* @date 20/1/19
* @since 5.7.10
*
* @param array $field The field array.
* @return array
*/
function acf_prepare_field( $field ) {
// Bail early if already prepared.
if ( ! empty( $field['_prepare'] ) ) {
return $field;
}
// Use field key to override input name.
if ( $field['key'] ) {
$field['name'] = $field['key'];
}
// Use field prefix to modify input name.
if ( ! empty( $field['prefix'] ) ) {
$field['name'] = "{$field['prefix']}[{$field['name']}]";
}
// Generate id attribute from name.
$field['id'] = acf_idify( $field['name'] );
// Add state to field.
$field['_prepare'] = true;
/**
* Filters the $field array.
*
* Allows developers to modify field settings or return false to remove field.
*
* @date 12/02/2014
* @since 5.0.0
*
* @param array $field The field array.
*/
$field = apply_filters( 'acf/prepare_field', $field );
// return
return $field;
}
// Register variation.
acf_add_filter_variations( 'acf/prepare_field', array( 'type', 'name', 'key' ), 0 );
/**
* acf_render_fields
*
* Renders an array of fields. Also loads the field's value.
*
* @date 8/10/13
* @since 5.0.0
* @since 5.6.9 Changed parameter order.
*
* @param array $fields An array of fields.
* @param (int|string) $post_id The post ID to load values from.
* @param string $element The wrapping element type.
* @param string $instruction The instruction render position (label|field).
* @return void
*/
function acf_render_fields( $fields, $post_id = 0, $el = 'div', $instruction = 'label' ) {
// Parameter order changed in ACF 5.6.9.
if ( is_array( $post_id ) ) {
$args = func_get_args();
$fields = $args[1];
$post_id = $args[0];
}
/**
* Filters the $fields array before they are rendered.
*
* @date 12/02/2014
* @since 5.0.0
*
* @param array $fields An array of fields.
* @param (int|string) $post_id The post ID to load values from.
*/
$fields = apply_filters( 'acf/pre_render_fields', $fields, $post_id );
// Filter our false results.
$fields = array_filter( $fields );
// Loop over and render fields.
if ( $fields ) {
foreach ( $fields as $field ) {
$field = apply_filters( 'acf/pre_render_field', $field, $post_id );
// Load value if not already loaded.
if ( $field['value'] === null ) {
$field['value'] = acf_get_value( $post_id, $field );
}
// Render wrap.
acf_render_field_wrap( $field, $el, $instruction );
}
}
/**
* Fires after fields have been rendered.
*
* @date 12/02/2014
* @since 5.0.0
*
* @param array $fields An array of fields.
* @param (int|string) $post_id The post ID to load values from.
*/
do_action( 'acf/render_fields', $fields, $post_id );
}
/**
* Render the wrapping element for a given field.
*
* @since 5.0.0
*
* @param array $field The field array.
* @param string $element The wrapping element type.
* @param string $instruction The instruction render position (label|field).
* @param bool $field_setting If a field setting is being rendered.
* @return void
*/
function acf_render_field_wrap( $field, $element = 'div', $instruction = 'label', $field_setting = false ) {
// Ensure field is complete (adds all settings).
$field = acf_validate_field( $field );
// Prepare field for input (modifies settings).
$field = acf_prepare_field( $field );
// Allow filters to cancel render.
if ( ! $field ) {
return;
}
// Determine wrapping element.
$elements = array(
'div' => 'div',
'tr' => 'td',
'td' => 'div',
'ul' => 'li',
'ol' => 'li',
'dl' => 'dt',
);
if ( isset( $elements[ $element ] ) ) {
$inner_element = $elements[ $element ];
} else {
$element = $inner_element = 'div';
}
// Generate wrapper attributes.
$wrapper = array(
'id' => '',
'class' => 'acf-field',
'width' => '',
'style' => '',
'data-name' => $field['_name'],
'data-type' => $field['type'],
'data-key' => $field['key'],
);
// Add field type attributes.
$wrapper['class'] .= " acf-field-{$field['type']}";
// add field key attributes
if ( $field['key'] ) {
$wrapper['class'] .= " acf-field-{$field['key']}";
}
// Add required attributes.
// Todo: Remove data-required
if ( $field['required'] ) {
$wrapper['class'] .= ' is-required';
$wrapper['data-required'] = 1;
}
// Clean up class attribute.
$wrapper['class'] = str_replace( '_', '-', $wrapper['class'] );
$wrapper['class'] = str_replace( 'field-field-', 'field-', $wrapper['class'] );
// Merge in field 'wrapper' setting without destroying class and style.
if ( $field['wrapper'] ) {
$wrapper = acf_merge_attributes( $wrapper, $field['wrapper'] );
}
// Extract wrapper width and generate style.
// Todo: Move from $wrapper out into $field.
$width = acf_extract_var( $wrapper, 'width' );
if ( $width ) {
$width = acf_numval( $width );
if ( $element !== 'tr' && $element !== 'td' ) {
$wrapper['data-width'] = $width;
$wrapper['style'] .= " width:{$width}%;";
}
}
// Clean up all attributes.
$wrapper = array_map( 'trim', $wrapper );
$wrapper = array_filter( $wrapper );
/**
* Filters the $wrapper array before rendering.
*
* @date 21/1/19
* @since 5.7.10
*
* @param array $wrapper The wrapper attributes array.
* @param array $field The field array.
*/
$wrapper = apply_filters( 'acf/field_wrapper_attributes', $wrapper, $field );
// Append conditional logic attributes.
if ( ! empty( $field['conditional_logic'] ) ) {
$wrapper['data-conditions'] = $field['conditional_logic'];
}
if ( ! empty( $field['conditions'] ) ) {
$wrapper['data-conditions'] = $field['conditions'];
}
// Vars for render.
$attributes_html = acf_esc_attr( $wrapper );
// Render HTML
echo "<$element $attributes_html>" . "\n";
if ( $element !== 'td' ) {
echo "<$inner_element class=\"acf-label\">" . "\n";
acf_render_field_label( $field );
if ( $instruction == 'label' ) {
acf_render_field_instructions( $field, $field_setting );
}
echo "</$inner_element>" . "\n";
}
echo "<$inner_element class=\"acf-input\">" . "\n";
acf_render_field( $field );
if ( ! $field_setting && $instruction == 'field' ) {
acf_render_field_instructions( $field );
}
echo "</$inner_element>" . "\n";
if ( $field_setting && $instruction == 'field' ) {
acf_render_field_instructions( $field );
}
echo "</$element>" . "\n";
}
/**
* acf_render_field
*
* Render the input element for a given field.
*
* @date 21/1/19
* @since 5.7.10
*
* @param array $field The field array.
* @return void
*/
function acf_render_field( $field ) {
// Ensure field is complete (adds all settings).
$field = acf_validate_field( $field );
// Prepare field for input (modifies settings).
$field = acf_prepare_field( $field );
// Allow filters to cancel render.
if ( ! $field ) {
return;
}
/**
* Fires when rendering a field.
*
* @date 12/02/2014
* @since 5.0.0
*
* @param array $field The field array.
*/
do_action( 'acf/render_field', $field );
}
// Register variation.
acf_add_action_variations( 'acf/render_field', array( 'type', 'name', 'key' ), 0 );
/**
* acf_render_field_label
*
* Renders the field's label.
*
* @date 19/9/17
* @since 5.6.3
*
* @param array $field The field array.
* @return void
*/
function acf_render_field_label( $field ) {
// Get label.
$label = acf_get_field_label( $field );
// Output label.
if ( $label ) {
echo '<label' . ( $field['id'] ? ' for="' . esc_attr( $field['id'] ) . '"' : '' ) . '>' . acf_esc_html( $label ) . '</label>';
}
}
/**
* acf_get_field_label
*
* Returns the field's label with appropriate required label.
*
* @date 4/11/2013
* @since 5.0.0
*
* @param array $field The field array.
* @param string $context The output context (admin).
* @return void
*/
function acf_get_field_label( $field, $context = '' ) {
// Get label.
$label = $field['label'];
// Display empty text when editing field.
if ( $context == 'admin' && $label === '' ) {
$label = __( '(no label)', 'acf' );
}
// Add required HTML.
if ( $field['required'] ) {
$label .= ' <span class="acf-required">*</span>';
}
/**
* Filters the field's label HTML.
*
* @date 21/1/19
* @since 5.7.10
*
* @param string The label HTML.
* @param array $field The field array.
* @param string $context The output context (admin).
*/
$label = apply_filters( 'acf/get_field_label', $label, $field, $context );
// Return label.
return $label;
}
/**
* Renders the field's instructions.
*
* @since 5.6.3
*
* @param array $field The field array.
* @param boolean $tooltip If the instructions are being rendered as a tooltip.
* @return void
*/
function acf_render_field_instructions( $field, $tooltip = false ) {
if ( $field['instructions'] ) {
$instructions = acf_esc_html( $field['instructions'] );
if ( $tooltip ) {
printf( '<div class="acf-tip"><i tabindex="0" class="acf-icon acf-icon-help acf-js-tooltip" title="%s">?</i></div>', $instructions );
} else {
printf( '<p class="description">%s</p>', $instructions );
}
} elseif ( ! empty( $field['hint'] ) ) {
$instructions = acf_esc_html( $field['hint'] );
printf( '<p class="description">%s</p>', $instructions );
}
}
/**
* acf_render_field_setting
*
* Renders a field setting used in the admin edit screen.
*
* @date 21/1/19
* @since 5.7.10
*
* @param array $field The field array.
* @param array $setting The settings field array.
* @param bool $global Whether this setting is a global or field type specific one.
* @return void
*/
function acf_render_field_setting( $field, $setting, $global = false ) {
// Validate field.
$setting = acf_validate_field( $setting );
// Add custom attributes to setting wrapper.
$setting['wrapper']['data-key'] = $setting['name'];
$setting['wrapper']['class'] .= ' acf-field-setting-' . $setting['name'];
if ( ! $global ) {
$setting['wrapper']['data-setting'] = $field['type'];
}
// Add classes for appended and prepended fields.
if ( ! empty( $setting['append'] ) ) {
$setting['wrapper']['class'] .= ' acf-field-appended';
}
if ( ! empty( $setting['prepend'] ) ) {
$setting['wrapper']['class'] .= ' acf-field-prepended';
}
// Copy across prefix.
$setting['prefix'] = $field['prefix'];
// Find setting value from field.
if ( $setting['value'] === null ) {
// Name.
if ( isset( $field[ $setting['name'] ] ) ) {
$setting['value'] = $field[ $setting['name'] ];
// Default value.
} elseif ( isset( $setting['default_value'] ) ) {
$setting['value'] = $setting['default_value'];
}
}
// Add append attribute used by JS to join settings.
if ( isset( $setting['_append'] ) ) {
$setting['wrapper']['data-append'] = $setting['_append'];
}
// If we're using a hint, set the label location as field so it appears after.
$label_location = ! empty( $setting['instructions'] ) ? 'field' : 'label';
// If we're a true false field, force label location to label.
if ( $setting['type'] === 'true_false' ) {
$label_location = 'label';
}
// Render setting.
acf_render_field_wrap( $setting, 'div', $label_location );
}
/**
* acf_update_field
*
* Updates a field in the database.
*
* @date 21/1/19
* @since 5.7.10
*
* @param array $field The field array.
* @param array $specific An array of specific field attributes to update.
* @return array
*/
function acf_update_field( $field, $specific = array() ) {
// Validate field.
$field = acf_validate_field( $field );
// May have been posted. Remove slashes.
$field = wp_unslash( $field );
// Parse types (converts string '0' to int 0).
$field = acf_parse_types( $field );
// Clean up conditional logic keys.
if ( $field['conditional_logic'] ) {
// Remove empty values and convert to associated array.
$field['conditional_logic'] = array_filter( $field['conditional_logic'] );
$field['conditional_logic'] = array_values( $field['conditional_logic'] );
$field['conditional_logic'] = array_map( 'array_filter', $field['conditional_logic'] );
$field['conditional_logic'] = array_map( 'array_values', $field['conditional_logic'] );
}
// Parent may be provided as a field key.
if ( $field['parent'] && ! is_numeric( $field['parent'] ) ) {
$parent = acf_get_field_post( $field['parent'] );
$field['parent'] = $parent ? $parent->ID : 0;
}
/**
* Filters the $field array before it is updated.
*
* @date 12/02/2014
* @since 5.0.0
*
* @param array $field The field array.
*/
$field = apply_filters( 'acf/update_field', $field );
// Make a backup of field data and remove some args.
$_field = $field;
acf_extract_vars( $_field, array( 'ID', 'key', 'label', 'name', 'prefix', 'value', 'menu_order', 'id', 'class', 'parent', '_name', '_prepare', '_valid' ) );
// Create array of data to save.
$save = array(
'ID' => $field['ID'],
'post_status' => 'publish',
'post_type' => 'acf-field',
'post_title' => $field['label'],
'post_name' => $field['key'],
'post_excerpt' => $field['name'],
'post_content' => maybe_serialize( $_field ),
'post_parent' => $field['parent'],
'menu_order' => $field['menu_order'],
);
// Reduce save data if specific key list is provided.
if ( $specific ) {
$specific[] = 'ID';
$save = acf_get_sub_array( $save, $specific );
}
// Unhook wp_targeted_link_rel() filter from WP 5.1 corrupting serialized data.
remove_filter( 'content_save_pre', 'wp_targeted_link_rel' );
// Slash data.
// WP expects all data to be slashed and will unslash it (fixes '\' character issues).
$save = wp_slash( $save );
// Update or Insert.
if ( $field['ID'] ) {
wp_update_post( $save );
} else {
$field['ID'] = wp_insert_post( $save );
}
// Flush field cache.
acf_flush_field_cache( $field );
/**
* Fires after a field has been updated, and the field cache has been cleaned.
*
* @date 24/1/19
* @since 5.7.10
*
* @param array $field The field array.
*/
do_action( 'acf/updated_field', $field );
// Return field.
return $field;
}
// Register variation.
acf_add_filter_variations( 'acf/update_field', array( 'type', 'name', 'key' ), 0 );
/**
* _acf_apply_unique_field_slug
*
* Allows full control over 'acf-field' slugs.
*
* @date 21/1/19
* @since 5.7.10
*
* @param string $slug The post slug.
* @param int $post_ID Post ID.
* @param string $post_status The post status.
* @param string $post_type Post type.
* @param int $post_parent Post parent ID
* @param string $original_slug The original post slug.
*/
function _acf_apply_unique_field_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' ) {
return $original_slug;
}
// Return slug.
return $slug;
}
// Hook into filter.
add_filter( 'wp_unique_post_slug', '_acf_apply_unique_field_slug', 999, 6 );
/**
* acf_flush_field_cache
*
* Deletes all caches for this field.
*
* @date 22/1/19
* @since 5.7.10
*
* @param array $field The field array.
* @return void
*/
function acf_flush_field_cache( $field ) {
// Delete stored data.
acf_get_store( 'fields' )->remove( $field['key'] );
// Flush cached post_id for this field's name and key.
wp_cache_delete( acf_cache_key( "acf_get_field_post:name:{$field['name']}" ), 'acf' );
wp_cache_delete( acf_cache_key( "acf_get_field_post:key:{$field['key']}" ), 'acf' );
// Flush cached array of post_ids for this field's parent.
wp_cache_delete( acf_cache_key( "acf_get_field_posts:{$field['parent']}" ), 'acf' );
}
/**
* acf_delete_field
*
* Deletes a field from the database.
*
* @date 21/1/19
* @since 5.7.10
*
* @param (int|string) $id The field ID, key or name.
* @return bool True if field was deleted.
*/
function acf_delete_field( $id = 0 ) {
// Get the field.
$field = acf_get_field( $id );
// Bail early if field was not found.
if ( ! $field || ! $field['ID'] ) {
return false;
}
// Delete post.
wp_delete_post( $field['ID'], true );
// Flush field cache.
acf_flush_field_cache( $field );
/**
* Fires immediately after a field has been deleted.
*
* @date 12/02/2014
* @since 5.0.0
*
* @param array $field The field array.
*/
do_action( 'acf/delete_field', $field );
// Return true.
return true;
}
// Register variation.
acf_add_action_variations( 'acf/delete_field', array( 'type', 'name', 'key' ), 0 );
/**
* acf_trash_field
*
* Trashes a field from the database.
*
* @date 2/10/13
* @since 5.0.0
*
* @param (int|string) $id The field ID, key or name.
* @return bool True if field was trashed.
*/
function acf_trash_field( $id = 0 ) {
// Get the field.
$field = acf_get_field( $id );
// Bail early if field was not found.
if ( ! $field || ! $field['ID'] ) {
return false;
}
// Trash post.
wp_trash_post( $field['ID'], true );
/**
* Fires immediately after a field has been trashed.
*
* @date 12/02/2014
* @since 5.0.0
*
* @param array $field The field array.
*/
do_action( 'acf/trash_field', $field );
// Return true.
return true;
}
/**
* acf_untrash_field
*
* Restores a field from the trash.
*
* @date 2/10/13
* @since 5.0.0
*
* @param (int|string) $id The field ID, key or name.
* @return bool True if field was trashed.
*/
function acf_untrash_field( $id = 0 ) {
// Get the field.
$field = acf_get_field( $id );
// Bail early if field was not found.
if ( ! $field || ! $field['ID'] ) {
return false;
}
// Untrash post.
wp_untrash_post( $field['ID'], true );
// Flush field cache.
acf_flush_field_cache( $field );
/**
* Fires immediately after a field has been trashed.
*
* @date 12/02/2014
* @since 5.0.0
*
* @param array $field The field array.
*/
do_action( 'acf/untrash_field', $field );
// Return true.
return true;
}
/**
* Filter callback which returns the previous post_status instead of "draft" for the "acf-field" post type.
*
* Prior to WordPress 5.6.0, this filter was not needed as restored posts were always assigned their original status.
*
* @since 5.9.5
*
* @param string $new_status The new status of the post being restored.
* @param int $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_post_status( $new_status, $post_id, $previous_status ) {
return ( get_post_type( $post_id ) === 'acf-field' ) ? $previous_status : $new_status;
}
add_action( 'wp_untrash_post_status', '_acf_untrash_field_post_status', 10, 3 );
/**
* acf_prefix_fields
*
* Changes the prefix for an array of fields by reference.
*
* @date 5/9/17
* @since 5.6.0
*
* @param array $fields An array of fields.
* @param string $prefix The new prefix.
* @return void
*/
function acf_prefix_fields( &$fields, $prefix = 'acf' ) {
// Loopover fields.
foreach ( $fields as &$field ) {
// Replace 'acf' with $prefix.
$field['prefix'] = $prefix . substr( $field['prefix'], 3 );
}
}
/**
* acf_get_sub_field
*
* Searches a field for sub fields matching the given selector.
*
* @date 21/1/19
* @since 5.7.10
*
* @param (int|string) $id The field ID, key or name.
* @param array $field The parent field array.
* @return (array|false)
*/
function acf_get_sub_field( $id, $field ) {
// Vars.
$sub_field = false;
// Search sub fields.
if ( isset( $field['sub_fields'] ) ) {
$sub_field = acf_search_fields( $id, $field['sub_fields'] );
}
/**
* Filters the $sub_field found.
*
* @date 12/02/2014
* @since 5.0.0
*
* @param array $sub_field The found sub field array.
* @param string $selector The selector used to search.
* @param array $field The parent field array.
*/
$sub_field = apply_filters( 'acf/get_sub_field', $sub_field, $id, $field );
// return
return $sub_field;
}
// Register variation.
acf_add_filter_variations( 'acf/get_sub_field', array( 'type' ), 2 );
/**
* acf_search_fields
*
* Searches an array of fields for one that matches the given identifier.
*
* @date 12/2/19
* @since 5.7.11
*
* @param (int|string) $id The field ID, key or name.
* @param array $haystack The array of fields.
* @return (int|false)
*/
function acf_search_fields( $id, $fields ) {
// Loop over searchable keys in order of priority.
// Important to search "name" on all fields before "_name" backup.
foreach ( array( 'key', 'name', '_name', '__name' ) as $key ) {
// Loop over fields and compare.
foreach ( $fields as $field ) {
if ( isset( $field[ $key ] ) && $field[ $key ] === $id ) {
return $field;
}
}
}
// Return not found.
return false;
}
/**
* acf_is_field
*
* Returns true if the given params match a field.
*
* @date 21/1/19
* @since 5.7.10
*
* @param array $field The field array.
* @param mixed $id An optional identifier to search for.
* @return bool
*/
function acf_is_field( $field = false, $id = '' ) {
return (
is_array( $field )
&& isset( $field['key'] )
&& isset( $field['name'] )
);
}
/**
* acf_get_field_ancestors
*
* Returns an array of ancestor field ID's or keys.
*
* @date 22/06/2016
* @since 5.3.8
*
* @param array $field The field array.
* @return array
*/
function acf_get_field_ancestors( $field ) {
// Vars.
$ancestors = array();
// Loop over parents.
while ( $field['parent'] && $field = acf_get_field( $field['parent'] ) ) {
$ancestors[] = $field['ID'] ? $field['ID'] : $field['key'];
}
// return
return $ancestors;
}
/**
* acf_duplicate_fields
*
* Duplicate an array of fields.
*
* @date 16/06/2014
* @since 5.0.0
*
* @param array $fields An array of fields.
* @param int $parent_id The new parent ID.
* @return array
*/
function acf_duplicate_fields( $fields = array(), $parent_id = 0 ) {
// Generate keys for all new fields
// - Needed to alter conditional logic rules
// - Use usleep() to ensure unique keys.
$keys = array();
foreach ( $fields as $field ) {
usleep( 1 );
$keys[ $field['key'] ] = uniqid( 'field_' );
}
acf_append_data( 'generated_keys', $keys );
$duplicates = array();
// Duplicate fields.
foreach ( $fields as $field ) {
$field_id = $field['ID'] ? $field['ID'] : $field['key'];
$duplicates[] = acf_duplicate_field( $field_id, $parent_id );
}
// Return.
return $duplicates;
}
/**
* acf_duplicate_field
*
* Duplicates a field.
*
* @date 16/06/2014
* @since 5.0.0
*
* @param (int|string) $id The field ID, key or name.
* @param int $parent_id The new parent ID.
* @return bool True if field was duplicated.
*/
function acf_duplicate_field( $id = 0, $parent_id = 0 ) {
// Get the field.
$field = acf_get_field( $id );
// Bail early if field was not found.
if ( ! $field ) {
return false;
}
// Remove ID to avoid update.
$field['ID'] = 0;
// Generate key.
$keys = acf_get_data( 'generated_keys' );
if ( isset( $keys[ $field['key'] ] ) ) {
$field['key'] = $keys[ $field['key'] ];
} else {
$field['key'] = uniqid( 'field_' );
}
// Set parent.
if ( $parent_id ) {
$field['parent'] = $parent_id;
}
// Update conditional logic references because field keys have changed.
if ( $field['conditional_logic'] ) {
// Loop over groups
foreach ( $field['conditional_logic'] as $group_i => $group ) {
// Loop over rules
foreach ( $group as $rule_i => $rule ) {
$field['conditional_logic'][ $group_i ][ $rule_i ]['field'] = isset( $keys[ $rule['field'] ] ) ? $keys[ $rule['field'] ] : $rule['field'];
}
}
}
/**
* Filters the $field array after it has been duplicated.
*
* @date 12/02/2014
* @since 5.0.0
*
* @param array $field The field array.
*/
$field = apply_filters( 'acf/duplicate_field', $field );
// Update and return.
return acf_update_field( $field );
}
// Register variation.
acf_add_filter_variations( 'acf/duplicate_field', array( 'type' ), 0 );
/**
* acf_prepare_fields_for_export
*
* Returns a modified array of fields ready for export.
*
* @date 11/03/2014
* @since 5.0.0
*
* @param array $fields An array of fields.
* @return array
*/
function acf_prepare_fields_for_export( $fields = array() ) {
// Map function and return.
return array_map( 'acf_prepare_field_for_export', $fields );
}
/**
* acf_prepare_field_for_export
*
* Returns a modified field ready for export.
*
* @date 11/03/2014
* @since 5.0.0
*
* @param array $field The field array.
* @return array
*/
function acf_prepare_field_for_export( $field ) {
// Remove args.
acf_extract_vars( $field, array( 'ID', 'prefix', 'value', 'menu_order', 'id', 'class', 'parent', '_name', '_prepare', '_valid' ) );
/**
* Filters the $field array before being returned to the export tool.
*
* @date 12/02/2014
* @since 5.0.0
*
* @param array $field The field array.
*/
return apply_filters( 'acf/prepare_field_for_export', $field );
}
// Register variation.
acf_add_filter_variations( 'acf/prepare_field_for_export', array( 'type' ), 0 );
/**
* acf_prepare_field_for_import
*
* Returns a modified array of fields ready for import.
*
* @date 11/03/2014
* @since 5.0.0
*
* @param array $fields An array of fields.
* @return array
*/
function acf_prepare_fields_for_import( $fields = array() ) {
// Ensure array is sequential.
$fields = array_values( $fields );
// Prepare each field for import making sure to detect additional sub fields.
$i = 0;
while ( $i < count( $fields ) ) {
// Prepare field.
$field = acf_prepare_field_for_import( $fields[ $i ] );
// Update single field.
if ( isset( $field['key'] ) ) {
$fields[ $i ] = $field;
// Insert multiple fields.
} else {
array_splice( $fields, $i, 1, $field );
}
// Iterate.
$i++;
}
/**
* Filters the $fields array before being returned to the import tool.
*
* @date 12/02/2014
* @since 5.0.0
*
* @param array $fields The array of fields.
*/
return apply_filters( 'acf/prepare_fields_for_import', $fields );
}
/**
* acf_prepare_field_for_import
*
* Returns a modified field ready for import.
* Allows parent fields to modify themselves and also return sub fields.
*
* @date 11/03/2014
* @since 5.0.0
*
* @param array $field The field array.
* @return array
*/
function acf_prepare_field_for_import( $field ) {
/**
* Filters the $field array before being returned to the import tool.
*
* @date 12/02/2014
* @since 5.0.0
*
* @param array $field The field array.
*/
return apply_filters( 'acf/prepare_field_for_import', $field );
}
// Register variation.
acf_add_filter_variations( 'acf/prepare_field_for_import', array( 'type' ), 0 );