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

Conversation

sanmai
Copy link

@sanmai sanmai commented May 30, 2024

Description

Let's add a post ID to the custom metadata.

https://docs.parse.ly/api-custom-metadata/

CC @medeiros-amanda @rebeccahum @robersongomes

Motivation and context

Injecting post IDs will let Parsely link back to the edit form of the post.

As compared to LD-JSON (#2510), we can expect that there are no SEO implications.

How has this been tested?

LD-JSON works as usual.

Screenshots (if appropriate)

Summary by CodeRabbit

  • New Features

    • Added a custom metadata field for the current post's ID, enhancing metadata handling.
  • Bug Fixes

    • Improved metadata accuracy by including the new custom metadata field in the output.
  • Tests

    • Updated tests to validate the inclusion of the new custom metadata in JSON-LD and repeated meta formats.

@sanmai sanmai requested a review from a team as a code owner May 30, 2024 15:11
Copy link
Contributor

coderabbitai bot commented May 30, 2024

📝 Walkthrough
📝 Walkthrough

Walkthrough

The recent changes introduce a new method build_post_id in the Metadata_Builder class to populate custom metadata related to post IDs. This method encodes the post ID into a JSON format and stores it in the metadata array. Additionally, the tests in RestMetadataTest.php have been updated to include checks for the new custom metadata in JSON-LD scripts and repeated meta tags.

Changes

Files Change Summary
src/Metadata/class-metadata-builder.php Added build_post_id method to populate custom metadata related to post ID.
tests/Integration/Endpoints/RestMetadataTest.php Updated tests to include custom metadata in JSON-LD scripts and additional metadata in repeated meta tags.

Sequence Diagram(s) (Beta)

sequenceDiagram
    participant Client
    participant MetadataBuilder
    participant WP_Post

    Client->>MetadataBuilder: build_post_id(WP_Post)
    MetadataBuilder->>WP_Post: Access post data
    WP_Post-->>MetadataBuilder: Return post ID
    MetadataBuilder-->>Client: Return custom metadata with post ID
Loading

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 762531b and b86645c.

Files selected for processing (2)
  • src/Metadata/class-metadata-builder.php (1 hunks)
  • src/Metadata/class-post-builder.php (1 hunks)
Additional context used
Path-based instructions (2)
src/Metadata/class-post-builder.php (1)

Pattern **/*.{html,php}: "Perform a detailed review of the provided code with following key aspects in mind:

  • Review the HTML and PHP code to ensure it is well-structured and adheres to best practices.
  • Ensure the code follows WordPress coding standards and is well-documented.
  • Confirm the code is secure and free from vulnerabilities.
  • Optimize the code for performance, removing any unnecessary elements.
  • Validate comments for accuracy, currency, and adherence to WordPress coding standards.
  • Ensure each line comment concludes with a period.
  • Verify code compatibility with the latest version of WordPress, avoiding deprecated functions or features."
src/Metadata/class-metadata-builder.php (1)

Pattern **/*.{html,php}: "Perform a detailed review of the provided code with following key aspects in mind:

  • Review the HTML and PHP code to ensure it is well-structured and adheres to best practices.
  • Ensure the code follows WordPress coding standards and is well-documented.
  • Confirm the code is secure and free from vulnerabilities.
  • Optimize the code for performance, removing any unnecessary elements.
  • Validate comments for accuracy, currency, and adherence to WordPress coding standards.
  • Ensure each line comment concludes with a period.
  • Verify code compatibility with the latest version of WordPress, avoiding deprecated functions or features."
Additional comments not posted (2)
src/Metadata/class-post-builder.php (1)

63-63: The integration of build_post_id into get_metadata looks good.

Verification successful

The build_post_id method is defined in class-metadata-builder.php and called in class-post-builder.php. Let's verify the implementation of build_post_id to ensure it aligns with the intended functionality.


The ast-grep tool does not support PHP, which caused an error. Let's use rg to search for the build_post_id method definition and extract its implementation.


The build_post_id method populates the custom_metadata field with the post ID. Let's verify that the custom_metadata field is correctly integrated and used in the metadata.


The custom_metadata field is used in class-metadata.php and class-metadata-renderer.php. Let's verify the usage in class-metadata-renderer.php to ensure proper handling and rendering.


The custom_metadata field is correctly handled in class-metadata-renderer.php, where it is checked for existence and then rendered as a meta tag. This confirms that the integration of the build_post_id method and the custom_metadata field is properly implemented and does not conflict with other metadata fields.

  • class-metadata-renderer.php:
    • Properly checks for the existence of custom_metadata.
    • Renders custom_metadata as a meta tag.
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that the `build_post_id` method integrates well with other metadata components.

# Test: Search for the method usage in related files. Expect: Proper integration without conflicts.
rg --type php $'build_post_id'

Length of output: 203



Script:

#!/bin/bash
# Description: Verify the implementation of the `build_post_id` method in `class-metadata-builder.php`.

# Extract the implementation of the `build_post_id` method.
ast-grep --lang php --pattern $'protected function build_post_id( WP_Post $post ): void {
  $$$
}' --json

