Skip to content

Commit

Permalink
Add skip_query_integration from upstream commit (#212)
Browse files Browse the repository at this point in the history
* Add skip_query_integration from upstream commit

* lint

* lint

* lint
  • Loading branch information
rebeccahum authored Aug 15, 2024
1 parent 2675125 commit afd996b
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
36 changes: 36 additions & 0 deletions includes/classes/Feature/Search/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public function search_setup() {
add_filter( 'ep_formatted_args', [ $this, 'add_search_highlight_tags' ], 10, 2 );
add_filter( 'ep_highlighting_tag', [ $this, 'get_highlighting_tag' ] );
add_action( 'ep_highlighting_pre_add_highlight', [ $this, 'allow_excerpt_html' ] );
add_filter( 'ep_skip_query_integration', [ $this, 'skip_query_integration' ], 10, 2 );
}


Expand Down Expand Up @@ -661,4 +662,39 @@ public function output_feature_box_settings() {

<?php
}

/**
* If WP_Query has unsupported orderby, skip ES query integration and use the WP query instead.
*
* @param bool $skip Whether to skip ES query integration
* @param \WP_Query $query WP_Query object
*
* @since 4.5
* @return bool
*/
public function skip_query_integration( $skip, $query ) {
if ( ! $query instanceof \WP_Query ) {
return $skip;
}

$unsupported_orderby = [
'post__in',
'post_name__in',
'post_parent__in',
'parent',
];

$orderby = is_string( $query->get( 'orderby' ) ) ? explode( ' ', $query->get( 'orderby' ) ) : $query->get( 'orderby', 'date' );

$parse_orderby = array();
foreach ( $orderby as $key => $value ) {
$parse_orderby[] = is_string( $key ) ? $key : $value;
}

if ( array_intersect( $parse_orderby, $unsupported_orderby ) ) {
return true;
}

return $skip;
}
}
42 changes: 42 additions & 0 deletions tests/php/indexables/TestPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -7515,4 +7515,46 @@ function( $preempt, $parsed_args, $url ) {
$this->assertTrue( $query->elasticsearch_success );
}

/**
* Test that query with unsupported orderby does not use EP.
*
* @since 4.5.0
*/
public function testQueryWithUnSupportedOrderByDoesNotUseEP() {
// test for post__in
$query = new \WP_Query(
array(
'orderby' => 'post__in',
'ep_integrate' => true,
)
);
$this->assertNull( $query->elasticsearch_success );

// test for post_name__in
$query = new \WP_Query(
array(
'orderby' => 'post_name__in',
'ep_integrate' => true,
)
);
$this->assertNull( $query->elasticsearch_success );

// test for post_parent__in
$query = new \WP_Query(
array(
'orderby' => 'post_parent__in',
'ep_integrate' => true,
)
);
$this->assertNull( $query->elasticsearch_success );

// test for parent
$query = new \WP_Query(
array(
'orderby' => 'parent',
'ep_integrate' => true,
)
);
$this->assertNull( $query->elasticsearch_success );
}
}

0 comments on commit afd996b

Please sign in to comment.