NF_AJAX_Controllers_Submission

The Ninja Forms NF AJAX Controllers Submission class.

Defined (1)

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

/includes/AJAX/Controllers/Submission.php  
  1. class NF_AJAX_Controllers_Submission extends NF_Abstracts_Controller 
  2. protected $_form_data = array(); 
  3.  
  4. protected $_preview_data = array(); 
  5.  
  6. protected $_form_id = ''; 
  7.  
  8. public function __construct() 
  9. if( isset( $_POST[ 'nf_resume' ] ) && isset( $_COOKIE[ 'nf_wp_session' ] ) ) { 
  10. add_action( 'ninja_forms_loaded', array( $this, 'resume' ) ); 
  11. return; 
  12.  
  13. if( isset( $_POST['formData'] ) ) { 
  14. $this->_form_data = json_decode( $_POST['formData'], TRUE ); 
  15.  
  16. // php5.2 fallback 
  17. if( ! $this->_form_data ) $this->_form_data = json_decode( stripslashes( $_POST['formData'] ), TRUE ); 
  18.  
  19.  
  20. add_action( 'wp_ajax_nf_ajax_submit', array( $this, 'submit' ) ); 
  21. add_action( 'wp_ajax_nopriv_nf_ajax_submit', array( $this, 'submit' ) ); 
  22.  
  23. add_action( 'wp_ajax_nf_ajax_resume', array( $this, 'resume' ) ); 
  24. add_action( 'wp_ajax_nopriv_nf_ajax_resume', array( $this, 'resume' ) ); 
  25.  
  26. public function submit() 
  27. check_ajax_referer( 'ninja_forms_display_nonce', 'security' ); 
  28.  
  29. if( ! $this->_form_data ) { 
  30.  
  31. if( function_exists( 'json_last_error' ) // Function not supported in php5.2 
  32. && function_exists( 'json_last_error_msg' )// Function not supported in php5.2 
  33. && json_last_error() ) { 
  34. $this->_errors[] = json_last_error_msg(); 
  35. } else { 
  36. $this->_errors[] = __( 'An unexpected error occurred.', 'ninja-forms' ); 
  37.  
  38. $this->_respond(); 
  39.  
  40. $this->_form_id = $this->_data[ 'form_id' ] = $this->_form_data['id']; 
  41.  
  42. if( isset( $this->_form_data[ 'settings' ][ 'is_preview' ] ) && $this->_form_data[ 'settings' ][ 'is_preview' ] ) { 
  43. $this->_preview_data = get_user_option( 'nf_form_preview_' . $this->_form_id ); 
  44.  
  45. // Add preview field keys to form data. 
  46. foreach( $this->_form_data[ 'fields' ] as $key => $field ) { 
  47. $field_id = $field[ 'id' ]; 
  48. $this->_form_data[ 'fields' ][ $key ][ 'key' ] = $this->_preview_data[ 'fields' ][ $field_id ][ 'settings' ][ 'key' ]; 
  49.  
  50. if( ! $this->_preview_data ) { 
  51. $this->_errors[ 'preview' ] = __( 'Preview does not exist.', 'ninja-forms' ); 
  52. $this->_respond(); 
  53.  
  54. $this->_data['settings'] = $this->_form_data['settings']; 
  55.  
  56. $this->_data['extra'] = $this->_form_data['extra']; 
  57.  
  58. $this->_data['fields'] = $this->_form_data['fields']; 
  59.  
  60. $this->_data = apply_filters( 'ninja_forms_submit_data', $this->_data ); 
  61.  
  62. $this->validate_fields(); 
  63.  
  64. $this->process_fields(); 
  65.  
  66. $this->process(); 
  67.  
  68. public function resume() 
  69. $this->_data = Ninja_Forms()->session()->get( 'nf_processing_data' ); 
  70. $this->_data[ 'resume' ] = $_POST[ 'nf_resume' ]; 
  71.  
  72. $this->_form_id = $this->_data[ 'form_id' ]; 
  73.  
  74. unset( $this->_data[ 'halt' ] ); 
  75. unset( $this->_data[ 'actions' ][ 'redirect' ] ); 
  76.  
  77. $this->process(); 
  78.  
  79. protected function process() 
  80. foreach( $this->_data[ 'fields' ] as $field_id => $field ) { 
  81. if( $this->_preview_data ) { 
  82. if( ! isset( $this->_preview_data[ 'fields' ][ $field[ 'id' ] ][ 'settings' ] ) ) return; 
  83. $settings = $this->_preview_data[ 'fields' ][ $field[ 'id' ] ][ 'settings' ]; 
  84. } else { 
  85. $field_model = Ninja_Forms()->form()->field($field['id'])->get(); 
  86. $settings = $field_model->get_settings(); 
  87. $this->_data[ 'fields' ][ $field_id ] = array_merge( $this->_data[ 'fields' ][ $field_id ], $settings ); 
  88. $field_merge_tags = Ninja_Forms()->merge_tags[ 'fields' ]; 
  89. $this->populate_field_merge_tags( $this->_data['fields'], $field_merge_tags ); 
  90.  
  91. if( isset( $this->_data[ 'settings' ][ 'calculations' ] ) ) { 
  92. $calcs_merge_tags = Ninja_Forms()->merge_tags[ 'calcs' ]; 
  93. $this->populate_calcs_merge_tags( $this->_data[ 'settings' ][ 'calculations' ], $calcs_merge_tags ); 
  94.  
  95. if( isset( $this->_data[ 'settings' ][ 'is_preview' ] ) && $this->_data[ 'settings' ][ 'is_preview' ] ) { 
  96. $this->run_actions_preview(); 
  97. } else { 
  98. $this->run_actions(); 
  99.  
  100. do_action( 'ninja_forms_after_submission', $this->_data ); 
  101.  
  102. $this->_respond(); 
  103.  
  104. protected function populate_field_merge_tags( $fields, $field_merge_tags ) 
  105. foreach( $fields as $field ) { 
  106.  
  107. $field_merge_tags->add_field( $field ); 
  108.  
  109. protected function populate_calcs_merge_tags( $calcs, $calcs_merge_tags ) 
  110. foreach( $calcs as $calc ) { 
  111.  
  112. $calcs_merge_tags->set_merge_tags( $calc[ 'name' ], apply_filters( 'ninja_forms_calc_setting', $calc[ 'eq' ] ) ); 
  113.  
  114. protected function validate_fields() 
  115. foreach( $this->_data['fields'] as $field ) { 
  116.  
  117. $errors = $this->validate_field( $field, $this->_data ); 
  118.  
  119. if( ! empty( $errors ) ) { 
  120. $this->_errors[ 'fields' ][ $field['id'] ] = $errors; 
  121.  
  122. if( $this->_errors ) $this->_respond(); 
  123.  
  124. protected function validate_field( $field, $data ) 
  125. if( $this->_preview_data ) { 
  126. if( ! isset( $this->_preview_data[ 'fields' ][ $field[ 'id' ] ][ 'settings' ] ) ) return; 
  127. $settings = $this->_preview_data[ 'fields' ][ $field[ 'id' ] ][ 'settings' ]; 
  128. } else { 
  129. $field_model = Ninja_Forms()->form()->field($field['id'])->get(); 
  130. $settings = $field_model->get_settings(); 
  131.  
  132. $field = apply_filters( 'ninja_forms_pre_validate_field_settings', array_merge($field, $settings ) ); 
  133.  
  134. $field_class = Ninja_Forms()->fields[ $field['type'] ]; 
  135.  
  136. return $errors = $field_class->validate( $field, $data ); 
  137.  
  138. protected function process_fields() 
  139. foreach( $this->_data['fields'] as $field ) { 
  140.  
  141. $data = $this->process_field( $field, $this->_data ); 
  142.  
  143. if( ! empty( $data ) ) { 
  144. $this->_data = $data; 
  145.  
  146. protected function process_field( $field, $data ) 
  147. if( $this->_preview_data ) { 
  148. if( ! isset( $this->_preview_data[ 'fields' ][ $field[ 'id' ] ][ 'settings' ] ) ) return; 
  149. $settings = $this->_preview_data[ 'fields' ][ $field[ 'id' ] ][ 'settings' ]; 
  150. } else { 
  151. $field_model = Ninja_Forms()->form()->field($field['id'])->get(); 
  152. $settings = $field_model->get_settings(); 
  153.  
  154. $field = array_merge($field, $settings ); 
  155.  
  156. $field_class = Ninja_Forms()->fields[ $field['type'] ]; 
  157.  
  158.  
  159. return $field_class->process( $field, $data ); 
  160.  
  161. protected function run_actions() 
  162. $actions = Ninja_Forms()->form( $this->_form_id )->get_actions(); 
  163.  
  164. $actions = apply_filters( 'ninja_forms_submission_actions', $actions, $this->_form_data ); 
  165.  
  166. usort($actions, array($this, 'sort_form_actions')); 
  167.  
  168. if( ! isset( $this->_data[ 'processed_actions' ] ) ) $this->_data[ 'processed_actions' ] = array(); 
  169. foreach( $actions as $action ) { 
  170.  
  171. if( in_array( $action->get_id(), $this->_data[ 'processed_actions' ] ) ) continue; 
  172.  
  173. $action_settings = apply_filters( 'ninja_forms_run_action_settings', $action->get_settings(), $this->_form_id, $action->get_id(), $this->_data['settings'] ); 
  174.  
  175. if( ! $action_settings['active'] ) continue; 
  176.  
  177. $type = $action_settings['type']; 
  178.  
  179. if( ! apply_filters( 'ninja_forms_run_action_type_' . $type, TRUE ) ) continue; 
  180.  
  181. $action_settings[ 'id' ] = $action->get_id(); 
  182.  
  183. if( ! isset( Ninja_Forms()->actions[ $type ] ) ) continue; 
  184.  
  185. $data = Ninja_Forms()->actions[$type]->process($action_settings, $this->_form_id, $this->_data); 
  186.  
  187. $this->_data = ( $data ) ? $data : $this->_data; 
  188.  
  189. $this->maybe_halt( $action->get_id() ); 
  190.  
  191. protected function run_actions_preview() 
  192. $form = get_user_option( 'nf_form_preview_' . $this->_form_id ); 
  193.  
  194. if( ! isset( $form[ 'actions' ] ) || empty( $form[ 'actions' ] ) ) return; 
  195.  
  196. foreach( $this->_form_data[ 'fields' ] as $key => $field ) { 
  197. if( ! isset( $form[ 'fields' ] ) ) continue; 
  198. $field_tmp_id = $field[ 'id' ]; 
  199. $field_key = $form[ 'fields' ][ $field_tmp_id ][ 'settings' ][ 'key' ]; 
  200. $this->_form_data[ 'fields' ][ $key ][ 'key' ] = $field_key; 
  201.  
  202. $form[ 'actions' ] = apply_filters( 'ninja_forms_submission_actions_preview', $form[ 'actions' ], array_merge( $form, $this->_form_data ) ); 
  203.  
  204. usort( $form[ 'actions' ], array( $this, 'sort_form_actions' ) ); 
  205.  
  206. if( ! isset( $this->_data[ 'processed_actions' ] ) ) $this->_data[ 'processed_actions' ] = array(); 
  207. foreach( $form[ 'actions' ] as $action_id => $action ) { 
  208.  
  209. if( in_array( $action_id, $this->_data[ 'processed_actions' ] ) ) continue; 
  210.  
  211. $action_settings = apply_filters( 'ninja_forms_run_action_settings_preview', $action[ 'settings' ], $this->_form_id, '', $this->_data['settings'] ); 
  212.  
  213. if( ! $action_settings['active'] ) continue; 
  214.  
  215. $type = $action_settings['type']; 
  216.  
  217. if ( ! isset( Ninja_Forms()->actions[ $type ] ) ) { 
  218. continue; 
  219.  
  220. $data = Ninja_Forms()->actions[ $type ]->process( $action_settings, $this->_form_id, $this->_data ); 
  221.  
  222. $this->_data = ( $data ) ? $data : $this->_data; 
  223.  
  224. $this->maybe_halt( $action_id ); 
  225.  
  226. protected function maybe_halt( $action_id ) 
  227. if( isset( $this->_data[ 'errors' ] ) && $this->_data[ 'errors' ] ) { 
  228. $this->_respond(); 
  229.  
  230. if( isset( $this->_data[ 'halt' ] ) && $this->_data[ 'halt' ] ) { 
  231.  
  232. Ninja_Forms()->session()->set( 'nf_processing_data', $this->_data ); 
  233.  
  234. $this->_respond(); 
  235.  
  236. array_push( $this->_data[ 'processed_actions' ], $action_id ); 
  237.  
  238. protected function sort_form_actions( $a, $b ) 
  239. if( is_object( $a ) ) { 
  240. if( ! isset( Ninja_Forms()->actions[ $a->get_setting( 'type' ) ] ) ) return -1; 
  241. $a = Ninja_Forms()->actions[ $a->get_setting( 'type' ) ]; 
  242. } else { 
  243. if( ! isset( Ninja_Forms()->actions[ $a[ 'settings' ][ 'type' ] ] ) ) return -1; 
  244. $a = Ninja_Forms()->actions[ $a[ 'settings' ][ 'type' ] ]; 
  245.  
  246. if( is_object( $b ) ) { 
  247. if( ! isset( Ninja_Forms()->actions[ $b->get_setting( 'type' ) ] ) ) return 1; 
  248. $b = Ninja_Forms()->actions[ $b->get_setting( 'type' ) ]; 
  249. } else { 
  250. if( ! isset( Ninja_Forms()->actions[ $b[ 'settings' ][ 'type' ] ] ) ) return 1; 
  251. $b = Ninja_Forms()->actions[ $b[ 'settings' ][ 'type' ] ]; 
  252.  
  253. if ( $a->get_timing() == $b->get_timing() ) { 
  254. if ( $a->get_priority() == $b->get_priority() ) { 
  255. return 0; 
  256. return ( $a->get_priority() < $b->get_priority() ) ? -1 : 1; 
  257.  
  258. return ( $a->get_timing() < $b->get_timing() ) ? -1 : 1;