Make WordPress Core

Ticket #16854: 16854.2.diff

File 16854.2.diff, 7.9 KB (added by wonderboymusic, 12 years ago)
  • tests/phpunit/tests/query/results.php

     
    369369                        'child-two',
    370370                ), wp_list_pluck( $posts, 'post_title' ) );
    371371        }
     372
     373        /**
     374         * @ticket 16854
     375         */
     376        function test_query_author_vars() {
     377                $author_1 = $this->factory->user->create( array( 'user_login' => 'admin1', 'user_pass' => rand_str(), 'role' => 'author' ) );
     378                $post_1 = $this->factory->post->create( array( 'post_title' => rand_str(), 'post_author' => $author_1, 'post_date' => '2007-01-01 00:00:00' ) );
     379
     380                $author_2 = $this->factory->user->create( array( 'user_login' => rand_str(), 'user_pass' => rand_str(), 'role' => 'author' ) );
     381                $post_2 = $this->factory->post->create( array( 'post_title' => rand_str(), 'post_author' => $author_2, 'post_date' => '2007-01-01 00:00:00' ) );
     382
     383                $author_3 = $this->factory->user->create( array( 'user_login' => rand_str(), 'user_pass' => rand_str(), 'role' => 'author' ) );
     384                $post_3 = $this->factory->post->create( array( 'post_title' => rand_str(), 'post_author' => $author_3, 'post_date' => '2007-01-01 00:00:00' ) );
     385
     386                $author_4 = $this->factory->user->create( array( 'user_login' => rand_str(), 'user_pass' => rand_str(), 'role' => 'author' ) );
     387                $post_4 = $this->factory->post->create( array( 'post_title' => rand_str(), 'post_author' => $author_4, 'post_date' => '2007-01-01 00:00:00' ) );
     388
     389                $posts = $this->q->query( array(
     390                        'author' => '',
     391                        'post__in' => array( $post_1, $post_2, $post_3, $post_4 )
     392                ) );
     393                $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) );
     394                $this->assertEqualSets( array( $author_1, $author_2, $author_3, $author_4 ), $author_ids );
     395
     396                $posts = $this->q->query( array(
     397                        'author' => 0,
     398                        'post__in' => array( $post_1, $post_2, $post_3, $post_4 )
     399                ) );
     400                $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) );
     401                $this->assertEqualSets( array( $author_1, $author_2, $author_3, $author_4 ), $author_ids );
     402
     403                $posts = $this->q->query( array(
     404                        'author' => '0',
     405                        'post__in' => array( $post_1, $post_2, $post_3, $post_4 )
     406                ) );
     407                $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) );
     408                $this->assertEqualSets( array( $author_1, $author_2, $author_3, $author_4 ), $author_ids );
     409
     410                $posts = $this->q->query( array(
     411                        'author' => $author_1,
     412                        'post__in' => array( $post_1, $post_2, $post_3, $post_4 )
     413                ) );
     414                $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) );
     415                $this->assertEqualSets( array( $author_1 ), $author_ids );
     416
     417                $posts = $this->q->query( array(
     418                        'author' => "$author_1",
     419                        'post__in' => array( $post_1, $post_2, $post_3, $post_4 )
     420                ) );
     421                $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) );
     422                $this->assertEqualSets( array( $author_1 ), $author_ids );
     423
     424                $posts = $this->q->query( array(
     425                        'author' => "{$author_1},{$author_2}",
     426                        'post__in' => array( $post_1, $post_2, $post_3, $post_4 )
     427                ) );
     428                $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) );
     429                $this->assertEqualSets( array( $author_1, $author_2 ), $author_ids );
     430
     431                $posts = $this->q->query( array(
     432                        'author' => "-{$author_1},{$author_2}",
     433                        'post__in' => array( $post_1, $post_2, $post_3, $post_4 )
     434                ) );
     435                $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) );
     436                $this->assertEqualSets( array( $author_2 ), $author_ids );
     437
     438                $posts = $this->q->query( array(
     439                        'author' => "{$author_1},-{$author_2}",
     440                        'post__in' => array( $post_1, $post_2, $post_3, $post_4 )
     441                ) );
     442                $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) );
     443                $this->assertEqualSets( array( $author_1 ), $author_ids );
     444
     445                $posts = $this->q->query( array(
     446                        'author' => "-{$author_1},-{$author_2}",
     447                        'post__in' => array( $post_1, $post_2, $post_3, $post_4 )
     448                ) );
     449                $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) );
     450                $this->assertEqualSets( array( $author_3, $author_4 ), $author_ids );
     451
     452                $posts = $this->q->query( array(
     453                        'author__in' => array( $author_1, $author_2 ),
     454                        'post__in' => array( $post_1, $post_2, $post_3, $post_4 )
     455                ) );
     456                $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) );
     457                $this->assertEqualSets( array( $author_1, $author_2 ), $author_ids );
     458
     459                $posts = $this->q->query( array(
     460                        'author__not_in' => array( $author_1, $author_2 ),
     461                        'post__in' => array( $post_1, $post_2, $post_3, $post_4 )
     462                ) );
     463                $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) );
     464                $this->assertEqualSets( array( $author_3, $author_4 ), $author_ids );
     465
     466                $posts = $this->q->query( array(
     467                        'author_name' => 'admin1',
     468                        'post__in' => array( $post_1, $post_2, $post_3, $post_4 )
     469                ) );
     470                $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) );
     471                $this->assertEqualSets( array( $author_1 ), $author_ids );
     472        }
    372473}
  • src/wp-includes/query.php

     
    13961396                        , 'tag'
    13971397                        , 'cat'
    13981398                        , 'tag_id'
     1399                        , 'author'
    13991400                        , 'author_name'
    14001401                        , 'feed'
    14011402                        , 'tb'
     
    14161417                }
    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 ) {
    14221424                        if ( !isset($array[$key]) )
     
    14571459                $qv['m'] = preg_replace( '|[^0-9]|', '', $qv['m'] );
    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'] );
    14621465                if ( '' !== $qv['hour'] ) $qv['hour'] = absint($qv['hour']);
     
    23252328
    23262329                // Author/user stuff
    23272330
    2328                 if ( empty($q['author']) || ($q['author'] == '0') ) {
    2329                         $whichauthor = '';
    2330                 } else {
    2331                         $q['author'] = (string)urldecode($q['author']);
    2332                         $q['author'] = addslashes_gpc($q['author']);
    2333                         if ( strpos($q['author'], '-') !== false ) {
    2334                                 $eq = '!=';
    2335                                 $andor = 'AND';
    2336                                 $q['author'] = explode('-', $q['author']);
    2337                                 $q['author'] = (string)absint($q['author'][1]);
    2338                         } else {
    2339                                 $eq = '=';
    2340                                 $andor = 'OR';
     2331                if ( ! empty( $q['author'] ) && $q['author'] != '0' ) {
     2332                        $q['author'] = addslashes_gpc( '' . urldecode( $q['author'] ) );
     2333                        $authors = array_unique( array_map( 'intval', (array) preg_split( '/[,\s]+/', $q['author'] ) ) );
     2334                        foreach ( $authors as $author ) {
     2335                                $key = $author > 0 ? 'author__in' : 'author__not_in';
     2336                                $q[$key][] = abs( $author );
    23412337                        }
    2342                         $author_array = preg_split('/[,\s]+/', $q['author']);
    2343                         $_author_array = array();
    2344                         foreach ( $author_array as $key => $_author )
    2345                                 $_author_array[] = "$wpdb->posts.post_author " . $eq . ' ' . absint($_author);
    2346                         $whichauthor .= ' AND (' . implode(" $andor ", $_author_array) . ')';
    2347                         unset($author_array, $_author_array);
     2338                        $q['author'] = implode( ',', $authors );
    23482339                }
    23492340
     2341                if ( ! empty( $q['author__in'] ) ) {
     2342                        $author__in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__in'] ) ) );
     2343                        $where .= " AND {$wpdb->posts}.post_author IN ($author__in) ";
     2344                }
     2345
     2346                if ( ! empty( $q['author__not_in'] ) ) {
     2347                        $author__not_in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__not_in'] ) ) );
     2348                        $where .= " AND {$wpdb->posts}.post_author NOT IN ($author__not_in) ";
     2349                }
     2350
    23502351                // Author stuff for nice URLs
    23512352
    23522353                if ( '' != $q['author_name'] ) {