Media: Refactor search by filename within the admin.

Props vortfu, xknown, peterwilsoncc, paulkevan.


git-svn-id: https://develop.svn.wordpress.org/trunk@54524 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jb Audras
2022-10-17 11:17:38 +00:00
parent 5fcdee1b4d
commit 8836d46822
7 changed files with 80 additions and 54 deletions
+30 -18
View File
@@ -454,7 +454,7 @@ class Tests_Query_Search extends WP_UnitTestCase {
);
add_post_meta( $attachment, '_wp_attached_file', 'some-image1.png', true );
add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
add_filter( 'wp_allow_query_attachment_by_filename', '__return_true' );
// Pass post_type a string value.
$q = new WP_Query(
@@ -484,7 +484,7 @@ class Tests_Query_Search extends WP_UnitTestCase {
);
add_post_meta( $attachment, '_wp_attached_file', 'some-image2.png', true );
add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
add_filter( 'wp_allow_query_attachment_by_filename', '__return_true' );
// Pass post_type an array value.
$q = new WP_Query(
@@ -543,7 +543,7 @@ class Tests_Query_Search extends WP_UnitTestCase {
add_post_meta( $attachment, '_wp_attached_file', 'some-image4.png', true );
add_post_meta( $attachment, '_test_meta_key', 'value', true );
add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
add_filter( 'wp_allow_query_attachment_by_filename', '__return_true' );
// Pass post_type a string value.
$q = new WP_Query(
@@ -583,7 +583,7 @@ class Tests_Query_Search extends WP_UnitTestCase {
wp_set_post_terms( $attachment, 'test', 'post_tag' );
add_post_meta( $attachment, '_wp_attached_file', 'some-image5.png', true );
add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
add_filter( 'wp_allow_query_attachment_by_filename', '__return_true' );
// Pass post_type a string value.
$q = new WP_Query(
@@ -608,25 +608,37 @@ class Tests_Query_Search extends WP_UnitTestCase {
/**
* @ticket 22744
*/
public function test_filter_query_attachment_filenames_unhooks_itself() {
add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
apply_filters(
'posts_clauses',
public function test_wp_query_removes_filter_wp_allow_query_attachment_by_filename() {
$attachment = self::factory()->post->create(
array(
'where' => '',
'groupby' => '',
'join' => '',
'orderby' => '',
'distinct' => '',
'fields' => '',
'limit' => '',
'post_type' => 'attachment',
'post_status' => 'publish',
'post_title' => 'bar foo',
'post_content' => 'foo bar',
'post_excerpt' => 'This post has foo',
)
);
$result = has_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
add_post_meta( $attachment, '_wp_attached_file', 'some-image1.png', true );
add_filter( 'wp_allow_query_attachment_by_filename', '__return_true' );
$this->assertFalse( $result );
$q = new WP_Query(
array(
's' => 'image1',
'fields' => 'ids',
'post_type' => 'attachment',
'post_status' => 'inherit',
)
);
$this->assertSame( array( $attachment ), $q->posts );
/*
* WP_Query should have removed the wp_allow_query_attachment_by_filename filter
* and thus not match the attachment created above.
*/
$q->get_posts();
$this->assertEmpty( $q->posts );
}
public function filter_posts_search( $sql ) {