From 286ecd2a1b8e35e0f7b8d645cab1b83a536df1f2 Mon Sep 17 00:00:00 2001 From: Matthew Bernhardt Date: Wed, 30 Apr 2025 11:28:01 -0400 Subject: [PATCH 1/2] Add filter to allow conditional max cache ages ** Why are these changes being introduced: There are several paths which have proven problematic under our current caching setup (where content lasts for a week before re-rendering) 1. The hours grid, which accepts a URL parameter to highligh specific days. 2. The news site's events page 3. The exhibit site's homepage 4. The exhibit site's event index, which contains lists of current, future, and past exhibits. Each of these pages doesn't work well when cached for more than a day, because content can get out of date after 24 hours. ** Relevant ticket(s): * https://mitlibraries.atlassian.net/browse/web-2050 * https://mitlibraries.atlassian.net/browse/web-2052 ** How does this address that need: This adds a function to the parent theme which implements the add_filter command provided by WordPress. This allows us to define a conditional that assigns the cache length according to defined logic - logic which can be pretty flexible in the checks it performs. The default response from this function is the existing cache length of one week. ** Document any side effects to this change: * The function is being defined within the parent theme, which gives it reach across the network only because everything on the network uses either the parent theme or one descended from it. Content within a site other than the parent site is a little awkward to identify, so there is a $site variable defined that seems to use PHP's parse_url to extract the site slug reliably. * Because we are implementing this filter to occasionally define the max cache lifetime, the existing admin UI that also allows site admins to set a default cache length will get disabled. This moves the cache length management firmly into the realm of engineers and version control, and away from site admins. * Code Climate is flagging the next bit of functions.php for a poorly formatted function comment - so I'm fixing it here, even though it isn't strictly necessary. --- web/app/themes/mitlib-parent/functions.php | 30 ++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/web/app/themes/mitlib-parent/functions.php b/web/app/themes/mitlib-parent/functions.php index 89f5abf2..d30bcd5e 100644 --- a/web/app/themes/mitlib-parent/functions.php +++ b/web/app/themes/mitlib-parent/functions.php @@ -111,9 +111,35 @@ function setup_scss_folders() { } /** - * Register and selectively enqueue the scripts and stylesheets needed for this - * page. + * Override max cache age for certain conditions. + * + * The default cache behavior is defined by the combination of two plugins: + * Pantheon MU Plugin and Pantheon Advanced Page Cache. The cache lifetime is + * typically set to 1 week via the former plugin, but certain pages require a + * shorter cache life. This function implements those alternative lifetimes. + * + * @link https://docs.pantheon.io/guides/wordpress-configurations/wordpress-cache-plugin#override-the-default-max-age */ +function override_cache_default_max_age() { + $site = parse_url( get_site_url(), PHP_URL_PATH ); + if ( is_page( 'hours' ) ) { // All hours pages (including those with query parameters). + return 1 * DAY_IN_SECONDS; + } elseif ( '/news' == $site && is_page( 'events' ) ) { // The news site events page. + return 1 * DAY_IN_SECONDS; + } elseif ( '/exhibits' == $site && is_page( '' ) ) { // The exhibits site homepage. + return 1 * DAY_IN_SECONDS; + } elseif ( '/exhibits' == $site && is_page( 'current-upcoming-past-exhibits' ) ) { // The exhibits site composite listing. + return 1 * DAY_IN_SECONDS; + } else { // All other content should be cached for a week. + return 1 * WEEK_IN_SECONDS; + } +} +add_filter( 'pantheon_cache_default_max_age', 'Mitlib\Parent\override_cache_default_max_age' ); + + /** + * Register and selectively enqueue the scripts and stylesheets needed for this + * page. + */ function setup_scripts_styles() { // This allows us to cache-bust these assets without needing to remember to // increment the theme version here. From 5166cd1a39a7a5c2cfd6824c416747e10688418b Mon Sep 17 00:00:00 2001 From: Matthew Bernhardt Date: Wed, 30 Apr 2025 14:43:44 -0400 Subject: [PATCH 2/2] Extend composer multidev prompting by one step ** Why are these changes being introduced: We define some custom Composer commands to remind engineers of the syntax to use for multidev management. One of those commands is focused on the search-replace commands to manipulate database contents, but this workflow has recently found a second step to more accurately manage db contents. Specificially, after the blunt tool to update domain names, there needs to be a second, more targeted, command to revert some email addresses that need to preserved with the original value in order for WordPress to still be able to send emails. ** Relevant ticket(s): n/a ** How does this address that need: This updates the existing multidev-search-replace-syntax command to also provide the suggested syntax for this second command. Like before, this command is only echoed to the terminal, and not run directly. ** Document any side effects to this change: * The search-replace command does not currently adapt to multidevs that have a custom domain assigned (live, test, dev, and stage). That is a known issue with this command, which I've chosen not to try and solve here. --- scripts/composer/class-mitcomposerscripts.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/scripts/composer/class-mitcomposerscripts.php b/scripts/composer/class-mitcomposerscripts.php index f436200f..4734a223 100644 --- a/scripts/composer/class-mitcomposerscripts.php +++ b/scripts/composer/class-mitcomposerscripts.php @@ -63,6 +63,8 @@ public static function multidev_search_replace_syntax( $event ): void { $multidev = self::multidev_name( $event ); $terminus_command = "terminus remote:wp mitlib-wp-network.$multidev -- search-replace libraries.mit.edu $multidev-mitlib-wp-network.pantheonsite.io --url=libraries.mit.edu --network"; $event->getIO()->write( $terminus_command ); + $terminus_command = "terminus remote:wp mitlib-wp-network.$multidev -- search-replace noreply@$multidev-mitlib-wp-network.pantheonsite.io noreply@libraries.mit.edu --network"; + $event->getIO()->write( $terminus_command ); $event->getIO()->write( '-----' ); } }