diff --git a/src/wp-admin/includes/post.php b/src/wp-admin/includes/post.php
index ab50c27..eca583a 100644
|
a
|
b
|
function wp_edit_attachments_query_vars( $q = false ) {
|
| 1155 | 1155 | } |
| 1156 | 1156 | |
| 1157 | 1157 | /** |
| 1158 | | * Filter the SQL clauses of an attachment query to include filenames. |
| 1159 | | * |
| 1160 | | * @since 4.7.0 |
| 1161 | | * @access private |
| 1162 | | * |
| 1163 | | * @global wpdb $wpdb WordPress database abstraction object. |
| 1164 | | * |
| 1165 | | * @param array $clauses An array including WHERE, GROUP BY, JOIN, ORDER BY, |
| 1166 | | * DISTINCT, fields (SELECT), and LIMITS clauses. |
| 1167 | | * @return array The modified clauses. |
| 1168 | | */ |
| 1169 | | function _filter_query_attachment_filenames( $clauses ) { |
| 1170 | | global $wpdb; |
| 1171 | | remove_filter( 'posts_clauses', __FUNCTION__ ); |
| 1172 | | |
| 1173 | | // Add a LEFT JOIN of the postmeta table so we don't trample existing JOINs. |
| 1174 | | $clauses['join'] .= " LEFT JOIN {$wpdb->postmeta} AS sq1 ON ( {$wpdb->posts}.ID = sq1.post_id AND sq1.meta_key = '_wp_attached_file' )"; |
| 1175 | | |
| 1176 | | $clauses['groupby'] = "{$wpdb->posts}.ID"; |
| 1177 | | |
| 1178 | | $clauses['where'] = preg_replace( |
| 1179 | | "/\({$wpdb->posts}.post_content (NOT LIKE|LIKE) (\'[^']+\')\)/", |
| 1180 | | "$0 OR ( sq1.meta_value $1 $2 )", |
| 1181 | | $clauses['where'] ); |
| 1182 | | |
| 1183 | | return $clauses; |
| 1184 | | } |
| 1185 | | |
| 1186 | | /** |
| 1187 | 1158 | * Executes a query for attachments. An array of WP_Query arguments |
| 1188 | 1159 | * can be passed in, which will override the arguments set by this function. |
| 1189 | 1160 | * |
diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php
index c81f5c5..d9c64f5 100644
|
a
|
b
|
function wp_add_trashed_suffix_to_post_name_for_post( $post ) {
|
| 6188 | 6188 | clean_post_cache( $post->ID ); |
| 6189 | 6189 | return $post_name; |
| 6190 | 6190 | } |
| | 6191 | |
| | 6192 | /** |
| | 6193 | * Filter the SQL clauses of an attachment query to include filenames. |
| | 6194 | * |
| | 6195 | * @since 4.7.0 |
| | 6196 | * @access private |
| | 6197 | * |
| | 6198 | * @global wpdb $wpdb WordPress database abstraction object. |
| | 6199 | * |
| | 6200 | * @param array $clauses An array including WHERE, GROUP BY, JOIN, ORDER BY, |
| | 6201 | * DISTINCT, fields (SELECT), and LIMITS clauses. |
| | 6202 | * @return array The modified clauses. |
| | 6203 | */ |
| | 6204 | function _filter_query_attachment_filenames( $clauses ) { |
| | 6205 | global $wpdb; |
| | 6206 | remove_filter( 'posts_clauses', __FUNCTION__ ); |
| | 6207 | |
| | 6208 | // Add a LEFT JOIN of the postmeta table so we don't trample existing JOINs. |
| | 6209 | $clauses['join'] .= " LEFT JOIN {$wpdb->postmeta} AS sq1 ON ( {$wpdb->posts}.ID = sq1.post_id AND sq1.meta_key = '_wp_attached_file' )"; |
| | 6210 | |
| | 6211 | $clauses['groupby'] = "{$wpdb->posts}.ID"; |
| | 6212 | |
| | 6213 | $clauses['where'] = preg_replace( |
| | 6214 | "/\({$wpdb->posts}.post_content (NOT LIKE|LIKE) (\'[^']+\')\)/", |
| | 6215 | "$0 OR ( sq1.meta_value $1 $2 )", |
| | 6216 | $clauses['where'] ); |
| | 6217 | |
| | 6218 | return $clauses; |
| | 6219 | } |
diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php
index f0055ee..8f0318a 100644
|
a
|
b
|
class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller {
|
| 47 | 47 | } |
| 48 | 48 | } |
| 49 | 49 | |
| | 50 | // Filter query clauses to include filenames. |
| | 51 | if ( isset( $query_args['s'] ) ) { |
| | 52 | add_filter( 'posts_clauses', '_filter_query_attachment_filenames' ); |
| | 53 | } |
| | 54 | |
| 50 | 55 | return $query_args; |
| 51 | 56 | } |
| 52 | 57 | |
diff --git a/tests/phpunit/tests/rest-api/rest-attachments-controller.php b/tests/phpunit/tests/rest-api/rest-attachments-controller.php
index 0de092d..502f3a3 100644
|
a
|
b
|
class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
| 1143 | 1143 | $wp_rest_additional_fields = array(); |
| 1144 | 1144 | } |
| 1145 | 1145 | |
| | 1146 | public function test_search_item_by_filename() { |
| | 1147 | $id = $this->factory->attachment->create_object( $this->test_file, 0, array( |
| | 1148 | 'post_mime_type' => 'image/jpeg', |
| | 1149 | ) ); |
| | 1150 | $id2 = $this->factory->attachment->create_object( $this->test_file2, 0, array( |
| | 1151 | 'post_mime_type' => 'image/png', |
| | 1152 | ) ); |
| | 1153 | |
| | 1154 | $filename = basename( $this->test_file2 ); |
| | 1155 | |
| | 1156 | $request = new WP_REST_Request( 'GET', '/wp/v2/media' ); |
| | 1157 | $request->set_param( 'search', $filename ); |
| | 1158 | $response = $this->server->dispatch( $request ); |
| | 1159 | $data = $response->get_data(); |
| | 1160 | |
| | 1161 | $this->assertCount( 1, $data ); |
| | 1162 | $this->assertEquals( $id2, $data[0]['id'] ); |
| | 1163 | $this->assertEquals( 'image/png', $data[0]['mime_type'] ); |
| | 1164 | } |
| | 1165 | |
| 1146 | 1166 | public function additional_field_get_callback( $object, $request ) { |
| 1147 | 1167 | return 123; |
| 1148 | 1168 | } |