Length of output: 213



Script:

#!/bin/bash
# Description: Verify the implementation of the `build_post_id` method in `class-metadata-builder.php`.

# Search for the `build_post_id` method definition and display its implementation.
rg --type php --context 10 'protected function build_post_id' src/Metadata/class-metadata-builder.php

Length of output: 538



Script:

#!/bin/bash
# Description: Verify the usage of the `custom_metadata` field in the codebase.

# Search for the usage of the `custom_metadata` field in the codebase.
rg --type php 'custom_metadata'

Length of output: 402



Script:

#!/bin/bash
# Description: Verify the implementation of `custom_metadata` handling in `class-metadata-renderer.php`.

# Extract the relevant lines from `class-metadata-renderer.php` to review the handling of `custom_metadata`.
rg --type php --context 10 'custom_metadata' src/UI/class-metadata-renderer.php

Length of output: 816

src/Metadata/class-metadata-builder.php (1)

123-136: The implementation of build_post_id in Metadata_Builder is secure and efficient.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between b86645c and 1455961.

Files selected for processing (1)
  • tests/Integration/Endpoints/RestMetadataTest.php (2 hunks)
Additional context used
Path-based instructions (1)
tests/Integration/Endpoints/RestMetadataTest.php (1)

Pattern **/*.{html,php}: "Perform a detailed review of the provided code with following key aspects in mind:

  • Review the HTML and PHP code to ensure it is well-structured and adheres to best practices.
  • Ensure the code follows WordPress coding standards and is well-documented.
  • Confirm the code is secure and free from vulnerabilities.
  • Optimize the code for performance, removing any unnecessary elements.
  • Validate comments for accuracy, currency, and adherence to WordPress coding standards.
  • Ensure each line comment concludes with a period.
  • Verify code compatibility with the latest version of WordPress, avoiding deprecated functions or features."
Additional comments not posted (1)
tests/Integration/Endpoints/RestMetadataTest.php (1)

475-476: Correct implementation of repeated meta tags.

The test method test_get_rendered_repeated_metas correctly checks the inclusion of additional metadata in the meta tags, ensuring that the custom metadata is properly formatted and included.

tests/Integration/Endpoints/RestMetadataTest.php Outdated Show resolved Hide resolved
@sanmai sanmai marked this pull request as draft June 5, 2024 01:39
@sanmai sanmai marked this pull request as ready for review June 5, 2024 04:18
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 1455961 and f38a201.

Files selected for processing (1)
  • tests/Integration/Endpoints/RestMetadataTest.php (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • tests/Integration/Endpoints/RestMetadataTest.php

@acicovic acicovic added this to the 3.16.0 milestone Jun 5, 2024
@acicovic acicovic changed the title Add post ID into custom metadata Include post ID in metadata Jun 5, 2024
@acicovic acicovic changed the title Include post ID in metadata Include post ID in custom metadata Jun 5, 2024
Copy link
Collaborator

@acicovic acicovic left a comment

Choose a reason for hiding this comment

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

Thanks for working on this! Left a suggestion and a question 🙂

src/Metadata/class-metadata-builder.php Outdated Show resolved Hide resolved
@@ -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

@sanmai sanmai requested a review from acicovic June 6, 2024 01:26
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between f38a201 and 0ae8fcb.

Files selected for processing (2)
  • src/Metadata/class-metadata-builder.php (1 hunks)
  • tests/Integration/Endpoints/RestMetadataTest.php (2 hunks)
Files skipped from review as they are similar to previous changes (2)
  • src/Metadata/class-metadata-builder.php
  • tests/Integration/Endpoints/RestMetadataTest.php

@acicovic acicovic modified the milestones: 3.16.0, 3.17.0 Jun 17, 2024
@acicovic acicovic modified the milestones: 3.17.0, Future Milestone Oct 14, 2024
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (1)
src/Metadata/class-metadata-builder.php (1)

122-135: Consider adding error handling and sanitization.

The implementation looks good and follows WordPress coding standards. However, consider these improvements:

  1. Add error handling for wp_json_encode failure
  2. Sanitize the post ID before encoding

Apply this diff to improve error handling and sanitization:

 protected function build_post_id( WP_Post $post ): void {
+    $post_id = absint( $post->ID );
+    $json_data = wp_json_encode(
+        array(
+            'wpParselyPostID' => $post_id,
+        )
+    );
+    
+    if ( false !== $json_data ) {
+        $this->metadata['custom_metadata'] = $json_data;
+    }
-    $this->metadata['custom_metadata'] = wp_json_encode(
-        array(
-            'wpParselyPostID' => $post->ID,
-        )
-    );
 }
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 0ae8fcb and 816e60f.

📒 Files selected for processing (2)
  • src/Metadata/class-metadata-builder.php (1 hunks)
  • tests/Integration/Endpoints/RestMetadataTest.php (2 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
src/Metadata/class-metadata-builder.php (1)

Pattern **/*.{html,php}: "Perform a detailed review of the provided code with following key aspects in mind:

  • Review the HTML and PHP code to ensure it is well-structured and adheres to best practices.
  • Ensure the code follows WordPress coding standards and is well-documented.
  • Confirm the code is secure and free from vulnerabilities.
  • Optimize the code for performance, removing any unnecessary elements.
  • Validate comments for accuracy, currency, and adherence to WordPress coding standards.
  • Ensure each line comment concludes with a period.
  • Verify code compatibility with the latest version of WordPress, avoiding deprecated functions or features."
tests/Integration/Endpoints/RestMetadataTest.php (1)

Pattern **/*.{html,php}: "Perform a detailed review of the provided code with following key aspects in mind:

  • Review the HTML and PHP code to ensure it is well-structured and adheres to best practices.
  • Ensure the code follows WordPress coding standards and is well-documented.
  • Confirm the code is secure and free from vulnerabilities.
  • Optimize the code for performance, removing any unnecessary elements.
  • Validate comments for accuracy, currency, and adherence to WordPress coding standards.
  • Ensure each line comment concludes with a period.
  • Verify code compatibility with the latest version of WordPress, avoiding deprecated functions or features."
🔇 Additional comments (2)
tests/Integration/Endpoints/RestMetadataTest.php (2)

406-407: LGTM! The test correctly verifies the custom metadata implementation.

The test case properly validates that:

  1. The post ID is correctly encoded in the JSON-LD output
  2. The post ID is properly escaped in the meta tag

475-476: LGTM! The test case validates repeated meta tags.

The test correctly verifies that:

  1. The section meta tag is present
  2. The post ID is properly encoded in the parsely-metadata meta tag

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants