From 221e18d99e0f7d05f96787c5fd0575c8741544b0 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Mon, 28 Nov 2022 10:04:13 -0300 Subject: [PATCH] Add a new ep_bypass_exclusion_from_search filter --- .../Feature/InstantResults/InstantResults.php | 15 +++++++ includes/classes/Feature/Search/Search.php | 13 +++++- tests/php/indexables/TestPost.php | 45 +++++++++++++++++++ 3 files changed, 71 insertions(+), 2 deletions(-) diff --git a/includes/classes/Feature/InstantResults/InstantResults.php b/includes/classes/Feature/InstantResults/InstantResults.php index b5a4ad53f2..f06b9873c9 100644 --- a/includes/classes/Feature/InstantResults/InstantResults.php +++ b/includes/classes/Feature/InstantResults/InstantResults.php @@ -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' ] ); @@ -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. * diff --git a/includes/classes/Feature/Search/Search.php b/includes/classes/Feature/Search/Search.php index 27cc0b2fb2..64f367ee6b 100644 --- a/includes/classes/Feature/Search/Search.php +++ b/includes/classes/Feature/Search/Search.php @@ -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; } diff --git a/tests/php/indexables/TestPost.php b/tests/php/indexables/TestPost.php index 41b8d611d4..bfa45200e6 100644 --- a/tests/php/indexables/TestPost.php +++ b/tests/php/indexables/TestPost.php @@ -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. */