Widgets: Improve caching within get_calendar().

Improves caching of the `get_calendar()` function by:
* fixing incorrect cache collisions for different `initial` `post_type` and week values, and,
* ensuring parameter equivalents generate the same cache key, ie passing the same values in a different order.

Improves tests for the function by:
* navigating to February 2025 in test set up to ensure the correct calendar month is displayed,
* adding messages for tests with multiple assertions,
* improving the tests for the calendar captions by wrapping the expected value in the HTML tag,
* adding dedicated test for the different `initial` parameter,
* ensuring caches do not collide for different parameters, and,
* ensuring caches do collide for equivalent parameters.

Follow up to r4522, r59908, r59909, r59917 (reverted), r59918 (reverted), r59930.

Props peterwilsoncc, jorbin, audrasjb.
Fixes #34093.

Built from https://develop.svn.wordpress.org/trunk@59939


git-svn-id: http://core.svn.wordpress.org/trunk@59281 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Peter Wilson
2025-03-05 22:38:25 +00:00
parent dcfea21e26
commit 0ff2494d27
2 changed files with 34 additions and 8 deletions

View File

@@ -2292,7 +2292,39 @@ function get_calendar( $args = array() ) {
*/
$args = apply_filters( 'get_calendar_args', wp_parse_args( $args, $defaults ) );
$key = md5( $m . $monthnum . $year );
if ( ! post_type_exists( $args['post_type'] ) ) {
$args['post_type'] = 'post';
}
$w = 0;
if ( isset( $_GET['w'] ) ) {
$w = (int) $_GET['w'];
}
/*
* Normalize the cache key.
*
* The following ensures the same cache key is used for the same parameter
* and parameter equivalents. This prevents `post_type > post, initial > true`
* from generating a different key from the same values in the reverse order.
*
* `display` is excluded from the cache key as the cache contains the same
* HTML regardless of this functions need to echo or return the output.
*
* The global values contain data generated by the URL querystring variables.
*/
$cache_args = $args;
unset( $cache_args['display'] );
$cache_args['globals'] = array(
'm' => $m,
'monthnum' => $monthnum,
'year' => $year,
'week' => $w,
);
wp_recursive_ksort( $cache_args );
$key = md5( serialize( $cache_args ) );
$cache = wp_cache_get( 'get_calendar', 'calendar' );
if ( $cache && is_array( $cache ) && isset( $cache[ $key ] ) ) {
@@ -2312,9 +2344,6 @@ function get_calendar( $args = array() ) {
}
$post_type = $args['post_type'];
if ( ! post_type_exists( $post_type ) ) {
$post_type = 'post';
}
// Quick check. If we have no posts at all, abort!
if ( ! $posts ) {
@@ -2327,9 +2356,6 @@ function get_calendar( $args = array() ) {
}
}
if ( isset( $_GET['w'] ) ) {
$w = (int) $_GET['w'];
}
// week_begins = 0 stands for Sunday.
$week_begins = (int) get_option( 'start_of_week' );

View File

@@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.8-beta1-59938';
$wp_version = '6.8-beta1-59939';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.