Skip to content

Commit b24c832

Browse files
committed
gw-cache-buster.php: Added support for GF Blocks
1 parent 4425281 commit b24c832

File tree

1 file changed

+43
-3
lines changed

1 file changed

+43
-3
lines changed

gravity-forms/gw-cache-buster.php

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class GW_Cache_Buster {
1616

1717
private $_args = array();
1818

19+
private $_form_args = array();
20+
1921
public function __construct( $args = array() ) {
2022

2123
// set our default arguments, parse against the provided arguments, and store for use throughout the class
@@ -40,6 +42,8 @@ public function init() {
4042
}
4143

4244
add_filter( 'gform_shortcode_form', array( $this, 'shortcode' ), 10, 3 );
45+
add_filter( 'gform_get_form_filter', array( $this, 'form_filter' ), 10, 2 );
46+
add_filter( 'gform_form_args', array( $this, 'stash_form_args' ) );
4347
add_filter( 'gform_save_and_continue_resume_url', array( $this, 'filter_resume_link' ), 15, 4 );
4448
add_filter( 'gform_pre_replace_merge_tags', array( $this, 'replace_embed_url' ), 10, 4 );
4549
add_action( 'gform_after_submission', array( $this, 'entry_source_url' ), 10, 2 );
@@ -72,12 +76,16 @@ public function set_form_styles( $form, $style_settings, $form_theme ) {
7276
unset( $form_styles['theme'] );
7377
}
7478
$form['styles'] = GFFormDisplay::get_form_styles( $form_styles );
75-
$form['theme'] = ! empty( $form_theme ) ? $form_theme : GFForms::get_default_theme();
79+
$form['theme'] = $form_theme;
7680
return $form;
7781
}
7882

79-
public function shortcode( $markup, $attributes, $content ) {
83+
public function stash_form_args( $form_args ) {
84+
$this->_form_args[ $form_args['form_id'] ] = $form_args;
85+
return $form_args;
86+
}
8087

88+
public function shortcode( $markup, $attributes, $content ) {
8189
$atts = shortcode_atts(
8290
array(
8391
'id' => 0,
@@ -90,6 +98,36 @@ public function shortcode( $markup, $attributes, $content ) {
9098
$attributes
9199
);
92100

101+
remove_filter( 'gform_get_form_filter', array( $this, 'form_filter' ), 10, 2 );
102+
return $this->get_cachebuster_markup( $markup, array_merge( $attributes, $atts ), $content );
103+
}
104+
105+
public function form_filter( $markup, $form ) {
106+
/**
107+
* Prevent recursion
108+
*/
109+
if ( rgar( $GLOBALS, 'processing' ) ) {
110+
return $markup;
111+
}
112+
113+
$form_args = rgar( $this->_form_args, $form['id'] );
114+
115+
$args = array (
116+
'id' => $form['id'],
117+
'cachebuster' => true,
118+
'title' => $form_args['display_title'],
119+
'description' => $form_args['display_description'],
120+
'field_values' => $form_args['field_values'],
121+
'ajax' => $form_args['ajax'],
122+
'tabindex' => $form_args['tabindex']
123+
'theme' => rgar( $form, 'theme' ),
124+
'styles' => rgar( $form, 'styles' ),
125+
);
126+
127+
return $this->get_cachebuster_markup( $markup, $args, null );
128+
}
129+
130+
public function get_cachebuster_markup( $markup, $atts, $content ) {
93131
$form_id = $atts['id'];
94132

95133
if ( ! $this->is_cache_busting_applicable() ) {
@@ -210,7 +248,7 @@ public function shortcode( $markup, $attributes, $content ) {
210248
$.post( '<?php echo $ajax_url; ?>', {
211249
action: 'gfcb_get_form',
212250
form_id: '<?php echo $form_id; ?>',
213-
atts: <?php echo wp_json_encode( $attributes ); ?>,
251+
atts: <?php echo wp_json_encode( $atts ); ?>,
214252
form_request_origin: '<?php echo esc_js( GFCommon::openssl_encrypt( GFFormsModel::get_current_page_url() ) ); ?>',
215253
lang: '<?php echo $lang; ?>'
216254
}, function( response ) {
@@ -310,7 +348,9 @@ public function ajax_get_form() {
310348
$GLOBALS['GWCB_POST'] = $_POST;
311349
$_POST = array();
312350

351+
$GLOBALS['processing'] = true;
313352
gravity_form( $form_id, filter_var( rgar( $atts, 'title', true ), FILTER_VALIDATE_BOOLEAN ), filter_var( rgar( $atts, 'description', true ), FILTER_VALIDATE_BOOLEAN ), false, $field_values, true /* default to true; add support for non-ajax in the future */, rgar( $atts, 'tabindex' ) );
353+
$GLOBALS['processing'] = false;
314354

315355
remove_filter( 'gform_form_tag_' . $form_id, array( $this, 'add_hidden_inputs' ) );
316356
remove_filter( 'gform_pre_render_' . $form_id, array( $this, 'replace_embed_tag_for_field_default_values' ) );

0 commit comments

Comments
 (0)