-
Notifications
You must be signed in to change notification settings - Fork 385
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -187,7 +187,6 @@ public static function register_hooks() { | |
add_action( 'wp_print_styles', array( __CLASS__, 'print_amp_styles' ), 0 ); // Print boilerplate before theme and plugin stylesheets. | ||
add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueue_amp_default_styles' ), 9 ); | ||
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 ); | ||
|
@@ -829,6 +828,13 @@ protected static function ensure_required_markup( DOMDocument $dom ) { | |
) ); | ||
$head->insertBefore( $meta_viewport, $meta_charset->nextSibling ); | ||
} | ||
if ( ! self::schema_org_present( $dom ) ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As noted below, I think this logic should be inlined (for now) as it is done above for |
||
$script = $dom->createElement( 'script', wp_json_encode( amp_get_schemaorg_metadata() ) ); | ||
AMP_DOM_Utils::add_attributes_to_node( $script, array( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since you're using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also true. Corrected it. |
||
'type' => 'application/ld+json', | ||
) ); | ||
$head->appendChild( $script ); | ||
} | ||
|
||
// Ensure rel=canonical link. | ||
$rel_canonical = null; | ||
|
@@ -1007,4 +1013,20 @@ public static function prepare_response( $response, $args = array() ) { | |
|
||
return $response; | ||
} | ||
|
||
/** | ||
* Checks if there is already a schema.org script in the head. | ||
* | ||
* @param DOMDocument $dom Representation of the document. | ||
* @return bool | ||
*/ | ||
public static function schema_org_present( DOMDocument $dom ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's rename this to If you changed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added your suggestions. I also did the test for ensure_required_markup, however, I only made tests for the schema.org script part. Could you use tests for the entire function? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this method needs more tests, so if that's something you want to add that would be great. |
||
$head = $dom->getElementsByTagName( 'head' )->item( 0 ); | ||
foreach ( $head->getElementsByTagName( 'script' ) as $script ) { | ||
if ( 1 === preg_match( '/{"@context":"https?:[\\\]\/[\\\]\/schema\.org/', $script->nodeValue ) ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Before looking at the content of the element ( Also, I think the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed the integer comparison and added your suggestion, indeed it makes it more precise. |
||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -330,4 +330,49 @@ 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 schema_org_present(). | ||
* | ||
* @dataProvider get_script_data | ||
* @covers AMP_Theme_Support::schema_org_present() | ||
* @param string $script The value of the script. | ||
* @param boolean $expected The expected result. | ||
*/ | ||
public function test_schema_org_present( $script, $expected ) { | ||
$page = '<html><head><script>%s</script></head><body>Test</body></html>'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very good observation, I added the type attribute on the new tests for ensure_required_markup() |
||
$dom = new DOMDocument(); | ||
$dom->loadHTML( sprintf( $page, $script ) ); | ||
$this->assertEquals( $expected, AMP_Theme_Support::schema_org_present( $dom ) ); | ||
} | ||
|
||
/** | ||
* Data provider for test_schema_org_present(). | ||
* | ||
* @return array | ||
*/ | ||
public function get_script_data() { | ||
return array( | ||
'string_schema_value' => array( | ||
'schema.org', | ||
false, | ||
), | ||
'string_not_schema' => array( | ||
'somethinghere.org', | ||
false, | ||
), | ||
'json_schema_present_https' => array( | ||
wp_json_encode( array( '@context' => 'https://schema.org' ) ), | ||
true, | ||
), | ||
'json_schema_present_http' => array( | ||
wp_json_encode( array( '@context' => 'http://schema.org' ) ), | ||
true, | ||
), | ||
'json_schema_not_present' => array( | ||
wp_json_encode( array( '@anothercontext' => 'https://schema.org' ) ), | ||
false, | ||
), | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should not be removed. The legacy post templates should remain as-is, I believe. The
ensure_required_markup
method below does not apply for the legacy post template actions.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @westonruter
Thanks for your feedback. I corrected that mistake and restored the line.