Skip to content

Commit d182bc4

Browse files
committed
Fix: Media search not working
1 parent 7b2c8c7 commit d182bc4

File tree

3 files changed

+82
-3
lines changed

3 files changed

+82
-3
lines changed

includes/classes/Feature/ProtectedContent/ProtectedContent.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -464,8 +464,8 @@ public function maybe_change_sort( $default_sort ) {
464464
* @return array
465465
*/
466466
public function filter_private_posts_for_current_user( $formatted_args, $args ): array {
467-
$post_types = (array) $args['post_type'];
468-
467+
$post_types = (array) $args['post_type'];
468+
$post_statuses = (array) ( is_string( $args['post_status'] ) ? explode( ',', $args['post_status'] ) : $args['post_status'] );
469469
$valid_post_types = array_filter( $post_types, 'post_type_exists' );
470470

471471
$base_statuses = array_merge(
@@ -476,8 +476,9 @@ public function filter_private_posts_for_current_user( $formatted_args, $args ):
476476
'show_in_admin_all_list' => true,
477477
]
478478
),
479-
! empty( $args['post_status'] ) ? (array) $args['post_status'] : []
479+
$post_statuses
480480
);
481+
$base_statuses = array_unique( $base_statuses );
481482

482483
$post_types_with_capability = [];
483484
$post_types_without_capability = [];
@@ -502,6 +503,7 @@ public function filter_private_posts_for_current_user( $formatted_args, $args ):
502503

503504
if ( ! empty( $post_types_with_capability ) ) {
504505
$all_statuses = array_merge( $base_statuses, get_post_stati( [ 'private' => true ] ) );
506+
$all_statuses = array_unique( $all_statuses );
505507

506508
$should_clauses[] = [
507509
'bool' => [

tests/e2e/src/specs/protected-content.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,26 @@ test.describe('Protected Content Feature', { tag: '@group1' }, () => {
6969
await expect(results.hits.total.value).toBe(1);
7070
});
7171

72+
test('Can use Elasticsearch in the Media Library Admin Screen', async ({ loggedInPage }) => {
73+
await maybeEnableFeature('protected_content');
74+
await goToAdminPage(loggedInPage, 'upload.php?mode=list');
75+
await expect(
76+
loggedInPage.locator('#debug-menu-target-EP_Debug_Bar_ElasticPress'),
77+
).toContainText('Time Taken');
78+
79+
// Check there are some rows in the list
80+
const initialRows = loggedInPage.locator('#the-list tr');
81+
await expect(initialRows.count()).resolves.toBeGreaterThan(0);
82+
83+
await loggedInPage.locator('#media-search-input').fill('woocommerce-placeholder');
84+
await loggedInPage.locator('#media-search-input').press('Enter');
85+
86+
// Wait for search results and verify exactly 1 row
87+
await loggedInPage.waitForLoadState('networkidle');
88+
const rows = loggedInPage.locator('#the-list tr');
89+
await expect(rows).toHaveCount(1);
90+
});
91+
7292
test('Can sync autosaved drafts', async ({ loggedInPage }) => {
7393
await maybeEnableFeature('protected_content');
7494

tests/php/features/TestProtectedContent.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,4 +623,61 @@ public function test_post_statuses_for_admin() {
623623
$this->assertContains( 'pending', $statuses );
624624
$this->assertContains( 'private', $statuses );
625625
}
626+
627+
/**
628+
* Tests post statuses for admin with multiple statuses.
629+
*
630+
* @since 5.3.2
631+
* @group protected-content
632+
*/
633+
public function test_post_statuses_for_admin_with_multiple_statuses() {
634+
set_current_screen( 'edit.php' );
635+
$this->assertTrue( is_admin() );
636+
637+
ElasticPress\Features::factory()->activate_feature( 'protected_content' );
638+
ElasticPress\Features::factory()->setup_features();
639+
640+
$post_1_id = $this->ep_factory->post->create(
641+
[
642+
'post_status' => 'inherit',
643+
]
644+
);
645+
$post_2_id = $this->ep_factory->post->create(
646+
[
647+
'post_status' => 'private',
648+
]
649+
);
650+
$this->ep_factory->post->create(
651+
[
652+
'post_status' => 'draft',
653+
]
654+
);
655+
656+
ElasticPress\Elasticsearch::factory()->refresh_indices();
657+
658+
$query = new \WP_Query(
659+
[
660+
'post_status' => 'inherit,private',
661+
'ep_integrate' => true,
662+
'orderby' => 'date',
663+
]
664+
);
665+
666+
$this->assertTrue( $query->elasticsearch_success );
667+
$this->assertEquals( 2, $query->found_posts );
668+
$this->assertEquals( $post_1_id, $query->posts[0]->ID );
669+
$this->assertEquals( $post_2_id, $query->posts[1]->ID );
670+
671+
$query = new \WP_Query(
672+
[
673+
'post_status' => [ 'inherit', 'private' ],
674+
'ep_integrate' => true,
675+
'orderby' => 'date',
676+
]
677+
);
678+
$this->assertTrue( $query->elasticsearch_success );
679+
$this->assertEquals( 2, $query->found_posts );
680+
$this->assertEquals( $post_1_id, $query->posts[0]->ID );
681+
$this->assertEquals( $post_2_id, $query->posts[1]->ID );
682+
}
626683
}

0 commit comments

Comments
 (0)