Skip to content

Commit 018ca41

Browse files
committed
refactor: Move filter into wp_get_media_library_attachment_months()
Consolidate the media_library_months_with_files filter inside the function so both call sites just use the getter. No transient caching. Add test for filter override behavior.
1 parent b7f1f68 commit 018ca41

3 files changed

Lines changed: 54 additions & 27 deletions

File tree

src/wp-admin/includes/media.php

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

28532853
<div class="alignleft actions">
28542854
<?php
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-
}
2855+
$months = wp_get_media_library_attachment_months();
28602856

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

src/wp-includes/media.php

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

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-
}
4869+
$months = wp_get_media_library_attachment_months();
48884870

48894871
foreach ( $months as $month_year ) {
48904872
$month_year->text = sprintf(
@@ -5148,9 +5130,6 @@ function wp_enqueue_media( $args = array() ) {
51485130
/**
51495131
* Retrieves the months that have media library attachments.
51505132
*
5151-
* Results are cached in a transient and automatically invalidated when
5152-
* attachments are created, updated, or deleted.
5153-
*
51545133
* Example:
51555134
*
51565135
* $months = wp_get_media_library_attachment_months();
@@ -5168,6 +5147,26 @@ function wp_enqueue_media( $args = array() ) {
51685147
function wp_get_media_library_attachment_months(): array {
51695148
global $wpdb;
51705149

5150+
/**
5151+
* Allows overriding the list of months displayed in the media library.
5152+
*
5153+
* By default (if this filter does not return an array), a query will be
5154+
* run to determine the months that have media items. This query can be
5155+
* expensive for large media libraries, so it may be desirable for sites to
5156+
* override this behavior.
5157+
*
5158+
* @since 4.7.4
5159+
*
5160+
* @link https://core.trac.wordpress.org/ticket/31071
5161+
*
5162+
* @param array<int, object{year: string, month: string}>|null $months An array of objects with `month` and `year`
5163+
* properties, or `null` for default behavior.
5164+
*/
5165+
$months = apply_filters( 'media_library_months_with_files', null );
5166+
if ( is_array( $months ) ) {
5167+
return $months;
5168+
}
5169+
51715170
return $wpdb->get_results(
51725171
$wpdb->prepare(
51735172
"SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month

tests/phpunit/tests/media/wpGetMediaLibraryAttachmentMonths.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,36 @@ public function test_returns_months() {
3535
$this->assertSame( '2024', $months[1]->year );
3636
$this->assertSame( '11', $months[1]->month );
3737
}
38+
39+
/**
40+
* Tests that the filter overrides the query result.
41+
*
42+
* @ticket 63279
43+
*/
44+
public function test_filter_overrides_result() {
45+
self::factory()->post->create(
46+
array(
47+
'post_type' => 'attachment',
48+
'post_date' => '2025-06-01 00:00:00',
49+
)
50+
);
51+
52+
$override = array(
53+
(object) array(
54+
'year' => '2020',
55+
'month' => '1',
56+
),
57+
);
58+
59+
add_filter(
60+
'media_library_months_with_files',
61+
static function () use ( $override ) {
62+
return $override;
63+
}
64+
);
65+
66+
$months = wp_get_media_library_attachment_months();
67+
68+
$this->assertSame( $override, $months );
69+
}
3870
}

0 commit comments

Comments
 (0)