Skip to content

Commit

Permalink
Merge pull request CalderaWP#3229 from CalderaWP/feature/3228
Browse files Browse the repository at this point in the history
Add support for round, ceil and floor on calculation field PHP 7.2 CalderaWP#3228
  • Loading branch information
Josh Pollock authored May 24, 2019
2 parents 4a0a169 + c9d0ae4 commit 57a7ae6
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 59 deletions.
32 changes: 20 additions & 12 deletions classes/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
*
*/

use FormulaParser\FormulaParser;
use MathParser\StdMathParser;
use MathParser\Interpreting\Evaluator;

class Caldera_Forms
{
Expand Down Expand Up @@ -1404,7 +1405,7 @@ static public function akismet_scanner($config, $form)
static public function check_deprecated_math_functions($formula)
{
//List known depracated functions
$deprecated = array( ',', '**', 'pow', 'acos', 'asin', 'atan', 'atan2', 'ceil', 'floor', 'max', 'min', 'random', 'round');
$deprecated = array( ',', '**', 'pow', 'acos', 'asin', 'atan', 'atan2', 'max', 'min', 'random');
//Look for matching known symbol in formula and push it in a symbols array
foreach ($deprecated as $symbol) {
if (strpos($formula, $symbol) !== FALSE) {
Expand Down Expand Up @@ -1527,6 +1528,8 @@ static public function run_calculation($value, $field, $form)
if (false !== strpos($formula, $fid)) {
$number = Caldera_Forms_Field_Calculation::get_value($form_field, $form);

$number = '(' . $number . ')';

$formula = str_replace($fid, $number, $formula);
}
}
Expand All @@ -1549,19 +1552,24 @@ static public function run_calculation($value, $field, $form)

//Use parser if no deprecated caught
if( $deprecated === false ){
//Initiate parser
$parser = new FormulaParser($formula);
$result = $parser->getResult();
//Get result if parser returns a correct status
if($result[0] === "done"){
$total = $result['1'];
} else if($result[0] === "error") {
//else change notice for admins and run original process
$total = self::calculation_deprecated_caught_process( $formula, __('This formula', 'caldera_forms') );

try {
//Initiate parser
$parser = new StdMathParser();
// Generate an abstract syntax tree
$AST = $parser->parse($formula);
// Do something with the AST, e.g. evaluate the expression:
$evaluator = new Evaluator();
//Get result
$total = $AST->accept($evaluator);

} catch( Exception $e) {
//Return to old process if an Exception was returned and display a warning for admins
$total = self::calculation_deprecated_caught_process( $formula, __('A symbol in the formula', 'caldera_forms') );
}

} else {
//else change notice for admins and run original process
//else add warning for admins and run original process
$total = self::calculation_deprecated_caught_process( $formula, $deprecated );
}

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"composer/installers": "^1.6",
"a5hleyrich/wp-queue": "^1.3",
"symfony/translation": "~3.0",
"denissimon/formula-parser": "^2.5"
"mossadal/math-parser": "^1.3"
},
"autoload": {
"psr-4": {
Expand Down
95 changes: 49 additions & 46 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 57a7ae6

Please sign in to comment.