| 1 |
<?php |
| 2 |
/** |
| 3 |
* TablePress Formula Evaluation Class |
| 4 |
* |
| 5 |
* @package TablePress |
| 6 |
* @subpackage Formulas |
| 7 |
* @author Tobias Bäthge |
| 8 |
* @since 1.5.0 |
| 9 |
*/ |
| 10 |
|
| 11 |
// Prohibit direct script loading. |
| 12 |
defined( 'ABSPATH' ) || die( 'No direct script access allowed!' ); |
| 13 |
|
| 14 |
/** |
| 15 |
* TablePress Formula Evaluation Class |
| 16 |
* |
| 17 |
* Before TablePress 1.5, this was part of the TablePress_Render class. |
| 18 |
* |
| 19 |
* @package TablePress |
| 20 |
* @subpackage Formulas |
| 21 |
* @author Tobias Bäthge |
| 22 |
* @since 1.5.0 |
| 23 |
*/ |
| 24 |
class TablePress_Evaluate { |
| 25 |
|
| 26 |
/** |
| 27 |
* Checks whether the requirements for the PHPSpreadsheet evaluate class are fulfilled or if the legacy evaluate class should be used. |
| 28 |
* |
| 29 |
* @since 2.0.0 |
| 30 |
* |
| 31 |
* @return bool Whether the legacy evaluate class should be used. |
| 32 |
*/ |
| 33 |
protected function _should_use_legacy_evaluate_class(): bool { |
| 34 |
/** |
| 35 |
* Filters whether the Legacy Table Evaluate class shall be used. |
| 36 |
* |
| 37 |
* @since 2.0.0 |
| 38 |
* |
| 39 |
* @param bool $use_legacy_class Whether to use the legacy table evaluate class. Default false. |
| 40 |
*/ |
| 41 |
if ( apply_filters( 'tablepress_use_legacy_table_evaluate_class', false ) ) { |
| 42 |
return true; |
| 43 |
} |
| 44 |
|
| 45 |
// Use the legacy evaluate class, if the requirements for PHPSpreadsheet Calculations are not fulfilled. |
| 46 |
$phpspreadsheet_requirements_fulfilled = extension_loaded( 'mbstring' ); |
| 47 |
if ( ! $phpspreadsheet_requirements_fulfilled ) { |
| 48 |
return true; |
| 49 |
} |
| 50 |
|
| 51 |
return false; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Evaluate formulas in the passed table. |
| 56 |
* |
| 57 |
* @since 1.0.0 |
| 58 |
* |
| 59 |
* @param array<int, array<int, string>> $table_data Table data in which formulas shall be evaluated. |
| 60 |
* @param string $table_id ID of the passed table. |
| 61 |
* @return array<int, array<int, string>> Table data with evaluated formulas. |
| 62 |
*/ |
| 63 |
public function evaluate_table_data( array $table_data, string $table_id ): array { |
| 64 |
// Choose the Table Evaluate library based on the PHP version and the filter hook value. |
| 65 |
if ( $this->_should_use_legacy_evaluate_class() ) { |
| 66 |
$evaluate_class = TablePress::load_class( 'TablePress_Evaluate_Legacy', 'class-evaluate-legacy.php', 'classes' ); |
| 67 |
} else { |
| 68 |
$evaluate_class = TablePress::load_class( 'TablePress_Evaluate_PHPSpreadsheet', 'class-evaluate-phpspreadsheet.php', 'classes' ); |
| 69 |
} |
| 70 |
|
| 71 |
return $evaluate_class->evaluate_table_data( $table_data, $table_id ); |
| 72 |
} |
| 73 |
|
| 74 |
} // class TablePress_Evaluate |
| 75 |
|