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

REST API: Enhance author details by including additional data when either user_id or site_id are available. #41159

Merged
merged 10 commits into from
Feb 4, 2025

Conversation

anton-vlasenko
Copy link
Contributor

@anton-vlasenko anton-vlasenko commented Jan 17, 2025

Fixes #9984

Proposed changes:

  • Use the wp_comments table as the source of truth if the $author parameter passed to the WPCOM_JSON_API_Endpoint::get_author method is an instance of WP_Comment;
  • If the WP_Comment instance has a valid user_id, include the first_name, last_name, nice_name, and login fields in the response;
  • Add the site_id field to the response when a WP_Comment instance is passed to the WPCOM_JSON_API_Endpoint::get_author method.

Other information:

  • Have you written new tests for your changes, if applicable?
  • Have you checked the E2E test CI results, and verified that your changes do not break them?
  • Have you tested your changes on WordPress.com, if applicable (if so, you'll see a generated comment below with a script to run)?

Jetpack product discussion

Does this pull request change what data or activity we track or use?

Testing instructions:

  1. Apply the changes from this branch to your WordPress.com development website.
  2. After applying the changes, open the comments page in wp-admin. The request to the public-api.wordpress.com comments endpoint should return complete data about the commenter, including the login, first_name, last_name, and nice_name fields, if the user_id value in the wp_comments table is defined.

Refer to the screenshots below for comparison:

Before the patch:
Screenshot of the comments page before changes

After the patch:
Screenshot of the comments page after changes

Copy link
Contributor

github-actions bot commented Jan 17, 2025

Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.

  • To test on WoA, go to the Plugins menu on a WordPress.com Simple site. Click on the "Upload" button and follow the upgrade flow to be able to upload, install, and activate the Jetpack Beta plugin. Once the plugin is active, go to Jetpack > Jetpack Beta, select your plugin, and enable the fix/author-details-are-missing branch.

  • To test on Simple, run the following command on your sandbox:

    bin/jetpack-downloader test jetpack fix/author-details-are-missing
    

Interested in more tips and information?

  • In your local development environment, use the jetpack rsync command to sync your changes to a WoA dev blog.
  • Read more about our development workflow here: PCYsg-eg0-p2
  • Figure out when your changes will be shipped to customers here: PCYsg-eg5-p2

@github-actions github-actions bot added [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Status] In Progress labels Jan 17, 2025
Copy link
Contributor

github-actions bot commented Jan 17, 2025

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ Add a "[Status]" label (In Progress, Needs Team Review, ...).
  • ✅ Add a "[Type]" label (Bug, Enhancement, Janitorial, Task).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


The e2e test report can be found here. Please note that it can take a few minutes after the e2e tests checks are complete for the report to be available.


Follow this PR Review Process:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Choose a review path based on your changes:
    • A. Team Review: add the "[Status] Needs Team Review" label
      • For most changes, including minor cross-team impacts.
      • Example: Updating a team-specific component or a small change to a shared library.
    • B. Crew Review: add the "[Status] Needs Review" label
      • For significant changes to core functionality.
      • Example: Major updates to a shared library or complex features.
    • C. Both: Start with Team, then request Crew
      • For complex changes or when you need extra confidence.
      • Example: Refactor affecting multiple systems.
  3. Get at least one approval before merging.

Still unsure? Reach out in #jetpack-developers for guidance!


Jetpack plugin:

The Jetpack plugin has different release cadences depending on the platform:

  • WordPress.com Simple releases happen semi-continuously (PCYsg-Jjm-p2).
  • WoA releases happen weekly.
  • Releases to self-hosted sites happen monthly. The next release is scheduled for February 4, 2025 (scheduled code freeze on February 4, 2025).

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.

@github-actions github-actions bot added the [Status] Needs Author Reply We would need you to make some changes or provide some more details about your PR. Thank you! label Jan 17, 2025
@anton-vlasenko anton-vlasenko added the [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it label Jan 21, 2025
@anton-vlasenko anton-vlasenko changed the title Author details are missing. Enhance author details by including additional data when either user_id or site_id are available. Jan 21, 2025
@anton-vlasenko anton-vlasenko changed the title Enhance author details by including additional data when either user_id or site_id are available. REST API: Enhance author details by including additional data when either user_id or site_id are available. Jan 21, 2025
@anton-vlasenko anton-vlasenko marked this pull request as ready for review January 22, 2025 17:34
@anton-vlasenko anton-vlasenko added [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. and removed [Status] In Progress labels Jan 23, 2025
@anton-vlasenko
Copy link
Contributor Author

anton-vlasenko commented Jan 29, 2025

Please note that #41254 which was merged last week, adds two optional fields to the response: wpcom_login and wpcom_id. These fields contain similar data to the login and ID fields. However, they only appear if the author_wpcom_data parameter is present in the request.

@jeherve jeherve added [Type] Bug When a feature is broken and / or not performing as intended [Feature] WPCOM API and removed [Status] Needs Author Reply We would need you to make some changes or provide some more details about your PR. Thank you! labels Jan 31, 2025
jeherve
jeherve previously approved these changes Jan 31, 2025
Copy link
Member

@jeherve jeherve left a comment

Choose a reason for hiding this comment

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

This tests well for me. I have some notes / questions, but no blockers.

$profile_url = 'https://gravatar.com/' . md5( strtolower( trim( $email ) ) );
$nice = '';
$site_id = -1;
$id = ( isset( $author->user_id ) && $author->user_id ) ? $author->user_id : 0;
Copy link
Member

Choose a reason for hiding this comment

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

Since we're here, maybe we can simplify this a bit? It may be an opportunity to cast the return user ID. I don't know if that's necessary, but it may avoid any issues since we check 0 < $id later.

Suggested change
$id = ( isset( $author->user_id ) && $author->user_id ) ? $author->user_id : 0;
$id = ! empty( $author->user_id ) ? (int) $author->user_id : 0;

Copy link
Contributor Author

@anton-vlasenko anton-vlasenko Jan 31, 2025

Choose a reason for hiding this comment

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

Thank you for the review, @jeherve.
I didn't want to touch this old code since refactoring wasn't the main goal of my PR. But I absolutely agree — it can and should be simplified.
Fixed in c63354d

Comment on lines 1408 to 1411
$login = $user->user_login;
$first_name = $user->first_name;
$last_name = $user->last_name;
$nice = $user->user_nicename;
Copy link
Member

Choose a reason for hiding this comment

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

Is there any chance that some of those values return an empty string or are not defined at some point?

Copy link
Contributor Author

@anton-vlasenko anton-vlasenko Jan 31, 2025

Choose a reason for hiding this comment

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

Thank you for pointing that out. I naively assumed these class properties were declared on the WP_User class — I should have checked. It turns out they are dynamic.
I've added the necessary checks in 9deae9b.
As for whether they can be empty, I think it's fine since this code doesn't replace any existing data but simply augments what's available in the wp_comments table.

@jeherve jeherve added [Status] Needs Author Reply We would need you to make some changes or provide some more details about your PR. Thank you! and removed [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. labels Jan 31, 2025
Copy link
Contributor

github-actions bot commented Jan 31, 2025

Code Coverage Summary

Coverage changed in 1 file.

File Coverage Δ% Δ Uncovered
projects/plugins/jetpack/class.json-api-endpoints.php 239/1258 (19.00%) 1.59% -15 💚

Full summary · PHP report · JS report

@anton-vlasenko anton-vlasenko force-pushed the fix/author-details-are-missing branch from c192526 to c63354d Compare January 31, 2025 16:05
@jeherve jeherve added [Status] Ready to Merge Go ahead, you can push that green button! and removed [Status] Needs Author Reply We would need you to make some changes or provide some more details about your PR. Thank you! labels Feb 3, 2025
@anton-vlasenko anton-vlasenko merged commit aa3a69b into trunk Feb 4, 2025
65 checks passed
@anton-vlasenko anton-vlasenko deleted the fix/author-details-are-missing branch February 4, 2025 12:32
@github-actions github-actions bot removed the [Status] Ready to Merge Go ahead, you can push that green button! label Feb 4, 2025
@github-actions github-actions bot added this to the jetpack/14.2.2 milestone Feb 4, 2025
@tbradsha tbradsha modified the milestones: jetpack/14.2.2, jetpack/14.4 Feb 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] WPCOM API [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Pri] Normal [Tests] Includes Tests [Type] Bug When a feature is broken and / or not performing as intended [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

REST API: Author Details are bare and missing extra data that can be gained from site users.
3 participants