Skip to content

Commit

Permalink
Media: Improve plugin compatibility of taxonomy query (#13208)
Browse files Browse the repository at this point in the history
Co-authored-by: Pascal Birchler <pascalb@google.com>
Co-authored-by: Anurag Vasanwala <75766877+AnuragVasanwala@users.noreply.github.com>
  • Loading branch information
3 people committed Jun 21, 2023
1 parent 76042d3 commit 636e2df
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 53 deletions.
38 changes: 20 additions & 18 deletions includes/Media/Media_Source_Taxonomy.php
Expand Up @@ -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 );
}

/**
Expand Down Expand Up @@ -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;
}
}
74 changes: 39 additions & 35 deletions tests/phpunit/integration/tests/Media/Media_Source_Taxonomy.php
Expand Up @@ -57,7 +57,7 @@ public function test_register(): void {
)
);
$this->assertSame(
10,
PHP_INT_MAX,
has_filter(
'ajax_query_attachments_args',
[
Expand All @@ -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',
[
Expand Down Expand Up @@ -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',
],
],
];
Expand All @@ -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',
],
],
],
],
];

Expand Down Expand Up @@ -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' );
Expand Down Expand Up @@ -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',
],
],
];
Expand Down

0 comments on commit 636e2df

Please sign in to comment.