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
tablepress / classes / class-render.php

class-render.php in TablePress – Tables in WordPress made easy 3.4, at classes/class-render.php

996 lines 36.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * TablePress Rendering Class
4 *
5 * @package TablePress
6 * @subpackage Rendering
7 * @author Tobias Bäthge
8 * @since 1.0.0
9 */
10
11 declare(strict_types=1);
12
13 // Prohibit direct script loading.
14 defined( 'ABSPATH' ) || die( 'No direct script access allowed!' );
15
16 /**
17 * TablePress Rendering Class
18 *
19 * @package TablePress
20 * @subpackage Rendering
21 * @author Tobias Bäthge
22 * @since 1.0.0
23 */
24 class TablePress_Render {
25
26 /**
27 * Table data that is rendered.
28 *
29 * @since 1.0.0
30 * @var array<string, mixed>
31 */
32 protected array $table = array();
33
34 /**
35 * Table options that influence the output result.
36 *
37 * @since 1.0.0
38 * @var array<string, mixed>
39 */
40 protected array $render_options = array();
41
42 /**
43 * Rendered HTML code of the table or PHP array.
44 *
45 * @since 1.0.0
46 * @var string|array<int, array<int, string>>
47 */
48 protected $output;
49
50 /**
51 * Trigger words for colspan, rowspan, or the combination of both.
52 *
53 * @since 1.0.0
54 * @var array<string, string>
55 */
56 protected array $span_trigger = array(
57 'colspan' => '#colspan#',
58 'rowspan' => '#rowspan#',
59 'span' => '#span#',
60 );
61
62 /**
63 * Buffer to store the counts of rowspan per column, initialized in _render_table().
64 *
65 * @since 1.0.0
66 * @var int[]
67 */
68 protected array $rowspan = array();
69
70 /**
71 * Buffer to store the counts of colspan per row, initialized in _render_table().
72 *
73 * @since 1.0.0
74 * @var int[]
75 */
76 protected array $colspan = array();
77
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 /**
86 * Index of the last row of the visible data in the table, set in _render_table().
87 *
88 * @since 1.0.0
89 */
90 protected int $last_row_idx;
91
92 /**
93 * Index of the last column of the visible data in the table, set in _render_table().
94 *
95 * @since 1.0.0
96 */
97 protected int $last_column_idx;
98
99 /**
100 * Class constructor.
101 *
102 * @since 1.0.0
103 */
104 public function __construct() {
105 // Unused.
106 }
107
108 /**
109 * Set the table (data, options, visibility, ...) that is to be rendered.
110 *
111 * @since 1.0.0
112 *
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.
115 */
116 public function set_input( array $table, array $render_options ): void {
117 $this->table = $table;
118 $this->render_options = $render_options;
119 /**
120 * Filters the table before the render process.
121 *
122 * @since 1.0.0
123 *
124 * @param array<string, mixed> $table The table.
125 * @param array<string, mixed> $render_options The render options for the table.
126 */
127 $this->table = apply_filters( 'tablepress_table_raw_render_data', $this->table, $this->render_options );
128 }
129
130 /**
131 * Process the table rendering and return the HTML output.
132 *
133 * @since 1.0.0
134 * @since 2.0.0 Add the $format parameter.
135 *
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.
138 */
139 public function get_output( string $format = 'html' ) /* : string|array */ {
140 // Evaluate math expressions/formulas.
141 $this->_evaluate_table_data();
142 // Remove hidden rows and columns.
143 $this->_prepare_render_data();
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
165 return $this->output;
166 }
167
168 /**
169 * Loop through the table to evaluate math expressions/formulas.
170 *
171 * @since 1.0.0
172 */
173 protected function _evaluate_table_data(): void {
174 $orig_table = $this->table;
175
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
181 /**
182 * Filters the table after evaluating formulas in the table.
183 *
184 * @since 1.0.0
185 *
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.
189 */
190 $this->table = apply_filters( 'tablepress_table_evaluate_data', $this->table, $orig_table, $this->render_options );
191 }
192
193 /**
194 * Remove all cells from the data set that shall not be rendered, because they are hidden.
195 *
196 * @since 1.0.0
197 */
198 protected function _prepare_render_data(): void {
199 $orig_table = $this->table;
200
201 $counts = array(
202 'rows' => count( $this->table['data'] ),
203 );
204 $counts['columns'] = ( $counts['rows'] > 0 ) ? count( $this->table['data'][0] ) : 0;
205
206 // Evaluate show/hide_rows/columns parameters.
207 $actions = array( 'show', 'hide' );
208 $elements = array( 'rows', 'columns' );
209 foreach ( $actions as $action ) {
210 foreach ( $elements as $element ) {
211 $action_element = "{$action}_{$element}";
212
213 if ( empty( $this->render_options[ $action_element ] ) ) {
214 $this->render_options[ $action_element ] = array();
215 continue;
216 }
217
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 );
221 continue;
222 }
223
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 ) );
226 }
227 }
228
229 // Load information about hidden rows and columns.
230 // Get indexes of hidden rows (array value of 0).
231 $hidden_rows = array_keys( $this->table['visibility']['rows'], 0, true );
232 $hidden_rows = array_merge( $hidden_rows, $this->render_options['hide_rows'] );
233 $hidden_rows = array_diff( $hidden_rows, $this->render_options['show_rows'] );
234 // Get indexes of hidden columns (array value of 0).
235 $hidden_columns = array_keys( $this->table['visibility']['columns'], 0, true );
236 $hidden_columns = array_merge( $hidden_columns, $this->render_options['hide_columns'] );
237 $hidden_columns = array_merge( array_diff( $hidden_columns, $this->render_options['show_columns'] ) );
238
239 // Remove hidden rows and re-index.
240 foreach ( $hidden_rows as $row_idx ) {
241 unset( $this->table['data'][ $row_idx ] );
242 }
243 $this->table['data'] = array_merge( $this->table['data'] );
244 // Remove hidden columns and re-index.
245 foreach ( $this->table['data'] as $row_idx => $row ) {
246 foreach ( $hidden_columns as $col_idx ) {
247 unset( $row[ $col_idx ] );
248 }
249 $this->table['data'][ $row_idx ] = array_merge( $row );
250 }
251
252 /**
253 * Filters the table after processing the table visibility information.
254 *
255 * @since 1.0.0
256 *
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.
260 */
261 $this->table = apply_filters( 'tablepress_table_render_data', $this->table, $orig_table, $this->render_options );
262 }
263
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 /**
320 * Generate the HTML output of the table.
321 *
322 * @since 1.0.0
323 */
324 protected function _render_table(): void {
325 $num_rows = count( $this->table['data'] );
326 $num_columns = ( $num_rows > 0 ) ? count( $this->table['data'][0] ) : 0;
327
328 // Check if there are rows and columns in the table (might not be the case after removing hidden rows/columns!).
329 if ( 0 === $num_rows || 0 === $num_columns ) {
330 /* translators: %s: Table ID */
331 $this->output = sprintf( __( '<!-- The table with the ID %s is empty! -->', 'tablepress' ), $this->table['id'] );
332 return;
333 }
334
335 // Counters for spans of rows and columns, init to 1 for each row and column (as that means no span).
336 $this->rowspan = array_fill( 0, $num_columns, 1 );
337 $this->colspan = array_fill( 0, $num_rows, 1 );
338
339 /**
340 * Filters the trigger keywords for "colspan" and "rowspan"
341 *
342 * @since 1.0.0
343 *
344 * @param array<string, string> $span_trigger The trigger keywords for combining table cells.
345 * @param string $table_id The current table ID.
346 */
347 $this->span_trigger = apply_filters( 'tablepress_span_trigger_keywords', $this->span_trigger, $this->table['id'] );
348
349 // Explode from string to array.
350 $this->render_options['column_widths'] = ( ! empty( $this->render_options['column_widths'] ) ) ? explode( '|', $this->render_options['column_widths'] ) : array();
351 // Make array $this->render_options['column_widths'] have $columns entries.
352 $this->render_options['column_widths'] = array_pad( $this->render_options['column_widths'], $num_columns, '' );
353
354 $output = '';
355
356 if ( $this->render_options['print_name'] ) {
357 /**
358 * Filters the HTML tag that wraps the printed table name.
359 *
360 * @since 1.0.0
361 *
362 * @param string $tag The HTML tag around the table name. Default h2.
363 * @param string $table_id The current table ID.
364 */
365 $name_html_tag = apply_filters( 'tablepress_print_name_html_tag', 'h2', $this->table['id'] );
366
367 $name_attributes = array();
368 if ( ! empty( $this->render_options['html_id'] ) ) {
369 $name_attributes['id'] = "{$this->render_options['html_id']}-name";
370 }
371 /**
372 * Filters the class attribute for the printed table name.
373 *
374 * @since 1.0.0
375 * @deprecated 1.13.0 Use {@see 'tablepress_table_name_tag_attributes'} instead.
376 *
377 * @param string $class The class attribute for the table name that can be used in CSS code.
378 * @param string $table_id The current table ID.
379 */
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' );
381 /**
382 * Filters the attributes for the table name (HTML h2 element, by default).
383 *
384 * @since 1.13.0
385 *
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.
389 */
390 $name_attributes = apply_filters( 'tablepress_table_name_tag_attributes', $name_attributes, $this->table, $this->render_options );
391 $name_attributes = $this->_attributes_array_to_string( $name_attributes );
392
393 $print_name_html = "<{$name_html_tag}{$name_attributes}>" . $this->safe_output( $this->table['name'] ) . "</{$name_html_tag}>\n";
394 }
395 if ( $this->render_options['print_description'] ) {
396 /**
397 * Filters the HTML tag that wraps the printed table description.
398 *
399 * @since 1.0.0
400 *
401 * @param string $tag The HTML tag around the table description. Default span.
402 * @param string $table_id The current table ID.
403 */
404 $description_html_tag = apply_filters( 'tablepress_print_description_html_tag', 'span', $this->table['id'] );
405
406 $description_attributes = array();
407 if ( ! empty( $this->render_options['html_id'] ) ) {
408 $description_attributes['id'] = "{$this->render_options['html_id']}-description";
409 }
410 /**
411 * Filters the class attribute for the printed table description.
412 *
413 * @since 1.0.0
414 * @deprecated 1.13.0 Use {@see 'tablepress_table_description_tag_attributes'} instead.
415 *
416 * @param string $class The class attribute for the table description that can be used in CSS code.
417 * @param string $table_id The current table ID.
418 */
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' );
420 /**
421 * Filters the attributes for the table description (HTML span element, by default).
422 *
423 * @since 1.13.0
424 *
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.
428 */
429 $description_attributes = apply_filters( 'tablepress_table_description_tag_attributes', $description_attributes, $this->table, $this->render_options );
430 $description_attributes = $this->_attributes_array_to_string( $description_attributes );
431
432 $print_description_html = "<{$description_html_tag}{$description_attributes}>" . $this->safe_output( $this->table['description'] ) . "</{$description_html_tag}>\n";
433 }
434
435 if ( $this->render_options['print_name'] && 'above' === $this->render_options['print_name_position'] ) {
436 $output .= $print_name_html;
437 }
438 if ( $this->render_options['print_description'] && 'above' === $this->render_options['print_description_position'] ) {
439 $output .= $print_description_html;
440 }
441
442 $thead = array();
443 $tfoot = array();
444 $tbody = array();
445
446 $this->last_row_idx = $num_rows - 1;
447 $this->last_column_idx = $num_columns - 1;
448
449 // Loop through rows in reversed order, to search for rowspan trigger keyword.
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;
458 }
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 ) {
466 $tbody[] = $this->_render_row( $row_idx, 'td' );
467 --$row_idx;
468 }
469 // Reverse rows because we looped through the rows in reverse order.
470 $tbody = array_reverse( $tbody );
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
480 // <caption> tag.
481 /**
482 * Filters the content for the HTML caption element of the table.
483 *
484 * If the "Edit" link for a table is shown, it is also added to the caption element.
485 *
486 * @since 1.0.0
487 *
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.
490 */
491 $caption = apply_filters( 'tablepress_print_caption_text', '', $this->table );
492 $caption_style = '';
493 $caption_class = '';
494 if ( ! empty( $caption ) ) {
495 /**
496 * Filters the class attribute for the HTML caption element of the table.
497 *
498 * @since 1.0.0
499 *
500 * @param string $class The class attribute for the HTML caption element of the table.
501 * @param string $table_id The current table ID.
502 */
503 $caption_class = apply_filters( 'tablepress_print_caption_class', "tablepress-table-caption tablepress-table-caption-id-{$this->table['id']}", $this->table['id'] );
504 $caption_class = ' class="' . $caption_class . '"';
505 }
506 if ( ! empty( $this->render_options['edit_table_url'] ) ) {
507 if ( empty( $caption ) ) {
508 $caption_style = ' style="caption-side:bottom;text-align:left;border:none;background:none;margin:0;padding:0;"';
509 } else {
510 $caption .= '<br />';
511 }
512 $caption .= '<a href="' . esc_url( $this->render_options['edit_table_url'] ) . '" rel="nofollow">' . __( 'Edit', 'default' ) . '</a>';
513 }
514 if ( ! empty( $caption ) ) {
515 $caption = "<caption{$caption_class}{$caption_style}>{$caption}</caption>\n";
516 }
517
518 // <colgroup> tag.
519 $colgroup = '';
520 /**
521 * Filters whether the HTML colgroup tag shall be added to the table output.
522 *
523 * @since 1.0.0
524 *
525 * @param bool $print Whether the colgroup element shall be printed.
526 * @param string $table_id The current table ID.
527 */
528 if ( apply_filters( 'tablepress_print_colgroup_tag', false, $this->table['id'] ) ) {
529 for ( $col_idx = 0; $col_idx < $num_columns; $col_idx++ ) {
530 $attributes = ' class="colgroup-column-' . ( $col_idx + 1 ) . ' "';
531 /**
532 * Filters the attributes of the HTML col tags in the HTML colgroup tag.
533 *
534 * @since 1.0.0
535 *
536 * @param string $attributes The attributes in the col element.
537 * @param string $table_id The current table ID.
538 * @param int $col_idx The number of the column.
539 */
540 $attributes = apply_filters( 'tablepress_colgroup_tag_attributes', $attributes, $this->table['id'], $col_idx + 1 );
541 $colgroup .= "\t<col{$attributes} />\n";
542 }
543 }
544 if ( ! empty( $colgroup ) ) {
545 $colgroup = "<colgroup>\n{$colgroup}</colgroup>\n";
546 }
547
548 /*
549 * <thead>, <tfoot>, and <tbody> tags.
550 */
551
552 if ( ! empty( $thead ) ) {
553 $thead = "<thead>\n" . implode( '', $thead ) . "</thead>\n";
554 } else {
555 $thead = '';
556 }
557
558 if ( ! empty( $tfoot ) ) {
559 $tfoot = "<tfoot>\n" . implode( '', $tfoot ) . "</tfoot>\n";
560 } else {
561 $tfoot = '';
562 }
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
576 $tbody = "<tbody{$tbody_class}>\n" . implode( '', $tbody ) . "</tbody>\n";
577
578 // Attributes for the table (HTML table element).
579 $table_attributes = array();
580
581 // "id" attribute.
582 if ( ! empty( $this->render_options['html_id'] ) ) {
583 $table_attributes['id'] = $this->render_options['html_id'];
584 }
585
586 // "class" attribute.
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 }
595 /**
596 * Filters the CSS classes that are given to the HTML table element.
597 *
598 * @since 1.0.0
599 *
600 * @param string[] $css_classes The CSS classes for the table element.
601 * @param string $table_id The current table ID.
602 */
603 $css_classes = apply_filters( 'tablepress_table_css_classes', $css_classes, $this->table['id'] );
604 // $css_classes might contain several classes in one array entry.
605 $css_classes = explode( ' ', implode( ' ', $css_classes ) );
606 $css_classes = array_map( array( 'TablePress', 'sanitize_css_class' ), $css_classes );
607 $css_classes = array_unique( $css_classes );
608 $css_classes = array_filter( $css_classes ); // Remove empty entries.
609 $css_classes = implode( ' ', $css_classes );
610 if ( '' !== $css_classes ) {
611 $table_attributes['class'] = $css_classes;
612 }
613
614 // ARIA label attributes.
615 if ( $this->render_options['print_name'] && ! empty( $this->render_options['html_id'] ) ) {
616 $table_attributes['aria-labelledby'] = "{$this->render_options['html_id']}-name";
617 }
618 if ( $this->render_options['print_description'] && ! empty( $this->render_options['html_id'] ) ) {
619 $table_attributes['aria-describedby'] = "{$this->render_options['html_id']}-description";
620 }
621
622 // "summary" attribute.
623 $summary = '';
624 /**
625 * Filters the content for the summary attribute of the HTML table element.
626 *
627 * The attribute is only added if it is not empty.
628 *
629 * @since 1.0.0
630 *
631 * @param string $summary The content for the summary attribute of the table. Default empty.
632 * @param array<string, mixed> $table The current table.
633 */
634 $summary = apply_filters( 'tablepress_print_summary_attr', $summary, $this->table );
635 if ( ! empty( $summary ) ) {
636 $table_attributes['summary'] = esc_attr( $summary );
637 }
638
639 // Legacy support for attributes that are not encouraged in HTML5.
640 foreach ( array( 'cellspacing', 'cellpadding', 'border' ) as $attribute ) {
641 if ( false !== $this->render_options[ $attribute ] ) {
642 $table_attributes[ $attribute ] = (string) absint( $this->render_options[ $attribute ] );
643 }
644 }
645
646 /**
647 * Filters the attributes for the table (HTML table element).
648 *
649 * @since 1.4.0
650 *
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.
654 */
655 $table_attributes = apply_filters( 'tablepress_table_tag_attributes', $table_attributes, $this->table, $this->render_options );
656 $table_attributes = $this->_attributes_array_to_string( $table_attributes );
657
658 $table_html = "<table{$table_attributes}>\n";
659 $table_html .= $caption . $colgroup . $thead . $tbody . $tfoot;
660 $table_html .= '</table>';
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
676 // name/description below table (HTML already generated above).
677 if ( $this->render_options['print_name'] && 'below' === $this->render_options['print_name_position'] ) {
678 $output .= $print_name_html;
679 }
680 if ( $this->render_options['print_description'] && 'below' === $this->render_options['print_description_position'] ) {
681 $output .= $print_description_html;
682 }
683
684 /**
685 * Filters the generated HTML code for the table and HTML elements around it.
686 *
687 * @since 1.0.0
688 *
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.
692 */
693 $this->output = apply_filters( 'tablepress_table_output', $output, $this->table, $this->render_options );
694 }
695
696 /**
697 * Generate the HTML of a row.
698 *
699 * @since 1.0.0
700 *
701 * @param int $row_idx Index of the row to be rendered.
702 * @param string $tag HTML tag to use for the cells (td or th).
703 * @return string HTML for the row.
704 */
705 protected function _render_row( int $row_idx, string $tag ): string {
706 $row_cells = array();
707 // Loop through cells in reversed order, to search for colspan or rowspan trigger words.
708 for ( $col_idx = $this->last_column_idx; $col_idx >= 0; $col_idx-- ) {
709 $cell_content = $this->table['data'][ $row_idx ][ $col_idx ];
710
711 if ( $this->span_trigger['rowspan'] === $cell_content ) { // There will be a rowspan.
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 ) ) {
717 // Increase counter for rowspan in this column.
718 ++$this->rowspan[ $col_idx ];
719 // Reset counter for colspan in this row, combined col- and rowspan might be happening.
720 $this->colspan[ $row_idx ] = 1;
721 continue;
722 }
723 // Invalid rowspan, so we set cell content from #rowspan# to empty.
724 $cell_content = '';
725 } elseif ( $this->span_trigger['colspan'] === $cell_content ) { // There will be a colspan.
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 ) ) {
730 // Increase counter for colspan in this row.
731 ++$this->colspan[ $row_idx ];
732 // Reset counter for rowspan in this column, combined col- and rowspan might be happening.
733 $this->rowspan[ $col_idx ] = 1;
734 continue;
735 }
736 // Invalid colspan, so we set cell content from #colspan# to empty.
737 $cell_content = '';
738 } elseif ( $this->span_trigger['span'] === $cell_content ) { // There will be a combined col- and rowspan.
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 ) ) {
747 continue;
748 }
749 // Invalid span, so we set cell content from #span# to empty.
750 $cell_content = '';
751 }
752
753 // Attributes for the table cell (HTML td or th element).
754 $tag_attributes = array();
755
756 // "colspan" and "rowspan" attributes.
757 if ( $this->colspan[ $row_idx ] > 1 ) { // We have colspaned cells.
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 }
763 }
764 if ( $this->rowspan[ $col_idx ] > 1 ) { // We have rowspaned cells.
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 }
770 }
771
772 // "class" attribute.
773 $cell_class = 'column-' . ( $col_idx + 1 );
774 /**
775 * Filters the CSS classes that are given to a single cell (HTML td element) of a table.
776 *
777 * @since 1.0.0
778 *
779 * @param string $cell_class The CSS classes for the cell.
780 * @param string $table_id The current table ID.
781 * @param string $cell_content The cell content.
782 * @param int $row_idx The row number of the cell.
783 * @param int $col_idx The column number of the cell.
784 * @param int $colspan_row The number of combined columns for this cell.
785 * @param int $rowspan_col The number of combined rows for this cell.
786 */
787 $cell_class = apply_filters( 'tablepress_cell_css_class', $cell_class, $this->table['id'], $cell_content, $row_idx + 1, $col_idx + 1, $this->colspan[ $row_idx ], $this->rowspan[ $col_idx ] );
788 if ( ! empty( $cell_class ) ) {
789 $tag_attributes['class'] = $cell_class;
790 }
791
792 // "style" attribute.
793 if ( ( 0 === $row_idx ) && ! empty( $this->render_options['column_widths'][ $col_idx ] ) ) {
794 $tag_attributes['style'] = 'width:' . preg_replace( '#[^0-9a-z.%]#', '', $this->render_options['column_widths'][ $col_idx ] ) . ';';
795 }
796
797 /**
798 * Filters the attributes for the table cell (HTML td or th element).
799 *
800 * @since 1.4.0
801 *
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.
809 */
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 ] );
811 $tag_attributes = $this->_attributes_array_to_string( $tag_attributes );
812
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.
819 }
820
821 $row_cells[] = "<{$cell_tag}{$tag_attributes}>{$cell_content}</{$cell_tag}>";
822 $this->colspan[ $row_idx ] = 1; // Reset.
823 $this->rowspan[ $col_idx ] = 1; // Reset.
824 }
825
826 // Attributes for the table row (HTML tr element).
827 $tr_attributes = array();
828
829 // "class" attribute.
830 $row_classes = 'row-' . ( $row_idx + 1 );
831 /**
832 * Filters the CSS classes that are given to a row (HTML tr element) of a table.
833 *
834 * @since 1.0.0
835 *
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.
841 */
842 $row_classes = apply_filters( 'tablepress_row_css_class', $row_classes, $this->table['id'], $row_cells, $row_idx + 1, $this->table['data'][ $row_idx ] );
843 if ( ! empty( $row_classes ) ) {
844 $tr_attributes['class'] = $row_classes;
845 }
846
847 /**
848 * Filters the attributes for the table row (HTML tr element).
849 *
850 * @since 1.4.0
851 *
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.
856 */
857 $tr_attributes = apply_filters( 'tablepress_row_tag_attributes', $tr_attributes, $this->table['id'], $row_idx + 1, $this->table['data'][ $row_idx ] );
858 $tr_attributes = $this->_attributes_array_to_string( $tr_attributes );
859
860 // Reverse rows because we looped through the cells in reverse order.
861 $row_cells = array_reverse( $row_cells );
862 return "<tr{$tr_attributes}>\n\t" . implode( '', $row_cells ) . "\n</tr>\n";
863 }
864
865 /**
866 * Convert an array of HTML tag attributes to a string.
867 *
868 * @since 1.4.0
869 *
870 * @param array<string, string> $attributes Attributes for the HTML tag in the array keys, and their values in the array values.
871 * @return string The attributes as a string for usage in a HTML element.
872 */
873 protected function _attributes_array_to_string( array $attributes ): string {
874 $attributes_string = '';
875 foreach ( $attributes as $attribute => $value ) {
876 $attributes_string .= " {$attribute}=\"{$value}\"";
877 }
878 return $attributes_string;
879 }
880
881 /**
882 * Possibly replace certain HTML entities and replace line breaks with HTML.
883 *
884 * @since 1.0.0
885 *
886 * @param string $text The string to process.
887 * @return string Processed string for output.
888 */
889 protected function safe_output( string $text ): string {
890 /*
891 * Replace any & with &amp; that is not already an encoded entity (from function htmlentities2 in WP 2.8).
892 * A complete htmlentities2() or htmlspecialchars() would encode <HTML> tags, which we don't want.
893 */
894 $text = (string) preg_replace( '/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,4};)/', '&amp;', $text );
895 /**
896 * Filters whether line breaks in the cell content shall be replaced with HTML br tags.
897 *
898 * @since 1.0.0
899 *
900 * @param bool $replace Whether to replace line breaks with HTML br tags. Default true.
901 * @param string $table_id The current table ID.
902 */
903 if ( apply_filters( 'tablepress_apply_nl2br', true, $this->table['id'] ) ) {
904 $text = nl2br( $text );
905 }
906 return $text;
907 }
908
909 /**
910 * Get the default render options, null means: Use option from "Edit" screen.
911 *
912 * @since 1.0.0
913 *
914 * @return array<string, mixed> Default render options.
915 */
916 public function get_default_render_options(): array {
917 // Attention: Array keys have to be lowercase, otherwise they won't match the Shortcode attributes, which will be passed in lowercase by WP.
918 return array(
919 'alternating_row_colors' => null,
920 'block_preview' => false,
921 'border' => false,
922 'cache_table_output' => true,
923 'cellpadding' => false,
924 'cellspacing' => false,
925 'column_widths' => '',
926 'convert_line_breaks' => true,
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(),
933 'datatables_paginate' => null,
934 'datatables_paginate_entries' => null,
935 'datatables_scrollx' => null,
936 'datatables_scrolly' => false,
937 'datatables_sort' => null,
938 'evaluate_formulas' => true,
939 'extra_css_classes' => null,
940 'first_column_th' => false,
941 'hide_columns' => '',
942 'hide_rows' => '',
943 'id' => '',
944 'print_description' => null,
945 'print_description_position' => null,
946 'print_name' => null,
947 'print_name_position' => null,
948 'row_hover' => null,
949 'shortcode_debug' => false,
950 'show_columns' => '',
951 'show_rows' => '',
952 'table_foot' => null,
953 'table_head' => null,
954 'use_datatables' => null,
955 );
956 }
957
958 /**
959 * Get the CSS code for the Preview iframe.
960 *
961 * @since 1.0.0
962 *
963 * @return string CSS for the Preview iframe.
964 */
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 = '';
971 }
972
973 $rtl_direction = $is_rtl ? "\ndirection: rtl;" : '';
974
975 return <<<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;
993 }
994
995 } // class TablePress_Render
996