Ninja_Forms_Loading

Ninja Forms Loading class.

Defined (1)

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

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