Make WordPress Core


Ignore:
Timestamp:
03/05/2025 10:36:38 PM (21 months ago)
Author:
peterwilsoncc
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/general-template.php

    r59909 r59939  
    22932293        $args = apply_filters( 'get_calendar_args', wp_parse_args( $args, $defaults ) );
    22942294
    2295         $key   = md5( $m . $monthnum . $year );
     2295        if ( ! post_type_exists( $args['post_type'] ) ) {
     2296                $args['post_type'] = 'post';
     2297        }
     2298
     2299        $w = 0;
     2300        if ( isset( $_GET['w'] ) ) {
     2301                $w = (int) $_GET['w'];
     2302        }
     2303
     2304        /*
     2305         * Normalize the cache key.
     2306         *
     2307         * The following ensures the same cache key is used for the same parameter
     2308         * and parameter equivalents. This prevents `post_type > post, initial > true`
     2309         * from generating a different key from the same values in the reverse order.
     2310         *
     2311         * `display` is excluded from the cache key as the cache contains the same
     2312         * HTML regardless of this functions need to echo or return the output.
     2313         *
     2314         * The global values contain data generated by the URL querystring variables.
     2315         */
     2316        $cache_args = $args;
     2317        unset( $cache_args['display'] );
     2318
     2319        $cache_args['globals'] = array(
     2320                'm'        => $m,
     2321                'monthnum' => $monthnum,
     2322                'year'     => $year,
     2323                'week'     => $w,
     2324        );
     2325
     2326        wp_recursive_ksort( $cache_args );
     2327        $key   = md5( serialize( $cache_args ) );
    22962328        $cache = wp_cache_get( 'get_calendar', 'calendar' );
    22972329
    … …  
    23132345
    23142346        $post_type = $args['post_type'];
    2315         if ( ! post_type_exists( $post_type ) ) {
    2316                 $post_type = 'post';
    2317         }
    23182347
    23192348        // Quick check. If we have no posts at all, abort!
    … …  
    23282357        }
    23292358
    2330         if ( isset( $_GET['w'] ) ) {
    2331                 $w = (int) $_GET['w'];
    2332         }
    23332359        // week_begins = 0 stands for Sunday.
    23342360        $week_begins = (int) get_option( 'start_of_week' );
Note: See TracChangeset for help on using the changeset viewer.