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

Include post ID in custom metadata #2511

Open
wants to merge 17 commits into
base: develop
Choose a base branch
from
15 changes: 15 additions & 0 deletions src/Metadata/class-metadata-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,21 @@ protected function build_type( WP_Post $post, string $parsely_type ): void {
$this->metadata['@type'] = $type;
}

/**
* Populates all the fields related to post ID.
*
* @param WP_Post $post The post/page for which to populate the field.
*
* @since 3.0.x
*/
sanmai marked this conversation as resolved.
Show resolved Hide resolved
protected function build_post_id( WP_Post $post ): void {
$this->metadata['custom_metadata'] = wp_json_encode(
array(
'postID' => $post->ID,
)
);
}

/**
* Populates the mainEntityOfPage field in the metadata object.
*
Expand Down
1 change: 1 addition & 0 deletions src/Metadata/class-post-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public function get_metadata(): array {
$this->build_publisher();
$this->build_keywords( $this->post );
$this->build_metadata_post_times( $this->post );
$this->build_post_id( $this->post );

return $this->metadata;
}
Expand Down
6 changes: 4 additions & 2 deletions tests/Integration/Endpoints/RestMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,8 @@ 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">{"@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 . '","custom_metadata":"{\\"postID\\":' . $post_id . '}"}</script>' .
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need the \\ in {\\"postID\\"?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most definitely, even the docs call for it:

Escape double quotes in JSON item values. Double quotes should be escaped with a backslash symbol like this: \".

And then they provide clearly broken JSON in the example 🤷

https://docs.parse.ly/api-custom-metadata/#h-json-ld-configuration

'<meta name="parsely-metadata" content="{&quot;postID&quot;:' . $post_id . '}" />';
self::assertSame( $expected, $meta_string );
}

Expand Down Expand Up @@ -472,7 +473,8 @@ public function test_get_rendered_repeated_metas(): void {
<meta name="parsely-link" content="http://example.org/?p=' . $post_id . '" />
<meta name="parsely-type" content="post" />
<meta name="parsely-pub-date" content="' . $date . '" />
<meta name="parsely-section" content="Uncategorized" />';
<meta name="parsely-section" content="Uncategorized" />
<meta name="parsely-metadata" content="{&quot;postID&quot;:' . $post_id . '}" />';
self::assertSame( $expected, $meta_string );
}

Expand Down