Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/wp-includes/class-wp-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ class WP_Query {
*
* @since 2.1.0
* @var int
* @phpstan-var int<0, max>
*/
public $found_posts = 0;

Expand Down Expand Up @@ -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 ) );

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.

This also changes what found_posts filter callbacks receive when found_posts_query returns a negative number: they now get 0. Unlikely in practice, but maybe the @since 7.2.0 note on the filter could mention this too, not just the clamped return value?

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.

Added in 887706a

} else {
if ( is_array( $this->posts ) ) {
$this->found_posts = count( $this->posts );
Expand All @@ -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'] ) );
Expand Down
6 changes: 6 additions & 0 deletions src/wp-includes/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) ) {
Expand Down Expand Up @@ -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 );
}
Expand All @@ -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 );
Expand Down
2 changes: 2 additions & 0 deletions src/wp-includes/rest-api/class-wp-rest-response.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* @phpstan-type Link_Attributes array{
* embeddable?: bool,
* targetHints?: Target_Hints,
* count?: int<0, max>,
* ...<string, mixed>
* }
* @phpstan-type Link array{
Expand All @@ -34,6 +35,7 @@
* href: non-falsy-string,
* embeddable?: bool,
* targetHints?: array<string, mixed>, // 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.
* ...<string, mixed>
* }
* @phpstan-type Curie array{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<string, array<string, string|int>> 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 ) {

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.

Nit: prepare_item_for_response() still calls this with $post->ID (line 362), which makes sense for subclasses that override prepare_links() and expect an ID. Could we add a short comment at that call site saying it's on purpose? Otherwise someone may "simplify" it to $post later.

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.

Yeah, good point.

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.

Added in 887706a

$id = $post instanceof WP_Post ? $post->ID : $post;
$base = sprintf( '%s/%s', $this->namespace, $this->rest_base );

$links = array(
Expand All @@ -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(
Expand Down
2 changes: 2 additions & 0 deletions src/wp-includes/revision.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment thread
westonruter marked this conversation as resolved.
Resolved
*/
function wp_get_latest_revision_id_and_total_count( $post = 0 ) {
$post = get_post( $post );
Expand Down
21 changes: 18 additions & 3 deletions tests/phpstan/baselines/argument.type.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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\.$#'
Expand All @@ -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\.$#'
Expand Down
5 changes: 0 additions & 5 deletions tests/phpstan/baselines/method.childParameterType.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 41 additions & 4 deletions tests/phpunit/tests/query/noFoundRows.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down
54 changes: 54 additions & 0 deletions tests/phpunit/tests/rest-api/rest-global-styles-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Loading