Skip to content

Global Styles: Accept a post object in the REST controller's prepare_links() - #86470

Merged
westonruter merged 11 commits into
trunkfrom
fix/global-styles-controller-prepare-links-phpstan
Nov 10, 2026
Merged

westonruter merged 11 commits into
trunkfrom
fix/global-styles-controller-prepare-links-phpstan

Conversation

@westonruter

@westonruter westonruter commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

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_Gutenberg extends WP_REST_Posts_Controller, whose prepare_links( $post ) takes a WP_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 takes WP_Post|int $post and resolves the ID from it. Widening the parameter rather than switching it to WP_Post keeps any code that still passes an ID working.
  • prepare_item_for_response() still passes the ID, so a subclass that overrides prepare_links() and treats its argument as an ID is not broken.
  • New tests assert the version-history link's href and that its count matches the number of revisions, and that prepare_links() returns the same links for a post ID as for the post object.
  • The return type is now the links it builds: self and about, plus version-history with its revisions count when the post type supports revisions. The plain @return is array<string, array<string, string|int>>, and @phpstan-return has the shape.
  • Removed the entry from 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() and get_rest_url() return non-falsy-string, as WP_REST_Response::add_links() requires of a link's href.
  • WP_Query::$found_posts is int<0, max>, so the revisions count from wp_get_latest_revision_id_and_total_count() is too.
  • count is a declared link attribute in WP_REST_Response's link types. PHPStan merges Link_Input with its list form in add_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=trunk once the Core change is committed.

There is no change in behavior.

Testing Instructions

  1. Run composer phpstan and confirm it passes with no unmatched ignored errors. It generates the stubs from the Core pull request first.
  2. Run composer phpstan:baselines and confirm tools/phpstan/baseline.neon has no further changes.
  3. Run the controller's PHP unit tests: 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.

…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
@westonruter westonruter added [Type] Code Quality Issues or PRs that relate to code quality Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json labels Nov 7, 2026
@github-actions

github-actions Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

🤖 PR meta 🤖

Preview in WordPress Playground Preview in Storybook

🎉 Props

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: westonruter <westonruter@git.wordpress.org>
Co-authored-by: huzaifaalmesbah <huzaifaalmesbah@git.wordpress.org>
Co-authored-by: dknauss <dpknauss@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

Updated as activity occurs, without notifying anyone named here. Add the props-bot label to refresh.

⚡ Performance

Show the results

Client side metrics exclude the server response time.

front-end-block-theme

Metric d7f615b trunk % Change
timeToFirstByte 54.4 ms +4.6% -2.48% 57.6 ms +7.9% -2.08% -5.56%
largestContentfulPaint 92 ms +6.52% -4.35% 94 ms +6.38% -2.13% -2.13%
lcpMinusTtfb 34.8 ms +19.83% -6.75% 34.85 ms +6.89% -1.72% -0.14%
wpBeforeTemplate 28.37 ms +10.22% -1.34% 30.2 ms +8.97% -2.48% -6.06%
wpTemplate 21.68 ms +4.75% -4.11% 22.94 ms +6.28% -2.66% -5.49%
wpTotal 50.53 ms +4.75% -2.69% 53.6 ms +8.28% -1.98% -5.73%
wpMemoryUsage 7.72 MB +0% -0% 7.69 MB +0% -0% 0.43%
wpDbQueries 16 +0% -0% 16 +0% -0% 0%

front-end-classic-theme

Metric d7f615b trunk % Change
timeToFirstByte 46.35 ms +7.01% -2.48% 47.4 ms +4.64% -1.69% -2.22%
largestContentfulPaint 102 ms +1.96% -1.96% 100 ms +4% -0% 2%
lcpMinusTtfb 54.85 ms +1.28% -1.73% 53.6 ms +2.99% -1.59% 2.33%
wpBeforeTemplate 26.62 ms +11.53% -2.07% 26.86 ms +3.57% -1.86% -0.89%
wpTemplate 16.41 ms +3.29% -2.13% 16.88 ms +4.09% -2.31% -2.78%
wpTotal 43.03 ms +7.92% -1.91% 44.01 ms +5.07% -1.34% -2.23%
wpMemoryUsage 6.41 MB +0% -0% 6.38 MB +0% -0% 0.53%
wpDbQueries 13 +0% -0% 13 +0% -0% 0%

