@@ -115,10 +115,14 @@ function the_field( $selector, $post_id = false, $format_value = true ) {
115115 $ unescaped_value = implode ( ', ' , $ unescaped_value );
116116 }
117117
118+ if ( ! is_scalar ( $ unescaped_value ) ) {
119+ $ unescaped_value = false ;
120+ }
121+
118122 $ field_type = is_array ( $ field ) && isset ( $ field ['type ' ] ) ? $ field ['type ' ] : 'text ' ;
119123 if ( apply_filters ( 'acf/the_field/allow_unsafe_html ' , false , $ selector , $ post_id , $ field_type , $ field ) ) {
120124 $ value = $ unescaped_value ;
121- } elseif ( (string ) $ value !== (string ) $ unescaped_value ) {
125+ } elseif ( $ unescaped_value !== false && (string ) $ value !== (string ) $ unescaped_value ) {
122126 do_action ( 'acf/removed_unsafe_html ' , __FUNCTION__ , $ selector , $ field , $ post_id );
123127 }
124128
@@ -889,10 +893,14 @@ function the_sub_field( $field_name, $format_value = true ) {
889893 $ unescaped_value = implode ( ', ' , $ unescaped_value );
890894 }
891895
896+ if ( ! is_scalar ( $ unescaped_value ) ) {
897+ $ unescaped_value = false ;
898+ }
899+
892900 $ field_type = is_array ( $ field ) && isset ( $ field ['type ' ] ) ? $ field ['type ' ] : 'text ' ;
893901 if ( apply_filters ( 'acf/the_field/allow_unsafe_html ' , false , $ field_name , 'sub_field ' , $ field_type , $ field ) ) {
894902 $ value = $ unescaped_value ;
895- } elseif ( (string ) $ value !== (string ) $ unescaped_value ) {
903+ } elseif ( $ unescaped_value !== false && (string ) $ value !== (string ) $ unescaped_value ) {
896904 do_action ( 'acf/removed_unsafe_html ' , __FUNCTION__ , $ field_name , $ field , false );
897905 }
898906
@@ -999,7 +1007,11 @@ function get_row_layout() {
9991007function acf_shortcode ( $ atts ) {
10001008 // Return if the ACF shortcode is disabled.
10011009 if ( ! acf_get_setting ( 'enable_shortcode ' ) ) {
1002- return ;
1010+ if ( is_preview () ) {
1011+ return apply_filters ( 'acf/shortcode/disabled_message ' , __ ( '[The ACF shortcode is disabled on this site] ' , 'acf ' ) );
1012+ } else {
1013+ return ;
1014+ }
10031015 }
10041016
10051017 if ( function_exists ( 'wp_is_block_theme ' ) && wp_is_block_theme () ) {
@@ -1031,6 +1043,21 @@ function acf_shortcode( $atts ) {
10311043 'acf '
10321044 );
10331045
1046+ // Decode the post ID for filtering.
1047+ $ post_id = acf_get_valid_post_id ( $ atts ['post_id ' ] );
1048+ $ decoded_post_id = acf_decode_post_id ( $ post_id );
1049+
1050+ // If we've decoded to a post, ensure the post is publicly visible.
1051+ if ( $ decoded_post_id ['type ' ] === 'post ' ) {
1052+ if ( $ atts ['post_id ' ] !== false && ( (int ) $ atts ['post_id ' ] !== (int ) acf_get_valid_post_id () ) && ( ! is_post_publicly_viewable ( $ decoded_post_id ['id ' ] ) ) && apply_filters ( 'acf/shortcode/prevent_access_to_fields_on_non_public_posts ' , true ) ) {
1053+ if ( is_preview () ) {
1054+ return apply_filters ( 'acf/shortcode/post_not_public_message ' , __ ( '[The ACF shortcode cannot display fields from non-public posts] ' , 'acf ' ) );
1055+ } else {
1056+ return ;
1057+ }
1058+ }
1059+ }
1060+
10341061 $ access_already_prevented = apply_filters ( 'acf/prevent_access_to_unknown_fields ' , false );
10351062 $ filter_applied = false ;
10361063
@@ -1039,10 +1066,6 @@ function acf_shortcode( $atts ) {
10391066 add_filter ( 'acf/prevent_access_to_unknown_fields ' , '__return_true ' );
10401067 }
10411068
1042- // Decode the post ID for filtering.
1043- $ post_id = acf_get_valid_post_id ( $ atts ['post_id ' ] );
1044- $ decoded_post_id = acf_decode_post_id ( $ post_id );
1045-
10461069 // Try to get the field value, ensuring any non-safe HTML is stripped from wysiwyg fields via `acf_the_content`
10471070 $ field = get_field_object ( $ atts ['field ' ], $ post_id , $ atts ['format_value ' ], true , true );
10481071 $ value = $ field ? $ field ['value ' ] : get_field ( $ atts ['field ' ], $ post_id , $ atts ['format_value ' ], true );
@@ -1053,17 +1076,9 @@ function acf_shortcode( $atts ) {
10531076 return ;
10541077 }
10551078
1056- if ( is_array ( $ value ) ) {
1057- $ value = implode ( ', ' , $ value );
1058- }
1059-
10601079 // Temporarily always get the unescaped version for action comparison.
10611080 $ unescaped_value = get_field ( $ atts ['field ' ], $ post_id , $ atts ['format_value ' ], false );
10621081
1063- if ( $ filter_applied ) {
1064- remove_filter ( 'acf/prevent_access_to_unknown_fields ' , '__return_true ' );
1065- }
1066-
10671082 // Remove the filter preventing access to unknown filters now we've got all the values.
10681083 if ( $ filter_applied ) {
10691084 remove_filter ( 'acf/prevent_access_to_unknown_fields ' , '__return_true ' );
@@ -1073,10 +1088,14 @@ function acf_shortcode( $atts ) {
10731088 $ unescaped_value = implode ( ', ' , $ unescaped_value );
10741089 }
10751090
1091+ if ( ! is_scalar ( $ unescaped_value ) ) {
1092+ $ unescaped_value = false ;
1093+ }
1094+
10761095 // Handle getting the unescaped version if we're allowed unsafe html.
10771096 if ( apply_filters ( 'acf/shortcode/allow_unsafe_html ' , false , $ atts , $ field_type , $ field ) ) {
10781097 $ value = $ unescaped_value ;
1079- } elseif ( (string ) $ value !== (string ) $ unescaped_value ) {
1098+ } elseif ( $ unescaped_value !== false && (string ) $ value !== (string ) $ unescaped_value ) {
10801099 do_action ( 'acf/removed_unsafe_html ' , __FUNCTION__ , $ atts ['field ' ], $ field , $ post_id );
10811100 }
10821101
0 commit comments