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
← All changes | classes/class-export.php +71 -124 1.9.2 → 3.4 View file →
@@ -7,13 +7,16 @@
7 7 * @author Tobias Bäthge
8 8 * @since 1.0.0
9 9 */
10 10
11 +declare(strict_types=1);
12 +
11 13 // Prohibit direct script loading.
12 14 defined( 'ABSPATH' ) || die( 'No direct script access allowed!' );
13 15
14 16 /**
15 17 * TablePress Table Export Class
18 + *
16 19 * @package TablePress
17 20 * @subpackage Export/Import
18 21 * @author Tobias Bäthge
19 22 * @since 1.0.0
@@ -20,43 +23,79 @@
20 23 */
21 24 class TablePress_Export {
22 25
23 26 /**
27 + * Available exporters for the export.
28 + *
29 + * @since 3.4.0
30 + * @var array<string, array{label: string, class: string, file: string, folder: string}>
31 + */
32 + protected array $exporters = array();
33 +
34 + /**
24 35 * File/Data Formats that are available for the export.
25 36 *
26 37 * @since 1.0.0
27 - * @var array
38 + * @var array<string, string>
28 39 */
29 - public $export_formats = array();
40 + public array $export_formats = array();
30 41
31 42 /**
32 43 * Delimiters for the CSV export.
33 44 *
34 45 * @since 1.0.0
35 - * @var array
46 + * @var array<string, string>
36 47 */
37 - public $csv_delimiters = array();
48 + public array $csv_delimiters = array();
38 49
39 50 /**
40 51 * Whether ZIP archive support is available in the PHP installation on the server.
41 52 *
42 53 * @since 1.0.0
43 - * @var bool
44 54 */
45 - public $zip_support_available = false;
55 + public bool $zip_support_available = false;
46 56
47 57 /**
48 - * Initialize the Export class.
58 + * Initializes the Export class.
49 59 *
50 60 * @since 1.0.0
51 61 */
52 62 public function __construct() {
53 63 // Initiate here, because function call not possible outside a class method.
54 - $this->export_formats = array(
55 - 'csv' => __( 'CSV - Character-Separated Values', 'tablepress' ),
56 - 'html' => __( 'HTML - Hypertext Markup Language', 'tablepress' ),
57 - 'json' => __( 'JSON - JavaScript Object Notation', 'tablepress' ),
64 + $this->exporters = array(
65 + 'csv' => array(
66 + 'label' => __( 'CSV - Character-Separated Values', 'tablepress' ),
67 + 'class' => \TablePress\Export\CSV_Exporter::class,
68 + 'file' => 'class-exporter-csv.php',
69 + 'folder' => 'classes/exporters',
70 + ),
71 + 'html' => array(
72 + 'label' => __( 'HTML - Hypertext Markup Language', 'tablepress' ),
73 + 'class' => \TablePress\Export\HTML_Exporter::class,
74 + 'file' => 'class-exporter-html.php',
75 + 'folder' => 'classes/exporters',
76 + ),
77 + 'json' => array(
78 + 'label' => __( 'JSON - JavaScript Object Notation', 'tablepress' ),
79 + 'class' => \TablePress\Export\JSON_Exporter::class,
80 + 'file' => 'class-exporter-json.php',
81 + 'folder' => 'classes/exporters',
82 + ),
58 83 );
84 +
85 + /**
86 + * Filters the available export formats.
87 + *
88 + * @since 3.4.0
89 + *
90 + * @param array<string, array{label: string, class: string, file: string, folder: string}> $exporters Associative array of available export formats.
91 + */
92 + $this->exporters = apply_filters( 'tablepress_exporters', $this->exporters );
93 +
94 + foreach ( $this->exporters as $format => $exporter ) {
95 + $this->export_formats[ $format ] = $exporter['label'];
96 + }
97 +
59 98 $this->csv_delimiters = array(
60 99 ';' => __( '; (semicolon)', 'tablepress' ),
61 100 ',' => __( ', (comma)', 'tablepress' ),
62 101 'tab' => __( '\t (tabulator)', 'tablepress' ),
@@ -61,136 +100,44 @@
61 100 ',' => __( ', (comma)', 'tablepress' ),
62 101 'tab' => __( '\t (tabulator)', 'tablepress' ),
63 102 );
64 103
65 - /** This filter is documented in the WordPress function unzip_file() in wp-admin/includes/file.php */
66 - if ( class_exists( 'ZipArchive', false ) && apply_filters( 'unzip_file_use_ziparchive', true ) ) {
104 + if ( class_exists( 'ZipArchive', false ) ) {
67 105 $this->zip_support_available = true;
68 106 }
69 107 }
70 108
71 109 /**
72 - * Export a table.
110 + * Exports a table to the specified format using the corresponding exporter class.
73 111 *
74 112 * @since 1.0.0
113 + * @since 3.4.0 The $options parameter is now an array. If a string is passed, it is treated as the CSV delimiter (deprecated).
75 114 *
76 - * @param array $table Table to be exported.
77 - * @param string $export_format Format for the export ('csv', 'html', 'json').
78 - * @param string $csv_delimiter Delimiter for CSV export.
79 - * @return string Exported table (only data for CSV and HTML, full tables (including options) for JSON).
115 + * @param array<string, mixed> $table Table to be exported.
116 + * @param string $export_format Format for the export, e.g. 'csv', 'html', or 'json'.
117 + * @param array<string, mixed>|string $options Options for the export if an array, or the CSV delimiter if a string. The latter is deprecated.
118 + * @return string Exported table data.
80 119 */
81 - public function export_table( array $table, $export_format, $csv_delimiter ) {
82 - switch ( $export_format ) {
83 - case 'csv':
84 - $output = '';
85 - if ( 'tab' === $csv_delimiter ) {
86 - $csv_delimiter = "\t";
87 - }
88 - foreach ( $table['data'] as $row_idx => $row ) {
89 - $csv_row = array();
90 - foreach ( $row as $column_idx => $cell_content ) {
91 - $csv_row[] = $this->csv_wrap_and_escape( $cell_content, $csv_delimiter );
92 - }
93 - $output .= implode( $csv_delimiter, $csv_row );
94 - $output .= "\n";
95 - }
96 - break;
97 - case 'html':
98 - $num_rows = count( $table['data'] );
99 - $last_row_idx = $num_rows - 1;
100 - $thead = '';
101 - $tfoot = '';
102 - $tbody = array();
120 + public function export_table( array $table, string $export_format, /* array|string */ $options ): string {
121 + if ( ! isset( $this->exporters[ $export_format ] ) ) {
122 + return '';
123 + }
103 124
104 - foreach ( $table['data'] as $row_idx => $row ) {
105 - // First row, need to check for head (but only if at least two rows).
106 - if ( 0 === $row_idx && $table['options']['table_head'] && $num_rows > 1 ) {
107 - $thead = $this->html_render_row( $row, 'th' );
108 - continue;
109 - }
110 - // Last row, need to check for footer (but only if at least two rows).
111 - if ( $last_row_idx === $row_idx && $table['options']['table_foot'] && $num_rows > 1 ) {
112 - $tfoot = $this->html_render_row( $row, 'th' );
113 - continue;
114 - }
115 - // Neither first nor last row (with respective head/foot enabled), so render as body row.
116 - $tbody[] = $this->html_render_row( $row, 'td' );
117 - }
118 -
119 - // <thead>, <tfoot>, and <tbody> tags.
120 - if ( ! empty( $thead ) ) {
121 - $thead = "\t<thead>\n{$thead}\t</thead>\n";
122 - }
123 - if ( ! empty( $tfoot ) ) {
124 - $tfoot = "\t<tfoot>\n{$tfoot}\t</tfoot>\n";
125 - }
126 - $tbody = "\t<tbody>\n" . implode( '', $tbody ) . "\t</tbody>\n";
127 -
128 - $output = "<table>\n" . $thead . $tfoot . $tbody . "</table>\n";
129 - break;
130 - case 'json':
131 - $output = wp_json_encode( $table, TABLEPRESS_JSON_OPTIONS );
132 - break;
133 - default:
134 - $output = '';
125 + if ( ! is_array( $options ) ) {
126 + $options = array(
127 + 'csv_delimiter' => $options,
128 + );
135 129 }
136 130
137 - return $output;
138 - }
131 + \TablePress::load_file( 'interface-exporter.php', 'classes/exporters' );
139 132
140 - /**
141 - * Wrap and escape a cell for CSV export.
142 - *
143 - * @since 1.0.0
144 - *
145 - * @param string $string Content of a cell.
146 - * @param string $delimiter CSV delimiter character.
147 - * @return string Wrapped string for CSV export
148 - */
149 - protected function csv_wrap_and_escape( $string, $delimiter ) {
150 - // Escape CSV delimiter for RegExp (e.g. '|').
151 - $delimiter = preg_quote( $delimiter, '#' );
152 - if ( 1 === preg_match( '#' . $delimiter . '|"|\n|\r#i', $string ) || ' ' === substr( $string, 0, 1 ) || ' ' === substr( $string, -1 ) ) {
153 - // Escape single " as double "".
154 - $string = str_replace( '"', '""', $string );
155 - // Wrap string in "".
156 - $string = '"' . $string . '"';
133 + if ( '' !== $this->exporters[ $export_format ]['file'] ) {
134 + \TablePress::load_file( $this->exporters[ $export_format ]['file'], $this->exporters[ $export_format ]['folder'] );
157 135 }
158 - return $string;
159 - }
160 136
161 - /**
162 - * Generate the HTML of a row.
163 - *
164 - * @since 1.0.0
165 - *
166 - * @param array $row Cells of the row to be rendered.
167 - * @param string $tag HTML tag to use for the cells (td or th).
168 - * @return string HTML code for the row.
169 - */
170 - protected function html_render_row( array $row, $tag ) {
171 - $output = "\t\t<tr>\n";
172 - array_walk( $row, array( $this, 'html_wrap_and_escape' ), $tag );
173 - $output .= implode( '', $row );
174 - $output .= "\t\t</tr>\n";
175 - return $output;
176 - }
137 + $exporter_class = $this->exporters[ $export_format ]['class'];
138 + $exporter = new $exporter_class();
177 139
178 - /**
179 - * Wrap and escape a cell for HTML export.
180 - *
181 - * @since 1.0.0
182 - *
183 - * @param string $cell_content Content of a cell.
184 - * @param int|null $column_idx Column index, or null if omitted. Unused, but defined to be able to use function as callback in array_walk().
185 - * @param string $html_tag HTML tag that shall be used for the cell.
186 - */
187 - protected function html_wrap_and_escape( &$cell_content, $column_idx, $html_tag ) {
188 - /*
189 - * Replace any & with &amp; that is not already an encoded entity (from function htmlentities2 in WP 2.8).
190 - * A complete htmlentities2() or htmlspecialchars() would encode <HTML> tags, which we don't want.
191 - */
192 - $cell_content = preg_replace( '/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,4};)/', '&amp;', $cell_content );
193 - $cell_content = "\t\t\t<{$html_tag}>{$cell_content}</{$html_tag}>\n";
140 + return $exporter->export( $table, $options );
194 141 }
195 142
196 143 } // class TablePress_Export