Make WordPress Core

Changeset 25248


Ignore:
Timestamp:
09/04/2013 09:32:11 PM (11 years ago)
Author:
wonderboymusic
Message:

Introduce author__in and author__not_in query vars. Fixes issue with multiple author exclusion when comma-separated string is passed for author. Adds a bunch of missing unit tests.

Props pollett for initial patch.
Fixes #16854.

Location:
trunk
Files:
2 edited

Legend:

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

    r25239 r25248  
    13971397            , 'cat'
    13981398            , 'tag_id'
     1399            , 'author'
    13991400            , 'author_name'
    14001401            , 'feed'
     
    14171418
    14181419        $array_keys = array( 'category__in', 'category__not_in', 'category__and', 'post__in', 'post__not_in',
    1419             'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and', 'post_parent__in', 'post_parent__not_in' );
     1420            'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and', 'post_parent__in', 'post_parent__not_in',
     1421            'author__in', 'author__not_in' );
    14201422
    14211423        foreach ( $array_keys as $key ) {
     
    14581460        $qv['paged'] = absint($qv['paged']);
    14591461        $qv['cat'] = preg_replace( '|[^0-9,-]|', '', $qv['cat'] ); // comma separated list of positive or negative integers
     1462        $qv['author'] = preg_replace( '|[^0-9,-]|', '', $qv['author'] ); // comma separated list of positive or negative integers
    14601463        $qv['pagename'] = trim( $qv['pagename'] );
    14611464        $qv['name'] = trim( $qv['name'] );
     
    17701773            unset( $q['category__and'] );
    17711774        }
    1772            
     1775
    17731776        if ( ! empty( $q['category__in'] ) ) {
    1774             $q['category__in'] = array_map( 'absint', array_unique( (array) $q['category__in'] ) );         
     1777            $q['category__in'] = array_map( 'absint', array_unique( (array) $q['category__in'] ) );
    17751778            $tax_query[] = array(
    17761779                'taxonomy' => 'category',
     
    23342337        // Author/user stuff
    23352338
    2336         if ( empty($q['author']) || ($q['author'] == '0') ) {
    2337             $whichauthor = '';
    2338         } else {
    2339             $q['author'] = (string)urldecode($q['author']);
    2340             $q['author'] = addslashes_gpc($q['author']);
    2341             if ( strpos($q['author'], '-') !== false ) {
    2342                 $eq = '!=';
    2343                 $andor = 'AND';
    2344                 $q['author'] = explode('-', $q['author']);
    2345                 $q['author'] = (string)absint($q['author'][1]);
    2346             } else {
    2347                 $eq = '=';
    2348                 $andor = 'OR';
    2349             }
    2350             $author_array = preg_split('/[,\s]+/', $q['author']);
    2351             $_author_array = array();
    2352             foreach ( $author_array as $key => $_author )
    2353                 $_author_array[] = "$wpdb->posts.post_author " . $eq . ' ' . absint($_author);
    2354             $whichauthor .= ' AND (' . implode(" $andor ", $_author_array) . ')';
    2355             unset($author_array, $_author_array);
     2339        if ( ! empty( $q['author'] ) && $q['author'] != '0' ) {
     2340            $q['author'] = addslashes_gpc( '' . urldecode( $q['author'] ) );
     2341            $authors = array_unique( array_map( 'intval', preg_split( '/[,\s]+/', $q['author'] ) ) );
     2342            foreach ( $authors as $author ) {
     2343                $key = $author > 0 ? 'author__in' : 'author__not_in';
     2344                $q[$key][] = abs( $author );
     2345            }
     2346            $q['author'] = implode( ',', $authors );
     2347        }
     2348
     2349        if ( ! empty( $q['author__not_in'] ) ) {
     2350            $author__not_in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__not_in'] ) ) );
     2351            $where .= " AND {$wpdb->posts}.post_author NOT IN ($author__not_in) ";
     2352        } elseif ( ! empty( $q['author__in'] ) ) {
     2353            $author__in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__in'] ) ) );
     2354            $where .= " AND {$wpdb->posts}.post_author IN ($author__in) ";
    23562355        }
    23572356
     
    24582457            if ( empty( $in_search_post_types ) )
    24592458                $where .= ' AND 1=0 ';
    2460             else   
     2459            else
    24612460                $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $in_search_post_types ) . "')";
    24622461        } elseif ( !empty( $post_type ) && is_array( $post_type ) ) {
  • trunk/tests/phpunit/tests/query/results.php

    r25239 r25248  
    389389        $this->assertNotRegExp( '#AND 1=0#', $this->q->request );
    390390    }
     391
     392    function test_query_author_vars() {
     393        $author_1 = $this->factory->user->create( array( 'user_login' => 'admin1', 'user_pass' => rand_str(), 'role' => 'author' ) );
     394        $post_1 = $this->factory->post->create( array( 'post_title' => rand_str(), 'post_author' => $author_1, 'post_date' => '2007-01-01 00:00:00' ) );
     395
     396        $author_2 = $this->factory->user->create( array( 'user_login' => rand_str(), 'user_pass' => rand_str(), 'role' => 'author' ) );
     397        $post_2 = $this->factory->post->create( array( 'post_title' => rand_str(), 'post_author' => $author_2, 'post_date' => '2007-01-01 00:00:00' ) );
     398
     399        $author_3 = $this->factory->user->create( array( 'user_login' => rand_str(), 'user_pass' => rand_str(), 'role' => 'author' ) );
     400        $post_3 = $this->factory->post->create( array( 'post_title' => rand_str(), 'post_author' => $author_3, 'post_date' => '2007-01-01 00:00:00' ) );
     401
     402        $author_4 = $this->factory->user->create( array( 'user_login' => rand_str(), 'user_pass' => rand_str(), 'role' => 'author' ) );
     403        $post_4 = $this->factory->post->create( array( 'post_title' => rand_str(), 'post_author' => $author_4, 'post_date' => '2007-01-01 00:00:00' ) );
     404
     405        $posts = $this->q->query( array(
     406            'author' => '',
     407            'post__in' => array( $post_1, $post_2, $post_3, $post_4 )
     408        ) );
     409        $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) );
     410        $this->assertEqualSets( array( $author_1, $author_2, $author_3, $author_4 ), $author_ids );
     411
     412        $posts = $this->q->query( array(
     413            'author' => 0,
     414            'post__in' => array( $post_1, $post_2, $post_3, $post_4 )
     415        ) );
     416        $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) );
     417        $this->assertEqualSets( array( $author_1, $author_2, $author_3, $author_4 ), $author_ids );
     418
     419        $posts = $this->q->query( array(
     420            'author' => '0',
     421            'post__in' => array( $post_1, $post_2, $post_3, $post_4 )
     422        ) );
     423        $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) );
     424        $this->assertEqualSets( array( $author_1, $author_2, $author_3, $author_4 ), $author_ids );
     425
     426        $posts = $this->q->query( array(
     427            'author' => $author_1,
     428            'post__in' => array( $post_1, $post_2, $post_3, $post_4 )
     429        ) );
     430        $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) );
     431        $this->assertEqualSets( array( $author_1 ), $author_ids );
     432
     433        $posts = $this->q->query( array(
     434            'author' => "$author_1",
     435            'post__in' => array( $post_1, $post_2, $post_3, $post_4 )
     436        ) );
     437        $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) );
     438        $this->assertEqualSets( array( $author_1 ), $author_ids );
     439
     440        $posts = $this->q->query( array(
     441            'author' => "{$author_1},{$author_2}",
     442            'post__in' => array( $post_1, $post_2, $post_3, $post_4 )
     443        ) );
     444        $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) );
     445        $this->assertEqualSets( array( $author_1, $author_2 ), $author_ids );
     446
     447        $posts = $this->q->query( array(
     448            'author' => "-{$author_1},{$author_2}",
     449            'post__in' => array( $post_1, $post_2, $post_3, $post_4 )
     450        ) );
     451        $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) );
     452        $this->assertEqualSets( array( $author_2, $author_3, $author_4 ), $author_ids );
     453
     454        $posts = $this->q->query( array(
     455            'author' => "{$author_1},-{$author_2}",
     456            'post__in' => array( $post_1, $post_2, $post_3, $post_4 )
     457        ) );
     458        $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) );
     459        $this->assertEqualSets( array( $author_1, $author_3, $author_4 ), $author_ids );
     460
     461        $posts = $this->q->query( array(
     462            'author' => "-{$author_1},-{$author_2}",
     463            'post__in' => array( $post_1, $post_2, $post_3, $post_4 )
     464        ) );
     465        $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) );
     466        $this->assertEqualSets( array( $author_3, $author_4 ), $author_ids );
     467
     468        $posts = $this->q->query( array(
     469            'author__in' => array( $author_1, $author_2 ),
     470            'post__in' => array( $post_1, $post_2, $post_3, $post_4 )
     471        ) );
     472        $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) );
     473        $this->assertEqualSets( array( $author_1, $author_2 ), $author_ids );
     474
     475        $posts = $this->q->query( array(
     476            'author__not_in' => array( $author_1, $author_2 ),
     477            'post__in' => array( $post_1, $post_2, $post_3, $post_4 )
     478        ) );
     479        $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) );
     480        $this->assertEqualSets( array( $author_3, $author_4 ), $author_ids );
     481
     482        $posts = $this->q->query( array(
     483            'author_name' => 'admin1',
     484            'post__in' => array( $post_1, $post_2, $post_3, $post_4 )
     485        ) );
     486        $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) );
     487        $this->assertEqualSets( array( $author_1 ), $author_ids );
     488    }
    391489}
Note: See TracChangeset for help on using the changeset viewer.