Media: Better handling of JOINs when searching filenames.

Following [38625], any media searches that already included JOINs,
e.g., `tax_queries`, would get trampled when we joined the post meta
table to search for filenames. This preserves existing JOINs and
also only applies the `_filter_query_attachment_filenames()` filter
when a search query is being performed.

Props flixos90, joemcgill.
Fixes #22744.
Built from https://develop.svn.wordpress.org/trunk@38733


git-svn-id: http://core.svn.wordpress.org/trunk@38676 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Joe McGill 2016-10-05 19:51:30 +00:00
parent 0383319aef
commit 173f9b6f22
3 changed files with 11 additions and 5 deletions

View File

@ -2401,7 +2401,9 @@ function wp_ajax_query_attachments() {
$query['post_status'] .= ',private';
// Filter query clauses to include filenames.
add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
if ( isset( $query['s'] ) ) {
add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
}
/**
* Filters the arguments passed to WP_Query during an Ajax

View File

@ -1145,7 +1145,9 @@ function wp_edit_attachments_query_vars( $q = false ) {
}
// Filter query clauses to include filenames.
add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
if ( isset( $q['s'] ) ) {
add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
}
return $q;
}
@ -1164,12 +1166,14 @@ function _filter_query_attachment_filenames( $clauses ) {
global $wpdb;
remove_filter( 'posts_clauses', __FUNCTION__ );
$clauses['join'] = " INNER JOIN {$wpdb->postmeta} ON ( {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id )";
// Add a LEFT JOIN of the postmeta table so we don't trample existing JOINs.
$clauses['join'] .= " LEFT JOIN {$wpdb->postmeta} AS sq1 ON ( {$wpdb->posts}.ID = sq1.post_id AND sq1.meta_key = '_wp_attached_file' )";
$clauses['groupby'] = "{$wpdb->posts}.ID";
$clauses['where'] = preg_replace(
"/\({$wpdb->posts}.post_content (NOT LIKE|LIKE) (\'[^']+\')\)/",
"$0 OR ( {$wpdb->postmeta}.meta_key = '_wp_attached_file' AND {$wpdb->postmeta}.meta_value $1 $2 )",
"$0 OR ( sq1.meta_value $1 $2 )",
$clauses['where'] );
return $clauses;

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.7-alpha-38732';
$wp_version = '4.7-alpha-38733';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.