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 +226 -185 2.1.7 → 3.4 View file →
@@ -7,8 +7,10 @@
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 /**
@@ -24,25 +26,25 @@
24 26 /**
25 27 * Table data that is rendered.
26 28 *
27 29 * @since 1.0.0
28 - * @var array
30 + * @var array<string, mixed>
29 31 */
30 - protected $table = array();
32 + protected array $table = array();
31 33
32 34 /**
33 35 * Table options that influence the output result.
34 36 *
35 37 * @since 1.0.0
36 - * @var array
38 + * @var array<string, mixed>
37 39 */
38 - protected $render_options = array();
40 + protected array $render_options = array();
39 41
40 42 /**
41 43 * Rendered HTML code of the table or PHP array.
42 44 *
43 45 * @since 1.0.0
44 - * @var string|array
46 + * @var string|array<int, array<int, string>>
45 47 */
46 48 protected $output;
47 49
48 50 /**
@@ -48,11 +50,11 @@
48 50 /**
49 51 * Trigger words for colspan, rowspan, or the combination of both.
50 52 *
51 53 * @since 1.0.0
52 - * @var array
54 + * @var array<string, string>
53 55 */
54 - protected $span_trigger = array(
56 + protected array $span_trigger = array(
55 57 'colspan' => '#colspan#',
56 58 'rowspan' => '#rowspan#',
57 59 'span' => '#span#',
58 60 );
@@ -60,35 +62,40 @@
60 62 /**
61 63 * Buffer to store the counts of rowspan per column, initialized in _render_table().
62 64 *
63 65 * @since 1.0.0
64 - * @var array
66 + * @var int[]
65 67 */
66 - protected $rowspan = array();
68 + protected array $rowspan = array();
67 69
68 70 /**
69 71 * Buffer to store the counts of colspan per row, initialized in _render_table().
70 72 *
71 73 * @since 1.0.0
72 - * @var array
74 + * @var int[]
73 75 */
74 - protected $colspan = array();
76 + protected array $colspan = array();
75 77
76 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 + /**
77 86 * Index of the last row of the visible data in the table, set in _render_table().
78 87 *
79 88 * @since 1.0.0
80 - * @var int
81 89 */
82 - protected $last_row_idx;
90 + protected int $last_row_idx;
83 91
84 92 /**
85 93 * Index of the last column of the visible data in the table, set in _render_table().
86 94 *
87 95 * @since 1.0.0
88 - * @var int
89 96 */
90 - protected $last_column_idx;
97 + protected int $last_column_idx;
91 98
92 99 /**
93 100 * Class constructor.
94 101 *
@@ -102,12 +109,12 @@
102 109 * Set the table (data, options, visibility, ...) that is to be rendered.
103 110 *
104 111 * @since 1.0.0
105 112 *
106 - * @param array $table Table to be rendered.
107 - * @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.
108 115 */
109 - public function set_input( array $table, array $render_options ) {
116 + public function set_input( array $table, array $render_options ): void {
110 117 $this->table = $table;
111 118 $this->render_options = $render_options;
112 119 /**
113 120 * Filters the table before the render process.
@@ -113,10 +120,10 @@
113 120 * Filters the table before the render process.
114 121 *
115 122 * @since 1.0.0
116 123 *
117 - * @param array $table The table.
118 - * @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.
119 126 */
120 127 $this->table = apply_filters( 'tablepress_table_raw_render_data', $this->table, $this->render_options );
121 128 }
122 129
@@ -126,11 +133,11 @@
126 133 * @since 1.0.0
127 134 * @since 2.0.0 Add the $format parameter.
128 135 *
129 136 * @param string $format Optional. Output format, 'html' (default) or 'array'.
130 - * @return string|array[] HTML code of the rendered table, or a PHP array, or an error message.
137 + * @return string|array<int, array<int, string>> HTML code of the rendered table, or a PHP array, or an error message.
131 138 */
132 - public function get_output( $format = 'html' ) {
139 + public function get_output( string $format = 'html' ) /* : string|array */ {
133 140 // Evaluate math expressions/formulas.
134 141 $this->_evaluate_table_data();
135 142 // Remove hidden rows and columns.
136 143 $this->_prepare_render_data();
@@ -162,9 +169,9 @@
162 169 * Loop through the table to evaluate math expressions/formulas.
163 170 *
164 171 * @since 1.0.0
165 172 */
166 - protected function _evaluate_table_data() {
173 + protected function _evaluate_table_data(): void {
167 174 $orig_table = $this->table;
168 175
169 176 if ( $this->render_options['evaluate_formulas'] ) {
170 177 $formula_evaluator = TablePress::load_class( 'TablePress_Evaluate', 'class-evaluate.php', 'classes' );
@@ -175,11 +182,11 @@
175 182 * Filters the table after evaluating formulas in the table.
176 183 *
177 184 * @since 1.0.0
178 185 *
179 - * @param array $table The table with evaluated formulas.
180 - * @param array $orig_table The table with unevaluated formulas.
181 - * @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.
182 189 */
183 190 $this->table = apply_filters( 'tablepress_table_evaluate_data', $this->table, $orig_table, $this->render_options );
184 191 }
185 192
@@ -187,13 +194,15 @@
187 194 * Remove all cells from the data set that shall not be rendered, because they are hidden.
188 195 *
189 196 * @since 1.0.0
190 197 */
191 - protected function _prepare_render_data() {
198 + protected function _prepare_render_data(): void {
192 199 $orig_table = $this->table;
193 200
194 - $num_rows = count( $this->table['data'] );
195 - $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;
196 205
197 206 // Evaluate show/hide_rows/columns parameters.
198 207 $actions = array( 'show', 'hide' );
199 208 $elements = array( 'rows', 'columns' );
@@ -198,56 +207,23 @@
198 207 $actions = array( 'show', 'hide' );
199 208 $elements = array( 'rows', 'columns' );
200 209 foreach ( $actions as $action ) {
201 210 foreach ( $elements as $element ) {
202 - if ( empty( $this->render_options[ "{$action}_{$element}" ] ) ) {
203 - $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();
204 215 continue;
205 216 }
206 217
207 - // Add all rows/columns to array if "all" value set for one of the four parameters.
208 - if ( 'all' === $this->render_options[ "{$action}_{$element}" ] ) {
209 - $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 );
210 221 continue;
211 222 }
212 223
213 - // We have a list of rows/columns (possibly with ranges in it).
214 - $this->render_options[ "{$action}_{$element}" ] = explode( ',', $this->render_options[ "{$action}_{$element}" ] );
215 - // Support for ranges like 3-6 or A-BA.
216 - $range_cells = array();
217 - foreach ( $this->render_options[ "{$action}_{$element}" ] as $key => $value ) {
218 - $range_dash = strpos( $value, '-' );
219 - if ( false !== $range_dash ) {
220 - unset( $this->render_options[ "{$action}_{$element}" ][ $key ] );
221 - $start = trim( substr( $value, 0, $range_dash ) );
222 - if ( ! is_numeric( $start ) ) {
223 - $start = TablePress::letter_to_number( $start );
224 - }
225 - $end = trim( substr( $value, $range_dash + 1 ) );
226 - if ( ! is_numeric( $end ) ) {
227 - $end = TablePress::letter_to_number( $end );
228 - }
229 - $current_range = range( $start, $end );
230 - $range_cells = array_merge( $range_cells, $current_range );
231 - }
232 - }
233 - $this->render_options[ "{$action}_{$element}" ] = array_merge( $this->render_options[ "{$action}_{$element}" ], $range_cells );
234 -
235 - /*
236 - * Parse single letters and change from regular numbering to zero-based numbering,
237 - * as rows/columns are indexed from 0 internally, but from 1 externally.
238 - */
239 - foreach ( $this->render_options[ "{$action}_{$element}" ] as $key => $value ) {
240 - $value = trim( $value );
241 - if ( ! is_numeric( $value ) ) {
242 - $value = TablePress::letter_to_number( $value );
243 - }
244 - $this->render_options[ "{$action}_{$element}" ][ $key ] = (int) $value - 1;
245 - }
246 -
247 - // Remove duplicate entries and sort the array.
248 - $this->render_options[ "{$action}_{$element}" ] = array_unique( $this->render_options[ "{$action}_{$element}" ] );
249 - 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 ) );
250 226 }
251 227 }
252 228
253 229 // Load information about hidden rows and columns.
@@ -277,11 +253,11 @@
277 253 * Filters the table after processing the table visibility information.
278 254 *
279 255 * @since 1.0.0
280 256 *
281 - * @param array $table The processed table.
282 - * @param array $orig_table The unprocessed table.
283 - * @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.
284 260 */
285 261 $this->table = apply_filters( 'tablepress_table_render_data', $this->table, $orig_table, $this->render_options );
286 262 }
287 263
@@ -289,9 +265,9 @@
289 265 * Generate the data that is to be rendered.
290 266 *
291 267 * @since 2.0.0
292 268 */
293 - protected function _process_render_data() {
269 + protected function _process_render_data(): void {
294 270 $orig_table = $this->table;
295 271
296 272 // Deactivate nl2br() for this render process, if "convert_line_breaks" Shortcode parameter is set to false.
297 273 if ( ! $this->render_options['convert_line_breaks'] ) {
@@ -300,13 +276,13 @@
300 276
301 277 foreach ( $this->table['data'] as $row_idx => $row ) {
302 278 foreach ( $row as $col_idx => $cell_content ) {
303 279 // Print formulas that are escaped with '= (like in Excel) as text.
304 - if ( "'=" === substr( $cell_content, 0, 2 ) ) {
280 + if ( str_starts_with( $cell_content, "'=" ) ) {
305 281 $cell_content = substr( $cell_content, 1 );
306 282 }
307 283 $cell_content = $this->safe_output( $cell_content );
308 - if ( false !== strpos( $cell_content, '[' ) ) {
284 + if ( str_contains( $cell_content, '[' ) ) {
309 285 $cell_content = do_shortcode( $cell_content );
310 286 }
311 287 /**
312 288 * Filters the content of a single cell, after formulas have been evaluated, the output has been sanitized, and Shortcodes have been evaluated.
@@ -332,11 +308,11 @@
332 308 * Filters the table after processing the table content handling.
333 309 *
334 310 * @since 2.0.0
335 311 *
336 - * @param array $table The processed table.
337 - * @param array $orig_table The unprocessed table.
338 - * @param array $render_options The render options for the table.
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.
339 315 */
340 316 $this->table = apply_filters( 'tablepress_table_content_render_data', $this->table, $orig_table, $this->render_options );
341 317 }
342 318
@@ -344,14 +320,15 @@
344 320 * Generate the HTML output of the table.
345 321 *
346 322 * @since 1.0.0
347 323 */
348 - protected function _render_table() {
324 + protected function _render_table(): void {
349 325 $num_rows = count( $this->table['data'] );
350 326 $num_columns = ( $num_rows > 0 ) ? count( $this->table['data'][0] ) : 0;
351 327
352 328 // Check if there are rows and columns in the table (might not be the case after removing hidden rows/columns!).
353 329 if ( 0 === $num_rows || 0 === $num_columns ) {
330 + /* translators: %s: Table ID */
354 331 $this->output = sprintf( __( '<!-- The table with the ID %s is empty! -->', 'tablepress' ), $this->table['id'] );
355 332 return;
356 333 }
357 334
@@ -363,10 +340,10 @@
363 340 * Filters the trigger keywords for "colspan" and "rowspan"
364 341 *
365 342 * @since 1.0.0
366 343 *
367 - * @param array $span_trigger The trigger keywords for combining table cells.
368 - * @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.
369 346 */
370 347 $this->span_trigger = apply_filters( 'tablepress_span_trigger_keywords', $this->span_trigger, $this->table['id'] );
371 348
372 349 // Explode from string to array.
@@ -405,11 +382,11 @@
405 382 * Filters the attributes for the table name (HTML h2 element, by default).
406 383 *
407 384 * @since 1.13.0
408 385 *
409 - * @param array $name_attributes The attributes for the table name element.
410 - * @param array $table The current table.
411 - * @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.
412 389 */
413 390 $name_attributes = apply_filters( 'tablepress_table_name_tag_attributes', $name_attributes, $this->table, $this->render_options );
414 391 $name_attributes = $this->_attributes_array_to_string( $name_attributes );
415 392
@@ -444,11 +421,11 @@
444 421 * Filters the attributes for the table description (HTML span element, by default).
445 422 *
446 423 * @since 1.13.0
447 424 *
448 - * @param array $description_attributes The attributes for the table description element.
449 - * @param array $table The current table.
450 - * @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.
451 428 */
452 429 $description_attributes = apply_filters( 'tablepress_table_description_tag_attributes', $description_attributes, $this->table, $this->render_options );
453 430 $description_attributes = $this->_attributes_array_to_string( $description_attributes );
454 431
@@ -461,30 +438,46 @@
461 438 if ( $this->render_options['print_description'] && 'above' === $this->render_options['print_description_position'] ) {
462 439 $output .= $print_description_html;
463 440 }
464 441
465 - $thead = '';
466 - $tfoot = '';
442 + $thead = array();
443 + $tfoot = array();
467 444 $tbody = array();
468 445
469 446 $this->last_row_idx = $num_rows - 1;
470 447 $this->last_column_idx = $num_columns - 1;
448 +
471 449 // Loop through rows in reversed order, to search for rowspan trigger keyword.
472 - for ( $row_idx = $this->last_row_idx; $row_idx >= 0; $row_idx-- ) {
473 - // Last row, need to check for footer (but only if at least two rows).
474 - if ( $this->last_row_idx === $row_idx && $this->render_options['table_foot'] && $num_rows > 1 ) {
475 - $tfoot = $this->_render_row( $row_idx, 'th' );
476 - 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;
477 458 }
478 - // First row, need to check for head (but only if at least two rows).
479 - if ( 0 === $row_idx && $this->render_options['table_head'] && $num_rows > 1 ) {
480 - $thead = $this->_render_row( $row_idx, 'th' );
481 - continue;
482 - }
483 - // 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 ) {
484 466 $tbody[] = $this->_render_row( $row_idx, 'td' );
467 + --$row_idx;
485 468 }
469 + // Reverse rows because we looped through the rows in reverse order.
470 + $tbody = array_reverse( $tbody );
486 471
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;
476 + }
477 + // Reverse rows because we looped through the rows in reverse order.
478 + $thead = array_reverse( $thead );
479 +
487 480 // <caption> tag.
488 481 /**
489 482 * Filters the content for the HTML caption element of the table.
490 483 *
@@ -491,10 +484,10 @@
491 484 * If the "Edit" link for a table is shown, it is also added to the caption element.
492 485 *
493 486 * @since 1.0.0
494 487 *
495 - * @param string $caption The content for the HTML caption element of the table. Default empty.
496 - * @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.
497 490 */
498 491 $caption = apply_filters( 'tablepress_print_caption_text', '', $this->table );
499 492 $caption_style = '';
500 493 $caption_class = '';
@@ -544,9 +537,9 @@
544 537 * @param string $table_id The current table ID.
545 538 * @param int $col_idx The number of the column.
546 539 */
547 540 $attributes = apply_filters( 'tablepress_colgroup_tag_attributes', $attributes, $this->table['id'], $col_idx + 1 );
548 - $colgroup .= "\t<col{$attributes}/>\n";
541 + $colgroup .= "\t<col{$attributes} />\n";
549 542 }
550 543 }
551 544 if ( ! empty( $colgroup ) ) {
552 545 $colgroup = "<colgroup>\n{$colgroup}</colgroup>\n";
@@ -551,18 +544,36 @@
551 544 if ( ! empty( $colgroup ) ) {
552 545 $colgroup = "<colgroup>\n{$colgroup}</colgroup>\n";
553 546 }
554 547
555 - // <thead>, <tfoot>, and <tbody> tags.
548 + /*
549 + * <thead>, <tfoot>, and <tbody> tags.
550 + */
551 +
556 552 if ( ! empty( $thead ) ) {
557 - $thead = "<thead>\n{$thead}</thead>\n";
553 + $thead = "<thead>\n" . implode( '', $thead ) . "</thead>\n";
554 + } else {
555 + $thead = '';
558 556 }
557 +
559 558 if ( ! empty( $tfoot ) ) {
560 - $tfoot = "<tfoot>\n{$tfoot}</tfoot>\n";
559 + $tfoot = "<tfoot>\n" . implode( '', $tfoot ) . "</tfoot>\n";
560 + } else {
561 + $tfoot = '';
561 562 }
562 - $tbody_class = ( $this->render_options['row_hover'] ) ? ' class="row-hover"' : '';
563 - // Reverse rows because we looped through the rows in reverse order.
564 - $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 +
565 576 $tbody = "<tbody{$tbody_class}>\n" . implode( '', $tbody ) . "</tbody>\n";
566 577
567 578 // Attributes for the table (HTML table element).
568 579 $table_attributes = array();
@@ -572,16 +583,23 @@
572 583 $table_attributes['id'] = $this->render_options['html_id'];
573 584 }
574 585
575 586 // "class" attribute.
576 - $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 + }
577 595 /**
578 596 * Filters the CSS classes that are given to the HTML table element.
579 597 *
580 598 * @since 1.0.0
581 599 *
582 - * @param array $css_classes The CSS classes for the table element.
583 - * @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.
584 602 */
585 603 $css_classes = apply_filters( 'tablepress_table_css_classes', $css_classes, $this->table['id'] );
586 604 // $css_classes might contain several classes in one array entry.
587 605 $css_classes = explode( ' ', implode( ' ', $css_classes ) );
@@ -586,10 +604,11 @@
586 604 // $css_classes might contain several classes in one array entry.
587 605 $css_classes = explode( ' ', implode( ' ', $css_classes ) );
588 606 $css_classes = array_map( array( 'TablePress', 'sanitize_css_class' ), $css_classes );
589 607 $css_classes = array_unique( $css_classes );
590 - $css_classes = trim( implode( ' ', $css_classes ) );
591 - if ( ! empty( $css_classes ) ) {
608 + $css_classes = array_filter( $css_classes ); // Remove empty entries.
609 + $css_classes = implode( ' ', $css_classes );
610 + if ( '' !== $css_classes ) {
592 611 $table_attributes['class'] = $css_classes;
593 612 }
594 613
595 614 // ARIA label attributes.
@@ -608,10 +627,10 @@
608 627 * The attribute is only added if it is not empty.
609 628 *
610 629 * @since 1.0.0
611 630 *
612 - * @param string $summary The content for the summary attribute of the table. Default empty.
613 - * @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.
614 633 */
615 634 $summary = apply_filters( 'tablepress_print_summary_attr', $summary, $this->table );
616 635 if ( ! empty( $summary ) ) {
617 636 $table_attributes['summary'] = esc_attr( $summary );
@@ -619,9 +638,9 @@
619 638
620 639 // Legacy support for attributes that are not encouraged in HTML5.
621 640 foreach ( array( 'cellspacing', 'cellpadding', 'border' ) as $attribute ) {
622 641 if ( false !== $this->render_options[ $attribute ] ) {
623 - $table_attributes[ $attribute ] = (int) $this->render_options[ $attribute ];
642 + $table_attributes[ $attribute ] = (string) absint( $this->render_options[ $attribute ] );
624 643 }
625 644 }
626 645
627 646 /**
@@ -628,19 +647,33 @@
628 647 * Filters the attributes for the table (HTML table element).
629 648 *
630 649 * @since 1.4.0
631 650 *
632 - * @param array $table_attributes The attributes for the table element.
633 - * @param array $table The current table.
634 - * @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.
635 654 */
636 655 $table_attributes = apply_filters( 'tablepress_table_tag_attributes', $table_attributes, $this->table, $this->render_options );
637 656 $table_attributes = $this->_attributes_array_to_string( $table_attributes );
638 657
639 - $output .= "\n<table{$table_attributes}>\n";
640 - $output .= $caption . $colgroup . $thead . $tbody . $tfoot;
641 - $output .= "</table>\n";
658 + $table_html = "<table{$table_attributes}>\n";
659 + $table_html .= $caption . $colgroup . $thead . $tbody . $tfoot;
660 + $table_html .= '</table>';
642 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 +
643 676 // name/description below table (HTML already generated above).
644 677 if ( $this->render_options['print_name'] && 'below' === $this->render_options['print_name_position'] ) {
645 678 $output .= $print_name_html;
646 679 }
@@ -648,15 +681,15 @@
648 681 $output .= $print_description_html;
649 682 }
650 683
651 684 /**
652 - * Filters the generated HTML code for table.
685 + * Filters the generated HTML code for the table and HTML elements around it.
653 686 *
654 687 * @since 1.0.0
655 688 *
656 - * @param string $output The generated HTML for the table.
657 - * @param array $table The current table.
658 - * @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.
659 692 */
660 693 $this->output = apply_filters( 'tablepress_table_output', $output, $this->table, $this->render_options );
661 694 }
662 695
@@ -668,9 +701,9 @@
668 701 * @param int $row_idx Index of the row to be rendered.
669 702 * @param string $tag HTML tag to use for the cells (td or th).
670 703 * @return string HTML for the row.
671 704 */
672 - protected function _render_row( $row_idx, $tag ) {
705 + protected function _render_row( int $row_idx, string $tag ): string {
673 706 $row_cells = array();
674 707 // Loop through cells in reversed order, to search for colspan or rowspan trigger words.
675 708 for ( $col_idx = $this->last_column_idx; $col_idx >= 0; $col_idx-- ) {
676 709 $cell_content = $this->table['data'][ $row_idx ][ $col_idx ];
@@ -677,10 +710,10 @@
677 710
678 711 if ( $this->span_trigger['rowspan'] === $cell_content ) { // There will be a rowspan.
679 712 if ( ! (
680 713 ( 0 === $row_idx ) // No rowspan inside first row.
681 - || ( 1 === $row_idx && $this->render_options['table_head'] ) // No rowspan into table head.
682 - || ( $this->last_row_idx === $row_idx && $this->render_options['table_foot'] ) // No rowspan out of table foot.
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.
683 716 ) ) {
684 717 // Increase counter for rowspan in this column.
685 718 ++$this->rowspan[ $col_idx ];
686 719 // Reset counter for colspan in this row, combined col- and rowspan might be happening.
@@ -704,10 +737,10 @@
704 737 $cell_content = '';
705 738 } elseif ( $this->span_trigger['span'] === $cell_content ) { // There will be a combined col- and rowspan.
706 739 if ( ! (
707 740 ( 0 === $row_idx ) // No rowspan inside first row.
708 - || ( 1 === $row_idx && $this->render_options['table_head'] ) // No rowspan into table head.
709 - || ( $this->last_row_idx === $row_idx && $this->render_options['table_foot'] ) // No rowspan out of table foot.
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.
710 743 ) && ! (
711 744 ( 0 === $col_idx ) // No colspan inside first column.
712 745 || ( 1 === $col_idx && $this->render_options['first_column_th'] ) // No colspan into first column head.
713 746 ) ) {
@@ -721,12 +754,20 @@
721 754 $tag_attributes = array();
722 755
723 756 // "colspan" and "rowspan" attributes.
724 757 if ( $this->colspan[ $row_idx ] > 1 ) { // We have colspaned cells.
725 - $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 + }
726 763 }
727 764 if ( $this->rowspan[ $col_idx ] > 1 ) { // We have rowspaned cells.
728 - $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 + }
729 770 }
730 771
731 772 // "class" attribute.
732 773 $cell_class = 'column-' . ( $col_idx + 1 );
@@ -757,24 +798,28 @@
757 798 * Filters the attributes for the table cell (HTML td or th element).
758 799 *
759 800 * @since 1.4.0
760 801 *
761 - * @param array $tag_attributes The attributes for the td or th element.
762 - * @param string $table_id The current table ID.
763 - * @param string $cell_content The cell content.
764 - * @param int $row_idx The row number of the cell.
765 - * @param int $col_idx The column number of the cell.
766 - * @param int $colspan_row The number of combined columns for this cell.
767 - * @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.
768 809 */
769 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 ] );
770 811 $tag_attributes = $this->_attributes_array_to_string( $tag_attributes );
771 812
772 - if ( $this->render_options['first_column_th'] && 0 === $col_idx ) {
773 - $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.
774 819 }
775 820
776 - $row_cells[] = "<{$tag}{$tag_attributes}>{$cell_content}</{$tag}>";
821 + $row_cells[] = "<{$cell_tag}{$tag_attributes}>{$cell_content}</{$cell_tag}>";
777 822 $this->colspan[ $row_idx ] = 1; // Reset.
778 823 $this->rowspan[ $col_idx ] = 1; // Reset.
779 824 }
780 825
@@ -782,21 +827,18 @@
782 827 $tr_attributes = array();
783 828
784 829 // "class" attribute.
785 830 $row_classes = 'row-' . ( $row_idx + 1 );
786 - if ( $this->render_options['alternating_row_colors'] ) {
787 - $row_classes .= ( 1 === ( $row_idx % 2 ) ) ? ' even' : ' odd';
788 - }
789 831 /**
790 832 * Filters the CSS classes that are given to a row (HTML tr element) of a table.
791 833 *
792 834 * @since 1.0.0
793 835 *
794 - * @param string $row_classes The CSS classes for the row.
795 - * @param string $table_id The current table ID.
796 - * @param array $row_cells The HTML code for the cells of the row.
797 - * @param int $row_idx The row number.
798 - * @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.
799 841 */
800 842 $row_classes = apply_filters( 'tablepress_row_css_class', $row_classes, $this->table['id'], $row_cells, $row_idx + 1, $this->table['data'][ $row_idx ] );
801 843 if ( ! empty( $row_classes ) ) {
802 844 $tr_attributes['class'] = $row_classes;
@@ -806,12 +848,12 @@
806 848 * Filters the attributes for the table row (HTML tr element).
807 849 *
808 850 * @since 1.4.0
809 851 *
810 - * @param array $tr_attributes The attributes for the tr element.
811 - * @param string $table_id The current table ID.
812 - * @param int $row_idx The row number.
813 - * @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.
814 856 */
815 857 $tr_attributes = apply_filters( 'tablepress_row_tag_attributes', $tr_attributes, $this->table['id'], $row_idx + 1, $this->table['data'][ $row_idx ] );
816 858 $tr_attributes = $this->_attributes_array_to_string( $tr_attributes );
817 859
@@ -824,12 +866,12 @@
824 866 * Convert an array of HTML tag attributes to a string.
825 867 *
826 868 * @since 1.4.0
827 869 *
828 - * @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.
829 871 * @return string The attributes as a string for usage in a HTML element.
830 872 */
831 - protected function _attributes_array_to_string( array $attributes ) {
873 + protected function _attributes_array_to_string( array $attributes ): string {
832 874 $attributes_string = '';
833 875 foreach ( $attributes as $attribute => $value ) {
834 876 $attributes_string .= " {$attribute}=\"{$value}\"";
835 877 }
@@ -838,21 +880,19 @@
838 880
839 881 /**
840 882 * Possibly replace certain HTML entities and replace line breaks with HTML.
841 883 *
842 - * @TODO: Find a better solution than this function, e.g. something like wpautop().
843 - *
844 884 * @since 1.0.0
845 885 *
846 886 * @param string $text The string to process.
847 887 * @return string Processed string for output.
848 888 */
849 - protected function safe_output( $text ) {
889 + protected function safe_output( string $text ): string {
850 890 /*
851 891 * Replace any & with &amp; that is not already an encoded entity (from function htmlentities2 in WP 2.8).
852 892 * A complete htmlentities2() or htmlspecialchars() would encode <HTML> tags, which we don't want.
853 893 */
854 - $text = preg_replace( '/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,4};)/', '&amp;', $text );
894 + $text = (string) preg_replace( '/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,4};)/', '&amp;', $text );
855 895 /**
856 896 * Filters whether line breaks in the cell content shall be replaced with HTML br tags.
857 897 *
858 898 * @since 1.0.0
@@ -870,14 +910,15 @@
870 910 * Get the default render options, null means: Use option from "Edit" screen.
871 911 *
872 912 * @since 1.0.0
873 913 *
874 - * @return array Default render options.
914 + * @return array<string, mixed> Default render options.
875 915 */
876 - public function get_default_render_options() {
916 + public function get_default_render_options(): array {
877 917 // Attention: Array keys have to be lowercase, otherwise they won't match the Shortcode attributes, which will be passed in lowercase by WP.
878 918 return array(
879 919 'alternating_row_colors' => null,
920 + 'block_preview' => false,
880 921 'border' => false,
881 922 'cache_table_output' => true,
882 923 'cellpadding' => false,
883 924 'cellspacing' => false,
@@ -883,8 +924,9 @@
883 924 'cellspacing' => false,
884 925 'column_widths' => '',
885 926 'convert_line_breaks' => true,
886 927 'datatables_custom_commands' => null,
928 + 'datatables_datetime' => '',
887 929 'datatables_filter' => null,
888 930 'datatables_info' => null,
889 931 'datatables_lengthchange' => null,
890 932 'datatables_locale' => get_locale(),
@@ -919,36 +961,35 @@
919 961 * @since 1.0.0
920 962 *
921 963 * @return string CSS for the Preview iframe.
922 964 */
923 - public function get_preview_css() {
965 + public function get_preview_css(): string {
924 966 $is_rtl = is_rtl();
925 967 $tablepress_css = TablePress::load_class( 'TablePress_CSS', 'class-css.php', 'classes' );
926 968 $default_css_minified = $tablepress_css->load_default_css_from_file( $is_rtl );
927 969 if ( false === $default_css_minified ) {
928 970 $default_css_minified = '';
929 - } else {
930 - // Change relative URLs to web font files to absolute URLs, as combining the CSS files and saving to another directory breaks the relative URLs.
931 - $absolute_path = plugins_url( 'css/build/tablepress.', TABLEPRESS__FILE__ );
932 - // Make the absolute URL protocol-relative to prevent mixed content warnings.
933 - $absolute_path = str_replace( array( 'http:', 'https:' ), '', $absolute_path );
934 - $default_css_minified = str_replace( 'url(tablepress.', 'url(' . $absolute_path, $default_css_minified );
935 971 }
936 972
937 973 $rtl_direction = $is_rtl ? "\ndirection: rtl;" : '';
938 974
939 975 return <<<CSS
940 -<style>
941 -/* iframe */
942 -body {
943 - margin: 10px;
944 - font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;{$rtl_direction}
945 -}
946 -p {
947 - font-size: 13px;
948 -}
949 -{$default_css_minified}
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