Make WordPress Core

Ticket #42469: 42469.3.diff

File 42469.3.diff, 2.9 KB (added by birgire, 7 years ago)
  • src/wp-includes/class-wp-query.php

    diff --git src/wp-includes/class-wp-query.php src/wp-includes/class-wp-query.php
    index afc8f96..78eb66d 100644
    class WP_Query { 
    30303030                         * @param string   $found_posts The query to run to find the found posts.
    30313031                         * @param WP_Query $this        The WP_Query instance (passed by reference).
    30323032                         */
    3033                         $this->found_posts = $wpdb->get_var( apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ) );
     3033                        $found_posts_query = apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) );
     3034
     3035                        $this->found_posts = (int) $wpdb->get_var( $found_posts_query );
    30343036                } else {
    30353037                        $this->found_posts = count( $this->posts );
    30363038                }
    class WP_Query { 
    30433045                 * @param int      $found_posts The number of posts found.
    30443046                 * @param WP_Query $this        The WP_Query instance (passed by reference).
    30453047                 */
    3046                 $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );
     3048                $this->found_posts = (int) apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );
    30473049
    30483050                if ( ! empty( $limits ) )
    30493051                        $this->max_num_pages = ceil( $this->found_posts / $q['posts_per_page'] );
  • tests/phpunit/tests/post/query.php

    diff --git tests/phpunit/tests/post/query.php tests/phpunit/tests/post/query.php
    index 6ae8114..6fb9d91 100644
    class Tests_Post_Query extends WP_UnitTestCase { 
    535535                $this->assertEquals( 2, $q->found_posts );
    536536                $this->assertEquals( 2, $q->max_num_pages );
    537537        }
     538
     539        /**
     540         * The found_posts property should be of type int, not string.
     541         *
     542         * @ticket 42469
     543         */
     544        public function test_found_posts_should_be_integer_not_string() {
     545                $this->post_id = self::factory()->post->create();
     546
     547                $q = new WP_Query( array(
     548                        'posts_per_page' => 1,
     549                ) );
     550
     551                $this->assertTrue( is_int( $q->found_posts ) );
     552                $this->assertFalse( is_string( $q->found_posts ) );
     553        }
     554
     555        /**
     556         * The found_posts property should be of type int, even if the found_posts filter returns a string value.
     557         *
     558         * @ticket 42469
     559         */
     560        public function test_found_posts_should_be_integer_even_if_found_posts_filter_returns_string_value() {
     561                $this->post_id = self::factory()->post->create();
     562
     563                add_filter( 'found_posts', array( $this, 'filter_found_posts_string' ) );
     564
     565                $q = new WP_Query( array(
     566                        'posts_per_page' => 1,
     567                ) );
     568
     569                remove_filter( 'found_posts', array( $this, 'filter_found_posts_string' ) );
     570
     571                $this->assertTrue( is_int( $q->found_posts ) );
     572                $this->assertFalse( is_string( $q->found_posts ) );
     573        }
     574
     575        /**
     576         * Filter callback - string casting.
     577         *
     578         * @param  int $found_posts Found posts.
     579         * @return string $found_posts Found posts as string value.
     580         */
     581        public function filter_found_posts_string( $found_posts ) {
     582                return (string) $found_posts;
     583        }
    538584}