Skip to content

Commit

Permalink
Merge pull request CalderaWP#3208 from CalderaWP/feature/2335-3
Browse files Browse the repository at this point in the history
Work on calculation field for PHP version >= 7.2.0
  • Loading branch information
New0 authored May 23, 2019
2 parents 80f52e6 + 28fbe20 commit 9858bb0
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 5 deletions.
25 changes: 24 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ env:

matrix:
include:
- php: 7.3
env: WP_VERSION=latest TEST_JS=yes
- php: 7.2
env: WP_VERSION=latest TEST_JS=yes
- php: 7.1
env: WP_VERSION=trunk TEST_JS=yes
- php: 7.1
Expand All @@ -43,7 +47,26 @@ before_script:
- |
if [[ ! -z "$WP_VERSION" ]] ; then
bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION
composer global require "phpunit/phpunit=4.8.*|5.7.*"
if [[ "$WP_TRAVISCI" == "travis:phpunit" ]]; then
case "$TRAVIS_PHP_VERSION" in
7.4snapshot|7.3|7.2|7.1|nightly)
echo "Using PHPUnit 7.x"
travis_retry composer global require "phpunit/phpunit:^7"
;;
7.0)
echo "Using PHPUnit 6.x"
travis_retry composer global require "phpunit/phpunit:^6"
;;
5.6)
echo "Using PHPUnit 4.x"
travis_retry composer global require "phpunit/phpunit:^4"
;;
*)
echo "No PHPUnit version handling for PHP version $TRAVIS_PHP_VERSION"
exit 1
;;
esac
fi
fi
- |
if [[ "$WP_TRAVISCI" == "phpcs" ]] ; then
Expand Down
120 changes: 118 additions & 2 deletions classes/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*
*/

use FormulaParser\FormulaParser;

class Caldera_Forms
{

Expand Down Expand Up @@ -1390,6 +1392,92 @@ static public function akismet_scanner($config, $form)

}

/**
* Catch known functions that were supported at https://calderaforms.com/doc/calculation-fields/ and are not sipported by parser https://github.com/denissimon/formula-parser
*
* @since 1.8.5
*
* @param string $formula The string used to evaluate the math formula
*
* @return bool||string of coma seperated deprecated symbol
*/
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');
//Look for matching known symbol in formula and push it in a symbols array
foreach ($deprecated as $symbol) {
if (strpos($formula, $symbol) !== FALSE) {
$symbols[] = $symbol;
}
}
//If symbols array is not empty convert it to a string and return the string
if(is_array($symbols) && !empty($symbols)){
$symbols = implode( ',', $symbols);
return $symbols;
} else {
//Else return false
return false;
}

}

/**
* Calculation based on create_function() ( deprecated since PHP 7.2.0 )
*
* @since 1.8.5
*
* @param string $formula that needs to be parsed and processed as a calculation
*
* @return result of calculation
*/
static public function original_calculation_job( $formula )
{
$total_function = create_function(null, 'return ' . $formula . ';');
$total = $total_function();

return $total;
}

/**
* Process when php >= 7.2.0 and deprecated is caught
*
* @since 1.8.5
*
* @param string $formula to be sent to original calculation job
* @param string $deprecated function or symbol caught
*
* @return original process and notice for admin users
*/
static public function calculation_deprecated_caught_process( $formula, $deprecated )
{
if( current_user_can( Caldera_Forms::get_manage_cap('admin') ) ) {

$message = sprintf(
'"%s" %s',
$deprecated,
__('has been deprecated in calculation fields for compatibility with PHP 7.2 or later', 'caldera-forms')
);

/**
* The notice currentl only works whe ajax is enabled
*
* TODO Add notice for non ajax submissions
*/
add_filter('caldera_forms_render_notices', function( $out ) use ( $message ){

//Add an error notice holding the $message
$out[ 'error' ][ 'note' ] = $message;

return $out;

}, 25);

}

return self::original_calculation_job($formula);
}

/**
* Process a calculation field.
*
Expand Down Expand Up @@ -1447,9 +1535,37 @@ static public function run_calculation($value, $field, $form)
return new WP_Error($field['ID'] . '-calculation',
__('Calculation is invalid (division by zero)', 'caldera-forms'));
}

//If PHP version is less than 7.2.0, continue using old function
if(version_compare(PHP_VERSION, '7.2.0', '<')){

$total = self::original_calculation_job($formula);

} else {
//else avoid using create_function() when using PHP version >= 7.2.0

//Check if the formula use a symbol or function not supported by the parser but declared as supported at https://calderaforms.com/doc/calculation-fields/
$deprecated = self::check_deprecated_math_functions( $formula );

//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') );
}

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

$total_function = create_function(null, 'return ' . $formula . ';');
$total = $total_function();
}

if (is_infinite($total) || !is_numeric($total)) {
return new WP_Error($field['ID'] . '-calculation', __('Calculation is invalid', 'caldera-forms'));
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"calderawp/caldera-containers": "^0.2.0",
"composer/installers": "^1.6",
"a5hleyrich/wp-queue": "^1.3",
"symfony/translation": "~3.0"
"symfony/translation": "~3.0",
"denissimon/formula-parser": "^2.5"
},
"autoload": {
"psr-4": {
Expand Down
46 changes: 45 additions & 1 deletion composer.lock

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

0 comments on commit 9858bb0

Please sign in to comment.