Media: Refactor search by filename within the admin.

Props vortfu, xknown, peterwilsoncc, paulkevan.

Built from https://develop.svn.wordpress.org/trunk@54524


git-svn-id: http://core.svn.wordpress.org/trunk@54079 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
audrasjb 2022-10-17 11:19:11 +00:00
parent 45a6b73042
commit b7f48d4cb4
7 changed files with 51 additions and 37 deletions

View File

@ -3023,7 +3023,7 @@ function wp_ajax_query_attachments() {
// Filter query clauses to include filenames.
if ( isset( $query['s'] ) ) {
add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
add_filter( 'wp_allow_query_attachment_by_filename', '__return_true' );
}
/**

View File

@ -1308,7 +1308,7 @@ function wp_edit_attachments_query_vars( $q = false ) {
// Filter query clauses to include filenames.
if ( isset( $q['s'] ) ) {
add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
add_filter( 'wp_allow_query_attachment_by_filename', '__return_true' );
}
return $q;

View File

@ -445,6 +445,14 @@ class WP_Query {
*/
public $thumbnails_cached = false;
/**
* Controls whether an attachment query should include filenames or not.
*
* @since 6.0.3
* @var bool
*/
protected $allow_query_attachment_by_filename = false;
/**
* Cached list of search stopwords.
*
@ -1429,8 +1437,13 @@ class WP_Query {
$q['search_orderby_title'][] = $wpdb->prepare( "{$wpdb->posts}.post_title LIKE %s", $like );
}
$like = $n . $wpdb->esc_like( $term ) . $n;
$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 );
$like = $n . $wpdb->esc_like( $term ) . $n;
if ( ! empty( $this->allow_query_attachment_by_filename ) ) {
$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 );
} else {
$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 );
}
$searchand = ' AND ';
}
@ -1825,6 +1838,16 @@ class WP_Query {
// Fill again in case 'pre_get_posts' unset some vars.
$q = $this->fill_query_vars( $q );
/**
* Filters whether an attachment query should include filenames or not.
*
* @since 6.0.3
*
* @param bool $allow_query_attachment_by_filename Whether or not to include filenames.
*/
$this->allow_query_attachment_by_filename = apply_filters( 'wp_allow_query_attachment_by_filename', false );
remove_all_filters( 'wp_allow_query_attachment_by_filename' );
// Parse meta query.
$this->meta_query = new WP_Meta_Query();
$this->meta_query->parse_query_vars( $q );
@ -2256,7 +2279,7 @@ class WP_Query {
}
}
if ( ! empty( $this->tax_query->queries ) || ! empty( $this->meta_query->queries ) ) {
if ( ! empty( $this->tax_query->queries ) || ! empty( $this->meta_query->queries ) || ! empty( $this->allow_query_attachment_by_filename ) ) {
$groupby = "{$wpdb->posts}.ID";
}
@ -2333,6 +2356,10 @@ class WP_Query {
}
$where .= $search . $whichauthor . $whichmimetype;
if ( ! empty( $this->allow_query_attachment_by_filename ) ) {
$join .= " LEFT JOIN {$wpdb->postmeta} AS sq1 ON ( {$wpdb->posts}.ID = sq1.post_id AND sq1.meta_key = '_wp_attached_file' )";
}
if ( ! empty( $this->meta_query->queries ) ) {
$clauses = $this->meta_query->get_sql( 'post', $wpdb->posts, 'ID', $this );
$join .= $clauses['join'];

View File

@ -4511,3 +4511,20 @@ function global_terms_enabled() {
return false;
}
/**
* Filter the SQL clauses of an attachment query to include filenames.
*
* @since 4.7.0
* @deprecated 6.0.3
* @access private
*
* @param array $clauses An array including WHERE, GROUP BY, JOIN, ORDER BY,
* DISTINCT, fields (SELECT), and LIMITS clauses.
* @return array The unmodified clauses.
*/
function _filter_query_attachment_filenames( $clauses ) {
_deprecated_function( __FUNCTION__, '4.9.9', 'add_filter( "wp_allow_query_attachment_by_filename", "__return_true" )' );
remove_filter( 'posts_clauses', __FUNCTION__ );
return $clauses;
}

View File

@ -7932,36 +7932,6 @@ function wp_add_trashed_suffix_to_post_name_for_post( $post ) {
return $post_name;
}
/**
* Filters the SQL clauses of an attachment query to include filenames.
*
* @since 4.7.0
* @access private
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param string[] $clauses An array including WHERE, GROUP BY, JOIN, ORDER BY,
* DISTINCT, fields (SELECT), and LIMITS clauses.
* @return string[] The modified array of clauses.
*/
function _filter_query_attachment_filenames( $clauses ) {
global $wpdb;
remove_filter( 'posts_clauses', __FUNCTION__ );
// 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 ( sq1.meta_value $1 $2 )',
$clauses['where']
);
return $clauses;
}
/**
* Sets the last changed time for the 'posts' cache group.
*

View File

@ -97,7 +97,7 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller {
// Filter query clauses to include filenames.
if ( isset( $query_args['s'] ) ) {
add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
add_filter( 'wp_allow_query_attachment_by_filename', '__return_true' );
}
return $query_args;

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.1-RC1-54523';
$wp_version = '6.1-RC1-54524';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.