Live Refresh
Live refresh the target field whenever a field changes.
Instructions
See “Where do I put snippets?” in our documentation for installation instructions.
Code
Filename: gpps-live-refresh.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
/**
* Gravity Perks // Preview Submission // Live Refresh
* https://gravitywiz.com/documentation/gravity-forms-preview-submission/
*
* Live refresh the target field whenever a field changes.
*
* Plugin Name: GP Preview Submission - Live Refresh
* Plugin URI: https://gravitywiz.com/documentation/gravity-forms-preview-submission/
* Description: Live refresh the target field whenever a field changes.
* Author: Gravity Wiz
* Version: 0.8
* Author URI: https://gravitywiz.com/
*/
class GPPS_Live_Refresh {
private $_args = array();
public function __construct( $args = array() ) {
$this->_args = wp_parse_args( $args, array(
'form_id' => false,
'target_field_id' => false,
) );
add_action( 'wp_ajax_gpps_refresh_field', array( $this, 'ajax_refresh' ) );
add_action( 'wp_ajax_nopriv_gpps_refresh_field', array( $this, 'ajax_refresh' ) );
add_filter( 'gform_pre_render', array( $this, 'load_form_script' ), 10, 2 );
add_filter( 'gform_register_init_scripts', array( $this, 'add_init_script' ), 10, 2 );
if ( rgpost( 'action' ) == 'gpps_refresh_field' ) {
remove_action( 'wp', array( 'GFForms', 'maybe_process_form' ), 9 );
remove_action( 'admin_init', array( 'GFForms', 'maybe_process_form' ), 9 );
}
}
public function load_form_script( $form, $is_ajax_enabled ) {
if ( $this->is_applicable_form( $form ) && ! has_action( 'wp_footer', array( $this, 'output_script' ) ) ) {
wp_enqueue_script( 'gform_gravityforms' );
add_action( 'wp_footer', array( $this, 'output_script' ) );
add_action( 'gform_preview_footer', array( $this, 'output_script' ) );
}
return $form;
}
public function output_script() {
?>
<script type="text/javascript">
( function( $ ) {
window.GPPSLiveRefresh = function( args ) {
var self = this;
// copy all args to current object: (list expected props)
for( prop in args ) {
if( args.hasOwnProperty( prop ) )
self[prop] = args[prop];
}
self.init = function() {
self.$form = $( '#gform_wrapper_{0}'.gformFormat( self.formId ) );
self.$targetField = $( '#field_{0}_{1}'.gformFormat( self.formId, self.targetFieldId ) );
self.$form.find( 'input, select, textarea' ).on( 'change', function() {
self.refresh();
} );
self.refresh();
};
self.refresh = function() {
if( ! self.$targetField.is( ':visible' ) ) {
return;
}
var data = {
action: 'gpps_refresh_field'
};
self.$form.find( 'input, select, textarea' ).each( function() {
var $input = $( this ),
name = $input.attr( 'name' );
if ( $input.is(':radio') ) {
if ( $input.is(':checked') ) {
data[name] = $input.val();
}
} else {
data[name] = $input.val();
}
} );
$.post( self.ajaxUrl, data, function( response ) {
if( response.success ) {
self.$targetField.html( response.data );
}
} );
};
self.init();
}
} )( jQuery );
</script>
<?php
}
public function add_init_script( $form ) {
if ( ! $this->is_applicable_form( $form ) ) {
return;
}
$args = array(
'formId' => $this->_args['form_id'],
'targetFieldId' => $this->_args['target_field_id'],
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
);
$script = 'new GPPSLiveRefresh( ' . json_encode( $args ) . ' );';
$slug = implode( '_', array( 'gpps_live_refresh', $this->_args['form_id'], $this->_args['target_field_id'] ) );
GFFormDisplay::add_init_script( $this->_args['form_id'], $slug, GFFormDisplay::ON_PAGE_RENDER, $script );
}
public function is_applicable_form( $form ) {
$form_id = isset( $form['id'] ) ? $form['id'] : $form;
return empty( $this->_args['form_id'] ) || $form_id == $this->_args['form_id'];
}
public function ajax_refresh() {
$entry = GFFormsModel::get_current_lead();
if ( ! $entry ) {
wp_send_json_error();
}
$form = gf_apply_filters( array( 'gform_pre_render', $entry['form_id'] ), GFAPI::get_form( $entry['form_id'] ), false, array() );
$field = GFFormsModel::get_field( $form, $this->_args['target_field_id'] );
if ( $field->get_input_type() == 'html' ) {
$field->content = GWPreviewConfirmation::preview_replace_variables( $field->content, $form );
$content = $field->get_field_input( $form, '', $entry );
} else {
$value = rgpost( 'input_' . $field->id );
$content = $field->get_field_content( $value, true, $form );
$content = str_replace( '{FIELD}', $field->get_field_input( $form, $value, $entry ), $content );
}
wp_send_json_success( $content );
}
}
# Configuration
new GPPS_Live_Refresh( array(
'form_id' => 123,
'target_field_id' => 4,
) );