|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Plugin Name: Gravity Forms: Multiple Form Instances |
|
4
|
|
|
* Description: Allows multiple instances of the same form to be run on a single page when using AJAX. |
|
5
|
|
|
* Author: tyxla |
|
6
|
|
|
* Author URI: http://marinatanasov.com/ |
|
7
|
|
|
* Plugin URI: https://github.com/tyxla/Gravity-Forms-Multiple-Form-Instances |
|
8
|
|
|
* Version: 1.1.2 |
|
9
|
|
|
* License: GPL2 |
|
10
|
|
|
* Requires at least: 3.0.1 |
|
11
|
|
|
* Tested up to: 4.5 |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* The main plugin class. |
|
16
|
|
|
*/ |
|
17
|
|
|
class Gravity_Forms_Multiple_Form_Instances { |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Constructor. |
|
21
|
|
|
* |
|
22
|
|
|
* Used to initialize the plugin and hook the related functionality. |
|
23
|
|
|
* |
|
24
|
|
|
* @access public |
|
25
|
|
|
*/ |
|
26
|
1 |
|
public function __construct() { |
|
27
|
|
|
// hook the HTML ID string find & replace functionality |
|
28
|
1 |
|
add_filter( 'gform_get_form_filter', array( $this, 'gform_get_form_filter' ), 10, 2 ); |
|
29
|
1 |
|
add_filter( 'gform_confirmation', array( $this, 'gform_get_form_filter' ), 10, 2 ); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Replaces all occurences of the form ID with a new, unique ID. |
|
34
|
|
|
* |
|
35
|
|
|
* This is where the magic happens. |
|
36
|
|
|
* |
|
37
|
|
|
* @access public |
|
38
|
|
|
* |
|
39
|
|
|
* @param string $form_string The form HTML string. |
|
40
|
|
|
* @param array $form Array with the form settings. |
|
41
|
|
|
* @return string $form_string The modified form HTML string. |
|
42
|
45 |
|
*/ |
|
43
|
|
|
public function gform_get_form_filter( $form_string, $form ) { |
|
44
|
45 |
|
// if form has been submitted, use the submitted ID, otherwise generate a new unique ID |
|
45
|
45 |
|
if ( isset( $_POST['gform_random_id'] ) ) { |
|
46
|
45 |
|
$random_id = absint( $_POST['gform_random_id'] ); // Input var okay. |
|
47
|
|
|
} else { |
|
48
|
|
|
$random_id = mt_rand(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
45 |
|
// this is where we keep our unique ID |
|
52
|
|
|
$hidden_field = "<input type='hidden' name='gform_field_values'"; |
|
53
|
|
|
|
|
54
|
|
|
// define all occurences of the original form ID that wont hurt the form input |
|
55
|
45 |
|
$strings = array( |
|
56
|
45 |
|
' gform_wrapper' => ' gform_wrapper gform_wrapper_original_id_' . $form['id'], |
|
57
|
45 |
|
"for='choice_" => "for='choice_" . $random_id . '_', |
|
58
|
45 |
|
"id='label_" => "id='label_" . $random_id . '_', |
|
59
|
45 |
|
"'gform_wrapper_" . $form['id'] . "'" => "'gform_wrapper_" . $random_id . "'", |
|
60
|
45 |
|
"'gf_" . $form['id'] . "'" => "'gf_" . $random_id . "'", |
|
61
|
45 |
|
"'gform_" . $form['id'] . "'" => "'gform_" . $random_id . "'", |
|
62
|
45 |
|
"'gform_ajax_frame_" . $form['id'] . "'" => "'gform_ajax_frame_" . $random_id . "'", |
|
63
|
45 |
|
'#gf_' . $form['id'] . "'" => '#gf_' . $random_id . "'", |
|
64
|
45 |
|
"'gform_fields_" . $form['id'] . "'" => "'gform_fields_" . $random_id . "'", |
|
65
|
45 |
|
"id='field_" . $form['id'] . '_' => "id='field_" . $random_id . '_', |
|
66
|
45 |
|
"for='input_" . $form['id'] . '_' => "for='input_" . $random_id . '_', |
|
67
|
45 |
|
"id='input_" . $form['id'] . '_' => "id='input_" . $random_id . '_', |
|
68
|
45 |
|
"id='choice_" . $form['id'] . '_' => "id='choice_" . $random_id . '_', |
|
69
|
45 |
|
"'gform_submit_button_" . $form['id'] . "'" => "'gform_submit_button_" . $random_id . "'", |
|
70
|
45 |
|
'"gf_submitting_' . $form['id'] . '"' => '"gf_submitting_' . $random_id . '"', |
|
71
|
45 |
|
"'gf_submitting_" . $form['id'] . "'" => "'gf_submitting_" . $random_id . "'", |
|
72
|
45 |
|
'#gform_ajax_frame_' . $form['id'] => '#gform_ajax_frame_' . $random_id, |
|
73
|
45 |
|
'#gform_wrapper_' . $form['id'] => '#gform_wrapper_' . $random_id, |
|
74
|
45 |
|
'#gform_' . $form['id'] => '#gform_' . $random_id, |
|
75
|
45 |
|
"trigger('gform_post_render', [" . $form['id'] => "trigger('gform_post_render', [" . $random_id, |
|
76
|
45 |
|
'gformInitSpinner( ' . $form['id'] . ',' => 'gformInitSpinner( ' . $random_id . ',', |
|
77
|
45 |
|
"trigger('gform_page_loaded', [" . $form['id'] => "trigger('gform_page_loaded', [" . $random_id, |
|
78
|
45 |
|
"'gform_confirmation_loaded', [" . $form['id'] . ']' => "'gform_confirmation_loaded', [" . $random_id . ']', |
|
79
|
45 |
|
'gf_apply_rules(' . $form['id'] . ',' => 'gf_apply_rules(' . $random_id . ',', |
|
80
|
45 |
|
'gform_confirmation_wrapper_' . $form['id'] => 'gform_confirmation_wrapper_' . $random_id, |
|
81
|
45 |
|
'gforms_confirmation_message_' . $form['id'] => 'gforms_confirmation_message_' . $random_id, |
|
82
|
45 |
|
'gform_confirmation_message_' . $form['id'] => 'gform_confirmation_message_' . $random_id, |
|
83
|
45 |
|
'if(formId == ' . $form['id'] . ')' => 'if(formId == ' . $random_id . ')', |
|
84
|
45 |
|
"window['gf_form_conditional_logic'][" . $form['id'] . ']' => "window['gf_form_conditional_logic'][" . $random_id . ']', |
|
85
|
45 |
|
"trigger('gform_post_conditional_logic', [" . $form['id'] . ',' => "trigger('gform_post_conditional_logic', [" . $random_id . ',', |
|
86
|
45 |
|
'gformShowPasswordStrength("input_' . $form['id'] . '_' => 'gformShowPasswordStrength("input_' . $random_id . '_', |
|
87
|
45 |
|
"gformInitChosenFields('#input_" . $form['id'] . '_' => "gformInitChosenFields('#input_" . $random_id . '_', |
|
88
|
45 |
|
"jQuery('#input_" . $form['id'] . '_' => "jQuery('#input_" . $random_id . '_', |
|
89
|
45 |
|
'gforms_calendar_icon_input_' . $form['id'] . '_' => 'gforms_calendar_icon_input_' . $random_id . '_', |
|
90
|
45 |
|
"id='ginput_base_price_" . $form['id'] . '_' => "id='ginput_base_price_" . $random_id . '_', |
|
91
|
45 |
|
"id='ginput_quantity_" . $form['id'] . '_' => "id='ginput_quantity_" . $random_id . '_', |
|
92
|
45 |
|
'gfield_price_' . $form['id'] . '_' => 'gfield_price_' . $random_id . '_', |
|
93
|
45 |
|
'gfield_quantity_' . $form['id'] . '_' => 'gfield_quantity_' . $random_id . '_', |
|
94
|
45 |
|
'gfield_product_' . $form['id'] . '_' => 'gfield_product_' . $random_id . '_', |
|
95
|
45 |
|
'ginput_total_' . $form['id'] => 'ginput_total_' . $random_id, |
|
96
|
45 |
|
'GFCalc(' . $form['id'] . ',' => 'GFCalc(' . $random_id . ',', |
|
97
|
45 |
|
'gf_global["number_formats"][' . $form['id'] . ']' => 'gf_global["number_formats"][' . $random_id . ']', |
|
98
|
45 |
|
'gform_next_button_' . $form['id'] . '_' => 'gform_next_button_' . $random_id . '_', |
|
99
|
45 |
|
$hidden_field => "<input type='hidden' name='gform_random_id' value='" . $random_id . "' />" . $hidden_field, |
|
100
|
|
|
); |
|
101
|
|
|
|
|
102
|
45 |
|
// allow addons & plugins to add additional find & replace strings |
|
103
|
|
|
$strings = apply_filters( 'gform_multiple_instances_strings', $strings, $form['id'], $random_id ); |
|
104
|
|
|
|
|
105
|
45 |
|
// replace all occurences with the new unique ID |
|
106
|
45 |
|
foreach ( $strings as $find => $replace ) { |
|
107
|
45 |
|
$form_string = str_replace( $find, $replace, $form_string ); |
|
108
|
|
|
} |
|
109
|
45 |
|
|
|
110
|
|
|
return $form_string; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
// initialize the plugin |
|
116
|
|
|
global $gravity_forms_multiple_form_instances; |
|
117
|
|
|
$gravity_forms_multiple_form_instances = new Gravity_Forms_Multiple_Form_Instances(); |
|
118
|
|
|
|