Make WordPress Core

Changeset 54534


Ignore:
Timestamp:
10/17/2022 12:24:45 PM (2 years ago)
Author:
audrasjb
Message:

Media: Refactor search by filename within the admin.

Props vortfu, xknown, peterwilsoncc, paulkevan.
Merges [54524] to the 6.0 branch.

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  
    29952995    // Filter query clauses to include filenames.
    29962996    if ( isset( $query['s'] ) ) {
    2997         add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
     2997        add_filter( 'wp_allow_query_attachment_by_filename', '__return_true' );
    29982998    }
    29992999
  • branches/6.0/src/wp-admin/includes/post.php

    r53299 r54534  
    13031303    // Filter query clauses to include filenames.
    13041304    if ( isset( $q['s'] ) ) {
    1305         add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
     1305        add_filter( 'wp_allow_query_attachment_by_filename', '__return_true' );
    13061306    }
    13071307
  • branches/6.0/src/wp-includes/class-wp-query.php

    r53379 r54534  
    444444     */
    445445    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;
    446454
    447455    /**
     
    14151423            }
    14161424
    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            }
    14191432            $searchand = ' AND ';
    14201433        }
     
    18111824        $q = $this->fill_query_vars( $q );
    18121825
     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
    18131836        // Parse meta query.
    18141837        $this->meta_query = new WP_Meta_Query();
     
    22422265        }
    22432266
    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 ) ) {
    22452268            $groupby = "{$wpdb->posts}.ID";
    22462269        }
     
    23182341        }
    23192342        $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        }
    23202347
    23212348        if ( ! empty( $this->meta_query->queries ) ) {
  • branches/6.0/src/wp-includes/deprecated.php

    r53287 r54534  
    43124312    _deprecated_function( __FUNCTION__, '6.0.0' );
    43134313}
     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 */
     4326function _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  
    79207920
    79217921/**
    7922  * Filters the SQL clauses of an attachment query to include filenames.
    7923  *
    7924  * @since 4.7.0
    7925  * @access private
    7926  *
    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 /**
    79527922 * Sets the last changed time for the 'posts' cache group.
    79537923 *
  • branches/6.0/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php

    r52068 r54534  
    9898        // Filter query clauses to include filenames.
    9999        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' );
    101101        }
    102102
  • branches/6.0/tests/phpunit/tests/query/search.php

    r52389 r54534  
    455455
    456456        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' );
    458458
    459459        // Pass post_type a string value.
     
    485485
    486486        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' );
    488488
    489489        // Pass post_type an array value.
     
    544544        add_post_meta( $attachment, '_wp_attached_file', 'some-image4.png', true );
    545545        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' );
    547547
    548548        // Pass post_type a string value.
     
    584584
    585585        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' );
    587587
    588588        // Pass post_type a string value.
     
    609609     * @ticket 22744
    610610     */
    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 );
    630642    }
    631643
Note: See TracChangeset for help on using the changeset viewer.