diff --git a/includes/Media/Media_Source_Taxonomy.php b/includes/Media/Media_Source_Taxonomy.php index 5f39915c201f..0648dbb9632d 100644 --- a/includes/Media/Media_Source_Taxonomy.php +++ b/includes/Media/Media_Source_Taxonomy.php @@ -84,11 +84,11 @@ public function register(): void { add_filter( 'wp_prepare_attachment_for_js', [ $this, 'wp_prepare_attachment_for_js' ] ); // Hide video posters from Media grid view. - add_filter( 'ajax_query_attachments_args', [ $this, 'filter_ajax_query_attachments_args' ] ); + add_filter( 'ajax_query_attachments_args', [ $this, 'filter_ajax_query_attachments_args' ], PHP_INT_MAX ); // Hide video posters from Media list view. - add_action( 'pre_get_posts', [ $this, 'filter_generated_media_attachments' ] ); + add_action( 'pre_get_posts', [ $this, 'filter_generated_media_attachments' ], PHP_INT_MAX ); // Hide video posters from web-stories/v1/media REST API requests. - add_filter( 'web_stories_rest_attachment_query', [ $this, 'filter_rest_generated_media_attachments' ] ); + add_filter( 'web_stories_rest_attachment_query', [ $this, 'filter_rest_generated_media_attachments' ], PHP_INT_MAX ); } /** @@ -379,23 +379,25 @@ private function get_exclude_tax_query( array $args ): array { * [ [ any ], [ existing ], [ tax queries] ] * ] */ - array_unshift( - $tax_query, + $new_tax_query = [ + 'relation' => 'AND', [ - [ - 'taxonomy' => $this->taxonomy_slug, - 'field' => 'slug', - 'terms' => [ - self::TERM_POSTER_GENERATION, - self::TERM_SOURCE_VIDEO, - self::TERM_SOURCE_IMAGE, - self::TERM_PAGE_TEMPLATE, - ], - 'operator' => 'NOT IN', + 'taxonomy' => $this->taxonomy_slug, + 'field' => 'slug', + 'terms' => [ + self::TERM_POSTER_GENERATION, + self::TERM_SOURCE_VIDEO, + self::TERM_SOURCE_IMAGE, + self::TERM_PAGE_TEMPLATE, ], - ] - ); + 'operator' => 'NOT IN', + ], + ]; + + if ( ! empty( $tax_query ) ) { + $new_tax_query[] = [ $tax_query ]; + } - return $tax_query; + return $new_tax_query; } } diff --git a/tests/phpunit/integration/tests/Media/Media_Source_Taxonomy.php b/tests/phpunit/integration/tests/Media/Media_Source_Taxonomy.php index ae4aaf7661f5..b5e1332229e9 100644 --- a/tests/phpunit/integration/tests/Media/Media_Source_Taxonomy.php +++ b/tests/phpunit/integration/tests/Media/Media_Source_Taxonomy.php @@ -57,7 +57,7 @@ public function test_register(): void { ) ); $this->assertSame( - 10, + PHP_INT_MAX, has_filter( 'ajax_query_attachments_args', [ @@ -66,9 +66,9 @@ public function test_register(): void { ] ) ); - $this->assertSame( 10, has_filter( 'pre_get_posts', [ $this->instance, 'filter_generated_media_attachments' ] ) ); + $this->assertSame( PHP_INT_MAX, has_filter( 'pre_get_posts', [ $this->instance, 'filter_generated_media_attachments' ] ) ); $this->assertSame( - 10, + PHP_INT_MAX, has_filter( 'web_stories_rest_attachment_query', [ @@ -215,13 +215,12 @@ public function test_filter_ajax_query_attachments_args(): void { $expected = [ 'tax_query' => [ + 'relation' => 'AND', [ - [ - 'taxonomy' => $tax_slug, - 'field' => 'slug', - 'terms' => [ 'poster-generation', 'source-video', 'source-image', 'page-template' ], - 'operator' => 'NOT IN', - ], + 'taxonomy' => $tax_slug, + 'field' => 'slug', + 'terms' => [ 'poster-generation', 'source-video', 'source-image', 'page-template' ], + 'operator' => 'NOT IN', ], ], ]; @@ -240,20 +239,23 @@ public function test_filter_ajax_query_attachments_args_existing_tax_query(): vo $expected = [ 'tax_query' => [ + 'relation' => 'AND', [ - [ - 'taxonomy' => $tax_slug, - 'field' => 'slug', - 'terms' => [ 'poster-generation', 'source-video', 'source-image', 'page-template' ], - 'operator' => 'NOT IN', - ], - ], - [ - 'taxonomy' => 'category', + 'taxonomy' => $tax_slug, 'field' => 'slug', - 'terms' => [ 'uncategorized' ], + 'terms' => [ 'poster-generation', 'source-video', 'source-image', 'page-template' ], 'operator' => 'NOT IN', ], + [ + [ + [ + 'taxonomy' => 'category', + 'field' => 'slug', + 'terms' => [ 'uncategorized' ], + 'operator' => 'NOT IN', + ], + ], + ], ], ]; @@ -323,20 +325,23 @@ public function test_filter_generated_media_attachments_not_upload_screen(): voi public function test_filter_generated_media_attachmentss(): void { $expected = [ + 'relation' => 'AND', [ - [ - 'taxonomy' => $this->instance->get_taxonomy_slug(), - 'field' => 'slug', - 'terms' => [ 'poster-generation', 'source-video', 'source-image', 'page-template' ], - 'operator' => 'NOT IN', - ], - ], - [ - 'taxonomy' => 'category', + 'taxonomy' => $this->instance->get_taxonomy_slug(), 'field' => 'slug', - 'terms' => [ 'uncategorized' ], + 'terms' => [ 'poster-generation', 'source-video', 'source-image', 'page-template' ], 'operator' => 'NOT IN', ], + [ + [ + [ + 'taxonomy' => 'category', + 'field' => 'slug', + 'terms' => [ 'uncategorized' ], + 'operator' => 'NOT IN', + ], + ], + ], ]; $GLOBALS['current_screen'] = convert_to_screen( 'upload' ); @@ -370,13 +375,12 @@ public function test_filter_rest_generated_media_attachments(): void { $expected = [ 'tax_query' => [ + 'relation' => 'AND', [ - [ - 'taxonomy' => $tax_slug, - 'field' => 'slug', - 'terms' => [ 'poster-generation', 'source-video', 'source-image', 'page-template' ], - 'operator' => 'NOT IN', - ], + 'taxonomy' => $tax_slug, + 'field' => 'slug', + 'terms' => [ 'poster-generation', 'source-video', 'source-image', 'page-template' ], + 'operator' => 'NOT IN', ], ], ];