Make WordPress Core


Ignore:
Timestamp:
10/17/2022 12:24:45 PM (20 months 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:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/6.0

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