media-processing

Metric d7f615b trunk % Change
mediaProcessingJpeg 406.16 ms +0.66% -0.37% 407.26 ms +0.2% -0.48% -0.27%
mediaProcessingAvif 6129.7 ms +0.29% -0.22% 6149.52 ms +0.34% -0.47% -0.32%
mediaProcessingJpegToAvif 4208.28 ms +0.46% -0.56% 4217.06 ms +0.53% -0.19% -0.21%

media-upload

Metric d7f615b trunk % Change
jpegUploadProcessing 1484.4 ms +31.94% -1.88% 1468 ms +3.79% -0.88% 1.12%
pngUploadProcessing 176.07 ms +2.1% -4.78% 178.67 ms +39.44% -5.43% -1.46%
largeJpegUploadProcessing 1468.33 ms +31.87% -1.09% 1470.47 ms +32.58% -0.87% -0.15%
multipleImageUploadProcessing 2057.41 ms +0.42% -2.56% 2060 ms +0.61% -1.46% -0.13%

post-editor

Metric d7f615b trunk % Change
serverResponse 480 ms +3.24% -8.25% 463.56 ms +5.17% -5.23% 3.55%
firstPaint 341.67 ms +22.09% -11.37% 350.26 ms +13.2% -18.12% -2.45%
domContentLoaded 863.55 ms +0.96% -2.88% 865.89 ms +1.25% -0.87% -0.27%
loaded 864.64 ms +0.95% -2.87% 867.14 ms +1.21% -0.89% -0.29%
firstContentfulPaint 504.61 ms +1.14% -2.96% 506.49 ms +2.43% -1.48% -0.37%
firstBlock 2247.58 ms +1.46% -1.17% 2266.48 ms +0.49% -0.88% -0.83%
type 20.87 ms +6.18% -9.82% 22.99 ms +8.44% -8.79% -9.22%
typeWithoutInspector 18.96 ms +8.65% -8.76% 21.85 ms +7.28% -7.92% -13.23%
typeWithTopToolbar 25.22 ms +12.93% -8.37% 29.14 ms +8.89% -10.78% -13.45%
typeContainer 9.15 ms +18.36% -13.88% 10.92 ms +5.31% -1.65% -16.21%
focus 133.22 ms +8.23% -4.44% 149.62 ms +5.26% -6.72% -10.96%
firstFocus 283.98 ms +0% -0% 344.27 ms +0% -0% -17.51%
selectAll 439.95 ms +5.89% -1.39% 456.7 ms +1.02% -6.48% -3.67%
undo 31.28 ms +6.39% -5.31% 35.05 ms +4.68% -6.13% -10.76%
listViewOpen 83.23 ms +1.93% -8.03% 82.74 ms +12.41% -10.06% 0.59%
inserterOpen 44.14 ms +5.75% -1.43% 51.03 ms +1.84% -5.13% -13.5%
inserterHover 13.88 ms +6.41% -13.69% 15.53 ms +13.14% -8.56% -10.62%
inserterSearch 9.2 ms +14.67% -8.26% 9.93 ms +3.32% -2.82% -7.35%
loadPatterns 703.95 ms +3.85% -3.96% 663.81 ms +4.58% -5.53% 6.05%
wpTotal 470.12 ms +3.35% -8.36% 453.74 ms +5.33% -5.34% 3.61%
wpMemoryUsage 14.77 MB +0% -0% 14.74 MB +0% -0% 0.24%
wpDbQueries 55 +0% -0% 55 +0% -0% 0%

site-editor

