Skip to content

Commit 95b9d62

Browse files
committed
Revert "refactor: Move filter into wp_get_media_library_attachment_months()"
This reverts commit 018ca41.
1 parent 018ca41 commit 95b9d62

3 files changed

Lines changed: 27 additions & 54 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: 22 additions & 21 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(
@@ -5130,6 +5148,9 @@ function wp_enqueue_media( $args = array() ) {
51305148
/**
51315149
* Retrieves the months that have media library attachments.
51325150
*
5151+
* Results are cached in a transient and automatically invalidated when
5152+
* attachments are created, updated, or deleted.
5153+
*
51335154
* Example:
51345155
*
51355156
* $months = wp_get_media_library_attachment_months();
@@ -5147,26 +5168,6 @@ function wp_enqueue_media( $args = array() ) {
51475168
function wp_get_media_library_attachment_months(): array {
51485169
global $wpdb;
51495170

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-
51705171
return $wpdb->get_results(
51715172
$wpdb->prepare(
51725173
"SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month

tests/phpunit/tests/media/wpGetMediaLibraryAttachmentMonths.php

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -35,36 +35,4 @@ 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-
}
7038
}

0 commit comments

Comments
 (0)