Repository navigation
Global Styles: Accept a post object in the REST controller's prepare_links() - #86470
Conversation
…links() `WP_REST_Global_Styles_Controller_Gutenberg::prepare_links()` overrides `WP_REST_Posts_Controller::prepare_links( $post )` but took a post ID instead of a `WP_Post`, which PHPStan reports as an incompatible child parameter type. Widen the parameter to `WP_Post|int`, so it accepts what the parent method does while still accepting an ID, and pass the post object from `prepare_item_for_response()`. Also give the links array a value type. Remove the corresponding baseline entry. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: 9bb536c7-7399-4262-80ed-cebfc277b43b
🤖 PR meta 🤖🎉 PropsIf you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
Updated as activity occurs, without notifying anyone named here. Add the ⚡ PerformanceShow the resultsClient side metrics exclude the server response time. front-end-block-theme
front-end-classic-theme
media-processing
media-upload
post-editor
site-editor
🏁 Flaky testsSome tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information. As a user I want to be able to add multiple close buttons so that users can close the overlay from different positions in
|
The Core backport is going in under the PHPStan code quality ticket for the 7.2 milestone, so the `@since` tag needs to match. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: 9bb536c7-7399-4262-80ed-cebfc277b43b
Narrow the return type of `prepare_links()` to the links it builds: `self` and `about`, plus `version-history` with its revisions `count` when the post type supports revisions, as in the Core backport. The shape relies on types added in that backport: `rest_url()` returning a non-falsy string, the revisions count being non-negative, and `count` being a declared link attribute in `WP_REST_Response::add_links()`. Generate the WordPress stubs from the backport's pull request, so that they include these types until it is committed. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: 9bb536c7-7399-4262-80ed-cebfc277b43b
Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: 9bb536c7-7399-4262-80ed-cebfc277b43b
`prepare_links()` now receives the post object, and `wp_get_latest_revision_id_and_total_count()` accepts one, so pass it through rather than its ID, which the helper would look up again. This matches the Core backport. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: 9bb536c7-7399-4262-80ed-cebfc277b43b
`prepare_links()` now accepts a post object as well as an ID, but a subclass that overrides it may still treat its argument as an ID. Pass the ID from `prepare_item_for_response()` as before, matching the Core backport. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: 9bb536c7-7399-4262-80ed-cebfc277b43b
The `version-history` link of a global styles response was not tested. Assert that it points to the revisions, and that its `count` matches the number of revisions. Also assert that `prepare_links()` returns the same links for a post ID as for the post object, now that it accepts either. These mirror the tests in the Core backport. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: 9bb536c7-7399-4262-80ed-cebfc277b43b
There was a problem hiding this comment.
TODO: Regenerate stubs once WordPress/wordpress-develop#15826 is committed.
There was a problem hiding this comment.
🔵 Needs a closer look
Approval should wait until WordPress core PR #15826 is committed and the generated stubs and PHPStan results are regenerated and verified against it.
No open issues 🎉
What changed in this PR
Updates the Global Styles REST controller so prepare_links() accepts either a WP_Post object or post ID, aligning with its parent method while preserving existing callers. The change also adds precise PHPStan typing, regression tests, and removes the resolved baseline error.
Changes:
- Supports post objects and IDs in
prepare_links(). - Documents the precise link return shape.
- Adds coverage for revision-history links, counts, and ID/object equivalence.
- Updates PHPStan stubs and removes the resolved baseline entry.
- Records the related Core backport.
| File | Description |
|---|---|
tools/phpstan/wordpress-stubs/composer.json |
Updates metadata for the WordPress stubs generated from Core PR #15826. |
tools/phpstan/baseline.neon |
Removes the resolved incompatible-parameter baseline entry. |
phpunit/class-wp-rest-global-styles-controller-gutenberg-test.php |
Tests revision-history links and ID/object input equivalence. |
lib/class-wp-rest-global-styles-controller-gutenberg.php |
Accepts post IDs and objects and documents the precise link return shape. |
backport-changelog/7.2/15826.md |
Records the Core backport reference. |
Review effort: Lite
huzaifaalmesbah
left a comment
There was a problem hiding this comment.
Looks good, thanks! Widening to WP_Post|int matches the parent while keeping ID callers working, and the change mirrors the Core backport (WordPress/wordpress-develop#15826). Tests cover the version-history link and ID/object equivalence well.
Approving, with the stubs to be regenerated once the Core change is committed, as noted in the TODO.
…ix/global-styles-controller-prepare-links-phpstan
|
Tested the new One question: the stubs in |
Yes, I'll regenerate the stubs once the core commit lands and then I'll merge this. |
Add a comment at the call site in `prepare_item_for_response()` saying the ID is passed on purpose, so that it isn't later "simplified" to pass the post object, which would break subclasses that override `prepare_links()` and expect an ID. This mirrors the change in the Core backport. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: 9bb536c7-7399-4262-80ed-cebfc277b43b
The Core change this depends on has been committed in r65369, so the stubs are now generated from trunk instead of from the head of its pull request. The PHPStan baseline is unchanged. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: 9bb536c7-7399-4262-80ed-cebfc277b43b
…phpstan Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: 9bb536c7-7399-4262-80ed-cebfc277b43b
…links() (WordPress#86470) * Global Styles: Accept a post object in the REST controller's prepare_links() `WP_REST_Global_Styles_Controller_Gutenberg::prepare_links()` overrides `WP_REST_Posts_Controller::prepare_links( $post )` but took a post ID instead of a `WP_Post`, which PHPStan reports as an incompatible child parameter type. Widen the parameter to `WP_Post|int`, so it accepts what the parent method does while still accepting an ID, and pass the post object from `prepare_item_for_response()`. Also give the links array a value type. Remove the corresponding baseline entry. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> * Global Styles: Target 7.2.0 for the prepare_links() signature change The Core backport is going in under the PHPStan code quality ticket for the 7.2 milestone, so the `@since` tag needs to match. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> * Global Styles: Add a shape for the REST controller's links Narrow the return type of `prepare_links()` to the links it builds: `self` and `about`, plus `version-history` with its revisions `count` when the post type supports revisions, as in the Core backport. The shape relies on types added in that backport: `rest_url()` returning a non-falsy string, the revisions count being non-negative, and `count` being a declared link attribute in `WP_REST_Response::add_links()`. Generate the WordPress stubs from the backport's pull request, so that they include these types until it is committed. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> * Add a backport changelog entry for the global styles links change Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> * Global Styles: Pass the post to the revisions count helper `prepare_links()` now receives the post object, and `wp_get_latest_revision_id_and_total_count()` accepts one, so pass it through rather than its ID, which the helper would look up again. This matches the Core backport. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> * Global Styles: Keep passing the post ID to prepare_links() `prepare_links()` now accepts a post object as well as an ID, but a subclass that overrides it may still treat its argument as an ID. Pass the ID from `prepare_item_for_response()` as before, matching the Core backport. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> * Global Styles: Test the version-history link and prepare_links() The `version-history` link of a global styles response was not tested. Assert that it points to the revisions, and that its `count` matches the number of revisions. Also assert that `prepare_links()` returns the same links for a post ID as for the post object, now that it accepts either. These mirror the tests in the Core backport. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> * Explain why prepare_links() is passed a post ID Add a comment at the call site in `prepare_item_for_response()` saying the ID is passed on purpose, so that it isn't later "simplified" to pass the post object, which would break subclasses that override `prepare_links()` and expect an ID. This mirrors the change in the Core backport. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> * Regenerate the WordPress stubs from trunk at r65369 The Core change this depends on has been committed in r65369, so the stubs are now generated from trunk instead of from the head of its pull request. The PHPStan baseline is unchanged. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> --------- Co-authored-by: westonruter <westonruter@git.wordpress.org> Co-authored-by: huzaifaalmesbah <huzaifaalmesbah@git.wordpress.org> Co-authored-by: dknauss <dpknauss@git.wordpress.org>
What?
See #86088
Makes
WP_REST_Global_Styles_Controller_Gutenberg::prepare_links()accept a post object, matching the parent method, gives it a precise return type, and removes the corresponding entry from the PHPStan baseline. Together with #86469, this empties the baseline.Core backport: WordPress/wordpress-develop#15826
Why?
WP_REST_Global_Styles_Controller_GutenbergextendsWP_REST_Posts_Controller, whoseprepare_links( $post )takes aWP_Post. The override took a post ID instead, which PHPStan reports as an incompatible child parameter type: the child must accept at least what the parent accepts. (method.childParameterType)How?
prepare_links()now takesWP_Post|int $postand resolves the ID from it. Widening the parameter rather than switching it toWP_Postkeeps any code that still passes an ID working.prepare_item_for_response()still passes the ID, so a subclass that overridesprepare_links()and treats its argument as an ID is not broken.version-historylink'shrefand that itscountmatches the number of revisions, and thatprepare_links()returns the same links for a post ID as for the post object.selfandabout, plusversion-historywith its revisionscountwhen the post type supports revisions. The plain@returnisarray<string, array<string, string|int>>, and@phpstan-returnhas the shape.tools/phpstan/baseline.neon.The shape relies on types added in the Core backport, so the WordPress stubs are generated from that pull request for now:
rest_url()andget_rest_url()returnnon-falsy-string, asWP_REST_Response::add_links()requires of a link'shref.WP_Query::$found_postsisint<0, max>, so the revisions count fromwp_get_latest_revision_id_and_total_count()is too.countis a declared link attribute inWP_REST_Response's link types. PHPStan mergesLink_Inputwith its list form inadd_links()and drops the unsealed...<string, mixed>keys, so an undeclared attribute is rejected when a caller passes a precise shape.Before this is merged, the stubs need to be regenerated with
composer phpstan:generate-wordpress-stubs -- --commit=trunkonce the Core change is committed.There is no change in behavior.
Testing Instructions
composer phpstanand confirm it passes with no unmatched ignored errors. It generates the stubs from the Core pull request first.composer phpstan:baselinesand confirmtools/phpstan/baseline.neonhas no further changes.npm run test:unit:php -- -- --filter WP_REST_Global_Styles_Controller_Gutenberg_Test.Testing Instructions for Keyboard
Not applicable. There are no UI changes.
Screenshots or screencast
Not applicable.
Use of AI Tools
I used Claude Code (Claude Opus 5.5, medium effort) to analyze the baselined error, probe the types, draft the change and this description, sync it with the Core backport, and run the tests. I reviewed the diff and take responsibility for it.