WordPress.org

Make WordPress Core

Changeset 40382


Ignore:
Timestamp:
04/06/2017 03:56:38 AM (14 months ago)
Author:
jnylen0
Message:

Media: Add filters to allow overriding slow media queries.

There are a couple of queries that do a full table scan of attachment posts to support features of the media library. Pending a more complete solution, allow overriding these queries via filters.

Props sboisvert, jnylen0.
See #31071.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/media.php

    r40283 r40382  
    33223322    }
    33233323
    3324     $has_audio = $wpdb->get_var( "
    3325         SELECT ID
    3326         FROM $wpdb->posts
    3327         WHERE post_type = 'attachment'
    3328         AND post_mime_type LIKE 'audio%'
    3329         LIMIT 1
    3330     " );
    3331     $has_video = $wpdb->get_var( "
    3332         SELECT ID
    3333         FROM $wpdb->posts
    3334         WHERE post_type = 'attachment'
    3335         AND post_mime_type LIKE 'video%'
    3336         LIMIT 1
    3337     " );
    3338     $months = $wpdb->get_results( $wpdb->prepare( "
    3339         SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month
    3340         FROM $wpdb->posts
    3341         WHERE post_type = %s
    3342         ORDER BY post_date DESC
    3343     ", 'attachment' ) );
     3324    /**
     3325     * Allows showing or hiding the "Create Audio Playlist" button in the media library.
     3326     *
     3327     * By default (if this filter returns `null`), a query will be run to
     3328     * determine whether the media library contains any audio items.  This
     3329     * query is expensive for large media libraries, so it may be desirable for
     3330     * sites to override this behavior.
     3331     *
     3332     * @since 4.7.4
     3333     *
     3334     * @link https://core.trac.wordpress.org/ticket/31071
     3335     *
     3336     * @return bool|null Whether to show the button, or `null` for default behavior.
     3337     */
     3338    $has_audio = apply_filters( 'media_has_audio', null );
     3339    if ( is_null( $has_audio ) ) {
     3340        $has_audio = $wpdb->get_var( "
     3341            SELECT ID
     3342            FROM $wpdb->posts
     3343            WHERE post_type = 'attachment'
     3344            AND post_mime_type LIKE 'audio%'
     3345            LIMIT 1
     3346        " );
     3347    }
     3348
     3349    /**
     3350     * Allows showing or hiding the "Create Video Playlist" button in the media library.
     3351     *
     3352     * By default (if this filter returns `null`), a query will be run to
     3353     * determine whether the media library contains any video items.  This
     3354     * query is expensive for large media libraries, so it may be desirable for
     3355     * sites to override this behavior.
     3356     *
     3357     * @since 4.7.4
     3358     *
     3359     * @link https://core.trac.wordpress.org/ticket/31071
     3360     *
     3361     * @return bool|null Whether to show the button, or `null` for default behavior.
     3362     */
     3363    $has_video = apply_filters( 'media_has_video', null );
     3364    if ( is_null( $has_video ) ) {
     3365        $has_video = $wpdb->get_var( "
     3366            SELECT ID
     3367            FROM $wpdb->posts
     3368            WHERE post_type = 'attachment'
     3369            AND post_mime_type LIKE 'video%'
     3370            LIMIT 1
     3371        " );
     3372    }
     3373
     3374    /**
     3375     * Allows overriding the list of months displayed in the media library.
     3376     *
     3377     * By default (if this filter does not return an array), a query will be
     3378     * run to determine the months that have media items.  This query can be
     3379     * expensive for large media libraries, so it may be desirable for sites to
     3380     * override this behavior.
     3381     *
     3382     * @since 4.7.4
     3383     *
     3384     * @link https://core.trac.wordpress.org/ticket/31071
     3385     *
     3386     * @return array|null An array of objects with `month` and `year`
     3387     *                    properties, or `null` (or any other non-array value)
     3388     *                    for default behavior.
     3389     */
     3390    $months = apply_filters( 'media_months', null );
     3391    if ( ! is_array( $months ) ) {
     3392        $months = $wpdb->get_results( $wpdb->prepare( "
     3393            SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month
     3394            FROM $wpdb->posts
     3395            WHERE post_type = %s
     3396            ORDER BY post_date DESC
     3397        ", 'attachment' ) );
     3398    }
    33443399    foreach ( $months as $month_year ) {
    33453400        $month_year->text = sprintf( __( '%1$s %2$d' ), $wp_locale->get_month( $month_year->month ), $month_year->year );
Note: See TracChangeset for help on using the changeset viewer.