PluginProbe
TablePress – Tables in WordPress made easy / 3.4
TablePress – Tables in WordPress made easy v3.4
3.4 3.3.4 3.3.3 3.3.2 3.3.1 trunk 1.12 1.14 1.9.2 2.0.4 2.1.7 2.1.8 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3 2.3.1 2.3.2 2.4 2.4.1 2.4.2 2.4.3 All 45 releases
← All changes | classes/class-render.php +376 -335 1.14 → 3.4 View file →
@@ -7,13 +7,16 @@
7 7 * @author Tobias Bäthge
8 8 * @since 1.0.0
9 9 */
10 10
11 +declare(strict_types=1);
12 +
11 13 // Prohibit direct script loading.
12 14 defined( 'ABSPATH' ) || die( 'No direct script access allowed!' );
13 15
14 16 /**
15 17 * TablePress Rendering Class
18 + *
16 19 * @package TablePress
17 20 * @subpackage Rendering
18 21 * @author Tobias Bäthge
19 22 * @since 1.0.0
@@ -23,25 +26,25 @@
23 26 /**
24 27 * Table data that is rendered.
25 28 *
26 29 * @since 1.0.0
27 - * @var array
30 + * @var array<string, mixed>
28 31 */
29 - protected $table = array();
32 + protected array $table = array();
30 33
31 34 /**
32 35 * Table options that influence the output result.
33 36 *
34 37 * @since 1.0.0
35 - * @var array
38 + * @var array<string, mixed>
36 39 */
37 - protected $render_options = array();
40 + protected array $render_options = array();
38 41
39 42 /**
40 - * Rendered HTML of the table.
43 + * Rendered HTML code of the table or PHP array.
41 44 *
42 45 * @since 1.0.0
43 - * @var string
46 + * @var string|array<int, array<int, string>>
44 47 */
45 48 protected $output;
46 49
47 50 /**
@@ -47,11 +50,11 @@
47 50 /**
48 51 * Trigger words for colspan, rowspan, or the combination of both.
49 52 *
50 53 * @since 1.0.0
51 - * @var array
54 + * @var array<string, string>
52 55 */
53 - protected $span_trigger = array(
56 + protected array $span_trigger = array(
54 57 'colspan' => '#colspan#',
55 58 'rowspan' => '#rowspan#',
56 59 'span' => '#span#',
57 60 );
@@ -59,35 +62,40 @@
59 62 /**
60 63 * Buffer to store the counts of rowspan per column, initialized in _render_table().
61 64 *
62 65 * @since 1.0.0
63 - * @var array
66 + * @var int[]
64 67 */
65 - protected $rowspan = array();
68 + protected array $rowspan = array();
66 69
67 70 /**
68 71 * Buffer to store the counts of colspan per row, initialized in _render_table().
69 72 *
70 73 * @since 1.0.0
71 - * @var array
74 + * @var int[]
72 75 */
73 - protected $colspan = array();
76 + protected array $colspan = array();
74 77
75 78 /**
79 + * Whether the table has connected cells (colspan or rowspan), set in _render_table().
80 + *
81 + * @since 3.0.0
82 + */
83 + protected bool $tbody_has_connected_cells = false;
84 +
85 + /**
76 86 * Index of the last row of the visible data in the table, set in _render_table().
77 87 *
78 88 * @since 1.0.0
79 - * @var int
80 89 */
81 - protected $last_row_idx;
90 + protected int $last_row_idx;
82 91
83 92 /**
84 93 * Index of the last column of the visible data in the table, set in _render_table().
85 94 *
86 95 * @since 1.0.0
87 - * @var int
88 96 */
89 - protected $last_column_idx;
97 + protected int $last_column_idx;
90 98
91 99 /**
92 100 * Class constructor.
93 101 *
@@ -101,21 +109,21 @@
101 109 * Set the table (data, options, visibility, ...) that is to be rendered.
102 110 *
103 111 * @since 1.0.0
104 112 *
105 - * @param array $table Table to be rendered.
106 - * @param array $render_options Options for rendering, from both "Edit" screen and Shortcode.
113 + * @param array<string, mixed> $table Table to be rendered.
114 + * @param array<string, mixed> $render_options Options for rendering, from both "Edit" screen and Shortcode.
107 115 */
108 - public function set_input( array $table, array $render_options ) {
116 + public function set_input( array $table, array $render_options ): void {
109 117 $this->table = $table;
110 118 $this->render_options = $render_options;
111 119 /**
112 - * Filter the table before the render process.
120 + * Filters the table before the render process.
113 121 *
114 122 * @since 1.0.0
115 123 *
116 - * @param array $table The table.
117 - * @param array $render_options The render options for the table.
124 + * @param array<string, mixed> $table The table.
125 + * @param array<string, mixed> $render_options The render options for the table.
118 126 */
119 127 $this->table = apply_filters( 'tablepress_table_raw_render_data', $this->table, $this->render_options );
120 128 }
121 129
@@ -122,18 +130,39 @@
122 130 /**
123 131 * Process the table rendering and return the HTML output.
124 132 *
125 133 * @since 1.0.0
134 + * @since 2.0.0 Add the $format parameter.
126 135 *
127 - * @return string HTML of the rendered table (or an error message).
136 + * @param string $format Optional. Output format, 'html' (default) or 'array'.
137 + * @return string|array<int, array<int, string>> HTML code of the rendered table, or a PHP array, or an error message.
128 138 */
129 - public function get_output() {
139 + public function get_output( string $format = 'html' ) /* : string|array */ {
130 140 // Evaluate math expressions/formulas.
131 141 $this->_evaluate_table_data();
132 142 // Remove hidden rows and columns.
133 143 $this->_prepare_render_data();
134 - // Generate HTML output.
135 - $this->_render_table();
144 +
145 + if ( 'html' !== $format ) {
146 + add_filter( 'tablepress_cell_content', 'wptexturize' );
147 + }
148 +
149 + // Evaluate Shortcodes and escape cell content.
150 + $this->_process_render_data();
151 +
152 + if ( 'html' !== $format ) {
153 + remove_filter( 'tablepress_cell_content', 'wptexturize' );
154 + }
155 +
156 + switch ( $format ) {
157 + case 'html':
158 + $this->_render_table();
159 + break;
160 + case 'array':
161 + $this->output = $this->table['data'];
162 + break;
163 + }
164 +
136 165 return $this->output;
137 166 }
138 167
139 168 /**
@@ -140,21 +169,24 @@
140 169 * Loop through the table to evaluate math expressions/formulas.
141 170 *
142 171 * @since 1.0.0
143 172 */
144 - protected function _evaluate_table_data() {
173 + protected function _evaluate_table_data(): void {
145 174 $orig_table = $this->table;
146 175
147 - $formula_evaluator = TablePress::load_class( 'TablePress_Evaluate', 'class-evaluate.php', 'classes' );
148 - $this->table['data'] = $formula_evaluator->evaluate_table_data( $this->table['data'], $this->table['id'] );
176 + if ( $this->render_options['evaluate_formulas'] ) {
177 + $formula_evaluator = TablePress::load_class( 'TablePress_Evaluate', 'class-evaluate.php', 'classes' );
178 + $this->table['data'] = $formula_evaluator->evaluate_table_data( $this->table['data'], $this->table['id'] );
179 + }
180 +
149 181 /**
150 - * Filter the table after evaluating formulas in the table.
182 + * Filters the table after evaluating formulas in the table.
151 183 *
152 184 * @since 1.0.0
153 185 *
154 - * @param array $table The table with evaluated formulas.
155 - * @param array $orig_table The table with unevaluated formulas.
156 - * @param array $render_options The render options for the table.
186 + * @param array<string, mixed> $table The table with evaluated formulas.
187 + * @param array<string, mixed> $orig_table The table with unevaluated formulas.
188 + * @param array<string, mixed> $render_options The render options for the table.
157 189 */
158 190 $this->table = apply_filters( 'tablepress_table_evaluate_data', $this->table, $orig_table, $this->render_options );
159 191 }
160 192
@@ -162,13 +194,15 @@
162 194 * Remove all cells from the data set that shall not be rendered, because they are hidden.
163 195 *
164 196 * @since 1.0.0
165 197 */
166 - protected function _prepare_render_data() {
198 + protected function _prepare_render_data(): void {
167 199 $orig_table = $this->table;
168 200
169 - $num_rows = count( $this->table['data'] );
170 - $num_columns = ( $num_rows > 0 ) ? count( $this->table['data'][0] ) : 0;
201 + $counts = array(
202 + 'rows' => count( $this->table['data'] ),
203 + );
204 + $counts['columns'] = ( $counts['rows'] > 0 ) ? count( $this->table['data'][0] ) : 0;
171 205
172 206 // Evaluate show/hide_rows/columns parameters.
173 207 $actions = array( 'show', 'hide' );
174 208 $elements = array( 'rows', 'columns' );
@@ -173,54 +207,23 @@
173 207 $actions = array( 'show', 'hide' );
174 208 $elements = array( 'rows', 'columns' );
175 209 foreach ( $actions as $action ) {
176 210 foreach ( $elements as $element ) {
177 - if ( empty( $this->render_options[ "{$action}_{$element}" ] ) ) {
178 - $this->render_options[ "{$action}_{$element}" ] = array();
211 + $action_element = "{$action}_{$element}";
212 +
213 + if ( empty( $this->render_options[ $action_element ] ) ) {
214 + $this->render_options[ $action_element ] = array();
179 215 continue;
180 216 }
181 217
182 - // Add all rows/columns to array if "all" value set for one of the four parameters.
183 - if ( 'all' === $this->render_options[ "{$action}_{$element}" ] ) {
184 - $this->render_options[ "{$action}_{$element}" ] = range( 0, ${'num_' . $element} - 1 );
218 + // Add all rows/columns to array if a parameter is set to "all".
219 + if ( 'all' === $this->render_options[ $action_element ] ) {
220 + $this->render_options[ $action_element ] = range( 0, $counts[ $element ] - 1 );
185 221 continue;
186 222 }
187 223
188 - // We have a list of rows/columns (possibly with ranges in it).
189 - $this->render_options[ "{$action}_{$element}" ] = explode( ',', $this->render_options[ "{$action}_{$element}" ] );
190 - // Support for ranges like 3-6 or A-BA.
191 - $range_cells = array();
192 - foreach ( $this->render_options[ "{$action}_{$element}" ] as $key => $value ) {
193 - $range_dash = strpos( $value, '-' );
194 - if ( false !== $range_dash ) {
195 - unset( $this->render_options[ "{$action}_{$element}" ][ $key ] );
196 - $start = substr( $value, 0, $range_dash );
197 - if ( ! is_numeric( $start ) ) {
198 - $start = TablePress::letter_to_number( $start );
199 - }
200 - $end = substr( $value, $range_dash + 1 );
201 - if ( ! is_numeric( $end ) ) {
202 - $end = TablePress::letter_to_number( $end );
203 - }
204 - $current_range = range( $start, $end );
205 - $range_cells = array_merge( $range_cells, $current_range );
206 - }
207 - }
208 - $this->render_options[ "{$action}_{$element}" ] = array_merge( $this->render_options[ "{$action}_{$element}" ], $range_cells );
209 - /*
210 - * Parse single letters and change from regular numbering to zero-based numbering,
211 - * as rows/columns are indexed from 0 internally, but from 1 externally
212 - */
213 - foreach ( $this->render_options[ "{$action}_{$element}" ] as $key => $value ) {
214 - if ( ! is_numeric( $value ) ) {
215 - $value = TablePress::letter_to_number( $value );
216 - }
217 - $this->render_options[ "{$action}_{$element}" ][ $key ] = (int) $value - 1;
218 - }
219 -
220 - // Remove duplicate entries and sort the array.
221 - $this->render_options[ "{$action}_{$element}" ] = array_unique( $this->render_options[ "{$action}_{$element}" ] );
222 - sort( $this->render_options[ "{$action}_{$element}" ], SORT_NUMERIC );
224 + // Convert the list to a unique, sorted, and zero-based array of integers.
225 + $this->render_options[ $action_element ] = TablePress::convert_row_column_list_to_array( $this->render_options[ $action_element ], 1, $counts[ $element ], array( 'zero-based' => true ) );
223 226 }
224 227 }
225 228
226 229 // Load information about hidden rows and columns.
@@ -246,30 +249,86 @@
246 249 $this->table['data'][ $row_idx ] = array_merge( $row );
247 250 }
248 251
249 252 /**
250 - * Filter the table after processing the table visibility information.
253 + * Filters the table after processing the table visibility information.
251 254 *
252 255 * @since 1.0.0
253 256 *
254 - * @param array $table The processed table.
255 - * @param array $orig_table The unprocessed table.
256 - * @param array $render_options The render options for the table.
257 + * @param array<string, mixed> $table The processed table.
258 + * @param array<string, mixed> $orig_table The unprocessed table.
259 + * @param array<string, mixed> $render_options The render options for the table.
257 260 */
258 261 $this->table = apply_filters( 'tablepress_table_render_data', $this->table, $orig_table, $this->render_options );
259 262 }
260 263
261 264 /**
265 + * Generate the data that is to be rendered.
266 + *
267 + * @since 2.0.0
268 + */
269 + protected function _process_render_data(): void {
270 + $orig_table = $this->table;
271 +
272 + // Deactivate nl2br() for this render process, if "convert_line_breaks" Shortcode parameter is set to false.
273 + if ( ! $this->render_options['convert_line_breaks'] ) {
274 + add_filter( 'tablepress_apply_nl2br', '__return_false', 9 ); // Priority 9, so that this filter can easily be overwritten at the default priority.
275 + }
276 +
277 + foreach ( $this->table['data'] as $row_idx => $row ) {
278 + foreach ( $row as $col_idx => $cell_content ) {
279 + // Print formulas that are escaped with '= (like in Excel) as text.
280 + if ( str_starts_with( $cell_content, "'=" ) ) {
281 + $cell_content = substr( $cell_content, 1 );
282 + }
283 + $cell_content = $this->safe_output( $cell_content );
284 + if ( str_contains( $cell_content, '[' ) ) {
285 + $cell_content = do_shortcode( $cell_content );
286 + }
287 + /**
288 + * Filters the content of a single cell, after formulas have been evaluated, the output has been sanitized, and Shortcodes have been evaluated.
289 + *
290 + * @since 1.0.0
291 + *
292 + * @param string $cell_content The cell content.
293 + * @param string $table_id The current table ID.
294 + * @param int $row_idx The row number of the cell.
295 + * @param int $col_idx The column number of the cell.
296 + */
297 + $cell_content = apply_filters( 'tablepress_cell_content', $cell_content, $this->table['id'], $row_idx + 1, $col_idx + 1 );
298 + $this->table['data'][ $row_idx ][ $col_idx ] = $cell_content;
299 + }
300 + }
301 +
302 + // Re-instate nl2br() behavior after this render process, if "convert_line_breaks" Shortcode parameter is set to false.
303 + if ( ! $this->render_options['convert_line_breaks'] ) {
304 + remove_filter( 'tablepress_apply_nl2br', '__return_false', 9 ); // Priority 9, so that this filter can easily be overwritten at the default priority.
305 + }
306 +
307 + /**
308 + * Filters the table after processing the table content handling.
309 + *
310 + * @since 2.0.0
311 + *
312 + * @param array<string, mixed> $table The processed table.
313 + * @param array<string, mixed> $orig_table The unprocessed table.
314 + * @param array<string, mixed> $render_options The render options for the table.
315 + */
316 + $this->table = apply_filters( 'tablepress_table_content_render_data', $this->table, $orig_table, $this->render_options );
317 + }
318 +
319 + /**
262 320 * Generate the HTML output of the table.
263 321 *
264 322 * @since 1.0.0
265 323 */
266 - protected function _render_table() {
324 + protected function _render_table(): void {
267 325 $num_rows = count( $this->table['data'] );
268 326 $num_columns = ( $num_rows > 0 ) ? count( $this->table['data'][0] ) : 0;
269 327
270 328 // Check if there are rows and columns in the table (might not be the case after removing hidden rows/columns!).
271 329 if ( 0 === $num_rows || 0 === $num_columns ) {
330 + /* translators: %s: Table ID */
272 331 $this->output = sprintf( __( '<!-- The table with the ID %s is empty! -->', 'tablepress' ), $this->table['id'] );
273 332 return;
274 333 }
275 334
@@ -277,14 +336,14 @@
277 336 $this->rowspan = array_fill( 0, $num_columns, 1 );
278 337 $this->colspan = array_fill( 0, $num_rows, 1 );
279 338
280 339 /**
281 - * Filter the trigger keywords for "colspan" and "rowspan"
340 + * Filters the trigger keywords for "colspan" and "rowspan"
282 341 *
283 342 * @since 1.0.0
284 343 *
285 - * @param array $span_trigger The trigger keywords for combining table cells.
286 - * @param string $table_id The current table ID.
344 + * @param array<string, string> $span_trigger The trigger keywords for combining table cells.
345 + * @param string $table_id The current table ID.
287 346 */
288 347 $this->span_trigger = apply_filters( 'tablepress_span_trigger_keywords', $this->span_trigger, $this->table['id'] );
289 348
290 349 // Explode from string to array.
@@ -295,9 +354,9 @@
295 354 $output = '';
296 355
297 356 if ( $this->render_options['print_name'] ) {
298 357 /**
299 - * Filter the HTML tag that wraps the printed table name.
358 + * Filters the HTML tag that wraps the printed table name.
300 359 *
301 360 * @since 1.0.0
302 361 *
303 362 * @param string $tag The HTML tag around the table name. Default h2.
@@ -309,9 +368,9 @@
309 368 if ( ! empty( $this->render_options['html_id'] ) ) {
310 369 $name_attributes['id'] = "{$this->render_options['html_id']}-name";
311 370 }
312 371 /**
313 - * Filter the class attribute for the printed table name.
372 + * Filters the class attribute for the printed table name.
314 373 *
315 374 * @since 1.0.0
316 375 * @deprecated 1.13.0 Use {@see 'tablepress_table_name_tag_attributes'} instead.
317 376 *
@@ -319,15 +378,15 @@
319 378 * @param string $table_id The current table ID.
320 379 */
321 380 $name_attributes['class'] = apply_filters_deprecated( 'tablepress_print_name_css_class', array( "tablepress-table-name tablepress-table-name-id-{$this->table['id']}", $this->table['id'] ), 'TablePress 1.13.0', 'tablepress_table_name_tag_attributes' );
322 381 /**
323 - * Filter the attributes for the table name (HTML h2 element, by default).
382 + * Filters the attributes for the table name (HTML h2 element, by default).
324 383 *
325 384 * @since 1.13.0
326 385 *
327 - * @param array $name_attributes The attributes for the table name element.
328 - * @param array $table The current table.
329 - * @param array $render_options The render options for the table.
386 + * @param array<string, string> $name_attributes The attributes for the table name element.
387 + * @param array<string, mixed> $table The current table.
388 + * @param array<string, mixed> $render_options The render options for the table.
330 389 */
331 390 $name_attributes = apply_filters( 'tablepress_table_name_tag_attributes', $name_attributes, $this->table, $this->render_options );
332 391 $name_attributes = $this->_attributes_array_to_string( $name_attributes );
333 392
@@ -334,9 +393,9 @@
334 393 $print_name_html = "<{$name_html_tag}{$name_attributes}>" . $this->safe_output( $this->table['name'] ) . "</{$name_html_tag}>\n";
335 394 }
336 395 if ( $this->render_options['print_description'] ) {
337 396 /**
338 - * Filter the HTML tag that wraps the printed table description.
397 + * Filters the HTML tag that wraps the printed table description.
339 398 *
340 399 * @since 1.0.0
341 400 *
342 401 * @param string $tag The HTML tag around the table description. Default span.
@@ -348,9 +407,9 @@
348 407 if ( ! empty( $this->render_options['html_id'] ) ) {
349 408 $description_attributes['id'] = "{$this->render_options['html_id']}-description";
350 409 }
351 410 /**
352 - * Filter the class attribute for the printed table description.
411 + * Filters the class attribute for the printed table description.
353 412 *
354 413 * @since 1.0.0
355 414 * @deprecated 1.13.0 Use {@see 'tablepress_table_description_tag_attributes'} instead.
356 415 *
@@ -358,15 +417,15 @@
358 417 * @param string $table_id The current table ID.
359 418 */
360 419 $description_attributes['class'] = apply_filters_deprecated( 'tablepress_print_description_css_class', array( "tablepress-table-description tablepress-table-description-id-{$this->table['id']}", $this->table['id'] ), 'TablePress 1.13.0', 'tablepress_table_description_tag_attributes' );
361 420 /**
362 - * Filter the attributes for the table description (HTML span element, by default).
421 + * Filters the attributes for the table description (HTML span element, by default).
363 422 *
364 423 * @since 1.13.0
365 424 *
366 - * @param array $description_attributes The attributes for the table description element.
367 - * @param array $table The current table.
368 - * @param array $render_options The render options for the table.
425 + * @param array<string, string> $description_attributes The attributes for the table description element.
426 + * @param array<string, mixed> $table The current table.
427 + * @param array<string, mixed> $render_options The render options for the table.
369 428 */
370 429 $description_attributes = apply_filters( 'tablepress_table_description_tag_attributes', $description_attributes, $this->table, $this->render_options );
371 430 $description_attributes = $this->_attributes_array_to_string( $description_attributes );
372 431
@@ -379,50 +438,56 @@
379 438 if ( $this->render_options['print_description'] && 'above' === $this->render_options['print_description_position'] ) {
380 439 $output .= $print_description_html;
381 440 }
382 441
383 - // Deactivate nl2br() for this render process, if "convert_line_breaks" Shortcode parameter is set to false.
384 - if ( ! $this->render_options['convert_line_breaks'] ) {
385 - add_filter( 'tablepress_apply_nl2br', '__return_false', 9 ); // Priority 9, so that this filter can easily be overwritten at the default priority.
386 - }
387 -
388 - $thead = '';
389 - $tfoot = '';
442 + $thead = array();
443 + $tfoot = array();
390 444 $tbody = array();
391 445
392 446 $this->last_row_idx = $num_rows - 1;
393 447 $this->last_column_idx = $num_columns - 1;
448 +
394 449 // Loop through rows in reversed order, to search for rowspan trigger keyword.
395 - for ( $row_idx = $this->last_row_idx; $row_idx >= 0; $row_idx-- ) {
396 - // Last row, need to check for footer (but only if at least two rows).
397 - if ( $this->last_row_idx === $row_idx && $this->render_options['table_foot'] && $num_rows > 1 ) {
398 - $tfoot = $this->_render_row( $row_idx, 'th' );
399 - continue;
450 + $row_idx = $this->last_row_idx;
451 +
452 + // Render the table footer rows, if there is at least one extra row.
453 + if ( $this->render_options['table_foot'] > 0 && $num_rows >= $this->render_options['table_head'] + $this->render_options['table_foot'] ) {
454 + $last_tbody_idx = $this->last_row_idx - $this->render_options['table_foot'];
455 + while ( $row_idx > $last_tbody_idx ) {
456 + $tfoot[] = $this->_render_row( $row_idx, 'th' );
457 + --$row_idx;
400 458 }
401 - // First row, need to check for head (but only if at least two rows).
402 - if ( 0 === $row_idx && $this->render_options['table_head'] && $num_rows > 1 ) {
403 - $thead = $this->_render_row( $row_idx, 'th' );
404 - continue;
405 - }
406 - // Neither first nor last row (with respective head/foot enabled), so render as body row.
459 + // Reverse rows because we looped through the rows in reverse order.
460 + $tfoot = array_reverse( $tfoot );
461 + }
462 +
463 + // Render the table body rows.
464 + $last_thead_idx = $this->render_options['table_head'] - 1;
465 + while ( $row_idx > $last_thead_idx ) {
407 466 $tbody[] = $this->_render_row( $row_idx, 'td' );
467 + --$row_idx;
408 468 }
469 + // Reverse rows because we looped through the rows in reverse order.
470 + $tbody = array_reverse( $tbody );
409 471
410 - // Re-instate nl2br() behavior after this render process, if "convert_line_breaks" Shortcode parameter is set to false.
411 - if ( ! $this->render_options['convert_line_breaks'] ) {
412 - remove_filter( 'tablepress_apply_nl2br', '__return_false', 9 ); // Priority 9, so that this filter can easily be overwritten at the default priority.
472 + // Render the table header rows, if rows are left.
473 + while ( $row_idx > -1 ) {
474 + $thead[] = $this->_render_row( $row_idx, 'th' );
475 + --$row_idx;
413 476 }
477 + // Reverse rows because we looped through the rows in reverse order.
478 + $thead = array_reverse( $thead );
414 479
415 480 // <caption> tag.
416 481 /**
417 - * Filter the content for the HTML caption element of the table.
482 + * Filters the content for the HTML caption element of the table.
418 483 *
419 484 * If the "Edit" link for a table is shown, it is also added to the caption element.
420 485 *
421 486 * @since 1.0.0
422 487 *
423 - * @param string $caption The content for the HTML caption element of the table. Default empty.
424 - * @param array $table The current table.
488 + * @param string $caption The content for the HTML caption element of the table. Default empty.
489 + * @param array<string, mixed> $table The current table.
425 490 */
426 491 $caption = apply_filters( 'tablepress_print_caption_text', '', $this->table );
427 492 $caption_style = '';
428 493 $caption_class = '';
@@ -427,9 +492,9 @@
427 492 $caption_style = '';
428 493 $caption_class = '';
429 494 if ( ! empty( $caption ) ) {
430 495 /**
431 - * Filter the class attribute for the HTML caption element of the table.
496 + * Filters the class attribute for the HTML caption element of the table.
432 497 *
433 498 * @since 1.0.0
434 499 *
435 500 * @param string $class The class attribute for the HTML caption element of the table.
@@ -452,9 +517,9 @@
452 517
453 518 // <colgroup> tag.
454 519 $colgroup = '';
455 520 /**
456 - * Filter whether the HTML colgroup tag shall be added to the table output.
521 + * Filters whether the HTML colgroup tag shall be added to the table output.
457 522 *
458 523 * @since 1.0.0
459 524 *
460 525 * @param bool $print Whether the colgroup element shall be printed.
@@ -463,9 +528,9 @@
463 528 if ( apply_filters( 'tablepress_print_colgroup_tag', false, $this->table['id'] ) ) {
464 529 for ( $col_idx = 0; $col_idx < $num_columns; $col_idx++ ) {
465 530 $attributes = ' class="colgroup-column-' . ( $col_idx + 1 ) . ' "';
466 531 /**
467 - * Filter the attributes of the HTML col tags in the HTML colgroup tag.
532 + * Filters the attributes of the HTML col tags in the HTML colgroup tag.
468 533 *
469 534 * @since 1.0.0
470 535 *
471 536 * @param string $attributes The attributes in the col element.
@@ -472,9 +537,9 @@
472 537 * @param string $table_id The current table ID.
473 538 * @param int $col_idx The number of the column.
474 539 */
475 540 $attributes = apply_filters( 'tablepress_colgroup_tag_attributes', $attributes, $this->table['id'], $col_idx + 1 );
476 - $colgroup .= "\t<col{$attributes}/>\n";
541 + $colgroup .= "\t<col{$attributes} />\n";
477 542 }
478 543 }
479 544 if ( ! empty( $colgroup ) ) {
480 545 $colgroup = "<colgroup>\n{$colgroup}</colgroup>\n";
@@ -479,18 +544,36 @@
479 544 if ( ! empty( $colgroup ) ) {
480 545 $colgroup = "<colgroup>\n{$colgroup}</colgroup>\n";
481 546 }
482 547
483 - // <thead>, <tfoot>, and <tbody> tags.
548 + /*
549 + * <thead>, <tfoot>, and <tbody> tags.
550 + */
551 +
484 552 if ( ! empty( $thead ) ) {
485 - $thead = "<thead>\n{$thead}</thead>\n";
553 + $thead = "<thead>\n" . implode( '', $thead ) . "</thead>\n";
554 + } else {
555 + $thead = '';
486 556 }
557 +
487 558 if ( ! empty( $tfoot ) ) {
488 - $tfoot = "<tfoot>\n{$tfoot}</tfoot>\n";
559 + $tfoot = "<tfoot>\n" . implode( '', $tfoot ) . "</tfoot>\n";
560 + } else {
561 + $tfoot = '';
489 562 }
490 - $tbody_class = ( $this->render_options['row_hover'] ) ? ' class="row-hover"' : '';
491 - // Reverse rows because we looped through the rows in reverse order.
492 - $tbody = array_reverse( $tbody );
563 +
564 + $tbody_classes = array();
565 + if ( $this->render_options['alternating_row_colors'] ) {
566 + $tbody_classes[] = 'row-striping';
567 + }
568 + if ( $this->render_options['row_hover'] ) {
569 + $tbody_classes[] = 'row-hover';
570 + }
571 + $tbody_class = implode( ' ', $tbody_classes );
572 + if ( '' !== $tbody_class ) {
573 + $tbody_class = ' class="' . esc_attr( $tbody_class ) . '"';
574 + }
575 +
493 576 $tbody = "<tbody{$tbody_class}>\n" . implode( '', $tbody ) . "</tbody>\n";
494 577
495 578 // Attributes for the table (HTML table element).
496 579 $table_attributes = array();
@@ -500,16 +583,23 @@
500 583 $table_attributes['id'] = $this->render_options['html_id'];
501 584 }
502 585
503 586 // "class" attribute.
504 - $css_classes = array( 'tablepress', "tablepress-id-{$this->table['id']}", $this->render_options['extra_css_classes'] );
587 + $css_classes = array(
588 + 'tablepress',
589 + "tablepress-id-{$this->table['id']}",
590 + $this->render_options['extra_css_classes'],
591 + );
592 + if ( $this->tbody_has_connected_cells ) {
593 + $css_classes[] = 'tbody-has-connected-cells';
594 + }
505 595 /**
506 - * Filter the CSS classes that are given to the HTML table element.
596 + * Filters the CSS classes that are given to the HTML table element.
507 597 *
508 598 * @since 1.0.0
509 599 *
510 - * @param array $css_classes The CSS classes for the table element.
511 - * @param string $table_id The current table ID.
600 + * @param string[] $css_classes The CSS classes for the table element.
601 + * @param string $table_id The current table ID.
512 602 */
513 603 $css_classes = apply_filters( 'tablepress_table_css_classes', $css_classes, $this->table['id'] );
514 604 // $css_classes might contain several classes in one array entry.
515 605 $css_classes = explode( ' ', implode( ' ', $css_classes ) );
@@ -514,10 +604,11 @@
514 604 // $css_classes might contain several classes in one array entry.
515 605 $css_classes = explode( ' ', implode( ' ', $css_classes ) );
516 606 $css_classes = array_map( array( 'TablePress', 'sanitize_css_class' ), $css_classes );
517 607 $css_classes = array_unique( $css_classes );
518 - $css_classes = trim( implode( ' ', $css_classes ) );
519 - if ( ! empty( $css_classes ) ) {
608 + $css_classes = array_filter( $css_classes ); // Remove empty entries.
609 + $css_classes = implode( ' ', $css_classes );
610 + if ( '' !== $css_classes ) {
520 611 $table_attributes['class'] = $css_classes;
521 612 }
522 613
523 614 // ARIA label attributes.
@@ -530,16 +621,16 @@
530 621
531 622 // "summary" attribute.
532 623 $summary = '';
533 624 /**
534 - * Filter the content for the summary attribute of the HTML table element.
625 + * Filters the content for the summary attribute of the HTML table element.
535 626 *
536 627 * The attribute is only added if it is not empty.
537 628 *
538 629 * @since 1.0.0
539 630 *
540 - * @param string $summary The content for the summary attribute of the table. Default empty.
541 - * @param array $table The current table.
631 + * @param string $summary The content for the summary attribute of the table. Default empty.
632 + * @param array<string, mixed> $table The current table.
542 633 */
543 634 $summary = apply_filters( 'tablepress_print_summary_attr', $summary, $this->table );
544 635 if ( ! empty( $summary ) ) {
545 636 $table_attributes['summary'] = esc_attr( $summary );
@@ -547,28 +638,42 @@
547 638
548 639 // Legacy support for attributes that are not encouraged in HTML5.
549 640 foreach ( array( 'cellspacing', 'cellpadding', 'border' ) as $attribute ) {
550 641 if ( false !== $this->render_options[ $attribute ] ) {
551 - $table_attributes[ $attribute ] = (int) $this->render_options[ $attribute ];
642 + $table_attributes[ $attribute ] = (string) absint( $this->render_options[ $attribute ] );
552 643 }
553 644 }
554 645
555 646 /**
556 - * Filter the attributes for the table (HTML table element).
647 + * Filters the attributes for the table (HTML table element).
557 648 *
558 649 * @since 1.4.0
559 650 *
560 - * @param array $table_attributes The attributes for the table element.
561 - * @param array $table The current table.
562 - * @param array $render_options The render options for the table.
651 + * @param array<string, string> $table_attributes The attributes for the table element.
652 + * @param array<string, mixed> $table The current table.
653 + * @param array<string, mixed> $render_options The render options for the table.
563 654 */
564 655 $table_attributes = apply_filters( 'tablepress_table_tag_attributes', $table_attributes, $this->table, $this->render_options );
565 656 $table_attributes = $this->_attributes_array_to_string( $table_attributes );
566 657
567 - $output .= "\n<table{$table_attributes}>\n";
568 - $output .= $caption . $colgroup . $thead . $tbody . $tfoot;
569 - $output .= "</table>\n";
658 + $table_html = "<table{$table_attributes}>\n";
659 + $table_html .= $caption . $colgroup . $thead . $tbody . $tfoot;
660 + $table_html .= '</table>';
570 661
662 + /**
663 + * Filters the generated HTML code for the table, without HTML elements around it.
664 + *
665 + * @since 2.4.0
666 + *
667 + * @param string $output The generated HTML for the table, without HTML elements around it.
668 + * @param array<string, mixed> $table The current table.
669 + * @param array<string, mixed> $render_options The render options for the table, without HTML elements around it.
670 + */
671 + $table_html = apply_filters( 'tablepress_table_html', $table_html, $this->table, $this->render_options );
672 +
673 + $output .= "\n{$table_html}\n";
674 + unset( $table_html ); // Unset the potentially large variable to free up memory.
675 +
571 676 // name/description below table (HTML already generated above).
572 677 if ( $this->render_options['print_name'] && 'below' === $this->render_options['print_name_position'] ) {
573 678 $output .= $print_name_html;
574 679 }
@@ -576,15 +681,15 @@
576 681 $output .= $print_description_html;
577 682 }
578 683
579 684 /**
580 - * Filter the generated HTML code for table.
685 + * Filters the generated HTML code for the table and HTML elements around it.
581 686 *
582 687 * @since 1.0.0
583 688 *
584 - * @param string $output The generated HTML for the table.
585 - * @param array $table The current table.
586 - * @param array $render_options The render options for the table.
689 + * @param string $output The generated HTML for the table and HTML elements around it.
690 + * @param array<string, mixed> $table The current table.
691 + * @param array<string, mixed> $render_options The render options for the table and HTML elements around it.
587 692 */
588 693 $this->output = apply_filters( 'tablepress_table_output', $output, $this->table, $this->render_options );
589 694 }
590 695
@@ -596,40 +701,22 @@
596 701 * @param int $row_idx Index of the row to be rendered.
597 702 * @param string $tag HTML tag to use for the cells (td or th).
598 703 * @return string HTML for the row.
599 704 */
600 - protected function _render_row( $row_idx, $tag ) {
705 + protected function _render_row( int $row_idx, string $tag ): string {
601 706 $row_cells = array();
602 707 // Loop through cells in reversed order, to search for colspan or rowspan trigger words.
603 708 for ( $col_idx = $this->last_column_idx; $col_idx >= 0; $col_idx-- ) {
604 709 $cell_content = $this->table['data'][ $row_idx ][ $col_idx ];
605 710
606 - // Print formulas that are escaped with '= (like in Excel) as text.
607 - if ( "'=" === substr( $cell_content, 0, 2 ) ) {
608 - $cell_content = substr( $cell_content, 1 );
609 - }
610 - $cell_content = do_shortcode( $this->safe_output( $cell_content ) );
611 - /**
612 - * Filter the content of a single cell.
613 - *
614 - * Filter the content of a single cell, after formulas have been evaluated, the output has been sanitized, and Shortcodes have been evaluated.
615 - *
616 - * @since 1.0.0
617 - *
618 - * @param string $cell_content The cell content.
619 - * @param string $table_id The current table ID.
620 - * @param int $row_idx The row number of the cell.
621 - * @param int $col_idx The column number of the cell.
622 - */
623 - $cell_content = apply_filters( 'tablepress_cell_content', $cell_content, $this->table['id'], $row_idx + 1, $col_idx + 1 );
624 -
625 711 if ( $this->span_trigger['rowspan'] === $cell_content ) { // There will be a rowspan.
626 - // Check for #rowspan# in first row, which doesn't make sense.
627 - if ( ( $row_idx > 1 && $row_idx < $this->last_row_idx )
628 - || ( 1 === $row_idx && ! $this->render_options['table_head'] ) // No rowspan into table head.
629 - || ( $this->last_row_idx === $row_idx && ! $this->render_options['table_foot'] ) ) { // No rowspan out of table foot.
712 + if ( ! (
713 + ( 0 === $row_idx ) // No rowspan inside first row.
714 + || ( $this->render_options['table_head'] === $row_idx ) // No rowspan into table head.
715 + || ( $this->last_row_idx - $this->render_options['table_foot'] + 1 === $row_idx ) // No rowspan out of table foot.
716 + ) ) {
630 717 // Increase counter for rowspan in this column.
631 - $this->rowspan[ $col_idx ]++;
718 + ++$this->rowspan[ $col_idx ];
632 719 // Reset counter for colspan in this row, combined col- and rowspan might be happening.
633 720 $this->colspan[ $row_idx ] = 1;
634 721 continue;
635 722 }
@@ -635,13 +722,14 @@
635 722 }
636 723 // Invalid rowspan, so we set cell content from #rowspan# to empty.
637 724 $cell_content = '';
638 725 } elseif ( $this->span_trigger['colspan'] === $cell_content ) { // There will be a colspan.
639 - // Check for #colspan# in first column, which doesn't make sense.
640 - if ( $col_idx > 1
641 - || ( 1 === $col_idx && ! $this->render_options['first_column_th'] ) ) { // No colspan into first column head.
726 + if ( ! (
727 + ( 0 === $col_idx ) // No colspan inside first column.
728 + || ( 1 === $col_idx && $this->render_options['first_column_th'] ) // No colspan into first column head.
729 + ) ) {
642 730 // Increase counter for colspan in this row.
643 - $this->colspan[ $row_idx ]++;
731 + ++$this->colspan[ $row_idx ];
644 732 // Reset counter for rowspan in this column, combined col- and rowspan might be happening.
645 733 $this->rowspan[ $col_idx ] = 1;
646 734 continue;
647 735 }
@@ -647,22 +735,20 @@
647 735 }
648 736 // Invalid colspan, so we set cell content from #colspan# to empty.
649 737 $cell_content = '';
650 738 } elseif ( $this->span_trigger['span'] === $cell_content ) { // There will be a combined col- and rowspan.
651 - // Check for #span# in first column or first or last row, which is not always possible.
652 - if ( ( $row_idx > 1 && $row_idx < $this->last_row_idx && $col_idx > 1 )
653 - // We are in first, second, or last row or in the first or second column, so more checks are necessary.
654 - || ( ( 1 === $row_idx && ! $this->render_options['table_head'] ) // No rowspan into table head.
655 - && ( $col_idx > 1 || ( 1 === $col_idx && ! $this->render_options['first_column_th'] ) ) ) // And no colspan into first column head.
656 - || ( ( $this->last_row_idx === $row_idx && ! $this->render_options['table_foot'] ) // No rowspan out of table foot.
657 - && ( $col_idx > 1 || ( 1 === $col_idx && ! $this->render_options['first_column_th'] ) ) ) ) { // And no colspan into first column head.
739 + if ( ! (
740 + ( 0 === $row_idx ) // No rowspan inside first row.
741 + || ( $this->render_options['table_head'] === $row_idx ) // No rowspan into table head.
742 + || ( $this->last_row_idx - $this->render_options['table_foot'] + 1 === $row_idx ) // No rowspan out of table foot.
743 + ) && ! (
744 + ( 0 === $col_idx ) // No colspan inside first column.
745 + || ( 1 === $col_idx && $this->render_options['first_column_th'] ) // No colspan into first column head.
746 + ) ) {
658 747 continue;
659 748 }
660 749 // Invalid span, so we set cell content from #span# to empty.
661 750 $cell_content = '';
662 - } elseif ( '' === $cell_content && 0 === $row_idx && $this->render_options['table_head'] ) {
663 - // Make empty cells have a space in the table head, to give sorting arrows the correct position in IE9.
664 - $cell_content = '&nbsp;';
665 751 }
666 752
667 753 // Attributes for the table cell (HTML td or th element).
668 754 $tag_attributes = array();
@@ -668,18 +754,26 @@
668 754 $tag_attributes = array();
669 755
670 756 // "colspan" and "rowspan" attributes.
671 757 if ( $this->colspan[ $row_idx ] > 1 ) { // We have colspaned cells.
672 - $tag_attributes['colspan'] = $this->colspan[ $row_idx ];
758 + $tag_attributes['colspan'] = (string) $this->colspan[ $row_idx ];
759 + if ( ! $this->tbody_has_connected_cells && $row_idx > $this->render_options['table_head'] - 1 && $row_idx < $this->last_row_idx - $this->render_options['table_foot'] + 1 ) {
760 + // Set flag that there are connected cells in the tbody.
761 + $this->tbody_has_connected_cells = true;
762 + }
673 763 }
674 764 if ( $this->rowspan[ $col_idx ] > 1 ) { // We have rowspaned cells.
675 - $tag_attributes['rowspan'] = $this->rowspan[ $col_idx ];
765 + $tag_attributes['rowspan'] = (string) $this->rowspan[ $col_idx ];
766 + if ( ! $this->tbody_has_connected_cells && $row_idx > $this->render_options['table_head'] - 1 && $row_idx < $this->last_row_idx - $this->render_options['table_foot'] + 1 ) {
767 + // Set flag that there are connected cells in the tbody.
768 + $this->tbody_has_connected_cells = true;
769 + }
676 770 }
677 771
678 772 // "class" attribute.
679 773 $cell_class = 'column-' . ( $col_idx + 1 );
680 774 /**
681 - * Filter the CSS classes that are given to a single cell (HTML td element) of a table.
775 + * Filters the CSS classes that are given to a single cell (HTML td element) of a table.
682 776 *
683 777 * @since 1.0.0
684 778 *
685 779 * @param string $cell_class The CSS classes for the cell.
@@ -700,28 +794,32 @@
700 794 $tag_attributes['style'] = 'width:' . preg_replace( '#[^0-9a-z.%]#', '', $this->render_options['column_widths'][ $col_idx ] ) . ';';
701 795 }
702 796
703 797 /**
704 - * Filter the attributes for the table cell (HTML td or th element).
798 + * Filters the attributes for the table cell (HTML td or th element).
705 799 *
706 800 * @since 1.4.0
707 801 *
708 - * @param array $tag_attributes The attributes for the td or th element.
709 - * @param string $table_id The current table ID.
710 - * @param string $cell_content The cell content.
711 - * @param int $row_idx The row number of the cell.
712 - * @param int $col_idx The column number of the cell.
713 - * @param int $colspan_row The number of combined columns for this cell.
714 - * @param int $rowspan_col The number of combined rows for this cell.
802 + * @param array<string, string> $tag_attributes The attributes for the td or th element.
803 + * @param string $table_id The current table ID.
804 + * @param string $cell_content The cell content.
805 + * @param int $row_idx The row number of the cell.
806 + * @param int $col_idx The column number of the cell.
807 + * @param int $colspan_row The number of combined columns for this cell.
808 + * @param int $rowspan_col The number of combined rows for this cell.
715 809 */
716 810 $tag_attributes = apply_filters( 'tablepress_cell_tag_attributes', $tag_attributes, $this->table['id'], $cell_content, $row_idx + 1, $col_idx + 1, $this->colspan[ $row_idx ], $this->rowspan[ $col_idx ] );
717 811 $tag_attributes = $this->_attributes_array_to_string( $tag_attributes );
718 812
719 - if ( $this->render_options['first_column_th'] && 0 === $col_idx ) {
720 - $tag = 'th';
813 + if ( '' === $cell_content ) {
814 + $cell_tag = 'td'; // For accessibility, empty cells should use `td` and not `th` tags.
815 + } elseif ( $this->render_options['first_column_th'] && 0 === $col_idx ) {
816 + $cell_tag = 'th'; // Non-empty cells in the first column should use `th` tags, if enabled.
817 + } else {
818 + $cell_tag = $tag; // Otherwise, use the tag that was passed in as the default for the row.
721 819 }
722 820
723 - $row_cells[] = "<{$tag}{$tag_attributes}>{$cell_content}</{$tag}>";
821 + $row_cells[] = "<{$cell_tag}{$tag_attributes}>{$cell_content}</{$cell_tag}>";
724 822 $this->colspan[ $row_idx ] = 1; // Reset.
725 823 $this->rowspan[ $col_idx ] = 1; // Reset.
726 824 }
727 825
@@ -729,21 +827,18 @@
729 827 $tr_attributes = array();
730 828
731 829 // "class" attribute.
732 830 $row_classes = 'row-' . ( $row_idx + 1 );
733 - if ( $this->render_options['alternating_row_colors'] ) {
734 - $row_classes .= ( 1 === ( $row_idx % 2 ) ) ? ' even' : ' odd';
735 - }
736 831 /**
737 - * Filter the CSS classes that are given to a row (HTML tr element) of a table.
832 + * Filters the CSS classes that are given to a row (HTML tr element) of a table.
738 833 *
739 834 * @since 1.0.0
740 835 *
741 - * @param string $row_classes The CSS classes for the row.
742 - * @param string $table_id The current table ID.
743 - * @param array $row_cells The HTML code for the cells of the row.
744 - * @param int $row_idx The row number.
745 - * @param array $row_data The content of the cells of the row.
836 + * @param string $row_classes The CSS classes for the row.
837 + * @param string $table_id The current table ID.
838 + * @param string[] $row_cells The HTML code for the cells of the row.
839 + * @param int $row_idx The row number.
840 + * @param string[] $row_data The content of the cells of the row.
746 841 */
747 842 $row_classes = apply_filters( 'tablepress_row_css_class', $row_classes, $this->table['id'], $row_cells, $row_idx + 1, $this->table['data'][ $row_idx ] );
748 843 if ( ! empty( $row_classes ) ) {
749 844 $tr_attributes['class'] = $row_classes;
@@ -749,16 +844,16 @@
749 844 $tr_attributes['class'] = $row_classes;
750 845 }
751 846
752 847 /**
753 - * Filter the attributes for the table row (HTML tr element).
848 + * Filters the attributes for the table row (HTML tr element).
754 849 *
755 850 * @since 1.4.0
756 851 *
757 - * @param array $tr_attributes The attributes for the tr element.
758 - * @param string $table_id The current table ID.
759 - * @param int $row_idx The row number.
760 - * @param array $row_data The content of the cells of the row.
852 + * @param array<string, mixed> $tr_attributes The attributes for the tr element.
853 + * @param string $table_id The current table ID.
854 + * @param int $row_idx The row number.
855 + * @param string[] $row_data The content of the cells of the row.
761 856 */
762 857 $tr_attributes = apply_filters( 'tablepress_row_tag_attributes', $tr_attributes, $this->table['id'], $row_idx + 1, $this->table['data'][ $row_idx ] );
763 858 $tr_attributes = $this->_attributes_array_to_string( $tr_attributes );
764 859
@@ -771,12 +866,12 @@
771 866 * Convert an array of HTML tag attributes to a string.
772 867 *
773 868 * @since 1.4.0
774 869 *
775 - * @param array $attributes Attributes for the HTML tag in the array keys, and their values in the array values.
870 + * @param array<string, string> $attributes Attributes for the HTML tag in the array keys, and their values in the array values.
776 871 * @return string The attributes as a string for usage in a HTML element.
777 872 */
778 - protected function _attributes_array_to_string( array $attributes ) {
873 + protected function _attributes_array_to_string( array $attributes ): string {
779 874 $attributes_string = '';
780 875 foreach ( $attributes as $attribute => $value ) {
781 876 $attributes_string .= " {$attribute}=\"{$value}\"";
782 877 }
@@ -785,23 +880,21 @@
785 880
786 881 /**
787 882 * Possibly replace certain HTML entities and replace line breaks with HTML.
788 883 *
789 - * @TODO: Find a better solution than this function, e.g. something like wpautop().
790 - *
791 884 * @since 1.0.0
792 885 *
793 - * @param string $string The string to process.
886 + * @param string $text The string to process.
794 887 * @return string Processed string for output.
795 888 */
796 - protected function safe_output( $string ) {
889 + protected function safe_output( string $text ): string {
797 890 /*
798 891 * Replace any & with &amp; that is not already an encoded entity (from function htmlentities2 in WP 2.8).
799 892 * A complete htmlentities2() or htmlspecialchars() would encode <HTML> tags, which we don't want.
800 893 */
801 - $string = preg_replace( '/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,4};)/', '&amp;', $string );
894 + $text = (string) preg_replace( '/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,4};)/', '&amp;', $text );
802 895 /**
803 - * Filter whether line breaks in the cell content shall be replaced with HTML br tags.
896 + * Filters whether line breaks in the cell content shall be replaced with HTML br tags.
804 897 *
805 898 * @since 1.0.0
806 899 *
807 900 * @param bool $replace Whether to replace line breaks with HTML br tags. Default true.
@@ -807,11 +900,11 @@
807 900 * @param bool $replace Whether to replace line breaks with HTML br tags. Default true.
808 901 * @param string $table_id The current table ID.
809 902 */
810 903 if ( apply_filters( 'tablepress_apply_nl2br', true, $this->table['id'] ) ) {
811 - $string = nl2br( $string );
904 + $text = nl2br( $text );
812 905 }
813 - return $string;
906 + return $text;
814 907 }
815 908
816 909 /**
817 910 * Get the default render options, null means: Use option from "Edit" screen.
@@ -817,46 +910,49 @@
817 910 * Get the default render options, null means: Use option from "Edit" screen.
818 911 *
819 912 * @since 1.0.0
820 913 *
821 - * @return array Default render options.
914 + * @return array<string, mixed> Default render options.
822 915 */
823 - public function get_default_render_options() {
916 + public function get_default_render_options(): array {
824 917 // Attention: Array keys have to be lowercase, otherwise they won't match the Shortcode attributes, which will be passed in lowercase by WP.
825 918 return array(
826 - 'id' => '',
827 - 'column_widths' => '',
828 919 'alternating_row_colors' => null,
829 - 'row_hover' => null,
830 - 'table_head' => null,
831 - 'table_foot' => null,
832 - 'first_column_th' => false,
833 - 'print_name' => null,
834 - 'print_name_position' => null,
835 - 'print_description' => null,
836 - 'print_description_position' => null,
920 + 'block_preview' => false,
921 + 'border' => false,
837 922 'cache_table_output' => true,
923 + 'cellpadding' => false,
924 + 'cellspacing' => false,
925 + 'column_widths' => '',
838 926 'convert_line_breaks' => true,
839 - 'extra_css_classes' => null,
840 - 'use_datatables' => null,
841 - 'datatables_sort' => null,
927 + 'datatables_custom_commands' => null,
928 + 'datatables_datetime' => '',
929 + 'datatables_filter' => null,
930 + 'datatables_info' => null,
931 + 'datatables_lengthchange' => null,
932 + 'datatables_locale' => get_locale(),
842 933 'datatables_paginate' => null,
843 934 'datatables_paginate_entries' => null,
844 - 'datatables_lengthchange' => null,
845 - 'datatables_filter' => null,
846 - 'datatables_info' => null,
847 935 'datatables_scrollx' => null,
848 936 'datatables_scrolly' => false,
849 - 'datatables_custom_commands' => null,
850 - 'datatables_locale' => get_locale(),
851 - 'show_rows' => '',
852 - 'show_columns' => '',
937 + 'datatables_sort' => null,
938 + 'evaluate_formulas' => true,
939 + 'extra_css_classes' => null,
940 + 'first_column_th' => false,
941 + 'hide_columns' => '',
853 942 'hide_rows' => '',
854 - 'hide_columns' => '',
855 - 'cellspacing' => false,
856 - 'cellpadding' => false,
857 - 'border' => false,
943 + 'id' => '',
944 + 'print_description' => null,
945 + 'print_description_position' => null,
946 + 'print_name' => null,
947 + 'print_name_position' => null,
948 + 'row_hover' => null,
858 949 'shortcode_debug' => false,
950 + 'show_columns' => '',
951 + 'show_rows' => '',
952 + 'table_foot' => null,
953 + 'table_head' => null,
954 + 'use_datatables' => null,
859 955 );
860 956 }
861 957
862 958 /**
@@ -865,90 +961,35 @@
865 961 * @since 1.0.0
866 962 *
867 963 * @return string CSS for the Preview iframe.
868 964 */
869 - public function get_preview_css() {
870 - if ( is_rtl() ) {
871 - $rtl = "\ndirection: rtl;";
872 - $rtl_align = 'right';
873 - } else {
874 - $rtl = '';
875 - $rtl_align = 'left';
965 + public function get_preview_css(): string {
966 + $is_rtl = is_rtl();
967 + $tablepress_css = TablePress::load_class( 'TablePress_CSS', 'class-css.php', 'classes' );
968 + $default_css_minified = $tablepress_css->load_default_css_from_file( $is_rtl );
969 + if ( false === $default_css_minified ) {
970 + $default_css_minified = '';
876 971 }
972 +
973 + $rtl_direction = $is_rtl ? "\ndirection: rtl;" : '';
974 +
877 975 return <<<CSS
878 -<style type="text/css">
879 -/* iframe */
880 -body {
881 - margin: 10px;
882 - font-family: sans-serif;{$rtl}
883 -}
884 -/* Inline Shortcodes, in texts */
885 -.table-shortcode-inline {
886 - background: transparent;
887 - border: none;
888 - color: #333333;
889 - width: 110px;
890 - margin: 0;
891 - padding: 0;
892 - -webkit-box-shadow: none;
893 - box-shadow: none;
894 - text-align: center;
895 - font-weight: bold;
896 - font-size: 100%;
897 -}
898 -.table-shortcode {
899 - cursor: text;
900 -}
901 -/* Default table styling */
902 -.tablepress {
903 - border-collapse: collapse;
904 - border-spacing: 0;
905 - width: 100%;
906 - margin-bottom: 1em;
907 - border: none;
908 -}
909 -.tablepress td,
910 -.tablepress th {
911 - padding: 8px;
912 - border: none;
913 - background: none;
914 - text-align: {$rtl_align};
915 -}
916 -.tablepress tbody td {
917 - vertical-align: top;
918 -}
919 -.tablepress tbody tr td,
920 -.tablepress tfoot tr th {
921 - border-top: 1px solid #dddddd;
922 -}
923 -.tablepress tbody tr:first-child td {
924 - border-top: 0;
925 -}
926 -.tablepress thead tr th {
927 - border-bottom: 1px solid #dddddd;
928 -}
929 -.tablepress thead th,
930 -.tablepress tfoot th {
931 - background-color: #d9edf7;
932 - font-weight: bold;
933 - vertical-align: middle;
934 -}
935 -.tablepress tbody tr.odd td {
936 - background-color: #f9f9f9;
937 -}
938 -.tablepress tbody tr.even td {
939 - background-color: #ffffff;
940 -}
941 -.tablepress .row-hover tr:hover td {
942 - background-color: #f3f3f3;
943 -}
944 -.tablepress img {
945 - margin: 0;
946 - padding: 0;
947 - border: none;
948 - max-width: none;
949 -}
950 -</style>
951 -CSS;
976 + <style>
977 + /* iframe */
978 + body {
979 + margin: 10px;
980 + font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;{$rtl_direction}
981 + }
982 + p {
983 + font-size: 13px;
984 + }
985 + .preview {
986 + overflow-x: auto;
987 + overflow-y: hidden;
988 + scroll-behavior: smooth;
989 + }
990 + {$default_css_minified}
991 + </style>
992 + CSS;
952 993 }
953 994
954 995 } // class TablePress_Render