|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Plugin Name: ACF Detailed Escaping Debug Logger |
| 4 | + * Plugin URI: https://github.com/lgladdy/acf-escaping-debug-plugin/ |
| 5 | + * Description: This plugin will add a detailed error log message whenever ACF has removed or modified (or will modify, in ACF 6.2.7) potentially unsafe HTML on output when using the ACF shortcode, the_field or the_sub_field. |
| 6 | + * Version: 1.0.0 |
| 7 | + * Author: Liam Gladdy |
| 8 | + */ |
| 9 | + |
| 10 | +add_action( 'acf/will_remove_unsafe_html', 'acf_enable_detailed_escape_logging_to_php_error_log', 10, 4 ); |
| 11 | +add_action( 'acf/removed_unsafe_html', 'acf_enable_detailed_escape_logging_to_php_error_log', 10, 4 ); |
| 12 | +function acf_enable_detailed_escape_logging_to_php_error_log( $function, $selector, $field_object, $post_id ) { |
| 13 | + if ( $function === 'the_sub_field' ) { |
| 14 | + $field = get_sub_field_object( $selector, true ); |
| 15 | + $value = ( is_array( $field ) && isset( $field['value'] ) ) ? $field['value'] : false; |
| 16 | + } else { |
| 17 | + $value = get_field( $selector, $post_id ); |
| 18 | + } |
| 19 | + if ( is_array( $value ) ) { |
| 20 | + $value = implode( ', ', $value ); |
| 21 | + } |
| 22 | + |
| 23 | + $field_type = is_array( $field_object ) && isset( $field_object['type'] ) ? $field_object['type'] : 'text'; |
| 24 | + $field_type_escapes_html = acf_field_type_supports( $field_type, 'escaping_html' ); |
| 25 | + |
| 26 | + if ( $field_type_escapes_html ) { |
| 27 | + if ( $function === 'the_sub_field' ) { |
| 28 | + $field = get_sub_field_object( $selector, true, true, true ); |
| 29 | + $new_value = ( is_array( $field ) && isset( $field['value'] ) ) ? $field['value'] : false; |
| 30 | + } else { |
| 31 | + $new_value = get_field( $selector, $post_id, true, true ); |
| 32 | + } |
| 33 | + if ( is_array( $new_value ) ) { |
| 34 | + $new_value = implode( ', ', $new_value ); |
| 35 | + } |
| 36 | + } else { |
| 37 | + $new_value = acf_esc_html( $value ); |
| 38 | + } |
| 39 | + |
| 40 | + if ( empty( $post_id ) ) { |
| 41 | + $post_id = acf_get_valid_post_id( $post_id ); |
| 42 | + } |
| 43 | + |
| 44 | + if ( $function === 'acf_shortcode' ) { |
| 45 | + $template = get_page_template() . ' (likely not relevant for shortcode)'; |
| 46 | + } else { |
| 47 | + $template = get_page_template(); |
| 48 | + } |
| 49 | + |
| 50 | + error_log( |
| 51 | + '***ACF HTML Escaping Debug***' . PHP_EOL . |
| 52 | + 'HTML modification detected the value of ' . $selector . ' on post ID ' . $post_id . ' via ' . $function . PHP_EOL . |
| 53 | + 'Raw Value: ' . var_export( $value, true ) . PHP_EOL . |
| 54 | + 'Escaped Value: ' . var_export( $new_value, true ) . PHP_EOL . |
| 55 | + 'Template: ' . $template |
| 56 | + ); |
| 57 | +} |
0 commit comments