ninja_forms_field_calc_pre_process

Function that runs during pre_processing and calcuates the value of this field.

Description

ninja_forms_field_calc_pre_process(); 

Usage

  1. if ( !function_exists( 'ninja_forms_field_calc_pre_process' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'ninja-forms/deprecated/includes/fields/calc.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = ninja_forms_field_calc_pre_process(); 
  7.  

Defined (1)

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

/deprecated/includes/fields/calc.php  
  1. function ninja_forms_field_calc_pre_process() { 
  2. global $ninja_forms_loading, $ninja_forms_processing, $wp_locale; 
  3.  
  4. if ( isset ( $ninja_forms_loading ) ) { 
  5. $form_id = $ninja_forms_loading->get_form_ID(); 
  6. $all_fields = $ninja_forms_loading->get_all_fields(); 
  7. } else { 
  8. $form_id = $ninja_forms_processing->get_form_ID(); 
  9. $all_fields = $ninja_forms_processing->get_all_fields(); 
  10.  
  11.  
  12. if ( is_array ( $all_fields ) ) { 
  13. foreach ( $all_fields as $field_id => $user_value ) { 
  14.  
  15. if ( isset ( $ninja_forms_loading ) ) { 
  16. $field_row = $ninja_forms_loading->get_field_settings( $field_id ); 
  17. } else { 
  18. $field_row = $ninja_forms_processing->get_field_settings( $field_id ); 
  19.  
  20. if ( isset ( $field_row['type'] ) ) { 
  21. $field_type = $field_row['type']; 
  22. } else { 
  23. $field_type = ''; 
  24.  
  25.  
  26. if ( $field_type == '_calc' ) { 
  27. $field_data = $field_row['data']; 
  28. if ( isset ( $field_data['default_value'] ) ) { 
  29. $default_value = $field_data['default_value']; 
  30. } else { 
  31. $default_value = 0; 
  32.  
  33. $result = $default_value; 
  34.  
  35. // Figure out which method we are using to calculate this field. 
  36. if ( isset ( $field_data['calc_method'] ) ) { 
  37. $calc_method = $field_data['calc_method']; 
  38. } else { 
  39. $calc_method = 'auto'; 
  40.  
  41. // Get our advanced field op settings if they exist. 
  42. if ( isset ( $field_data['calc'] ) ) { 
  43. $calc_fields = $field_data['calc']; 
  44. } else { 
  45. $calc_fields = array(); 
  46.  
  47. // Get our calculation equation if it exists. 
  48. if ( isset ( $field_data['calc_eq'] ) ) { 
  49. $calc_eq = $field_data['calc_eq']; 
  50. } else { 
  51. $calc_eq = array(); 
  52.  
  53. // Get our calculation equation if it exists. 
  54. if ( isset ( $field_data['calc_places'] ) ) { 
  55. $calc_places = $field_data['calc_places']; 
  56. } else { 
  57. $calc_places = 0; 
  58.  
  59. // 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. 
  60. $tax = false; 
  61. $sub_total = false; 
  62. if ( is_array ( $all_fields ) ) { 
  63. foreach ( $all_fields as $f_id => $user_value ) { 
  64. if ( isset ( $ninja_forms_loading ) ) { 
  65. $field = $ninja_forms_loading->get_field_settings( $f_id ); 
  66. } else { 
  67. $field = $ninja_forms_processing->get_field_settings( $f_id ); 
  68.  
  69. if ( isset ( $field['type'] ) ) { 
  70. $f_type = $field['type']; 
  71. } else { 
  72. $f_type = ''; 
  73.  
  74. $data = apply_filters( 'ninja_forms_field', $field['data'], $f_id ); 
  75. if ( $f_type == '_tax' ) { 
  76. // There is a tax field; save its field_id. 
  77. $tax = $field['id']; 
  78. } else if ( isset ( $data['payment_sub_total'] ) AND $data['payment_sub_total'] == 1 ) { 
  79. // There is a sub_total field; save its field_id. 
  80. $sub_total = $field['id']; 
  81.  
  82.  
  83. // 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. 
  84. if ( $tax AND $sub_total AND isset ( $field_data['payment_total'] ) AND $field_data['payment_total'] == 1 AND $calc_method == 'auto' ) { 
  85. $calc_method = 'eq'; 
  86. if ( isset ( $ninja_forms_loading ) ) { 
  87. $tax_rate = $ninja_forms_loading->get_field_value( $tax ); 
  88. } else { 
  89. $tax_rate = $ninja_forms_processing->get_field_value( $tax ); 
  90.  
  91. if ( strpos( $tax_rate, "%" ) !== false ) { 
  92. $tax_rate = str_replace( "%", "", $tax_rate ); 
  93. $tax_rate = $tax_rate / 100; 
  94. $calc_eq = 'field_'.$sub_total.' + ( field_'.$sub_total.' * '.$tax_rate.' )'; 
  95.  
  96. if ( isset ( $ninja_forms_loading ) ) { 
  97. $field_settings = $ninja_forms_loading->get_field_settings( $field_id ); 
  98. } else { 
  99. $field_settings = $ninja_forms_processing->get_field_settings( $field_id ); 
  100.  
  101. $field_settings['data']['calc_method'] = $calc_method; 
  102. $field_settings['data']['calc_eq'] = $calc_eq; 
  103. if ( isset ( $ninja_forms_loading ) ) { 
  104. $ninja_forms_loading->update_field_settings( $field_id, $field_settings ); 
  105. } else { 
  106. $ninja_forms_processing->update_field_settings( $field_id, $field_settings ); 
  107.  
  108. // Loop through our fields and see which ones should be used for calculations. 
  109. if ( is_array ( $all_fields ) ) { 
  110. foreach ( $all_fields as $f_id => $user_value ) { 
  111. if ( isset ( $ninja_forms_loading ) ) { 
  112. $field = $ninja_forms_loading->get_field_settings( $f_id ); 
  113. $field_value = $ninja_forms_loading->get_field_value( $f_id ); 
  114. } else { 
  115. $field = $ninja_forms_processing->get_field_settings( $f_id ); 
  116. $field_value = $ninja_forms_processing->get_field_value( $f_id ); 
  117.  
  118. $field_data = $field['data']; 
  119. if ( $f_id == $tax ) { 
  120. $tax = ninja_forms_field_calc_value( $field['id'], $field_value, 'auto' );; 
  121.  
  122. switch ( $calc_method ) { 
  123. case 'auto': // We are automatically totalling the fields that have a calc_auto_include set to 1. 
  124. if ( isset ( $field_data['calc_auto_include'] ) AND $field_data['calc_auto_include'] == 1 && $field_value ) { 
  125.  
  126. if ( $field['type'] == '_calc' ) { 
  127. $calc_value = ninja_forms_calc_field_loop( $field['id'], '', $result ); 
  128. } else { 
  129. $calc_value = ninja_forms_field_calc_value( $field['id'], $field_value, $calc_method ); 
  130.  
  131. if ( $calc_value !== false ) { 
  132. $result = ninja_forms_calc_evaluate( 'add', $result, $calc_value ); 
  133.  
  134. break; 
  135. case 'fields': // We are performing a specific set of operations on a set of fields. 
  136. if ( is_array ( $calc_fields ) ) { 
  137. foreach ( $calc_fields as $c ) { 
  138. if ( $c['field'] == $field['id'] ) { 
  139. if ( $field['type'] == '_calc' ) { 
  140. $calc_value = ninja_forms_calc_field_loop( $field['id'], '', $result ); 
  141. $calc_value = ninja_forms_field_calc_value( $field['id'], $field_value, $calc_method ); 
  142. if ( $calc_value !== false ) { 
  143. $result = ninja_forms_calc_evaluate( $c['op'], $result, $calc_value ); 
  144. break; 
  145. case 'eq': 
  146. if (preg_match("/\bfield_".$f_id."\b/i", $calc_eq ) ) { 
  147. if ( $field['type'] == '_calc' ) { 
  148. $calc_value = ninja_forms_calc_field_loop( $field['id'], $calc_eq ); 
  149. } else { 
  150. $calc_value = ninja_forms_field_calc_value( $field['id'], $field_value, $calc_method ); 
  151.  
  152. if ( $calc_value !== false ) { 
  153. $calc_eq = preg_replace('/\bfield_'.$field['id'].'\b/', $calc_value, $calc_eq ); 
  154. break; 
  155.  
  156. if ( $calc_method == 'eq' ) { 
  157. $eq = new eqEOS(); 
  158.  
  159. // Swap out decimal separator 
  160. $decimal_point = $wp_locale->number_format['decimal_point']; 
  161. $calc_eq = str_replace( $decimal_point, '.', $calc_eq ); 
  162.  
  163. $result = $eq->solveIF($calc_eq); 
  164.  
  165. // Swap back decimal separator 
  166. $result = str_replace( '.', $decimal_point, $result ); 
  167.  
  168. if ( isset ( $calc_places ) ) { 
  169. if ( empty( $calc_places ) ) { 
  170. $calc_places = 0; 
  171.  
  172. // Swap out decimal separator 
  173. $decimal_point = $wp_locale->number_format['decimal_point']; 
  174. $result = str_replace( $decimal_point, '.', $result ); 
  175.  
  176. // Round and Format 
  177. $result = number_format( round( $result, $calc_places ), $calc_places ); 
  178.  
  179. // Swap back decimal separator 
  180. $result = str_replace( '.', $decimal_point, $result ); 
  181. $result = str_replace( $wp_locale->number_format['thousands_sep'], '', $result ); 
  182.  
  183. if ( isset ( $ninja_forms_loading ) ) { 
  184. $ninja_forms_loading->update_field_value( $field_id, $result ); 
  185. } else { 
  186. $ninja_forms_processing->update_field_value( $field_id, $result ); 
  187.