MC4WP_Lite_Form_Manager

This class takes care of all form related functionality.

Defined (1)

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

/includes/class-form-manager.php  
  1. class MC4WP_Lite_Form_Manager { 
  2.  
  3. /** 
  4. * @var array 
  5. */ 
  6. private $options = array(); 
  7.  
  8. /** 
  9. * @var int 
  10. */ 
  11. private $outputted_forms_count = 0; 
  12.  
  13. /** 
  14. * @var bool Is the inline CSS printed already? 
  15. */ 
  16. private $inline_css_printed = false; 
  17.  
  18. /** 
  19. * @var bool Is the inline JavaScript printed to the page already? 
  20. */ 
  21. private $inline_js_printed = false; 
  22.  
  23. /** 
  24. * @var bool Whether to print the JS snippet "fixing" date fields 
  25. */ 
  26. private $print_date_fallback = false; 
  27.  
  28. /** 
  29. * Constructor 
  30. */ 
  31. public function __construct() { 
  32. $this->options = mc4wp_get_options( 'form' ); 
  33.  
  34. /** 
  35. * Init all form related functionality 
  36. */ 
  37. public function init() { 
  38. $this->add_hooks(); 
  39. $this->register_scripts(); 
  40. $this->register_shortcodes(); 
  41.  
  42.  
  43. public function add_hooks() { 
  44. // load checkbox css if necessary 
  45. add_action( 'wp_enqueue_scripts', array( $this, 'load_stylesheet' ) ); 
  46.  
  47. // enable shortcodes in text widgets 
  48. add_filter( 'widget_text', 'shortcode_unautop' ); 
  49. add_filter( 'widget_text', 'do_shortcode', 11 ); 
  50.  
  51. /** 
  52. * Registers the [mc4wp_form] shortcode 
  53. */ 
  54. protected function register_shortcodes() { 
  55. // register shortcodes 
  56. add_shortcode( 'mc4wp_form', array( $this, 'output_form' ) ); 
  57.  
  58. // @deprecated, use [mc4wp_form] instead 
  59. add_shortcode( 'mc4wp-form', array( $this, 'output_form' ) ); 
  60.  
  61. /** 
  62. * Register the various JS files used by the plugin 
  63. */ 
  64. protected function register_scripts() { 
  65. $suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; 
  66.  
  67. // register placeholder script, which will later be enqueued for IE only 
  68. wp_register_script( 'mc4wp-placeholders', MC4WP_LITE_PLUGIN_URL . 'assets/js/third-party/placeholders.min.js', array(), MC4WP_LITE_VERSION, true ); 
  69.  
  70. // register non-AJAX script (that handles form submissions) 
  71. wp_register_script( 'mc4wp-form-request', MC4WP_LITE_PLUGIN_URL . 'assets/js/form-request' . $suffix . '.js', array(), MC4WP_LITE_VERSION, true ); 
  72.  
  73. /** 
  74. * Load the form stylesheet(s) 
  75. */ 
  76. public function load_stylesheet( ) { 
  77.  
  78. if ( ! $this->options['css'] ) { 
  79. return false; 
  80.  
  81. $suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; 
  82.  
  83. if( $this->options['css'] != 1 && $this->options['css'] !== 'default' ) { 
  84.  
  85. $form_theme = $this->options['css']; 
  86. if( in_array( $form_theme, array( 'blue', 'green', 'dark', 'light', 'red' ) ) ) { 
  87. wp_enqueue_style( 'mailchimp-for-wp-form-theme-' . $this->options['css'], MC4WP_LITE_PLUGIN_URL . 'assets/css/form-theme-' . $this->options['css'] . $suffix . '.css', array(), MC4WP_LITE_VERSION, 'all' ); 
  88.  
  89. } else { 
  90. wp_enqueue_style( 'mailchimp-for-wp-form', MC4WP_LITE_PLUGIN_URL . 'assets/css/form' . $suffix . '.css', array(), MC4WP_LITE_VERSION, 'all' ); 
  91.  
  92. return true; 
  93.  
  94. /** 
  95. * Returns the MailChimp for WP form mark-up 
  96. * @param array $attributes 
  97. * @param string $content 
  98. * @return string 
  99. */ 
  100. public function output_form( $attributes = array(), $content = '' ) { 
  101.  
  102. global $is_IE; 
  103.  
  104. // increase count of outputted forms 
  105. $this->outputted_forms_count++; 
  106.  
  107. $attributes = shortcode_atts( 
  108. array( 
  109. 'id' => 0,  
  110. 'element_id' => 'mc4wp-form-' . $this->outputted_forms_count,  
  111. ),  
  112. $attributes,  
  113. 'mc4wp_form' 
  114. ); 
  115.  
  116. // create or retrieve form instance 
  117. $form = MC4WP_Form::get(); 
  118.  
  119. // make sure to print date fallback later on if form contains a date field 
  120. if( $form->contains_field_type( 'date' ) ) { 
  121. $this->print_date_fallback = true; 
  122.  
  123. // was form submited? 
  124. if( $form->is_submitted( $attributes['element_id'] ) ) { 
  125.  
  126. // enqueue scripts (in footer) if form was submitted 
  127. $animate_scroll = apply_filters( 'mc4wp_form_animate_scroll', true ); 
  128.  
  129. wp_enqueue_script( 'mc4wp-form-request' ); 
  130. wp_localize_script( 'mc4wp-form-request', 'mc4wpFormRequestData', array( 
  131. 'success' => ( $form->request->success ) ? 1 : 0,  
  132. 'formElementId' => $form->request->form_element_id,  
  133. 'data' => $form->request->user_data,  
  134. 'animate_scroll' => $animate_scroll 
  135. ); 
  136.  
  137.  
  138. // Print small JS snippet later on in the footer. 
  139. add_action( 'wp_footer', array( $this, 'print_js' ), 99 ); 
  140.  
  141. // output form 
  142. return $form->output( $attributes['element_id'], $attributes, false ); 
  143.  
  144. /** 
  145. * Prints some JavaScript to enhance the form functionality 
  146. * This is only printed on pages that actually contain a form. 
  147. */ 
  148. public function print_js() { 
  149.  
  150. if( $this->inline_js_printed === true ) { 
  151. return false; 
  152.  
  153. // Print vanilla JavaScript 
  154. ?><script type="text/javascript"> 
  155. (function() { 
  156. function addSubmittedClassToFormContainer(e) { 
  157. var form = e.target.form.parentNode; 
  158. var className = 'mc4wp-form-submitted'; 
  159. (form.classList) ? form.classList.add(className) : form.className += ' ' + className; 
  160.  
  161. var forms = document.querySelectorAll('.mc4wp-form'); 
  162. for (var i = 0; i < forms.length; i++) { 
  163. (function(f) { 
  164.  
  165. /* add class on submit */ 
  166. var b = f.querySelector('[type="submit"]'); 
  167. if(b.length > 0 ) { 
  168. if(b.addEventListener) { 
  169. b.addEventListener('click', addSubmittedClassToFormContainer); 
  170. } else { 
  171. b.attachEvent('click', addSubmittedClassToFormContainer); 
  172.  
  173. })(forms[i]); 
  174. })(); 
  175.  
  176. <?php if( $this->print_date_fallback ) { ?> 
  177. (function() { 
  178. /* test if browser supports date fields */ 
  179. var testInput = document.createElement('input'); 
  180. testInput.setAttribute('type', 'date'); 
  181. if( testInput.type !== 'date') { 
  182.  
  183. /* add placeholder & pattern to all date fields */ 
  184. var dateFields = document.querySelectorAll('.mc4wp-form input[type="date"]'); 
  185. for(var i=0; i<dateFields.length; i++) { 
  186. if(!dateFields[i].placeholder) { 
  187. dateFields[i].placeholder = 'yyyy/mm/dd'; 
  188. if(!dateFields[i].pattern) { 
  189. dateFields[i].pattern = '(?:19|20)[0-9]{2}/(?:(?:0[1-9]|1[0-2])/(?:0[1-9]|1[0-9]|2[0-9])|(?:(?!02)(?:0[1-9]|1[0-2])/(?:30))|(?:(?:0[13578]|1[02])-31))'; 
  190. })(); 
  191. <?php } ?> 
  192. </script><?php 
  193.  
  194. // make sure this function only runs once 
  195. $this->inline_js_printed = true; 
  196. return true; 
  197.