Skip to content

Commit 2882222

Browse files
committed
gw-cache-buster.php: Added support for Gravity Forms Blocks.
1 parent 7d2ea71 commit 2882222

File tree

1 file changed

+41
-2
lines changed

1 file changed

+41
-2
lines changed

gravity-forms/gw-cache-buster.php

Lines changed: 41 additions & 2 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 );
@@ -73,11 +77,16 @@ public function set_form_styles( $form, $style_settings, $form_theme ) {
7377
}
7478
$form['styles'] = GFFormDisplay::get_form_styles( $form_styles );
7579
$form['theme'] = $form_theme;
80+
7681
return $form;
7782
}
7883

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

89+
public function shortcode( $markup, $attributes, $content ) {
8190
$atts = shortcode_atts(
8291
array(
8392
'id' => 0,
@@ -90,6 +99,34 @@ public function shortcode( $markup, $attributes, $content ) {
9099
$attributes
91100
);
92101

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

95132
if ( ! $this->is_cache_busting_applicable() ) {
@@ -210,7 +247,7 @@ public function shortcode( $markup, $attributes, $content ) {
210247
$.post( '<?php echo $ajax_url; ?>', {
211248
action: 'gfcb_get_form',
212249
form_id: '<?php echo $form_id; ?>',
213-
atts: <?php echo wp_json_encode( $attributes ); ?>,
250+
atts: <?php echo wp_json_encode( $atts ); ?>,
214251
form_request_origin: '<?php echo esc_js( GFCommon::openssl_encrypt( GFFormsModel::get_current_page_url() ) ); ?>',
215252
lang: '<?php echo $lang; ?>'
216253
}, function( response ) {
@@ -310,7 +347,9 @@ public function ajax_get_form() {
310347
$GLOBALS['GWCB_POST'] = $_POST;
311348
$_POST = array();
312349

350+
$GLOBALS['processing'] = true;
313351
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' ) );
352+
$GLOBALS['processing'] = false;
314353

315354
remove_filter( 'gform_form_tag_' . $form_id, array( $this, 'add_hidden_inputs' ) );
316355
remove_filter( 'gform_pre_render_' . $form_id, array( $this, 'replace_embed_tag_for_field_default_values' ) );

0 commit comments

Comments
 (0)