JSON API: support path-parameter endpoints over the REST transport#50400
JSON API: support path-parameter endpoints over the REST transport#50400darssen wants to merge 1 commit into
Conversation
|
Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.
Interested in more tips and information?
|
|
Thank you for your PR! When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:
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:
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. |
Code Coverage SummaryCoverage changed in 2 files.
|
544f307 to
a6a900f
Compare
a6a900f to
3c83fb0
Compare
3c83fb0 to
16a079b
Compare
There was a problem hiding this comment.
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/%dand/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. |
Proposed changes
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/%stokens in arest_routeinto ordered named captures forregister_rest_route()on the remote (e.g./posts/%d→v1.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/7→v1.1/posts/7)./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.rest_min_jp_versionand the rollout, and the change is a no-op for endpoints already live. This is the Jetpack (remote) half — the wpcom transport andsun/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
jetpack docker phpunit jetpack -- --filter=WPCOM_JSON_API_Endpoint_Rest_Route_TestGET /rest/v1.1/sites/<blog_id>/posts/<post_id>andGET /rest/v1.1/sites/<blog_id>/posts/slug:<slug>should return the post over REST, matching what XML-RPC returns.