|
9 | 9 | * Plugin URI: https://gravitywiz.com/cache-busting-with-gravity-forms/ |
10 | 10 | * Description: Bypass your website cache when loading a Gravity Forms form. |
11 | 11 | * Author: Gravity Wiz |
12 | | - * Version: 0.6 |
| 12 | + * Version: 0.6.1 |
13 | 13 | * Author URI: https://gravitywiz.com |
14 | 14 | */ |
15 | 15 | class GW_Cache_Buster { |
@@ -48,12 +48,44 @@ public function init() { |
48 | 48 | add_action( 'wp_ajax_gfcb_get_form', array( $this, 'ajax_get_form' ) ); |
49 | 49 | } |
50 | 50 |
|
| 51 | + /** |
| 52 | + * Duplicate method due to GFFormDisplay::set_form_styles() being private. |
| 53 | + * |
| 54 | + * Applies the form styles and form theme to the form object so that the proper style block is rendered to the page. |
| 55 | + * |
| 56 | + * @param mixed $form The form object. |
| 57 | + * @param string|null $style_settings Style settings for the form. This will either come from the shortcode or from the block editor settings. |
| 58 | + * @param string|null $form_theme The theme selected for the form. This will either come from the shortcode or from the block editor settings. |
| 59 | +
|
| 60 | + * @return array Returns the form object with the 'styles' and 'theme' properties set. |
| 61 | + */ |
| 62 | + public function set_form_styles( $form, $style_settings, $form_theme ) { |
| 63 | + if ( $style_settings === false ) { |
| 64 | + // Form styles specifically set to false disables inline form css styles. |
| 65 | + $form_styles = false; |
| 66 | + } else { |
| 67 | + $form_styles = !empty( $style_settings ) ? json_decode( $style_settings, true ) : array(); |
| 68 | + } |
| 69 | + |
| 70 | + // Removing theme from styles for consistency. $form['theme'] should be used instead. |
| 71 | + if ( $form_styles ) { |
| 72 | + unset( $form_styles['theme'] ); |
| 73 | + } |
| 74 | + $form['styles'] = GFFormDisplay::get_form_styles( $form_styles ); |
| 75 | + $form['theme'] = ! empty( $form_theme ) ? $form_theme : GFForms::get_default_theme(); |
| 76 | + return $form; |
| 77 | + } |
| 78 | + |
51 | 79 | public function shortcode( $markup, $attributes, $content ) { |
52 | 80 |
|
53 | 81 | $atts = shortcode_atts( |
54 | 82 | array( |
55 | 83 | 'id' => 0, |
56 | 84 | 'cachebuster' => false, |
| 85 | + |
| 86 | + // Attributes needed for theme/styles. |
| 87 | + 'theme' => method_exists( 'GFForms', 'get_default_theme' ) ? GFForms::get_default_theme() : '', |
| 88 | + 'styles' => '', |
57 | 89 | ), |
58 | 90 | $attributes |
59 | 91 | ); |
@@ -142,10 +174,19 @@ public function shortcode( $markup, $attributes, $content ) { |
142 | 174 | $exclude_params = array( 'action', 'form_id', 'atts' ); |
143 | 175 | $ajax_url = remove_query_arg( $exclude_params, add_query_arg( $_GET, admin_url( 'admin-ajax.php' ) ) ); |
144 | 176 |
|
| 177 | + // Get the form theme. |
| 178 | + if ( method_exists( 'GFFormDisplay', 'get_form_theme_slug' ) ) { |
| 179 | + $form = $this->set_form_styles( GFAPI::get_form( $form_id ), rgar( $atts, 'styles' ), rgar( $atts, 'theme' ) ); |
| 180 | + $form_theme = GFFormDisplay::get_form_theme_slug( $form ); |
| 181 | + } else { |
| 182 | + $form_theme = null; |
| 183 | + } |
| 184 | + |
145 | 185 | // Still needed for the AJAX submission. |
146 | 186 | $ajax_params = array( |
147 | 187 | 'action' => 'gfcb_get_form', |
148 | 188 | 'form_id' => $form_id, |
| 189 | + 'form_theme' => $form_theme, |
149 | 190 | ); |
150 | 191 |
|
151 | 192 | // Ensure AJAX parameters for GPNF are also correctly populated. |
@@ -256,6 +297,10 @@ public function ajax_get_form() { |
256 | 297 | add_filter( 'gform_form_tag_' . $form_id, array( $this, 'add_hidden_inputs' ), 10, 2 ); |
257 | 298 | add_filter( 'gform_pre_render_' . $form_id, array( $this, 'replace_embed_tag_for_field_default_values' ) ); |
258 | 299 |
|
| 300 | + add_filter( 'gform_form_theme_slug', function( $slug, $form ) { |
| 301 | + return rgar( $_REQUEST, 'form_theme' ) ?: $slug; |
| 302 | + }, 10, 2 ); |
| 303 | + |
259 | 304 | $atts = rgpost( 'atts' ); |
260 | 305 |
|
261 | 306 | // GF expects an associative array for field values. Parse them before passing it on. |
|
0 commit comments