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.


git-svn-id: https://develop.svn.wordpress.org/branches/4.7@40425 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Pascal Birchler 2017-04-14 08:39:46 +00:00
parent 41e53f4385
commit 59e00d43a5
1 changed files with 78 additions and 23 deletions

View File

@ -3319,26 +3319,81 @@ function wp_enqueue_media( $args = array() ) {
} }
} }
$has_audio = $wpdb->get_var( " /**
SELECT ID * Allows showing or hiding the "Create Audio Playlist" button in the media library.
FROM $wpdb->posts *
WHERE post_type = 'attachment' * By default (if this filter returns `null`), a query will be run to
AND post_mime_type LIKE 'audio%' * determine whether the media library contains any audio items. This
LIMIT 1 * query is expensive for large media libraries, so it may be desirable for
" ); * sites to override this behavior.
$has_video = $wpdb->get_var( " *
SELECT ID * @since 4.7.4
FROM $wpdb->posts *
WHERE post_type = 'attachment' * @link https://core.trac.wordpress.org/ticket/31071
AND post_mime_type LIKE 'video%' *
LIMIT 1 * @param bool|null Whether to show the button, or `null` for default behavior.
" ); */
$months = $wpdb->get_results( $wpdb->prepare( " $show_audio_playlist = apply_filters( 'media_library_show_audio_playlist', null );
SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month if ( null === $show_audio_playlist ) {
FROM $wpdb->posts $show_audio_playlist = $wpdb->get_var( "
WHERE post_type = %s SELECT ID
ORDER BY post_date DESC FROM $wpdb->posts
", 'attachment' ) ); WHERE post_type = 'attachment'
AND post_mime_type LIKE 'audio%'
LIMIT 1
" );
}
/**
* Allows showing or hiding the "Create Video Playlist" button in the media library.
*
* By default (if this filter returns `null`), a query will be run to
* determine whether the media library contains any video items. This
* query is expensive for large media libraries, so it may be desirable for
* sites to override this behavior.
*
* @since 4.7.4
*
* @link https://core.trac.wordpress.org/ticket/31071
*
* @param bool|null Whether to show the button, or `null` for default behavior.
*/
$show_video_playlist = apply_filters( 'media_library_show_video_playlist', null );
if ( null === $show_video_playlist ) {
$show_video_playlist = $wpdb->get_var( "
SELECT ID
FROM $wpdb->posts
WHERE post_type = 'attachment'
AND post_mime_type LIKE 'video%'
LIMIT 1
" );
}
/**
* Allows overriding the list of months displayed in the media library.
*
* By default (if this filter does not return an array), a query will be
* run to determine the months that have media items. This query can be
* expensive for large media libraries, so it may be desirable for sites to
* override this behavior.
*
* @since 4.7.4
*
* @link https://core.trac.wordpress.org/ticket/31071
*
* @param array|null An array of objects with `month` and `year`
* properties, or `null` (or any other non-array value)
* for default behavior.
*/
$months = apply_filters( 'media_library_months_with_files', null );
if ( ! is_array( $months ) ) {
$months = $wpdb->get_results( $wpdb->prepare( "
SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month
FROM $wpdb->posts
WHERE post_type = %s
ORDER BY post_date DESC
", 'attachment' ) );
}
foreach ( $months as $month_year ) { foreach ( $months as $month_year ) {
$month_year->text = sprintf( __( '%1$s %2$d' ), $wp_locale->get_month( $month_year->month ), $month_year->year ); $month_year->text = sprintf( __( '%1$s %2$d' ), $wp_locale->get_month( $month_year->month ), $month_year->year );
} }
@ -3357,14 +3412,14 @@ function wp_enqueue_media( $args = array() ) {
), ),
'defaultProps' => $props, 'defaultProps' => $props,
'attachmentCounts' => array( 'attachmentCounts' => array(
'audio' => ( $has_audio ) ? 1 : 0, 'audio' => ( $show_audio_playlist ) ? 1 : 0,
'video' => ( $has_video ) ? 1 : 0 'video' => ( $show_video_playlist ) ? 1 : 0,
), ),
'embedExts' => $exts, 'embedExts' => $exts,
'embedMimes' => $ext_mimes, 'embedMimes' => $ext_mimes,
'contentWidth' => $content_width, 'contentWidth' => $content_width,
'months' => $months, 'months' => $months,
'mediaTrash' => MEDIA_TRASH ? 1 : 0 'mediaTrash' => MEDIA_TRASH ? 1 : 0,
); );
$post = null; $post = null;