Skip to content

Commit

Permalink
Merge pull request #3161 from 10up/chore/new-ep_bypass_exclusion_from…
Browse files Browse the repository at this point in the history
…_search-filter

Add a new ep_bypass_exclusion_from_search filter
  • Loading branch information
felipeelia authored Nov 28, 2022
2 parents ceb48c1 + 221e18d commit 68b1dd4
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
15 changes: 15 additions & 0 deletions includes/classes/Feature/InstantResults/InstantResults.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ public function setup() {
add_filter( 'ep_post_sync_args', [ $this, 'add_post_sync_args' ], 10, 2 );
add_filter( 'ep_after_sync_index', [ $this, 'epio_save_search_template' ] );
add_filter( 'ep_saved_weighting_configuration', [ $this, 'epio_save_search_template' ] );
add_filter( 'ep_bypass_exclusion_from_search', [ $this, 'maybe_bypass_post_exclusion' ], 10, 2 );
add_action( 'pre_get_posts', [ $this, 'maybe_apply_product_visibility' ] );
add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_frontend_assets' ] );
add_action( 'wp_footer', [ $this, 'render' ] );
Expand Down Expand Up @@ -571,6 +572,20 @@ public function intercept_search_request( $response, $query = [], $args = [], $f
return wp_remote_request( $query['url'], $args );
}

/**
* If generating the search template query, do not bypass the post exclusion
*
* @since 4.4.0
* @param bool $bypass_exclusion_from_search Whether the post exclusion from search should be applied or not
* @param WP_Query $query The WP Query
* @return bool
*/
public function maybe_bypass_post_exclusion( $bypass_exclusion_from_search, $query ) {
return true === $query->get( 'ep_search_template' ) ?
false : // not bypass, apply
$bypass_exclusion_from_search;
}

/**
* Apply product visibility taxonomy query to search template queries.
*
Expand Down
13 changes: 11 additions & 2 deletions includes/classes/Feature/Search/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -710,8 +710,17 @@ public function enqueue_block_editor_assets() {
* @param WP_Query $query WP Query
*/
public function exclude_posts_from_search( $query ) {

if ( is_admin() || ! $query->is_search() ) {
$bypass_exclusion_from_search = is_admin() || ! $query->is_search();
/**
* Filter whether the exclusion from the "exclude from search" checkbox should be applied
*
* @since 4.4.0
* @hook ep_bypass_exclusion_from_search
* @param {bool} $bypass_exclusion_from_search True means all posts will be returned
* @param {WP_Query} $query WP Query
* @return {bool} New $bypass_exclusion_from_search value
*/
if ( apply_filters( 'ep_bypass_exclusion_from_search', $bypass_exclusion_from_search, $query ) ) {
return;
}

Expand Down
45 changes: 45 additions & 0 deletions tests/php/indexables/TestPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -7633,6 +7633,51 @@ public function testHighlightTags() {

}

/**
* Tests the `ep_bypass_exclusion_from_search` filter
*/
public function testExcludeFromSearchQueryBypassFilter() {
$this->ep_factory->post->create_many(
2,
array(
'post_content' => 'find me in search',
'meta_input' => array( 'ep_exclude_from_search' => false ),
)
);
$this->ep_factory->post->create(
array(
'post_content' => 'exlcude from search',
'meta_input' => array( 'ep_exclude_from_search' => true ),
)
);

ElasticPress\Elasticsearch::factory()->refresh_indices();

$bypass = function( $should_bypass, $query ) {
$this->assertInstanceOf( \WP_Query::class, $query );
return true;
};
add_filter( 'ep_bypass_exclusion_from_search', $bypass, 10, 2 );

$args = array(
's' => 'search',
);
$query = new \WP_Query( $args );

$this->assertTrue( $query->elasticsearch_success );
$this->assertEquals( 3, $query->post_count );

remove_filter( 'ep_bypass_exclusion_from_search', $bypass, 10, 2 );

$args = array(
's' => 'search',
);
$query = new \WP_Query( $args );

$this->assertTrue( $query->elasticsearch_success );
$this->assertEquals( 2, $query->post_count );
}

/**
* Tests query doesn't return the post in if `ep_exclude_from_search` meta is set.
*/
Expand Down

0 comments on commit 68b1dd4

Please sign in to comment.