Metric d7f615b trunk % Change
serverResponse 311.03 ms +2.21% -9.84% 297.16 ms +5.59% -5.2% 4.67%
firstPaint 297.88 ms +2.44% -4.6% 274.49 ms +2.25% -1.83% 8.52%
domContentLoaded 677.61 ms +4.8% -0.51% 681.03 ms +2.88% -0.23% -0.5%
loaded 678.36 ms +4.8% -0.5% 681.86 ms +2.86% -0.22% -0.51%
firstContentfulPaint 393.24 ms +6.02% -1.48% 399.16 ms +1.32% -1.24% -1.48%
firstBlock 2023.57 ms +6.83% -0.86% 2025.53 ms +1.84% -0.89% -0.1%
type 14.63 ms +5.33% -3.76% 14.43 ms +4.57% -6.65% 1.39%
navigate 94.24 ms +7.64% -5.65% 88.69 ms +6.28% -0.7% 6.26%
loadPatterns 1064.14 ms +5.21% -13.01% 978.96 ms +13.39% -11.12% 8.7%
loadPages 1014.04 ms +1.65% -1.49% 1021 ms +2.94% -0.8% -0.68%
wpTotal 302.73 ms +2.2% -10.08% 288.89 ms +5.75% -5.36% 4.79%
wpMemoryUsage 12.79 MB +0% -0% 12.75 MB +0% -0% 0.27%
wpDbQueries 45 +0% -0% 45 +0% -0% 0%

2a4d8ec Run

🏁 Flaky tests

Some 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 /test/e2e/specs/site-editor/navigation-overlay-template-part.spec.js, passed after 1 failed attempt.
Error: expect(locator).toHaveCount(expected) failed

Locator:  getByRole('button', { name: 'Close' })
Expected: 2
Received: 1
Timeout:  5000ms

Call log:
  - Expect "toHaveCount" getByRole('button', { name: 'Close' }) with timeout 5000ms
  - waiting for getByRole('button', { name: 'Close' })
    14 × locator resolved to 1 element
       - unexpected value "1"

    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/site-editor/navigation-overlay-template-part.spec.js:228:33
Navigates the items list via UP/DOWN arrow keys (Site Editor v2) in /test/e2e/specs/site-editor/dataviews-list-layout-keyboard.spec.js, passed after 1 failed attempt.
Error: expect(locator).toBeFocused() failed

Locator:  getByLabel('Page One')
Expected: focused
Received: inactive
Timeout:  5000ms

Call log:
  - Expect "toBeFocused" getByLabel('Page One') with timeout 5000ms
  - waiting for getByLabel('Page One')
    14 × locator resolved to <button type="button" aria-pressed="true" data-active-item="true" id="view-list-0-12-item-wrapper" class="dataviews-view-list__item" aria-labelledby="view-list-0-12-label" aria-describedby="view-list-0-12-description"></button>
       - unexpected value "inactive"

    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/site-editor/dataviews-list-layout-keyboard.spec.js:149:49
Page List converts to Navigation Links with entity bindings in /test/e2e/specs/editor/blocks/navigation.spec.js, passed after 1 failed attempt.
Error: expect(locator).toContainText(expected) failed

Locator: getByRole('region', { name: 'Editor settings' }).getByRole('tabpanel', { name: 'Content' }).getByRole('button', { name: /Link to:/ })
Expected substring: "test-page-1"
Received string:    "Link to:Cat/catPagePublished"
Timeout: 5000ms

Call log:
  - Expect "toContainText" getByRole('region', { name: 'Editor settings' }).getByRole('tabpanel', { name: 'Content' }).getByRole('button', { name: /Link to:/ }) with timeout 5000ms
  - waiting for getByRole('region', { name: 'Editor settings' }).getByRole('tabpanel', { name: 'Content' }).getByRole('button', { name: /Link to:/ })
    14 × locator resolved to <button type="button" aria-expanded="false" aria-haspopup="dialog" class="components-button link-preview-button is-next-40px-default-size is-secondary">…</button>
       - unexpected value "Link to:Cat/catPagePublished"

    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/blocks/navigation.spec.js:1949:31
Undo restores the post selection when another synced entity is loaded (RTC WebSocket) in /test/e2e/specs/editor/collaboration/collaboration-undo-redo.spec.ts, passed after 1 failed attempt.
Error: expect(received).toMatchObject(expected)

- Expected  - 2
+ Received  + 2

  Object {
    "attributeKey": "content",
    "blockIndex": 0,
    "content": "abcdef",
-   "endOffset": 3,
-   "offset": 3,
+   "endOffset": 6,
+   "offset": 6,
  }

Call Log:
- Timeout 5000ms exceeded while waiting on the predicate
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/collaboration/collaboration-undo-redo.spec.ts:363:5
Undo restores the post selection when another synced entity is loaded in /test/e2e/specs/editor/collaboration/collaboration-undo-redo.spec.ts, passed after 1 failed attempt.
Error: expect(received).toMatchObject(expected)

