From 0ff2494d278efdfcf359e2821ea5016d90f52503 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Wed, 5 Mar 2025 22:38:25 +0000 Subject: [PATCH] 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 --- wp-includes/general-template.php | 40 ++++++++++++++++++++++++++------ wp-includes/version.php | 2 +- 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/wp-includes/general-template.php b/wp-includes/general-template.php index 527289901e..3b0875f876 100644 --- a/wp-includes/general-template.php +++ b/wp-includes/general-template.php @@ -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' ); diff --git a/wp-includes/version.php b/wp-includes/version.php index fb94dec7f8..7eb71f3a75 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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.