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.

Props jblz, tyxla.
Fixes #39092.


git-svn-id: https://develop.svn.wordpress.org/trunk@39598 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
James Nylen
2016-12-13 14:08:24 +00:00
parent 03c947c7fd
commit 2642833293
4 changed files with 54 additions and 29 deletions

View File

@@ -1156,6 +1156,26 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
$wp_rest_additional_fields = array();
}
public function test_search_item_by_filename() {
$id = $this->factory->attachment->create_object( $this->test_file, 0, array(
'post_mime_type' => 'image/jpeg',
) );
$id2 = $this->factory->attachment->create_object( $this->test_file2, 0, array(
'post_mime_type' => 'image/png',
) );
$filename = basename( $this->test_file2 );
$request = new WP_REST_Request( 'GET', '/wp/v2/media' );
$request->set_param( 'search', $filename );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertCount( 1, $data );
$this->assertEquals( $id2, $data[0]['id'] );
$this->assertEquals( 'image/png', $data[0]['mime_type'] );
}
public function additional_field_get_callback( $object, $request ) {
return 123;
}