Make WordPress Core

Changeset 28664


Ignore:
Timestamp:
06/04/2014 05:49:26 PM (11 years ago)
Author:
wonderboymusic
Message:

After [28613], also kill queries that explicityly pass empty arrays to category__in, tag__in, tag_slug__in, and author__in to WP_Query.

Adds unit tests.
Fixes #28099.

Location:
trunk
Files:
3 edited

Legend:

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

    r28623 r28664  
    18311831                'include_children' => false
    18321832            );
     1833        } elseif ( isset( $this->query['category__in'] ) ) {
     1834            $q['category__in'] = false;
    18331835        }
    18341836
     
    18881890                'terms' => $q['tag__in']
    18891891            );
     1892        } elseif ( isset( $this->query['tag__in'] ) ) {
     1893            $q['tag__in'] = false;
    18901894        }
    18911895
     
    19151919                'field' => 'slug'
    19161920            );
     1921        } elseif ( isset( $this->query['tag_slug__in'] ) ) {
     1922            $q['tag_slug__in'] = false;
    19171923        }
    19181924
     
    25012507        }
    25022508
     2509        // If *__in is passed to WP_Query as an empty array, don't return results
     2510        foreach ( array( 'category', 'tag', 'tag_slug' ) as $in ) {
     2511            if ( isset( $q["{$in}__in"] ) && false === $q["{$in}__in"] ) {
     2512                $where = " AND 1=0 $where";
     2513            }
     2514        }
     2515
    25032516        if ( $this->is_tax ) {
    25042517            if ( empty($post_type) ) {
     
    25922605            $author__in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__in'] ) ) );
    25932606            $where .= " AND {$wpdb->posts}.post_author IN ($author__in) ";
     2607        } elseif ( isset( $this->query['author__in'] ) ) {
     2608            $author__in = 0;
     2609            $where .= ' AND 1=0 ';
    25942610        }
    25952611
  • trunk/tests/phpunit/tests/query/results.php

    r28613 r28664  
    514514        $this->assertEqualSets( array( $author_1, $author_2 ), $author_ids );
    515515
     516        $posts = $this->q->query( array( 'author__in' => array() ) );
     517        $this->assertEmpty( $posts );
     518
    516519        $posts = $this->q->query( array(
    517520            'author__not_in' => array( $author_1, $author_2 ),
  • trunk/tests/phpunit/tests/term/query.php

    r28647 r28664  
    203203        $this->assertEquals( array( $posts[0], $posts[3] ), $results2, 'Relation: AND; Operator: IN' );
    204204    }
     205
     206    function test_empty__in() {
     207        $cat_id = $this->factory->category->create();
     208        $post_id = $this->factory->post->create();
     209        wp_set_post_categories( $post_id, $cat_id );
     210
     211        $q1 = get_posts( array( 'category__in' => array( $cat_id ) ) );
     212        $this->assertNotEmpty( $q1 );
     213        $q2 = get_posts( array( 'category__in' => array() ) );
     214        $this->assertEmpty( $q2 );
     215
     216        $tag = wp_insert_term( 'woo', 'post_tag' );
     217        $tag_id = $tag['term_id'];
     218        $slug = get_tag( $tag_id )->slug;
     219        wp_set_post_tags( $post_id, $slug );
     220
     221        $q3 = get_posts( array( 'tag__in' => array( $tag_id ) ) );
     222        $this->assertNotEmpty( $q3 );
     223        $q4 = get_posts( array( 'tag__in' => array() ) );
     224        $this->assertEmpty( $q4 );
     225
     226        $q5 = get_posts( array( 'tag_slug__in' => array( $slug ) ) );
     227        $this->assertNotEmpty( $q5 );
     228        $q6 = get_posts( array( 'tag_slug__in' => array() ) );
     229        $this->assertEmpty( $q6 );
     230    }
    205231}
Note: See TracChangeset for help on using the changeset viewer.