/deprecated/includes/fields/calc.php

  1. <?php if ( ! defined( 'ABSPATH' ) ) exit; 
  2. /** 
  3. * Function to register a new field for calculations 
  4. * 
  5. * @since 2.2.28 
  6. * @return void 
  7. */ 
  8. function ninja_forms_register_field_calc() { 
  9. $args = array( 
  10. 'name' => __( 'Calculation', 'ninja-forms' ),  
  11. 'sidebar' => 'template_fields',  
  12. 'edit_function' => 'ninja_forms_field_calc_edit',  
  13. 'display_function' => 'ninja_forms_field_calc_display',  
  14. 'group' => 'standard_fields',  
  15. 'edit_conditional' => true,  
  16. 'edit_req' => false,  
  17. 'edit_label' => false,  
  18. 'edit_label_pos' => false,  
  19. 'edit_custom_class' => false,  
  20. 'edit_help' => false,  
  21. //'process_field' => false,  
  22. //'pre_process' => 'ninja_forms_field_calc_strip_currency_symbol',  
  23. 'edit_options' => array( 
  24. array( 
  25. 'type' => 'hidden',  
  26. 'name' => 'payment_field_group',  
  27. 'default' => 1,  
  28. ),  
  29. array( 
  30. 'type' => 'hidden',  
  31. 'name' => 'payment_total',  
  32. //'label' => __( 'Total', 'ninja-forms' ),  
  33. ),  
  34. array( 
  35. 'type' => 'hidden',  
  36. 'name' => 'payment_sub_total',  
  37. //'label' => __( 'Sub Total', 'ninja-forms' ),  
  38. ),  
  39. array( 
  40. 'type' => 'text',  
  41. 'name' => 'calc_places',  
  42. 'label' => __( 'Number of decimal places.', 'ninja-forms' ),  
  43. 'default' => 2,  
  44. ),  
  45. ),  
  46. 'conditional' => array( 
  47. 'value' => array( 
  48. 'type' => 'text',  
  49. ),  
  50. ),  
  51. ); 
  52.  
  53. ninja_forms_register_field( '_calc', $args ); 
  54. add_action( 'init', 'ninja_forms_register_field_calc' ); 
  55.  
  56.  
  57. /** 
  58. * Function that filters the field LI label on the edit field back-end. 
  59. * 
  60. * @since 2.2.28 
  61. * @return $li_label 
  62. */ 
  63. function ninja_forms_calc_edit_label_filter( $li_label, $field_id ) { 
  64. $field_row = ninja_forms_get_field_by_id( $field_id ); 
  65. if ( $field_row['type'] == '_calc' ) { 
  66. if ( isset ( $field_row['data']['calc_name'] ) ) { 
  67. $li_label = $field_row['data']['calc_name']; 
  68. } else { 
  69. $li_label = __( 'Calculation', 'ninja-forms' ); 
  70.  
  71. return $li_label; 
  72. add_filter( 'ninja_forms_edit_field_li_label', 'ninja_forms_calc_edit_label_filter', 10, 2 ); 
  73.  
  74.  
  75. /** 
  76. * Function that outputs the edit options for our calculation field 
  77. * 
  78. * @since 2.2.28 
  79. * @return void 
  80. */ 
  81. function ninja_forms_field_calc_edit( $field_id, $data ) { 
  82. $calc_name = isset ( $data['calc_name'] ) ? $data['calc_name'] : 'calc_name'; 
  83. $default_value = isset ( $data['default_value'] ) ? $data['default_value'] : '';  
  84. $calc_payment = isset ( $data['calc_payment'] ) ? $data['calc_payment'] : ''; 
  85. $calc_autho = isset ( $data['calc_auto'] ) ? $data['calc_auto'] : 0; 
  86.  
  87. // Output calculation display type 
  88. $options = array( 
  89. array( 'name' => __( '- None', 'ninja-forms' ), 'value' => 'hidden' ),  
  90. array( 'name' => __( 'Textbox', 'ninja-forms' ), 'value' => 'text'),  
  91. array( 'name' => __( 'HTML', 'ninja-forms' ), 'value' => 'html'),  
  92. ); 
  93.  
  94. $calc_display_type = isset ( $data['calc_display_type'] ) ? $data['calc_display_type'] : 'text'; 
  95.  
  96. ninja_forms_edit_field_el_output($field_id, 'select', __( 'Output calculation as', 'ninja-forms' ), 'calc_display_type', $calc_display_type, 'wide', $options, 'widefat ninja-forms-calc-display'); 
  97.  
  98. // If the calc_display_type is set to text, then we have several options to output. 
  99. // Set the output to hidden for these options if the calc_display_type is not set to text. 
  100. if ( $calc_display_type != 'text' ) { 
  101. $class = 'hidden'; 
  102. } else { 
  103. $class = ''; 
  104. echo '<div id="ninja_forms_field_'.$field_id.'_calc_text_display" class="'.$class.'">'; 
  105. // Output a label input textbox. 
  106. $label = isset ( $data['label'] ) ? stripslashes( $data['label'] ) : __( 'Calculation', 'ninja-forms' ); 
  107.  
  108. ninja_forms_edit_field_el_output($field_id, 'text', __( 'Label', 'ninja-forms' ), 'label', $label, 'wide', '', 'widefat'); 
  109.  
  110. // Output a label position select box. 
  111. if ( isset ( $data['label_pos'] ) ) { 
  112. $label_pos = $data['label_pos']; 
  113. } else { 
  114. $label_pos = ''; 
  115. $options = array( 
  116. array('name' => __( 'Left of Element', 'ninja-forms' ), 'value' => 'left'),  
  117. array('name' => __( 'Above Element', 'ninja-forms' ), 'value' => 'above'),  
  118. array('name' => __( 'Below Element', 'ninja-forms' ), 'value' => 'below'),  
  119. array('name' => __( 'Right of Element', 'ninja-forms' ), 'value' => 'right'),  
  120. ); 
  121. ninja_forms_edit_field_el_output($field_id, 'select', __( 'Label Position', 'ninja-forms' ), 'label_pos', $label_pos, 'wide', $options, 'widefat'); 
  122.  
  123. // Output a disabled option checkbox. 
  124. if( isset ( $data['calc_display_text_disabled'] ) ) { 
  125. $calc_display_text_disabled = $data['calc_display_text_disabled']; 
  126. } else { 
  127. $calc_display_text_disabled = 1; 
  128. ninja_forms_edit_field_el_output($field_id, 'checkbox', __( 'Disable input?', 'ninja-forms' ), 'calc_display_text_disabled', $calc_display_text_disabled, 'wide', '', ''); 
  129. echo '</div>'; 
  130.  
  131. // Set the output to hidden for the HTML RTE if the calc_display_type is not set to HTML. 
  132. if ( $calc_display_type != 'html' ) { 
  133. $class = 'hidden'; 
  134. } else { 
  135. $class = ''; 
  136. // Output our RTE. This is the only extra setting needed if the calc_display_type is set to HTML. 
  137. if ( isset ( $data['calc_display_html'] ) ) { 
  138. $calc_display_html = $data['calc_display_html']; 
  139. } else { 
  140. $calc_display_html = '[ninja_forms_calc]'; 
  141. echo '<div id="ninja_forms_field_'.$field_id.'_calc_html_display" class="'.$class.'">'; 
  142. ninja_forms_edit_field_el_output($field_id, 'rte', '', 'calc_display_html', $calc_display_html, '', '', '', __( 'Use the following shortcode to insert the final calculation: [ninja_forms_calc]', 'ninja-forms' ) ); 
  143. echo '</div>'; 
  144.  
  145. if ( isset ( $data['calc_method'] ) ) { 
  146. $calc_method = $data['calc_method']; 
  147. } else { 
  148. $calc_method = 'auto'; 
  149.  
  150. switch ( $calc_method ) { 
  151. case 'auto': 
  152. $eq_class = 'hidden'; 
  153. $field_class = 'hidden'; 
  154. break; 
  155. case 'fields': 
  156. $eq_class = 'hidden'; 
  157. $field_class = ''; 
  158. break; 
  159. case 'eq': 
  160. $eq_class = ''; 
  161. $field_class = 'hidden'; 
  162. break; 
  163.  
  164.  
  165. if ( isset ( $data['calc_eq'] ) ) { 
  166. $calc_eq = $data['calc_eq']; 
  167. } else { 
  168. $calc_eq = ''; 
  169.  
  170. if ( isset ( $data['calc'] ) AND $data['calc'] != '' ) { 
  171. $calc = $data['calc']; 
  172. } else { 
  173. $calc = array(); 
  174.  
  175. $desc = '<p>' . sprintf( __( 'You can enter calculation equations here using field_x where x is the ID of the field you want to use. For example, %sfield_53 + field_28 + field_65%s.', 'field_ should NOT be translated.', 'ninja-forms' ), '<strong>', '</strong>' ) . '</p>'; 
  176. $desc .= '<p>' . sprintf( __( 'Complex equations can be created by adding parentheses: %s( field_45 * field_2 ) / 2%s.', 'field_ should NOT be translated.', 'ninja-forms' ), '<strong>', '</strong>' ) . '</p>'; 
  177. $desc .= '<p>' .__( 'Please use these operators: + - * /. This is an advanced feature. Watch out for things like division by 0.', 'ninja-forms' ).'</p>'; 
  178. $options = array( 
  179. array( 'name' => __( 'Automatically Total Calculation Values', 'ninja-forms' ), 'value' => 'auto' ),  
  180. array( 'name' => __( 'Specify Operations And Fields (Advanced)', 'ninja-forms' ), 'value' => 'fields' ),  
  181. array( 'name' => __( 'Use An Equation (Advanced)', 'ninja-forms' ), 'value' => 'eq' ),  
  182. ); 
  183. ninja_forms_edit_field_el_output($field_id, 'select', __( 'Calculation Method', 'ninja-forms' ), 'calc_method', $calc_method, 'wide', $options, 'widefat ninja-forms-calc-method'); 
  184.  
  185. ?> 
  186.  
  187. <div class="ninja-forms-calculations <?php echo $field_class;?>"> 
  188. <div class="label"> 
  189. <?php _e( 'Field Operations', 'ninja-forms' );?> - <a href="#" name="" id="ninja_forms_field_<?php echo $field_id;?>_add_calc" class="ninja-forms-field-add-calc" rel="<?php echo $field_id;?>"><?php _e( 'Add Operation', 'ninja-forms' );?></a> 
  190.  
  191. <span class="spinner" style="float:left;"></span> 
  192. </div> 
  193.  
  194. <input type="hidden" name="ninja_forms_field_<?php echo $field_id;?>[calc]" value=""> 
  195. <div id="ninja_forms_field_<?php echo $field_id;?>_calc" class="" name=""> 
  196. <?php 
  197. $x = 0; 
  198. foreach ( $calc as $c ) { 
  199. ninja_forms_output_field_calc_row( $field_id, $c, $x ); 
  200. $x++; 
  201. ?> 
  202. </div> 
  203. </div> 
  204. <div class="ninja-forms-eq <?php echo $eq_class;?>"> 
  205. <?php 
  206. ninja_forms_edit_field_el_output($field_id, 'textarea', __( 'Advanced Equation', 'ninja-forms' ), 'calc_eq', $calc_eq, 'wide', '', 'widefat', $desc); 
  207. ?> 
  208. </div> 
  209. <hr> 
  210. <?php 
  211.  
  212. function nf_field_calc_advanced_settings( $field_id, $data ) { 
  213. $field = ninja_forms_get_field_by_id( $field_id ); 
  214. if ( '_calc' != $field['type'] ) 
  215. return false; 
  216.  
  217. $calc_name = isset ( $data['calc_name'] ) ? $data['calc_name'] : 'calc_name'; 
  218. $default_value = isset ( $data['default_value'] ) ? $data['default_value'] : '';  
  219. $calc_payment = isset ( $data['calc_payment'] ) ? $data['calc_payment'] : ''; 
  220. $calc_autho = isset ( $data['calc_auto'] ) ? $data['calc_auto'] : 0; 
  221. $calc_display_type = isset ( $data['calc_display_type'] ) ? $data['calc_display_type'] : 'text'; 
  222.  
  223. ninja_forms_edit_field_el_output($field_id, 'text', __( 'Calculation name', 'ninja-forms' ), 'calc_name', $calc_name, 'wide', '', 'widefat ninja-forms-calc-name', __( 'This is the programmatic name of your field. Examples are: my_calc, price_total, user-total.', 'ninja-forms' )); 
  224. ninja_forms_edit_field_el_output($field_id, 'text', __( 'Default Value', 'ninja-forms' ), 'default_value', $default_value, 'wide', '', 'widefat' ); 
  225.  
  226.  
  227. // If any option besides "none" is selected, then show our custom class and help options. 
  228. if ( $calc_display_type == 'hidden' ) { 
  229. $class = 'hidden'; 
  230. } else { 
  231. $class = ''; 
  232.  
  233. if ( isset ( $data['class'] ) ) { 
  234. $custom_class = $data['class']; 
  235. } else { 
  236. $custom_class = ''; 
  237.  
  238. if ( isset ( $data['show_help'] ) ) { 
  239. $show_help = $data['show_help']; 
  240. } else { 
  241. $show_help = 0; 
  242.  
  243. if ( isset ( $data['help_text'] ) ) { 
  244. $help_text = $data['help_text']; 
  245. } else { 
  246. $help_text = ''; 
  247.  
  248. if( $show_help == 1 ) { 
  249. $display_span = ''; 
  250. } else { 
  251. $display_span = 'display:none;'; 
  252.  
  253. echo '<div id="ninja_forms_field_'.$field_id.'_calc_extra_display" class="'.$class.'">'; 
  254. // Output our custom class textbox. 
  255. ninja_forms_edit_field_el_output($field_id, 'text', __( 'Custom CSS Class', 'ninja-forms' ), 'class', $custom_class, 'wide', '', 'widefat'); 
  256.  
  257. // Output our help text options. 
  258. $help_desc = sprintf(__('If "help text" is enabled, there will be a question mark %s placed next to the input field. Hovering over this question mark will show the help text.', 'ninja-forms'), '<img src="'.NINJA_FORMS_URL.'images/question-ico.gif">'); 
  259. ninja_forms_edit_field_el_output($field_id, 'checkbox', __( 'Show Help Text', 'ninja-forms' ), 'show_help', $show_help, 'wide', '', 'ninja-forms-show-help'); 
  260. ?> 
  261. <span id="ninja_forms_field_<?php echo $field_id;?>_help_span" style="<?php echo $display_span;?>"> 
  262. <?php 
  263. ninja_forms_edit_field_el_output($field_id, 'textarea', __( 'Help Text', 'ninja-forms' ), 'help_text', $help_text, 'wide', '', 'widefat', $help_desc); 
  264. ?> 
  265. </span> 
  266. <?php 
  267. echo '</div>'; 
  268.  
  269. add_action( 'nf_edit_field_advanced', 'nf_field_calc_advanced_settings', 7, 2 ); 
  270.  
  271. /** 
  272. * Function that outputs the display for our calculation field 
  273. * 
  274. * @since 2.2.28 
  275. * @return void 
  276. */ 
  277. function ninja_forms_field_calc_display( $field_id, $data, $form_id = '' ) { 
  278.  
  279. if ( isset( $data['default_value'] ) ) { 
  280. $default_value = $data['default_value']; 
  281. } else { 
  282. $default_value = 0; 
  283.  
  284. if ( $default_value == '' ) { 
  285. $default_value = 0; 
  286.  
  287. if ( isset ( $data['calc_display_text_disabled'] ) AND $data['calc_display_text_disabled'] == 1 ) { 
  288. $disabled = "disabled"; 
  289. } else { 
  290. $disabled = ''; 
  291.  
  292. if ( isset ( $data['calc_display_type'] ) ) { 
  293. $calc_display_type = $data['calc_display_type']; 
  294. } else { 
  295. $calc_display_type = 'text'; 
  296.  
  297. if ( isset ( $data['calc_display_html'] ) ) { 
  298. $calc_display_html = $data['calc_display_html']; 
  299. } else { 
  300. $calc_display_html = ''; 
  301.  
  302. if ( isset ( $data['calc_method'] ) ) { 
  303. $calc_method = $data['calc_method']; 
  304. } else { 
  305. $calc_method = ''; 
  306.  
  307. $field_class = ninja_forms_get_field_class( $field_id, $form_id ); 
  308.  
  309. ?> 
  310. <input type="hidden" name="ninja_forms_field_<?php echo $field_id;?>" value="<?php echo $default_value;?>" class="<?php echo $field_class;?>"> 
  311. <?php 
  312.  
  313. switch ( $calc_display_type ) { 
  314. case 'text': 
  315. ?> 
  316. <input type="text" id="ninja_forms_field_<?php echo $field_id;?>" name="ninja_forms_field_<?php echo $field_id;?>" value="<?php echo $default_value;?>" <?php echo $disabled;?> class="<?php echo $field_class;?>" rel="<?php echo $field_id;?>"> 
  317. <?php 
  318. break; 
  319. case 'html': 
  320. $calc_display_html = str_replace( '[ninja_forms_calc]', '<span id="ninja_forms_field_'.$field_id.'" class="'.$field_class.'" rel="'.$field_id.'">'.$default_value.'</span>', $calc_display_html ); 
  321. echo $calc_display_html; 
  322. break; 
  323.  
  324. /** 
  325. * Function to output specific calculation options for a given field 
  326. * 
  327. * @param int $field_id - ID of the field being edited. 
  328. * @param array $c - Array containing the data. 
  329. * @param int $x - Index for this row of the calc array. 
  330. * @since 2.2.28 
  331. * @returns void 
  332. */ 
  333. function ninja_forms_output_field_calc_row( $field_id, $c = array(), $x = 0 ) { 
  334. global $ninja_forms_fields; 
  335. $field_row = ninja_forms_get_field_by_id( $field_id ); 
  336. $field_type = $field_row['type']; 
  337. $form_id = $field_row['form_id']; 
  338.  
  339. if ( isset ( $c['field'] ) ) { 
  340. $calc_field = $c['field']; 
  341. } else { 
  342. $calc_field = ''; 
  343.  
  344. if ( isset ( $c['op'] ) ) { 
  345. $op = $c['op']; 
  346. } else { 
  347. $op = ''; 
  348.  
  349. ?> 
  350. <div id="ninja_forms_field_<?php echo $field_id;?>_calc_row_<?php echo $x;?>" class="ninja-forms-calc-row" rel="<?php echo $x;?>"> 
  351. <a href="#" id="ninja_forms_field_<?php echo $field_id;?>_remove_calc" name="<?php echo $x;?>" rel="<?php echo $field_id;?>" class="ninja-forms-field-remove-calc">X</a> 
  352.  
  353. <select name="ninja_forms_field_<?php echo $field_id;?>[calc][<?php echo $x;?>][op]"> 
  354. <option value="add" <?php selected( $op, 'add' );?>>+</option> 
  355. <option value="subtract" <?php selected( $op, 'subtract' );?>>-</option> 
  356. <option value="multiply" <?php selected( $op, 'multiply' );?>>*</option> 
  357. <option value="divide" <?php selected( $op, 'divide' );?>>/</option> 
  358. </select> 
  359.  
  360. <select name="ninja_forms_field_<?php echo $field_id;?>[calc][<?php echo $x;?>][field]" class="ninja-forms-calc-select"> 
  361. <option value=""><?php _e( '- Select a Field', 'ninja-forms' );?></option> 
  362. <?php 
  363. // Loop through our fields and output all of our calculation fields. 
  364. $fields = ninja_forms_get_fields_by_form_id( $form_id ); 
  365. foreach ( $fields as $field ) { 
  366. if ( isset ( $field['data']['label'] ) ) { 
  367. $label = $field['data']['label']; 
  368. } else { 
  369. $label = ''; 
  370. if ( strlen ( $label ) > 15 ) { 
  371. $label = substr ( $label, 0, 15 ); 
  372. $label .= '...'; 
  373. $process_field = $ninja_forms_fields[$field['type']]['process_field']; 
  374. if ( $field['id'] != $field_id AND $process_field ) { 
  375. ?> 
  376. <option value="<?php echo $field['id'];?>" <?php selected( $calc_field, $field['id'] );?>><?php echo $field['id'];?> - <?php echo $label;?></option> 
  377. <?php 
  378. ?> 
  379. </select> 
  380. </div> 
  381. <?php 
  382.  
  383.  
  384. /** 
  385. * Function that runs during pre_processing and calcuates the value of this field. 
  386. * 
  387. * @since 2.2.30 
  388. * @return void 
  389. */ 
  390. function ninja_forms_field_calc_pre_process() { 
  391. global $ninja_forms_loading, $ninja_forms_processing, $wp_locale; 
  392.  
  393. if ( isset ( $ninja_forms_loading ) ) { 
  394. $form_id = $ninja_forms_loading->get_form_ID(); 
  395. $all_fields = $ninja_forms_loading->get_all_fields(); 
  396. } else { 
  397. $form_id = $ninja_forms_processing->get_form_ID(); 
  398. $all_fields = $ninja_forms_processing->get_all_fields(); 
  399.  
  400.  
  401. if ( is_array ( $all_fields ) ) { 
  402. foreach ( $all_fields as $field_id => $user_value ) { 
  403.  
  404. if ( isset ( $ninja_forms_loading ) ) { 
  405. $field_row = $ninja_forms_loading->get_field_settings( $field_id ); 
  406. } else { 
  407. $field_row = $ninja_forms_processing->get_field_settings( $field_id ); 
  408.  
  409. if ( isset ( $field_row['type'] ) ) { 
  410. $field_type = $field_row['type']; 
  411. } else { 
  412. $field_type = ''; 
  413.  
  414.  
  415. if ( $field_type == '_calc' ) { 
  416. $field_data = $field_row['data']; 
  417. if ( isset ( $field_data['default_value'] ) ) { 
  418. $default_value = $field_data['default_value']; 
  419. } else { 
  420. $default_value = 0; 
  421.  
  422. $result = $default_value; 
  423.  
  424. // Figure out which method we are using to calculate this field. 
  425. if ( isset ( $field_data['calc_method'] ) ) { 
  426. $calc_method = $field_data['calc_method']; 
  427. } else { 
  428. $calc_method = 'auto'; 
  429.  
  430. // Get our advanced field op settings if they exist. 
  431. if ( isset ( $field_data['calc'] ) ) { 
  432. $calc_fields = $field_data['calc']; 
  433. } else { 
  434. $calc_fields = array(); 
  435.  
  436. // Get our calculation equation if it exists. 
  437. if ( isset ( $field_data['calc_eq'] ) ) { 
  438. $calc_eq = $field_data['calc_eq']; 
  439. } else { 
  440. $calc_eq = array(); 
  441.  
  442. // Get our calculation equation if it exists. 
  443. if ( isset ( $field_data['calc_places'] ) ) { 
  444. $calc_places = $field_data['calc_places']; 
  445. } else { 
  446. $calc_places = 0; 
  447.  
  448. // Figure out if there is a sub_total and a tax field. If there are, and this is a total field set to calc_method auto, we're using an equation, not auto. 
  449. $tax = false; 
  450. $sub_total = false; 
  451. if ( is_array ( $all_fields ) ) { 
  452. foreach ( $all_fields as $f_id => $user_value ) { 
  453. if ( isset ( $ninja_forms_loading ) ) { 
  454. $field = $ninja_forms_loading->get_field_settings( $f_id ); 
  455. } else { 
  456. $field = $ninja_forms_processing->get_field_settings( $f_id ); 
  457.  
  458. if ( isset ( $field['type'] ) ) { 
  459. $f_type = $field['type']; 
  460. } else { 
  461. $f_type = ''; 
  462.  
  463. $data = apply_filters( 'ninja_forms_field', $field['data'], $f_id ); 
  464. if ( $f_type == '_tax' ) { 
  465. // There is a tax field; save its field_id. 
  466. $tax = $field['id']; 
  467. } else if ( isset ( $data['payment_sub_total'] ) AND $data['payment_sub_total'] == 1 ) { 
  468. // There is a sub_total field; save its field_id. 
  469. $sub_total = $field['id']; 
  470.  
  471.  
  472. // If the tax and sub_total have been found, and this is a total field set to auto, change the calc_method and calc_eq. 
  473. if ( $tax AND $sub_total AND isset ( $field_data['payment_total'] ) AND $field_data['payment_total'] == 1 AND $calc_method == 'auto' ) { 
  474. $calc_method = 'eq'; 
  475. if ( isset ( $ninja_forms_loading ) ) { 
  476. $tax_rate = $ninja_forms_loading->get_field_value( $tax ); 
  477. } else { 
  478. $tax_rate = $ninja_forms_processing->get_field_value( $tax ); 
  479.  
  480. if ( strpos( $tax_rate, "%" ) !== false ) { 
  481. $tax_rate = str_replace( "%", "", $tax_rate ); 
  482. $tax_rate = $tax_rate / 100; 
  483. $calc_eq = 'field_'.$sub_total.' + ( field_'.$sub_total.' * '.$tax_rate.' )'; 
  484.  
  485. if ( isset ( $ninja_forms_loading ) ) { 
  486. $field_settings = $ninja_forms_loading->get_field_settings( $field_id ); 
  487. } else { 
  488. $field_settings = $ninja_forms_processing->get_field_settings( $field_id ); 
  489.  
  490. $field_settings['data']['calc_method'] = $calc_method; 
  491. $field_settings['data']['calc_eq'] = $calc_eq; 
  492. if ( isset ( $ninja_forms_loading ) ) { 
  493. $ninja_forms_loading->update_field_settings( $field_id, $field_settings ); 
  494. } else { 
  495. $ninja_forms_processing->update_field_settings( $field_id, $field_settings ); 
  496.  
  497. // Loop through our fields and see which ones should be used for calculations. 
  498. if ( is_array ( $all_fields ) ) { 
  499. foreach ( $all_fields as $f_id => $user_value ) { 
  500. if ( isset ( $ninja_forms_loading ) ) { 
  501. $field = $ninja_forms_loading->get_field_settings( $f_id ); 
  502. $field_value = $ninja_forms_loading->get_field_value( $f_id ); 
  503. } else { 
  504. $field = $ninja_forms_processing->get_field_settings( $f_id ); 
  505. $field_value = $ninja_forms_processing->get_field_value( $f_id ); 
  506.  
  507. $field_data = $field['data']; 
  508. if ( $f_id == $tax ) { 
  509. $tax = ninja_forms_field_calc_value( $field['id'], $field_value, 'auto' );; 
  510.  
  511. switch ( $calc_method ) { 
  512. case 'auto': // We are automatically totalling the fields that have a calc_auto_include set to 1. 
  513. if ( isset ( $field_data['calc_auto_include'] ) AND $field_data['calc_auto_include'] == 1 && $field_value ) { 
  514.  
  515. if ( $field['type'] == '_calc' ) { 
  516. $calc_value = ninja_forms_calc_field_loop( $field['id'], '', $result ); 
  517. } else { 
  518. $calc_value = ninja_forms_field_calc_value( $field['id'], $field_value, $calc_method ); 
  519.  
  520. if ( $calc_value !== false ) { 
  521. $result = ninja_forms_calc_evaluate( 'add', $result, $calc_value ); 
  522.  
  523. break; 
  524. case 'fields': // We are performing a specific set of operations on a set of fields. 
  525. if ( is_array ( $calc_fields ) ) { 
  526. foreach ( $calc_fields as $c ) { 
  527. if ( $c['field'] == $field['id'] ) { 
  528. if ( $field['type'] == '_calc' ) { 
  529. $calc_value = ninja_forms_calc_field_loop( $field['id'], '', $result ); 
  530. $calc_value = ninja_forms_field_calc_value( $field['id'], $field_value, $calc_method ); 
  531. if ( $calc_value !== false ) { 
  532. $result = ninja_forms_calc_evaluate( $c['op'], $result, $calc_value ); 
  533. break; 
  534. case 'eq': 
  535. if (preg_match("/\bfield_".$f_id."\b/i", $calc_eq ) ) { 
  536. if ( $field['type'] == '_calc' ) { 
  537. $calc_value = ninja_forms_calc_field_loop( $field['id'], $calc_eq ); 
  538. } else { 
  539. $calc_value = ninja_forms_field_calc_value( $field['id'], $field_value, $calc_method ); 
  540.  
  541. if ( $calc_value !== false ) { 
  542. $calc_eq = preg_replace('/\bfield_'.$field['id'].'\b/', $calc_value, $calc_eq ); 
  543. break; 
  544.  
  545. if ( $calc_method == 'eq' ) { 
  546. $eq = new eqEOS(); 
  547.  
  548. // Swap out decimal separator 
  549. $decimal_point = $wp_locale->number_format['decimal_point']; 
  550. $calc_eq = str_replace( $decimal_point, '.', $calc_eq ); 
  551.  
  552. $result = $eq->solveIF($calc_eq); 
  553.  
  554. // Swap back decimal separator 
  555. $result = str_replace( '.', $decimal_point, $result ); 
  556.  
  557. if ( isset ( $calc_places ) ) { 
  558. if ( empty( $calc_places ) ) { 
  559. $calc_places = 0; 
  560.  
  561. // Swap out decimal separator 
  562. $decimal_point = $wp_locale->number_format['decimal_point']; 
  563. $result = str_replace( $decimal_point, '.', $result ); 
  564.  
  565. // Round and Format 
  566. $result = number_format( round( $result, $calc_places ), $calc_places ); 
  567.  
  568. // Swap back decimal separator 
  569. $result = str_replace( '.', $decimal_point, $result ); 
  570. $result = str_replace( $wp_locale->number_format['thousands_sep'], '', $result ); 
  571.  
  572. if ( isset ( $ninja_forms_loading ) ) { 
  573. $ninja_forms_loading->update_field_value( $field_id, $result ); 
  574. } else { 
  575. $ninja_forms_processing->update_field_value( $field_id, $result ); 
  576.  
  577.  
  578. add_action( 'ninja_forms_pre_process', 'ninja_forms_field_calc_pre_process', 999 ); 
  579.  
  580. function ninja_forms_calc_check_load() { 
  581. global $ninja_forms_processing; 
  582.  
  583. if ( ! is_object ( $ninja_forms_processing ) ) { 
  584. ninja_forms_field_calc_pre_process(); 
  585.  
  586. add_action( 'ninja_forms_display_pre_init', 'ninja_forms_calc_check_load', 999 ); 
  587.  
  588.  
  589. function ninja_forms_calc_field_loop( $field_id, $calc_eq = '', $result = '' ) { 
  590. global $ninja_forms_loading, $ninja_forms_processing; 
  591.  
  592. if ( isset ( $ninja_forms_loading ) ) { 
  593. $field_settings = $ninja_forms_loading->get_field_settings( $field_id ); 
  594. } else { 
  595. $field_settings = $ninja_forms_processing->get_field_settings( $field_id ); 
  596.  
  597. $calc_data = $field_settings['data']; 
  598.  
  599. // Figure out which method we are using to calculate this field. 
  600. if ( isset ( $calc_data['calc_method'] ) ) { 
  601. $calc_method = $calc_data['calc_method']; 
  602. } else { 
  603. $calc_method = 'auto'; 
  604.  
  605. // Get our advanced field op settings if they exist. 
  606. if ( isset ( $calc_data['calc'] ) ) { 
  607. $calc_fields = $calc_data['calc']; 
  608. } else { 
  609. $calc_fields = array(); 
  610.  
  611. // Get our calculation equation if it exists. 
  612. if ( isset ( $calc_data['calc_eq'] ) ) { 
  613. $calc_eq = $calc_data['calc_eq']; 
  614. } else { 
  615. $calc_eq = array(); 
  616.  
  617. if ( isset ( $ninja_forms_loading ) ) { 
  618. $form_id = $ninja_forms_loading->get_form_ID(); 
  619. $all_fields = $ninja_forms_loading->get_all_fields(); 
  620. } else { 
  621. $form_id = $ninja_forms_processing->get_form_ID(); 
  622. $all_fields = $ninja_forms_processing->get_all_fields(); 
  623.  
  624.  
  625. // Figure out if there is a sub_total and a tax field. If there are, and this is a total field set to calc_method auto, we're using an equation, not auto. 
  626. $tax = false; 
  627. $sub_total = false; 
  628. foreach ( $all_fields as $f_id => $user_value ) { 
  629.  
  630. if ( isset ( $ninja_forms_loading ) ) { 
  631. $field = $ninja_forms_loading->get_field_settings( $f_id ); 
  632. } else { 
  633. $field = $ninja_forms_processing->get_field_settings( $f_id ); 
  634.  
  635. $field_value = $user_value; 
  636. $data = $field['data']; 
  637. if ( $field['type'] == '_tax' ) { 
  638. // There is a tax field; save its field_id. 
  639. $tax = $field['id']; 
  640. } else if ( isset ( $data['payment_sub_total'] ) AND $data['payment_sub_total'] == 1 ) { 
  641. // There is a sub_total field; save its field_id. 
  642. $sub_total = $field['id']; 
  643.  
  644. // If the tax and sub_total have been found, and this is a total field set to auto, change the calc_method and calc_eq. 
  645. if ( $tax AND $sub_total AND isset ( $calc_data['payment_total'] ) AND $calc_data['payment_total'] == 1 AND $calc_method == 'auto' ) { 
  646. $calc_method = 'eq'; 
  647. if ( isset ( $ninja_forms_loading ) ) { 
  648. $tax_rate = $ninja_forms_loading->get_field_value( $tax ); 
  649. } else { 
  650. $tax_rate = $ninja_forms_processing->get_field_value( $tax ); 
  651.  
  652. if ( strpos( $tax_rate, "%" ) !== false ) { 
  653. $tax_rate = str_replace( "%", "", $tax_rate ); 
  654. $tax_rate = $tax_rate / 100; 
  655. $calc_eq = 'field_'.$sub_total.' + ( field_'.$sub_total.' * '.$tax_rate.' )'; 
  656.  
  657. // Figure out how many calculation fields we have and run 
  658. foreach ( $all_fields as $f_id => $user_value ) { 
  659. if ( isset ( $ninja_forms_loading ) ) { 
  660. $field = $ninja_forms_loading->get_field_settings( $f_id ); 
  661. $field_value = $ninja_forms_loading->get_field_value( $f_id ); 
  662. } else { 
  663. $field = $ninja_forms_processing->get_field_settings( $f_id ); 
  664. $field_value = $ninja_forms_processing->get_field_value( $f_id ); 
  665.  
  666. $field_data = $field['data']; 
  667. if ( $f_id != $field_id ) { 
  668. switch ( $calc_method ) { 
  669. case 'auto': // We are automatically totalling the fields that have a calc_auto_include set to 1. 
  670. if ( isset ( $field_data['calc_auto_include'] ) AND $field_data['calc_auto_include'] == 1 ) { 
  671.  
  672. if ( $field['type'] == '_calc' ) { 
  673. $calc_value = ninja_forms_calc_field_loop( $field['id'], '', $result ); 
  674. } else { 
  675. $calc_value = ninja_forms_field_calc_value( $field['id'], $field_value, $calc_method ); 
  676.  
  677. if ( $calc_value !== false ) { 
  678. $result = ninja_forms_calc_evaluate( 'add', $result, $calc_value ); 
  679.  
  680. break; 
  681. case 'fields': // We are performing a specific set of operations on a set of fields. 
  682. if ( is_array ( $calc_fields ) ) { 
  683. foreach ( $calc_fields as $c ) { 
  684. if ( $c['field'] == $field['id'] ) { 
  685. if ( $field['type'] == '_calc' ) { 
  686. $result = ninja_forms_calc_field_loop( $field['id'], '', $result ); 
  687. } else { 
  688. $calc_value = ninja_forms_field_calc_value( $field['id'], $field_value, $calc_method ); 
  689. if ( $calc_value !== false ) { 
  690. $result = ninja_forms_calc_evaluate( $c['op'], $result, $calc_value ); 
  691. break; 
  692. case 'eq': 
  693. if (preg_match("/\bfield_".$field['id']."\b/i", $calc_eq ) ) { 
  694. if ( $field['type'] == '_calc' ) { 
  695. $calc_value = ninja_forms_calc_field_loop( $field['id'], $calc_eq, $result ); 
  696. } else { 
  697. $calc_value = ninja_forms_field_calc_value( $field['id'], $field_value, $calc_method ); 
  698. if ( $calc_value !== false ) { 
  699. $calc_eq = preg_replace('/\bfield_'.$field['id'].'\b/', $calc_value, $calc_eq ); 
  700. break; 
  701. if ( $calc_method == 'eq' ) { 
  702. $eq = new eqEOS(); 
  703. $result = $eq->solveIF($calc_eq); 
  704.  
  705. if ( $result == '' ) { 
  706. $result = 0; 
  707.  
  708. return $result; 
  709.  
  710.  
  711. /** 
  712. * Function that filters the list options span and adds the appropriate listener class if there is a calc needed for the field. 
  713. * 
  714. * @since 2.2.28 
  715. * @return $class 
  716. */ 
  717. function ninja_forms_calc_filter_list_options_span( $class, $field_id ) { 
  718. global $ninja_forms_loading, $ninja_forms_processing; 
  719.  
  720. if ( isset ( $ninja_forms_loading ) ) { 
  721. $field_row = $ninja_forms_loading->get_field_settings( $field_id ); 
  722. } else { 
  723. $field_row = $ninja_forms_processing->get_field_settings( $field_id ); 
  724.  
  725. $add_class = false; 
  726. // Check to see if this field has cal_auto_include set to 1. If it does, we want to output a class name. 
  727. if ( isset ( $field_row['data']['calc_auto_include'] ) AND !empty ( $field_row['data']['calc_auto_include'] ) ) { 
  728. $add_class = true; 
  729.  
  730. if ( isset ( $ninja_forms_loading ) ) { 
  731. $all_fields = $ninja_forms_loading->get_all_fields(); 
  732. } else { 
  733. $all_fields = $ninja_forms_processing->get_all_fields(); 
  734.  
  735. foreach ( $all_fields as $f_id => $user_value ) { 
  736.  
  737. if ( isset ( $ninja_forms_loading ) ) { 
  738. $field = $ninja_forms_loading->get_field_settings( $f_id ); 
  739. } else { 
  740. $field = $ninja_forms_processing->get_field_settings( $f_id ); 
  741.  
  742. if ( isset ( $field['type'] ) && $field['type'] == '_calc' ) { 
  743. if ( isset ( $field['data']['calc_method'] ) ) { 
  744. $calc_method = $field['data']['calc_method']; 
  745. } else { 
  746. $calc_method = 'auto'; 
  747.  
  748. switch ( $calc_method ) { 
  749. case 'fields': 
  750. if ( isset ( $field['data']['calc'] ) ) { 
  751. foreach ( $field['data']['calc'] as $calc ) { 
  752. if ( $calc['field'] == $field_id ) { 
  753. $add_class = true; 
  754. break; 
  755. break; 
  756. case 'eq': 
  757. $eq = $field['data']['calc_eq']; 
  758. if (preg_match("/\bfield_".$field_id."\b/i", $eq ) ) { 
  759. $add_class = true; 
  760. break; 
  761. break; 
  762. if ( $add_class ) { 
  763. $class .= ' ninja-forms-field-list-options-span-calc-listen'; 
  764.  
  765. return $class; 
  766. add_filter( 'ninja_forms_display_list_options_span_class', 'ninja_forms_calc_filter_list_options_span', 10, 2 ); 
  767.  
  768.  
  769. /** 
  770. * Function that takes two variables and our calculation string operator and returns the result. 
  771. * 
  772. * @since 2.2.28 
  773. * @return int value 
  774. */ 
  775. function ninja_forms_calc_evaluate( $op, $value1, $value2 ) { 
  776. switch ( $op ) { 
  777. case 'add': 
  778. return $value1 + $value2; 
  779. break; 
  780. case 'subtract': 
  781. return $value1 - $value2; 
  782. break; 
  783. case 'multiply': 
  784. return $value1 * $value2; 
  785. break; 
  786. case 'divide': 
  787. return $value1 / $value2; 
  788. break; 
  789.  
  790.  
  791. /** 
  792. * Function that returns the calculation value of a field given by field_id if it is to be included in the auto total. 
  793. * 
  794. * @since 2.2.30 
  795. * @return calc_value 
  796. */ 
  797. function ninja_forms_field_calc_value( $field_id, $field_value = '', $calc_method = 'auto' ) { 
  798. global $ninja_forms_loading, $ninja_forms_processing, $wp_locale; 
  799.  
  800. if ( isset ( $ninja_forms_loading ) ) { 
  801. $field = $ninja_forms_loading->get_field_settings( $field_id ); 
  802. } else { 
  803. $field = $ninja_forms_processing->get_field_settings( $field_id ); 
  804.  
  805. $field_data = apply_filters( 'ninja_forms_field', $field['data'], $field_id ); 
  806.  
  807. if ( isset ( $field_data['default_value'] ) ) { 
  808. $default_value = $field_data['default_value']; 
  809. } else { 
  810. $default_value = ''; 
  811.  
  812. if ( $field_value == '' ) { 
  813. $field_value = $default_value; 
  814.  
  815. $calc_value = 0; 
  816. if ( $field['type'] == '_list' ) { 
  817. if ( isset ( $field_data['list']['options'] ) ) { 
  818. foreach ( $field_data['list']['options'] as $option ) { 
  819. if ( isset ( $field_data['list_show_value'] ) AND $field_data['list_show_value'] == 1 ) { 
  820. $option_value = $option['value']; 
  821. } else { 
  822. $option_value = $option['label']; 
  823. if ( $option_value == $field_value OR ( is_array ( $field_value ) AND in_array ( $option_value, $field_value ) ) ) { 
  824. $calc_value += $option['calc']; 
  825. } else if ( $field['type'] == '_checkbox' ) { 
  826. if ( $field_value == 'checked' ) { 
  827. $calc_value = $field_data['calc_value']['checked']; 
  828. } else { 
  829. if ( $calc_method == 'auto' ) { 
  830. return false; 
  831. } else { 
  832. $calc_value = $field_data['calc_value']['unchecked']; 
  833. } else { 
  834. if ( !$field_value OR $field_value == '' ) { 
  835. $field_value = 0; 
  836. $decimal_point = $wp_locale->number_format['decimal_point']; 
  837.  
  838. /** Casting to a Float removes decimal */ 
  839. // $calc_value = (float) preg_replace('/[^0-9' . $decimal_point . '-]*/', '', $field_value); 
  840. $calc_value = preg_replace('/[^0-9' . $decimal_point . '-]*/', '', $field_value); 
  841.  
  842.  
  843. if ( is_string( $calc_value ) AND strpos( $calc_value, "%" ) !== false ) { 
  844. $calc_value = str_replace( "%", "", $calc_value ); 
  845. $calc_value = $calc_value / 100; 
  846. if ( $calc_value == '' OR !$calc_value ) { 
  847. $calc_value = 0; 
  848.  
  849. return $calc_value; 
.