WordPress.org

Make WordPress Core

Ticket #39004: 39004.3.patch

File 39004.3.patch, 5.6 KB (added by Mista-Flo, 9 months ago)
  • src/wp-admin/includes/ajax-actions.php

    diff --git a/src/wp-admin/includes/ajax-actions.php b/src/wp-admin/includes/ajax-actions.php
    index 6ff2d2d9fd..9fd70f48cb 100644
    a b function wp_ajax_query_attachments() { 
    29752975
    29762976        // Filter query clauses to include filenames.
    29772977        if ( isset( $query['s'] ) ) {
    2978                 add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
     2978                add_filter( 'posts_clauses', '_filter_query_attachment_meta' );
    29792979        }
    29802980
    29812981        /**
  • src/wp-admin/includes/post.php

    diff --git a/src/wp-admin/includes/post.php b/src/wp-admin/includes/post.php
    index 77aadb2135..0be860ea01 100644
    a b function wp_edit_attachments_query_vars( $q = false ) { 
    12691269
    12701270        // Filter query clauses to include filenames.
    12711271        if ( isset( $q['s'] ) ) {
    1272                 add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
     1272                add_filter( 'posts_clauses', '_filter_query_attachment_meta' );
    12731273        }
    12741274
    12751275        return $q;
  • src/wp-includes/post.php

    diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php
    index a5bd9678bb..ba80b0111c 100644
    a b function wp_add_trashed_suffix_to_post_name_for_post( $post ) { 
    74827482}
    74837483
    74847484/**
    7485  * Filters the SQL clauses of an attachment query to include filenames.
     7485 * Filters the SQL clauses of an attachment query to include meta (filename/alt).
    74867486 *
    74877487 * @since 4.7.0
    74887488 * @access private
    function wp_add_trashed_suffix_to_post_name_for_post( $post ) { 
    74937493 *                          DISTINCT, fields (SELECT), and LIMITS clauses.
    74947494 * @return string[] The modified array of clauses.
    74957495 */
    7496 function _filter_query_attachment_filenames( $clauses ) {
     7496function _filter_query_attachment_meta( $clauses ) {
    74977497        global $wpdb;
    74987498        remove_filter( 'posts_clauses', __FUNCTION__ );
    74997499
    75007500        // Add a LEFT JOIN of the postmeta table so we don't trample existing JOINs.
    7501         $clauses['join'] .= " LEFT JOIN {$wpdb->postmeta} AS sq1 ON ( {$wpdb->posts}.ID = sq1.post_id AND sq1.meta_key = '_wp_attached_file' )";
     7501        $clauses['join'] .= " LEFT JOIN {$wpdb->postmeta} AS sq1 ON ( {$wpdb->posts}.ID = sq1.post_id AND ( sq1.meta_key = '_wp_attached_file' OR sq1.meta_key = '_wp_attachment_image_alt'  ) )";
    75027502
    75037503        $clauses['groupby'] = "{$wpdb->posts}.ID";
    75047504
  • tests/phpunit/tests/query/search.php

    diff --git a/tests/phpunit/tests/query/search.php b/tests/phpunit/tests/query/search.php
    index c1c74c8240..38af8fb872 100644
    a b class Tests_Query_Search extends WP_UnitTestCase { 
    455455                );
    456456
    457457                add_post_meta( $attachment, '_wp_attached_file', 'some-image1.png', true );
    458                 add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
     458                add_filter( 'posts_clauses', '_filter_query_attachment_meta' );
    459459
    460460                // Pass post_type a string value.
    461461                $q = new WP_Query(
    class Tests_Query_Search extends WP_UnitTestCase { 
    485485                );
    486486
    487487                add_post_meta( $attachment, '_wp_attached_file', 'some-image2.png', true );
    488                 add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
     488                add_filter( 'posts_clauses', '_filter_query_attachment_meta' );
    489489
    490490                // Pass post_type an array value.
    491491                $q = new WP_Query(
    class Tests_Query_Search extends WP_UnitTestCase { 
    544544
    545545                add_post_meta( $attachment, '_wp_attached_file', 'some-image4.png', true );
    546546                add_post_meta( $attachment, '_test_meta_key', 'value', true );
    547                 add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
     547                add_filter( 'posts_clauses', '_filter_query_attachment_meta' );
    548548
    549549                // Pass post_type a string value.
    550550                $q = new WP_Query(
    class Tests_Query_Search extends WP_UnitTestCase { 
    584584                wp_set_post_terms( $attachment, 'test', 'post_tag' );
    585585
    586586                add_post_meta( $attachment, '_wp_attached_file', 'some-image5.png', true );
    587                 add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
     587                add_filter( 'posts_clauses', '_filter_query_attachment_meta' );
    588588
    589589                // Pass post_type a string value.
    590590                $q = new WP_Query(
    class Tests_Query_Search extends WP_UnitTestCase { 
    606606                $this->assertSame( array( $attachment ), $q->posts );
    607607        }
    608608
     609        /**
     610         * @ticket 39004
     611         */
     612        public function test_include_file_names_in_attachment_search_with_alt_text() {
     613                $attachment = self::factory()->post->create(
     614                        array(
     615                                'post_type'    => 'attachment',
     616                                'post_status'  => 'publish',
     617                                'post_title'   => 'bar foo',
     618                                'post_content' => 'foo bar',
     619                                'post_excerpt' => 'This post has foo',
     620                        )
     621                );
     622
     623                add_post_meta( $attachment, '_wp_attached_file', 'some-image4.png', true );
     624
     625                // Add an alt text
     626                add_post_meta( $attachment, '_wp_attachment_image_alt', 'Alt text', true );
     627
     628                add_filter( 'posts_clauses', '_filter_query_attachment_meta' );
     629
     630                // Pass post_type a string value.
     631                $q = new WP_Query(
     632                        array(
     633                                's'           => 'Alt text',
     634                                'fields'      => 'ids',
     635                                'post_type'   => 'attachment',
     636                                'post_status' => 'inherit',
     637                        )
     638                );
     639
     640                $this->assertSame( array( $attachment ), $q->posts );
     641        }
     642
    609643        /**
    610644         * @ticket 22744
    611645         */
    612         public function test_filter_query_attachment_filenames_unhooks_itself() {
    613                 add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
     646        public function test_filter_query_attachment_meta_unhooks_itself() {
     647                add_filter( 'posts_clauses', '_filter_query_attachment_meta' );
    614648
    615649                apply_filters(
    616650                        'posts_clauses',
    class Tests_Query_Search extends WP_UnitTestCase { 
    625659                        )
    626660                );
    627661
    628                 $result = has_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
     662                $result = has_filter( 'posts_clauses', '_filter_query_attachment_meta' );
    629663
    630664                $this->assertFalse( $result );
    631665        }