Make WordPress Core


Ignore:
Timestamp:
09/04/2013 09:32:11 PM (13 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.

File:
1 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 ) ) {
Note: See TracChangeset for help on using the changeset viewer.