Skip to content

Commit 8836d46

Browse files
committed
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
1 parent 5fcdee1 commit 8836d46

File tree

7 files changed

+79
-53
lines changed

7 files changed

+79
-53
lines changed

src/wp-admin/includes/ajax-actions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3023,7 +3023,7 @@ function wp_ajax_query_attachments() {
30233023

30243024
// Filter query clauses to include filenames.
30253025
if ( isset( $query['s'] ) ) {
3026-
add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
3026+
add_filter( 'wp_allow_query_attachment_by_filename', '__return_true' );
30273027
}
30283028

30293029
/**

src/wp-admin/includes/post.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1308,7 +1308,7 @@ function wp_edit_attachments_query_vars( $q = false ) {
13081308

13091309
// Filter query clauses to include filenames.
13101310
if ( isset( $q['s'] ) ) {
1311-
add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
1311+
add_filter( 'wp_allow_query_attachment_by_filename', '__return_true' );
13121312
}
13131313

13141314
return $q;

src/wp-includes/class-wp-query.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,14 @@ class WP_Query {
445445
*/
446446
public $thumbnails_cached = false;
447447

448+
/**
449+
* Controls whether an attachment query should include filenames or not.
450+
*
451+
* @since 6.0.3
452+
* @var bool
453+
*/
454+
protected $allow_query_attachment_by_filename = false;
455+
448456
/**
449457
* Cached list of search stopwords.
450458
*
@@ -1429,8 +1437,13 @@ protected function parse_search( &$q ) {
14291437
$q['search_orderby_title'][] = $wpdb->prepare( "{$wpdb->posts}.post_title LIKE %s", $like );
14301438
}
14311439

1432-
$like = $n . $wpdb->esc_like( $term ) . $n;
1433-
$search .= $wpdb->prepare( "{$searchand}(({$wpdb->posts}.post_title $like_op %s) $andor_op ({$wpdb->posts}.post_excerpt $like_op %s) $andor_op ({$wpdb->posts}.post_content $like_op %s))", $like, $like, $like );
1440+
$like = $n . $wpdb->esc_like( $term ) . $n;
1441+
1442+
if ( ! empty( $this->allow_query_attachment_by_filename ) ) {
1443+
$search .= $wpdb->prepare( "{$searchand}(({$wpdb->posts}.post_title $like_op %s) $andor_op ({$wpdb->posts}.post_excerpt $like_op %s) $andor_op ({$wpdb->posts}.post_content $like_op %s) $andor_op (sq1.meta_value $like_op %s))", $like, $like, $like, $like );
1444+
} else {
1445+
$search .= $wpdb->prepare( "{$searchand}(({$wpdb->posts}.post_title $like_op %s) $andor_op ({$wpdb->posts}.post_excerpt $like_op %s) $andor_op ({$wpdb->posts}.post_content $like_op %s))", $like, $like, $like );
1446+
}
14341447
$searchand = ' AND ';
14351448
}
14361449

@@ -1825,6 +1838,16 @@ public function get_posts() {
18251838
// Fill again in case 'pre_get_posts' unset some vars.
18261839
$q = $this->fill_query_vars( $q );
18271840

1841+
/**
1842+
* Filters whether an attachment query should include filenames or not.
1843+
*
1844+
* @since 6.0.3
1845+
*
1846+
* @param bool $allow_query_attachment_by_filename Whether or not to include filenames.
1847+
*/
1848+
$this->allow_query_attachment_by_filename = apply_filters( 'wp_allow_query_attachment_by_filename', false );
1849+
remove_all_filters( 'wp_allow_query_attachment_by_filename' );
1850+
18281851
// Parse meta query.
18291852
$this->meta_query = new WP_Meta_Query();
18301853
$this->meta_query->parse_query_vars( $q );
@@ -2256,7 +2279,7 @@ public function get_posts() {
22562279
}
22572280
}
22582281

