Ninja_Forms_Processing

Ninja Forms Processing class.

Defined (1)

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

/deprecated/includes/display/processing/class-ninja-forms-processing.php  
  1. class Ninja_Forms_Processing { 
  2.  
  3. /** 
  4. * Stores the data accessed by the other parts of the class. 
  5. * All response messages will be stored in this value. 
  6. * @var array 
  7. * @access private 
  8. */ 
  9. var $data = array(); 
  10.  
  11. /** 
  12. * Constructor - Sets up the form ID. 
  13. * If the form_ID parameter is empty then nothing will be done. 
  14. */ 
  15. function __construct($form_ID = '') { 
  16. if(empty($form_ID)) { 
  17. return false; 
  18. }else{ 
  19. $this->data['form_ID'] = $form_ID; 
  20. $user_ID = get_current_user_id(); 
  21. if(!$user_ID) { 
  22. $user_ID = ''; 
  23. $this->data['user_ID'] = $user_ID; 
  24.  
  25. /** 
  26. * Add the submitted vars to $this->data['fields']. 
  27. * Also runs any functions registered to the field's pre_process hook. 
  28. */ 
  29. function setup_submitted_vars() { 
  30. global $ninja_forms_fields, $wp; 
  31. $form_ID = $this->data['form_ID']; 
  32.  
  33. //Get our plugin settings 
  34. $plugin_settings = nf_get_settings(); 
  35. $req_field_error = __( $plugin_settings['req_field_error'], 'ninja-forms' ); 
  36.  
  37. if ( empty ( $this->data ) ) 
  38. return ''; 
  39.  
  40. $this->data['action'] = 'submit'; 
  41. $this->data['form']['form_url'] = $this->get_current_url(); 
  42. $cache = ( Ninja_Forms()->session->get( 'nf_cache' ) ) ? Ninja_Forms()->session->get( 'nf_cache' ) : null; 
  43.  
  44. // If we have fields in our $_POST object, then loop through the $_POST'd field values and add them to our global variable. 
  45. if ( isset ( $_POST['_ninja_forms_display_submit'] ) OR isset ( $_POST['_ninja_forms_edit_sub'] ) ) { 
  46. $field_results = ninja_forms_get_fields_by_form_id($form_ID); 
  47. //$field_results = apply_filters('ninja_forms_display_fields_array', $field_results, $form_ID); 
  48.  
  49. foreach( $field_results as $field ) { 
  50. $data = $field['data']; 
  51. $field_id = $field['id']; 
  52. $field_type = $field['type']; 
  53.  
  54. if ( isset ( $_POST['ninja_forms_field_' . $field_id ] ) ) { 
  55. $val = ninja_forms_stripslashes_deep( $_POST['ninja_forms_field_' . $field_id ] ); 
  56. $this->data['submitted_fields'][] = $field_id; 
  57. } else { 
  58. $val = false; 
  59.  
  60. $val = nf_wp_kses_post_deep( $val ); 
  61.  
  62. $this->data['fields'][$field_id] = $val; 
  63. $field_row = ninja_forms_get_field_by_id( $field_id ); 
  64. $field_row['data']['field_class'] = 'ninja-forms-field'; 
  65. $this->data['field_data'][$field_id] = $field_row; 
  66.  
  67. foreach($_POST as $key => $val) { 
  68. if(substr($key, 0, 1) == '_') { 
  69. $this->data['extra'][$key] = $val; 
  70.  
  71. //Grab the form info from the database and store it in our global form variables. 
  72. $form_row = ninja_forms_get_form_by_id($form_ID); 
  73. $form_data = $form_row['data']; 
  74.  
  75. if(isset($_REQUEST['_sub_id']) AND !empty($_REQUEST['_sub_id'])) { 
  76. $form_data['sub_id'] = absint ( $_REQUEST['_sub_id'] ); 
  77. }else{ 
  78. $form_data['sub_id'] = ''; 
  79.  
  80. //Loop through the form data and set the global $ninja_form_data variable. 
  81. if(is_array($form_data) AND !empty($form_data)) { 
  82. foreach($form_data as $key => $val) { 
  83. if(!is_array($val)) { 
  84. $value = stripslashes($val); 
  85. $value = nf_wp_kses_post_deep( $value ); 
  86. //$value = htmlspecialchars($value); 
  87. }else{ 
  88. $value = nf_wp_kses_post_deep( $val ); 
  89. $this->data['form'][$key] = $value; 
  90. $this->data['form']['admin_attachments'] = array(); 
  91. $this->data['form']['user_attachments'] = array(); 
  92.  
  93. } else if ( $cache !== null ) { // Check to see if we have cached values from a submission. 
  94. if ( is_array ( $cache['field_values'] ) ) { 
  95. // We do have a submission contained in our cache. We'll populate the field values with that data. 
  96. foreach ( $cache['field_values'] as $field_id => $val ) { 
  97. $field_row = ninja_forms_get_field_by_id($field_id); 
  98. if(is_array($field_row) AND !empty($field_row)) { 
  99. if(isset($field_row['type'])) { 
  100. $field_type = $field_row['type']; 
  101. }else{ 
  102. $field_type = ''; 
  103. if(isset($field_row['data']['req'])) { 
  104. $req = $field_row['data']['req']; 
  105. }else{ 
  106. $req = ''; 
  107.  
  108. $val = ninja_forms_stripslashes_deep( $val ); 
  109. $val = nf_wp_kses_post_deep( $val ); 
  110.  
  111. $this->data['fields'][$field_id] = $val; 
  112. if ( isset ( $cache['field_settings'][$field_id] ) ) { 
  113. $field_row = $cache['field_settings'][$field_id]; 
  114. } else { 
  115. $field_row = ninja_forms_get_field_by_id( $field_id ); 
  116.  
  117. $field_row['data']['field_class'] = 'ninja-forms-field'; 
  118.  
  119. $this->data['field_data'][$field_id] = $field_row; 
  120. $this->data['form'] = $cache['form_settings']; 
  121. $this->data['success'] = $cache['success_msgs']; 
  122. $this->data['errors'] = $cache['error_msgs']; 
  123. $this->data['extra'] = $cache['extra_values']; 
  124.  
  125.  
  126. } // Submitted Vars function 
  127.  
  128. /** 
  129. * Submitted Values Methods: 
  130. **/ 
  131.  
  132. /** 
  133. * Retrieve the form ID of the form currently being processed. 
  134. */ 
  135. function get_form_ID() { 
  136. if ( empty($this->data) ) { 
  137. return false; 
  138. }else{ 
  139. return $this->data['form_ID']; 
  140.  
  141. /** 
  142. * Retrieve the User ID of the form currently being processed. 
  143. */ 
  144. function get_user_ID() { 
  145. if ( empty($this->data) ) { 
  146. return false; 
  147. }else{ 
  148. return $this->data['user_ID']; 
  149.  
  150. /** 
  151. * Set the User ID of the form currently being processed. 
  152. */ 
  153. function set_user_ID( $user_id ) { 
  154. if ( empty($this->data) ) { 
  155. return false; 
  156. }else{ 
  157. return $this->data['user_ID'] = $user_id; 
  158.  
  159. /** 
  160. * Retrieve the action currently being performed. 
  161. */ 
  162. function get_action() { 
  163. if ( empty($this->data['action']) ) { 
  164. return false; 
  165. }else{ 
  166. return $this->data['action']; 
  167.  
  168. /** 
  169. * Set the action currently being performed. 
  170. */ 
  171. function set_action( $action ) { 
  172. if ( empty($this->data) ) { 
  173. return false; 
  174. }else{ 
  175. return $this->data['action'] = $action; 
  176.  
  177. /** 
  178. * Retrieve all the fields attached to a form. 
  179. */ 
  180. function get_all_fields() { 
  181. if ( empty($this->data['fields']) ) { 
  182. return false; 
  183. }else{ 
  184. return $this->data['fields']; 
  185.  
  186. /** 
  187. * Retrieve all the user submitted form data. 
  188. */ 
  189. function get_all_submitted_fields() { 
  190. if ( empty( $this->data['submitted_fields'] ) ) { 
  191. return false; 
  192. } else { 
  193. $fields = array(); 
  194. $submitted_fields = $this->data['submitted_fields']; 
  195. foreach ( $submitted_fields as $field_id ) { 
  196. if ( isset ( $this->data['fields'][$field_id] ) ) { 
  197. $fields[$field_id] = $this->data['fields'][$field_id]; 
  198. return $fields; 
  199.  
  200.  
  201. /** 
  202. * Retrieve user submitted form data by field ID. 
  203. */ 
  204. function get_field_value($field_ID = '') { 
  205. if(empty($this->data) OR $field_ID == '' OR !isset($this->data['fields'][$field_ID])) { 
  206. return false; 
  207. }else{ 
  208. return $this->data['fields'][$field_ID]; 
  209.  
  210. /** 
  211. * Change the value of a field. 
  212. */ 
  213. function update_field_value($field_ID = '', $new_value = '') { 
  214. if(empty($this->data) OR $field_ID == '') { 
  215. return false; 
  216. }else{ 
  217. $this->data['fields'][$field_ID] = $new_value; 
  218. return true; 
  219.  
  220. /** 
  221. * Remove a field and its value from the user submissions. 
  222. */ 
  223. function remove_field_value($field_ID = '') { 
  224. if(empty($this->data) OR $field_ID == '') { 
  225. return false; 
  226. }else{ 
  227. unset($this->data['fields'][$field_ID]); 
  228. return true; 
  229.  
  230. /** 
  231. * Retrieve field data by field ID. This data includes all of the information entered in the admin back-end. 
  232. */ 
  233. function get_field_settings($field_ID = '') { 
  234. if(empty($this->data) OR $field_ID == '' OR !isset($this->data['field_data'][$field_ID])) { 
  235. return false; 
  236. }else{ 
  237. return $this->data['field_data'][$field_ID]; 
  238.  
  239. /** 
  240. * Retrieve a specific piece of field setting data. 
  241. * @since 2.2.45 
  242. * @return $value or bool(false) 
  243. */ 
  244. function get_field_setting( $field_id = '', $setting_id = '' ) { 
  245. if ( empty ( $this->data ) OR $field_id == '' OR $setting_id == '' ) 
  246. return false; 
  247. if ( isset ( $this->data['field_data'][$field_id][$setting_id] ) ) { 
  248. return $this->data['field_data'][$field_id][$setting_id]; 
  249. } else if ( isset ( $this->data['field_data'][$field_id]['data'][$setting_id] ) ) { 
  250. return $this->data['field_data'][$field_id]['data'][$setting_id]; 
  251. } else { 
  252. return false; 
  253.  
  254. /** 
  255. * Update field data by field ID. This data includes all of the informatoin entered into the admin back-end. (Please note that the changes made with these methods only affect the current process and DO NOT permanently change these settings): 
  256. */ 
  257. function update_field_settings($field_ID = '', $new_value = '') { 
  258. if(empty($this->data) OR $field_ID == '') { 
  259. return false; 
  260. }else{ 
  261. $this->data['field_data'][$field_ID] = $new_value; 
  262. return true; 
  263.  
  264. /** 
  265. * Update a specific piece of field setting data by giving the field id and setting id. 
  266. * @since 2.2.45 
  267. * @return void or bool(false) 
  268. */ 
  269. function update_field_setting( $field_id = '', $setting_id = '', $value = '' ) { 
  270. if( empty( $this->data ) OR $field_id == '' OR $setting_id == '' OR $value == '' ) 
  271. return false; 
  272.  
  273. if ( isset ( $this->data['field_data'][$field_id][$setting_id] ) ) { 
  274. $this->data['field_data'][$field_id][$setting_id] = $value; 
  275. } else { 
  276. $this->data['field_data'][$field_id]['data'][$setting_id] = $value; 
  277.  
  278.  
  279. /** 
  280. * Extra Form Values Methods 
  281. **/ 
  282.  
  283. /** 
  284. * Retrieve all the extra submitted form data. 
  285. */ 
  286. function get_all_extras() { 
  287. if ( empty($this->data['extra']) ) { 
  288. return false; 
  289. }else{ 
  290. return $this->data['extra']; 
  291.  
  292.  
  293. /** 
  294. * Retrieve user submitted form data by field ID. 
  295. */ 
  296. function get_extra_value($name = '') { 
  297. if(empty($this->data) OR $name == '' OR !isset($this->data['extra'][$name])) { 
  298. return false; 
  299. }else{ 
  300. return $this->data['extra'][$name]; 
  301.  
  302. /** 
  303. * Change the value of a field. 
  304. */ 
  305. function update_extra_value($name = '', $new_value = '') { 
  306. if(empty($this->data) OR $name == '') { 
  307. return false; 
  308. }else{ 
  309. $this->data['extra'][$name] = $new_value; 
  310. return true; 
  311.  
  312. /** 
  313. * Remove a field and its value from the user submissions. 
  314. */ 
  315. function remove_extra_value($name = '') { 
  316. if(empty($this->data) OR $name == '') { 
  317. return false; 
  318. }else{ 
  319. unset($this->data['extra'][$name]); 
  320. return true; 
  321.  
  322.  
  323. /** 
  324. * Form Settings Methods (Please note that the changes made with these methods only affect the current process and DO NOT permanently change these settings): 
  325. **/ 
  326.  
  327. /** 
  328. * Retrieve all the settings for the form currently being processed. 
  329. */ 
  330. function get_all_form_settings() { 
  331. if(empty($this->data['form']) OR !isset($this->data['form'])) { 
  332. return false; 
  333. }else{ 
  334. return $this->data['form']; 
  335.  
  336. /** 
  337. * Retrieve a form setting value by its unique ID. 
  338. */ 
  339. function get_form_setting($setting_ID) { 
  340. if(empty($this->data['form']) OR !isset($this->data['form'][$setting_ID])) { 
  341. return false; 
  342. }else{ 
  343. return $this->data['form'][$setting_ID]; 
  344.  
  345. /** 
  346. * Update a form setting value by its unique ID. 
  347. */ 
  348. function update_form_setting($setting_ID, $new_value = '') { 
  349. if(empty($this->data['form'])) { 
  350. return false; 
  351. }else{ 
  352. return $this->data['form'][$setting_ID] = $new_value; 
  353.  
  354. /** 
  355. * Remove a form setting value by its unique ID. 
  356. */ 
  357. function remove_form_setting($setting_ID, $new_value = '') { 
  358. if(empty($this->data['form']) OR !isset($this->data['form'][$setting_ID])) { 
  359. return false; 
  360. }else{ 
  361. unset($this->data['form'][$setting_ID]); 
  362. return true; 
  363.  
  364. /** 
  365. * Error Reporting Methods: 
  366. **/ 
  367.  
  368. /** 
  369. * Retrieve all error messages. 
  370. */ 
  371. function get_all_errors() { 
  372. if(empty($this->data['errors']) OR !isset($this->data['errors'])) { 
  373. return false; 
  374. }else{ 
  375. return $this->data['errors']; 
  376.  
  377. /** 
  378. * Retrieve an error message and location by its unique ID. 
  379. */ 
  380. function get_error($error_ID = '') { 
  381. if(empty($this->data['errors']) OR !isset($this->data['errors'][$error_ID]) OR $error_ID == '') { 
  382. return false; 
  383. }else{ 
  384. return $this->data['errors'][$error_ID]; 
  385.  
  386.  
  387. /** 
  388. * Retrieve an array of error_IDs and messages by display location. 
  389. */ 
  390. function get_errors_by_location($error_location = '') { 
  391. $tmp_array = array(); 
  392. if(empty($this->data['errors']) OR !isset($this->data['errors']) OR $error_location == '') { 
  393. return false; 
  394. }else{ 
  395. foreach($this->data['errors'] as $ID => $error) { 
  396. if($error['location'] == $error_location) { 
  397. $tmp_array[$ID] = $error; 
  398. if(!empty($tmp_array)) { 
  399. return $tmp_array; 
  400. }else{ 
  401. return false; 
  402.  
  403.  
  404. /** 
  405. * Add an error message. 
  406. */ 
  407. function add_error($error_ID, $error_msg, $error_location = 'general') { 
  408. $this->data['errors'][$error_ID]['msg'] = $error_msg; 
  409. $this->data['errors'][$error_ID]['location'] = $error_location; 
  410. return true; 
  411.  
  412. /** 
  413. * Remove an error message by its unique ID. 
  414. */ 
  415. function remove_error($error_ID = '') { 
  416. if(empty($this->data['errors']) OR !isset($this->data['errors']) OR $error_ID == '') { 
  417. return false; 
  418. }else{ 
  419. unset($this->data['errors'][$error_ID]); 
  420. return true; 
  421.  
  422. /** 
  423. * Remove all set error messages. 
  424. */ 
  425. function remove_all_errors() { 
  426. if(empty($this->data['errors']) OR !isset($this->data['errors'])) { 
  427. return true; 
  428. }else{ 
  429. $this->data['errors'] = array(); 
  430. return true; 
  431.  
  432. /** 
  433. * Success Reporting Methods: 
  434. **/ 
  435.  
  436. /** 
  437. * Retrieve all success messages. 
  438. */ 
  439. function get_all_success_msgs() { 
  440. if(empty($this->data['success']) OR !isset($this->data['success'])) { 
  441. return false; 
  442. }else{ 
  443. return $this->data['success']; 
  444.  
  445. /** 
  446. * Retrieve a success message by unique ID. 
  447. */ 
  448. function get_success_msg($success_ID = '') { 
  449. if(empty($this->data['success']) OR !isset($this->data['success']) OR $success_ID == '') { 
  450. return array(); 
  451. }else{ 
  452. return $this->data['success'][$success_ID]; 
  453.  
  454. /** 
  455. * Add a success message. 
  456. */ 
  457. function add_success_msg($success_ID, $success_msg) { 
  458. $this->data['success'][$success_ID] = $success_msg; 
  459. return true; 
  460.  
  461. /** 
  462. * Remove a success message by its unique ID. 
  463. */ 
  464. function remove_success_msg($success_ID = '') { 
  465. if(empty($this->data['success']) OR !isset($this->data['success']) OR $success_ID == '') { 
  466. return false; 
  467. }else{ 
  468. unset($this->data['success'][$success_ID]); 
  469. return true; 
  470. }  
  471.  
  472. /** 
  473. * Remove all success messages 
  474. */ 
  475. function remove_all_success_msgs() { 
  476. if(empty($this->data['success']) OR !isset($this->data['success'])) { 
  477. return false; 
  478. }else{ 
  479. $this->data['success'] = array(); 
  480. return true; 
  481.  
  482. /** 
  483. * Function that returns an array of user information fields. 
  484. * @since 2.2.30 
  485. * @returns array $user_info 
  486. */ 
  487. function get_user_info() { 
  488. if ( !isset ( $this->data['field_data'] ) ) { 
  489. return false; 
  490. $user_info = array(); 
  491. foreach ( $this->data['field_data'] as $field ) { 
  492. $data = $field['data']; 
  493. $field_id = $field['id']; 
  494. $user_value = $this->get_field_value( $field_id ); 
  495. if ( isset ( $data['user_info_field_group'] ) AND $data['user_info_field_group'] == 1 ) { 
  496.  
  497. if ( isset ( $data['user_info_field_group_name'] ) ) { 
  498. $group_name = $data['user_info_field_group_name']; 
  499. } else { 
  500. $group_name = ''; 
  501.  
  502. if ( isset ( $data['user_info_field_group_custom'] ) ) { 
  503. $custom_group = $data['user_info_field_group_custom']; 
  504. } else { 
  505. $custom_group = ''; 
  506.  
  507. if ( $group_name == 'custom' ) { 
  508. $group_name = $custom_group; 
  509.  
  510. if ( $group_name != '' ) { 
  511. if ( isset ( $data['first_name'] ) AND $data['first_name'] == 1 ) { 
  512. $user_info[$group_name]['first_name'] = $user_value; 
  513. } else if ( isset ( $data['last_name'] ) AND $data['last_name'] == 1 ) { 
  514. $user_info[$group_name]['last_name'] = $user_value; 
  515. } else if ( isset ( $data['user_address_1'] ) AND $data['user_address_1'] == 1 ) { 
  516. $user_info[$group_name]['address_1'] = $user_value; 
  517. } else if ( isset ( $data['user_address_2'] ) AND $data['user_address_2'] == 1 ) { 
  518. $user_info[$group_name]['address_2'] = $user_value; 
  519. } else if ( isset ( $data['user_city'] ) AND $data['user_city'] == 1 ) { 
  520. $user_info[$group_name]['city'] = $user_value; 
  521. } else if ( isset ( $data['user_state'] ) AND $data['user_state'] == 1 ) { 
  522. $user_info[$group_name]['state'] = $user_value; 
  523. } else if ( isset ( $data['user_zip'] ) AND $data['user_zip'] == 1 ) { 
  524. $user_info[$group_name]['zip'] = $user_value; 
  525. } else if ( isset ( $data['user_email'] ) AND $data['user_email'] == 1 ) { 
  526. $user_info[$group_name]['email'] = $user_value; 
  527. } else if ( isset ( $data['user_phone'] ) AND $data['user_phone'] == 1 ) { 
  528. $user_info[$group_name]['phone'] = $user_value; 
  529. } else if ( $field['type'] == '_country' ) { 
  530. $user_info[$group_name]['country'] = $user_value; 
  531. }  
  532. } else { 
  533. if ( isset ( $data['first_name'] ) AND $data['first_name'] == 1 ) { 
  534. $user_info['first_name'] = $user_value; 
  535. } else if ( isset ( $data['last_name'] ) AND $data['last_name'] == 1 ) { 
  536. $user_info['last_name'] = $user_value; 
  537. } else if ( isset ( $data['user_address_1'] ) AND $data['user_address_1'] == 1 ) { 
  538. $user_info['address_1'] = $user_value; 
  539. } else if ( isset ( $data['user_address_2'] ) AND $data['user_address_2'] == 1 ) { 
  540. $user_info['address_2'] = $user_value; 
  541. } else if ( isset ( $data['user_city'] ) AND $data['user_city'] == 1 ) { 
  542. $user_info['city'] = $user_value; 
  543. } else if ( isset ( $data['user_state'] ) AND $data['user_state'] == 1 ) { 
  544. $user_info['state'] = $user_value; 
  545. } else if ( isset ( $data['user_zip'] ) AND $data['user_zip'] == 1 ) { 
  546. $user_info['zip'] = $user_value; 
  547. } else if ( isset ( $data['user_email'] ) AND $data['user_email'] == 1 ) { 
  548. $user_info['email'] = $user_value; 
  549. } else if ( isset ( $data['user_phone'] ) AND $data['user_phone'] == 1 ) { 
  550. $user_info['phone'] = $user_value; 
  551. } else if ( $field['type'] == '_country' ) { 
  552. $user_info['country'] = $user_value; 
  553. return $user_info; 
  554.  
  555. /** 
  556. * Function that returns the value of a calculation field and optionally the fields that contributed to that value. 
  557. * @since 2.2.30 
  558. * @returns $calc 
  559. */ 
  560. function get_calc( $name, $array = true ) { 
  561. if ( !isset ( $this->data['field_data'] ) ) { 
  562. return false; 
  563.  
  564. if ( $name == '' ) { 
  565. return false; 
  566.  
  567. // Check to see if we have a name or an ID. 
  568. if ( is_numeric ( $name ) AND isset ( $this->data['field_data'][$name] ) ) { 
  569. // We have an ID. 
  570. $calc_id = $name; 
  571. $calc_row = $this->get_field_settings( $calc_id ); 
  572. $places = $calc_row['data']['calc_places']; 
  573. } else { 
  574. // Search for our field by name. 
  575. $calc_id = ''; 
  576. foreach ( $this->data['field_data'] as $field ) { 
  577. if ( $field['type'] == '_calc' AND $field['calc_name'] == $name ) { 
  578. $calc_id = $field['id']; 
  579. $places = $field['data']['calc_places']; 
  580. break; 
  581. if ( $calc_id == '' ) { 
  582. return false; 
  583. $fields = $this->get_calc_fields( $calc_id ); 
  584. $total = number_format( round( $this->get_field_value( $calc_id ), $places ), $places ); 
  585.  
  586. if ( $array ) { 
  587. $calc = array( 'total' => $total, 'fields' => $fields ); 
  588. } else { 
  589. $calc = $total; 
  590.  
  591. return $calc; 
  592.  
  593. /** 
  594. * Function that returns the "total" field value if it exists. 
  595. * @since 2.2.30 
  596. * @returns array $total 
  597. */ 
  598. function get_calc_total( $array = true, $add_tax = true ) { 
  599. if ( !isset ( $this->data['field_data'] ) ) { 
  600. return false; 
  601. $total_field = ''; 
  602.  
  603. // Get our sub total. 
  604. $sub_total = $this->get_calc_sub_total( false ); 
  605.  
  606. // Get our tax rate. 
  607. $tax_rate = $this->get_calc_tax_rate(); 
  608.  
  609. foreach ( $this->data['field_data'] as $field ) { 
  610. $data = $field['data']; 
  611. $field_id = $field['id']; 
  612. $user_value = $this->get_field_value( $field_id ); 
  613.  
  614. if ( isset ( $data['payment_total'] ) AND $data['payment_total'] == 1 ) { 
  615. $calc_method = $data['calc_method']; 
  616. if ( isset ( $data['calc'] ) ) { 
  617. $calc_fields = $data['calc']; 
  618. $calc_eq = $data['calc_eq']; 
  619. $places = $data['calc_places']; 
  620. $total_field = $field_id; 
  621.  
  622. $total_value = number_format( round( $user_value, $places ), $places );  
  623.  
  624. break; 
  625. if ( $total_field == '' ) { 
  626. return false; 
  627. if ( $array ) { 
  628. // Get the list of fields that affected this value. 
  629. $fields = $this->get_calc_fields( $total_field ); 
  630.  
  631. $tmp_array = array(); 
  632. // Loop through the fields in that list and remove any that don't have a calc_option value of 1 
  633. foreach ( $fields as $field_id => $value ) { 
  634. $field_settings = $this->get_field_settings( $field_id ); 
  635. $field_value = $this->get_field_value( $field_id ); 
  636. $data = $field_settings['data']; 
  637. $tmp_array[$field_id] = $value; 
  638. $fields = $tmp_array; 
  639. $total = array(); 
  640.  
  641. if ( !$sub_total AND $tax_rate AND $add_tax ) { 
  642. if ( is_string( $tax_rate ) AND strpos( $tax_rate, "%" ) !== false ) { 
  643. $tax_rate_decimal = str_replace( "%", "", $tax_rate ); 
  644. $tax_rate_decimal = $tax_rate_decimal / 100; 
  645. $total['sub_total'] = $total_value; 
  646. $total_value = number_format( round( $user_value + ( $user_value * $tax_rate_decimal ), $places ), $places ); 
  647.  
  648.  
  649. $total['total'] = $total_value; 
  650. $total['fields'] = $fields; 
  651.  
  652. if ( $sub_total ) { 
  653. $total['sub_total'] = $sub_total; 
  654.  
  655. if ( $tax_rate ) { 
  656. $total['tax_rate'] = $tax_rate; 
  657.  
  658. // Get our tax total. 
  659. $tax_total = $this->get_calc_tax_total(); 
  660. if ( $tax_total ) { 
  661. $total['tax_total'] = $tax_total; 
  662.  
  663. } else { 
  664. $total = $total_value; 
  665.  
  666. return $total; 
  667. }  
  668.  
  669. /** 
  670. * Function that returns the "sub total" field value if it exists. 
  671. * @since 2.2.30 
  672. * @returns array $sub_total 
  673. */ 
  674. function get_calc_sub_total( $array = true ) { 
  675. if ( !isset ( $this->data['field_data'] ) ) { 
  676. return false; 
  677. $sub_total_value = ''; 
  678. $sub_total_field = '';  
  679. foreach ( $this->data['field_data'] as $field ) { 
  680. $data = $field['data']; 
  681. $field_id = $field['id']; 
  682. $user_value = $this->get_field_value( $field_id ); 
  683.  
  684. if ( isset ( $data['payment_sub_total'] ) AND $data['payment_sub_total'] == 1 ) { 
  685. $calc_method = $data['calc_method']; 
  686. if ( isset ( $data['calc'] ) ) { 
  687. $calc_fields = $data['calc']; 
  688. $calc_eq = $data['calc_eq']; 
  689. $places = $data['calc_places']; 
  690. $sub_total_field = $field_id; 
  691. $sub_total_value = number_format( round( $user_value, $places ), $places ); 
  692. break; 
  693. if ( $sub_total_field == '' ) { 
  694. return false; 
  695. if ( $array ) { 
  696. // Get the list of fields that affected this value. 
  697. $fields = $this->get_calc_fields( $sub_total_field ); 
  698.  
  699. $tmp_array = array(); 
  700. // Loop through the fields in that list and remove any that don't have a calc_option value of 1 
  701. foreach ( $fields as $field_id => $value ) { 
  702. $field_settings = $this->get_field_settings( $field_id ); 
  703. $field_value = $this->get_field_value( $field_id ); 
  704. $data = $field_settings['data']; 
  705. $tmp_array[$field_id] = $value; 
  706. $fields = $tmp_array; 
  707.  
  708.  
  709. $sub_total = array( 'sub_total' => $sub_total_value, 'fields' => $fields ); 
  710. } else { 
  711. $sub_total = $sub_total_value; 
  712.  
  713. return $sub_total; 
  714.  
  715. /** 
  716. * Function that returns the total amount of tax if the tax_rate field exists in the form. 
  717. * @since 2.2.30 
  718. * @returns int $tax_total 
  719. */ 
  720. function get_calc_tax_total() { 
  721. if ( !isset ( $this->data['field_data'] ) ) { 
  722. return false; 
  723. $tax_rate = $this->get_calc_tax_rate(); 
  724. if ( !$tax_rate ) { 
  725. return false; 
  726. // Get our sub-total if it exists. 
  727. $sub_total = $this->get_calc_sub_total( false ); 
  728. $locale_info = localeconv(); 
  729. $decimal_point = $locale_info['decimal_point']; 
  730. if ( $decimal_point == '.' ) { 
  731. $sub_total = str_replace(', ', '', $sub_total ); 
  732. } else { 
  733. $sub_total = str_replace('.', '', $sub_total ); 
  734.  
  735. $sub_total = intval( $sub_total ); 
  736.  
  737. // Get our total if it exists. 
  738. $total = $this->get_calc_total( false, false ); 
  739.  
  740. if ( strpos( $tax_rate, "%" ) !== false ) { 
  741. $tax_rate = str_replace( "%", "", $tax_rate ); 
  742. $tax_rate = $tax_rate / 100; 
  743.  
  744. if ( $sub_total ) { 
  745. $tax_total = $sub_total * $tax_rate; 
  746. } else if ( $total ) { 
  747. $tax_total = $total * $tax_rate; 
  748. } else { 
  749. return false; 
  750.  
  751.  
  752. $tax_total = number_format( round( $tax_total, 2 ), 2 ); 
  753. return $tax_total;  
  754.  
  755. /** 
  756. * Function that returns tax_rate if the field exists in the form. 
  757. * @since 2.2.30 
  758. * @returns string $tax_rate; 
  759. */ 
  760. function get_calc_tax_rate() { 
  761. if ( !isset ( $this->data['field_data'] ) ) { 
  762. return false; 
  763. $tax_rate = ''; 
  764. foreach ( $this->data['field_data'] as $field ) { 
  765. if ( $field['type'] == '_tax' ) { 
  766. $tax_rate = $this->get_field_value( $field['id'] ); 
  767. break; 
  768. if ( $tax_rate == '' ) { 
  769. return false; 
  770. } else { 
  771. return $tax_rate; 
  772.  
  773. /** 
  774. * Function that returns an array of field IDs and calc_values that contributed to the given calc id. 
  775. * @since 2.2.30 
  776. * @returns array $calc_array 
  777. */ 
  778. function get_calc_fields( $calc_id = '' ) { 
  779.  
  780. if ( $calc_id == '' OR !isset ( $this->data['field_data'] ) ) { 
  781. return false; 
  782. // Get our calculation settings. 
  783. $field_settings = $this->get_field_settings( $calc_id ); 
  784. $calc_method = $field_settings['data']['calc_method']; 
  785.  
  786. if ( isset ( $field_settings['data']['calc_eq'] ) ) { 
  787. $calc_eq = $field_settings['data']['calc_eq']; 
  788. } else { 
  789. $calc_eq = ''; 
  790.  
  791. if ( isset ( $field_settings['data']['calc'] ) ) { 
  792. $calc_fields = $field_settings['data']['calc']; 
  793. } else { 
  794. $calc_fields = ''; 
  795.  
  796. $tmp_array = array(); 
  797. // Loop through the fields 
  798. foreach ( $this->data['fields'] as $field_id => $user_value ) { 
  799. $field = $this->data['field_data'][$field_id]; 
  800. $field_value = $this->get_field_value( $field['id'] ); 
  801. // We don't want our field to be added if it's a tax field. 
  802. if ( $field['type'] != '_tax' ) { 
  803. switch ( $calc_method ) { 
  804. case 'auto': 
  805. // If this field's calc_auto_include is set to 1, then add this field's ID to the list. 
  806. if ( isset ( $field['data']['calc_auto_include'] ) AND $field['data']['calc_auto_include'] == 1 ) { 
  807. if ( $field['type'] != '_calc' ) { 
  808. $calc_value = ninja_forms_field_calc_value( $field['id'], $field_value, $calc_method ); 
  809. if ( $calc_value ) { 
  810. $tmp_array[] = array( $field['id'] => $calc_value ); 
  811. } else { 
  812. if ( $this->get_field_value( $field['id'] ) ) { 
  813. $tmp_array[] = array( $field['id'] => $this->get_field_value( $field['id'] ) ); 
  814. // If this is a calc field, then call this same function so that we can get all the fields that contributed to that. 
  815. $tmp_array[] = $this->get_calc_fields( $field['id'] ); 
  816. break; 
  817. case 'fields': 
  818. // If this field is in our list of field operations, add this field's ID to our list. 
  819. if ( $calc_fields != '' ) { 
  820. foreach ( $calc_fields as $calc ) { 
  821. if ( $field['id'] == $calc['field'] ) { 
  822. if ( $field['type'] != '_calc' ) { 
  823. //echo "FIELD ID: ".$field['id']; 
  824. $calc_value = ninja_forms_field_calc_value( $field['id'], $field_value, $calc_method ); 
  825. if ( $calc_value ) { 
  826. $tmp_array[] = array( $field['id'] => $calc_value ); 
  827. } else { 
  828. if ( $this->get_field_value( $field['id'] ) ) { 
  829. $tmp_array[] = array( $field['id'] => $this->get_field_value( $field['id'] ) ); 
  830. // If this is a calc field, then call this same function so that we can get all the fields that contributed to that. 
  831. $tmp_array[] = $this->get_calc_fields( $field['id'] ); 
  832. break; 
  833. case 'eq': 
  834. // If this field exists in our equation, then add this field's ID to our list. 
  835. if ( $calc_eq != '' ) { 
  836. if ( preg_match("/\bfield_".$field['id']."\b/i", $calc_eq ) ) { 
  837. if ( $field['type'] != '_calc' ) { 
  838. $calc_value = ninja_forms_field_calc_value( $field['id'], $field_value, $calc_method ); 
  839. if ( $calc_value ) { 
  840. $tmp_array[] = array( $field['id'] => $calc_value ); 
  841. } else { 
  842. if ( $this->get_field_value( $field['id'] ) ) { 
  843. $tmp_array[] = array( $field['id'] => $this->get_field_value( $field['id'] ) ); 
  844. // If this is a calc field, then call this same function so that we can get all the fields that contributed to that. 
  845. $tmp_array[] = $this->get_calc_fields( $field['id'] ); 
  846. break; 
  847.  
  848.  
  849. // Loop through our array and make sure that it's not multi-dimensional. 
  850. $calc_array = array(); 
  851. foreach ( $tmp_array as $key => $field ) { 
  852. foreach ( $field as $field_id => $value ) { 
  853. if ( count ( $value ) <= 1 ) { 
  854. $calc_array[$field_id] = $value; 
  855. } else { 
  856. foreach ( $value as $f_id => $v ) { 
  857. $calc_array[$f_id] = $v; 
  858.  
  859. return $calc_array; 
  860.  
  861. /** 
  862. * Function that returns an array of field IDs and calc_values that contributed to the given calc id. 
  863. * @since 2.2.37 
  864. * @returns array $credit_card 
  865. */ 
  866. function get_credit_card() { 
  867. $credit_card = array(); 
  868. if ( empty( $this->data ) OR !isset ( $this->data['extra']['_credit_card_number'] ) ) { 
  869. return false; 
  870. }else{ 
  871. $number = str_replace( ' ', '', $this->data['extra']['_credit_card_number'] ); 
  872. $credit_card['number'] = $number; 
  873.  
  874. if(isset( $this->data['extra']['_credit_card_cvc'] )) { 
  875.  
  876. $credit_card['cvc'] = $this->data['extra']['_credit_card_cvc']; 
  877.  
  878.  
  879. if(isset( $this->data['extra']['_credit_card_name'] )) { 
  880.  
  881. $credit_card['name'] = $this->data['extra']['_credit_card_name']; 
  882.  
  883.  
  884. //$credit_card['expires'] = $this->data['extra']['_credit_card_expires']; 
  885.  
  886. if(isset( $this->data['extra']['_credit_card_expires_month'] )) { 
  887.  
  888. $credit_card['expires'] = $this->data['extra']['_credit_card_expires_month']  
  889. . '/' . $this->data['extra']['_credit_card_expires_year']; 
  890.  
  891. $credit_card['expires_month'] = $this->data['extra']['_credit_card_expires_month']; 
  892. $credit_card['expires_year'] = $this->data['extra']['_credit_card_expires_year']; 
  893.  
  894. return $credit_card; 
  895.  
  896. /** 
  897. * Function that gets the current URL of the page, including querystring. 
  898. * @since 2.2.47 
  899. * @return $url string 
  900. */ 
  901. function get_current_url() { 
  902. $protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://"; 
  903. $url = $protocol . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]; 
  904. return $url; 
  905.