Make WordPress Core

Changeset 30016


Ignore:
Timestamp:
10/24/2014 07:50:53 PM (10 years ago)
Author:
boonebgorges
Message:

Accept 'orderby=include' in WP_User_Query.

This lets the results of a user query be sorted manually by the value of the
'include' param.

Props jipmoors.
Fixes #30064.

Location:
trunk
Files:
2 edited

Legend:

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

    r29962 r30016  
    599599        $this->query_where = "WHERE 1=1";
    600600
     601        // Parse and sanitize 'include', for use by 'orderby' as well as 'include' below.
     602        if ( ! empty( $qv['include'] ) ) {
     603            $include = wp_parse_id_list( $qv['include'] );
     604        } else {
     605            $include = false;
     606        }
     607
    601608        // sorting
    602609        if ( isset( $qv['orderby'] ) ) {
     
    622629            } elseif ( 'meta_value' == $qv['orderby'] ) {
    623630                $orderby = "$wpdb->usermeta.meta_value";
     631            } else if ( 'include' === $qv['orderby'] && ! empty( $include ) ) {
     632                // Sanitized earlier.
     633                $include_sql = implode( ',', $include );
     634                $orderby = "FIELD( $wpdb->users.ID, $include_sql )";
    624635            } else {
    625636                $orderby = 'user_login';
     
    735746        }
    736747
    737         if ( ! empty( $qv['include'] ) ) {
    738             $ids = implode( ',', wp_parse_id_list( $qv['include'] ) );
     748        if ( ! empty( $include ) ) {
     749            // Sanitized earlier.
     750            $ids = implode( ',', $include );
    739751            $this->query_where .= " AND $wpdb->users.ID IN ($ids)";
    740752        } elseif ( ! empty( $qv['exclude'] ) ) {
  • trunk/tests/phpunit/tests/user/query.php

    r30008 r30016  
    133133
    134134        $this->assertEquals( $names, $values );
     135    }
     136
     137    /**
     138     * @ticket 30064
     139     */
     140    public function test_orderby_include_with_empty_include() {
     141        $q = new WP_User_Query( array(
     142            'orderby' => 'include',
     143        ) );
     144
     145        $this->assertContains( 'ORDER BY user_login', $q->query_orderby );
     146    }
     147
     148    /**
     149     * @ticket 30064
     150     */
     151    public function test_orderby_include() {
     152        global $wpdb;
     153
     154        $users = $this->factory->user->create_many( 4 );
     155        $q = new WP_User_Query( array(
     156            'orderby' => 'include',
     157            'include' => array( $users[1], $users[0], $users[3] ),
     158            'fields' => '',
     159        ) );
     160
     161        $expected_orderby = 'ORDER BY FIELD( ' . $wpdb->users . '.ID, ' . $users[1] . ',' . $users[0] . ',' . $users[3] . ' )';
     162        $this->assertContains( $expected_orderby, $q->query_orderby );
     163
     164        // assertEquals() respects order but ignores type (get_results() returns numeric strings).
     165        $this->assertEquals( array( $users[1], $users[0], $users[3] ), $q->get_results() );
     166    }
     167
     168    /**
     169     * @ticket 30064
     170     */
     171    public function test_orderby_include_duplicate_values() {
     172        global $wpdb;
     173
     174        $users = $this->factory->user->create_many( 4 );
     175        $q = new WP_User_Query( array(
     176            'orderby' => 'include',
     177            'include' => array( $users[1], $users[0], $users[1], $users[3] ),
     178            'fields' => '',
     179        ) );
     180
     181        $expected_orderby = 'ORDER BY FIELD( ' . $wpdb->users . '.ID, ' . $users[1] . ',' . $users[0] . ',' . $users[3] . ' )';
     182        $this->assertContains( $expected_orderby, $q->query_orderby );
     183
     184        // assertEquals() respects order but ignores type (get_results() returns numeric strings).
     185        $this->assertEquals( array( $users[1], $users[0], $users[3] ), $q->get_results() );
    135186    }
    136187
Note: See TracChangeset for help on using the changeset viewer.