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
5 changes: 3 additions & 2 deletions tests/Integration/Endpoints/RestMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,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">{"@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>';
sanmai marked this conversation as resolved.
Show resolved Hide resolved
self::assertSame( $expected, $meta_string );
}

Expand Down Expand Up @@ -472,7 +472,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
Loading