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
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,109 @@ 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(); | ||
|
|
||
| return FrmAppHelper::plugin_version() . '-' . ( $last_update ? $last_update : '0' ) . '-' . substr( md5( is_string( $custom_css ) ? $custom_css : '' ), 0, 8 ); | ||
| } | ||
|
|
||
| /** | ||
| * 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_ajax' ), | ||
| ), | ||
| 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_ajax', 'nonce' ); | ||
|
|
||
| $version = FrmStylesThemeCssHelper::get_version(); | ||
| $cache_key = 'frm_styler_theme_css_' . md5( $version ); | ||
|
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.
|
||
| $css = get_transient( $cache_key ); | ||
|
|
||
| if ( ! is_string( $css ) ) { | ||
| $css = FrmStylesThemeCssHelper::get_scoped_theme_css( '#frm_active_style_form, #frm_sample_form' ); | ||
| set_transient( $cache_key, $css, DAY_IN_SECONDS ); | ||
|
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. Non-blocking: an empty result is cached for a day, and the version key misses real inputs.
|
||
| } | ||
|
|
||
| header( 'Content-type: text/css' ); | ||
| self::maybe_send_browser_cache_headers( $version ); | ||
|
|
||
| echo $css; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Generated CSS, escaping would corrupt it. | ||
|
|
||
| // 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(); | ||
| } | ||
|
|
||
| /** | ||
| * 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 | ||
| * | ||
|
|
@@ -1184,6 +1288,11 @@ public static function load_css() { | |
| public static function load_saved_css() { | ||
| $css = get_transient( 'frmpro_css' ); | ||
|
|
||
| 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() ); | ||
| } | ||
|
|
||
| ob_start(); | ||
| include FrmAppHelper::plugin_path() . '/css/custom_theme.css.php'; | ||
| $output = ob_get_clean(); | ||
|
|
||
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.
Using
md5(),sha1()function is not recommended to generate secure passwords. Due to its fast nature to compute passwords too quickly, these functions can become really easy to crack a password using brute force attack.It is recommended to use PHP's password hashing function
password_hash()to create a secure password hash.