Skip to content

Commit b64b6d0

Browse files
committed
gw-cache-buster.php: Improved compatibility with GF 2.9+ form themes and styles.
1 parent c35605a commit b64b6d0

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

gravity-forms/gw-cache-buster.php

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* Plugin URI: https://gravitywiz.com/cache-busting-with-gravity-forms/
1010
* Description: Bypass your website cache when loading a Gravity Forms form.
1111
* Author: Gravity Wiz
12-
* Version: 0.6
12+
* Version: 0.6.1
1313
* Author URI: https://gravitywiz.com
1414
*/
1515
class GW_Cache_Buster {
@@ -48,12 +48,44 @@ public function init() {
4848
add_action( 'wp_ajax_gfcb_get_form', array( $this, 'ajax_get_form' ) );
4949
}
5050

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+
5179
public function shortcode( $markup, $attributes, $content ) {
5280

5381
$atts = shortcode_atts(
5482
array(
5583
'id' => 0,
5684
'cachebuster' => false,
85+
86+
// Attributes needed for theme/styles.
87+
'theme' => method_exists( 'GFForms', 'get_default_theme' ) ? GFForms::get_default_theme() : '',
88+
'styles' => '',
5789
),
5890
$attributes
5991
);
@@ -142,10 +174,19 @@ public function shortcode( $markup, $attributes, $content ) {
142174
$exclude_params = array( 'action', 'form_id', 'atts' );
143175
$ajax_url = remove_query_arg( $exclude_params, add_query_arg( $_GET, admin_url( 'admin-ajax.php' ) ) );
144176

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+
145185
// Still needed for the AJAX submission.
146186
$ajax_params = array(
147187
'action' => 'gfcb_get_form',
148188
'form_id' => $form_id,
189+
'form_theme' => $form_theme,
149190
);
150191

151192
// Ensure AJAX parameters for GPNF are also correctly populated.
@@ -256,6 +297,10 @@ public function ajax_get_form() {
256297
add_filter( 'gform_form_tag_' . $form_id, array( $this, 'add_hidden_inputs' ), 10, 2 );
257298
add_filter( 'gform_pre_render_' . $form_id, array( $this, 'replace_embed_tag_for_field_default_values' ) );
258299

300+
add_filter( 'gform_form_theme_slug', function( $slug, $form ) {
301+
return rgar( $_REQUEST, 'form_theme' ) ?: $slug;
302+
}, 10, 2 );
303+
259304
$atts = rgpost( 'atts' );
260305

261306
// GF expects an associative array for field values. Parse them before passing it on.

0 commit comments

Comments
 (0)