<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[59939] trunk: Widgets: Improve caching within `get_calendar()`.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/59939">59939</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/59939","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>peterwilsoncc</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2025-03-05 22:36:38 +0000 (Wed, 05 Mar 2025)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>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 <a href="https://core.trac.wordpress.org/changeset/4522">r4522</a>, <a href="https://core.trac.wordpress.org/changeset/59908">r59908</a>, <a href="https://core.trac.wordpress.org/changeset/59909">r59909</a>, <a href="https://core.trac.wordpress.org/changeset/59917">r59917</a> (reverted), <a href="https://core.trac.wordpress.org/changeset/59918">r59918</a> (reverted), <a href="https://core.trac.wordpress.org/changeset/59930">r59930</a>.
Props peterwilsoncc, jorbin, audrasjb.
Fixes <a href="https://core.trac.wordpress.org/ticket/34093">#34093</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesgeneraltemplatephp">trunk/src/wp-includes/general-template.php</a></li>
<li><a href="#trunktestsphpunittestsgeneralgetCalendarphp">trunk/tests/phpunit/tests/general/getCalendar.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesgeneraltemplatephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/general-template.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/general-template.php 2025-03-05 22:15:09 UTC (rev 59938)
+++ trunk/src/wp-includes/general-template.php 2025-03-05 22:36:38 UTC (rev 59939)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2292,7 +2292,39 @@
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> $args = apply_filters( 'get_calendar_args', wp_parse_args( $args, $defaults ) );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $key = md5( $m . $monthnum . $year );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 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 ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> $cache = wp_cache_get( 'get_calendar', 'calendar' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( $cache && is_array( $cache ) && isset( $cache[ $key ] ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2312,9 +2344,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $post_type = $args['post_type'];
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( ! post_type_exists( $post_type ) ) {
- $post_type = 'post';
- }
</del><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Quick check. If we have no posts at all, abort!
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! $posts ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2327,9 +2356,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( isset( $_GET['w'] ) ) {
- $w = (int) $_GET['w'];
- }
</del><span class="cx" style="display: block; padding: 0 10px"> // week_begins = 0 stands for Sunday.
</span><span class="cx" style="display: block; padding: 0 10px"> $week_begins = (int) get_option( 'start_of_week' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre></div>
<a id="trunktestsphpunittestsgeneralgetCalendarphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/general/getCalendar.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/general/getCalendar.php 2025-03-05 22:15:09 UTC (rev 59938)
+++ trunk/tests/phpunit/tests/general/getCalendar.php 2025-03-05 22:36:38 UTC (rev 59939)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -30,17 +30,42 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'post_date' => '2025-02-01 12:00:00',
</span><span class="cx" style="display: block; padding: 0 10px"> )
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ self::factory()->post->create(
+ array(
+ 'post_type' => 'page',
+ 'post_date' => '2025-02-03 12:00:00',
+ )
+ );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Set up for each test.
+ */
+ public function set_up() {
+ parent::set_up();
+
+ /*
+ * Navigate to February 2025.
+ *
+ * All posts within this test suite are published in February 2025,
+ * navigating to the month ensures that the correct month is displayed
+ * in the calendar to allow the assertions to pass.
+ */
+ $this->go_to( '/?m=202502' );
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Test that get_calendar() displays output when display is true.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 34093
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function test_get_calendar_display() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $expected = '<table id="wp-calendar"';
- $actual = get_echo( 'get_calendar', array( array( 'display' => true ) ) );
- $this->assertStringContainsString( $expected, $actual );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $calendar_html = get_echo( 'get_calendar', array( array( 'display' => true ) ) );
+ $this->assertStringContainsString( '<th scope="col" aria-label="Monday">M</th>', $calendar_html, 'Calendar is expected to use initials for day names' );
+ $this->assertStringContainsString( '<table id="wp-calendar"', $calendar_html, 'Calendar is expected to contain the element table#wp-calendar' );
+ $this->assertStringContainsString( 'Posts published on February 1, 2025', $calendar_html, 'Calendar is expected to display posts published on February 1, 2025.' );
+ $this->assertStringContainsString( '<caption>February 2025</caption', $calendar_html, 'Calendar is expected to be captioned February 2025.' );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -49,13 +74,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 34093
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function test_get_calendar_args_filter() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $page_id = self::factory()->post->create(
- array(
- 'post_type' => 'page',
- 'post_date' => '2025-02-03 12:00:00',
- )
- );
-
</del><span class="cx" style="display: block; padding: 0 10px"> add_filter(
</span><span class="cx" style="display: block; padding: 0 10px"> 'get_calendar_args',
</span><span class="cx" style="display: block; padding: 0 10px"> function ( $args ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -66,12 +84,98 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $calendar_html = get_echo( 'get_calendar' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- remove_all_filters( 'get_calendar_args' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertStringContainsString( '<th scope="col" aria-label="Monday">M</th>', $calendar_html, 'Calendar is expected to use initials for day names' );
+ $this->assertStringContainsString( '<table id="wp-calendar"', $calendar_html, 'Calendar is expected to contain the element table#wp-calendar' );
+ $this->assertStringContainsString( 'Posts published on February 3, 2025', $calendar_html, 'Calendar is expected to display page published on February 3, 2025.' );
+ $this->assertStringNotContainsString( 'Posts published on February 1, 2025', $calendar_html, 'Calendar is not expected to display posts published on February 1, 2025.' );
+ $this->assertStringContainsString( '<caption>February 2025</caption', $calendar_html, 'Calendar is expected to be captioned February 2025.' );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertStringContainsString( '<table id="wp-calendar"', $calendar_html );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * Test that get_calendar() respects the args post type parameter.
+ *
+ * @ticket 34093
+ */
+ public function test_get_calendar_post_type_args() {
+ $calendar_html = get_echo( 'get_calendar', array( array( 'post_type' => 'page' ) ) );
+
+ $this->assertStringContainsString( '<th scope="col" aria-label="Monday">M</th>', $calendar_html, 'Calendar is expected to use initials for day names' );
+ $this->assertStringContainsString( '<table id="wp-calendar"', $calendar_html, 'Calendar is expected to contain the element table#wp-calendar' );
+ $this->assertStringContainsString( 'Posts published on February 3, 2025', $calendar_html, 'Calendar is expected to display page published on February 3, 2025.' );
+ $this->assertStringNotContainsString( 'Posts published on February 1, 2025', $calendar_html, 'Calendar is not expected to display posts published on February 1, 2025.' );
+ $this->assertStringContainsString( '<caption>February 2025</caption', $calendar_html, 'Calendar is expected to be captioned February 2025.' );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Test that get_calendar() respects the args initial parameter.
+ *
+ * @ticket 34093
+ */
+ public function test_get_calendar_initial_args() {
+ $first_calendar_html = get_echo( 'get_calendar', array( array( 'initial' => true ) ) );
+ $second_calendar_html = get_echo( 'get_calendar', array( array( 'initial' => false ) ) );
+
+ $this->assertStringContainsString( '<th scope="col" aria-label="Monday">M</th>', $first_calendar_html, 'First calendar is expected to use initials for day names' );
+ $this->assertStringContainsString( '<th scope="col" aria-label="Monday">Mon</th>', $second_calendar_html, 'Second calendar is expected to use abbreviations for day names' );
+ }
+
+ /**
+ * Test that get_calendar() uses a different cache for different arguments.
+ *
+ * @ticket 34093
+ */
+ public function test_get_calendar_caching_accounts_for_args() {
+ $first_calendar_html = get_echo( 'get_calendar' );
+ $second_calendar_html = get_echo( 'get_calendar', array( array( 'post_type' => 'page' ) ) );
+
+ $this->assertNotSame( $first_calendar_html, $second_calendar_html, 'Each calendar should be different' );
+ }
+
+ /**
+ * Test that get_calendar() uses the same cache for equivalent arguments.
+ *
+ * @ticket 34093
+ */
+ public function test_get_calendar_caching_accounts_for_equivalent_args() {
+ get_echo( 'get_calendar', array( array( 'post_type' => 'page' ) ) );
+
+ $num_queries_start = get_num_queries();
+ // Including an argument that is the same as the default value shouldn't miss the cache.
+ get_echo(
+ 'get_calendar',
+ array(
+ array(
+ 'post_type' => 'page',
+ 'initial' => true,
+ ),
+ )
+ );
+
+ // Changing the order of arguments shouldn't miss the cache.
+ get_echo(
+ 'get_calendar',
+ array(
+ array(
+ 'initial' => true,
+ 'post_type' => 'page',
+ ),
+ )
+ );
+
+ // Display param should be ignored for the cache.
+ get_calendar(
+ array(
+ 'post_type' => 'page',
+ 'initial' => true,
+ 'display' => false,
+ )
+ );
+ $num_queries_end = get_num_queries();
+
+ $this->assertSame( 0, $num_queries_end - $num_queries_start, 'Cache should be hit for subsequent equivalent calendar queries.' );
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Test that get_calendar() maintains backwards compatibility with old parameter format.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 34093
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -83,8 +187,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $second_calendar_html = get_calendar( false, false );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertStringContainsString( '<th scope="col" aria-label="Monday">Mon</th>', $first_calendar_html );
- $this->assertStringContainsString( '<table id="wp-calendar"', $second_calendar_html );
- $this->assertStringContainsString( '<th scope="col" aria-label="Monday">Mon</th>', $second_calendar_html );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertStringContainsString( '<th scope="col" aria-label="Monday">Mon</th>', $first_calendar_html, 'Calendar is expected to use abbreviations for day names' );
+ $this->assertStringContainsString( '<caption>February 2025</caption>', $first_calendar_html, 'Calendar is expected to be captioned February 2025' );
+ $this->assertStringContainsString( '<table id="wp-calendar"', $first_calendar_html, 'Calendar is expected to contain the element table#wp-calendar' );
+ $this->assertSame( $first_calendar_html, $second_calendar_html, 'Both calendars should be identical' );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>
</body>
</html>