From 530eedb538125b92ea1e0508db41fda99d321b4b Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Sun, 5 Jul 2020 21:32:26 +0000 Subject: [PATCH] Query: Make sure the `found_posts` property of `WP_Query` is always an integer, to match the documented type. This makes the property consistent with similar properties of other classes: * `WP_Comment_Query::$found_comments` * `WP_Network_Query::$found_networks` * `WP_Site_Query::$found_sites` * `WP_User_Query::$total_users` Props birgire, PressLabs. Fixes #42469. git-svn-id: https://develop.svn.wordpress.org/trunk@48328 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp-query.php | 10 ++++---- tests/phpunit/tests/post/query.php | 37 ++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/class-wp-query.php b/src/wp-includes/class-wp-query.php index 06d531c39a..6508e7f64e 100644 --- a/src/wp-includes/class-wp-query.php +++ b/src/wp-includes/class-wp-query.php @@ -3235,10 +3235,12 @@ class WP_Query { * * @since 2.1.0 * - * @param string $found_posts The query to run to find the found posts. - * @param WP_Query $this The WP_Query instance (passed by reference). + * @param string $found_posts_query The query to run to find the found posts. + * @param WP_Query $this The WP_Query instance (passed by reference). */ - $this->found_posts = $wpdb->get_var( apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ) ); + $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 ); } else { if ( is_array( $this->posts ) ) { $this->found_posts = count( $this->posts ); @@ -3259,7 +3261,7 @@ class WP_Query { * @param int $found_posts The number of posts found. * @param WP_Query $this The WP_Query instance (passed by reference). */ - $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) ); + $this->found_posts = (int) apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) ); if ( ! empty( $limits ) ) { $this->max_num_pages = ceil( $this->found_posts / $q['posts_per_page'] ); diff --git a/tests/phpunit/tests/post/query.php b/tests/phpunit/tests/post/query.php index 0659f2d613..5f131f5200 100644 --- a/tests/phpunit/tests/post/query.php +++ b/tests/phpunit/tests/post/query.php @@ -652,12 +652,14 @@ class Tests_Post_Query extends WP_UnitTestCase { } add_filter( 'split_the_query', '__return_true' ); + $q = new WP_Query( array( 'post_type' => 'wptests_pt', 'posts_per_page' => 1, ) ); + remove_filter( 'split_the_query', '__return_true' ); $this->assertEquals( 2, $q->found_posts ); @@ -677,12 +679,14 @@ class Tests_Post_Query extends WP_UnitTestCase { // ! $split_the_query add_filter( 'split_the_query', '__return_false' ); + $q = new WP_Query( array( 'post_type' => 'wptests_pt', 'posts_per_page' => 1, ) ); + remove_filter( 'split_the_query', '__return_false' ); $this->assertEquals( 2, $q->found_posts ); @@ -721,4 +725,37 @@ class Tests_Post_Query extends WP_UnitTestCase { $this->assertEquals( $expected, $q->found_posts ); } + /** + * @ticket 42469 + */ + public function test_found_posts_should_be_integer_not_string() { + $this->post_id = self::factory()->post->create(); + + $q = new WP_Query( + array( + 'posts_per_page' => 1, + ) + ); + + $this->assertInternalType( 'int', $q->found_posts ); + } + + /** + * @ticket 42469 + */ + public function test_found_posts_should_be_integer_even_if_found_posts_filter_returns_string_value() { + $this->post_id = self::factory()->post->create(); + + add_filter( 'found_posts', '__return_empty_string' ); + + $q = new WP_Query( + array( + 'posts_per_page' => 1, + ) + ); + + remove_filter( 'found_posts', '__return_empty_string' ); + + $this->assertInternalType( 'int', $q->found_posts ); + } }