Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/ninja-fo.../lib/Conversi...
File: Calculations.php
<?php
[0] Fix | Delete
[1] Fix | Delete
final class NF_Conversion_Calculations implements NF_Conversion
[2] Fix | Delete
{
[3] Fix | Delete
private $operations = array(
[4] Fix | Delete
'add' => '+',
[5] Fix | Delete
'subtract' => '-',
[6] Fix | Delete
'multiply' => '*',
[7] Fix | Delete
'divide' => '/'
[8] Fix | Delete
);
[9] Fix | Delete
private $form = array();
[10] Fix | Delete
[11] Fix | Delete
private $tax_rate;
[12] Fix | Delete
[13] Fix | Delete
public function __construct( $form_data )
[14] Fix | Delete
{
[15] Fix | Delete
$this->form = $form_data;
[16] Fix | Delete
}
[17] Fix | Delete
[18] Fix | Delete
public function run()
[19] Fix | Delete
{
[20] Fix | Delete
// Extract Calculations from Fields
[21] Fix | Delete
foreach( $this->form[ 'fields' ] as $key => $field ){
[22] Fix | Delete
[23] Fix | Delete
if( 'tax' == $field[ 'type' ] ){
[24] Fix | Delete
$this->set_tax( $field[ 'default_value' ] );
[25] Fix | Delete
continue;
[26] Fix | Delete
}
[27] Fix | Delete
[28] Fix | Delete
if( 'calc' != $field[ 'type' ] ) continue;
[29] Fix | Delete
[30] Fix | Delete
$calculation = array(
[31] Fix | Delete
'order' => $key,
[32] Fix | Delete
'name' => $field[ 'key' ],
[33] Fix | Delete
'eq' => '',
[34] Fix | Delete
'dec' => $field[ 'calc_places' ]
[35] Fix | Delete
);
[36] Fix | Delete
[37] Fix | Delete
switch( $field[ 'calc_method' ] ){
[38] Fix | Delete
case 'eq':
[39] Fix | Delete
$calculation[ 'eq' ] = $field[ 'calc_eq' ];
[40] Fix | Delete
break;
[41] Fix | Delete
case 'fields':
[42] Fix | Delete
$calculation[ 'eq' ] = trim( array_reduce( $field[ 'calc' ], array( $this, 'reduce_operations' ), '' ) );
[43] Fix | Delete
break;
[44] Fix | Delete
case 'auto':
[45] Fix | Delete
$calculation[ 'eq' ] = trim( array_reduce( $this->form[ 'fields' ], array( $this, 'reduce_auto_total' ), '' ) );
[46] Fix | Delete
break;
[47] Fix | Delete
}
[48] Fix | Delete
[49] Fix | Delete
// Remove any leading `+`.
[50] Fix | Delete
$calculation[ 'eq' ] = ltrim( $calculation[ 'eq' ], '+' );
[51] Fix | Delete
[52] Fix | Delete
// Handle opinionated "Total" calc and tax field.
[53] Fix | Delete
if( 'total' == $field[ 'calc_name' ] && isset( $this->tax_rate ) ){
[54] Fix | Delete
$calculation[ 'eq' ] = "( {$calculation[ 'eq' ]} ) * {$this->tax_rate}";
[55] Fix | Delete
}
[56] Fix | Delete
[57] Fix | Delete
$this->form[ 'settings' ][ 'calculations' ][] = $calculation;
[58] Fix | Delete
}
[59] Fix | Delete
[60] Fix | Delete
// Replace Field IDs with Merge Tags
[61] Fix | Delete
if( isset( $this->form[ 'settings' ][ 'calculations' ] ) ) {
[62] Fix | Delete
foreach ($this->form['fields'] as $field) {
[63] Fix | Delete
[64] Fix | Delete
if( ! isset( $field[ 'id' ] ) ) continue;
[65] Fix | Delete
[66] Fix | Delete
$search = 'field_' . $field['id'];
[67] Fix | Delete
$replace = $this->merge_tag( $field );
[68] Fix | Delete
[69] Fix | Delete
foreach ($this->form['settings']['calculations'] as $key => $calculation) {
[70] Fix | Delete
$this->form['settings']['calculations'][ $key ]['eq'] = str_replace($search, $replace, $calculation['eq']);
[71] Fix | Delete
}
[72] Fix | Delete
}
[73] Fix | Delete
}
[74] Fix | Delete
[75] Fix | Delete
// Convert Calc Fields to HTML Fields for displaying Calculations
[76] Fix | Delete
foreach( $this->form[ 'fields' ] as $key => $field ){
[77] Fix | Delete
[78] Fix | Delete
if( 'tax' == $field[ 'type' ] ){
[79] Fix | Delete
$this->form[ 'fields' ][ $key ][ 'type' ] = 'html';
[80] Fix | Delete
$this->form[ 'fields' ][ $key ][ 'default' ] = "<strong>{$field[ 'label' ]}</strong><br /> {$field[ 'default_value' ]}";
[81] Fix | Delete
continue;
[82] Fix | Delete
}
[83] Fix | Delete
[84] Fix | Delete
if( 'calc' != $field[ 'type' ] ) continue;
[85] Fix | Delete
[86] Fix | Delete
$this->form[ 'fields' ][ $key ][ 'type' ] = 'html';
[87] Fix | Delete
[88] Fix | Delete
if( 'html' == $field[ 'calc_display_type' ] ){
[89] Fix | Delete
// TODO: HTML Output fields seem to loose the label.
[90] Fix | Delete
$search = '[ninja_forms_calc]';
[91] Fix | Delete
$replace = $this->merge_tag( $field );
[92] Fix | Delete
$subject = $field[ 'calc_display_html' ];
[93] Fix | Delete
$this->form[ 'fields' ][ $key ][ 'default' ] = str_replace( $search, $replace, $subject );
[94] Fix | Delete
} else {
[95] Fix | Delete
$this->form[ 'fields' ][ $key ][ 'default' ] = '<strong>' . $field[ 'label' ] . '</strong><br />' . $this->merge_tag( $field );
[96] Fix | Delete
}
[97] Fix | Delete
}
[98] Fix | Delete
[99] Fix | Delete
return $this->form;
[100] Fix | Delete
}
[101] Fix | Delete
[102] Fix | Delete
private function reduce_operations( $eq, $calc )
[103] Fix | Delete
{
[104] Fix | Delete
$operation = $calc[ 'op' ];
[105] Fix | Delete
return ' ' . $eq . $this->operations[ $operation ] . ' field_' . $calc[ 'field' ] . ' ';
[106] Fix | Delete
}
[107] Fix | Delete
[108] Fix | Delete
private function reduce_auto_total( $eq, $field )
[109] Fix | Delete
{
[110] Fix | Delete
if( ! isset( $field[ 'calc_auto_include' ] ) || 1 != $field[ 'calc_auto_include' ] ) return $eq;
[111] Fix | Delete
return $eq . '+ {field:' . $field[ 'key' ] . ':calc} ';
[112] Fix | Delete
}
[113] Fix | Delete
[114] Fix | Delete
private function merge_tag( $field )
[115] Fix | Delete
{
[116] Fix | Delete
$tag = $field[ 'key' ];
[117] Fix | Delete
if( 'calc' == $field[ 'type' ] ){
[118] Fix | Delete
return '{calc:' . $tag . '}';
[119] Fix | Delete
} else {
[120] Fix | Delete
return '{field:' . $tag . ':calc}';
[121] Fix | Delete
}
[122] Fix | Delete
}
[123] Fix | Delete
[124] Fix | Delete
/**
[125] Fix | Delete
* Sets a float formatted tax rate.
[126] Fix | Delete
* @param string|int|float $tax
[127] Fix | Delete
*
[128] Fix | Delete
* @return float|int
[129] Fix | Delete
*/
[130] Fix | Delete
private function set_tax( $tax )
[131] Fix | Delete
{
[132] Fix | Delete
// ex 15% -> 1.15
[133] Fix | Delete
return $this->tax_rate = ( floatval( $tax ) + 100 ) / 100;
[134] Fix | Delete
}
[135] Fix | Delete
}
[136] Fix | Delete
[137] Fix | Delete
add_filter( 'ninja_forms_after_upgrade_settings', 'ninja_forms_conversion_calculations' );
[138] Fix | Delete
function ninja_forms_conversion_calculations( $form_data ){
[139] Fix | Delete
$conversion = new NF_Conversion_Calculations( $form_data );
[140] Fix | Delete
return $conversion->run();
[141] Fix | Delete
}
[142] Fix | Delete
[143] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function