PluginProbe
TablePress – Tables in WordPress made easy / 3.3
TablePress – Tables in WordPress made easy v3.3
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 2.4.4 All 44 releases
tablepress / classes / class-render.php

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

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