Last active
September 23, 2020 18:37
-
-
Save westonruter/d9df78faed75297f9ca6b6be36d37ab2 to your computer and use it in GitHub Desktop.
UPDATE: This is now obsolete. The fix to the AMP validator has been deployed.
This file contains 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 JSON Validation Error Workaround | |
* | |
* @link https://gist.github.com/westonruter/d9df78faed75297f9ca6b6be36d37ab2 | |
* @package AMP_JSON_Validation_Error_Workaround | |
* @license GPL-2.0+ | |
* | |
* @wordpress-plugin | |
* Plugin Name: AMP JSON Validation Error Workaround | |
* Description: Prevent AMP validation warnings from being reported due to newline characters appearing in JSON (which should not be invalid since such characters are valid whitespace). | |
* Plugin URI: https://gist.github.com/westonruter/d9df78faed75297f9ca6b6be36d37ab2 | |
* Author: Weston Ruter | |
* Author URI: https://weston.ruter.net | |
* Version: 0.1.2 | |
* License: GPLv2 or later | |
* Gist Plugin URI: https://gist.github.com/westonruter/d9df78faed75297f9ca6b6be36d37ab2 | |
*/ | |
namespace AMP_JSON_Validation_Error_Workaround; | |
/** | |
* Filter sanitizers. | |
* | |
* @param array $sanitizers Sanitizers. | |
* @return array Sanitizers. | |
*/ | |
function filter_sanitizers( $sanitizers ) { | |
require_once __DIR__ . '/class-json-reserializing-sanitizer.php'; | |
$sanitizers[ __NAMESPACE__ . '\JSON_Reserializing_Sanitizer' ] = []; | |
return $sanitizers; | |
} | |
add_filter( 'amp_content_sanitizers', __NAMESPACE__ . '\filter_sanitizers' ); |
This file contains 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 JSON Validation Error Workaround | |
* | |
* @package AMP_JSON_Validation_Error_Workaround | |
*/ | |
namespace AMP_JSON_Validation_Error_Workaround; | |
use AMP_Base_Sanitizer; | |
use DOMXPath; | |
use DOMElement; | |
/** | |
* Re-serialize JSON to remove whitespace. | |
*/ | |
class JSON_Reserializing_Sanitizer extends AMP_Base_Sanitizer { | |
/** | |
* Sanitize. | |
*/ | |
public function sanitize() { | |
$xpath = new DOMXPath( $this->dom ); | |
/** | |
* Script element. | |
* | |
* @var DOMElement $script | |
*/ | |
foreach ( $xpath->query( '//script[ contains( @type, "json" ) ]' ) as $script ) { | |
if ( false !== stripos( $script->textContent, '<\/script>' ) ) { | |
// Prevent JSON injection possibility. | |
continue; | |
} | |
$data = json_decode( $script->textContent, true ); | |
if ( json_last_error() ) { | |
continue; | |
} | |
while ( $script->firstChild ) { | |
$script->removeChild( $script->firstChild ); | |
} | |
$script->appendChild( $this->dom->createTextNode( wp_json_encode( $data, JSON_UNESCAPED_SLASHES ) ) ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is now obsolete as the fix has been deployed. Feel free to uninstall this plugin.