api-template.php
29.1 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
<?php
/*
* get_field()
*
* This function will return a custom field value for a specific field name/key + post_id.
* There is a 3rd parameter to turn on/off formating. This means that an image field will not use
* its 'return option' to format the value but return only what was saved in the database
*
* @type function
* @since 3.6
* @date 29/01/13
*
* @param $selector (string) the field name or key
* @param $post_id (mixed) the post_id of which the value is saved against
* @param $format_value (boolean) whether or not to format the value as described above
* @return (mixed)
*/
function get_field( $selector, $post_id = false, $format_value = true ) {
// filter post_id
$post_id = acf_get_valid_post_id( $post_id );
// get field
$field = acf_maybe_get_field( $selector, $post_id );
// create dummy field
if ( ! $field ) {
$field = acf_get_valid_field(
array(
'name' => $selector,
'key' => '',
'type' => '',
)
);
// prevent formatting
$format_value = false;
}
// get value for field
$value = acf_get_value( $post_id, $field );
// format value
if ( $format_value ) {
// get value for field
$value = acf_format_value( $value, $post_id, $field );
}
// return
return $value;
}
/**
* This function is the same as echo get_field().
*
* @since 1.0.3
* @date 29/01/13
*
* @param string $selector The field name or key.
* @param mixed $post_id The post_id of which the value is saved against.
* @return void
*/
function the_field( $selector, $post_id = false, $format_value = true ) {
$value = get_field( $selector, $post_id, $format_value );
if ( is_array( $value ) ) {
$value = implode( ', ', $value );
}
echo $value;
}
/**
* This function will return an array containing all the field data for a given field_name.
*
* @since 3.6
* @date 3/02/13
*
* @param string $selector The field name or key.
* @param mixed $post_id The post_id of which the value is saved against.
* @param bool $format_value Whether to format the field value.
* @param bool $load_value Whether to load the field value.
*
* @return array|false $field
*/
function get_field_object( $selector, $post_id = false, $format_value = true, $load_value = true ) {
// Compatibility with ACF ~4.
if ( is_array( $format_value ) && isset( $format_value['format_value'] ) ) {
$format_value = $format_value['format_value'];
}
$post_id = acf_get_valid_post_id( $post_id );
$field = acf_maybe_get_field( $selector, $post_id );
if ( ! $field ) {
return false;
}
if ( $load_value ) {
$field['value'] = acf_get_value( $post_id, $field );
}
if ( $format_value ) {
$field['value'] = acf_format_value( $field['value'], $post_id, $field );
}
return $field;
}
/*
* acf_get_object_field
*
* This function will return a field for the given selector.
* It will also review the field_reference to ensure the correct field is returned which makes it useful for the template API
*
* @type function
* @date 4/08/2015
* @since 5.2.3
*
* @param $selector (mixed) identifier of field. Can be an ID, key, name or post object
* @param $post_id (mixed) the post_id of which the value is saved against
* @param $strict (boolean) if true, return a field only when a field key is found.
* @return $field (array)
*/
function acf_maybe_get_field( $selector, $post_id = false, $strict = true ) {
// init
acf_init();
// Check if field key was given.
if ( acf_is_field_key( $selector ) ) {
return acf_get_field( $selector );
}
// Lookup field via reference.
$post_id = acf_get_valid_post_id( $post_id );
$field = acf_get_meta_field( $selector, $post_id );
if ( $field ) {
return $field;
}
// Lookup field loosely via name.
if ( ! $strict ) {
return acf_get_field( $selector );
}
// Return no result.
return false;
}
/*
* acf_maybe_get_sub_field
*
* This function will attempt to find a sub field
*
* @type function
* @date 3/10/2016
* @since 5.4.0
*
* @param $post_id (int)
* @return $post_id (int)
*/
function acf_maybe_get_sub_field( $selectors, $post_id = false, $strict = true ) {
// bail early if not enough selectors
if ( ! is_array( $selectors ) || count( $selectors ) < 3 ) {
return false;
}
// vars
$offset = acf_get_setting( 'row_index_offset' );
$selector = acf_extract_var( $selectors, 0 );
$selectors = array_values( $selectors ); // reset keys
// attempt get field
$field = acf_maybe_get_field( $selector, $post_id, $strict );
// bail early if no field
if ( ! $field ) {
return false;
}
// loop
for ( $j = 0; $j < count( $selectors ); $j += 2 ) {
// vars
$sub_i = $selectors[ $j ];
$sub_s = $selectors[ $j + 1 ];
$field_name = $field['name'];
// find sub field
$field = acf_get_sub_field( $sub_s, $field );
// bail early if no sub field
if ( ! $field ) {
return false;
}
// add to name
$field['name'] = $field_name . '_' . ( $sub_i - $offset ) . '_' . $field['name'];
}
// return
return $field;
}
/*
* get_fields()
*
* This function will return an array containing all the custom field values for a specific post_id.
* The function is not very elegant and wastes a lot of PHP memory / SQL queries if you are not using all the values.
*
* @type function
* @since 3.6
* @date 29/01/13
*
* @param $post_id (mixed) the post_id of which the value is saved against
* @param $format_value (boolean) whether or not to format the field value
* @return (array) associative array where field name => field value
*/
function get_fields( $post_id = false, $format_value = true ) {
// vars
$fields = get_field_objects( $post_id, $format_value );
$meta = array();
// bail early
if ( ! $fields ) {
return false;
}
// populate
foreach ( $fields as $k => $field ) {
$meta[ $k ] = $field['value'];
}
// return
return $meta;
}
/*
* get_field_objects()
*
* This function will return an array containing all the custom field objects for a specific post_id.
* The function is not very elegant and wastes a lot of PHP memory / SQL queries if you are not using all the fields / values.
*
* @type function
* @since 3.6
* @date 29/01/13
*
* @param $post_id (mixed) the post_id of which the value is saved against
* @param $format_value (boolean) whether or not to format the field value
* @param $load_value (boolean) whether or not to load the field value
* @return (array) associative array where field name => field
*/
function get_field_objects( $post_id = false, $format_value = true, $load_value = true ) {
// init
acf_init();
// validate post_id
$post_id = acf_get_valid_post_id( $post_id );
// get meta
$meta = acf_get_meta( $post_id );
// bail early if no meta
if ( empty( $meta ) ) {
return false;
}
// populate vars
$fields = array();
foreach ( $meta as $key => $value ) {
// bail if reference key does not exist
if ( ! isset( $meta[ "_$key" ] ) || ( ! is_string( $meta[ "_$key" ] ) && ! is_numeric( $meta[ "_$key" ] ) ) ) {
continue;
}
// get field
$field = acf_get_field( $meta[ "_$key" ] );
// bail early if no field, or if the field's name is different to $key
// - solves problem where sub fields (and clone fields) are incorrectly allowed
if ( ! $field || $field['name'] !== $key ) {
continue;
}
// load value
if ( $load_value ) {
$field['value'] = acf_get_value( $post_id, $field );
}
// format value
if ( $format_value ) {
$field['value'] = acf_format_value( $field['value'], $post_id, $field );
}
// append to $value
$fields[ $key ] = $field;
}
// no value
if ( empty( $fields ) ) {
return false;
}
// return
return $fields;
}
/**
* have_rows
*
* Checks if a field (such as Repeater or Flexible Content) has any rows of data to loop over.
* This function is intended to be used in conjunction with the_row() to step through available values.
*
* @date 2/09/13
* @since 4.3.0
*
* @param string $selector The field name or field key.
* @param mixed $post_id The post ID where the value is saved. Defaults to the current post.
* @return bool
*/
function have_rows( $selector, $post_id = false ) {
// Validate and backup $post_id.
$_post_id = $post_id;
$post_id = acf_get_valid_post_id( $post_id );
// Vars.
$key = "selector={$selector}/post_id={$post_id}";
$active_loop = acf_get_loop( 'active' );
$prev_loop = acf_get_loop( 'previous' );
$new_loop = false;
$sub_field = false;
// Check if no active loop.
if ( ! $active_loop ) {
$new_loop = 'parent';
// Detect "change" compared to the active loop.
} elseif ( $key !== $active_loop['key'] ) {
// Find sub field and check if a sub value exists.
$sub_field_exists = false;
$sub_field = acf_get_sub_field( $selector, $active_loop['field'] );
if ( $sub_field ) {
$sub_field_exists = isset( $active_loop['value'][ $active_loop['i'] ][ $sub_field['key'] ] );
}
// Detect change in post_id.
if ( $post_id != $active_loop['post_id'] ) {
// Case: Change in $post_id was due to this being a nested loop and not specifying the $post_id.
// Action: Move down one level into a new loop.
if ( empty( $_post_id ) && $sub_field_exists ) {
$new_loop = 'child';
// Case: Change in $post_id was due to a nested loop ending.
// Action: move up one level through the loops.
} elseif ( $prev_loop && $prev_loop['post_id'] == $post_id ) {
acf_remove_loop( 'active' );
$active_loop = $prev_loop;
// Case: Chang in $post_id is the most obvious, used in an WP_Query loop with multiple $post objects.
// Action: leave this current loop alone and create a new parent loop.
} else {
$new_loop = 'parent';
}
// Detect change in selector.
} elseif ( $selector != $active_loop['selector'] ) {
// Case: Change in $field_name was due to this being a nested loop.
// Action: move down one level into a new loop.
if ( $sub_field_exists ) {
$new_loop = 'child';
// Case: Change in $field_name was due to a nested loop ending.
// Action: move up one level through the loops.
} elseif ( $prev_loop && $prev_loop['selector'] == $selector && $prev_loop['post_id'] == $post_id ) {
acf_remove_loop( 'active' );
$active_loop = $prev_loop;
// Case: Change in $field_name is the most obvious, this is a new loop for a different field within the $post.
// Action: leave this current loop alone and create a new parent loop.
} else {
$new_loop = 'parent';
}
}
}
// Add loop if required.
if ( $new_loop ) {
$args = array(
'key' => $key,
'selector' => $selector,
'post_id' => $post_id,
'name' => null,
'value' => null,
'field' => null,
'i' => -1,
);
// Case: Parent loop.
if ( $new_loop === 'parent' ) {
$field = get_field_object( $selector, $post_id, false );
if ( $field ) {
$args['field'] = $field;
$args['value'] = $field['value'];
$args['name'] = $field['name'];
unset( $args['field']['value'] );
}
// Case: Child loop ($sub_field must exist).
} else {
$args['field'] = $sub_field;
$args['value'] = $active_loop['value'][ $active_loop['i'] ][ $sub_field['key'] ];
$args['name'] = "{$active_loop['name']}_{$active_loop['i']}_{$sub_field['name']}";
$args['post_id'] = $active_loop['post_id'];
}
// Bail early if value is either empty or a non array.
if ( ! $args['value'] || ! is_array( $args['value'] ) ) {
return false;
}
// Allow for non repeatable data for Group and Clone fields.
if ( acf_get_field_type_prop( $args['field']['type'], 'have_rows' ) === 'single' ) {
$args['value'] = array( $args['value'] );
}
// Add loop.
$active_loop = acf_add_loop( $args );
}
// Return true if next row exists.
if ( $active_loop && isset( $active_loop['value'][ $active_loop['i'] + 1 ] ) ) {
return true;
}
// Return false if no next row.
acf_remove_loop( 'active' );
return false;
}
/*
* the_row
*
* This function will progress the global repeater or flexible content value 1 row
*
* @type function
* @date 2/09/13
* @since 4.3.0
*
* @param N/A
* @return (array) the current row data
*/
function the_row( $format = false ) {
// vars
$i = acf_get_loop( 'active', 'i' );
// increase
$i++;
// update
acf_update_loop( 'active', 'i', $i );
// return
return get_row( $format );
}
function get_row( $format = false ) {
// vars
$loop = acf_get_loop( 'active' );
// bail early if no loop
if ( ! $loop ) {
return false;
}
// get value
$value = acf_maybe_get( $loop['value'], $loop['i'] );
// bail early if no current value
// possible if get_row_layout() is called before the_row()
if ( ! $value ) {
return false;
}
// format
if ( $format ) {
// vars
$field = $loop['field'];
// single row
if ( acf_get_field_type_prop( $field['type'], 'have_rows' ) === 'single' ) {
// format value
$value = acf_format_value( $value, $loop['post_id'], $field );
// multiple rows
} else {
// format entire value
// - solves problem where cached value is incomplete
// - no performance issues here thanks to cache
$value = acf_format_value( $loop['value'], $loop['post_id'], $field );
$value = acf_maybe_get( $value, $loop['i'] );
}
}
// return
return $value;
}
function get_row_index() {
// vars
$i = acf_get_loop( 'active', 'i' );
$offset = acf_get_setting( 'row_index_offset' );
// return
return $offset + $i;
}
function the_row_index() {
echo get_row_index();
}
/*
* get_row_sub_field
*
* This function is used inside a 'has_sub_field' while loop to return a sub field object
*
* @type function
* @date 16/05/2016
* @since 5.3.8
*
* @param $selector (string)
* @return (array)
*/
function get_row_sub_field( $selector ) {
// vars
$row = acf_get_loop( 'active' );
// bail early if no row
if ( ! $row ) {
return false;
}
// attempt to find sub field
$sub_field = acf_get_sub_field( $selector, $row['field'] );
// bail early if no field
if ( ! $sub_field ) {
return false;
}
// update field's name based on row data
$sub_field['name'] = "{$row['name']}_{$row['i']}_{$sub_field['name']}";
// return
return $sub_field;
}
/*
* get_row_sub_value
*
* This function is used inside a 'has_sub_field' while loop to return a sub field value
*
* @type function
* @date 16/05/2016
* @since 5.3.8
*
* @param $selector (string)
* @return (mixed)
*/
function get_row_sub_value( $selector ) {
// vars
$row = acf_get_loop( 'active' );
// bail early if no row
if ( ! $row ) {
return null;
}
// return value
if ( isset( $row['value'][ $row['i'] ][ $selector ] ) ) {
return $row['value'][ $row['i'] ][ $selector ];
}
// return
return null;
}
/*
* reset_rows
*
* This function will find the current loop and unset it from the global array.
* To bo used when loop finishes or a break is used
*
* @type function
* @date 26/10/13
* @since 5.0.0
*
* @param $hard_reset (boolean) completely wipe the global variable, or just unset the active row
* @return (boolean)
*/
function reset_rows() {
// remove last loop
acf_remove_loop( 'active' );
// return
return true;
}
/*
* has_sub_field()
*
* This function is used inside a while loop to return either true or false (loop again or stop).
* When using a repeater or flexible content field, it will loop through the rows until
* there are none left or a break is detected
*
* @type function
* @since 1.0.3
* @date 29/01/13
*
* @param $field_name (string) the field name
* @param $post_id (mixed) the post_id of which the value is saved against
* @return (boolean)
*/
function has_sub_field( $field_name, $post_id = false ) {
// vars
$r = have_rows( $field_name, $post_id );
// if has rows, progress through 1 row for the while loop to work
if ( $r ) {
the_row();
}
// return
return $r;
}
function has_sub_fields( $field_name, $post_id = false ) {
return has_sub_field( $field_name, $post_id );
}
/*
* get_sub_field()
*
* This function is used inside a 'has_sub_field' while loop to return a sub field value
*
* @type function
* @since 1.0.3
* @date 29/01/13
*
* @param $field_name (string) the field name
* @return (mixed)
*/
function get_sub_field( $selector = '', $format_value = true ) {
// get sub field
$sub_field = get_sub_field_object( $selector, $format_value );
// bail early if no sub field
if ( ! $sub_field ) {
return false;
}
// return
return $sub_field['value'];
}
/*
* the_sub_field()
*
* This function is the same as echo get_sub_field
*
* @type function
* @since 1.0.3
* @date 29/01/13
*
* @param $field_name (string) the field name
* @return n/a
*/
function the_sub_field( $field_name, $format_value = true ) {
$value = get_sub_field( $field_name, $format_value );
if ( is_array( $value ) ) {
$value = implode( ', ', $value );
}
echo $value;
}
/*
* get_sub_field_object()
*
* This function is used inside a 'has_sub_field' while loop to return a sub field object
*
* @type function
* @since 3.5.8.1
* @date 29/01/13
*
* @param $child_name (string) the field name
* @return (array)
*/
function get_sub_field_object( $selector, $format_value = true, $load_value = true ) {
// vars
$row = acf_get_loop( 'active' );
// bail early if no row
if ( ! $row ) {
return false;
}
// attempt to find sub field
$sub_field = get_row_sub_field( $selector );
// bail early if no sub field
if ( ! $sub_field ) {
return false;
}
// load value
if ( $load_value ) {
$sub_field['value'] = get_row_sub_value( $sub_field['key'] );
}
// format value
if ( $format_value ) {
// get value for field
$sub_field['value'] = acf_format_value( $sub_field['value'], $row['post_id'], $sub_field );
}
// return
return $sub_field;
}
/*
* get_row_layout()
*
* This function will return a string representation of the current row layout within a 'have_rows' loop
*
* @type function
* @since 3.0.6
* @date 29/01/13
*
* @param n/a
* @return (string)
*/
function get_row_layout() {
// vars
$row = get_row();
// return
if ( isset( $row['acf_fc_layout'] ) ) {
return $row['acf_fc_layout'];
}
// return
return false;
}
/**
* This function is used to add basic shortcode support for the ACF plugin
* eg. [acf field="heading" post_id="123" format_value="1"]
*
* @since 1.1.1
* @date 29/01/13
*
* @param array $atts The shortcode attributes.
*
* @return string
*/
function acf_shortcode( $atts ) {
// Return if the ACF shortcode is disabled.
if ( ! acf_get_setting( 'enable_shortcode' ) ) {
return;
}
if ( function_exists( 'wp_is_block_theme' ) && wp_is_block_theme() ) {
// Prevent the ACF shortcode in FSE block template parts by default.
if ( ! doing_filter( 'the_content' ) && ! apply_filters( 'acf/shortcode/allow_in_block_themes_outside_content', false ) ) {
return;
}
}
// Limit previews of ACF shortcode data for users without publish_posts permissions.
$preview_capability = apply_filters( 'acf/shortcode/preview_capability', 'publish_posts' );
if ( is_preview() && ! current_user_can( $preview_capability ) ) {
return apply_filters( 'acf/shortcode/preview_capability_message', __( '[ACF shortcode value disabled for preview]', 'acf' ) );
}
// Mitigate issue where some AJAX requests can return ACF field data.
$ajax_capability = apply_filters( 'acf/ajax/shortcode_capability', 'edit_posts' );
if ( wp_doing_ajax() && ( $ajax_capability !== false ) && ! current_user_can( $ajax_capability ) ) {
return;
}
$atts = shortcode_atts(
array(
'field' => '',
'post_id' => false,
'format_value' => true,
),
$atts,
'acf'
);
$access_already_prevented = apply_filters( 'acf/prevent_access_to_unknown_fields', false );
$filter_applied = false;
if ( ! $access_already_prevented ) {
$filter_applied = true;
add_filter( 'acf/prevent_access_to_unknown_fields', '__return_true' );
}
// Try to get the field value.
$value = get_field( $atts['field'], $atts['post_id'], $atts['format_value'] );
if ( $filter_applied ) {
remove_filter( 'acf/prevent_access_to_unknown_fields', '__return_true' );
}
if ( is_array( $value ) ) {
$value = implode( ', ', $value );
}
return $value;
}
add_shortcode( 'acf', 'acf_shortcode' );
/*
* update_field()
*
* This function will update a value in the database
*
* @type function
* @since 3.1.9
* @date 29/01/13
*
* @param $selector (string) the field name or key
* @param $value (mixed) the value to save in the database
* @param $post_id (mixed) the post_id of which the value is saved against
* @return (boolean)
*/
function update_field( $selector, $value, $post_id = false ) {
// filter post_id
$post_id = acf_get_valid_post_id( $post_id );
// get field
$field = acf_maybe_get_field( $selector, $post_id, false );
// create dummy field
if ( ! $field ) {
$field = acf_get_valid_field(
array(
'name' => $selector,
'key' => '',
'type' => '',
)
);
}
// save
return acf_update_value( $value, $post_id, $field );
}
/*
* update_sub_field
*
* This function will update a value of a sub field in the database
*
* @type function
* @date 2/04/2014
* @since 5.0.0
*
* @param $selector (mixed) the sub field name or key, or an array of ancestors
* @param $value (mixed) the value to save in the database
* @param $post_id (mixed) the post_id of which the value is saved against
* @return (boolean)
*/
function update_sub_field( $selector, $value, $post_id = false ) {
// vars
$sub_field = false;
// get sub field
if ( is_array( $selector ) ) {
$post_id = acf_get_valid_post_id( $post_id );
$sub_field = acf_maybe_get_sub_field( $selector, $post_id, false );
} else {
$post_id = acf_get_loop( 'active', 'post_id' );
$sub_field = get_row_sub_field( $selector );
}
// bail early if no sub field
if ( ! $sub_field ) {
return false;
}
// update
return acf_update_value( $value, $post_id, $sub_field );
}
/*
* delete_field()
*
* This function will remove a value from the database
*
* @type function
* @since 3.1.9
* @date 29/01/13
*
* @param $selector (string) the field name or key
* @param $post_id (mixed) the post_id of which the value is saved against
* @return (boolean)
*/
function delete_field( $selector, $post_id = false ) {
// filter post_id
$post_id = acf_get_valid_post_id( $post_id );
// get field
$field = acf_maybe_get_field( $selector, $post_id );
// delete
return $field ? acf_delete_value( $post_id, $field ) : false;
}
/*
* delete_sub_field
*
* This function will delete a value of a sub field in the database
*
* @type function
* @date 2/04/2014
* @since 5.0.0
*
* @param $selector (mixed) the sub field name or key, or an array of ancestors
* @param $value (mixed) the value to save in the database
* @param $post_id (mixed) the post_id of which the value is saved against
* @return (boolean)
*/
function delete_sub_field( $selector, $post_id = false ) {
return update_sub_field( $selector, null, $post_id );
}
/*
* add_row
*
* This function will add a row of data to a field
*
* @type function
* @date 16/10/2015
* @since 5.2.3
*
* @param $selector (string)
* @param $row (array)
* @param $post_id (mixed)
* @return (boolean)
*/
function add_row( $selector, $row = false, $post_id = false ) {
// filter post_id
$post_id = acf_get_valid_post_id( $post_id );
// get field
$field = acf_maybe_get_field( $selector, $post_id, false );
// bail early if no field
if ( ! $field ) {
return false;
}
// get raw value
$value = acf_get_value( $post_id, $field );
// ensure array
$value = acf_get_array( $value );
// append
$value[] = $row;
// Paginated repeaters should be saved normally.
$field['pagination'] = false;
// update value
acf_update_value( $value, $post_id, $field );
// return
return count( $value );
}
/*
* add_sub_row
*
* This function will add a row of data to a field
*
* @type function
* @date 16/10/2015
* @since 5.2.3
*
* @param $selector (string)
* @param $row (array)
* @param $post_id (mixed)
* @return (boolean)
*/
function add_sub_row( $selector, $row = false, $post_id = false ) {
// vars
$sub_field = false;
// get sub field
if ( is_array( $selector ) ) {
$post_id = acf_get_valid_post_id( $post_id );
$sub_field = acf_maybe_get_sub_field( $selector, $post_id, false );
} else {
$post_id = acf_get_loop( 'active', 'post_id' );
$sub_field = get_row_sub_field( $selector );
}
// bail early if no sub field
if ( ! $sub_field ) {
return false;
}
// get raw value
$value = acf_get_value( $post_id, $sub_field );
// ensure array
$value = acf_get_array( $value );
// append
$value[] = $row;
// update
acf_update_value( $value, $post_id, $sub_field );
// return
return count( $value );
}
/*
* update_row
*
* This function will update a row of data to a field
*
* @type function
* @date 19/10/2015
* @since 5.2.3
*
* @param $selector (string)
* @param $i (int)
* @param $row (array)
* @param $post_id (mixed)
* @return (boolean)
*/
function update_row( $selector, $i = 1, $row = false, $post_id = false ) {
// vars
$offset = acf_get_setting( 'row_index_offset' );
$i = $i - $offset;
// filter post_id
$post_id = acf_get_valid_post_id( $post_id );
// get field
$field = acf_maybe_get_field( $selector, $post_id, false );
// bail early if no field
if ( ! $field ) {
return false;
}
// get raw value
$value = acf_get_value( $post_id, $field );
// ensure array
$value = acf_get_array( $value );
// update
$value[ $i ] = $row;
// update value
acf_update_value( $value, $post_id, $field );
// return
return true;
}
/*
* update_sub_row
*
* This function will add a row of data to a field
*
* @type function
* @date 16/10/2015
* @since 5.2.3
*
* @param $selector (string)
* @param $row (array)
* @param $post_id (mixed)
* @return (boolean)
*/
function update_sub_row( $selector, $i = 1, $row = false, $post_id = false ) {
// vars
$sub_field = false;
$offset = acf_get_setting( 'row_index_offset' );
$i = $i - $offset;
// get sub field
if ( is_array( $selector ) ) {
$post_id = acf_get_valid_post_id( $post_id );
$sub_field = acf_maybe_get_sub_field( $selector, $post_id, false );
} else {
$post_id = acf_get_loop( 'active', 'post_id' );
$sub_field = get_row_sub_field( $selector );
}
// bail early if no sub field
if ( ! $sub_field ) {
return false;
}
// get raw value
$value = acf_get_value( $post_id, $sub_field );
// ensure array
$value = acf_get_array( $value );
// append
$value[ $i ] = $row;
// update
acf_update_value( $value, $post_id, $sub_field );
// return
return true;
}
/*
* delete_row
*
* This function will delete a row of data from a field
*
* @type function
* @date 19/10/2015
* @since 5.2.3
*
* @param $selector (string)
* @param $i (int)
* @param $post_id (mixed)
* @return (boolean)
*/
function delete_row( $selector, $i = 1, $post_id = false ) {
// vars
$offset = acf_get_setting( 'row_index_offset' );
$i = $i - $offset;
// filter post_id
$post_id = acf_get_valid_post_id( $post_id );
// get field
$field = acf_maybe_get_field( $selector, $post_id );
// bail early if no field
if ( ! $field ) {
return false;
}
// get value
$value = acf_get_value( $post_id, $field );
// ensure array
$value = acf_get_array( $value );
// bail early if index doesn't exist
if ( ! isset( $value[ $i ] ) ) {
return false;
}
// unset
unset( $value[ $i ] );
// update
acf_update_value( $value, $post_id, $field );
// return
return true;
}
/*
* delete_sub_row
*
* This function will add a row of data to a field
*
* @type function
* @date 16/10/2015
* @since 5.2.3
*
* @param $selector (string)
* @param $row (array)
* @param $post_id (mixed)
* @return (boolean)
*/
function delete_sub_row( $selector, $i = 1, $post_id = false ) {
// vars
$sub_field = false;
$offset = acf_get_setting( 'row_index_offset' );
$i = $i - $offset;
// get sub field
if ( is_array( $selector ) ) {
$post_id = acf_get_valid_post_id( $post_id );
$sub_field = acf_maybe_get_sub_field( $selector, $post_id, false );
} else {
$post_id = acf_get_loop( 'active', 'post_id' );
$sub_field = get_row_sub_field( $selector );
}
// bail early if no sub field
if ( ! $sub_field ) {
return false;
}
// get raw value
$value = acf_get_value( $post_id, $sub_field );
// ensure array
$value = acf_get_array( $value );
// bail early if index doesn't exist
if ( ! isset( $value[ $i ] ) ) {
return false;
}
// append
unset( $value[ $i ] );
// update
acf_update_value( $value, $post_id, $sub_field );
// return
return true;
}
/*
* Depreceated Functions
*
* These functions are outdated
*
* @type function
* @date 4/03/2014
* @since 1.0.0
*
* @param n/a
* @return n/a
*/
function create_field( $field ) {
acf_render_field( $field );
}
function render_field( $field ) {
acf_render_field( $field );
}
function reset_the_repeater_field() {
return reset_rows();
}
function the_repeater_field( $field_name, $post_id = false ) {
return has_sub_field( $field_name, $post_id );
}
function the_flexible_field( $field_name, $post_id = false ) {
return has_sub_field( $field_name, $post_id );
}
function acf_filter_post_id( $post_id ) {
return acf_get_valid_post_id( $post_id );
}