Make WordPress Core

Changeset 54524


Ignore:
Timestamp:
10/17/2022 11:17:38 AM (2 years ago)
Author:
audrasjb
Message:

Media: Refactor search by filename within the admin.

Props vortfu, xknown, peterwilsoncc, paulkevan.

Location:
trunk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/ajax-actions.php

    r54319 r54524  
    30243024    // Filter query clauses to include filenames.
    30253025    if ( isset( $query['s'] ) ) {
    3026         add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
     3026        add_filter( 'wp_allow_query_attachment_by_filename', '__return_true' );
    30273027    }
    30283028
  • trunk/src/wp-admin/includes/post.php

    r54244 r54524  
    13091309    // Filter query clauses to include filenames.
    13101310    if ( isset( $q['s'] ) ) {
    1311         add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
     1311        add_filter( 'wp_allow_query_attachment_by_filename', '__return_true' );
    13121312    }
    13131313
  • trunk/src/wp-includes/class-wp-query.php

    r54496 r54524  
    445445     */
    446446    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;
    447455
    448456    /**
     
    14301438            }
    14311439
    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            }
    14341447            $searchand = ' AND ';
    14351448        }
     
    18261839        $q = $this->fill_query_vars( $q );
    18271840
     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
    18281851        // Parse meta query.
    18291852        $this->meta_query = new WP_Meta_Query();
     
    22572280        }
    22582281
    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 ) ) {
    22602283            $groupby = "{$wpdb->posts}.ID";
    22612284        }
     
    23332356        }
    23342357        $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        }
    23352362
    23362363        if ( ! empty( $this->meta_query->queries ) ) {
  • trunk/src/wp-includes/deprecated.php

    r54480 r54524  
    45124512    return false;
    45134513}
     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 */
     4526function _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  
    79347934
    79357935/**
    7936  * Filters the SQL clauses of an attachment query to include filenames.
    7937  *
    7938  * @since 4.7.0
    7939  * @access private
    7940  *
    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 /**
    79667936 * Sets the last changed time for the 'posts' cache group.
    79677937 *
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php

    r54085 r54524  
    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
  • trunk/tests/phpunit/tests/query/search.php

    r54090 r54524  
    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.