From 6f5ec10b75c8f056725e51783a86a8bb886f4302 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Fri, 20 May 2022 09:30:12 -0300 Subject: [PATCH 1/2] Refactor User parse_orderby and add user_registered support --- includes/classes/Indexable/User/User.php | 128 ++++++++++++----------- 1 file changed, 65 insertions(+), 63 deletions(-) diff --git a/includes/classes/Indexable/User/User.php b/includes/classes/Indexable/User/User.php index ada5fe2d5c..96f1f96976 100644 --- a/includes/classes/Indexable/User/User.php +++ b/includes/classes/Indexable/User/User.php @@ -612,7 +612,6 @@ public function parse_orderby( $orderby, $default_order, $query_vars ) { * include * login__in * nicename__in - * user_registered registered * post_count */ @@ -622,6 +621,10 @@ public function parse_orderby( $orderby, $default_order, $query_vars ) { $sort = []; + if ( empty( $orderby ) ) { + return $sort; + } + foreach ( $orderby as $key => $value ) { if ( is_string( $key ) ) { $orderby_clause = $key; @@ -631,73 +634,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 ) && 'rand' === $orderby_clause ) { + 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; From 237983ba9fde1728fd6ef7772b835913686a34e2 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Fri, 20 May 2022 10:08:11 -0300 Subject: [PATCH 2/2] Fix logic, prevent error on unsupported clauses --- includes/classes/Indexable/User/User.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/includes/classes/Indexable/User/User.php b/includes/classes/Indexable/User/User.php index 96f1f96976..4bbd8d3348 100644 --- a/includes/classes/Indexable/User/User.php +++ b/includes/classes/Indexable/User/User.php @@ -625,6 +625,8 @@ public function parse_orderby( $orderby, $default_order, $query_vars ) { return $sort; } + $unsupported_clauses = [ 'rand', 'include', 'login__in', 'nicename__in', 'post_count' ]; + foreach ( $orderby as $key => $value ) { if ( is_string( $key ) ) { $orderby_clause = $key; @@ -634,7 +636,7 @@ public function parse_orderby( $orderby, $default_order, $query_vars ) { $order = $default_order; } - if ( empty( $orderby_clause ) && 'rand' === $orderby_clause ) { + if ( empty( $orderby_clause ) || in_array( $orderby_clause, $unsupported_clauses, true ) ) { continue; }