Skip to content

Commit 750ca84

Browse files
committed
Switch to a different filter name and implementation
see discussion in #10552 (comment)
1 parent 7ce9d9f commit 750ca84

File tree

2 files changed

+36
-42
lines changed

2 files changed

+36
-42
lines changed

src/wp-includes/general-template.php

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2018,6 +2018,17 @@ function wp_get_archives( $args = '' ) {
20182018
'w' => get_query_var( 'w' ),
20192019
);
20202020

2021+
/**
2022+
* Filters the archive links list arguments.
2023+
*
2024+
* @since 7.0.0
2025+
*
2026+
* @see wp_get_archives()
2027+
*
2028+
* @param array $args An array of arguments.
2029+
*/
2030+
$args = apply_filters( 'wp_get_archives_args', $args );
2031+
20212032
$parsed_args = wp_parse_args( $args, $defaults );
20222033

20232034
$post_type_object = get_post_type_object( $parsed_args['post_type'] );
@@ -2031,19 +2042,9 @@ function wp_get_archives( $args = '' ) {
20312042
$parsed_args['type'] = 'monthly';
20322043
}
20332044

2034-
/**
2035-
* Filters the limit for the number of posts to include in the archive.
2036-
*
2037-
* @since 7.0.0
2038-
*
2039-
* @param int $limit The limit for the number of posts to include in the archive. Default 0.
2040-
* @param array $parsed_args An array of default arguments.
2041-
*/
2042-
$limit_number = (int) apply_filters( 'getarchives_limit', absint( $parsed_args['limit'] ), $parsed_args );
2043-
if ( $limit_number > 0 ) {
2044-
$limit = " LIMIT $limit_number";
2045-
} else {
2046-
$limit = '';
2045+
if ( ! empty( $parsed_args['limit'] ) ) {
2046+
$parsed_args['limit'] = absint( $parsed_args['limit'] );
2047+
$parsed_args['limit'] = ' LIMIT ' . $parsed_args['limit'];
20472048
}
20482049

20492050
$order = strtoupper( $parsed_args['order'] );
@@ -2080,6 +2081,8 @@ function wp_get_archives( $args = '' ) {
20802081

20812082
$last_changed = wp_cache_get_last_changed( 'posts' );
20822083

2084+
$limit = $parsed_args['limit'];
2085+
20832086
if ( 'monthly' === $parsed_args['type'] ) {
20842087
$query = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date $order $limit";
20852088
$key = md5( $query );

tests/phpunit/tests/functions/wpGetArchives.php

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -208,46 +208,37 @@ public function test_wp_get_archives_post_type() {
208208
/**
209209
* @ticket 64304
210210
*/
211-
public function test_wp_get_archives_limit_filter() {
211+
public function test_wp_get_archives_args_filter() {
212+
// Test that the filter can modify the limit argument.
213+
$filter_callback = function( $args ) {
214+
$args['limit'] = 3;
215+
return $args;
216+
};
217+
add_filter( 'wp_get_archives_args', $filter_callback );
218+
212219
$ids = array_slice( array_reverse( self::$post_ids ), 0, 3 );
213220

221+
$link1 = get_permalink( $ids[0] );
222+
$link2 = get_permalink( $ids[1] );
223+
$link3 = get_permalink( $ids[2] );
224+
214225
$title1 = get_post( $ids[0] )->post_title;
215226
$title2 = get_post( $ids[1] )->post_title;
216227
$title3 = get_post( $ids[2] )->post_title;
217228

218-
// Test without filter - should return all 3 posts when limit is 3.
219-
$archives_without_filter = wp_get_archives(
229+
$expected = <<<EOF
230+
<li><a href='$link1'>$title1</a></li>
231+
<li><a href='$link2'>$title2</a></li>
232+
<li><a href='$link3'>$title3</a></li>
233+
EOF;
234+
$archives = wp_get_archives(
220235
array(
221236
'echo' => false,
222237
'type' => 'postbypost',
223-
'limit' => 3,
238+
'limit' => 5, // This should be overridden by the filter to 3.
224239
)
225240
);
226-
$this->assertStringContainsString( $title1, $archives_without_filter );
227-
$this->assertStringContainsString( $title2, $archives_without_filter );
228-
$this->assertStringContainsString( $title3, $archives_without_filter );
229-
230-
// Add filter to modify limit to 2.
231-
add_filter(
232-
'getarchives_limit',
233-
function ( $limit, $parsed_args ) {
234-
// Modify limit from 3 to 2.
235-
return 2;
236-
},
237-
10,
238-
2
239-
);
240241

241-
// Test with filter - should return only 2 posts.
242-
$archives_with_filter = wp_get_archives(
243-
array(
244-
'echo' => false,
245-
'type' => 'postbypost',
246-
'limit' => 3,
247-
)
248-
);
249-
$this->assertStringContainsString( $title1, $archives_with_filter );
250-
$this->assertStringContainsString( $title2, $archives_with_filter );
251-
$this->assertStringNotContainsString( $title3, $archives_with_filter );
242+
$this->assertSameIgnoreEOL( $expected, trim( $archives ) );
252243
}
253244
}

0 commit comments

Comments
 (0)