From d7ed1afa90407dcfc361cc12b4c9ba714148da4c Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Mon, 30 Nov 2026 17:29:27 -0400 Subject: [PATCH 1/5] perf(admin): cache the saved style CSS response The styler loads the CSS for every style from admin-ajax.php with the plugin version, and admin-ajax.php sends no-cache headers, so the full stylesheet was downloaded on every styler load. When the frmpro_css transient expired, the CSS for every style was rebuilt on every request even though the frmpro_css option still held it. Fall back to the frmpro_css option before rebuilding. Version the styler URL by frm_last_style_update, which changes on every style save, and let the browser cache the response for a day only when the requested version matches and the CSS came from the transient. Co-Authored-By: Claude Opus 5.5 --- classes/controllers/FrmStylesController.php | 63 ++++++++++++++++- .../styles/test_FrmStylesController.php | 70 +++++++++++++++++++ 2 files changed, 131 insertions(+), 2 deletions(-) diff --git a/classes/controllers/FrmStylesController.php b/classes/controllers/FrmStylesController.php index 9044c0c5e3..dde0c511cc 100644 --- a/classes/controllers/FrmStylesController.php +++ b/classes/controllers/FrmStylesController.php @@ -144,7 +144,8 @@ public static function admin_init() { wp_enqueue_style( 'formidable-color-picker' ); } - wp_enqueue_style( 'frm-custom-theme', admin_url( 'admin-ajax.php?action=frmpro_css&frm_scope_custom_css=1' ), array(), $version ); + // Version the URL by the last style update so the browser can cache it until a style is saved. + wp_enqueue_style( 'frm-custom-theme', admin_url( 'admin-ajax.php?action=frmpro_css&frm_scope_custom_css=1' ), array(), self::get_css_version( 'formidable', $version ) ); $style = apply_filters( 'frm_style_head', false ); @@ -1182,7 +1183,7 @@ public static function load_css() { * @return void */ public static function load_saved_css() { - $css = get_transient( 'frmpro_css' ); + $css = self::get_saved_css(); ob_start(); include FrmAppHelper::plugin_path() . '/css/custom_theme.css.php'; @@ -1205,6 +1206,64 @@ public static function load_saved_css() { wp_die(); } + /** + * Get the saved CSS for every style. + * + * @since x.x + * + * @return false|string False when the CSS has not been saved and needs to be generated. + */ + private static function get_saved_css() { + $css = get_transient( 'frmpro_css' ); + + if ( $css ) { + self::maybe_send_saved_css_cache_headers(); + return $css; + } + + // The transient expires, but the option is saved with it and does not. + // Fall back to it so the CSS for every style is not rebuilt on each request. + // The option is not sent with cache headers because a style save deletes the transient + // and updates the version before the option, so it may still hold the previous CSS. + return get_option( 'frmpro_css' ); + } + + /** + * Let the browser cache the saved CSS when the request is versioned by the last style update. + * Saving a style changes that version, so the URL changes and the cached copy is no longer used. + * Requests without a matching version keep the no-cache headers sent by admin-ajax.php. + * + * @since x.x + * + * @return void + */ + private static function maybe_send_saved_css_cache_headers() { + if ( ! self::should_cache_saved_css() || headers_sent() ) { + return; + } + + header_remove( 'Pragma' ); + header( 'Cache-Control: private, max-age=' . DAY_IN_SECONDS ); + header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + DAY_IN_SECONDS ) . ' GMT' ); + } + + /** + * Check if the requested CSS version matches the last style update. + * + * @since x.x + * + * @return bool + */ + private static function should_cache_saved_css() { + $last_update = get_option( 'frm_last_style_update' ); + + if ( ! $last_update ) { + return false; + } + + return FrmAppHelper::simple_get( 'ver' ) === (string) $last_update; + } + /** * Add an extra style rule to hide a broken style warning. * To avoid cluttering the front end with any unnecessary styles this is only added when the referer URL matches the styler. diff --git a/tests/phpunit/styles/test_FrmStylesController.php b/tests/phpunit/styles/test_FrmStylesController.php index fa10be463d..e8c0681fe0 100644 --- a/tests/phpunit/styles/test_FrmStylesController.php +++ b/tests/phpunit/styles/test_FrmStylesController.php @@ -229,6 +229,76 @@ public function test_buttons_section_submit_style_toggle_reflects_saved_value( $ $this->assertSame( $expected, str_contains( $matches[0], 'checked' ) ); } + /** + * The styler loads the CSS for every style from admin-ajax.php. Version it by the last style + * update so a style save changes the URL. + * + * @covers FrmStylesController::admin_init + */ + public function test_admin_init_versions_saved_css_by_last_style_update() { + global $pagenow; + $original_pagenow = $pagenow; + $pagenow = 'admin.php'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited + $_GET['page'] = 'formidable-styles'; + update_option( 'frm_last_style_update', '20260102030405' ); + + FrmStylesController::admin_init(); + $registered = wp_styles()->registered['frm-custom-theme'] ?? null; + + wp_dequeue_style( 'frm-custom-theme' ); + wp_deregister_style( 'frm-custom-theme' ); + unset( $_GET['page'] ); + $pagenow = $original_pagenow; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited + + $this->assertNotNull( $registered, 'The saved CSS was not enqueued in the styler.' ); + $this->assertSame( '20260102030405', $registered->ver ); + } + + /** + * When the transient has expired, the saved CSS should come from the option instead of being rebuilt. + * + * @covers FrmStylesController::get_saved_css + */ + public function test_get_saved_css_falls_back_to_option() { + set_transient( 'frmpro_css', '.from-transient{}', MONTH_IN_SECONDS ); + update_option( 'frmpro_css', '.from-option{}', false ); + $this->assertSame( '.from-transient{}', $this->run_private_method( array( 'FrmStylesController', 'get_saved_css' ) ) ); + + delete_transient( 'frmpro_css' ); + $this->assertSame( '.from-option{}', $this->run_private_method( array( 'FrmStylesController', 'get_saved_css' ) ) ); + + delete_option( 'frmpro_css' ); + $this->assertFalse( $this->run_private_method( array( 'FrmStylesController', 'get_saved_css' ) ) ); + } + + /** + * The saved CSS is only cacheable when the request is versioned by the current last style update. + * + * @covers FrmStylesController::should_cache_saved_css + */ + public function test_should_cache_saved_css() { + $method = array( 'FrmStylesController', 'should_cache_saved_css' ); + update_option( 'frm_last_style_update', '20260102030405' ); + + unset( $_GET['ver'] ); + $this->assertFalse( $this->run_private_method( $method ), 'A request without a version should not be cached.' ); + + $_GET['ver'] = '20250101000000'; + $this->assertFalse( $this->run_private_method( $method ), 'A request for an older version should not be cached.' ); + + $_GET['ver'] = FrmAppHelper::plugin_version(); + $this->assertFalse( $this->run_private_method( $method ), 'A request versioned by the plugin version should not be cached.' ); + + $_GET['ver'] = '20260102030405'; + $this->assertTrue( $this->run_private_method( $method ) ); + + delete_option( 'frm_last_style_update' ); + $_GET['ver'] = ''; + $this->assertFalse( $this->run_private_method( $method ), 'Nothing should be cached before a style is saved.' ); + + unset( $_GET['ver'] ); + } + /** * @return array */ From 533c16f3f99b3a390c58c8111e81f71f97d68fa1 Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Mon, 30 Nov 2026 17:56:42 -0400 Subject: [PATCH 2/5] fix: keep the styler CSS version fresh after a save The styler enqueues the saved CSS on admin_init before the style save runs, so the page shown after a save still linked the old version. The browser then served the CSS from before the save out of its cache. Enqueue it again with the new version once the save is done. The version is the save time, which only changes once a second. Two saves in the same second kept the same version while the CSS changed, so a browser could keep stale CSS for a day. A save in the same second as the last one now moves the version one past it. Co-Authored-By: Claude Opus 5.5 --- classes/controllers/FrmStylesController.php | 23 +++++++++- classes/models/FrmStyle.php | 21 ++++++++- tests/phpunit/styles/test_FrmStyle.php | 24 ++++++++++- .../styles/test_FrmStylesController.php | 43 +++++++++++++++++++ 4 files changed, 106 insertions(+), 5 deletions(-) diff --git a/classes/controllers/FrmStylesController.php b/classes/controllers/FrmStylesController.php index dde0c511cc..1800a3ef76 100644 --- a/classes/controllers/FrmStylesController.php +++ b/classes/controllers/FrmStylesController.php @@ -144,8 +144,7 @@ public static function admin_init() { wp_enqueue_style( 'formidable-color-picker' ); } - // Version the URL by the last style update so the browser can cache it until a style is saved. - wp_enqueue_style( 'frm-custom-theme', admin_url( 'admin-ajax.php?action=frmpro_css&frm_scope_custom_css=1' ), array(), self::get_css_version( 'formidable', $version ) ); + self::enqueue_saved_css(); $style = apply_filters( 'frm_style_head', false ); @@ -154,6 +153,19 @@ public static function admin_init() { } } + /** + * Load the saved CSS for every style in the styler. + * + * @since x.x + * + * @return void + */ + private static function enqueue_saved_css() { + // Version the URL by the last style update so the browser can cache it until a style is saved. + $version = self::get_css_version( 'formidable', FrmAppHelper::plugin_version() ); + wp_enqueue_style( 'frm-custom-theme', admin_url( 'admin-ajax.php?action=frmpro_css&frm_scope_custom_css=1' ), array(), $version ); + } + /** * @since 6.0 * @@ -786,6 +798,13 @@ public static function save_style() { $id = $frm_style->update( $post_id ); + // The saved CSS was enqueued earlier on admin_init, before this save changed its version. + // Enqueue it again so the page shown after the save does not load the cached CSS from before it. + if ( wp_style_is( 'frm-custom-theme', 'registered' ) ) { + wp_deregister_style( 'frm-custom-theme' ); + self::enqueue_saved_css(); + } + if ( ! $post_id && $id ) { self::maybe_redirect_after_save( $id ); // Set the post id to the new style so it will be loaded for editing. diff --git a/classes/models/FrmStyle.php b/classes/models/FrmStyle.php index d9b350339a..a99c026f1e 100644 --- a/classes/models/FrmStyle.php +++ b/classes/models/FrmStyle.php @@ -447,7 +447,7 @@ public function get_color_settings() { */ public function save_settings() { $filename = FrmAppHelper::plugin_path() . '/css/custom_theme.css.php'; - update_option( 'frm_last_style_update', gmdate( 'YmdHis' ) ); + update_option( 'frm_last_style_update', self::get_next_css_version() ); if ( ! is_file( $filename ) ) { return; @@ -463,6 +463,25 @@ public function save_settings() { set_transient( 'frmpro_css', $css, MONTH_IN_SECONDS ); } + /** + * Get a new version for the saved CSS. It is in the stylesheet URL, so it must change on every save + * or a browser can keep its cached copy. A save in the same second as the last one moves it one past. + * + * @since x.x + * + * @return string + */ + private static function get_next_css_version() { + $version = gmdate( 'YmdHis' ); + $last_version = (int) get_option( 'frm_last_style_update' ); + + if ( $last_version >= (int) $version ) { + return (string) ( $last_version + 1 ); + } + + return $version; + } + /** * @since 6.32 * diff --git a/tests/phpunit/styles/test_FrmStyle.php b/tests/phpunit/styles/test_FrmStyle.php index 06b7fb6018..0f24faf5a9 100644 --- a/tests/phpunit/styles/test_FrmStyle.php +++ b/tests/phpunit/styles/test_FrmStyle.php @@ -338,13 +338,33 @@ public function test_generated_css_aligns_inline_submit_with_inputs() { * never collide (e.g. 1 Jan 10:59 and 11 Jan 00:59 both produced "111059"). */ public function test_save_settings_stores_padded_timestamp_version() { - $before = gmdate( 'YmdHis' ); + $previous = (int) get_option( 'frm_last_style_update' ); + $before = gmdate( 'YmdHis' ); ( new FrmStyle() )->save_settings(); $after = gmdate( 'YmdHis' ); $version = get_option( 'frm_last_style_update' ); $this->assertMatchesRegularExpression( '/^\d{14}$/', $version ); $this->assertGreaterThanOrEqual( (int) $before, (int) $version ); - $this->assertLessThanOrEqual( (int) $after, (int) $version ); + // A save in the same second as the previous one moves the version one past it. + $this->assertLessThanOrEqual( max( (int) $after, $previous + 1 ), (int) $version ); + } + + /** + * The saved CSS is cached by the browser under its version, so two saves in the same + * second must not share a version. + */ + public function test_save_settings_changes_version_on_every_save() { + $style = new FrmStyle(); + + update_option( 'frm_last_style_update', gmdate( 'YmdHis' ) ); + $first = get_option( 'frm_last_style_update' ); + $style->save_settings(); + $second = get_option( 'frm_last_style_update' ); + $style->save_settings(); + $third = get_option( 'frm_last_style_update' ); + + $this->assertGreaterThan( (int) $first, (int) $second, 'A save in the same second should still change the version.' ); + $this->assertGreaterThan( (int) $second, (int) $third, 'Every save should change the version.' ); } } diff --git a/tests/phpunit/styles/test_FrmStylesController.php b/tests/phpunit/styles/test_FrmStylesController.php index e8c0681fe0..d5a0e0c636 100644 --- a/tests/phpunit/styles/test_FrmStylesController.php +++ b/tests/phpunit/styles/test_FrmStylesController.php @@ -254,6 +254,49 @@ public function test_admin_init_versions_saved_css_by_last_style_update() { $this->assertSame( '20260102030405', $registered->ver ); } + /** + * The styler enqueues the saved CSS on admin_init before a style save changes the version. + * The page shown after the save must load the new version, not the copy the browser cached. + * + * @covers FrmStylesController::save_style + */ + public function test_save_style_enqueues_saved_css_with_new_version() { + global $pagenow; + $this->set_current_user_to_1(); + $original_pagenow = $pagenow; + $pagenow = 'admin.php'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited + $_GET['page'] = 'formidable-styles'; + update_option( 'frm_last_style_update', '20260102030405' ); + + FrmStylesController::admin_init(); + + $style = ( new FrmStyle( 'default' ) )->get_one(); + $_POST = array( + 'ID' => $style->ID, + 'frm_style' => wp_create_nonce( 'frm_style_nonce' ), + 'frm_action' => 'save', + 'frm_style_setting' => array( + 'post_title' => $style->post_title, + 'post_content' => $style->post_content, + ), + ); + + FrmStylesController::save_style(); + $registered = wp_styles()->registered['frm-custom-theme'] ?? null; + $queued = wp_style_is( 'frm-custom-theme' ); + + wp_dequeue_style( 'frm-custom-theme' ); + wp_deregister_style( 'frm-custom-theme' ); + $_POST = array(); + unset( $_GET['page'] ); + $pagenow = $original_pagenow; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited + + $this->assertNotNull( $registered, 'The saved CSS should still be registered after the save.' ); + $this->assertTrue( $queued, 'The saved CSS should still be enqueued after the save.' ); + $this->assertNotSame( '20260102030405', $registered->ver, 'The saved CSS was left on the version from before the save.' ); + $this->assertSame( get_option( 'frm_last_style_update' ), $registered->ver ); + } + /** * When the transient has expired, the saved CSS should come from the option instead of being rebuilt. * From a557889b9ab75b3eeb19b8d03bd4952cbc2746c5 Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Tue, 1 Dec 2026 09:31:15 -0400 Subject: [PATCH 3/5] Write the styler CSS version after the CSS, test cache headers Drop the upper bound in the padded timestamp test, since the version can run ahead of the clock after saves in the same second. Update frm_last_style_update after the CSS option and transient so a request for the new version can never be cached with the old CSS. Move the cache header decision into get_saved_css_cache_headers() so the Cache-Control/Expires values and the option fallback rule are tested. Co-Authored-By: Claude Opus 5.5 --- classes/controllers/FrmStylesController.php | 34 ++++++++++++++--- classes/models/FrmStyle.php | 12 +++--- tests/phpunit/styles/test_FrmStyle.php | 37 +++++++++++++++++-- .../styles/test_FrmStylesController.php | 31 ++++++++++++++++ 4 files changed, 99 insertions(+), 15 deletions(-) diff --git a/classes/controllers/FrmStylesController.php b/classes/controllers/FrmStylesController.php index 1800a3ef76..3ff5a0ddf0 100644 --- a/classes/controllers/FrmStylesController.php +++ b/classes/controllers/FrmStylesController.php @@ -1202,6 +1202,7 @@ public static function load_css() { * @return void */ public static function load_saved_css() { + self::maybe_send_saved_css_cache_headers(); $css = self::get_saved_css(); ob_start(); @@ -1236,14 +1237,11 @@ private static function get_saved_css() { $css = get_transient( 'frmpro_css' ); if ( $css ) { - self::maybe_send_saved_css_cache_headers(); return $css; } // The transient expires, but the option is saved with it and does not. // Fall back to it so the CSS for every style is not rebuilt on each request. - // The option is not sent with cache headers because a style save deletes the transient - // and updates the version before the option, so it may still hold the previous CSS. return get_option( 'frmpro_css' ); } @@ -1257,13 +1255,37 @@ private static function get_saved_css() { * @return void */ private static function maybe_send_saved_css_cache_headers() { - if ( ! self::should_cache_saved_css() || headers_sent() ) { + $headers = self::get_saved_css_cache_headers(); + + if ( ! $headers || headers_sent() ) { return; } header_remove( 'Pragma' ); - header( 'Cache-Control: private, max-age=' . DAY_IN_SECONDS ); - header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + DAY_IN_SECONDS ) . ' GMT' ); + + foreach ( $headers as $name => $value ) { + header( $name . ': ' . $value ); + } + } + + /** + * Get the headers that let the browser cache the saved CSS. + * Only the transient is cached. The option fallback is served while the transient is expired + * or a style save is rebuilding it, so it is not kept by the browser. + * + * @since x.x + * + * @return array Header values keyed by name. Empty when the CSS should not be cached. + */ + private static function get_saved_css_cache_headers() { + if ( ! self::should_cache_saved_css() || ! get_transient( 'frmpro_css' ) ) { + return array(); + } + + return array( + 'Cache-Control' => 'private, max-age=' . DAY_IN_SECONDS, + 'Expires' => gmdate( 'D, d M Y H:i:s', time() + DAY_IN_SECONDS ) . ' GMT', + ); } /** diff --git a/classes/models/FrmStyle.php b/classes/models/FrmStyle.php index a99c026f1e..40f74c4052 100644 --- a/classes/models/FrmStyle.php +++ b/classes/models/FrmStyle.php @@ -447,9 +447,10 @@ public function get_color_settings() { */ public function save_settings() { $filename = FrmAppHelper::plugin_path() . '/css/custom_theme.css.php'; - update_option( 'frm_last_style_update', self::get_next_css_version() ); + $version = self::get_next_css_version(); if ( ! is_file( $filename ) ) { + update_option( 'frm_last_style_update', $version ); return; } @@ -461,6 +462,9 @@ public function save_settings() { update_option( 'frmpro_css', $css, false ); set_transient( 'frmpro_css', $css, MONTH_IN_SECONDS ); + + // Update the version last, so a request for the new version can only get the new CSS. + update_option( 'frm_last_style_update', $version ); } /** @@ -475,11 +479,7 @@ private static function get_next_css_version() { $version = gmdate( 'YmdHis' ); $last_version = (int) get_option( 'frm_last_style_update' ); - if ( $last_version >= (int) $version ) { - return (string) ( $last_version + 1 ); - } - - return $version; + return $last_version >= (int) $version ? (string) ( $last_version + 1 ) : $version; } /** diff --git a/tests/phpunit/styles/test_FrmStyle.php b/tests/phpunit/styles/test_FrmStyle.php index 0f24faf5a9..5fd62f6fba 100644 --- a/tests/phpunit/styles/test_FrmStyle.php +++ b/tests/phpunit/styles/test_FrmStyle.php @@ -341,13 +341,12 @@ public function test_save_settings_stores_padded_timestamp_version() { $previous = (int) get_option( 'frm_last_style_update' ); $before = gmdate( 'YmdHis' ); ( new FrmStyle() )->save_settings(); - $after = gmdate( 'YmdHis' ); $version = get_option( 'frm_last_style_update' ); $this->assertMatchesRegularExpression( '/^\d{14}$/', $version ); $this->assertGreaterThanOrEqual( (int) $before, (int) $version ); - // A save in the same second as the previous one moves the version one past it. - $this->assertLessThanOrEqual( max( (int) $after, $previous + 1 ), (int) $version ); + // The version is the current time, or past the previous version after a save in the same second. + $this->assertGreaterThan( $previous, (int) $version ); } /** @@ -367,4 +366,36 @@ public function test_save_settings_changes_version_on_every_save() { $this->assertGreaterThan( (int) $first, (int) $second, 'A save in the same second should still change the version.' ); $this->assertGreaterThan( (int) $second, (int) $third, 'Every save should change the version.' ); } + + /** + * The browser caches the saved CSS under its version, so the version must only change + * once the new CSS is saved. Otherwise a request for the new version can get the old CSS. + */ + public function test_save_settings_updates_version_after_css() { + set_transient( 'frmpro_css', '.stale{}', MONTH_IN_SECONDS ); + update_option( 'frmpro_css', '.stale{}', false ); + + $saved_css = array(); + $record = function ( $value ) use ( &$saved_css ) { + if ( $saved_css ) { + // Only the first write matters. A later one cannot undo a request that already got the old CSS. + return $value; + } + + $saved_css = array( + 'transient' => get_transient( 'frmpro_css' ), + 'option' => get_option( 'frmpro_css' ), + ); + return $value; + }; + add_filter( 'pre_update_option_frm_last_style_update', $record ); + + ( new FrmStyle() )->save_settings(); + remove_filter( 'pre_update_option_frm_last_style_update', $record ); + + $this->assertNotEmpty( $saved_css, 'The version was not updated.' ); + $this->assertNotSame( '.stale{}', $saved_css['transient'], 'The version was updated before the CSS transient.' ); + $this->assertSame( get_transient( 'frmpro_css' ), $saved_css['transient'] ); + $this->assertNotSame( '.stale{}', $saved_css['option'], 'The version was updated before the CSS option.' ); + } } diff --git a/tests/phpunit/styles/test_FrmStylesController.php b/tests/phpunit/styles/test_FrmStylesController.php index d5a0e0c636..582f1a0162 100644 --- a/tests/phpunit/styles/test_FrmStylesController.php +++ b/tests/phpunit/styles/test_FrmStylesController.php @@ -342,6 +342,37 @@ public function test_should_cache_saved_css() { unset( $_GET['ver'] ); } + /** + * Cache headers are only sent for the current version, and only when the CSS comes from the transient. + * + * @covers FrmStylesController::get_saved_css_cache_headers + */ + public function test_get_saved_css_cache_headers() { + $method = array( 'FrmStylesController', 'get_saved_css_cache_headers' ); + update_option( 'frm_last_style_update', '20260102030405' ); + set_transient( 'frmpro_css', '.from-transient{}', MONTH_IN_SECONDS ); + update_option( 'frmpro_css', '.from-option{}', false ); + + $_GET['ver'] = '20260102030405'; + $headers = $this->run_private_method( $method ); + + $this->assertSame( 'private, max-age=' . DAY_IN_SECONDS, $headers['Cache-Control'] ?? '' ); + $expires = strtotime( $headers['Expires'] ?? '' ); + $this->assertNotFalse( $expires, 'The Expires header is not a valid date.' ); + $this->assertEqualsWithDelta( time() + DAY_IN_SECONDS, $expires, 5 ); + + $_GET['ver'] = '20250101000000'; + $this->assertSame( array(), $this->run_private_method( $method ), 'An older version should not be cached.' ); + + $_GET['ver'] = '20260102030405'; + delete_transient( 'frmpro_css' ); + $this->assertSame( '.from-option{}', $this->run_private_method( array( 'FrmStylesController', 'get_saved_css' ) ) ); + $this->assertSame( array(), $this->run_private_method( $method ), 'The option fallback should not be cached.' ); + + unset( $_GET['ver'] ); + delete_option( 'frmpro_css' ); + } + /** * @return array */ From 6899d450b24301b4a99972e618b23af62ca8e9b3 Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Tue, 1 Dec 2026 12:30:46 -0400 Subject: [PATCH 4/5] Test the style version update without the CSS template Co-Authored-By: Claude Opus 5.5 --- tests/phpunit/styles/test_FrmStyle.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/phpunit/styles/test_FrmStyle.php b/tests/phpunit/styles/test_FrmStyle.php index 5fd62f6fba..d079466007 100644 --- a/tests/phpunit/styles/test_FrmStyle.php +++ b/tests/phpunit/styles/test_FrmStyle.php @@ -398,4 +398,26 @@ public function test_save_settings_updates_version_after_css() { $this->assertSame( get_transient( 'frmpro_css' ), $saved_css['transient'] ); $this->assertNotSame( '.stale{}', $saved_css['option'], 'The version was updated before the CSS option.' ); } + + /** + * Without the CSS template there is no CSS to save, but the version still changes + * so a stylesheet URL never keeps a version the browser may have cached. + */ + public function test_save_settings_updates_version_without_css_template() { + $template = FrmAppHelper::plugin_path() . '/css/custom_theme.css.php'; + $moved = $template . '.bak'; + $previous = gmdate( 'YmdHis' ); + + update_option( 'frm_last_style_update', $previous ); + $this->assertTrue( rename( $template, $moved ) ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.file_ops_rename + + try { + ( new FrmStyle() )->save_settings(); + } finally { + rename( $moved, $template ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.file_ops_rename + } + + $this->assertFileExists( $template ); + $this->assertGreaterThan( (int) $previous, (int) get_option( 'frm_last_style_update' ) ); + } } From 06c65c88dbc73303bd0771c8ba6c563c6ba798d0 Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Tue, 1 Dec 2026 14:54:14 -0400 Subject: [PATCH 5/5] Drop the frmpro_css option fallback now that the cache is transient-only The frmpro_css option was removed on master, so the saved CSS comes from the transient or the refill. Only a transient hit is sent cache headers, since a refill can return CSS that does not match the last save. Co-Authored-By: Claude Opus 5.5 --- classes/controllers/FrmStylesController.php | 39 ++++++------------- tests/phpunit/styles/test_FrmStyle.php | 6 +-- .../styles/test_FrmStylesController.php | 28 ++----------- 3 files changed, 18 insertions(+), 55 deletions(-) diff --git a/classes/controllers/FrmStylesController.php b/classes/controllers/FrmStylesController.php index 2a33de2ba3..7c65496990 100644 --- a/classes/controllers/FrmStylesController.php +++ b/classes/controllers/FrmStylesController.php @@ -1202,8 +1202,8 @@ public static function load_css() { * @return void */ public static function load_saved_css() { - self::maybe_send_saved_css_cache_headers(); - $css = self::get_saved_css(); + $css = get_transient( 'frmpro_css' ); + self::maybe_send_saved_css_cache_headers( false !== $css ); // Read by custom_theme.css.php. $merge_style_rules = true; @@ -1238,25 +1238,6 @@ public static function load_saved_css() { wp_die(); } - /** - * Get the saved CSS for every style. - * - * @since x.x - * - * @return false|string False when the CSS has not been saved and needs to be generated. - */ - private static function get_saved_css() { - $css = get_transient( 'frmpro_css' ); - - if ( $css ) { - return $css; - } - - // The transient expires, but the option is saved with it and does not. - // Fall back to it so the CSS for every style is not rebuilt on each request. - return get_option( 'frmpro_css' ); - } - /** * Let the browser cache the saved CSS when the request is versioned by the last style update. * Saving a style changes that version, so the URL changes and the cached copy is no longer used. @@ -1264,10 +1245,12 @@ private static function get_saved_css() { * * @since x.x * + * @param bool $from_transient Whether the CSS was read from the transient. + * * @return void */ - private static function maybe_send_saved_css_cache_headers() { - $headers = self::get_saved_css_cache_headers(); + private static function maybe_send_saved_css_cache_headers( $from_transient ) { + $headers = self::get_saved_css_cache_headers( $from_transient ); if ( ! $headers || headers_sent() ) { return; @@ -1282,15 +1265,17 @@ private static function maybe_send_saved_css_cache_headers() { /** * Get the headers that let the browser cache the saved CSS. - * Only the transient is cached. The option fallback is served while the transient is expired - * or a style save is rebuilding it, so it is not kept by the browser. + * Only a transient hit is cached. When the transient is empty the CSS is refilled or rebuilt, + * and a refill whose hash does not match the last save is not the saved CSS, so it is not kept by the browser. * * @since x.x * + * @param bool $from_transient Whether the CSS was read from the transient. + * * @return array Header values keyed by name. Empty when the CSS should not be cached. */ - private static function get_saved_css_cache_headers() { - if ( ! self::should_cache_saved_css() || ! get_transient( 'frmpro_css' ) ) { + private static function get_saved_css_cache_headers( $from_transient ) { + if ( ! $from_transient || ! self::should_cache_saved_css() ) { return array(); } diff --git a/tests/phpunit/styles/test_FrmStyle.php b/tests/phpunit/styles/test_FrmStyle.php index 1fc5efcbfa..c1a858429f 100644 --- a/tests/phpunit/styles/test_FrmStyle.php +++ b/tests/phpunit/styles/test_FrmStyle.php @@ -370,7 +370,7 @@ public function test_save_settings_changes_version_on_every_save() { */ public function test_save_settings_updates_version_after_css() { set_transient( 'frmpro_css', '.stale{}', MONTH_IN_SECONDS ); - update_option( 'frmpro_css', '.stale{}', false ); + update_option( 'frm_saved_css_hash', hash( 'sha256', '.stale{}' ), false ); $saved_css = array(); $record = function ( $value ) use ( &$saved_css ) { @@ -381,7 +381,7 @@ public function test_save_settings_updates_version_after_css() { $saved_css = array( 'transient' => get_transient( 'frmpro_css' ), - 'option' => get_option( 'frmpro_css' ), + 'hash' => get_option( 'frm_saved_css_hash' ), ); return $value; }; @@ -393,7 +393,7 @@ public function test_save_settings_updates_version_after_css() { $this->assertNotEmpty( $saved_css, 'The version was not updated.' ); $this->assertNotSame( '.stale{}', $saved_css['transient'], 'The version was updated before the CSS transient.' ); $this->assertSame( get_transient( 'frmpro_css' ), $saved_css['transient'] ); - $this->assertNotSame( '.stale{}', $saved_css['option'], 'The version was updated before the CSS option.' ); + $this->assertSame( hash( 'sha256', $saved_css['transient'] ), $saved_css['hash'], 'The version was updated before the refill hash.' ); } /** diff --git a/tests/phpunit/styles/test_FrmStylesController.php b/tests/phpunit/styles/test_FrmStylesController.php index 582f1a0162..bcccc9e804 100644 --- a/tests/phpunit/styles/test_FrmStylesController.php +++ b/tests/phpunit/styles/test_FrmStylesController.php @@ -297,23 +297,6 @@ public function test_save_style_enqueues_saved_css_with_new_version() { $this->assertSame( get_option( 'frm_last_style_update' ), $registered->ver ); } - /** - * When the transient has expired, the saved CSS should come from the option instead of being rebuilt. - * - * @covers FrmStylesController::get_saved_css - */ - public function test_get_saved_css_falls_back_to_option() { - set_transient( 'frmpro_css', '.from-transient{}', MONTH_IN_SECONDS ); - update_option( 'frmpro_css', '.from-option{}', false ); - $this->assertSame( '.from-transient{}', $this->run_private_method( array( 'FrmStylesController', 'get_saved_css' ) ) ); - - delete_transient( 'frmpro_css' ); - $this->assertSame( '.from-option{}', $this->run_private_method( array( 'FrmStylesController', 'get_saved_css' ) ) ); - - delete_option( 'frmpro_css' ); - $this->assertFalse( $this->run_private_method( array( 'FrmStylesController', 'get_saved_css' ) ) ); - } - /** * The saved CSS is only cacheable when the request is versioned by the current last style update. * @@ -350,11 +333,9 @@ public function test_should_cache_saved_css() { public function test_get_saved_css_cache_headers() { $method = array( 'FrmStylesController', 'get_saved_css_cache_headers' ); update_option( 'frm_last_style_update', '20260102030405' ); - set_transient( 'frmpro_css', '.from-transient{}', MONTH_IN_SECONDS ); - update_option( 'frmpro_css', '.from-option{}', false ); $_GET['ver'] = '20260102030405'; - $headers = $this->run_private_method( $method ); + $headers = $this->run_private_method( $method, array( true ) ); $this->assertSame( 'private, max-age=' . DAY_IN_SECONDS, $headers['Cache-Control'] ?? '' ); $expires = strtotime( $headers['Expires'] ?? '' ); @@ -362,15 +343,12 @@ public function test_get_saved_css_cache_headers() { $this->assertEqualsWithDelta( time() + DAY_IN_SECONDS, $expires, 5 ); $_GET['ver'] = '20250101000000'; - $this->assertSame( array(), $this->run_private_method( $method ), 'An older version should not be cached.' ); + $this->assertSame( array(), $this->run_private_method( $method, array( true ) ), 'An older version should not be cached.' ); $_GET['ver'] = '20260102030405'; - delete_transient( 'frmpro_css' ); - $this->assertSame( '.from-option{}', $this->run_private_method( array( 'FrmStylesController', 'get_saved_css' ) ) ); - $this->assertSame( array(), $this->run_private_method( $method ), 'The option fallback should not be cached.' ); + $this->assertSame( array(), $this->run_private_method( $method, array( false ) ), 'CSS refilled or rebuilt after a transient miss should not be cached.' ); unset( $_GET['ver'] ); - delete_option( 'frmpro_css' ); } /**