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

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

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