From 8f36a570cbebf537b6c58f766986caa9bbdba48a Mon Sep 17 00:00:00 2001 From: Joe McGill Date: Tue, 20 Sep 2016 01:45:31 +0000 Subject: [PATCH] Media: Make media library searchable by filename. This applies a new private function, `_filter_query_attachment_filenames()`, to the `post_clauses` filter hook during `wp_ajax_query_attachments()` and `wp_edit_attachments_query_vars()` to include `_wp_attached_file` post meta in search queries performed from the media library or in a `WP_Media_List_Table`. Props wonderboymusic, DrewAPicture, joemcgill, swissspidy. Fixes #22744. Built from https://develop.svn.wordpress.org/trunk@38625 git-svn-id: http://core.svn.wordpress.org/trunk@38568 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/ajax-actions.php | 3 +++ wp-admin/includes/post.php | 28 ++++++++++++++++++++++++++++ wp-includes/version.php | 2 +- 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/wp-admin/includes/ajax-actions.php b/wp-admin/includes/ajax-actions.php index b6bd7576ea..561c37b437 100644 --- a/wp-admin/includes/ajax-actions.php +++ b/wp-admin/includes/ajax-actions.php @@ -2397,6 +2397,9 @@ function wp_ajax_query_attachments() { if ( current_user_can( get_post_type_object( 'attachment' )->cap->read_private_posts ) ) $query['post_status'] .= ',private'; + // Filter query clauses to include filenames. + add_filter( 'posts_clauses', '_filter_query_attachment_filenames' ); + /** * Filters the arguments passed to WP_Query during an Ajax * call for querying attachments. diff --git a/wp-admin/includes/post.php b/wp-admin/includes/post.php index dfcb3ec440..b12aa5d070 100644 --- a/wp-admin/includes/post.php +++ b/wp-admin/includes/post.php @@ -1144,9 +1144,37 @@ function wp_edit_attachments_query_vars( $q = false ) { $q['post_parent'] = 0; } + // Filter query clauses to include filenames. + add_filter( 'posts_clauses', '_filter_query_attachment_filenames' ); + return $q; } +/** + * Filter the SQL clauses of an attachment query to include filenames. + * + * @since 4.7.0 + * @access private + * + * @param array $clauses An array including WHERE, GROUP BY, JOIN, ORDER BY, + * DISTINCT, fields (SELECT), and LIMITS clauses. + * @return array The modified clauses. + */ +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 )"; + $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 )", + $clauses['where'] ); + + return $clauses; +} + /** * Executes a query for attachments. An array of WP_Query arguments * can be passed in, which will override the arguments set by this function. diff --git a/wp-includes/version.php b/wp-includes/version.php index 9f5de3adf7..ba7907f8dd 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.7-alpha-38624'; +$wp_version = '4.7-alpha-38625'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.