Skip to content

Commit

Permalink
Merge pull request #3889 from 10up/fix/issue-3884
Browse files Browse the repository at this point in the history
Fix post type addition
  • Loading branch information
felipeelia committed Apr 10, 2024
2 parents eb6e7c9 + 48037ce commit 1fcc214
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 19 deletions.
26 changes: 7 additions & 19 deletions includes/classes/Feature/Documents/Documents.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

Expand Down Expand Up @@ -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.
*
Expand Down
53 changes: 53 additions & 0 deletions tests/php/features/TestDocuments.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
8 changes: 8 additions & 0 deletions tests/php/features/TestFacetTypeDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];

Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -359,6 +366,7 @@ public function testQueryPost() {
'ep_integrate' => true,
]
);
$this->assertTrue( $query->elasticsearch_success );
$this->assertEquals( 5, $query->found_posts );
}
}

0 comments on commit 1fcc214

Please sign in to comment.