ninja_forms_field_calc_value

Function that returns the calculation value of a field given by field_id if it is to be included in the auto total.

Description

(calc_value) ninja_forms_field_calc_value( $field_id, (string) $field_value = '', (string) $calc_method = 'auto' ); 

Returns (calc_value)

Parameters (3)

0. $field_id
The field id.
1. $field_value — Optional. (string) => ''
The field value.
2. $calc_method — Optional. (string) => 'auto'
The calc method.

Usage

  1. if ( !function_exists( 'ninja_forms_field_calc_value' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'ninja-forms/deprecated/includes/fields/calc.php'; 
  3.  
  4. // The field id. 
  5. $field_id = null; 
  6.  
  7. // The field value. 
  8. $field_value = ''; 
  9.  
  10. // The calc method. 
  11. $calc_method = 'auto'; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = ninja_forms_field_calc_value($field_id, $field_value, $calc_method); 
  15.  

Defined (1)

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

/deprecated/includes/fields/calc.php  
  1. function ninja_forms_field_calc_value( $field_id, $field_value = '', $calc_method = 'auto' ) { 
  2. global $ninja_forms_loading, $ninja_forms_processing, $wp_locale; 
  3.  
  4. if ( isset ( $ninja_forms_loading ) ) { 
  5. $field = $ninja_forms_loading->get_field_settings( $field_id ); 
  6. } else { 
  7. $field = $ninja_forms_processing->get_field_settings( $field_id ); 
  8.  
  9. $field_data = apply_filters( 'ninja_forms_field', $field['data'], $field_id ); 
  10.  
  11. if ( isset ( $field_data['default_value'] ) ) { 
  12. $default_value = $field_data['default_value']; 
  13. } else { 
  14. $default_value = ''; 
  15.  
  16. if ( $field_value == '' ) { 
  17. $field_value = $default_value; 
  18.  
  19. $calc_value = 0; 
  20. if ( $field['type'] == '_list' ) { 
  21. if ( isset ( $field_data['list']['options'] ) ) { 
  22. foreach ( $field_data['list']['options'] as $option ) { 
  23. if ( isset ( $field_data['list_show_value'] ) AND $field_data['list_show_value'] == 1 ) { 
  24. $option_value = $option['value']; 
  25. } else { 
  26. $option_value = $option['label']; 
  27. if ( $option_value == $field_value OR ( is_array ( $field_value ) AND in_array ( $option_value, $field_value ) ) ) { 
  28. $calc_value += $option['calc']; 
  29. } else if ( $field['type'] == '_checkbox' ) { 
  30. if ( $field_value == 'checked' ) { 
  31. $calc_value = $field_data['calc_value']['checked']; 
  32. } else { 
  33. if ( $calc_method == 'auto' ) { 
  34. return false; 
  35. } else { 
  36. $calc_value = $field_data['calc_value']['unchecked']; 
  37. } else { 
  38. if ( !$field_value OR $field_value == '' ) { 
  39. $field_value = 0; 
  40. $decimal_point = $wp_locale->number_format['decimal_point']; 
  41.  
  42. /** Casting to a Float removes decimal */ 
  43. // $calc_value = (float) preg_replace('/[^0-9' . $decimal_point . '-]*/', '', $field_value); 
  44. $calc_value = preg_replace('/[^0-9' . $decimal_point . '-]*/', '', $field_value); 
  45.  
  46.  
  47. if ( is_string( $calc_value ) AND strpos( $calc_value, "%" ) !== false ) { 
  48. $calc_value = str_replace( "%", "", $calc_value ); 
  49. $calc_value = $calc_value / 100; 
  50. if ( $calc_value == '' OR !$calc_value ) { 
  51. $calc_value = 0; 
  52.  
  53. return $calc_value;