Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix post_mime_type argument #2222

Merged
merged 5 commits into from
Jun 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion includes/classes/Indexable/Post/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -994,9 +994,26 @@ function( $tax_query ) use ( $args ) {
*/
if ( ! empty( $args['post_mime_type'] ) ) {
if ( is_array( $args['post_mime_type'] ) ) {

$args_post_mime_type = [];

foreach ( $args['post_mime_type'] as $mime_type ) {
/**
* check if matches the MIME type pattern: type/subtype and
* leave an empty string as posts, pages and CPTs don't have a MIME type
*/
if ( preg_match( '/^[-._a-z0-9]+\/[-._a-z0-9]+$/i', $mime_type ) || empty( $mime_type ) ) {
felipeelia marked this conversation as resolved.
Show resolved Hide resolved
$args_post_mime_type[] = $mime_type;
} else {
$filtered_mime_type_by_type = wp_match_mime_types( $mime_type, wp_get_mime_types() );

$args_post_mime_type = array_merge( $args_post_mime_type, $filtered_mime_type_by_type[ $mime_type ] );
}
}

$filter['bool']['must'][] = array(
'terms' => array(
'post_mime_type' => (array) $args['post_mime_type'],
'post_mime_type' => $args_post_mime_type,
),
);

Expand Down
122 changes: 118 additions & 4 deletions tests/php/indexables/TestPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -3950,27 +3950,34 @@ public function testTaxQueryNotExists() {
* @since 2.3
*/
public function testPostMimeTypeQuery() {
Functions\create_and_sync_post(
$attachment_id_1_jpeg = Functions\create_and_sync_post(
array(
'post_type' => 'attachment',
'post_mime_type' => 'image/jpeg',
'post_status' => 'inherit',
)
);
Functions\create_and_sync_post(
$attachment_id_2_jpeg = Functions\create_and_sync_post(
array(
'post_type' => 'attachment',
'post_mime_type' => 'image/jpeg',
'post_status' => 'inherit',
)
);
Functions\create_and_sync_post(
$attachment_id_3_pdf = Functions\create_and_sync_post(
array(
'post_type' => 'attachment',
'post_mime_type' => 'application/pdf',
'post_status' => 'inherit',
)
);
$attachment_id_4_png = Functions\create_and_sync_post(
array(
'post_type' => 'attachment',
'post_mime_type' => 'image/png',
'post_status' => 'inherit',
)
);

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

Expand All @@ -3983,7 +3990,28 @@ public function testPostMimeTypeQuery() {

$query = new \WP_Query( $args );

$this->assertEquals( 2, $query->post_count );
$attachment_names = wp_list_pluck( $query->posts, 'post_name' );

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

$this->assertContains( get_post_field( 'post_name', $attachment_id_1_jpeg ), $attachment_names );
$this->assertContains( get_post_field( 'post_name', $attachment_id_2_jpeg ), $attachment_names );
$this->assertContains( get_post_field( 'post_name', $attachment_id_4_png ), $attachment_names );

$args = array(
'ep_integrate' => true,
'post_mime_type' => 'image/png',
'post_type' => 'attachment',
'post_status' => 'inherit',
);

$query = new \WP_Query( $args );

$attachment_names = wp_list_pluck( $query->posts, 'post_name' );

$this->assertEquals( 1, $query->post_count );

$this->assertContains( get_post_field( 'post_name', $attachment_id_4_png ), $attachment_names );

$args = array(
'ep_integrate' => true,
Expand All @@ -3997,7 +4025,51 @@ public function testPostMimeTypeQuery() {

$query = new \WP_Query( $args );

$attachment_names = wp_list_pluck( $query->posts, 'post_name' );

$this->assertEquals( 3, $query->found_posts );

$this->assertContains( get_post_field( 'post_name', $attachment_id_1_jpeg ), $attachment_names );
$this->assertContains( get_post_field( 'post_name', $attachment_id_2_jpeg ), $attachment_names );
$this->assertContains( get_post_field( 'post_name', $attachment_id_3_pdf ), $attachment_names );

$args = array(
'ep_integrate' => true,
'post_mime_type' => array(
'image',
'application/pdf',
),
'post_type' => 'attachment',
'post_status' => 'inherit',
);

$query = new \WP_Query( $args );

$attachment_names = wp_list_pluck( $query->posts, 'post_name' );

$this->assertEquals( 4, $query->found_posts );

$this->assertContains( get_post_field( 'post_name', $attachment_id_1_jpeg ), $attachment_names );
$this->assertContains( get_post_field( 'post_name', $attachment_id_2_jpeg ), $attachment_names );
$this->assertContains( get_post_field( 'post_name', $attachment_id_3_pdf ), $attachment_names );
$this->assertContains( get_post_field( 'post_name', $attachment_id_4_png ), $attachment_names );

$args = array(
'ep_integrate' => true,
'post_mime_type' => array(
'image/png',
),
felipeelia marked this conversation as resolved.
Show resolved Hide resolved
'post_type' => 'attachment',
'post_status' => 'inherit',
);

$query = new \WP_Query( $args );

$attachment_names = wp_list_pluck( $query->posts, 'post_name' );

$this->assertEquals( 1, $query->found_posts );

$this->assertContains( get_post_field( 'post_name', $attachment_id_4_png ), $attachment_names );
}

/**
Expand Down Expand Up @@ -5254,7 +5326,49 @@ public function testFormatArgsPostMimeType() {
$query
);

$this->assertCount( 1, $args['post_filter']['bool']['must'][0]['terms']['post_mime_type'] );
$this->assertSame( 'image/jpeg', $args['post_filter']['bool']['must'][0]['terms']['post_mime_type'][0] );

$args = $post->format_args(
[
'post_mime_type' => [ 'image/jpeg', 'application/pdf' ],
],
$query
);

$this->assertCount( 2, $args['post_filter']['bool']['must'][0]['terms']['post_mime_type'] );
$this->assertContains( 'image/jpeg', $args['post_filter']['bool']['must'][0]['terms']['post_mime_type'] );
$this->assertContains( 'application/pdf', $args['post_filter']['bool']['must'][0]['terms']['post_mime_type'] );

$args = $post->format_args(
[
'post_mime_type' => [ 'image' ],
],
$query
);

$this->assertGreaterThan( 1, count( $args['post_filter']['bool']['must'][0]['terms']['post_mime_type'] ) );
$this->assertContains( 'image/jpeg', $args['post_filter']['bool']['must'][0]['terms']['post_mime_type'] );

$args = $post->format_args(
[
'post_mime_type' => [ 'image', 'application/pdf' ],
],
$query
);

$this->assertGreaterThan( 2, count( $args['post_filter']['bool']['must'][0]['terms']['post_mime_type'] ) );
$this->assertContains( 'image/jpeg', $args['post_filter']['bool']['must'][0]['terms']['post_mime_type'] );
$this->assertContains( 'application/pdf', $args['post_filter']['bool']['must'][0]['terms']['post_mime_type'] );

$args = $post->format_args(
[
'post_mime_type' => [],
],
$query
);

$this->assertArrayNotHasKey( 'terms', $args['post_filter']['bool']['must'][0] );
}

/**
Expand Down