Make WordPress Core

Changeset 31669


Ignore:
Timestamp:
03/07/2015 06:37:05 PM (12 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.

Location:
trunk
Files:
2 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
  • trunk/tests/phpunit/tests/user/query.php

    r31668 r31669  
    178178
    179179                $this->assertEquals( $expected, $q->get_results() );
     180        }
     181
     182        /**
     183         * @ticket 31265
     184         */
     185        public function test_orderby_somekey_where_meta_key_is_somekey() {
     186                $users = $this->factory->user->create_many( 3, array(
     187                        'role' => 'author'
     188                ) );
     189
     190                update_user_meta( $users[0], 'foo', 'zzz' );
     191                update_user_meta( $users[1], 'foo', 'aaa' );
     192                update_user_meta( $users[2], 'foo', 'jjj' );
     193
     194                $q = new WP_User_Query( array(
     195                        'include' => $users,
     196                        'meta_key' => 'foo',
     197                        'orderby' => 'foo',
     198                        'fields' => 'ids'
     199                ) );
     200
     201                $expected = array( $users[1], $users[2], $users[0] );
     202
     203                $this->assertEquals( $expected, $q->get_results() );
     204        }
     205
     206        /**
     207         * @ticket 31265
     208         */
     209        public function test_orderby_clause_key() {
     210                $users = $this->factory->user->create_many( 3 );
     211                add_user_meta( $users[0], 'foo', 'aaa' );
     212                add_user_meta( $users[1], 'foo', 'zzz' );
     213                add_user_meta( $users[2], 'foo', 'jjj' );
     214
     215                $q = new WP_User_Query( array(
     216                        'fields' => 'ids',
     217                        'meta_query' => array(
     218                                'foo_key' => array(
     219                                        'key' => 'foo',
     220                                        'compare' => 'EXISTS',
     221                                ),
     222                        ),
     223                        'orderby' => 'foo_key',
     224                        'order' => 'DESC',
     225                ) );
     226
     227                $this->assertEquals( array( $users[1], $users[2], $users[0] ), $q->results );
     228        }
     229
     230        /**
     231         * @ticket 31265
     232         */
     233        public function test_orderby_clause_key_as_secondary_sort() {
     234                $u1 = $this->factory->user->create( array(
     235                        'user_registered' => '2015-01-28 03:00:00',
     236                ) );
     237                $u2 = $this->factory->user->create( array(
     238                        'user_registered' => '2015-01-28 05:00:00',
     239                ) );
     240                $u3 = $this->factory->user->create( array(
     241                        'user_registered' => '2015-01-28 03:00:00',
     242                ) );
     243
     244                add_user_meta( $u1, 'foo', 'jjj' );
     245                add_user_meta( $u2, 'foo', 'zzz' );
     246                add_user_meta( $u3, 'foo', 'aaa' );
     247
     248                $q = new WP_User_Query( array(
     249                        'fields' => 'ids',
     250                        'meta_query' => array(
     251                                'foo_key' => array(
     252                                        'key' => 'foo',
     253                                        'compare' => 'EXISTS',
     254                                ),
     255                        ),
     256                        'orderby' => array(
     257                                'comment_date' => 'asc',
     258                                'foo_key' => 'asc',
     259                        ),
     260                ) );
     261
     262                $this->assertEquals( array( $u3, $u1, $u2 ), $q->results );
     263        }
     264
     265        /**
     266         * @ticket 31265
     267         */
     268        public function test_orderby_more_than_one_clause_key() {
     269                $users = $this->factory->user->create_many( 3 );
     270
     271                add_user_meta( $users[0], 'foo', 'jjj' );
     272                add_user_meta( $users[1], 'foo', 'zzz' );
     273                add_user_meta( $users[2], 'foo', 'jjj' );
     274                add_user_meta( $users[0], 'bar', 'aaa' );
     275                add_user_meta( $users[1], 'bar', 'ccc' );
     276                add_user_meta( $users[2], 'bar', 'bbb' );
     277
     278                $q = new WP_User_Query( array(
     279                        'fields' => 'ids',
     280                        'meta_query' => array(
     281                                'foo_key' => array(
     282                                        'key' => 'foo',
     283                                        'compare' => 'EXISTS',
     284                                ),
     285                                'bar_key' => array(
     286                                        'key' => 'bar',
     287                                        'compare' => 'EXISTS',
     288                                ),
     289                        ),
     290                        'orderby' => array(
     291                                'foo_key' => 'asc',
     292                                'bar_key' => 'desc',
     293                        ),
     294                ) );
     295
     296                $this->assertEquals( array( $users[2], $users[0], $users[1] ), $q->results );
    180297        }
    181298
Note: See TracChangeset for help on using the changeset viewer.