REST API: Add support for filename search in media endpoint.

In [38625], the functionality to search for attachments by filename was added via the `posts_clauses` filter and the `_filter_query_attachment_filenames()` function. This moves `_filter_query_attachment_filenames()` from `wp-admin/includes/post.php` to `wp-includes/post.php` so that it can be applied in the same manner in the REST API media endpoint.

Merge of [39598] to the 4.7 branch.

Props jblz, tyxla.
Fixes #39092.


Built from https://develop.svn.wordpress.org/branches/4.7@39629


git-svn-id: http://core.svn.wordpress.org/branches/4.7@39569 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Gary Pendergast 2016-12-21 05:19:34 +00:00
parent e6ce714219
commit bba21b983c
4 changed files with 35 additions and 30 deletions

View File

@ -1154,35 +1154,6 @@ function wp_edit_attachments_query_vars( $q = false ) {
return $q; return $q;
} }
/**
* Filter the SQL clauses of an attachment query to include filenames.
*
* @since 4.7.0
* @access private
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @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__ );
// 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;
}
/** /**
* Executes a query for attachments. An array of WP_Query arguments * Executes a query for attachments. An array of WP_Query arguments
* can be passed in, which will override the arguments set by this function. * can be passed in, which will override the arguments set by this function.

View File

@ -6188,3 +6188,32 @@ function wp_add_trashed_suffix_to_post_name_for_post( $post ) {
clean_post_cache( $post->ID ); clean_post_cache( $post->ID );
return $post_name; return $post_name;
} }
/**
* Filter the SQL clauses of an attachment query to include filenames.
*
* @since 4.7.0
* @access private
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @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__ );
// 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;
}

View File

@ -47,6 +47,11 @@ 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' );
}
return $query_args; return $query_args;
} }

View File

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