diff --git a/classes/controllers/FrmStylesController.php b/classes/controllers/FrmStylesController.php index eb039d3788..7c65496990 100644 --- a/classes/controllers/FrmStylesController.php +++ b/classes/controllers/FrmStylesController.php @@ -144,7 +144,7 @@ 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 ); + self::enqueue_saved_css(); $style = apply_filters( 'frm_style_head', false ); @@ -153,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 * @@ -785,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. @@ -1183,6 +1203,7 @@ public static function load_css() { */ public static function load_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; @@ -1217,6 +1238,70 @@ public static function load_saved_css() { wp_die(); } + /** + * 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 + * + * @param bool $from_transient Whether the CSS was read from the transient. + * + * @return void + */ + 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; + } + + header_remove( 'Pragma' ); + + foreach ( $headers as $name => $value ) { + header( $name . ': ' . $value ); + } + } + + /** + * Get the headers that let the browser cache the saved CSS. + * 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( $from_transient ) { + if ( ! $from_transient || ! self::should_cache_saved_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', + ); + } + + /** + * 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/classes/models/FrmStyle.php b/classes/models/FrmStyle.php index e83bc3ba20..cc6fe69200 100644 --- a/classes/models/FrmStyle.php +++ b/classes/models/FrmStyle.php @@ -462,9 +462,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', gmdate( 'YmdHis' ) ); + $version = self::get_next_css_version(); if ( ! is_file( $filename ) ) { + update_option( 'frm_last_style_update', $version ); return; } @@ -476,6 +477,24 @@ public function save_settings() { update_option( 'frm_saved_css_hash', hash( 'sha256', $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 ); + } + + /** + * 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' ); + + 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 5722f9a95e..c1a858429f 100644 --- a/tests/phpunit/styles/test_FrmStyle.php +++ b/tests/phpunit/styles/test_FrmStyle.php @@ -335,14 +335,87 @@ 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 ); + // The version is the current time, or past the previous version after a save in the same second. + $this->assertGreaterThan( $previous, (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.' ); + } + + /** + * 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( 'frm_saved_css_hash', hash( 'sha256', '.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' ), + 'hash' => get_option( 'frm_saved_css_hash' ), + ); + 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->assertSame( hash( 'sha256', $saved_css['transient'] ), $saved_css['hash'], 'The version was updated before the refill hash.' ); + } + + /** + * 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' ) ); } /** diff --git a/tests/phpunit/styles/test_FrmStylesController.php b/tests/phpunit/styles/test_FrmStylesController.php index fa10be463d..bcccc9e804 100644 --- a/tests/phpunit/styles/test_FrmStylesController.php +++ b/tests/phpunit/styles/test_FrmStylesController.php @@ -229,6 +229,128 @@ 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 ); + } + + /** + * 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 ); + } + + /** + * 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'] ); + } + + /** + * 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' ); + + $_GET['ver'] = '20260102030405'; + $headers = $this->run_private_method( $method, array( true ) ); + + $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, array( true ) ), 'An older version should not be cached.' ); + + $_GET['ver'] = '20260102030405'; + $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'] ); + } + /** * @return array */