WordPress.org

Make WordPress Core

Changeset 40425


Ignore:
Timestamp:
04/14/2017 08:39:46 AM (2 years ago)
Author:
swissspidy
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.

Merges [40382] and [40421] to the 4.7 branch.

Location:
branches/4.7
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.7

  • branches/4.7/src/wp-includes/media.php

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