Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 33 additions & 13 deletions src/wp-includes/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -4869,10 +4869,11 @@ function wp_enqueue_media( $args = array() ) {
/**
* 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.
* By default, if this filter does not return an array,
* {@see wp_get_media_library_months_with_files()} will run a query to determine
* the months that have media items. The result is stored in a transient
* and automatically invalidated when attachments are created, updated,
* or deleted.
*
* @since 4.7.4
*
Expand All @@ -4883,16 +4884,13 @@ function wp_enqueue_media( $args = array() ) {
*/
$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'
)
);
$months = get_transient( 'media_library_months_with_files' );
Comment thread
apermo marked this conversation as resolved.
Outdated
if ( false === $months ) {
$months = wp_get_media_library_months_with_files();
set_transient( 'media_library_months_with_files', $months );
Comment thread
apermo marked this conversation as resolved.
Outdated
}
}

foreach ( $months as $month_year ) {
$month_year->text = sprintf(
/* translators: 1: Month, 2: Year. */
Expand Down Expand Up @@ -5152,6 +5150,28 @@ function wp_enqueue_media( $args = array() ) {
do_action( 'wp_enqueue_media' );
}

/**
* Retrieves the months that have media items.
*
* @since tbd
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @return array<int, object{year: string, month: string}> Array of objects with `month` and `year` properties.
Comment thread
apermo marked this conversation as resolved.
Outdated
*/
function wp_get_media_library_months_with_files(): array {
Comment thread
apermo marked this conversation as resolved.
Outdated
global $wpdb;
return $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'
)
);
Comment thread
apermo marked this conversation as resolved.
}

/**
* Retrieves media attached to the passed post.
*
Expand Down
4 changes: 4 additions & 0 deletions src/wp-includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -7791,6 +7791,10 @@ function clean_post_cache( $post ) {
do_action( 'clean_page_cache', $post->ID );
}

if ( 'attachment' === $post->post_type ) {
delete_transient( 'media_library_months_with_files' );
Comment thread
apermo marked this conversation as resolved.
Outdated
}

wp_cache_set_posts_last_changed();
}

Expand Down
Loading