From 20efbb16793bd5ee09e125506db9d41c373d0734 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Sat, 7 Nov 2026 12:59:09 -0800 Subject: [PATCH 01/13] REST API: Accept a post object in the global styles controller's prepare_links() `WP_REST_Global_Styles_Controller::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) Claude-Code-Session-Id: 9bb536c7-7399-4262-80ed-cebfc277b43b --- .../class-wp-rest-global-styles-controller.php | 10 ++++++---- tests/phpstan/baselines/method.childParameterType.neon | 5 ----- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php index e9f3b281ed773..d19cb1cc22520 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php @@ -359,7 +359,7 @@ public function prepare_item_for_response( $post, $request ) { $response = rest_ensure_response( $data ); if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) { - $links = $this->prepare_links( $post->ID ); + $links = $this->prepare_links( $post ); // Only return resolved URIs for get requests to user theme JSON. if ( $theme_json ) { @@ -387,11 +387,13 @@ public function prepare_item_for_response( $post, $request ) { * * @since 5.9.0 * @since 6.3.0 Adds revisions count and rest URL href to version-history. + * @since 7.2.0 Accepts a post object, matching the parent method. * - * @param int $id ID. - * @return array Links for the given post. + * @param WP_Post|int $post Post object or ID. + * @return array> Links for the given post. */ - protected function prepare_links( $id ) { + protected function prepare_links( $post ) { + $id = $post instanceof WP_Post ? $post->ID : $post; $base = sprintf( '%s/%s', $this->namespace, $this->rest_base ); $links = array( diff --git a/tests/phpstan/baselines/method.childParameterType.neon b/tests/phpstan/baselines/method.childParameterType.neon index c1d35e69a2a30..3d6cfe40fdb08 100644 --- a/tests/phpstan/baselines/method.childParameterType.neon +++ b/tests/phpstan/baselines/method.childParameterType.neon @@ -53,11 +53,6 @@ parameters: identifier: method.childParameterType count: 1 path: ../../../src/wp-includes/class-walker-nav-menu.php - - - message: '#^Parameter \#1 \$id \(int\) of method WP_REST_Global_Styles_Controller\:\:prepare_links\(\) should be compatible with parameter \$post \(WP_Post\) of method WP_REST_Posts_Controller\:\:prepare_links\(\)$#' - identifier: method.childParameterType - count: 1 - path: ../../../src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php - message: '#^Parameter \#1 \$parent_template_id \(string\) of method WP_REST_Template_Revisions_Controller\:\:get_parent\(\) should be compatible with parameter \$parent_post_id \(int\) of method WP_REST_Revisions_Controller\:\:get_parent\(\)$#' identifier: method.childParameterType From 350f893bed5a0bf26b6ad64b3c02f7bb7c15226c Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Sat, 7 Nov 2026 13:19:54 -0800 Subject: [PATCH 02/13] REST API: Type REST URLs as non-falsy strings `get_rest_url()` always builds a non-empty URL: the home URL followed by the REST prefix and a route that starts with `/`, or by `index.php` with a `rest_route` query arg. Document the contract with `@phpstan-return non-falsy-string` on it and on `rest_url()`, and with a `@phpstan-param non-falsy-string` for `$url` on the `rest_url` filter, so that the filtered value is typed by the same contract. This lets callers pass REST URLs where a non-empty string is required, such as the `href` of a link given to `WP_REST_Response::add_links()`. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Code-Session-Id: 9bb536c7-7399-4262-80ed-cebfc277b43b --- src/wp-includes/rest-api.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/wp-includes/rest-api.php b/src/wp-includes/rest-api.php index f40021f4d06cd..f8d5e0a383e81 100644 --- a/src/wp-includes/rest-api.php +++ b/src/wp-includes/rest-api.php @@ -531,6 +531,8 @@ function rest_get_url_prefix() { * @param string $path Optional. REST route. Default '/'. * @param string $scheme Optional. Sanitization scheme. Default 'rest'. * @return string Full URL to the endpoint. + * + * @phpstan-return non-falsy-string */ function get_rest_url( $blog_id = null, $path = '/', $scheme = 'rest' ) { if ( empty( $path ) ) { @@ -589,6 +591,8 @@ function get_rest_url( $blog_id = null, $path = '/', $scheme = 'rest' ) { * @param string $path REST route. * @param int|null $blog_id Blog ID. * @param string $scheme Sanitization scheme. + * + * @phpstan-param non-falsy-string $url */ return apply_filters( 'rest_url', $url, $path, $blog_id, $scheme ); } @@ -603,6 +607,8 @@ function get_rest_url( $blog_id = null, $path = '/', $scheme = 'rest' ) { * @param string $path Optional. REST route. Default empty. * @param string $scheme Optional. Sanitization scheme. Default 'rest'. * @return string Full URL to the endpoint. + * + * @phpstan-return non-falsy-string */ function rest_url( $path = '', $scheme = 'rest' ) { return get_rest_url( null, $path, $scheme ); From 8d7f744761f74bd23e04842397be45bcd230b631 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Sat, 7 Nov 2026 13:20:25 -0800 Subject: [PATCH 03/13] Query: Type the found posts count as a non-negative integer `WP_Query::$found_posts` is a count of rows, so document it as `int<0, max>`, matching `$max_num_pages`. The count is set from `FOUND_ROWS()`, from the number of posts, or from the `found_posts` filter. Document the filter's `$found_posts` as `int<0, max>` too, and clamp the result of the `found_posts_query` query to zero, as is already done for `$max_num_pages`, since that query is itself filterable. `wp_get_latest_revision_id_and_total_count()` returns the count from `$found_posts`, so its `count` is now `int<0, max>` as well. Regenerate the baseline: the existing `header()` errors for `X-WP-Total` now report `int<0, max>` instead of `int`. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Code-Session-Id: 9bb536c7-7399-4262-80ed-cebfc277b43b --- src/wp-includes/class-wp-query.php | 5 ++++- src/wp-includes/revision.php | 2 ++ tests/phpstan/baselines/argument.type.neon | 21 ++++++++++++++++++--- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/class-wp-query.php b/src/wp-includes/class-wp-query.php index 2b593c0addd6b..0712409094ccc 100644 --- a/src/wp-includes/class-wp-query.php +++ b/src/wp-includes/class-wp-query.php @@ -178,6 +178,7 @@ class WP_Query { * * @since 2.1.0 * @var int + * @phpstan-var int<0, max> */ public $found_posts = 0; @@ -3709,7 +3710,7 @@ private function set_found_posts( $query_vars, $limits ) { */ $found_posts_query = apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ); - $this->found_posts = (int) $wpdb->get_var( $found_posts_query ); + $this->found_posts = max( 0, (int) $wpdb->get_var( $found_posts_query ) ); } else { if ( is_array( $this->posts ) ) { $this->found_posts = count( $this->posts ); @@ -3729,6 +3730,8 @@ private function set_found_posts( $query_vars, $limits ) { * * @param int $found_posts The number of posts found. * @param WP_Query $query The WP_Query instance (passed by reference). + * + * @phpstan-param int<0, max> $found_posts */ $this->found_posts = (int) apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) ); diff --git a/src/wp-includes/revision.php b/src/wp-includes/revision.php index 179a32c2832c5..77b0b6425ecb4 100644 --- a/src/wp-includes/revision.php +++ b/src/wp-includes/revision.php @@ -716,6 +716,8 @@ function wp_get_post_revisions( $post = 0, $args = null ) { * @type int $latest_id The latest revision post ID or 0 if no revisions exist. * @type int $count The total count of revisions for the given post. * } + * + * @phpstan-return array{ latest_id: int, count: int<0, max> }|WP_Error */ function wp_get_latest_revision_id_and_total_count( $post = 0 ) { $post = get_post( $post ); diff --git a/tests/phpstan/baselines/argument.type.neon b/tests/phpstan/baselines/argument.type.neon index 46dbd33173186..0a2f1c721948e 100644 --- a/tests/phpstan/baselines/argument.type.neon +++ b/tests/phpstan/baselines/argument.type.neon @@ -1101,7 +1101,12 @@ parameters: - message: '#^Parameter \#2 \$value of method WP_HTTP_Response\:\:header\(\) expects string, int given\.$#' identifier: argument.type - count: 2 + count: 1 + path: ../../../src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-revisions-controller.php + - + message: '#^Parameter \#2 \$value of method WP_HTTP_Response\:\:header\(\) expects string, int\<0, max\> given\.$#' + identifier: argument.type + count: 1 path: ../../../src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-revisions-controller.php - message: '#^Parameter \#1 \$data_object of method WP_REST_Controller\:\:update_additional_fields_for_object\(\) expects object, array given\.$#' @@ -1111,12 +1116,22 @@ parameters: - message: '#^Parameter \#2 \$value of method WP_HTTP_Response\:\:header\(\) expects string, int given\.$#' identifier: argument.type - count: 2 + count: 1 + path: ../../../src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php + - + message: '#^Parameter \#2 \$value of method WP_HTTP_Response\:\:header\(\) expects string, int\<0, max\> given\.$#' + identifier: argument.type + count: 1 path: ../../../src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php - message: '#^Parameter \#2 \$value of method WP_HTTP_Response\:\:header\(\) expects string, int given\.$#' identifier: argument.type - count: 2 + count: 1 + path: ../../../src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php + - + message: '#^Parameter \#2 \$value of method WP_HTTP_Response\:\:header\(\) expects string, int\<0, max\> given\.$#' + identifier: argument.type + count: 1 path: ../../../src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php - message: '#^Parameter \#2 \$value of method WP_HTTP_Response\:\:header\(\) expects string, int given\.$#' From 993719f23c8ccdd88202cfcd8e7017ffb76d1692 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Sat, 7 Nov 2026 13:20:39 -0800 Subject: [PATCH 04/13] REST API: Add a shape for the global styles controller's links Narrow the return type of `WP_REST_Global_Styles_Controller::prepare_links()` to the links it builds: `self` and `about`, plus `version-history` with its revisions `count` when the post type supports revisions. For `prepare_item_for_response()` to pass these links to `WP_REST_Response::add_links()`, the `count` link attribute is declared in the `Link_Attributes` and `Link_Input` types, as a non-negative integer. It is also used for `version-history` links by the posts and templates controllers. Declaring it is needed because PHPStan merges `Link_Input` with its list form in `add_links()` and drops the unsealed keys, so an attribute not named in the shape is rejected when a caller passes a precise shape. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Code-Session-Id: 9bb536c7-7399-4262-80ed-cebfc277b43b --- src/wp-includes/rest-api/class-wp-rest-response.php | 2 ++ .../endpoints/class-wp-rest-global-styles-controller.php | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/src/wp-includes/rest-api/class-wp-rest-response.php b/src/wp-includes/rest-api/class-wp-rest-response.php index f86976e94dfa0..7ec735a8f7a41 100644 --- a/src/wp-includes/rest-api/class-wp-rest-response.php +++ b/src/wp-includes/rest-api/class-wp-rest-response.php @@ -24,6 +24,7 @@ * @phpstan-type Link_Attributes array{ * embeddable?: bool, * targetHints?: Target_Hints, + * count?: int<0, max>, * ... * } * @phpstan-type Link array{ @@ -34,6 +35,7 @@ * href: non-falsy-string, * embeddable?: bool, * targetHints?: array, // Not narrowed like in Link_Attributes, since PHPStan merges this shape with its list form in add_links(). The merged attributes passed to add_link() there are only accepted while they include `mixed`; with the nested shape, the call would be an argument.type error even when `mixed` is not checked. + * count?: int<0, max>, // Declared rather than left to the unsealed keys, since that merge also drops them: an attribute not named here is rejected when a caller passes a precise shape. * ... * } * @phpstan-type Curie array{ diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php index d19cb1cc22520..58e8a6152c4d2 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php @@ -391,6 +391,12 @@ public function prepare_item_for_response( $post, $request ) { * * @param WP_Post|int $post Post object or ID. * @return array> Links for the given post. + * + * @phpstan-return array{ + * self: array{ href: non-falsy-string }, + * about: array{ href: non-falsy-string }, + * version-history?: array{ href: non-falsy-string, count: int<0, max> }, + * } */ protected function prepare_links( $post ) { $id = $post instanceof WP_Post ? $post->ID : $post; From 509c84fa99aa10a2f40c14cdc27e31fb222b3911 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Sat, 7 Nov 2026 14:13:58 -0800 Subject: [PATCH 05/13] Query: Clamp the filtered found posts count to zero `WP_Query::$found_posts` is documented as `int<0, max>`, but the `found_posts` filter runs after the `FOUND_ROWS()` result is clamped, so a filter returning a negative number still made the count negative. That value then reached the `X-WP-Total` header and, through `wp_get_latest_revision_id_and_total_count()`, the `count` of `version-history` links. Clamp the filtered value to zero as well, as is already done for `$max_num_pages`, and update the test that asserted the negative value was kept. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Code-Session-Id: 9bb536c7-7399-4262-80ed-cebfc277b43b --- src/wp-includes/class-wp-query.php | 2 +- tests/phpunit/tests/query/noFoundRows.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/class-wp-query.php b/src/wp-includes/class-wp-query.php index 0712409094ccc..bc3984d950d00 100644 --- a/src/wp-includes/class-wp-query.php +++ b/src/wp-includes/class-wp-query.php @@ -3733,7 +3733,7 @@ private function set_found_posts( $query_vars, $limits ) { * * @phpstan-param int<0, max> $found_posts */ - $this->found_posts = (int) apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) ); + $this->found_posts = max( 0, (int) apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) ) ); if ( ! empty( $limits ) ) { $this->max_num_pages = max( 0, (int) ceil( $this->found_posts / (int) $query_vars['posts_per_page'] ) ); diff --git a/tests/phpunit/tests/query/noFoundRows.php b/tests/phpunit/tests/query/noFoundRows.php index e9ffcd6581362..e718d3a21a4a4 100644 --- a/tests/phpunit/tests/query/noFoundRows.php +++ b/tests/phpunit/tests/query/noFoundRows.php @@ -104,8 +104,8 @@ public function test_no_found_rows_default_with_postsperpage_minus1() { } /** - * Ensures {@see WP_Query::$max_num_pages} is not negative when the - * {@see 'found_posts'} filter returns a negative count. + * Ensures {@see WP_Query::$found_posts} and {@see WP_Query::$max_num_pages} + * are not negative when the {@see 'found_posts'} filter returns a negative count. * * @ticket 65817 */ @@ -126,7 +126,7 @@ static function (): int { ) ); - $this->assertSame( -5, $q->found_posts, 'Expected the filtered found_posts value to be kept.' ); + $this->assertSame( 0, $q->found_posts, 'Expected found_posts to be clamped to zero.' ); $this->assertSame( 0, $q->max_num_pages, 'Expected max_num_pages to be clamped to zero.' ); } From 81990dc0cae6868648d761d7167a83e564908409 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Sat, 7 Nov 2026 15:01:23 -0800 Subject: [PATCH 06/13] Tests: Rename the negative found posts test to cover both properties The test now asserts that `found_posts` is clamped to zero as well as `max_num_pages`, so its name should say so. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Code-Session-Id: 9bb536c7-7399-4262-80ed-cebfc277b43b --- tests/phpunit/tests/query/noFoundRows.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/query/noFoundRows.php b/tests/phpunit/tests/query/noFoundRows.php index e718d3a21a4a4..baf97ce13e021 100644 --- a/tests/phpunit/tests/query/noFoundRows.php +++ b/tests/phpunit/tests/query/noFoundRows.php @@ -109,7 +109,7 @@ public function test_no_found_rows_default_with_postsperpage_minus1() { * * @ticket 65817 */ - public function test_max_num_pages_is_not_negative_when_found_posts_is_filtered_negative() { + public function test_found_posts_and_max_num_pages_are_not_negative_when_found_posts_is_filtered_negative() { self::factory()->post->create_many( 3 ); add_filter( From 80f20b73804b9b6b918733762eddff7780650103 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Sat, 7 Nov 2026 15:01:43 -0800 Subject: [PATCH 07/13] Query: Note that the found posts filter result is clamped A negative value returned by the `found_posts` filter used to be kept, and is now clamped to zero. Record the change in the filter's docblock. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Code-Session-Id: 9bb536c7-7399-4262-80ed-cebfc277b43b --- src/wp-includes/class-wp-query.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/wp-includes/class-wp-query.php b/src/wp-includes/class-wp-query.php index bc3984d950d00..b3cca26862654 100644 --- a/src/wp-includes/class-wp-query.php +++ b/src/wp-includes/class-wp-query.php @@ -3727,6 +3727,7 @@ private function set_found_posts( $query_vars, $limits ) { * Filters the number of found posts for the query. * * @since 2.1.0 + * @since 7.2.0 A negative return value is clamped to zero. * * @param int $found_posts The number of posts found. * @param WP_Query $query The WP_Query instance (passed by reference). From 874f374d21365e337d82cbf7f7d66eb572f64045 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Sat, 7 Nov 2026 15:02:28 -0800 Subject: [PATCH 08/13] REST API: Pass the global styles 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. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Code-Session-Id: 9bb536c7-7399-4262-80ed-cebfc277b43b --- .../endpoints/class-wp-rest-global-styles-controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php index 58e8a6152c4d2..30898256eb88c 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php @@ -412,7 +412,7 @@ protected function prepare_links( $post ) { ); if ( post_type_supports( $this->post_type, 'revisions' ) ) { - $revisions = wp_get_latest_revision_id_and_total_count( $id ); + $revisions = wp_get_latest_revision_id_and_total_count( $post ); $revisions_count = ! is_wp_error( $revisions ) ? $revisions['count'] : 0; $revisions_base = sprintf( '/%s/%d/revisions', $base, $id ); $links['version-history'] = array( From c2c4d3af9df9976f5a914b343d88a4edacf78912 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Sat, 7 Nov 2026 15:06:51 -0800 Subject: [PATCH 09/13] Tests: Cover the found rows clamp in WP_Query The result of the `found_posts_query` query is clamped to zero before it is passed to the `found_posts` filter, but only the filter's own clamp was tested. Add a test where `found_posts_query` returns a negative number, asserting that the filter receives zero. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Code-Session-Id: 9bb536c7-7399-4262-80ed-cebfc277b43b --- tests/phpunit/tests/query/noFoundRows.php | 37 +++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/tests/phpunit/tests/query/noFoundRows.php b/tests/phpunit/tests/query/noFoundRows.php index baf97ce13e021..97ca4c36f0bdd 100644 --- a/tests/phpunit/tests/query/noFoundRows.php +++ b/tests/phpunit/tests/query/noFoundRows.php @@ -130,6 +130,43 @@ static function (): int { $this->assertSame( 0, $q->max_num_pages, 'Expected max_num_pages to be clamped to zero.' ); } + /** + * Ensures the count passed to the {@see 'found_posts'} filter is not negative + * when the {@see 'found_posts_query'} filter makes the query return a negative number. + * + * @ticket 65817 + */ + public function test_found_posts_is_not_negative_when_found_posts_query_is_filtered_negative() { + self::factory()->post->create_many( 3 ); + + add_filter( + 'found_posts_query', + static function (): string { + return 'SELECT -5'; + } + ); + + $filtered_found_posts = null; + add_filter( + 'found_posts', + static function ( int $found_posts ) use ( &$filtered_found_posts ): int { + $filtered_found_posts = $found_posts; + return $found_posts; + } + ); + + $q = new WP_Query( + array( + 'post_type' => 'post', + 'posts_per_page' => 2, + ) + ); + + $this->assertSame( 0, $filtered_found_posts, 'Expected the count passed to the found_posts filter to be clamped to zero.' ); + $this->assertSame( 0, $q->found_posts, 'Expected found_posts to be clamped to zero.' ); + $this->assertSame( 0, $q->max_num_pages, 'Expected max_num_pages to be zero.' ); + } + /** * Ensures {@see WP_Query::$max_num_pages} is not negative when a * {@see 'post_limits'} filter injects a non-empty `LIMIT` clause on a From 204f7fe51fd0dcde1523b10aef4dc10def94e0d7 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Sat, 7 Nov 2026 15:15:11 -0800 Subject: [PATCH 10/13] REST API: Keep passing the post ID to the global styles 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 the ID it has received since 5.9.0, for example by concatenating it into a URL. Pass the ID from `prepare_item_for_response()` as before, so such an override keeps working. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Code-Session-Id: 9bb536c7-7399-4262-80ed-cebfc277b43b --- .../endpoints/class-wp-rest-global-styles-controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php index 30898256eb88c..a30a6d9105cbf 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php @@ -359,7 +359,7 @@ public function prepare_item_for_response( $post, $request ) { $response = rest_ensure_response( $data ); if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) { - $links = $this->prepare_links( $post ); + $links = $this->prepare_links( $post->ID ); // Only return resolved URIs for get requests to user theme JSON. if ( $theme_json ) { From f90036b7ba8838b639150459255cd2286be70149 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Sat, 7 Nov 2026 15:17:45 -0800 Subject: [PATCH 11/13] Tests: Cover the global styles 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. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Code-Session-Id: 9bb536c7-7399-4262-80ed-cebfc277b43b --- .../rest-global-styles-controller.php | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/tests/phpunit/tests/rest-api/rest-global-styles-controller.php b/tests/phpunit/tests/rest-api/rest-global-styles-controller.php index 518203e37c1b7..0b7064774d678 100644 --- a/tests/phpunit/tests/rest-api/rest-global-styles-controller.php +++ b/tests/phpunit/tests/rest-api/rest-global-styles-controller.php @@ -588,6 +588,60 @@ public function test_get_item() { $this->assertStringContainsString( '/wp/v2/global-styles/' . self::$global_styles_id, $links['self'][0]['href'] ); } + /** + * Ensures the version-history link points to the revisions and counts them. + * + * @ticket 65817 + * + * @covers WP_REST_Global_Styles_Controller::prepare_links + */ + public function test_get_item_version_history_link(): void { + wp_update_post( + array( + 'ID' => self::$global_styles_id, + 'post_content' => '{"version": ' . WP_Theme_JSON::LATEST_SCHEMA . ', "isGlobalStylesUserThemeJSON": true, "styles": { "color": { "text": "red" } } }', + ) + ); + wp_update_post( + array( + 'ID' => self::$global_styles_id, + 'post_content' => '{"version": ' . WP_Theme_JSON::LATEST_SCHEMA . ', "isGlobalStylesUserThemeJSON": true, "styles": { "color": { "text": "blue" } } }', + ) + ); + $revisions_count = count( wp_get_post_revisions( self::$global_styles_id ) ); + $this->assertGreaterThan( 0, $revisions_count, 'Expected the updates to create revisions.' ); + + wp_set_current_user( self::$admin_id ); + $request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id ); + $response = rest_get_server()->dispatch( $request ); + $links = $response->get_links(); + + $this->assertArrayHasKey( 'version-history', $links ); + $this->assertSame( rest_url( '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' ), $links['version-history'][0]['href'] ); + $this->assertArrayHasKey( 'count', $links['version-history'][0]['attributes'] ); + $this->assertSame( $revisions_count, $links['version-history'][0]['attributes']['count'] ); + } + + /** + * Ensures that links are prepared the same from a post ID as from a post object. + * + * @ticket 65817 + * + * @covers WP_REST_Global_Styles_Controller::prepare_links + */ + public function test_prepare_links_accepts_post_id_or_object(): void { + $controller = new WP_REST_Global_Styles_Controller( 'wp_global_styles' ); + $post_id = self::$global_styles_id; + $post = get_post( $post_id ); + $this->assertInstanceOf( WP_Post::class, $post ); + + $links_from_id = Closure::bind( fn () => $this->prepare_links( $post_id ), $controller, $controller )(); + $links_from_post = Closure::bind( fn () => $this->prepare_links( $post ), $controller, $controller )(); + + $this->assertSame( rest_url( '/wp/v2/global-styles/' . $post_id ), $links_from_id['self']['href'] ); + $this->assertSame( $links_from_id, $links_from_post ); + } + /** * Global styles has no collection create route; create requests should not match a route. * From 8dcc17b7f62daea8999b05ef47001ebd49243c9c Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 9 Nov 2026 17:53:28 -0800 Subject: [PATCH 12/13] REST API: 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. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Code-Session-Id: 9bb536c7-7399-4262-80ed-cebfc277b43b --- .../endpoints/class-wp-rest-global-styles-controller.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php index a30a6d9105cbf..daa83ba3526b7 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php @@ -359,6 +359,7 @@ public function prepare_item_for_response( $post, $request ) { $response = rest_ensure_response( $data ); if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) { + // The ID is passed on purpose, since subclasses that override prepare_links() may expect an ID rather than a post object. $links = $this->prepare_links( $post->ID ); // Only return resolved URIs for get requests to user theme JSON. From 887706a787433fc66129a68f954133d11b3ec00a Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 9 Nov 2026 17:53:41 -0800 Subject: [PATCH 13/13] Query: Note that found_posts callbacks no longer receive a negative count Extend the `@since 7.2.0` note on the `found_posts` filter: besides its return value being clamped, the value passed to it is now zero when the `found_posts_query` query returns a negative count. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Code-Session-Id: 9bb536c7-7399-4262-80ed-cebfc277b43b --- src/wp-includes/class-wp-query.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-query.php b/src/wp-includes/class-wp-query.php index b3cca26862654..998fe477d48d2 100644 --- a/src/wp-includes/class-wp-query.php +++ b/src/wp-includes/class-wp-query.php @@ -3727,7 +3727,8 @@ private function set_found_posts( $query_vars, $limits ) { * Filters the number of found posts for the query. * * @since 2.1.0 - * @since 7.2.0 A negative return value is clamped to zero. + * @since 7.2.0 A negative return value is clamped to zero, as is a negative count from the + * {@see 'found_posts_query'} query before it is passed to this filter. * * @param int $found_posts The number of posts found. * @param WP_Query $query The WP_Query instance (passed by reference).