| @@ -23,40 +23,33 @@ | ||
| 23 | 23 | */ |
| 24 | 24 | class TablePress_Evaluate { |
| 25 | 25 | |
| 26 | 26 | /** |
| 27 | - * Instance of the EvalMath class. | |
| 27 | + * Checks whether the requirements for the PHPSpreadsheet evaluate class are fulfilled or if the legacy evaluate class should be used. | |
| 28 | 28 | * |
| 29 | - * @since 1.0.0 | |
| 30 | - * @var EvalMath | |
| 31 | - */ | |
| 32 | - protected $evalmath; | |
| 33 | - | |
| 34 | - /** | |
| 35 | - * Table data in which formulas shall be evaluated. | |
| 29 | + * @since 2.0.0 | |
| 36 | 30 | * |
| 37 | - * @since 1.5.0 | |
| 38 | - * @var array | |
| 31 | + * @return bool Whether the legacy evaluate class should be used. | |
| 39 | 32 | */ |
| 40 | - protected $table_data; | |
| 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 | + } | |
| 41 | 44 | |
| 42 | - /** | |
| 43 | - * Storage for cell ranges that have been replaced in formulas. | |
| 44 | - * | |
| 45 | - * @since 1.0.0 | |
| 46 | - * @var array | |
| 47 | - */ | |
| 48 | - protected $known_ranges = array(); | |
| 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 | + } | |
| 49 | 50 | |
| 50 | - /** | |
| 51 | - * Initialize the Formula Evaluation class, include the EvalMath class. | |
| 52 | - * | |
| 53 | - * @since 1.0.0 | |
| 54 | - */ | |
| 55 | - public function __construct() { | |
| 56 | - $this->evalmath = TablePress::load_class( 'EvalMath', 'evalmath.class.php', 'libraries' ); | |
| 57 | - // Don't raise PHP warnings. | |
| 58 | - $this->evalmath->suppress_errors = true; | |
| 51 | + return false; | |
| 59 | 52 | } |
| 60 | 53 | |
| 61 | 54 | /** |
| 62 | 55 | * Evaluate formulas in the passed table. |
| @@ -62,183 +55,20 @@ | ||
| 62 | 55 | * Evaluate formulas in the passed table. |
| 63 | 56 | * |
| 64 | 57 | * @since 1.0.0 |
| 65 | 58 | * |
| 66 | - * @param array $table_data Table data in which formulas shall be evaluated. | |
| 67 | - * @param string $table_id ID of the passed table. | |
| 68 | - * @return array Table data with evaluated formulas. | |
| 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. | |
| 69 | 62 | */ |
| 70 | - public function evaluate_table_data( array $table_data, $table_id ) { | |
| 71 | - $this->table_data = $table_data; | |
| 72 | - $num_rows = count( $this->table_data ); | |
| 73 | - $num_columns = count( $this->table_data[0] ); | |
| 74 | - | |
| 75 | - // Make fixed table data available as variables in formulas. | |
| 76 | - $this->evalmath->variables['table_id'] = $table_id; | |
| 77 | - $this->evalmath->variables['num_rows'] = $num_rows; | |
| 78 | - $this->evalmath->variables['num_columns'] = $num_columns; | |
| 79 | - | |
| 80 | - // Use two for-loops instead of foreach here to be sure to always work on the "live" table data and not some in-memory copy. | |
| 81 | - for ( $row_idx = 0; $row_idx < $num_rows; $row_idx++ ) { | |
| 82 | - for ( $col_idx = 0; $col_idx < $num_columns; $col_idx++ ) { | |
| 83 | - $this->table_data[ $row_idx ][ $col_idx ] = $this->_evaluate_cell( $this->table_data[ $row_idx ][ $col_idx ], $row_idx, $col_idx ); | |
| 84 | - } | |
| 85 | - } | |
| 86 | - | |
| 87 | - return $this->table_data; | |
| 88 | - } | |
| 89 | - | |
| 90 | - /** | |
| 91 | - * Parse and evaluate the content of a cell. | |
| 92 | - * | |
| 93 | - * @since 1.0.0 | |
| 94 | - * | |
| 95 | - * @param string $content Content of a cell. | |
| 96 | - * @param int $row_idx Row index of the cell. | |
| 97 | - * @param int $col_idx Column index of the cell. | |
| 98 | - * @param array $parents Optional. List of cells that depend on this cell (to prevent circle references). | |
| 99 | - * @return string Result of the parsing/evaluation. | |
| 100 | - */ | |
| 101 | - protected function _evaluate_cell( $content, $row_idx, $col_idx, array $parents = array() ) { | |
| 102 | - if ( '' === $content || '=' === $content || '=' !== $content[0] ) { | |
| 103 | - return $content; | |
| 104 | - } | |
| 105 | - | |
| 106 | - // Cut off the leading =. | |
| 107 | - $content = substr( $content, 1 ); | |
| 108 | - | |
| 109 | - // Support putting formulas in strings, like =Total: {A3+A4}. | |
| 110 | - $expressions = array(); | |
| 111 | - if ( preg_match_all( '#{(.+?)}#', $content, $expressions, PREG_SET_ORDER ) ) { | |
| 112 | - $formula_in_string = true; | |
| 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' ); | |
| 113 | 67 | } else { |
| 114 | - $formula_in_string = false; | |
| 115 | - // Fill array so that it has the same structure as if it came from preg_match_all(). | |
| 116 | - $expressions[] = array( $content, $content ); | |
| 68 | + $evaluate_class = TablePress::load_class( 'TablePress_Evaluate_PHPSpreadsheet', 'class-evaluate-phpspreadsheet.php', 'classes' ); | |
| 117 | 69 | } |
| 118 | 70 | |
| 119 | - foreach ( $expressions as $expression ) { | |
| 120 | - $orig_expression = $expression[0]; | |
| 121 | - $expression = $expression[1]; | |
| 122 | - | |
| 123 | - $replaced_references = $replaced_ranges = array(); | |
| 124 | - | |
| 125 | - // Remove all whitespace characters. | |
| 126 | - $expression = str_replace( array( "\n", "\r", "\t", ' ' ), '', $expression ); | |
| 127 | - | |
| 128 | - // Expand cell ranges (like A3:A6) to a list of single cells (like A3,A4,A5,A6). | |
| 129 | - if ( preg_match_all( '#([A-Z]+)([0-9]+):([A-Z]+)([0-9]+)#', $expression, $referenced_cell_ranges, PREG_SET_ORDER ) ) { | |
| 130 | - foreach ( $referenced_cell_ranges as $cell_range ) { | |
| 131 | - if ( in_array( $cell_range[0], $replaced_ranges, true ) ) { | |
| 132 | - continue; | |
| 133 | - } | |
| 134 | - | |
| 135 | - $replaced_ranges[] = $cell_range[0]; | |
| 136 | - | |
| 137 | - if ( isset( $this->known_ranges[ $cell_range[0] ] ) ) { | |
| 138 | - $expression = preg_replace( '#(?<![A-Z])' . preg_quote( $cell_range[0], '#' ) . '(?![0-9])#', $this->known_ranges[ $cell_range[0] ], $expression ); | |
| 139 | - continue; | |
| 140 | - } | |
| 141 | - | |
| 142 | - // No -1 necessary for this transformation, as we don't actually access the table. | |
| 143 | - $first_col = TablePress::letter_to_number( $cell_range[1] ); | |
| 144 | - $first_row = $cell_range[2]; | |
| 145 | - $last_col = TablePress::letter_to_number( $cell_range[3] ); | |
| 146 | - $last_row = $cell_range[4]; | |
| 147 | - | |
| 148 | - $col_start = min( $first_col, $last_col ); | |
| 149 | - $col_end = max( $first_col, $last_col ) + 1; // +1 for loop below | |
| 150 | - $row_start = min( $first_row, $last_row ); | |
| 151 | - $row_end = max( $first_row, $last_row ) + 1; // +1 for loop below | |
| 152 | - | |
| 153 | - $cell_list = array(); | |
| 154 | - for ( $col = $col_start; $col < $col_end; $col++ ) { | |
| 155 | - for ( $row = $row_start; $row < $row_end; $row++ ) { | |
| 156 | - $column = TablePress::number_to_letter( $col ); | |
| 157 | - $cell_list[] = "{$column}{$row}"; | |
| 158 | - } | |
| 159 | - } | |
| 160 | - $cell_list = implode( ',', $cell_list ); | |
| 161 | - | |
| 162 | - $expression = preg_replace( '#(?<![A-Z])' . preg_quote( $cell_range[0], '#' ) . '(?![0-9])#', $cell_list, $expression ); | |
| 163 | - $this->known_ranges[ $cell_range[0] ] = $cell_list; | |
| 164 | - } | |
| 165 | - } | |
| 166 | - | |
| 167 | - // Parse and evaluate single cell references (like A3 or XY312), while prohibiting circle references. | |
| 168 | - if ( preg_match_all( '#([A-Z]+)([0-9]+)(?![0-9A-Z\(])#', $expression, $referenced_cells, PREG_SET_ORDER ) ) { | |
| 169 | - foreach ( $referenced_cells as $cell_reference ) { | |
| 170 | - if ( in_array( $cell_reference[0], $parents, true ) ) { | |
| 171 | - return '!ERROR! Circle Reference'; | |
| 172 | - } | |
| 173 | - | |
| 174 | - if ( in_array( $cell_reference[0], $replaced_references, true ) ) { | |
| 175 | - continue; | |
| 176 | - } | |
| 177 | - | |
| 178 | - $replaced_references[] = $cell_reference[0]; | |
| 179 | - | |
| 180 | - $ref_col = TablePress::letter_to_number( $cell_reference[1] ) - 1; | |
| 181 | - $ref_row = $cell_reference[2] - 1; | |
| 182 | - | |
| 183 | - if ( ! isset( $this->table_data[ $ref_row ] ) || ! isset( $this->table_data[ $ref_row ][ $ref_col ] ) ) { | |
| 184 | - return "!ERROR! Cell {$cell_reference[0]} does not exist"; | |
| 185 | - } | |
| 186 | - | |
| 187 | - $ref_parents = $parents; | |
| 188 | - $ref_parents[] = $cell_reference[0]; | |
| 189 | - | |
| 190 | - $result = $this->table_data[ $ref_row ][ $ref_col ] = $this->_evaluate_cell( $this->table_data[ $ref_row ][ $ref_col ], $ref_row, $ref_col, $ref_parents ); | |
| 191 | - // Bail if there was an error already. | |
| 192 | - if ( false !== strpos( $result, '!ERROR!' ) ) { | |
| 193 | - return $result; | |
| 194 | - } | |
| 195 | - // Remove all whitespace characters. | |
| 196 | - $result = str_replace( array( "\n", "\r", "\t", ' ' ), '', $result ); | |
| 197 | - // Treat empty cells as 0. | |
| 198 | - if ( '' === $result ) { | |
| 199 | - $result = 0; | |
| 200 | - } | |
| 201 | - // Bail if the cell does not result in a number (meaning it was a number or expression before being evaluated). | |
| 202 | - if ( ! is_numeric( $result ) ) { | |
| 203 | - return "!ERROR! {$cell_reference[0]} does not contain a number or expression"; | |
| 204 | - } | |
| 205 | - | |
| 206 | - $expression = preg_replace( '#(?<![A-Z])' . $cell_reference[0] . '(?![0-9])#', $result, $expression ); | |
| 207 | - } | |
| 208 | - } | |
| 209 | - | |
| 210 | - $result = $this->_evaluate_math_expression( $expression, $row_idx, $col_idx ); | |
| 211 | - // Support putting formulas in strings, like =Total: {A3+A4}. | |
| 212 | - if ( $formula_in_string ) { | |
| 213 | - $content = str_replace( $orig_expression, $result, $content ); | |
| 214 | - } else { | |
| 215 | - $content = $result; | |
| 216 | - } | |
| 217 | - } | |
| 218 | - | |
| 219 | - return $content; | |
| 220 | - } | |
| 221 | - | |
| 222 | - /** | |
| 223 | - * Evaluate a math expression. | |
| 224 | - * | |
| 225 | - * @since 1.0.0 | |
| 226 | - * | |
| 227 | - * @param string $expression without leading = sign. | |
| 228 | - * @return string Result of the evaluation. | |
| 229 | - */ | |
| 230 | - protected function _evaluate_math_expression( $expression, $row_idx, $col_idx ) { | |
| 231 | - // Make current cell's name and row and column number available as variables in formulas. | |
| 232 | - $this->evalmath->variables['row'] = $row_idx + 1; | |
| 233 | - $this->evalmath->variables['column'] = $col_idx + 1; | |
| 234 | - $this->evalmath->variables['cell'] = TablePress::number_to_letter( $this->evalmath->variables['column'] ) . $this->evalmath->variables['row']; | |
| 235 | - // Straight up evaluation, without parsing of variable or function assignments (which is why we only need one instance of the object). | |
| 236 | - $result = $this->evalmath->evaluate( $expression ); | |
| 237 | - if ( false === $result ) { | |
| 238 | - return '!ERROR! ' . $this->evalmath->last_error; | |
| 239 | - } else { | |
| 240 | - return (string) $result; | |
| 241 | - } | |
| 71 | + return $evaluate_class->evaluate_table_data( $table_data, $table_id ); | |
| 242 | 72 | } |
| 243 | 73 | |
| 244 | 74 | } // class TablePress_Evaluate |