Changeset 54534
- Timestamp:
- 10/17/2022 12:24:45 PM (2 years ago)
- Location:
- branches/6.0
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/6.0
-
branches/6.0/src/wp-admin/includes/ajax-actions.php
r53337 r54534 2995 2995 // Filter query clauses to include filenames. 2996 2996 if ( isset( $query['s'] ) ) { 2997 add_filter( ' posts_clauses', '_filter_query_attachment_filenames' );2997 add_filter( 'wp_allow_query_attachment_by_filename', '__return_true' ); 2998 2998 } 2999 2999 -
branches/6.0/src/wp-admin/includes/post.php
r53299 r54534 1303 1303 // Filter query clauses to include filenames. 1304 1304 if ( isset( $q['s'] ) ) { 1305 add_filter( ' posts_clauses', '_filter_query_attachment_filenames' );1305 add_filter( 'wp_allow_query_attachment_by_filename', '__return_true' ); 1306 1306 } 1307 1307 -
branches/6.0/src/wp-includes/class-wp-query.php
r53379 r54534 444 444 */ 445 445 public $thumbnails_cached = false; 446 447 /** 448 * Controls whether an attachment query should include filenames or not. 449 * 450 * @since 6.0.3 451 * @var bool 452 */ 453 protected $allow_query_attachment_by_filename = false; 446 454 447 455 /** … … 1415 1423 } 1416 1424 1417 $like = $n . $wpdb->esc_like( $term ) . $n; 1418 $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 ); 1425 $like = $n . $wpdb->esc_like( $term ) . $n; 1426 1427 if ( ! empty( $this->allow_query_attachment_by_filename ) ) { 1428 $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 ); 1429 } else { 1430 $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 ); 1431 } 1419 1432 $searchand = ' AND '; 1420 1433 } … … 1811 1824 $q = $this->fill_query_vars( $q ); 1812 1825 1826 /** 1827 * Filters whether an attachment query should include filenames or not. 1828 * 1829 * @since 6.0.3 1830 * 1831 * @param bool $allow_query_attachment_by_filename Whether or not to include filenames. 1832 */ 1833 $this->allow_query_attachment_by_filename = apply_filters( 'wp_allow_query_attachment_by_filename', false ); 1834 remove_all_filters( 'wp_allow_query_attachment_by_filename' ); 1835 1813 1836 // Parse meta query. 1814 1837 $this->meta_query = new WP_Meta_Query(); … … 2242 2265 } 2243 2266 2244 if ( ! empty( $this->tax_query->queries ) || ! empty( $this->meta_query->queries ) ) {2267 if ( ! empty( $this->tax_query->queries ) || ! empty( $this->meta_query->queries ) || ! empty( $this->allow_query_attachment_by_filename ) ) { 2245 2268 $groupby = "{$wpdb->posts}.ID"; 2246 2269 } … … 2318 2341 } 2319 2342 $where .= $search . $whichauthor . $whichmimetype; 2343 2344 if ( ! empty( $this->allow_query_attachment_by_filename ) ) { 2345 $join .= " LEFT JOIN {$wpdb->postmeta} AS sq1 ON ( {$wpdb->posts}.ID = sq1.post_id AND sq1.meta_key = '_wp_attached_file' )"; 2346 } 2320 2347 2321 2348 if ( ! empty( $this->meta_query->queries ) ) { -
branches/6.0/src/wp-includes/deprecated.php
r53287 r54534 4312 4312 _deprecated_function( __FUNCTION__, '6.0.0' ); 4313 4313 } 4314 4315 /** 4316 * Filter the SQL clauses of an attachment query to include filenames. 4317 * 4318 * @since 4.7.0 4319 * @deprecated 6.0.3 4320 * @access private 4321 * 4322 * @param array $clauses An array including WHERE, GROUP BY, JOIN, ORDER BY, 4323 * DISTINCT, fields (SELECT), and LIMITS clauses. 4324 * @return array The unmodified clauses. 4325 */ 4326 function _filter_query_attachment_filenames( $clauses ) { 4327 _deprecated_function( __FUNCTION__, '4.9.9', 'add_filter( "wp_allow_query_attachment_by_filename", "__return_true" )' ); 4328 remove_filter( 'posts_clauses', __FUNCTION__ ); 4329 return $clauses; 4330 } -
branches/6.0/src/wp-includes/post.php
r53300 r54534 7920 7920 7921 7921 /** 7922 * Filters the SQL clauses of an attachment query to include filenames.7923 *7924 * @since 4.7.07925 * @access private7926 *7927 * @global wpdb $wpdb WordPress database abstraction object.7928 *7929 * @param string[] $clauses An array including WHERE, GROUP BY, JOIN, ORDER BY,7930 * DISTINCT, fields (SELECT), and LIMITS clauses.7931 * @return string[] The modified array of clauses.7932 */7933 function _filter_query_attachment_filenames( $clauses ) {7934 global $wpdb;7935 remove_filter( 'posts_clauses', __FUNCTION__ );7936 7937 // Add a LEFT JOIN of the postmeta table so we don't trample existing JOINs.7938 $clauses['join'] .= " LEFT JOIN {$wpdb->postmeta} AS sq1 ON ( {$wpdb->posts}.ID = sq1.post_id AND sq1.meta_key = '_wp_attached_file' )";7939 7940 $clauses['groupby'] = "{$wpdb->posts}.ID";7941 7942 $clauses['where'] = preg_replace(7943 "/\({$wpdb->posts}.post_content (NOT LIKE|LIKE) (\'[^']+\')\)/",7944 '$0 OR ( sq1.meta_value $1 $2 )',7945 $clauses['where']7946 );7947 7948 return $clauses;7949 }7950 7951 /**7952 7922 * Sets the last changed time for the 'posts' cache group. 7953 7923 * -
branches/6.0/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php
r52068 r54534 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 -
branches/6.0/tests/phpunit/tests/query/search.php
r52389 r54534 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.