Skip to content

Commit

Permalink
Merge pull request #2781 from 10up/fix/users-orderby
Browse files Browse the repository at this point in the history
Refactor User parse_orderby and add user_registered support
  • Loading branch information
felipeelia authored May 20, 2022
2 parents 956f421 + 237983b commit afba6b8
Showing 1 changed file with 67 additions and 63 deletions.
130 changes: 67 additions & 63 deletions includes/classes/Indexable/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,6 @@ public function parse_orderby( $orderby, $default_order, $query_vars ) {
* include
* login__in
* nicename__in
* user_registered registered
* post_count
*/

Expand All @@ -622,6 +621,12 @@ public function parse_orderby( $orderby, $default_order, $query_vars ) {

$sort = [];

if ( empty( $orderby ) ) {
return $sort;
}

$unsupported_clauses = [ 'rand', 'include', 'login__in', 'nicename__in', 'post_count' ];

foreach ( $orderby as $key => $value ) {
if ( is_string( $key ) ) {
$orderby_clause = $key;
Expand All @@ -631,73 +636,72 @@ public function parse_orderby( $orderby, $default_order, $query_vars ) {
$order = $default_order;
}

if ( ! empty( $orderby_clause ) && 'rand' !== $orderby_clause ) {
if ( 'relevance' === $orderby_clause ) {
$sort[] = array(
'_score' => array(
'order' => $order,
),
);
} elseif ( 'user_login' === $orderby_clause || 'login' === $orderby_clause ) {
$sort[] = array(
'user_login.raw' => array(
'order' => $order,
),
);
} elseif ( 'ID' === $orderby_clause ) {
$sort[] = array(
'ID' => array(
'order' => $order,
),
);
} elseif ( 'display_name' === $orderby_clause || 'name' === $orderby_clause ) {
$sort[] = array(
'display_name.sortable' => array(
'order' => $order,
),
);
} elseif ( 'user_nicename' === $orderby_clause || 'nicename' === $orderby_clause ) {
$sort[] = array(
'user_nicename.raw' => array(
'order' => $order,
),
);
} elseif ( 'user_email' === $orderby_clause || 'email' === $orderby_clause ) {
$sort[] = array(
'user_email.raw' => array(
'order' => $order,
),
);
} elseif ( 'user_url' === $orderby_clause || 'url' === $orderby_clause ) {
$sort[] = array(
'user_url.raw' => array(
'order' => $order,
),
);
} elseif ( 'meta_value' === $orderby_clause ) {
if ( empty( $orderby_clause ) || in_array( $orderby_clause, $unsupported_clauses, true ) ) {
continue;
}

switch ( $orderby_clause ) {
case 'relevance':
$orderby_field = '_score';
break;

case 'user_login':
case 'login':
$orderby_field = 'user_login.raw';
break;

case 'ID':
case 'id':
$orderby_field = 'ID';
break;

case 'display_name':
case 'name':
$orderby_field = 'display_name.sortable';
break;

case 'nicename':
case 'user_nicename':
$orderby_field = 'user_nicename.raw';
break;

case 'user_email':
case 'email':
$orderby_field = 'user_email.raw';
break;

case 'user_url':
case 'url':
$orderby_field = 'user_url.raw';
break;

case 'user_registered':
case 'registered':
$orderby_field = 'user_registered';
break;

case 'meta_value':
if ( ! empty( $query_vars['meta_key'] ) ) {
$sort[] = array(
'meta.' . $query_vars['meta_key'] . '.raw' => array(
'order' => $order,
),
);
$orderby_field = 'meta.' . $query_vars['meta_key'] . '.raw';
}
} elseif ( 'meta_value_num' === $orderby_clause ) {
break;

case 'meta_value_num':
if ( ! empty( $query_vars['meta_key'] ) ) {
$sort[] = array(
'meta.' . $query_vars['meta_key'] . '.long' => array(
'order' => $order,
),
);
$orderby_field = 'meta.' . $query_vars['meta_key'] . '.long';
}
} else {
$sort[] = array(
$orderby_clause => array(
'order' => $order,
),
);
}
break;

default:
$orderby_field = $orderby_clause;
break;
}

$sort[] = array(
$orderby_field => array(
'order' => $order,
),
);
}

return $sort;
Expand Down

0 comments on commit afba6b8

Please sign in to comment.