diff --git a/includes/classes/Feature/Documents/Documents.php b/includes/classes/Feature/Documents/Documents.php index 29ee7352e..f7e0d552b 100644 --- a/includes/classes/Feature/Documents/Documents.php +++ b/includes/classes/Feature/Documents/Documents.php @@ -128,8 +128,14 @@ public function setup_document_search( $query ) { return; } + // Return if attachments are not involved in the query. + // If post_type is empty, attachments will be included automatically. + $post_type = (array) $query->get( 'post_type', [] ); + if ( ! empty( $post_type ) && ! in_array( 'attachment', $post_type, true ) ) { + return; + } + $this->maybe_set_post_status( $query ); - $this->maybe_set_post_type( $query ); $this->maybe_set_mime_type( $query ); } @@ -523,24 +529,6 @@ protected function maybe_set_post_status( $query ) { $query->set( 'post_status', array_unique( $post_status ) ); } - /** - * Add the attachment post type to post_type. - * - * @param WP_Query $query WP_Query to modify to search. - * @return void - */ - protected function maybe_set_post_type( $query ) { - $post_type = $query->get( 'post_type', [] ); - if ( empty( $post_type ) || ! is_string( $post_type ) || 'any' === $post_type ) { - return; - } - - $post_type = explode( ' ', $post_type ); - $post_type[] = 'attachment'; - - $query->set( 'post_type', array_unique( $post_type ) ); - } - /** * Add allowed mime types. If mime types are already set, append. * diff --git a/tests/php/features/TestDocuments.php b/tests/php/features/TestDocuments.php index ee9b85036..eb3525a5d 100644 --- a/tests/php/features/TestDocuments.php +++ b/tests/php/features/TestDocuments.php @@ -300,4 +300,57 @@ public function test_ep_allowed_documents_ingest_mime_types_filter() { $this->assertSame( 'text/test', $feature->get_allowed_ingest_mime_types()['test'] ); } + + /** + * Depending on the WP_Query post_type parameter, attachments should be added by default. + * + * @since 5.1.0 + * @group documents + */ + public function test_empty_post_type() { + ElasticPress\Features::factory()->activate_feature( 'search' ); + ElasticPress\Features::factory()->activate_feature( 'documents' ); + ElasticPress\Features::factory()->setup_features(); + + $this->ep_factory->post->create( + array( + 'post_title' => 'findme', + 'post_type' => 'post', + ) + ); + $this->ep_factory->post->create( + array( + 'post_title' => 'findme', + 'post_type' => 'attachment', + 'post_mime_type' => 'application/msword', + ) + ); + + ElasticPress\Elasticsearch::factory()->refresh_indices(); + + // No post type, attachment added by default + $query = new \WP_Query( [ 's' => 'findme' ] ); + $this->assertTrue( $query->elasticsearch_success ); + $this->assertEquals( 2, $query->post_count ); + + // Post type as string, attachment not added by default + $query = new \WP_Query( + [ + 'post_type' => 'post', + 's' => 'findme', + ] + ); + $this->assertTrue( $query->elasticsearch_success ); + $this->assertEquals( 1, $query->post_count ); + + // Post type as array, attachment not added by default + $query = new \WP_Query( + [ + 'post_type' => [ 'post' ], + 's' => 'findme', + ] + ); + $this->assertTrue( $query->elasticsearch_success ); + $this->assertEquals( 1, $query->post_count ); + } } diff --git a/tests/php/features/TestFacetTypeDate.php b/tests/php/features/TestFacetTypeDate.php index f3a3df3f8..65d5379aa 100644 --- a/tests/php/features/TestFacetTypeDate.php +++ b/tests/php/features/TestFacetTypeDate.php @@ -26,6 +26,10 @@ class TestFacetTypeDate extends BaseTestCase { * Setup each test. */ public function set_up() { + ElasticPress\Elasticsearch::factory()->delete_all_indices(); + ElasticPress\Indexables::factory()->get( 'post' )->put_mapping(); + ElasticPress\Indexables::factory()->get( 'post' )->sync_manager->reset_sync_queue(); + $facet_feature = Features::factory()->get_registered_feature( 'facets' ); $this->facet_type = $facet_feature->types['date']; @@ -332,6 +336,7 @@ public function testQueryPost() { 'ep_integrate' => true, ] ); + $this->assertTrue( $query->elasticsearch_success ); $this->assertEquals( 2, $query->found_posts ); // get all posts published on or after 2022-01-01. @@ -341,6 +346,7 @@ public function testQueryPost() { 'ep_integrate' => true, ] ); + $this->assertTrue( $query->elasticsearch_success ); $this->assertEquals( 4, $query->found_posts ); // get all posts published on or before 2022-01-01. @@ -350,6 +356,7 @@ public function testQueryPost() { 'ep_integrate' => true, ] ); + $this->assertTrue( $query->elasticsearch_success ); $this->assertEquals( 2, $query->found_posts ); // passing invalid date shouldn't apply any filter. @@ -359,6 +366,7 @@ public function testQueryPost() { 'ep_integrate' => true, ] ); + $this->assertTrue( $query->elasticsearch_success ); $this->assertEquals( 5, $query->found_posts ); } }