Changeset 54524
- Timestamp:
- 10/17/2022 11:17:38 AM (2 years ago)
- Location:
- trunk
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-admin/includes/ajax-actions.php
r54319 r54524 3024 3024 // Filter query clauses to include filenames. 3025 3025 if ( isset( $query['s'] ) ) { 3026 add_filter( ' posts_clauses', '_filter_query_attachment_filenames' );3026 add_filter( 'wp_allow_query_attachment_by_filename', '__return_true' ); 3027 3027 } 3028 3028 -
trunk/src/wp-admin/includes/post.php
r54244 r54524 1309 1309 // Filter query clauses to include filenames. 1310 1310 if ( isset( $q['s'] ) ) { 1311 add_filter( ' posts_clauses', '_filter_query_attachment_filenames' );1311 add_filter( 'wp_allow_query_attachment_by_filename', '__return_true' ); 1312 1312 } 1313 1313 -
trunk/src/wp-includes/class-wp-query.php
r54496 r54524 445 445 */ 446 446 public $thumbnails_cached = false; 447 448 /** 449 * Controls whether an attachment query should include filenames or not. 450 * 451 * @since 6.0.3 452 * @var bool 453 */ 454 protected $allow_query_attachment_by_filename = false; 447 455 448 456 /** … … 1430 1438 } 1431 1439 1432 $like = $n . $wpdb->esc_like( $term ) . $n; 1433 $search .= $wpdb->prepare( "{$searchand}(({$wpdb->posts}.post_title $like_op %s) $andor_op ({$wpdb->posts}.post_excerpt $like_op %s) $andor_op ({$wpdb->posts}.post_content $like_op %s))", $like, $like, $like ); 1440 $like = $n . $wpdb->esc_like( $term ) . $n; 1441 1442 if ( ! empty( $this->allow_query_attachment_by_filename ) ) { 1443 $search .= $wpdb->prepare( "{$searchand}(({$wpdb->posts}.post_title $like_op %s) $andor_op ({$wpdb->posts}.post_excerpt $like_op %s) $andor_op ({$wpdb->posts}.post_content $like_op %s) $andor_op (sq1.meta_value $like_op %s))", $like, $like, $like, $like ); 1444 } else { 1445 $search .= $wpdb->prepare( "{$searchand}(({$wpdb->posts}.post_title $like_op %s) $andor_op ({$wpdb->posts}.post_excerpt $like_op %s) $andor_op ({$wpdb->posts}.post_content $like_op %s))", $like, $like, $like ); 1446 } 1434 1447 $searchand = ' AND '; 1435 1448 } … … 1826 1839 $q = $this->fill_query_vars( $q ); 1827 1840 1841 /** 1842 * Filters whether an attachment query should include filenames or not. 1843 * 1844 * @since 6.0.3 1845 * 1846 * @param bool $allow_query_attachment_by_filename Whether or not to include filenames. 1847 */ 1848 $this->allow_query_attachment_by_filename = apply_filters( 'wp_allow_query_attachment_by_filename', false ); 1849 remove_all_filters( 'wp_allow_query_attachment_by_filename' ); 1850 1828 1851 // Parse meta query. 1829 1852 $this->meta_query = new WP_Meta_Query(); … … 2257 2280 } 2258 2281 2259 if ( ! empty( $this->tax_query->queries ) || ! empty( $this->meta_query->queries ) ) {2282 if ( ! empty( $this->tax_query->queries ) || ! empty( $this->meta_query->queries ) || ! empty( $this->allow_query_attachment_by_filename ) ) { 2260 2283 $groupby = "{$wpdb->posts}.ID"; 2261 2284 } … … 2333 2356 } 2334 2357 $where .= $search . $whichauthor . $whichmimetype; 2358 2359 if ( ! empty( $this->allow_query_attachment_by_filename ) ) { 2360 $join .= " LEFT JOIN {$wpdb->postmeta} AS sq1 ON ( {$wpdb->posts}.ID = sq1.post_id AND sq1.meta_key = '_wp_attached_file' )"; 2361 } 2335 2362 2336 2363 if ( ! empty( $this->meta_query->queries ) ) { -
trunk/src/wp-includes/deprecated.php
r54480 r54524 4512 4512 return false; 4513 4513 } 4514 4515 /** 4516 * Filter the SQL clauses of an attachment query to include filenames. 4517 * 4518 * @since 4.7.0 4519 * @deprecated 6.0.3 4520 * @access private 4521 * 4522 * @param array $clauses An array including WHERE, GROUP BY, JOIN, ORDER BY, 4523 * DISTINCT, fields (SELECT), and LIMITS clauses. 4524 * @return array The unmodified clauses. 4525 */ 4526 function _filter_query_attachment_filenames( $clauses ) { 4527 _deprecated_function( __FUNCTION__, '4.9.9', 'add_filter( "wp_allow_query_attachment_by_filename", "__return_true" )' ); 4528 remove_filter( 'posts_clauses', __FUNCTION__ ); 4529 return $clauses; 4530 } -
trunk/src/wp-includes/post.php
r54377 r54524 7934 7934 7935 7935 /** 7936 * Filters the SQL clauses of an attachment query to include filenames.7937 *7938 * @since 4.7.07939 * @access private7940 *7941 * @global wpdb $wpdb WordPress database abstraction object.7942 *7943 * @param string[] $clauses An array including WHERE, GROUP BY, JOIN, ORDER BY,7944 * DISTINCT, fields (SELECT), and LIMITS clauses.7945 * @return string[] The modified array of clauses.7946 */7947 function _filter_query_attachment_filenames( $clauses ) {7948 global $wpdb;7949 remove_filter( 'posts_clauses', __FUNCTION__ );7950 7951 // Add a LEFT JOIN of the postmeta table so we don't trample existing JOINs.7952 $clauses['join'] .= " LEFT JOIN {$wpdb->postmeta} AS sq1 ON ( {$wpdb->posts}.ID = sq1.post_id AND sq1.meta_key = '_wp_attached_file' )";7953 7954 $clauses['groupby'] = "{$wpdb->posts}.ID";7955 7956 $clauses['where'] = preg_replace(7957 "/\({$wpdb->posts}.post_content (NOT LIKE|LIKE) (\'[^']+\')\)/",7958 '$0 OR ( sq1.meta_value $1 $2 )',7959 $clauses['where']7960 );7961 7962 return $clauses;7963 }7964 7965 /**7966 7936 * Sets the last changed time for the 'posts' cache group. 7967 7937 * -
trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php
r54085 r54524 98 98 // Filter query clauses to include filenames. 99 99 if ( isset( $query_args['s'] ) ) { 100 add_filter( ' posts_clauses', '_filter_query_attachment_filenames' );100 add_filter( 'wp_allow_query_attachment_by_filename', '__return_true' ); 101 101 } 102 102 -
trunk/tests/phpunit/tests/query/search.php
r54090 r54524 455 455 456 456 add_post_meta( $attachment, '_wp_attached_file', 'some-image1.png', true ); 457 add_filter( ' posts_clauses', '_filter_query_attachment_filenames' );457 add_filter( 'wp_allow_query_attachment_by_filename', '__return_true' ); 458 458 459 459 // Pass post_type a string value. … … 485 485 486 486 add_post_meta( $attachment, '_wp_attached_file', 'some-image2.png', true ); 487 add_filter( ' posts_clauses', '_filter_query_attachment_filenames' );487 add_filter( 'wp_allow_query_attachment_by_filename', '__return_true' ); 488 488 489 489 // Pass post_type an array value. … … 544 544 add_post_meta( $attachment, '_wp_attached_file', 'some-image4.png', true ); 545 545 add_post_meta( $attachment, '_test_meta_key', 'value', true ); 546 add_filter( ' posts_clauses', '_filter_query_attachment_filenames' );546 add_filter( 'wp_allow_query_attachment_by_filename', '__return_true' ); 547 547 548 548 // Pass post_type a string value. … … 584 584 585 585 add_post_meta( $attachment, '_wp_attached_file', 'some-image5.png', true ); 586 add_filter( ' posts_clauses', '_filter_query_attachment_filenames' );586 add_filter( 'wp_allow_query_attachment_by_filename', '__return_true' ); 587 587 588 588 // Pass post_type a string value. … … 609 609 * @ticket 22744 610 610 */ 611 public function test_filter_query_attachment_filenames_unhooks_itself() { 612 add_filter( 'posts_clauses', '_filter_query_attachment_filenames' ); 613 614 apply_filters( 615 'posts_clauses', 616 array( 617 'where' => '', 618 'groupby' => '', 619 'join' => '', 620 'orderby' => '', 621 'distinct' => '', 622 'fields' => '', 623 'limit' => '', 624 ) 625 ); 626 627 $result = has_filter( 'posts_clauses', '_filter_query_attachment_filenames' ); 628 629 $this->assertFalse( $result ); 611 public function test_wp_query_removes_filter_wp_allow_query_attachment_by_filename() { 612 $attachment = self::factory()->post->create( 613 array( 614 'post_type' => 'attachment', 615 'post_status' => 'publish', 616 'post_title' => 'bar foo', 617 'post_content' => 'foo bar', 618 'post_excerpt' => 'This post has foo', 619 ) 620 ); 621 622 add_post_meta( $attachment, '_wp_attached_file', 'some-image1.png', true ); 623 add_filter( 'wp_allow_query_attachment_by_filename', '__return_true' ); 624 625 $q = new WP_Query( 626 array( 627 's' => 'image1', 628 'fields' => 'ids', 629 'post_type' => 'attachment', 630 'post_status' => 'inherit', 631 ) 632 ); 633 634 $this->assertSame( array( $attachment ), $q->posts ); 635 636 /* 637 * WP_Query should have removed the wp_allow_query_attachment_by_filename filter 638 * and thus not match the attachment created above. 639 */ 640 $q->get_posts(); 641 $this->assertEmpty( $q->posts ); 630 642 } 631 643
Note: See TracChangeset
for help on using the changeset viewer.