Skip to content

Commit

Permalink
Add identifier to JSON-LD metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
acicovic committed Dec 10, 2024
1 parent f82a774 commit 74f3704
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/Metadata/class-metadata-renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function render_metadata( string $meta_type ): void {

// Insert JSON-LD or repeated metas.
if ( 'json_ld' === $meta_type ) {
echo '<script type="application/ld+json">' . wp_json_encode( $metadata ) . '</script>';
echo '<script type="application/ld+json" class="wp-parsely-metadata">' . wp_json_encode( $metadata ) . '</script>';
} else {
// Assume `meta_type` is `repeated_metas`.
$parsely_post_type = $this->parsely->convert_jsonld_to_parsely_type( $metadata['@type'] ?? '' );
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Endpoints/RestMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ public function test_get_rendered_meta_json_ld(): void {
$this->go_to( (string) $this->get_permalink( $post_id ) );

$meta_string = self::$rest->get_rendered_meta( 'json_ld' );
$expected = '<script type="application/ld+json">{"@context":"https:\/\/schema.org","@type":"NewsArticle","headline":"My test_get_rendered_meta_json_ld title","url":"http:\/\/example.org\/?p=' . $post_id . '","mainEntityOfPage":{"@type":"WebPage","@id":"http:\/\/example.org\/?p=' . $post_id . '"},"thumbnailUrl":"","image":{"@type":"ImageObject","url":""},"articleSection":"Uncategorized","author":[],"creator":[],"publisher":{"@type":"Organization","name":"Test Blog","logo":""},"keywords":[],"dateCreated":"' . $date . '","datePublished":"' . $date . '","dateModified":"' . $date . '"}</script>';
$expected = '<script type="application/ld+json" class="wp-parsely-metadata">{"@context":"https:\/\/schema.org","@type":"NewsArticle","headline":"My test_get_rendered_meta_json_ld title","url":"http:\/\/example.org\/?p=' . $post_id . '","mainEntityOfPage":{"@type":"WebPage","@id":"http:\/\/example.org\/?p=' . $post_id . '"},"thumbnailUrl":"","image":{"@type":"ImageObject","url":""},"articleSection":"Uncategorized","author":[],"creator":[],"publisher":{"@type":"Organization","name":"Test Blog","logo":""},"keywords":[],"dateCreated":"' . $date . '","datePublished":"' . $date . '","dateModified":"' . $date . '"}</script>';
self::assertSame( $expected, $meta_string );
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/UI/MetadataRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function test_render_metadata_json_ld(): void {
*/
$out = ob_get_clean();

self::assertStringContainsString( '<script type="application/ld+json">', $out );
self::assertStringContainsString( '<script type="application/ld+json" class="wp-parsely-metadata">', $out );
self::assertStringContainsString( '</script>', $out );
}

Expand Down
12 changes: 6 additions & 6 deletions tests/e2e/specs/front-end-metadata.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ test.describe( 'Front end metadata insertion', () => {

const content = await page.content();

expect( content ).toContain( '<script type="application/ld+json">{"@context":"https:\\/\\/schema.org","@type":"WebPage","headline":"wp-parsely","url":"http:\\/\\/localhost:8889"}</script>' );
expect( content ).toContain( '<script type="application/ld+json" class="wp-parsely-metadata">{"@context":"https:\\/\\/schema.org","@type":"WebPage","headline":"wp-parsely","url":"http:\\/\\/localhost:8889"}</script>' );
expect( content ).not.toContain( '<meta name="parsely-title" ' );
} );

Expand All @@ -67,7 +67,7 @@ test.describe( 'Front end metadata insertion', () => {

const content = await page.content();

expect( content ).toContain( '<script type="application/ld+json">' );
expect( content ).toContain( '<script type="application/ld+json" class="wp-parsely-metadata">' );
expect( content ).toContain( '{"@context":"https:\\/\\/schema.org","@type":"NewsArticle","headline":"Hello world!","url":"http:\\/\\/localhost:8889\\/?p=1","mainEntityOfPage":{"@type":"WebPage","@id":"http:\\/\\/localhost:8889\\/?p=1"},"thumbnailUrl":"","image":{"@type":"ImageObject","url":""},"articleSection":"Uncategorized","author":[{"@type":"Person","name":"admin"}],"creator":["admin"],"publisher":{"@type":"Organization","name":"wp-parsely","logo":""},"keywords":[],"' );
expect( content ).not.toContain( '<meta name="parsely-title" ' );
} );
Expand All @@ -82,7 +82,7 @@ test.describe( 'Front end metadata insertion', () => {

const content = await page.content();

expect( content ).toContain( '<script type="application/ld+json">' );
expect( content ).toContain( '<script type="application/ld+json" class="wp-parsely-metadata">' );
expect( content ).toContain( '{"@context":"https:\\/\\/schema.org","@type":"WebPage","headline":"Sample Page","url":"http:\\/\\/localhost:8889\\/?page_id=2","mainEntityOfPage":{"@type":"WebPage","@id":"http:\\/\\/localhost:8889\\/?page_id=2"},"thumbnailUrl":"","image":{"@type":"ImageObject","url":""},"articleSection":"Uncategorized","author":[{"@type":"Person","name":"admin"}],"creator":["admin"],"publisher":{"@type":"Organization","name":"wp-parsely","logo":""},"keywords":[],"' );
expect( content ).not.toContain( '<meta name="parsely-title" ' );
} );
Expand All @@ -107,7 +107,7 @@ test.describe( 'Front end metadata insertion', () => {
expect( content ).not.toMatch( /<meta name="parsely-pub-date" content=".*Z">/ );
expect( content ).not.toContain( '<meta name="parsely-section" content="Uncategorized">' );
expect( content ).not.toContain( '<meta name="parsely-author" content="admin">' );
expect( content ).not.toContain( '<script type="application/ld+json">' );
expect( content ).not.toContain( '<script type="application/ld+json" class="wp-parsely-metadata">' );
} );

/**
Expand All @@ -126,7 +126,7 @@ test.describe( 'Front end metadata insertion', () => {
expect( content ).toMatch( /<meta name="parsely-pub-date" content=".*Z">/ );
expect( content ).toContain( '<meta name="parsely-section" content="Uncategorized">' );
expect( content ).toContain( '<meta name="parsely-author" content="admin">' );
expect( content ).not.toContain( '<script type="application/ld+json">' );
expect( content ).not.toContain( '<script type="application/ld+json" class="wp-parsely-metadata">' );
} );

/**
Expand All @@ -145,7 +145,7 @@ test.describe( 'Front end metadata insertion', () => {
expect( content ).toMatch( /<meta name="parsely-pub-date" content=".*Z">/ );
expect( content ).toContain( '<meta name="parsely-section" content="Uncategorized">' );
expect( content ).toContain( '<meta name="parsely-author" content="admin">' );
expect( content ).not.toContain( '<script type="application/ld+json">' );
expect( content ).not.toContain( '<script type="application/ld+json" class="wp-parsely-metadata">' );
} );
} );

Expand Down

0 comments on commit 74f3704

Please sign in to comment.