Make WordPress Core

Changeset 48328


Ignore:
Timestamp:
07/05/2020 09:32:26 PM (3 years ago)
Author:
SergeyBiryukov
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-query.php

    r48114 r48328  
    32363236             * @since 2.1.0
    32373237             *
    3238              * @param string   $found_posts The query to run to find the found posts.
    3239              * @param WP_Query $this        The WP_Query instance (passed by reference).
     3238             * @param string   $found_posts_query The query to run to find the found posts.
     3239             * @param WP_Query $this              The WP_Query instance (passed by reference).
    32403240             */
    3241             $this->found_posts = $wpdb->get_var( apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ) );
     3241            $found_posts_query = apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) );
     3242
     3243            $this->found_posts = (int) $wpdb->get_var( $found_posts_query );
    32423244        } else {
    32433245            if ( is_array( $this->posts ) ) {
     
    32603262         * @param WP_Query $this        The WP_Query instance (passed by reference).
    32613263         */
    3262         $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );
     3264        $this->found_posts = (int) apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );
    32633265
    32643266        if ( ! empty( $limits ) ) {
  • trunk/tests/phpunit/tests/post/query.php

    r47122 r48328  
    653653
    654654        add_filter( 'split_the_query', '__return_true' );
     655
    655656        $q = new WP_Query(
    656657            array(
     
    659660            )
    660661        );
     662
    661663        remove_filter( 'split_the_query', '__return_true' );
    662664
     
    678680        // ! $split_the_query
    679681        add_filter( 'split_the_query', '__return_false' );
     682
    680683        $q = new WP_Query(
    681684            array(
     
    684687            )
    685688        );
     689
    686690        remove_filter( 'split_the_query', '__return_false' );
    687691
     
    722726    }
    723727
     728    /**
     729     * @ticket 42469
     730     */
     731    public function test_found_posts_should_be_integer_not_string() {
     732        $this->post_id = self::factory()->post->create();
     733
     734        $q = new WP_Query(
     735            array(
     736                'posts_per_page' => 1,
     737            )
     738        );
     739
     740        $this->assertInternalType( 'int', $q->found_posts );
     741    }
     742
     743    /**
     744     * @ticket 42469
     745     */
     746    public function test_found_posts_should_be_integer_even_if_found_posts_filter_returns_string_value() {
     747        $this->post_id = self::factory()->post->create();
     748
     749        add_filter( 'found_posts', '__return_empty_string' );
     750
     751        $q = new WP_Query(
     752            array(
     753                'posts_per_page' => 1,
     754            )
     755        );
     756
     757        remove_filter( 'found_posts', '__return_empty_string' );
     758
     759        $this->assertInternalType( 'int', $q->found_posts );
     760    }
    724761}
Note: See TracChangeset for help on using the changeset viewer.