ninja_forms_calc_field_loop

The Ninja Forms ninja forms calc field loop function.

Description

ninja_forms_calc_field_loop( $field_id, (string) $calc_eq = '', (string) $result = '' ); 

Parameters (3)

0. $field_id
The field id.
1. $calc_eq — Optional. (string) => ''
The calc eq.
2. $result — Optional. (string) => ''
The result.

Usage

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

Defined (1)

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

/deprecated/includes/fields/calc.php  
  1. function ninja_forms_calc_field_loop( $field_id, $calc_eq = '', $result = '' ) { 
  2. global $ninja_forms_loading, $ninja_forms_processing; 
  3.  
  4. if ( isset ( $ninja_forms_loading ) ) { 
  5. $field_settings = $ninja_forms_loading->get_field_settings( $field_id ); 
  6. } else { 
  7. $field_settings = $ninja_forms_processing->get_field_settings( $field_id ); 
  8.  
  9. $calc_data = $field_settings['data']; 
  10.  
  11. // Figure out which method we are using to calculate this field. 
  12. if ( isset ( $calc_data['calc_method'] ) ) { 
  13. $calc_method = $calc_data['calc_method']; 
  14. } else { 
  15. $calc_method = 'auto'; 
  16.  
  17. // Get our advanced field op settings if they exist. 
  18. if ( isset ( $calc_data['calc'] ) ) { 
  19. $calc_fields = $calc_data['calc']; 
  20. } else { 
  21. $calc_fields = array(); 
  22.  
  23. // Get our calculation equation if it exists. 
  24. if ( isset ( $calc_data['calc_eq'] ) ) { 
  25. $calc_eq = $calc_data['calc_eq']; 
  26. } else { 
  27. $calc_eq = array(); 
  28.  
  29. if ( isset ( $ninja_forms_loading ) ) { 
  30. $form_id = $ninja_forms_loading->get_form_ID(); 
  31. $all_fields = $ninja_forms_loading->get_all_fields(); 
  32. } else { 
  33. $form_id = $ninja_forms_processing->get_form_ID(); 
  34. $all_fields = $ninja_forms_processing->get_all_fields(); 
  35.  
  36.  
  37. // 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. 
  38. $tax = false; 
  39. $sub_total = false; 
  40. foreach ( $all_fields as $f_id => $user_value ) { 
  41.  
  42. if ( isset ( $ninja_forms_loading ) ) { 
  43. $field = $ninja_forms_loading->get_field_settings( $f_id ); 
  44. } else { 
  45. $field = $ninja_forms_processing->get_field_settings( $f_id ); 
  46.  
  47. $field_value = $user_value; 
  48. $data = $field['data']; 
  49. if ( $field['type'] == '_tax' ) { 
  50. // There is a tax field; save its field_id. 
  51. $tax = $field['id']; 
  52. } else if ( isset ( $data['payment_sub_total'] ) AND $data['payment_sub_total'] == 1 ) { 
  53. // There is a sub_total field; save its field_id. 
  54. $sub_total = $field['id']; 
  55.  
  56. // 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. 
  57. if ( $tax AND $sub_total AND isset ( $calc_data['payment_total'] ) AND $calc_data['payment_total'] == 1 AND $calc_method == 'auto' ) { 
  58. $calc_method = 'eq'; 
  59. if ( isset ( $ninja_forms_loading ) ) { 
  60. $tax_rate = $ninja_forms_loading->get_field_value( $tax ); 
  61. } else { 
  62. $tax_rate = $ninja_forms_processing->get_field_value( $tax ); 
  63.  
  64. if ( strpos( $tax_rate, "%" ) !== false ) { 
  65. $tax_rate = str_replace( "%", "", $tax_rate ); 
  66. $tax_rate = $tax_rate / 100; 
  67. $calc_eq = 'field_'.$sub_total.' + ( field_'.$sub_total.' * '.$tax_rate.' )'; 
  68.  
  69. // Figure out how many calculation fields we have and run 
  70. foreach ( $all_fields as $f_id => $user_value ) { 
  71. if ( isset ( $ninja_forms_loading ) ) { 
  72. $field = $ninja_forms_loading->get_field_settings( $f_id ); 
  73. $field_value = $ninja_forms_loading->get_field_value( $f_id ); 
  74. } else { 
  75. $field = $ninja_forms_processing->get_field_settings( $f_id ); 
  76. $field_value = $ninja_forms_processing->get_field_value( $f_id ); 
  77.  
  78. $field_data = $field['data']; 
  79. if ( $f_id != $field_id ) { 
  80. switch ( $calc_method ) { 
  81. case 'auto': // We are automatically totalling the fields that have a calc_auto_include set to 1. 
  82. if ( isset ( $field_data['calc_auto_include'] ) AND $field_data['calc_auto_include'] == 1 ) { 
  83.  
  84. if ( $field['type'] == '_calc' ) { 
  85. $calc_value = ninja_forms_calc_field_loop( $field['id'], '', $result ); 
  86. } else { 
  87. $calc_value = ninja_forms_field_calc_value( $field['id'], $field_value, $calc_method ); 
  88.  
  89. if ( $calc_value !== false ) { 
  90. $result = ninja_forms_calc_evaluate( 'add', $result, $calc_value ); 
  91.  
  92. break; 
  93. case 'fields': // We are performing a specific set of operations on a set of fields. 
  94. if ( is_array ( $calc_fields ) ) { 
  95. foreach ( $calc_fields as $c ) { 
  96. if ( $c['field'] == $field['id'] ) { 
  97. if ( $field['type'] == '_calc' ) { 
  98. $result = ninja_forms_calc_field_loop( $field['id'], '', $result ); 
  99. } else { 
  100. $calc_value = ninja_forms_field_calc_value( $field['id'], $field_value, $calc_method ); 
  101. if ( $calc_value !== false ) { 
  102. $result = ninja_forms_calc_evaluate( $c['op'], $result, $calc_value ); 
  103. break; 
  104. case 'eq': 
  105. if (preg_match("/\bfield_".$field['id']."\b/i", $calc_eq ) ) { 
  106. if ( $field['type'] == '_calc' ) { 
  107. $calc_value = ninja_forms_calc_field_loop( $field['id'], $calc_eq, $result ); 
  108. } else { 
  109. $calc_value = ninja_forms_field_calc_value( $field['id'], $field_value, $calc_method ); 
  110. if ( $calc_value !== false ) { 
  111. $calc_eq = preg_replace('/\bfield_'.$field['id'].'\b/', $calc_value, $calc_eq ); 
  112. break; 
  113. if ( $calc_method == 'eq' ) { 
  114. $eq = new eqEOS(); 
  115. $result = $eq->solveIF($calc_eq); 
  116.  
  117. if ( $result == '' ) { 
  118. $result = 0; 
  119.  
  120. return $result;