Make WordPress Core


Ignore:
Timestamp:
03/07/2015 06:37:05 PM (10 years ago)
Author:
boonebgorges
Message:

Improved 'orderby' meta syntax in WP_User_Query.

Recent commits have added the ability to order query results by specific
clauses of the 'meta_query' parameter (comments [31467], posts [31312] and
[31340]). The current changeset ports the same functionality to WP_User_Query.

Also introduced is the ability to pass the value of $meta_key to 'orderby'.

The internals of WP_User_Query::prepare_users() had to be reordered
somewhat to support these changes, primarily to ensure that the meta_query
object generates its SQL clauses before the 'orderby' parameter is parsed.

See #31265.

File:
1 edited

Legend:

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

    r31665 r31669  
    532532     *     @type array        $search_columns  Array of column names to be searched. Accepts 'ID', 'login',
    533533     *                                         'nicename', 'email', 'url'. Default empty array.
    534      *     @type string|array $orderby         Field to sort the retrieved users by. May be a single value,
     534     *     @type string|array $orderby         Field(s) to sort the retrieved users by. May be a single value,
    535535     *                                         an array of values, or a multi-dimensional array with fields as keys
    536536     *                                         and orders ('ASC' or 'DESC') as values. Accepted values are'ID',
     
    538538     *                                         'user_nicename' (or 'nicename'), 'user_email' (or 'email'),
    539539     *                                         'user_url' (or 'url'), 'user_registered' (or 'registered'),
    540      *                                         'post_count', 'meta_value', or 'meta_value_num'. To use 'meta_value'
     540     *                                         'post_count', 'meta_value', 'meta_value_num', the value of
     541     *                                         `$meta_key`, or an array key of `$meta_query`. To use 'meta_value'
    541542     *                                         or 'meta_value_num', `$meta_key` must be also be defined.
    542543     *                                         Default 'user_login'.
     
    626627        }
    627628
     629        // Meta query.
     630        $this->meta_query = new WP_Meta_Query();
     631        $this->meta_query->parse_query_vars( $qv );
     632
     633        $blog_id = 0;
     634        if ( isset( $qv['blog_id'] ) ) {
     635            $blog_id = absint( $qv['blog_id'] );
     636        }
     637
     638        $role = '';
     639        if ( isset( $qv['role'] ) ) {
     640            $role = trim( $qv['role'] );
     641        }
     642
     643        if ( $blog_id && ( $role || is_multisite() ) ) {
     644            $cap_meta_query = array();
     645            $cap_meta_query['key'] = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities';
     646
     647            if ( $role ) {
     648                $cap_meta_query['value'] = '"' . $role . '"';
     649                $cap_meta_query['compare'] = 'like';
     650            }
     651
     652            if ( empty( $this->meta_query->queries ) ) {
     653                $this->meta_query->queries = array( $cap_meta_query );
     654            } elseif ( ! in_array( $cap_meta_query, $this->meta_query->queries, true ) ) {
     655                // Append the cap query to the original queries and reparse the query.
     656                $this->meta_query->queries = array(
     657                    'relation' => 'AND',
     658                    array( $this->meta_query->queries, $cap_meta_query ),
     659                );
     660            }
     661
     662            $this->meta_query->parse_query_vars( $this->meta_query->queries );
     663        }
     664
     665        if ( ! empty( $this->meta_query->queries ) ) {
     666            $clauses = $this->meta_query->get_sql( 'user', $wpdb->users, 'ID', $this );
     667            $this->query_from .= $clauses['join'];
     668            $this->query_where .= $clauses['where'];
     669
     670            if ( 'OR' == $this->meta_query->relation ) {
     671                $this->query_fields = 'DISTINCT ' . $this->query_fields;
     672            }
     673        }
     674
    628675        // sorting
    629676        $qv['order'] = isset( $qv['order'] ) ? strtoupper( $qv['order'] ) : '';
     
    729776        }
    730777
    731         $blog_id = 0;
    732         if ( isset( $qv['blog_id'] ) )
    733             $blog_id = absint( $qv['blog_id'] );
    734 
    735778        if ( isset( $qv['who'] ) && 'authors' == $qv['who'] && $blog_id ) {
    736779            $qv['meta_key'] = $wpdb->get_blog_prefix( $blog_id ) . 'user_level';
     
    738781            $qv['meta_compare'] = '!=';
    739782            $qv['blog_id'] = $blog_id = 0; // Prevent extra meta query
    740         }
    741 
    742         $this->meta_query = new WP_Meta_Query();
    743         $this->meta_query->parse_query_vars( $qv );
    744 
    745         $role = '';
    746         if ( isset( $qv['role'] ) )
    747             $role = trim( $qv['role'] );
    748 
    749         if ( $blog_id && ( $role || is_multisite() ) ) {
    750             $cap_meta_query = array();
    751             $cap_meta_query['key'] = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities';
    752 
    753             if ( $role ) {
    754                 $cap_meta_query['value'] = '"' . $role . '"';
    755                 $cap_meta_query['compare'] = 'like';
    756             }
    757 
    758             if ( empty( $this->meta_query->queries ) ) {
    759                 $this->meta_query->queries = array( $cap_meta_query );
    760             } elseif ( ! in_array( $cap_meta_query, $this->meta_query->queries, true ) ) {
    761                 // Append the cap query to the original queries and reparse the query.
    762                 $this->meta_query->queries = array(
    763                     'relation' => 'AND',
    764                     array( $this->meta_query->queries, $cap_meta_query ),
    765                 );
    766             }
    767 
    768             $this->meta_query->parse_query_vars( $this->meta_query->queries );
    769         }
    770 
    771         if ( !empty( $this->meta_query->queries ) ) {
    772             $clauses = $this->meta_query->get_sql( 'user', $wpdb->users, 'ID', $this );
    773             $this->query_from .= $clauses['join'];
    774             $this->query_where .= $clauses['where'];
    775 
    776             if ( 'OR' == $this->meta_query->relation )
    777                 $this->query_fields = 'DISTINCT ' . $this->query_fields;
    778783        }
    779784
     
    955960    protected function parse_orderby( $orderby ) {
    956961        global $wpdb;
     962
     963        $meta_query_clauses = $this->meta_query->get_clauses();
    957964
    958965        $_orderby = '';
     
    976983        } elseif ( 'ID' == $orderby || 'id' == $orderby ) {
    977984            $_orderby = 'ID';
    978         } elseif ( 'meta_value' == $orderby ) {
     985        } elseif ( 'meta_value' == $orderby || $this->get( 'meta_key' ) == $orderby ) {
    979986            $_orderby = "$wpdb->usermeta.meta_value";
    980987        } elseif ( 'meta_value_num' == $orderby ) {
     
    984991            $include_sql = implode( ',', $include );
    985992            $_orderby = "FIELD( $wpdb->users.ID, $include_sql )";
     993        } elseif ( isset( $meta_query_clauses[ $orderby ] ) ) {
     994            $meta_clause = $meta_query_clauses[ $orderby ];
     995            $_orderby = sprintf( "CAST(%s.meta_value AS %s)", esc_sql( $meta_clause['alias'] ), esc_sql( $meta_clause['cast'] ) );
    986996        }
    987997
Note: See TracChangeset for help on using the changeset viewer.