PluginProbe
TablePress – Tables in WordPress made easy / 3.4
TablePress – Tables in WordPress made easy v3.4
3.4 3.3.4 3.3.3 3.3.2 3.3.1 trunk 1.12 1.14 1.9.2 2.0.4 2.1.7 2.1.8 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3 2.3.1 2.3.2 2.4 2.4.1 2.4.2 2.4.3 All 45 releases
tablepress / classes / exporters / class-exporter-html.php

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

108 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * TablePress HTML Exporter Class
4 *
5 * @package TablePress
6 * @subpackage Export/Import
7 * @author Tobias Bäthge
8 * @since 3.4.0
9 */
10
11 declare(strict_types=1);
12
13 namespace TablePress\Export;
14
15 use TablePress\Export\Exporter_Interface;
16
17 // Prohibit direct script loading.
18 defined( 'ABSPATH' ) || die( 'No direct script access allowed!' );
19
20 /**
21 * TablePress HTML Exporter Class
22 *
23 * @package TablePress
24 * @subpackage Export/Import
25 * @since 3.4.0
26 */
27 class HTML_Exporter implements Exporter_Interface {
28
29 /**
30 * Exports a table to HTML format.
31 *
32 * @since 3.4.0
33 *
34 * @param array<string, mixed> $table Table to be exported.
35 * @param array<string, mixed> $options Format-specific options for the export.
36 * @return string Exported table.
37 */
38 public function export( array $table, array $options = array() ): string {
39 $num_rows = count( $table['data'] );
40 $last_row_idx = $num_rows - 1;
41 $thead = '';
42 $tfoot = '';
43 $tbody = array();
44
45 foreach ( $table['data'] as $row_idx => $row ) {
46 // Table head rows, but only if there's at least one additional row.
47 if ( $row_idx < $table['options']['table_head'] && $num_rows > $table['options']['table_head'] ) {
48 $thead = $this->render_row( $row, 'th' );
49 continue;
50 }
51 // Table foot rows, but only if there's at least one additional row.
52 if ( $row_idx > $last_row_idx - $table['options']['table_foot'] && $num_rows > $table['options']['table_foot'] ) {
53 $tfoot = $this->render_row( $row, 'th' );
54 continue;
55 }
56 // Neither first nor last row (with respective head/foot enabled), so render as body row.
57 $tbody[] = $this->render_row( $row, 'td' );
58 }
59
60 // <thead>, <tbody>, and <tfoot> tags.
61 if ( ! empty( $thead ) ) {
62 $thead = "\t<thead>\n{$thead}\t</thead>\n";
63 }
64 $tbody = "\t<tbody>\n" . implode( '', $tbody ) . "\t</tbody>\n";
65 if ( ! empty( $tfoot ) ) {
66 $tfoot = "\t<tfoot>\n{$tfoot}\t</tfoot>\n";
67 }
68
69 return "<table>\n" . $thead . $tbody . $tfoot . "</table>\n";
70 }
71
72 /**
73 * Generates the HTML code of a row.
74 *
75 * @since 3.4.0
76 *
77 * @param string[] $row Cells of the row to be rendered.
78 * @param string $tag HTML tag to use for the cells (td or th).
79 * @return string HTML code for the row.
80 */
81 protected function render_row( array $row, string $tag ): string {
82 $output = "\t\t<tr>\n";
83 array_walk( $row, array( $this, 'wrap_and_escape' ), $tag );
84 $output .= implode( '', $row );
85 $output .= "\t\t</tr>\n";
86 return $output;
87 }
88
89 /**
90 * Wraps and escapes a cell for HTML export.
91 *
92 * @since 3.4.0
93 *
94 * @param string $cell_content Content of a cell.
95 * @param int $column_idx Column index, or -1 if omitted. Unused, but defined to be able to use function as callback in array_walk().
96 * @param string $html_tag HTML tag that shall be used for the cell.
97 */
98 protected function wrap_and_escape( string &$cell_content, int $column_idx, string $html_tag ): void {
99 /*
100 * Replace any & with &amp; that is not already an encoded entity (from function htmlentities2 in WP 2.8).
101 * A complete htmlentities2() or htmlspecialchars() would encode <HTML> tags, which we don't want.
102 */
103 $cell_content = (string) preg_replace( '/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,4};)/', '&amp;', $cell_content );
104 $cell_content = "\t\t\t<{$html_tag}>{$cell_content}</{$html_tag}>\n";
105 }
106
107 } // class HTML_Exporter
108