Gravity_Forms_Multiple_Form_Instances

The main plugin class.

Defined (1)

The class is defined in the following location(s).

/gravityforms-multiple-form-instances.php  
  1. class Gravity_Forms_Multiple_Form_Instances { 
  2.  
  3. /** 
  4. * Constructor. 
  5. *  
  6. * Used to initialize the plugin and hook the related functionality. 
  7. * @access public 
  8. */ 
  9. public function __construct() { 
  10. // hook the HTML ID string find & replace functionality 
  11. add_filter( 'gform_get_form_filter', array( $this, 'gform_get_form_filter' ), 10, 2 ); 
  12.  
  13. /** 
  14. * Replaces all occurences of the form ID with a new, unique ID. 
  15. *  
  16. * This is where the magic happens. 
  17. * @access public 
  18. * @param string $form_string The form HTML string. 
  19. * @param array $form Array with the form settings. 
  20. * @return string $form_string The modified form HTML string. 
  21. */ 
  22. public function gform_get_form_filter( $form_string, $form ) { 
  23. // if form has been submitted, use the submitted ID, otherwise generate a new unique ID 
  24. if ( isset( $_POST['gform_random_id'] ) ) { 
  25. $random_id = absint( $_POST['gform_random_id'] ); // Input var okay. 
  26. } else { 
  27. $random_id = mt_rand();  
  28.  
  29. // this is where we keep our unique ID 
  30. $hidden_field = "<input type='hidden' name='gform_field_values'"; 
  31.  
  32. // define all occurences of the original form ID that wont hurt the form input 
  33. $strings = array( 
  34. ' gform_wrapper ' => ' gform_wrapper gform_wrapper_original_id_' . $form['id'] . ' ',  
  35. "for='choice_" => "for='choice_" . $random_id . '_',  
  36. "id='choice_" => "id='choice_" . $random_id . '_',  
  37. "id='label_" => "id='label_" . $random_id . '_',  
  38. "'gform_wrapper_" . $form['id'] . "'" => "'gform_wrapper_" . $random_id . "'",  
  39. "'gf_" . $form['id'] . "'" => "'gf_" . $random_id . "'",  
  40. "'gform_" . $form['id'] . "'" => "'gform_" . $random_id . "'",  
  41. "'gform_ajax_frame_" . $form['id'] . "'" => "'gform_ajax_frame_" . $random_id . "'",  
  42. '#gf_' . $form['id'] . "'" => '#gf_' . $random_id . "'",  
  43. "'gform_fields_" . $form['id'] . "'" => "'gform_fields_" . $random_id . "'",  
  44. "id='field_" . $form['id'] . '_' => "id='field_" . $random_id . '_',  
  45. "for='input_" . $form['id'] . '_' => "for='input_" . $random_id . '_',  
  46. "id='input_" . $form['id'] . '_' => "id='input_" . $random_id . '_',  
  47. "'gform_submit_button_" . $form['id'] . "'" => "'gform_submit_button_" . $random_id . "'",  
  48. '"gf_submitting_' . $form['id'] . '"' => '"gf_submitting_' . $random_id . '"',  
  49. "'gf_submitting_" . $form['id'] . "'" => "'gf_submitting_" . $random_id . "'",  
  50. '#gform_ajax_frame_' . $form['id'] => '#gform_ajax_frame_' . $random_id,  
  51. '#gform_wrapper_' . $form['id'] => '#gform_wrapper_' . $random_id,  
  52. '#gform_' . $form['id'] => '#gform_' . $random_id,  
  53. "trigger('gform_post_render', [" . $form['id'] => "trigger('gform_post_render', [" . $random_id,  
  54. 'gformInitSpinner( ' . $form['id'] . ', ' => 'gformInitSpinner( ' . $random_id . ', ',  
  55. "trigger('gform_page_loaded', [" . $form['id'] => "trigger('gform_page_loaded', [" . $random_id,  
  56. "'gform_confirmation_loaded', [" . $form['id'] . ']' => "'gform_confirmation_loaded', [" . $random_id . ']',  
  57. 'gf_apply_rules(' . $form['id'] . ', ' => 'gf_apply_rules(' . $random_id . ', ',  
  58. 'gform_confirmation_wrapper_' . $form['id'] => 'gform_confirmation_wrapper_' . $random_id,  
  59. 'gforms_confirmation_message_' . $form['id'] => 'gforms_confirmation_message_' . $random_id,  
  60. 'gform_confirmation_message_' . $form['id'] => 'gform_confirmation_message_' . $random_id,  
  61. 'if(formId == ' . $form['id'] . ')' => 'if(formId == ' . $random_id . ')',  
  62. "window['gf_form_conditional_logic'][" . $form['id'] . ']' => "window['gf_form_conditional_logic'][" . $random_id . ']',  
  63. "trigger('gform_post_conditional_logic', [" . $form['id'] . ', ' => "trigger('gform_post_conditional_logic', [" . $random_id . ', ',  
  64. 'gformShowPasswordStrength("input_' . $form['id'] . '_' => 'gformShowPasswordStrength("input_' . $random_id . '_',  
  65. "gformInitChosenFields('#input_" . $form['id'] . '_' => "gformInitChosenFields('#input_" . $random_id . '_',  
  66. "jQuery('#input_" . $form['id'] . '_' => "jQuery('#input_" . $random_id . '_',  
  67. 'gforms_calendar_icon_input_' . $form['id'] . '_' => 'gforms_calendar_icon_input_' . $random_id . '_',  
  68. "id='ginput_base_price_" . $form['id'] . '_' => "id='ginput_base_price_" . $random_id . '_',  
  69. "id='ginput_quantity_" . $form['id'] . '_' => "id='ginput_quantity_" . $random_id . '_',  
  70. 'gfield_price_' . $form['id'] . '_' => 'gfield_price_' . $random_id . '_',  
  71. 'gfield_quantity_' . $form['id'] . '_' => 'gfield_quantity_' . $random_id . '_',  
  72. 'gfield_product_' . $form['id'] . '_' => 'gfield_product_' . $random_id . '_',  
  73. 'ginput_total_' . $form['id'] => 'ginput_total_' . $random_id,  
  74. 'GFCalc(' . $form['id'] . ', ' => 'GFCalc(' . $random_id . ', ',  
  75. 'gf_global["number_formats"][' . $form['id'] . ']' => 'gf_global["number_formats"][' . $random_id . ']',  
  76. 'gform_next_button_' . $form['id'] . '_' => 'gform_next_button_' . $random_id . '_',  
  77. $hidden_field => "<input type='hidden' name='gform_random_id' value='" . $random_id . "' />" . $hidden_field,  
  78. ); 
  79.  
  80. // allow addons & plugins to add additional find & replace strings 
  81. $strings = apply_filters( 'gform_multiple_instances_strings', $strings ); 
  82.  
  83. // replace all occurences with the new unique ID 
  84. foreach ( $strings as $find => $replace ) { 
  85. $form_string = str_replace( $find, $replace, $form_string ); 
  86.  
  87. return $form_string; 
  88.