diff --git a/src/wp-includes/class-wp-query.php b/src/wp-includes/class-wp-query.php index 2b593c0addd6b..998fe477d48d2 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 ); @@ -3726,11 +3727,15 @@ 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, 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). + * + * @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/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 ); 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 e9f3b281ed773..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. @@ -387,11 +388,19 @@ 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. + * + * @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( $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( @@ -404,7 +413,7 @@ protected function prepare_links( $id ) { ); 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( 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\.$#' 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 diff --git a/tests/phpunit/tests/query/noFoundRows.php b/tests/phpunit/tests/query/noFoundRows.php index e9ffcd6581362..97ca4c36f0bdd 100644 --- a/tests/phpunit/tests/query/noFoundRows.php +++ b/tests/phpunit/tests/query/noFoundRows.php @@ -104,12 +104,12 @@ 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 */ - 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( @@ -126,10 +126,47 @@ 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.' ); } + /** + * 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 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. *