Make WordPress Core


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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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 ) ) {
Note: See TracChangeset for help on using the changeset viewer.