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