Skip to content

Commit

Permalink
Merge pull request #3042 from 10up/chore/refactor-post-parse-orderby
Browse files Browse the repository at this point in the history
Small refactor of parse_orderby to make it easier to read
  • Loading branch information
felipeelia authored Oct 3, 2022
2 parents 4220a2d + 6b90eba commit 08209d7
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 249 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
84 changes: 26 additions & 58 deletions includes/classes/Indexable/Post/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -1918,6 +1918,15 @@ protected function parse_order( $order ) {
protected function parse_orderby( $orderbys, $default_order, $args ) {
$orderbys = $this->get_orderby_array( $orderbys );

$from_to = [
'relevance' => '_score',
'date' => 'post_date',
'type' => 'post_type.raw',
'modified' => 'post_modified',
'name' => 'post_name.raw',
'title' => 'post_title.sortable',
];

$sort = [];

foreach ( $orderbys as $key => $value ) {
Expand All @@ -1929,67 +1938,26 @@ protected function parse_orderby( $orderbys, $default_order, $args ) {
$order = $default_order;
}

if ( ! empty( $orderby_clause ) && 'rand' !== $orderby_clause ) {
if ( 'relevance' === $orderby_clause ) {
$sort[] = array(
'_score' => array(
'order' => $order,
),
);
} elseif ( 'date' === $orderby_clause ) {
$sort[] = array(
'post_date' => array(
'order' => $order,
),
);
} elseif ( 'type' === $orderby_clause ) {
$sort[] = array(
'post_type.raw' => array(
'order' => $order,
),
);
} elseif ( 'modified' === $orderby_clause ) {
$sort[] = array(
'post_modified' => array(
'order' => $order,
),
);
} elseif ( 'name' === $orderby_clause ) {
$sort[] = array(
'post_' . $orderby_clause . '.raw' => array(
'order' => $order,
),
);
} elseif ( 'title' === $orderby_clause ) {
$sort[] = array(
'post_' . $orderby_clause . '.sortable' => array(
'order' => $order,
),
);
} elseif ( 'meta_value' === $orderby_clause ) {
if ( ! empty( $args['meta_key'] ) ) {
$sort[] = array(
'meta.' . $args['meta_key'] . '.raw' => array(
'order' => $order,
),
);
}
} elseif ( 'meta_value_num' === $orderby_clause ) {
if ( ! empty( $args['meta_key'] ) ) {
$sort[] = array(
'meta.' . $args['meta_key'] . '.long' => array(
'order' => $order,
),
);
}
if ( empty( $orderby_clause ) || 'rand' === $orderby_clause ) {
continue;
}

if ( in_array( $orderby_clause, [ 'meta_value', 'meta_value_num' ], true ) ) {
if ( empty( $args['meta_key'] ) ) {
continue;
} else {
$sort[] = array(
$orderby_clause => array(
'order' => $order,
),
);
$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_clause => 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
Loading

0 comments on commit 08209d7

Please sign in to comment.