Last active
February 3, 2021 20:50
-
-
Save westonruter/4ff8545e653efeeafe5b994c530a568e to your computer and use it in GitHub Desktop.
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 | |
/** | |
* AMP Validation Error Logger plugin initialization file. | |
* | |
* @package AMP_Validation_Error_Logger | |
* @author Weston Ruter, Google | |
* @license GPL-2.0-or-later | |
* @copyright 2019 Google Inc. | |
* | |
* @wordpress-plugin | |
* Plugin Name: AMP Validation Error Logger | |
* Plugin URI: https://gist.github.com/westonruter/4ff8545e653efeeafe5b994c530a568e | |
* Description: Append AMP Validation errors to the body as comments and if WP_DEBUG_LOG is enabled add AMP validation errors to the PHP error log. | |
* Version: 0.2 | |
* Author: Weston Ruter, Google | |
* Author URI: https://weston.ruter.net/ | |
* License: GNU General Public License v2 (or later) | |
* License URI: http://www.gnu.org/licenses/gpl-2.0.html | |
* Gist Plugin URI: https://gist.github.com/westonruter/4ff8545e653efeeafe5b994c530a568e | |
*/ | |
namespace AMP_Validation_Error_Logger; | |
use AMP_Validation_Error_Taxonomy; | |
use AMP_Validation_Manager; | |
use DOMDocument; | |
use DOMNode; | |
use WP_Term; | |
add_filter( | |
'amp_validation_error', | |
function( $validation_error, $args ) { | |
$term_data = AMP_Validation_Error_Taxonomy::prepare_validation_error_taxonomy_term( $validation_error ); | |
$term = AMP_Validation_Error_Taxonomy::get_term( $term_data['slug'] ); | |
$slug = $term_data['slug']; | |
if ( $term instanceof WP_Term && in_array( $term->term_group, [ 0, 1, 2, 3 ], true ) ) { | |
$term_status = $term->term_group; | |
} else { | |
$term_status = AMP_Validation_Manager::is_sanitization_auto_accepted( $validation_error ) ? AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_NEW_ACCEPTED_STATUS : AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_NEW_REJECTED_STATUS; | |
} | |
$removed = (bool) ( $term_status & 1 ); | |
$reviewed = (bool) ( $term_status & 2 ); | |
// Append validation errors as HTML comments at the end of the document. | |
if ( isset( $args['node'] ) && $args['node'] instanceof DOMNode && $args['node']->ownerDocument instanceof DOMDocument ) { | |
/** | |
* Document. | |
* | |
* @var DOMDocument $dom | |
*/ | |
$dom = $args['node']->ownerDocument; | |
$dom->appendChild( $dom->createTextNode( "\n" ) ); | |
$comment = $dom->createComment( | |
sprintf( | |
'amp_validation_error(%s):%s', | |
wp_json_encode( compact( 'removed', 'reviewed', 'slug' ) ), | |
wp_json_encode( $validation_error, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT ) | |
) | |
); | |
$dom->appendChild( $comment ); | |
} | |
// Also add the errors to the error log if WP_DEBUG_LOG is enabled. | |
if ( defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) { | |
error_log( // phpcs:ignore | |
sprintf( | |
'amp_validation_error(%s)@%s: %s', | |
wp_json_encode( compact( 'removed', 'reviewed', 'slug' ) ), | |
remove_query_arg( [ 'amp_validate', 'amp_cache_bust' ], wp_unslash( $_SERVER['REQUEST_URI'] ) ), | |
wp_json_encode( $validation_error, JSON_UNESCAPED_SLASHES ) | |
) | |
); | |
} | |
return $validation_error; | |
}, | |
PHP_INT_MAX, | |
2 | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Installation instructions: https://gist.github.com/westonruter/6110fbc4bef0c4b8c021a112012f7e9c