Make WordPress Core

Ticket #31071: 31071.2.diff

File 31071.2.diff, 3.2 KB (added by jnylen0, 8 years ago)

Allow overriding slow media queries via filters

  • src/wp-includes/media.php

    diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php
    index 0c83725..553ba1b 100644
    a b function wp_enqueue_media( $args = array() ) { 
    33213321                }
    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 (see #31071), so it may be
     3330         * desirable for sites to override this behavior.
     3331         *
     3332         * @since 4.7.5
     3333         *
     3334         * @return bool|null whether to show the button, `null` for default behavior.
     3335         */
     3336        $has_audio = apply_filters( 'media_has_audio', null );
     3337        if ( is_null( $has_audio ) ) {
     3338                $has_audio = $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 (see #31071), so it may be
     3353         * desirable for sites to override this behavior.
     3354         *
     3355         * @since 4.7.5
     3356         *
     3357         * @return bool|null whether to show the button, `null` for default behavior.
     3358         */
     3359        $has_video = apply_filters( 'media_has_video', null );
     3360        if ( is_null( $has_video ) ) {
     3361                $has_video = $wpdb->get_var( "
     3362                        SELECT ID
     3363                        FROM $wpdb->posts
     3364                        WHERE post_type = 'attachment'
     3365                        AND post_mime_type LIKE 'video%'
     3366                        LIMIT 1
     3367                " );
     3368        }
     3369
     3370        /**
     3371         * Allows overriding the list of months displayed in the media library.
     3372         *
     3373         * By default (if this filter does not return an array), a query will be
     3374         * run to determine the months that have media items.  This query can be
     3375         * expensive for large media libraries (see #31071), so it may be desirable
     3376         * for sites to override this behavior.
     3377         *
     3378         * @since 4.7.5
     3379         *
     3380         * @return array|null an array of objects with `month` and `year`
     3381         *                    properties, or `null` for default behavior.
     3382         */
     3383        $months = apply_filters( 'media_months', null );
     3384        if ( ! is_array( $months ) ) {
     3385                $months = $wpdb->get_results( $wpdb->prepare( "
     3386                        SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month
     3387                        FROM $wpdb->posts
     3388                        WHERE post_type = %s
     3389                        ORDER BY post_date DESC
     3390                ", 'attachment' ) );
     3391        }
    33443392        foreach ( $months as $month_year ) {
    33453393                $month_year->text = sprintf( __( '%1$s %2$d' ), $wp_locale->get_month( $month_year->month ), $month_year->year );
    33463394        }