- Expected  - 2
+ Received  + 2

  Object {
    "attributeKey": "content",
    "blockIndex": 0,
    "content": "abcdef",
-   "endOffset": 3,
-   "offset": 3,
+   "endOffset": 6,
+   "offset": 6,
  }

Call Log:
- Timeout 5000ms exceeded while waiting on the predicate
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/collaboration/collaboration-undo-redo.spec.ts:363:5
does not select blocks in the canvas preview when editing from the sidebar (Site Editor v2) in /test/e2e/specs/site-editor/navigation-sidebar-list-view.spec.js, passed after 1 failed attempt.
Error: expect(locator).toBeVisible() failed

Locator: getByRole('listbox', { name: 'Search results' })
Expected: visible
Timeout: 5000ms
Error: element(s) not found

Call log:
  - Expect "toBeVisible" getByRole('listbox', { name: 'Search results' }) with timeout 5000ms
  - waiting for getByRole('listbox', { name: 'Search results' })

    at LinkControl.useLinkControlSearch (/home/runner/work/gutenberg/gutenberg/test/e2e/specs/site-editor/navigation-sidebar-list-view.spec.js:423:33)
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/site-editor/navigation-sidebar-list-view.spec.js:179:3
falls back to the first tab when the default tab is removed in /test/e2e/specs/editor/blocks/tabs.spec.js, passed after 1 failed attempt.
Error: expect(received).toBe(expected) // Object.is equality

Expected: 1
Received: 0

Call Log:
- Timeout 5000ms exceeded while waiting on the predicate
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/blocks/tabs.spec.js:344:60
inserts a block should show a blue line indicator in /test/e2e/specs/editor/various/inserting-blocks.spec.js, passed after 1 failed attempt.
TimeoutError: locator.boundingBox: Timeout 10000ms exceeded.
Call log:
  - waiting for locator('data-testid=block-list-insertion-point-indicator')

    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/inserting-blocks.spec.js:678:41
keeps the default tab on the moved tab in /test/e2e/specs/editor/blocks/tabs.spec.js, passed after 1 failed attempt.
Error: expect(received).toBe(expected) // Object.is equality

Expected: 0
Received: 1

Call Log:
- Timeout 5000ms exceeded while waiting on the predicate
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/blocks/tabs.spec.js:359:60

2a4d8ec Run

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
westonruter and others added 5 commits November 7, 2026 13:26
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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

TODO: Regenerate stubs once WordPress/wordpress-develop#15826 is committed.

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.

🔵 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 huzaifaalmesbah left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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
@dknauss

dknauss commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Tested the new prepare_links() against the current one on WordPress 7.1.4 / PHP 8.3, with a global styles post that has three revisions. Links from an ID match the old method exactly, and links from a WP_Post, an int and a numeric string all match each other. The live GET /wp/v2/global-styles/{id} response still has version-history with the right count. Keeping prepare_item_for_response() on $post->ID makes sense for subclasses that override this method.

One question: the stubs in tools/phpstan/wordpress-stubs/composer.json are generated from a commit on wordpress-develop#15826. Is the plan to regenerate them from a core revision once that lands, before or after this merges?

@westonruter

Copy link
Copy Markdown
Member Author

One question: the stubs in tools/phpstan/wordpress-stubs/composer.json are generated from a commit on wordpress-develop#15826. Is the plan to regenerate them from a core revision once that lands, before or after this merges?

Yes, I'll regenerate the stubs once the core commit lands and then I'll merge this.

westonruter and others added 3 commits November 9, 2026 20:46
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
@westonruter
westonruter enabled auto-merge (squash) November 10, 2026 04:55
@westonruter
westonruter merged commit e3e83b2 into trunk Nov 10, 2026
78 checks passed
@westonruter
westonruter deleted the fix/global-styles-controller-prepare-links-phpstan branch November 10, 2026 05:18
@github-actions github-actions Bot added this to the Gutenberg 24.5 milestone Nov 10, 2026
Vedant-Gandhi pushed a commit to Vedant-Gandhi/gutenberg that referenced this pull request Nov 30, 2026
…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json [Type] Code Quality Issues or PRs that relate to code quality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants