Skip to content

Commit b7f1f68

Browse files
committed
refactor: Revert filter consolidation into wp_get_media_library_attachment_months()
Move the media_library_months_with_files filter back to the two call sites (wp_enqueue_media and media_upload_library_form) where it originally lived. Simplify the function to just the raw query without transient caching or int casting. Remove the now-unnecessary transient invalidation from clean_post_cache(). Update tests to match.
1 parent ad1cec4 commit b7f1f68

4 files changed

Lines changed: 34 additions & 106 deletions

File tree

src/wp-admin/includes/media.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2852,7 +2852,11 @@ function media_upload_library_form( $errors ) {
28522852

28532853
<div class="alignleft actions">
28542854
<?php
2855-
$months = wp_get_media_library_attachment_months();
2855+
/** This filter is documented in wp-includes/media.php */
2856+
$months = apply_filters( 'media_library_months_with_files', null );
2857+
if ( ! is_array( $months ) ) {
2858+
$months = wp_get_media_library_attachment_months();
2859+
}
28562860

28572861
$month_count = count( $months );
28582862
$selected_month = isset( $_GET['m'] ) ? (int) $_GET['m'] : 0;

src/wp-includes/media.php

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4866,7 +4866,25 @@ function wp_enqueue_media( $args = array() ) {
48664866
);
48674867
}
48684868

4869-
$months = wp_get_media_library_attachment_months();
4869+
/**
4870+
* Allows overriding the list of months displayed in the media library.
4871+
*
4872+
* By default (if this filter does not return an array), a query will be
4873+
* run to determine the months that have media items. This query can be
4874+
* expensive for large media libraries, so it may be desirable for sites to
4875+
* override this behavior.
4876+
*
4877+
* @since 4.7.4
4878+
*
4879+
* @link https://core.trac.wordpress.org/ticket/31071
4880+
*
4881+
* @param stdClass[]|null $months An array of objects with `month` and `year`
4882+
* properties, or `null` for default behavior.
4883+
*/
4884+
$months = apply_filters( 'media_library_months_with_files', null );
4885+
if ( ! is_array( $months ) ) {
4886+
$months = wp_get_media_library_attachment_months();
4887+
}
48704888

48714889
foreach ( $months as $month_year ) {
48724890
$month_year->text = sprintf(
@@ -5137,44 +5155,20 @@ function wp_enqueue_media( $args = array() ) {
51375155
*
51385156
* $months = wp_get_media_library_attachment_months();
51395157
* $months === array(
5140-
* (object) array( 'year' => 2025, 'month' => 2 ),
5141-
* (object) array( 'year' => 2024, 'month' => 11 ),
5158+
* (object) array( 'year' => '2025', 'month' => '2' ),
5159+
* (object) array( 'year' => '2024', 'month' => '11' ),
51425160
* );
51435161
*
51445162
* @since tbd
51455163
*
51465164
* @global wpdb $wpdb WordPress database abstraction object.
51475165
*
5148-
* @return array<int, object{year: int, month: int}> Months with associated attachment post dates.
5166+
* @return array<int, object{year: string, month: string}> Months with associated attachment post dates.
51495167
*/
51505168
function wp_get_media_library_attachment_months(): array {
51515169
global $wpdb;
51525170

5153-
/**
5154-
* Allows overriding the list of months displayed in the media library.
5155-
*
5156-
* Returning an array from this filter will bypass both the transient cache
5157-
* and the database query.
5158-
*
5159-
* @since 4.7.4
5160-
* @since tbd Moved to {@see wp_get_media_library_attachment_months()}.
5161-
*
5162-
* @link https://core.trac.wordpress.org/ticket/31071
5163-
*
5164-
* @param stdClass[]|null $months An array of objects with `year` and `month`
5165-
* properties, or `null` for default behavior.
5166-
*/
5167-
$months = apply_filters( 'media_library_months_with_files', null );
5168-
if ( is_array( $months ) ) {
5169-
return $months;
5170-
}
5171-
5172-
$months = get_transient( 'wp_media_library_attachment_months' );
5173-
if ( false !== $months ) {
5174-
return $months;
5175-
}
5176-
5177-
$months = $wpdb->get_results(
5171+
return $wpdb->get_results(
51785172
$wpdb->prepare(
51795173
"SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month
51805174
FROM $wpdb->posts
@@ -5183,15 +5177,6 @@ function wp_get_media_library_attachment_months(): array {
51835177
'attachment'
51845178
)
51855179
);
5186-
5187-
foreach ( $months as $row ) {
5188-
$row->year = (int) $row->year;
5189-
$row->month = (int) $row->month;
5190-
}
5191-
5192-
set_transient( 'wp_media_library_attachment_months', $months, WEEK_IN_SECONDS );
5193-
5194-
return $months;
51955180
}
51965181

51975182
/**

src/wp-includes/post.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7791,10 +7791,6 @@ function clean_post_cache( $post ) {
77917791
do_action( 'clean_page_cache', $post->ID );
77927792
}
77937793

7794-
if ( 'attachment' === $post->post_type ) {
7795-
delete_transient( 'wp_media_library_attachment_months' );
7796-
}
7797-
77987794
wp_cache_set_posts_last_changed();
77997795
}
78007796

tests/phpunit/tests/media/wpGetMediaLibraryAttachmentMonths.php

Lines changed: 6 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
class Tests_Media_wpGetMediaLibraryAttachmentMonths extends WP_UnitTestCase {
1010

1111
/**
12-
* Tests that the function returns the correct months and caches the result.
12+
* Tests that the function returns the correct months.
1313
*
1414
* @ticket 63279
1515
*/
16-
public function test_returns_months_and_caches_result() {
16+
public function test_returns_months() {
1717
self::factory()->post->create(
1818
array(
1919
'post_type' => 'attachment',
@@ -27,69 +27,12 @@ public function test_returns_months_and_caches_result() {
2727
)
2828
);
2929

30-
delete_transient( 'wp_media_library_attachment_months' );
31-
3230
$months = wp_get_media_library_attachment_months();
3331

3432
$this->assertCount( 2, $months );
35-
$this->assertSame( 2025, $months[0]->year );
36-
$this->assertSame( 3, $months[0]->month );
37-
$this->assertSame( 2024, $months[1]->year );
38-
$this->assertSame( 11, $months[1]->month );
39-
40-
// Verify the result was cached.
41-
$this->assertEquals( $months, get_transient( 'wp_media_library_attachment_months' ) );
42-
}
43-
44-
/**
45-
* Tests that the filter bypasses transient and query.
46-
*
47-
* @ticket 63279
48-
*/
49-
public function test_filter_overrides_result() {
50-
self::factory()->post->create(
51-
array(
52-
'post_type' => 'attachment',
53-
'post_date' => '2025-06-01 00:00:00',
54-
)
55-
);
56-
57-
delete_transient( 'wp_media_library_attachment_months' );
58-
59-
// Confirm the function returns data without the filter.
60-
$this->assertNotEmpty( wp_get_media_library_attachment_months() );
61-
62-
delete_transient( 'wp_media_library_attachment_months' );
63-
64-
$override = array(
65-
(object) array(
66-
'year' => 2020,
67-
'month' => 1,
68-
),
69-
);
70-
71-
add_filter(
72-
'media_library_months_with_files',
73-
static function () use ( $override ) {
74-
return $override;
75-
}
76-
);
77-
78-
$months = wp_get_media_library_attachment_months();
79-
80-
$this->assertSame( $override, $months );
81-
}
82-
83-
/**
84-
* Tests that the transient is deleted when a new attachment is created.
85-
*
86-
* @ticket 63279
87-
*/
88-
public function test_transient_is_invalidated_on_new_attachment() {
89-
set_transient( 'wp_media_library_attachment_months', array() );
90-
91-
self::factory()->post->create( array( 'post_type' => 'attachment' ) );
92-
93-
$this->assertFalse( get_transient( 'wp_media_library_attachment_months' ) );
33+
$this->assertSame( '2025', $months[0]->year );
34+
$this->assertSame( '3', $months[0]->month );
35+
$this->assertSame( '2024', $months[1]->year );
36+
$this->assertSame( '11', $months[1]->month );
9437
}
9538
}

0 commit comments

Comments
 (0)