Skip to content

Commit

Permalink
Apply the same refactor logic to the other indexables
Browse files Browse the repository at this point in the history
  • Loading branch information
felipeelia committed Oct 3, 2022
1 parent fc78503 commit 6b90eba
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 191 deletions.
111 changes: 25 additions & 86 deletions includes/classes/Indexable/Comment/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -1043,95 +1043,34 @@ protected function parse_orderby( $orderby, $order, $args ) {
return $sort;
}

switch ( $orderby ) {
case 'comment_agent':
$orderby_field = 'comment_agent.raw';
break;

case 'comment_approved':
$orderby_field = 'comment_approved.raw';
break;

case 'comment_author':
$orderby_field = 'comment_author.raw';
break;

case 'comment_author_email':
$orderby_field = 'comment_author_email.raw';
break;

case 'comment_author_IP':
$orderby_field = 'comment_author_IP.raw';
break;

case 'comment_author_url':
$orderby_field = 'comment_author_url.raw';
break;

case 'comment_content':
$orderby_field = 'comment_content.raw';
break;

case 'comment_date':
$orderby_field = 'comment_date';
break;

case 'comment_date_gmt':
$orderby_field = 'comment_date_gmt';
break;

case 'comment_ID':
$orderby_field = 'comment_ID';
break;

case 'comment_karma':
$orderby_field = 'comment_karma';
break;

case 'comment_parent':
$orderby_field = 'comment_parent';
break;

case 'comment_post_ID':
$orderby_field = 'comment_post_ID';
break;

case 'comment_type':
$orderby_field = 'comment_type.raw';
break;

case 'comment_post_type':
$orderby_field = 'comment_post_type.raw';
break;

case 'user_id':
$orderby_field = 'user_id';
break;

case 'meta_value':
if ( ! empty( $args['meta_key'] ) ) {
$orderby_field = 'meta.' . $args['meta_key'] . '.value';
}
break;

case 'meta_value_num':
if ( ! empty( $args['meta_key'] ) ) {
$orderby_field = 'meta.' . $args['meta_key'] . '.long';
}
break;
$from_to = [
'comment_agent' => 'comment_agent.raw',
'comment_approved' => 'comment_approved.raw',
'comment_author' => 'comment_author.raw',
'comment_author_email' => 'comment_author_email.raw',
'comment_author_IP' => 'comment_author_IP.raw',
'comment_author_url' => 'comment_author_url.raw',
'comment_content' => 'comment_content.raw',
'comment_type' => 'comment_type.raw',
'comment_post_type' => 'comment_post_type.raw',
];

default:
$orderby_field = $orderby;
break;
if ( in_array( $orderby, [ 'meta_value', 'meta_value_num' ], true ) ) {
if ( empty( $args['meta_key'] ) ) {
return $sort;
} else {
$from_to['meta_value'] = 'meta.' . $args['meta_key'] . '.raw';
$from_to['meta_value_num'] = 'meta.' . $args['meta_key'] . '.long';
}
}

if ( ! empty( $orderby_field ) ) {
$sort[] = [
$orderby_field => [
'order' => $order,
],
];
}
$orderby = $from_to[ $orderby ] ?? $orderby;

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

return $sort;
}
Expand Down
72 changes: 23 additions & 49 deletions includes/classes/Indexable/Term/Term.php
Original file line number Diff line number Diff line change
Expand Up @@ -911,60 +911,34 @@ protected function parse_orderby( $orderby, $order, $args ) {
return $sort;
}

switch ( $orderby ) {
case 'name':
$es_version = Elasticsearch::factory()->get_elasticsearch_version();
$es_field_name = 'name.sortable';

if ( version_compare( $es_version, '7.0', '<' ) ) {
$es_field_name = 'name.raw';
}

break;

case 'slug':
$es_field_name = 'slug.raw';
break;

case 'term_id':
case 'id':
$es_field_name = 'term_id';
break;

case 'description':
$es_field_name = 'description.sortable';
break;

case 'meta_value':
if ( ! empty( $args['meta_key'] ) ) {
$es_field_name = 'meta.' . $args['meta_key'] . '.value';
}

break;

case 'meta_value_num':
if ( ! empty( $args['meta_key'] ) ) {
$es_field_name = 'meta.' . $args['meta_key'] . '.long';
}

break;
$from_to = [
'slug' => 'slug.raw',
'id' => 'term_id',
'description' => 'description.sortable',
];

case 'parent':
case 'count':
default:
$es_field_name = $orderby;
break;
if ( in_array( $orderby, [ 'meta_value', 'meta_value_num' ], true ) ) {
if ( empty( $args['meta_key'] ) ) {
return $sort;
} else {
$from_to['meta_value'] = 'meta.' . $args['meta_key'] . '.value';
$from_to['meta_value_num'] = 'meta.' . $args['meta_key'] . '.long';
}
}

// For `meta_value` and `meta_value_num`, for example, there is a chance this wasn't set.
if ( ! empty( $es_field_name ) ) {
$sort[] = array(
$es_field_name => array(
'order' => $order,
),
);
if ( 'name' === $orderby ) {
$es_version = Elasticsearch::factory()->get_elasticsearch_version();
$from_to['name'] = version_compare( $es_version, '7.0', '<' ) ? 'name.raw' : 'name.sortable';
}

$orderby = $from_to[ $orderby ] ?? $orderby;

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

return $sort;
}

Expand Down
82 changes: 26 additions & 56 deletions includes/classes/Indexable/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,22 @@ public function parse_orderby( $orderby, $default_order, $query_vars ) {
$orderby = explode( ' ', $orderby );
}

$from_to = [
'relevance' => '_score',
'user_login' => 'user_login.raw',
'login' => 'user_login.raw',
'id' => 'ID',
'display_name' => 'display_name.sortable',
'name' => 'display_name.sortable',
'nicename' => 'user_nicename.raw',
'user_nicename' => 'user_nicename.raw',
'user_email' => 'user_email.raw',
'email' => 'user_email.raw',
'user_url' => 'user_url.raw',
'url' => 'user_url.raw',
'registered' => 'user_registered',
];

$sort = [];

if ( empty( $orderby ) ) {
Expand All @@ -575,65 +591,19 @@ public function parse_orderby( $orderby, $default_order, $query_vars ) {
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'] ) ) {
$orderby_field = 'meta.' . $query_vars['meta_key'] . '.raw';
}
break;

case 'meta_value_num':
if ( ! empty( $query_vars['meta_key'] ) ) {
$orderby_field = 'meta.' . $query_vars['meta_key'] . '.long';
}
break;

default:
$orderby_field = $orderby_clause;
break;
if ( in_array( $orderby_clause, [ 'meta_value', 'meta_value_num' ], true ) ) {
if ( empty( $args['meta_key'] ) ) {
continue;
} else {
$from_to['meta_value'] = 'meta.' . $args['meta_key'] . '.raw';
$from_to['meta_value_num'] = 'meta.' . $args['meta_key'] . '.long';
}
}

$orderby_clause = $from_to[ $orderby_clause ] ?? $orderby_clause;

$sort[] = array(
$orderby_field => array(
$orderby_clause => array(
'order' => $order,
),
);
Expand Down

0 comments on commit 6b90eba

Please sign in to comment.