Changeset 3132786
- Timestamp:
- 08/08/2024 03:11:05 PM (20 months ago)
- Location:
- pantheon-advanced-page-cache
- Files:
-
- 18 added
- 4 edited
-
tags/2.1.0 (added)
-
tags/2.1.0/LICENSE (added)
-
tags/2.1.0/assets (added)
-
tags/2.1.0/assets/css (added)
-
tags/2.1.0/assets/css/styles.css (added)
-
tags/2.1.0/assets/css/styles.min.css (added)
-
tags/2.1.0/assets/sass (added)
-
tags/2.1.0/assets/sass/main.scss (added)
-
tags/2.1.0/assets/sass/mixins.scss (added)
-
tags/2.1.0/assets/sass/styles.scss (added)
-
tags/2.1.0/inc (added)
-
tags/2.1.0/inc/admin-interface.php (added)
-
tags/2.1.0/inc/class-cli.php (added)
-
tags/2.1.0/inc/class-emitter.php (added)
-
tags/2.1.0/inc/class-purger.php (added)
-
tags/2.1.0/inc/class-user-interface.php (added)
-
tags/2.1.0/pantheon-advanced-page-cache.php (added)
-
tags/2.1.0/readme.txt (added)
-
trunk/inc/admin-interface.php (modified) (4 diffs)
-
trunk/inc/class-purger.php (modified) (2 diffs)
-
trunk/pantheon-advanced-page-cache.php (modified) (2 diffs)
-
trunk/readme.txt (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
pantheon-advanced-page-cache/trunk/inc/admin-interface.php
r3094130 r3132786 43 43 add_filter( 'pantheon_cache_max_age_input', __NAMESPACE__ . '\\update_default_ttl_input' ); 44 44 add_filter( 'pantheon_cache_max_age_input_allowed_html', __NAMESPACE__ . '\\max_age_input_allowed_html' ); 45 add_ filter( 'nonce_life', __NAMESPACE__ . '\\filter_nonce_cache_lifetime' );45 add_action( 'pantheon_cache_nonce_lifetime', __NAMESPACE__ . '\\filter_nonce_cache_lifetime' ); 46 46 } 47 47 … … 88 88 89 89 /** 90 * Get the callback(s) hooked to pantheon_cache_default_max_age, if one exists. 91 * 92 * @since 2.1.0 93 * @return string 94 */ 95 function get_pantheon_cache_filter_callback() { 96 global $wp_filter; 97 $hook = 'pantheon_cache_default_max_age'; 98 $output = ''; 99 100 if ( ! has_filter( $hook ) ) { 101 return $output; 102 } 103 104 $callback_functions = []; 105 if ( isset( $wp_filter[ $hook ] ) ) { 106 foreach ( $wp_filter[ $hook ]->callbacks as $callbacks ) { 107 foreach ( $callbacks as $callback ) { 108 if ( is_string( $callback['function'] ) ) { 109 // Function name. 110 $callback_functions[] = $callback['function']; 111 } elseif ( is_array( $callback['function'] ) ) { 112 // Method call. 113 $class = is_object( $callback['function'][0] ) ? get_class( $callback['function'][0] ) : $callback['function'][0]; 114 $method = $callback['function'][1]; 115 $callback_functions[] = "$class::$method"; 116 } else { 117 $callback_functions[] = __( 'an anonymous function', 'pantheon-advanced-page-cache' ); 118 } 119 } 120 } 121 } 122 123 // Count the callbacks and if there's only one, return the name (if able). 124 $callbacks_count = count( $callback_functions ); 125 if ( $callbacks_count === 1 ) { 126 return stripos( $callback_functions[0], 'an anonymous function' ) === false ? "<code>{$callback_functions[0]}<code>" : $callback_functions[0]; 127 } 128 129 // If there are multiple callbacks, format the output. 130 foreach ( $callback_functions as $index => $callback ) { 131 $callback = stripos( $callback, 'anonymous' ) !== false ? $callback : "<code>$callback</code>"; 132 $output .= $index === $callbacks_count - 1 ? __( 'and', 'pantheon-advanced-page-cache' ) . ' ' . $callback : $callback . ', '; 133 } 134 135 return $output; 136 } 137 138 /** 90 139 * Add a description to the max-age setting field. 91 140 * … … 95 144 function add_max_age_setting_description() { 96 145 $is_filtered = has_filter( 'pantheon_cache_default_max_age' ); 146 $filter_callback = get_pantheon_cache_filter_callback(); 147 $filtered_message = ''; 97 148 $above_recommended_message = __( 'Your cache maximum age is currently <strong>above</strong> the recommended value.', 'pantheon-advanced-page-cache' ); 98 149 $below_recommended_message = __( 'Your cache maximum age is currently <strong>below</strong> the recommended value.', 'pantheon-advanced-page-cache' ); 99 150 $recommended_message = __( 'Your cache maximum age is currently set to the recommended value.', 'pantheon-advanced-page-cache' ); 100 151 $recommendation_message = get_current_max_age() > WEEK_IN_SECONDS ? $above_recommended_message : ( get_current_max_age() < WEEK_IN_SECONDS ? $below_recommended_message : $recommended_message ); 101 $filtered_message = $is_filtered ? sprintf( 102 // translators: %s is the humanized max-age. 103 __( 'This value has been hardcoded to %s via a filter.', 'pantheon-advanced-page-cache' ), 104 '<strong>' . humanized_max_age() . '</strong>' 105 ) : ''; 152 153 if ( $is_filtered ) { 154 // Set the message to name the callback(s). 155 $filtered_message = ! empty( $filter_callback ) ? sprintf( 156 // translators: %1$s is the humanized max-age, %2$s is the callback function(s). 157 __( 'This value has been hardcoded to %1$s via a filter hooked to %2$s in your code.', 'pantheon-advanced-page-cache' ), 158 '<strong>' . humanized_max_age() . '</strong>', 159 $filter_callback 160 ) : sprintf( 161 // translators: %s is the humanized max-age. 162 __( 'This value has been hardcoded to %s via a filter.', 'pantheon-advanced-page-cache' ), 163 '<strong>' . humanized_max_age() . '</strong>' 164 ); // If there's no callback, we'll just note that it's been hardcoded. This shouldn't ever happen. 165 } 166 106 167 $pantheon_cache = get_option( 'pantheon-cache', [] ); 107 168 $has_custom_ttl = isset( $pantheon_cache['default_ttl'] ) && ! array_key_exists( $pantheon_cache['default_ttl'], max_age_options() ); … … 588 649 } 589 650 590 /** 591 * Filter the nonce cache lifetime. 592 * 593 * @param int $lifetime The lifetime of the nonce. 594 * 595 * @since 2.0.0 596 * @return int 597 */ 598 function filter_nonce_cache_lifetime( $lifetime ) { 651 652 /** 653 * Filter the cache lifetime for nonces. 654 * 655 * Hooked to pantheon_cache_nonce_lifetime action. Use this to filter the cache lifetime for nonces using the action, e.g.: 656 * 657 * do_action( 'pantheon_cache_nonce_lifetime' ); 658 * 659 * @since 2.0.0 660 * @return void 661 */ 662 function filter_nonce_cache_lifetime() { 599 663 // Bail early if we're in the admin. 600 664 if ( is_admin() ) { 601 return $lifetime;665 return; 602 666 } 603 667 604 668 // Filter the cache default max age to less than the nonce lifetime when creating nonces on the front-end. This prevents the cache from keeping the nonce around longer than it should. 605 add_filter( 'pantheon_cache_default_max_age', function () use ( $lifetime ) { 669 add_filter( 'pantheon_cache_default_max_age', function () { 670 $lifetime = apply_filters( 'nonce_life', DAY_IN_SECONDS ); 606 671 return $lifetime - HOUR_IN_SECONDS; 607 672 } ); 608 609 return $lifetime; 610 } 673 } -
pantheon-advanced-page-cache/trunk/inc/class-purger.php
r3049387 r3132786 72 72 * @param array $ignored_post_types Post types to ignore. 73 73 * @return array 74 * @since 1.5.0 -dev74 * @since 1.5.0 75 75 */ 76 76 $ignored_post_types = apply_filters( 'pantheon_purge_post_type_ignored', [ 'revision' ] ); … … 249 249 * @param array $ignored_post_types Post types to ignore. 250 250 * @return array 251 * @since 1.5.0 -dev251 * @since 1.5.0 252 252 */ 253 253 $ignored_post_types = apply_filters( 'pantheon_purge_post_type_ignored', [ 'revision' ] ); -
pantheon-advanced-page-cache/trunk/pantheon-advanced-page-cache.php
r3094130 r3132786 8 8 * Text Domain: pantheon-advanced-page-cache 9 9 * Domain Path: /languages 10 * Version: 2. 0.010 * Version: 2.1.0 11 11 * Requires at least: 6.4 12 * Tested up to: 6. 5.312 * Tested up to: 6.6.1 13 13 * 14 14 * @package Pantheon_Advanced_Page_Cache … … 148 148 } 149 149 ); 150 151 /** 152 * Init namespaced files. 153 */ 154 add_action( 'plugins_loaded', 'pantheon_bootstrap_namespaces' ); 150 155 151 156 /** -
pantheon-advanced-page-cache/trunk/readme.txt
r3094179 r3132786 3 3 Tags: pantheon, cdn, cache 4 4 Requires at least: 6.4 5 Tested up to: 6. 5.36 Stable tag: 2. 0.05 Tested up to: 6.6.1 6 Stable tag: 2.1.0 7 7 License: GPLv2 or later 8 8 License URI: http://www.gnu.org/licenses/gpl-2.0.html … … 134 134 = Ignoring Specific Post Types = 135 135 136 By default, Pantheon Advanced Page Cache is pretty aggressive in how it clears its surrogate keys. Specifically, any time `wp_insert_post` is called (which can include any time a post of any type is added or updated, even private post types), it will purge a variety of keys including `home`, `front`, `404` and `feed`. To bypass or override this behavior, since 1.5.0 -devwe have a filter allowing an array of post types to ignore to be passed before those caches are purged. By default, the `revision` post type is ignored, but others can be added:136 By default, Pantheon Advanced Page Cache is pretty aggressive in how it clears its surrogate keys. Specifically, any time `wp_insert_post` is called (which can include any time a post of any type is added or updated, even private post types), it will purge a variety of keys including `home`, `front`, `404` and `feed`. To bypass or override this behavior, since 1.5.0 we have a filter allowing an array of post types to ignore to be passed before those caches are purged. By default, the `revision` post type is ignored, but others can be added: 137 137 138 138 /** … … 161 161 When the cache max age is filtered in this way, the admin option is disabled and a notice is displayed. 162 162 163 = Setting the Cache Max Age with a filter = 164 165 The cache max age setting is controlled by the [Pantheon Page Cache](https://docs.pantheon.io/guides/wordpress-configurations/wordpress-cache-plugin) admin page. As of 2.0.0, there are three cache age options by default — 1 week, 1 month, 1 year. Pantheon Advanced Page Cache automatically purges the cache of updated and related posts and pages, but you might want to override the cache max age value and set it programmatically. In this case, you can use the `pantheon_cache_default_max_age` filter added in [Pantheon MU plugin 1.4.0+](https://docs.pantheon.io/guides/wordpress-configurations/wordpress-cache-plugin#override-the-default-max-age). For example: 166 167 add_filter( 'pantheon_cache_default_max_age', function() { 168 return 10 * DAY_IN_SECONDS; 169 } ); 170 171 When the cache max age is filtered in this way, the admin option is disabled and a notice is displayed. 172 173 = Setting the Cache Max Age with a filter = 174 175 The cache max age setting is controlled by the [Pantheon Page Cache](https://docs.pantheon.io/guides/wordpress-configurations/wordpress-cache-plugin) admin page. As of 2.0.0, there are three cache age options by default — 1 week, 1 month, 1 year. Pantheon Advanced Page Cache automatically purges the cache of updated and related posts and pages, but you might want to override the cache max age value and set it programmatically. In this case, you can use the `pantheon_cache_default_max_age` filter added in [Pantheon MU plugin 1.4.0+](https://docs.pantheon.io/guides/wordpress-configurations/wordpress-cache-plugin#override-the-default-max-age). For example: 176 177 add_filter( 'pantheon_cache_default_max_age', function() { 178 return 10 * DAY_IN_SECONDS; 179 } ); 180 181 When the cache max age is filtered in this way, the admin option is disabled and a notice is displayed. 163 = Updating the cache max age based on nonces = 164 165 Nonces created on the front-end, often used to secure forms and other data, have a lifetime, and if the cache max age is longer than the nonce lifetime, the nonce may expire before the cache does. To avoid this, you can use the `pantheon_cache_nonce_lifetime` action to set the `pantheon_cache_default_max_age` to less than the nonce lifetime. For example: 166 167 do_action( 'pantheon_cache_nonce_lifetime' ); 168 169 It's important to wrap your `do_action` in the appropriate conditionals to ensure that the action is only called when necessary and not filtering the cache max age in cases when it's not necessary. This might mean only running on certain pages or in certain contexts in your code. 182 170 183 171 == WP-CLI Commands == … … 354 342 == Other Filters == 355 343 356 = `pantheon_apc_disable_admin_notices`=344 = pantheon_apc_disable_admin_notices = 357 345 Since 2.0.0, Pantheon Advanced Page Cache displays a number of admin notices about your current cache max age value. You can disable these notices with the `pantheon_apc_disable_admin_notices` filter. 358 346 … … 370 358 The above example would disable _only_ the admin notice recommending a higher cache max age. 371 359 372 == Other Filters ==373 374 = pantheon_apc_disable_admin_notices =375 Since 2.0.0, Pantheon Advanced Page Cache displays a number of admin notices about your current cache max age value. You can disable these notices with the `pantheon_apc_disable_admin_notices` filter.376 377 add_filter( 'pantheon_apc_disable_admin_notices', '__return_true' );378 379 Alternately, the function callback is passed into the `pantheon_apc_disable_admin_notices` filter, allowing you to specify precisely _which_ notice to disable, for example:380 381 add_filter( 'pantheon_apc_disable_admin_notices', function( $disable_notices, $callback ) {382 if ( $callback === '\\Pantheon_Advanced_Page_Cache\\Admin_Interface\\admin_notice_maybe_recommend_higher_max_age' ) {383 return true;384 }385 return $disable_notices;386 }, 10, 2 );387 388 The above example would disable _only_ the admin notice recommending a higher cache max age.389 390 == Other Filters ==391 392 = pantheon_apc_disable_admin_notices =393 Since 2.0.0, Pantheon Advanced Page Cache displays a number of admin notices about your current cache max age value. You can disable these notices with the `pantheon_apc_disable_admin_notices` filter.394 395 add_filter( 'pantheon_apc_disable_admin_notices', '__return_true' );396 397 Alternately, the function callback is passed into the `pantheon_apc_disable_admin_notices` filter, allowing you to specify precisely _which_ notice to disable, for example:398 399 add_filter( 'pantheon_apc_disable_admin_notices', function( $disable_notices, $callback ) {400 if ( $callback === '\\Pantheon_Advanced_Page_Cache\\Admin_Interface\\admin_notice_maybe_recommend_higher_max_age' ) {401 return true;402 }403 return $disable_notices;404 }, 10, 2 );405 406 The above example would disable _only_ the admin notice recommending a higher cache max age.407 408 360 == Plugin Integrations == 409 361 … … 417 369 418 370 == Changelog == 371 = 2.1.0 (8 August 2024) = 372 * Adds any callable functions hooked to the `pantheon_cache_default_max_age` filter to the message that displays in the WordPress admin when a cache max age filter is active. [[#292](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/292)] This gives some context to troubleshoot if the filter is active somewhere in the codebase. If an anonymous function is used, it is noted in the message that displays. 373 * Removes the hook to `nonce_life` and replaces it with a new action (`pantheon_cache_nonce_lifetime`, see [documentation](https://github.com/pantheon-systems/pantheon-advanced-page-cache?tab=readme-ov-file#updating-the-cache-max-age-based-on-nonces)). [[#293](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/293)] This was erroneously overriding any admin settings and setting the default cache max age for some sites to always be 23 hours (the nonce lifetime minus 1 hour). This solution requires that developers add the `do_action` when they are creating nonces on the front-end, but allows the cache settings to work as designed in all other instances. 374 419 375 = 2.0.0 (28 May 2024) = 420 376 * Adds new admin alerts and Site Health tests about default cache max age settings and recommendations [[#268](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/268), [#271](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/271)]. The default Pantheon GCDN cache max age value has been updated to 1 week in the [Pantheon MU plugin](https://github.com/pantheon-systems/pantheon-mu-plugin). For more information, see the [release note](https://docs.pantheon.io/release-notes/2024/04/pantheon-mu-plugin-1-4-0-update). … … 507 463 = 1.3.0 = 508 464 = 1.3.0 = 509 This version also automatically updates the cache max age (set in the [Pantheon Page Cache settings](https://docs.pantheon.io/guides/wordpress-configurations/wordpress-cache-plugin)) to the recommended value (1 week) if it was saved at the old default value (600 seconds). If the cache max age was set to any other value (or not set at all), it will not be changed. A one-time notice will be displayed in the admin interface to inform administrators of this change.510 511 = 1.3.0 =512 465 Note that the Pantheon Advanced Page Cache 1.3.0 release now prefixes keys on a WordPress Multisite (WPMS) with the blog ID. For users who already have this plugin installed on a WPMS, they will need to click the Clear Cache button on the settings page to generate the prefixed keys.
Note: See TracChangeset
for help on using the changeset viewer.