Ticket #22744: 22744.9.diff
File 22744.9.diff, 4.5 KB (added by , 8 years ago) |
---|
-
src/wp-admin/includes/ajax-actions.php
diff --git src/wp-admin/includes/ajax-actions.php src/wp-admin/includes/ajax-actions.php index b6bd757..561c37b 100644
function wp_ajax_query_attachments() { 2397 2397 if ( current_user_can( get_post_type_object( 'attachment' )->cap->read_private_posts ) ) 2398 2398 $query['post_status'] .= ',private'; 2399 2399 2400 // Filter query clauses to include filenames. 2401 add_filter( 'posts_clauses', '_filter_query_attachment_filenames' ); 2402 2400 2403 /** 2401 2404 * Filters the arguments passed to WP_Query during an Ajax 2402 2405 * call for querying attachments. -
src/wp-admin/includes/post.php
diff --git src/wp-admin/includes/post.php src/wp-admin/includes/post.php index cde99d8..68c51df 100644
function wp_edit_attachments_query_vars( $q = false ) { 1144 1144 $q['post_parent'] = 0; 1145 1145 } 1146 1146 1147 // Filter query clauses to include filenames. 1148 add_filter( 'posts_clauses', '_filter_query_attachment_filenames' ); 1149 1147 1150 return $q; 1148 1151 } 1149 1152 1150 1153 /** 1154 * Filter the SQL clauses of an attachment query to include file names. 1155 * 1156 * @since 4.7 1157 * @access private 1158 * 1159 * @param array $clauses An array including WHERE, GROUP BY, JOIN, ORDER BY, 1160 * DISTINCT, fields (SELECT), and LIMITS clauses. 1161 * @return array The modified clauses. 1162 */ 1163 function _filter_query_attachment_filenames( $clauses ) { 1164 global $wpdb; 1165 remove_filter( 'posts_clauses', __FUNCTION__ ); 1166 1167 $clauses[ 'join' ] = " INNER JOIN {$wpdb->postmeta} ON ( {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id )"; 1168 $clauses[ 'groupby' ] = "{$wpdb->posts}.ID"; 1169 1170 $clauses[ 'where' ] = preg_replace( 1171 "/\({$wpdb->posts}.post_content (NOT LIKE|LIKE) (\'[^']+\')\)/", 1172 "$0 OR ( {$wpdb->postmeta}.meta_key = '_wp_attached_file' AND {$wpdb->postmeta}.meta_value $1 $2 )", 1173 $clauses[ 'where' ] ); 1174 1175 return $clauses; 1176 } 1177 1178 /** 1151 1179 * Executes a query for attachments. An array of WP_Query arguments 1152 1180 * can be passed in, which will override the arguments set by this function. 1153 1181 * -
tests/phpunit/tests/query/search.php
diff --git tests/phpunit/tests/query/search.php tests/phpunit/tests/query/search.php index a228f91..31392d4 100644
class Tests_Query_Search extends WP_UnitTestCase { 280 280 $this->assertSame( array( $p1, $p3, $p2 ), $q->posts ); 281 281 } 282 282 283 /** 284 * @ticket 22744 285 */ 286 public function test_s_should_include_file_names_in_attachment_search_as_string() { 287 $attachment = self::factory()->post->create( array( 288 'post_type' => 'attachment', 289 'post_status' => 'publish', 290 'post_title' => 'bar foo', 291 'post_content' => 'foo bar', 292 'post_excerpt' => 'This post has foo', 293 ) ); 294 295 add_post_meta( $attachment, '_wp_attached_file', 'some-image1.png', true ); 296 297 // Pass post_type a string value. 298 $q = new WP_Query( array( 299 's' => 'image1', 300 'fields' => 'ids', 301 'post_type' => 'attachment', 302 'post_status' => 'inherit', 303 ) ); 304 305 $this->assertSame( array( $attachment ), $q->posts ); 306 } 307 308 /** 309 * @ticket 22744 310 */ 311 public function test_s_should_include_file_names_in_attachment_search_as_array() { 312 $attachment = self::factory()->post->create( array( 313 'post_type' => 'attachment', 314 'post_status' => 'publish', 315 'post_title' => 'bar foo', 316 'post_content' => 'foo bar', 317 'post_excerpt' => 'This post has foo', 318 ) ); 319 320 add_post_meta( $attachment, '_wp_attached_file', 'some-image2.png', true ); 321 322 // Pass post_type an array value. 323 $q = new WP_Query( array( 324 's' => 'image2', 325 'fields' => 'ids', 326 'post_type' => array( 'attachment' ), 327 'post_status' => 'inherit', 328 ) ); 329 330 $this->assertSame( array( $attachment ), $q->posts ); 331 } 332 333 /** 334 * @ticket 22744 335 */ 336 public function test_s_should_exclude_attachment_file_names_in_general_searches() { 337 $attachment = self::factory()->post->create( array( 338 'post_type' => 'attachment', 339 'post_status' => 'publish', 340 'post_title' => 'bar foo', 341 'post_content' => 'foo bar', 342 'post_excerpt' => 'This post has foo', 343 ) ); 344 345 add_post_meta( $attachment, '_wp_attached_file', 'some-image3.png', true ); 346 347 $q = new WP_Query( array( 348 's' => 'image3', 349 'fields' => 'ids', 350 'post_type' => array( 'post', 'page', 'attachment' ), 351 'post_status' => 'inherit', 352 ) ); 353 354 $this->assertNotEquals( array( $attachment ), $q->posts ); 355 } 356 283 357 public function filter_posts_search( $sql ) { 284 358 return $sql . ' /* posts_search */'; 285 359 }