-
Notifications
You must be signed in to change notification settings - Fork 196
Expand file tree
/
Copy pathapi-template.php
More file actions
1557 lines (1265 loc) · 41.3 KB
/
Copy pathapi-template.php
File metadata and controls
1557 lines (1265 loc) · 41.3 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
<?php
/**
* 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
*
* @since 3.6
*
* @param string $selector The field name or key.
* @param mixed $post_id The post_id of which the value is saved against.
* @param boolean $format_value Whether or not to format the value as described above.
* @param boolean $escape_html If we're formatting the value, make sure it's also HTML safe.
*
* @return mixed
*/
function get_field( $selector, $post_id = false, $format_value = true, $escape_html = false ) {
// 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
$dummy_field = false;
if ( ! $field ) {
$field = acf_get_valid_field(
array(
'name' => $selector,
'key' => '',
'type' => '',
)
);
// prevent formatting, flag as dummy in case $escape_html is true.
$format_value = false;
$dummy_field = true;
}
// get value for field
$value = acf_get_value( $post_id, $field );
// escape html is only compatible when formatting the value too
if ( ! $dummy_field && ! $format_value && $escape_html ) {
_doing_it_wrong( __FUNCTION__, __( 'Returning an escaped HTML value is only possible when format_value is also true. The field value has not been returned for security.', 'acf' ), '6.2.6' ); //phpcs:ignore -- escape not required.
return false;
}
// format value
if ( $format_value ) {
if ( $escape_html ) {
// return the escaped HTML version if requested.
if ( acf_field_type_supports( $field['type'], 'escaping_html' ) ) {
$value = acf_format_value( $value, $post_id, $field, true );
} else {
$new_value = acf_format_value( $value, $post_id, $field );
if ( is_array( $new_value ) ) {
$value = map_deep( $new_value, 'acf_esc_html' );
} else {
$value = acf_esc_html( $new_value );
}
}
} else {
// get value for field
$value = acf_format_value( $value, $post_id, $field );
}
}
// If we've built a dummy text field, we won't format the value, but they may still request it escaped. Use `acf_esc_html`
if ( $dummy_field && $escape_html ) {
if ( is_array( $value ) ) {
$value = map_deep( $value, 'acf_esc_html' );
} else {
$value = acf_esc_html( $value );
}
}
// return
return $value;
}
/**
* This function is the same as echo get_field(), but will escape the value for safe HTML output regardless of parameters.
*
* @since 1.0.3
*
* @param string $selector The field name or key.
* @param mixed $post_id The post_id of which the value is saved against.
* @param boolean $format_value Enable formatting of value. Default true.
*
* @return void
*/
function the_field( $selector, $post_id = false, $format_value = true ) {
$field = get_field_object( $selector, $post_id, $format_value, true, $format_value );
$value = $field ? $field['value'] : get_field( $selector, $post_id, $format_value, $format_value );
if ( is_array( $value ) ) {
$value = implode( ', ', $value );
}
// If we're not a scalar we'd throw an error, so return early for safety.
if ( ! is_scalar( $value ) ) {
return;
}
// If $format_value is false, we've not been able to apply field level escaping as we're giving the raw DB value. Escape the output with `acf_esc_html`.
if ( ! $format_value ) {
$value = acf_esc_html( $value );
}
// Get the unescaped value while we're still logging removed_unsafe_html.
$unescaped_value = get_field( $selector, $post_id, $format_value, false );
if ( is_array( $unescaped_value ) ) {
$unescaped_value = implode( ', ', $unescaped_value );
}
if ( ! is_scalar( $unescaped_value ) ) {
$unescaped_value = false;
}
$field_type = is_array( $field ) && isset( $field['type'] ) ? $field['type'] : 'text';
if ( apply_filters( 'acf/the_field/allow_unsafe_html', false, $selector, $post_id, $field_type, $field ) ) {
$value = $unescaped_value;
} elseif ( $unescaped_value !== false && (string) $value !== (string) $unescaped_value ) {
do_action( 'acf/removed_unsafe_html', __FUNCTION__, $selector, $field, $post_id );
}
echo $value; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- escaped by logic above.
}
/**
* Logs instances of ACF successfully escaping unsafe HTML.
*
* @since 6.2.5
*
* @param string $function The function that resulted in HTML being escaped.
* @param string $selector The selector (field key, name, etc.) passed to that function.
* @param array $field The field being queried when HTML was escaped.
* @param mixed $post_id The post ID the function was called on.
* @return void
*/
function _acf_log_escaped_html( $function, $selector, $field, $post_id ) {
// If the notice isn't shown, no use in logging the errors.
if ( apply_filters( 'acf/admin/prevent_escaped_html_notice', false ) ) {
return;
}
// If the notice has been dismissed, don't log further errors.
if ( get_option( 'acf_escaped_html_notice_dismissed' ) ) {
return;
}
// If the field isn't set, we've output a non-ACF field, so don't log anything.
if ( ! is_array( $field ) ) {
return;
}
$escaped = _acf_get_escaped_html_log();
// Only store up to 100 results at a time.
if ( count( $escaped ) >= 100 ) {
return;
}
// Bail if we already logged an error for this field.
if ( isset( $escaped[ $field['key'] ] ) ) {
return;
}
$escaped[ $field['key'] ] = array(
'selector' => $selector,
'function' => $function,
'field' => $field['label'],
'post_id' => $post_id,
);
_acf_update_escaped_html_log( $escaped );
}
add_action( 'acf/removed_unsafe_html', '_acf_log_escaped_html', 10, 4 );
/**
* Returns an array of instances where HTML was altered due to escaping in the_field or a shortcode.
*
* @since 6.2.5
*
* @return array
*/
function _acf_get_escaped_html_log() {
$escaped = get_option( 'acf_escaped_html_log', array() );
return is_array( $escaped ) ? $escaped : array();
}
/**
* Updates the array of instances where HTML was altered due to escaping in the_field or a shortcode.
*
* @since 6.2.5
*
* @param array $escaped The array of instances.
* @return boolean True on success, or false on failure.
*/
function _acf_update_escaped_html_log( $escaped = array() ) {
return update_option( 'acf_escaped_html_log', (array) $escaped, false );
}
/**
* Deletes the array of instances where HTML was altered due to escaping in the_field or a shortcode.
* Since 6.2.7, also clears the legacy `acf_will_escape_html_log` option to clean up.
*
* @since 6.2.5
*
* @return boolean True on success, or false on failure.
*/
function _acf_delete_escaped_html_log() {
delete_option( 'acf_will_escape_html_log' );
return delete_option( 'acf_escaped_html_log' );
}
/**
* This function will return an array containing all the field data for a given field_name.
*
* @since 3.6
*
* @param string $selector The field name or key.
* @param mixed $post_id The post_id of which the value is saved against.
* @param boolean $format_value Whether to format the field value.
* @param boolean $load_value Whether to load the field value.
* @param boolean $escape_html Should the field return a HTML safe formatted value if $format_value is true.
*
* @return array|false $field
*/
function get_field_object( $selector, $post_id = false, $format_value = true, $load_value = true, $escape_html = false ) {
// 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 );
}
// escape html is only compatible when formatting the value too
if ( ! $format_value && $escape_html ) {
_doing_it_wrong( __FUNCTION__, __( 'Returning an escaped HTML value is only possible when format_value is also true. The field value has not been returned for security.', 'acf' ), '6.2.6' ); //phpcs:ignore -- escape not required.
$field['value'] = false;
return $field;
}
// format value
if ( $load_value && $format_value ) {
if ( $escape_html ) {
// return the escaped HTML version if requested.
if ( acf_field_type_supports( $field['type'], 'escaping_html' ) ) {
$field['value'] = acf_format_value( $field['value'], $post_id, $field, true );
} else {
$new_value = acf_format_value( $field['value'], $post_id, $field );
if ( is_array( $new_value ) ) {
$field['value'] = map_deep( $new_value, 'acf_esc_html' );
} else {
$field['value'] = acf_esc_html( $new_value );
}
}
} else {
// get value for field
$field['value'] = acf_format_value( $field['value'], $post_id, $field );
}
}
return $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
*
* @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;
}
/**
* This function will attempt to find a sub field
*
* @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;
}
/**
* 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.
*
* @since 3.6
*
* @param mixed $post_id The post_id of which the value is saved against.
* @param boolean $format_value Whether or not to format the field value.
* @param boolean $escape_html Should the field return a HTML safe formatted value if $format_value is true.
*
* @return array|false Associative array where field name => field value, or false on failure.
*/
function get_fields( $post_id = false, $format_value = true, $escape_html = false ) {
// escape html is only compatible when formatting the value too
if ( ! $format_value && $escape_html ) {
_doing_it_wrong( __FUNCTION__, __( 'Returning escaped HTML values is only possible when format_value is also true. The field values have not been returned for security.', 'acf' ), '6.2.6' ); //phpcs:ignore -- escape not required.
return false;
}
// vars
$fields = get_field_objects( $post_id, $format_value, true, $escape_html );
$meta = array();
// bail early
if ( ! $fields ) {
return false;
}
// populate
foreach ( $fields as $k => $field ) {
$meta[ $k ] = $field['value'];
}
// return
return $meta;
}
/**
* 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.
*
* @since 3.6
*
* @param mixed $post_id The post_id of which the value is saved against.
* @param boolean $format_value Whether or not to format the field value.
* @param boolean $load_value Whether or not to load the field value.
* @param boolean $escape_html Should the field return a HTML safe formatted value if $format_value is true.
*
* @return array|false Associative array where field name => field, or false on failure.
*/
function get_field_objects( $post_id = false, $format_value = true, $load_value = true, $escape_html = false ) {
// 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;
}
// escape html is only compatible when formatting the value too
if ( ! $format_value && $escape_html ) {
_doing_it_wrong( __FUNCTION__, __( 'Returning escaped HTML values is only possible when format_value is also true. The field values have not been returned for security.', 'acf' ), '6.2.6' ); //phpcs:ignore -- escape not required.
}
// 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 );
}
// avoid returning field values when the function is called incorrectly.
if ( ! $format_value && $escape_html ) {
$field['value'] = false;
}
// format value
if ( $load_value && $format_value ) {
if ( $escape_html ) {
// return the escaped HTML version if requested.
if ( acf_field_type_supports( $field['type'], 'escaping_html' ) ) {
$field['value'] = acf_format_value( $field['value'], $post_id, $field, true );
} else {
$new_value = acf_format_value( $field['value'], $post_id, $field );
if ( is_array( $new_value ) ) {
$field['value'] = map_deep( $new_value, 'acf_esc_html' );
} else {
$field['value'] = acf_esc_html( $new_value );
}
}
} else {
// get value for field
$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;
}
/**
* 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.
*
* @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 boolean
*/
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;
}
/**
* This function will progress the global repeater or flexible content value 1 row
*
* @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 intval( get_row_index() );
}
/**
* This function is used inside a 'has_sub_field' while loop to return a sub field object
*
* @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;
}
/**
* This function is used inside a 'has_sub_field' while loop to return a sub field value
*
* @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;
}
/**
* This function will find the current loop and unset it from the global array.
* To be used when loop finishes or a break is used
*
* @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;
}
/**
* 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
*
* @since 1.0.3
*
* @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;
}
/**
* Alias of has_sub_field
*/
function has_sub_fields( $field_name, $post_id = false ) {
return has_sub_field( $field_name, $post_id );
}
/**
* This function is used inside a 'has_sub_field' while loop to return a sub field value
*
* @since 1.0.3
*
* @param string $selector The field name or key.
* @param boolean $format_value Whether or not to format the value as described above.
* @param boolean $escape_html If we're formatting the value, make sure it's also HTML safe.
*
* @return mixed
*/
function get_sub_field( $selector = '', $format_value = true, $escape_html = false ) {
// get sub field
$sub_field = get_sub_field_object( $selector, $format_value, true, $escape_html );
// bail early if no sub field
if ( ! $sub_field ) {
return false;
}
// return
return $sub_field['value'];
}
/**
* This function is the same as echo get_sub_field(), but will escape the value for safe HTML output.
*
* @since 1.0.3
*
* @param string $field_name The field name.
* @param boolean $format_value Enable formatting of value. When false, the field value will be escaped at this level with `acf_esc_html`. Default true.
*
* @return void
*/
function the_sub_field( $field_name, $format_value = true ) {
$field = get_sub_field_object( $field_name, $format_value, true, $format_value );
$value = ( is_array( $field ) && isset( $field['value'] ) ) ? $field['value'] : false;
if ( is_array( $value ) ) {
$value = implode( ', ', $value );
}
// If we're not a scalar we'd throw an error, so return early for safety.
if ( ! is_scalar( $value ) ) {
return;
}
// If $format_value is false, we've not been able to apply field level escaping as we're giving the raw DB value. Escape the output with `acf_esc_html`.
if ( ! $format_value ) {
$value = acf_esc_html( $value );
}
$unescaped_field = get_sub_field_object( $field_name, $format_value, true, false );
$unescaped_value = ( is_array( $unescaped_field ) && isset( $unescaped_field['value'] ) ) ? $unescaped_field['value'] : false;
if ( is_array( $unescaped_value ) ) {
$unescaped_value = implode( ', ', $unescaped_value );
}
if ( ! is_scalar( $unescaped_value ) ) {
$unescaped_value = false;
}
$field_type = is_array( $field ) && isset( $field['type'] ) ? $field['type'] : 'text';
if ( apply_filters( 'acf/the_field/allow_unsafe_html', false, $field_name, 'sub_field', $field_type, $field ) ) {
$value = $unescaped_value;
} elseif ( $unescaped_value !== false && (string) $value !== (string) $unescaped_value ) {
do_action( 'acf/removed_unsafe_html', __FUNCTION__, $field_name, $field, false );
}
echo $value; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- escaped inside get_sub_field_object where necessary.
}
/**
* This function is used inside a 'has_sub_field' while loop to return a sub field object
*
* @since 3.5.8.1
*
* @param string $selector The field name or key.
* @param boolean $format_value Whether to format the field value.
* @param boolean $load_value Whether to load the field value.
* @param boolean $escape_html Should the field return a HTML safe formatted value.
*
* @return mixed
*/
function get_sub_field_object( $selector, $format_value = true, $load_value = true, $escape_html = false ) {
$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'] );
}
// escape html is only compatible when formatting the value too
if ( ! $format_value && $escape_html ) {
_doing_it_wrong( __FUNCTION__, __( 'Returning an escaped HTML value is only possible when format_value is also true. The field value has not been returned for security.', 'acf' ), '6.2.6' ); //phpcs:ignore -- escape not required.
$sub_field['value'] = false;
}
// format value
if ( $load_value && $format_value ) {
if ( $escape_html ) {
// return the escaped HTML version if requested.
if ( acf_field_type_supports( $sub_field['type'], 'escaping_html' ) ) {
$sub_field['value'] = acf_format_value( $sub_field['value'], $row['post_id'], $sub_field, true );
} else {
$new_value = acf_format_value( $sub_field['value'], $row['post_id'], $sub_field );
if ( is_array( $new_value ) ) {
$sub_field['value'] = map_deep( $new_value, 'acf_esc_html' );
} else {
$sub_field['value'] = acf_esc_html( $new_value );
}
}
} else {
// get value for field
$sub_field['value'] = acf_format_value( $sub_field['value'], $row['post_id'], $sub_field );
}
}
// return
return $sub_field;
}
/**
* This function will return a string representation of the current row layout within a 'have_rows' loop
*
* @since 3.0.6
*
* @return mixed
*/
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"]
*