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 @@ -119,6 +119,21 @@ protected function build_type( WP_Post $post, string $parsely_type ): void {
$this->metadata['@type'] = $type;
}

/**
* Populates the custom metadata field with the current post's/page's ID.
*
* @since 3.16.0
*
* @param WP_Post $post The post/page for which to populate the field.
*/
protected function build_post_id( WP_Post $post ): void {
$this->metadata['custom_metadata'] = wp_json_encode(
array(
'wpParselyPostID' => $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 @@ -403,7 +403,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":"{\\"wpParselyPostID\\":' . $post_id . '}"}</script>' .
'<meta name="parsely-metadata" content="{&quot;wpParselyPostID&quot;:' . $post_id . '}" />';
self::assertSame( $expected, $meta_string );
}

Expand Down Expand Up @@ -471,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;wpParselyPostID&quot;:' . $post_id . '}" />';
self::assertSame( $expected, $meta_string );
}

Expand Down
Loading