2259-
if ( ! empty( $this->tax_query->queries ) || ! empty( $this->meta_query->queries ) ) {
2282+
if ( ! empty( $this->tax_query->queries ) || ! empty( $this->meta_query->queries ) || ! empty( $this->allow_query_attachment_by_filename ) ) {
22602283
$groupby = "{$wpdb->posts}.ID";
22612284
}
22622285

@@ -2333,6 +2356,10 @@ public function get_posts() {
23332356
}
23342357
$where .= $search . $whichauthor . $whichmimetype;
23352358

2359+
if ( ! empty( $this->allow_query_attachment_by_filename ) ) {
2360+
$join .= " LEFT JOIN {$wpdb->postmeta} AS sq1 ON ( {$wpdb->posts}.ID = sq1.post_id AND sq1.meta_key = '_wp_attached_file' )";
2361+
}
2362+
23362363
if ( ! empty( $this->meta_query->queries ) ) {
23372364
$clauses = $this->meta_query->get_sql( 'post', $wpdb->posts, 'ID', $this );
23382365
$join .= $clauses['join'];

src/wp-includes/deprecated.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4511,3 +4511,20 @@ function global_terms_enabled() {
45114511

45124512
return false;
45134513
}
4514+
4515+
/**
4516+
* Filter the SQL clauses of an attachment query to include filenames.
4517+
*
4518+
* @since 4.7.0
4519+
* @deprecated 6.0.3
4520+
* @access private
4521+
*
4522+
* @param array $clauses An array including WHERE, GROUP BY, JOIN, ORDER BY,
4523+
* DISTINCT, fields (SELECT), and LIMITS clauses.
4524+
* @return array The unmodified clauses.
4525+
*/
4526+
function _filter_query_attachment_filenames( $clauses ) {
4527+
_deprecated_function( __FUNCTION__, '4.9.9', 'add_filter( "wp_allow_query_attachment_by_filename", "__return_true" )' );
4528+
remove_filter( 'posts_clauses', __FUNCTION__ );
4529+
return $clauses;
4530+
}

src/wp-includes/post.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7932,36 +7932,6 @@ function wp_add_trashed_suffix_to_post_name_for_post( $post ) {
79327932
return $post_name;
79337933
}
79347934

7935-
/**
7936-
* Filters the SQL clauses of an attachment query to include filenames.
7937-
*
7938-
* @since 4.7.0
7939-
* @access private
7940-
*
7941-
* @global wpdb $wpdb WordPress database abstraction object.
7942-
*
7943-
* @param string[] $clauses An array including WHERE, GROUP BY, JOIN, ORDER BY,
7944-
* DISTINCT, fields (SELECT), and LIMITS clauses.
7945-
* @return string[] The modified array of clauses.
7946-
*/
7947-
function _filter_query_attachment_filenames( $clauses ) {
7948-
global $wpdb;
7949-
remove_filter( 'posts_clauses', __FUNCTION__ );
7950-
7951-
// Add a LEFT JOIN of the postmeta table so we don't trample existing JOINs.
7952-
$clauses['join'] .= " LEFT JOIN {$wpdb->postmeta} AS sq1 ON ( {$wpdb->posts}.ID = sq1.post_id AND sq1.meta_key = '_wp_attached_file' )";
7953-
7954-
$clauses['groupby'] = "{$wpdb->posts}.ID";
7955-
7956-
$clauses['where'] = preg_replace(
7957-
"/\({$wpdb->posts}.post_content (NOT LIKE|LIKE) (\'[^']+\')\)/",
7958-
'$0 OR ( sq1.meta_value $1 $2 )',
7959-
$clauses['where']
7960-
);
7961-
7962-
return $clauses;
7963-
}
7964-
79657935
/**
79667936
* Sets the last changed time for the 'posts' cache group.
79677937
*

src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ protected function prepare_items_query( $prepared_args = array(), $request = nul
9797

9898
// Filter query clauses to include filenames.
9999
if ( isset( $query_args['s'] ) ) {
100-
add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
100+
add_filter( 'wp_allow_query_attachment_by_filename', '__return_true' );
101101
}
102102

103103
return $query_args;

tests/phpunit/tests/query/search.php

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ public function test_include_file_names_in_attachment_search_as_string() {
454454
);
455455

456456
add_post_meta( $attachment, '_wp_attached_file', 'some-image1.png', true );
457-
add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
457+
add_filter( 'wp_allow_query_attachment_by_filename', '__return_true' );
458458

459459
// Pass post_type a string value.
460460
$q = new WP_Query(
@@ -484,7 +484,7 @@ public function test_include_file_names_in_attachment_search_as_array() {
484484
);
485485

486486
add_post_meta( $attachment, '_wp_attached_file', 'some-image2.png', true );
487-
add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
487+
add_filter( 'wp_allow_query_attachment_by_filename', '__return_true' );
488488

489489
// Pass post_type an array value.
490490
$q = new WP_Query(
@@ -543,7 +543,7 @@ public function test_include_file_names_in_attachment_search_with_meta_query() {
543543

544544
add_post_meta( $attachment, '_wp_attached_file', 'some-image4.png', true );
545545
add_post_meta( $attachment, '_test_meta_key', 'value', true );
546-
add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
546+
add_filter( 'wp_allow_query_attachment_by_filename', '__return_true' );
547547

548548
// Pass post_type a string value.
549549
$q = new WP_Query(
@@ -583,7 +583,7 @@ public function test_include_file_names_in_attachment_search_with_tax_query() {
583583
wp_set_post_terms( $attachment, 'test', 'post_tag' );
584584

585585
add_post_meta( $attachment, '_wp_attached_file', 'some-image5.png', true );
586-
add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
586+
add_filter( 'wp_allow_query_attachment_by_filename', '__return_true' );
587587

588588
// Pass post_type a string value.
589589
$q = new WP_Query(
@@ -608,25 +608,37 @@ public function test_include_file_names_in_attachment_search_with_tax_query() {
608608
/**
609609
* @ticket 22744
610610
*/
611-
public function test_filter_query_attachment_filenames_unhooks_itself() {
612-
add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
611+
public function test_wp_query_removes_filter_wp_allow_query_attachment_by_filename() {
612+
$attachment = self::factory()->post->create(
613+
array(
614+
'post_type' => 'attachment',
615+
'post_status' => 'publish',
616+
'post_title' => 'bar foo',
617+
'post_content' => 'foo bar',
618+
'post_excerpt' => 'This post has foo',
619+
)
620+
);
613621

614-
apply_filters(
615-
'posts_clauses',
622+
add_post_meta( $attachment, '_wp_attached_file', 'some-image1.png', true );
623+
add_filter( 'wp_allow_query_attachment_by_filename', '__return_true' );
624+
625+
$q = new WP_Query(
616626
array(
617-
'where' => '',
618-
'groupby' => '',
619-
'join' => '',
620-
'orderby' => '',
621-
'distinct' => '',
622-
'fields' => '',
623-
'limit' => '',
627+
's' => 'image1',
628+
'fields' => 'ids',
629+
'post_type' => 'attachment',
630+
'post_status' => 'inherit',
624631
)
625632
);
626633

627-
$result = has_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
634+
$this->assertSame( array( $attachment ), $q->posts );
628635

629-
$this->assertFalse( $result );
636+
/*
637+
* WP_Query should have removed the wp_allow_query_attachment_by_filename filter
638+
* and thus not match the attachment created above.
639+
*/
640+
$q->get_posts();
641+
$this->assertEmpty( $q->posts );
630642
}
631643

632644
public function filter_posts_search( $sql ) {

0 commit comments

Comments
 (0)