From 948ed11ed70f3f02c90761106937ca990ddb0fc5 Mon Sep 17 00:00:00 2001 From: Burhan Nasir Date: Thu, 15 Feb 2024 17:03:58 +0500 Subject: [PATCH 1/2] Fix: Media search returns no return in admin dashboard --- .../classes/Feature/Documents/Documents.php | 5 +++ tests/php/features/TestDocuments.php | 38 +++++++++++++++++-- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/includes/classes/Feature/Documents/Documents.php b/includes/classes/Feature/Documents/Documents.php index f5046b011d..d1064c8759 100644 --- a/includes/classes/Feature/Documents/Documents.php +++ b/includes/classes/Feature/Documents/Documents.php @@ -166,6 +166,11 @@ public function setup_document_search( $query ) { $query->set( 'post_status', array_unique( $post_status ) ); + // Bail, if query is for media library. + if ( isset( $_REQUEST['action'] ) && 'query-attachments' === $_REQUEST['action'] ) { + return; + } + if ( ! empty( $mime_types ) && is_string( $mime_types ) ) { $mime_types = explode( ' ', $mime_types ); } diff --git a/tests/php/features/TestDocuments.php b/tests/php/features/TestDocuments.php index 59c15d772e..7a027bb4ca 100644 --- a/tests/php/features/TestDocuments.php +++ b/tests/php/features/TestDocuments.php @@ -107,8 +107,6 @@ public function testSearchDisallowedMimeType() { // Need to call this since it's hooked to init ElasticPress\Features::factory()->get_registered_feature( 'search' )->search_setup(); - $post_ids = array(); - $this->ep_factory->post->create(); $this->ep_factory->post->create( array( @@ -152,8 +150,6 @@ public function testSearchNormalPost() { // Need to call this since it's hooked to init ElasticPress\Features::factory()->get_registered_feature( 'search' )->search_setup(); - $post_ids = array(); - $this->ep_factory->post->create( array( 'post_content' => 'findme', @@ -226,4 +222,38 @@ public function testExcludeFromSearchQuery() { $this->assertTrue( $query->elasticsearch_success ); $this->assertEquals( 2, $query->post_count ); } + + /** + * Test that search in media library is working correctly. + * + * @since 5.1.0 + * @group documents + */ + public function testQueryForAttachments() { + ElasticPress\Features::factory()->activate_feature( 'search' ); + ElasticPress\Features::factory()->activate_feature( 'documents' ); + ElasticPress\Features::factory()->setup_features(); + + $this->ep_factory->post->create( + array( + 'post_content' => 'search me', + 'post_type' => 'attachment', + 'post_mime_type' => 'image', + ) + ); + + ElasticPress\Elasticsearch::factory()->refresh_indices(); + + $_REQUEST['action'] = 'query-attachments'; + $args = array( + 's' => 'search me', + 'post_type' => 'attachment', + 'post_status' => 'inherit', + ); + + $query = new \WP_Query( $args ); + + $this->assertTrue( $query->elasticsearch_success ); + $this->assertEquals( 1, $query->post_count ); + } } From 9eaa1babad815527d91c427d0ab79be6387550c1 Mon Sep 17 00:00:00 2001 From: Burhan Nasir Date: Wed, 21 Feb 2024 12:31:33 +0500 Subject: [PATCH 2/2] Fix linting issue --- includes/classes/Feature/Documents/Documents.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/includes/classes/Feature/Documents/Documents.php b/includes/classes/Feature/Documents/Documents.php index d1064c8759..f82e5e1587 100644 --- a/includes/classes/Feature/Documents/Documents.php +++ b/includes/classes/Feature/Documents/Documents.php @@ -129,6 +129,11 @@ public function setup_document_search( $query ) { return; } + // Bail, if query is for media library. + if ( isset( $_REQUEST['action'] ) && 'query-attachments' === $_REQUEST['action'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended + return; + } + $post_status = $query->get( 'post_status', [] ); $post_type = $query->get( 'post_type', [] ); $mime_types = $query->get( 'post_mime_type', [] ); @@ -166,11 +171,6 @@ public function setup_document_search( $query ) { $query->set( 'post_status', array_unique( $post_status ) ); - // Bail, if query is for media library. - if ( isset( $_REQUEST['action'] ) && 'query-attachments' === $_REQUEST['action'] ) { - return; - } - if ( ! empty( $mime_types ) && is_string( $mime_types ) ) { $mime_types = explode( ' ', $mime_types ); }