Skip to content

JSON API: support path-parameter endpoints over the REST transport#50400

Open
darssen wants to merge 1 commit into
trunkfrom
add/json-api-rest-path-param-endpoints
Open

JSON API: support path-parameter endpoints over the REST transport#50400
darssen wants to merge 1 commit into
trunkfrom
add/json-api-rest-path-param-endpoints

Conversation

@darssen

@darssen darssen commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Proposed changes

  • Infrastructure — this is the main goal of the PR. Today the Jetpack JSON API REST transport can only carry a static route: build_rest_route() returns 'v'.$max_version.$rest_route, and that fixed string is what the proxy sends to the remote site. That is fine for collection endpoints (/posts, /site, /users, /plugins), but any endpoint whose path carries a value — a post ID, a slug, a plugin slug, a comment ID, a term — has no way to get that value to the remote. As a result every single-item and action endpoint is stuck on XML-RPC regardless of where we are in the rollout, and the REST migration can only ever cover the collection endpoints. This PR puts the missing infrastructure in place so path parameters can travel over the REST transport:
    • build_rest_route_regex() converts the %d/%s tokens in a rest_route into ordered named captures for register_rest_route() on the remote (e.g. /posts/%dv1.1/posts/(?P<p1>\d+)).
    • build_concrete_rest_route() reflows the real request path into the route the proxy sends per request (e.g. .../sites/X/posts/7v1.1/posts/7).
    • Both are strict no-ops for token-less routes, so the collection endpoints already rolling out are byte-for-byte unchanged.
  • A concrete endpoint to exercise it. So there is something real to test on top of the new mechanism, the two single-post endpoints are enabled: /posts/%d (by ID) and /posts/slug:%s (by slug). Because the id/slug now arrive positionally on both transports, the endpoint callback needs no special-casing and reverts to its original form.
  • Unit tests covering token → named-capture conversion, path reflow (single token, mid-path token, multiple tokens, encoded slash, domain site), and the end-to-end guarantee that the concrete route a request produces matches the pattern the remote registered.
  • Backwards compatible. XML-RPC is untouched and remains the fallback; eligibility is gated by rest_min_jp_version and the rollout, and the change is a no-op for endpoints already live. This is the Jetpack (remote) half — the wpcom transport and sun/ mirror land in a companion wpcom PR, and the two need to ship together.

Related product discussion/links

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

No.

Testing instructions

  • Run the unit tests:
    • jetpack docker phpunit jetpack -- --filter=WPCOM_JSON_API_Endpoint_Rest_Route_Test
  • End-to-end (requires the companion wpcom change): on a site in JSON API dev mode, GET /rest/v1.1/sites/<blog_id>/posts/<post_id> and GET /rest/v1.1/sites/<blog_id>/posts/slug:<slug> should return the post over REST, matching what XML-RPC returns.

@github-actions

github-actions Bot commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

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 WoA dev 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 (Jetpack), and enable the add/json-api-rest-path-param-endpoints branch.
  • To test on Simple, run the following command on your sandbox:
bin/jetpack-downloader test jetpack add/json-api-rest-path-param-endpoints

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 [Feature] WPCOM API [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Status] In Progress [Tests] Includes Tests labels Jul 10, 2026
@github-actions

github-actions Bot commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

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 Review, ...).
  • ✅ 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 🤖


Follow this PR Review Process:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Make sure to test your changes on all platforms that it applies to. You're responsible for the quality of the code you ship.
  3. You can use GitHub's Reviewers functionality to request a review.
  4. When it's reviewed and merged, you will be pinged in Slack to deploy the changes to WordPress.com simple once the build is done.

If you have questions about anything, reach out in #jetpack-developers for guidance!


Jetpack plugin:

No scheduled milestone found for this plugin.

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 need more details from you. This label will be auto-added until the PR meets all requirements. label Jul 10, 2026
@darssen darssen removed the [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. label Jul 10, 2026
@jp-launch-control

jp-launch-control Bot commented Jul 10, 2026

Copy link
Copy Markdown

Code Coverage Summary

Coverage changed in 2 files.

File Coverage Δ% Δ Uncovered
projects/plugins/jetpack/class.json-api-endpoints.php 397/1357 (29.26%) 0.90% 0 💚
projects/plugins/jetpack/json-endpoints/class.wpcom-json-api-get-post-v1-1-endpoint.php 17/61 (27.87%) 5.06% 0 💚

Full summary · PHP report · JS report

@darssen darssen force-pushed the add/json-api-rest-path-param-endpoints branch from 544f307 to a6a900f Compare July 10, 2026 11:57
@darssen darssen self-assigned this Jul 10, 2026
@darssen darssen force-pushed the add/json-api-rest-path-param-endpoints branch from a6a900f to 3c83fb0 Compare July 10, 2026 13:12
@darssen darssen force-pushed the add/json-api-rest-path-param-endpoints branch from 3c83fb0 to 16a079b Compare July 10, 2026 13:42
@darssen darssen marked this pull request as ready for review July 10, 2026 14:09
@darssen darssen requested a review from Copilot July 10, 2026 14:09

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR extends the Jetpack JSON API REST transport to support endpoints whose paths include parameters (e.g., IDs/slugs), enabling more single-item/action endpoints to migrate from XML-RPC to REST while keeping existing static/collection routes unchanged.

Changes:

  • Add REST route shaping to WPCOM_JSON_API_Endpoint: tokenized route registration via named captures and per-request concrete route building.
  • Enable REST transport for single-post endpoints (/posts/%d and /posts/slug:%s) and simplify callback behavior to rely on positional args consistently.
  • Add unit tests for token→regex conversion, concrete-route reflow, and round-trip matching; update Jetpack changelog entry.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
projects/plugins/jetpack/class.json-api-endpoints.php Registers REST routes using a regex-capable builder and adds helpers for tokenized route registration + concrete per-request routing.
projects/plugins/jetpack/json-endpoints/class.wpcom-json-api-get-post-v1-1-endpoint.php Adds rest_route + rest_min_jp_version for single-post by-ID and by-slug endpoints to exercise the new transport capability.
projects/plugins/jetpack/tests/php/json-api/WPCOM_JSON_API_Endpoint_Rest_Route_Test.php New unit tests covering route regex building, concrete route construction, and end-to-end pattern matching.
projects/plugins/jetpack/tests/php/json-api/WPCOM_JSON_API_Get_Post_v1_1_Endpoint_Test.php Adds coverage ensuring the endpoint callback resolves posts correctly by ID and slug, including the unknown-slug error case.
projects/plugins/jetpack/changelog/add-json-api-rest-path-param-endpoints Adds a user-facing changelog entry describing the new REST transport support for path-parameter endpoints.

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/ [Status] In Progress [Tests] Includes Tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants