Forked from lgladdy/acf_enable_detailed_escape_logging_to_php_error_log.php
Created
January 30, 2024 17:23
-
-
Save andreicnegrea/e5c9d0d0b324780c5308dc873bcf3cab to your computer and use it in GitHub Desktop.
Always log the post ID and value of a HTML escaped field
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| add_action( 'acf/will_remove_unsafe_html', 'acf_enable_detailed_escape_logging_to_php_error_log', 10, 4 ); | |
| add_action( 'acf/removed_unsafe_html', 'acf_enable_detailed_escape_logging_to_php_error_log', 10, 4 ); | |
| function acf_enable_detailed_escape_logging_to_php_error_log( $function, $selector, $field_object, $post_id ) { | |
| if ( $function === 'the_sub_field' ) { | |
| $field = get_sub_field_object( $selector, true ); | |
| $value = ( is_array( $field ) && isset( $field['value'] ) ) ? $field['value'] : false; | |
| } else { | |
| $value = get_field( $selector, $post_id ); | |
| } | |
| if ( is_array( $value ) ) { | |
| $value = implode( ', ', $value ); | |
| } | |
| $field_type = is_array( $field_object ) && isset( $field_object['type'] ) ? $field_object['type'] : 'text'; | |
| $field_type_escapes_html = acf_field_type_supports( $field_type, 'escaping_html' ); | |
| if ( $field_type_escapes_html ) { | |
| if ( $function === 'the_sub_field' ) { | |
| $field = get_sub_field_object( $selector, true, true, true ); | |
| $new_value = ( is_array( $field ) && isset( $field['value'] ) ) ? $field['value'] : false; | |
| } else { | |
| $new_value = get_field( $selector, $post_id, true, true ); | |
| } | |
| if ( is_array( $new_value ) ) { | |
| $new_value = implode( ', ', $new_value ); | |
| } | |
| } else { | |
| $new_value = acf_esc_html( $value ); | |
| } | |
| if ( empty( $post_id ) ) { | |
| $post_id = acf_get_valid_post_id( $post_id ); | |
| } | |
| if ( $function === 'acf_shortcode' ) { | |
| $template = get_page_template() . ' (likely not relevant for shortcode)'; | |
| } else { | |
| $template = get_page_template(); | |
| } | |
| error_log( | |
| '***ACF HTML Escaping Debug***' . PHP_EOL . | |
| 'HTML modification detected the value of ' . $selector . ' on post ID ' . $post_id . ' via ' . $function . PHP_EOL . | |
| 'Raw Value: ' . var_export( $value, true ) . PHP_EOL . | |
| 'Escaped Value: ' . var_export( $new_value, true ) . PHP_EOL . | |
| 'Template: ' . $template | |
| ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment