Repository navigation
Make the Visual Styler preview match the front end #3701
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
11c8ee9
68e86cc
31c1008
b02ea6c
8b798b0
38e8e9c
faa0609
1cb98fe
5676ccd
9f3f4b2
43567bb
89b9caa
3f4a076
21aab9d
426cff3
213aa85
81252f9
af136ec
fa01958
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 ); | ||
| wp_enqueue_style( 'frm-custom-theme', admin_url( 'admin-ajax.php?action=frmpro_css&frm_scope_custom_css=1' ), array(), self::get_scoped_css_version() ); | ||
| self::enqueue_preview_theme_css(); | ||
|
|
||
| $style = apply_filters( 'frm_style_head', false ); | ||
|
|
||
|
|
@@ -153,6 +154,149 @@ public static function admin_init() { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Version the styler's copy of the saved stylesheet by the last style save and the global | ||
| * custom CSS, so the browser can keep it until either one changes. | ||
| * | ||
| * @since x.x | ||
| * | ||
| * @return string | ||
| */ | ||
| private static function get_scoped_css_version() { | ||
| $last_update = get_option( 'frm_last_style_update' ); | ||
| $custom_css = self::get_custom_css(); | ||
| $fingerprint = substr( hash( 'sha256', is_string( $custom_css ) ? $custom_css : '' ), 0, 12 ); | ||
|
|
||
| return FrmAppHelper::plugin_version() . '-' . ( $last_update ? $last_update : '0' ) . '-' . $fingerprint; | ||
| } | ||
|
|
||
| /** | ||
| * Style the styler preview with the active theme's front end CSS, scoped to the preview. | ||
| * | ||
| * Without this the preview inherits wp-admin typography, so a form that looks right on the | ||
| * front end can look different in the styler. | ||
| * | ||
| * @since x.x | ||
| * | ||
| * @return void | ||
| */ | ||
| private static function enqueue_preview_theme_css() { | ||
| // Undo the wp-admin element styles inside the preview. The theme CSS depends on this so it loads after it and wins. | ||
| wp_enqueue_style( 'frm-styler-preview', FrmAppHelper::plugin_url() . '/css/admin/styler-preview.css', array( 'frm-custom-theme' ), FrmAppHelper::plugin_version() ); | ||
|
|
||
| /** | ||
| * Allow the active theme's CSS to be kept out of the Visual Styler preview. | ||
| * | ||
| * @since x.x | ||
| * | ||
| * @param bool $load_theme_css True to style the preview with the theme's front end CSS. | ||
| */ | ||
| if ( ! apply_filters( 'frm_styler_load_theme_css', true ) ) { | ||
| return; | ||
| } | ||
|
|
||
| $url = add_query_arg( | ||
| array( | ||
| 'action' => 'frm_styler_theme_css', | ||
| 'nonce' => wp_create_nonce( 'frm_styler_theme_css' ), | ||
| ), | ||
| admin_url( 'admin-ajax.php' ) | ||
| ); | ||
| wp_enqueue_style( 'frm-styler-theme-preview', $url, array( 'frm-styler-preview' ), FrmStylesThemeCssHelper::get_version() ); | ||
| } | ||
|
|
||
| /** | ||
| * Output the active theme's front end CSS, scoped to the styler preview. | ||
| * | ||
| * @since x.x | ||
| * | ||
| * @return void | ||
| */ | ||
| public static function load_preview_theme_css() { | ||
| FrmAppHelper::permission_check( 'frm_change_settings' ); | ||
| check_ajax_referer( 'frm_styler_theme_css', 'nonce' ); | ||
|
|
||
| if ( ! apply_filters( 'frm_styler_load_theme_css', true ) ) { | ||
| wp_die( '', '', 403 ); | ||
| } | ||
|
|
||
| $version = FrmStylesThemeCssHelper::get_version(); | ||
| $cache_key = 'frm_styler_theme_css_v2_' . hash( 'sha256', $version ); | ||
| $css = get_transient( $cache_key ); | ||
|
|
||
| if ( ! is_string( $css ) ) { | ||
| $css = FrmStylesThemeCssHelper::get_scoped_theme_css( '#frm_active_style_form, #frm_sample_form' ); | ||
| $version = FrmStylesThemeCssHelper::get_version(); | ||
| $cache_key = 'frm_styler_theme_css_v2_' . hash( 'sha256', $version ); | ||
|
|
||
| if ( '' !== $css ) { | ||
| // An empty result can come from a failed read, so it is not kept for a day. | ||
| set_transient( $cache_key, $css, DAY_IN_SECONDS ); | ||
|
Comment on lines
+232
to
+234
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not cache incomplete theme CSS. When a theme stylesheet cannot be read, The effect is limited to the Visual Styler preview. The server copy expires after one day. A version change clears the browser copy, for example after a style save, a theme update, or a Global Styles edit. Report read failures from generation to the endpoint. For an incomplete response, skip the transient write and do not send the immutable headers. Keep both caches for complete, non-empty responses.
|
||
| } | ||
| } | ||
|
|
||
| if ( headers_sent() ) { | ||
| die(); | ||
| } | ||
|
|
||
| header( 'Content-Type: text/css; charset=UTF-8' ); | ||
| self::maybe_send_browser_cache_headers( $version ); | ||
|
|
||
| echo self::strip_preview_theme_css_html( $css ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- HTML is stripped from the CSS. | ||
|
|
||
| // Not wp_die(). Its AJAX handler sends no-cache headers again, which would undo the cache headers above. | ||
| // css/custom_theme.css.php ends the saved stylesheet request the same way. | ||
| die(); | ||
| } | ||
|
|
||
| /** | ||
| * Strip tags while preserving compact CSS comparisons such as width<=600px. | ||
| * | ||
| * @since x.x | ||
| * | ||
| * @param string $css Generated theme stylesheet. | ||
| * | ||
| * @return string | ||
| */ | ||
| private static function strip_preview_theme_css_html( $css ) { | ||
| $parts = preg_split( '/(<[=0-9.+-])/', $css, -1, PREG_SPLIT_DELIM_CAPTURE ); | ||
|
|
||
| if ( false === $parts ) { | ||
| return strip_tags( $css ); | ||
| } | ||
|
|
||
| foreach ( $parts as $index => $part ) { | ||
| if ( 0 === $index % 2 ) { | ||
| $parts[ $index ] = strip_tags( $part ); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep
The cleanup must leave CSS string content unchanged, including
|
||
| } | ||
| } | ||
|
|
||
| return implode( '', $parts ); | ||
| } | ||
|
|
||
| /** | ||
| * Let the browser keep a versioned stylesheet served from admin-ajax.php. | ||
| * | ||
| * WordPress sends no-cache headers from admin-ajax.php before any action runs. A stylesheet requested with | ||
| * the current version in its URL can be cached anyway, since any change to its content also | ||
| * changes the version, and with it the URL the page asks for. | ||
| * | ||
| * @since x.x | ||
| * | ||
| * @param string $version The current version of the stylesheet. | ||
| * | ||
| * @return void | ||
| */ | ||
| private static function maybe_send_browser_cache_headers( $version ) { | ||
| if ( headers_sent() || FrmAppHelper::simple_get( 'ver' ) !== $version ) { | ||
| return; | ||
| } | ||
|
|
||
| header_remove( 'Expires' ); | ||
| header_remove( 'Pragma' ); | ||
| header( 'Cache-Control: private, max-age=' . YEAR_IN_SECONDS . ', immutable' ); | ||
| } | ||
|
|
||
| /** | ||
| * @since 6.0 | ||
| * | ||
|
|
@@ -1186,6 +1330,11 @@ public static function load_saved_css() { | |
| // Read by custom_theme.css.php. | ||
| $merge_style_rules = true; | ||
|
|
||
| if ( FrmAppHelper::simple_get( 'frm_scope_custom_css' ) ) { | ||
| // The styler loads the full stylesheet on every visit, so let the browser keep it. | ||
| self::maybe_send_browser_cache_headers( self::get_scoped_css_version() ); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Preserve cache headers when saved CSS is generated. If
|
||
| } | ||
|
|
||
| // When $css is set (a transient hit or a refill, even one whose hash did not match), | ||
| // custom_theme.css.php echoes it and exits, so the frm_saved_css filter below does not run. | ||
| // This is accepted. Formidable API only filters &api=1 requests, and maybe_refill() skips those. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use the version sampled before the stylesheet read for the cache key.
The handler samples the theme version before it reads the stylesheets, then samples it again after
get_scoped_theme_css()returns. If a theme stylesheet changes after it is read and before the second sample, the stored CSS is the old body and the key is for the new version. The transient keeps that CSS for one day. A preview request whosevermatches the new version also receives browser-cache headers, so browsers can keep the stale CSS until the next stylesheet change.Remove the second sample. The key from the first sample then stays tied to the version that was current before the read. A later request with the new version misses the cache and regenerates the CSS.
Suggested fix
Committable suggestion