Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent schema.org duplicates #992

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 14 additions & 3 deletions includes/class-amp-theme-support.php
Expand Up @@ -220,7 +220,6 @@ public static function add_hooks() {
*/
add_action( 'wp_print_styles', array( __CLASS__, 'print_amp_styles' ), 0 ); // Print boilerplate before theme and plugin stylesheets.
add_action( 'wp_head', 'amp_add_generator_metadata', 20 );
add_action( 'wp_head', 'amp_print_schemaorg_metadata' );

if ( is_customize_preview() ) {
add_action( 'wp_enqueue_scripts', array( __CLASS__, 'dequeue_customize_preview_scripts' ), 1000 );
Expand Down Expand Up @@ -754,7 +753,7 @@ public static function print_amp_styles() {
*
* @param DOMDocument $dom Doc.
*/
protected static function ensure_required_markup( DOMDocument $dom ) {
public static function ensure_required_markup( DOMDocument $dom ) {
$xpath = new DOMXPath( $dom );

// First ensure the mandatory amp attribute is present on the html element, as otherwise it will be stripped entirely.
Expand Down Expand Up @@ -795,7 +794,19 @@ protected static function ensure_required_markup( DOMDocument $dom ) {
) );
$head->insertBefore( $meta_viewport, $meta_charset->nextSibling );
}

// Prevent schema.org duplicates.
$has_schema_org_metadata = false;
foreach ( $head->getElementsByTagName( 'script' ) as $script ) {
if ( 'application/ld+json' === $script->getAttribute( 'type' ) && false !== strpos( $script->nodeValue, 'schema.org' ) ) {
$has_schema_org_metadata = true;
break;
}
}
if ( ! $has_schema_org_metadata ) {
$script = $dom->createElement( 'script', wp_json_encode( amp_get_schemaorg_metadata() ) );
$script->setAttribute( 'type', 'application/ld+json' );
$head->appendChild( $script );
}
// Ensure rel=canonical link.
$rel_canonical = null;
foreach ( $head->getElementsByTagName( 'link' ) as $link ) {
Expand Down
41 changes: 41 additions & 0 deletions tests/test-class-amp-theme-support.php
Expand Up @@ -330,4 +330,45 @@ public function test_handle_xhr_request() {
AMP_Theme_Support::handle_xhr_request();
$this->assertContains( 'AMP-Access-Control-Allow-Source-Origin: https://example.org', xdebug_get_headers() );
}

/**
* Test ensure_required_markup().
*
* @dataProvider get_script_data
* @covers AMP_Theme_Support::ensure_required_markup()
* @param string $script The value of the script.
* @param boolean $expected The expected result.
*/
public function test_ensure_required_markup( $script, $expected ) {
$page = '<html><head><script type="application/ld+json">%s</script></head><body>Test</body></html>';
$dom = new DOMDocument();
$dom->loadHTML( sprintf( $page, $script ) );
AMP_Theme_Support::ensure_required_markup( $dom );
$this->assertEquals( $expected, substr_count( $dom->saveHTML(), 'schema.org' ) );
}
/**
* Data provider for test_ensure_required_markup.
*
* @return array
*/
public function get_script_data() {
return array(
'schema_org_not_present' => array(
'',
1,
),
'schema_org_present' => array(
wp_json_encode( array( '@context' => 'http://schema.org' ) ),
1,
),
'schema_org_output_not_escaped' => array(
'{"@context":"http://schema.org"',
1,
),
'schema_org_another_key' => array(
wp_json_encode( array( '@anothercontext' => 'https://schema.org' ) ),
1,
),
);
}
}