PluginProbe
TablePress – Tables in WordPress made easy / 3.2
TablePress – Tables in WordPress made easy v3.2
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
← All changes | classes/class-export.php +31 -27 2.0.43.2 View file →
@@ -24,27 +24,26 @@
24 24 /**
25 25 * File/Data Formats that are available for the export.
26 26 *
27 27 * @since 1.0.0
28 - * @var array
28 + * @var array<string, string>
29 29 */
30 - public $export_formats = array();
30 + public array $export_formats = array();
31 31
32 32 /**
33 33 * Delimiters for the CSV export.
34 34 *
35 35 * @since 1.0.0
36 - * @var array
36 + * @var array<string, string>
37 37 */
38 - public $csv_delimiters = array();
38 + public array $csv_delimiters = array();
39 39
40 40 /**
41 41 * Whether ZIP archive support is available in the PHP installation on the server.
42 42 *
43 43 * @since 1.0.0
44 - * @var bool
45 44 */
46 - public $zip_support_available = false;
45 + public bool $zip_support_available = false;
47 46
48 47 /**
49 48 * Initialize the Export class.
50 49 *
@@ -62,10 +61,9 @@
62 61 ',' => __( ', (comma)', 'tablepress' ),
63 62 'tab' => __( '\t (tabulator)', 'tablepress' ),
64 63 );
65 64
66 - /** This filter is documented in the WordPress function unzip_file() in wp-admin/includes/file.php */
67 - if ( class_exists( 'ZipArchive', false ) && apply_filters( 'unzip_file_use_ziparchive', true ) ) {
65 + if ( class_exists( 'ZipArchive', false ) ) {
68 66 $this->zip_support_available = true;
69 67 }
70 68 }
71 69
@@ -73,14 +71,14 @@
73 71 * Export a table.
74 72 *
75 73 * @since 1.0.0
76 74 *
77 - * @param array $table Table to be exported.
78 - * @param string $export_format Format for the export ('csv', 'html', 'json').
79 - * @param string $csv_delimiter Delimiter for CSV export.
75 + * @param array<string, mixed> $table Table to be exported.
76 + * @param string $export_format Format for the export ('csv', 'html', 'json').
77 + * @param string $csv_delimiter Delimiter for CSV export.
80 78 * @return string Exported table (only data for CSV and HTML, full tables (including options) for JSON).
81 79 */
82 - public function export_table( array $table, $export_format, $csv_delimiter ) {
80 + public function export_table( array $table, string $export_format, string $csv_delimiter ): string {
83 81 switch ( $export_format ) {
84 82 case 'csv':
85 83 $output = '';
86 84 if ( 'tab' === $csv_delimiter ) {
@@ -102,15 +100,15 @@
102 100 $tfoot = '';
103 101 $tbody = array();
104 102
105 103 foreach ( $table['data'] as $row_idx => $row ) {
106 - // First row, need to check for head (but only if at least two rows).
107 - if ( 0 === $row_idx && $table['options']['table_head'] && $num_rows > 1 ) {
104 + // Table head rows, but only if there's at least one additional row.
105 + if ( $row_idx < $table['options']['table_head'] && $num_rows > $table['options']['table_head'] ) {
108 106 $thead = $this->html_render_row( $row, 'th' );
109 107 continue;
110 108 }
111 - // Last row, need to check for footer (but only if at least two rows).
112 - if ( $last_row_idx === $row_idx && $table['options']['table_foot'] && $num_rows > 1 ) {
109 + // Table foot rows, but only if there's at least one additional row.
110 + if ( $row_idx > $last_row_idx - $table['options']['table_foot'] && $num_rows > $table['options']['table_foot'] ) {
113 111 $tfoot = $this->html_render_row( $row, 'th' );
114 112 continue;
115 113 }
116 114 // Neither first nor last row (with respective head/foot enabled), so render as body row.
@@ -129,8 +127,11 @@
129 127 $output = "<table>\n" . $thead . $tfoot . $tbody . "</table>\n";
130 128 break;
131 129 case 'json':
132 130 $output = wp_json_encode( $table, TABLEPRESS_JSON_OPTIONS );
131 + if ( false === $output ) {
132 + $output = '';
133 + }
133 134 break;
134 135 default:
135 136 $output = '';
136 137 }
@@ -146,9 +147,9 @@
146 147 * @param string $cell_content Content of a cell.
147 148 * @param string $delimiter CSV delimiter character.
148 149 * @return string Wrapped string for CSV export.
149 150 */
150 - protected function csv_wrap_and_escape( $cell_content, $delimiter ) {
151 + protected function csv_wrap_and_escape( string $cell_content, string $delimiter ): string {
151 152 // Return early if the cell is empty. No escaping or wrapping is needed then.
152 153 if ( '' === $cell_content ) {
153 154 return $cell_content;
154 155 }
@@ -168,10 +169,13 @@
168 169 'IMAGE(',
169 170 'HYPERLINK(',
170 171 'WEBSERVICE(',
171 172 );
173 +
174 + $fn_stripos = function_exists( 'mb_stripos' ) ? 'mb_stripos' : 'stripos';
175 +
172 176 foreach ( $functions_to_escape as $function ) {
173 - if ( false !== stripos( $cell_content, $function ) ) {
177 + if ( false !== $fn_stripos( $cell_content, $function ) ) {
174 178 $cell_content = "'" . $cell_content; // Prepend a ' to indicate that the cell format is a text string.
175 179 break;
176 180 }
177 181 }
@@ -178,9 +182,9 @@
178 182 }
179 183
180 184 // Escape CSV delimiter for RegExp (e.g. '|').
181 185 $delimiter = preg_quote( $delimiter, '#' );
182 - if ( 1 === preg_match( '#' . $delimiter . '|"|\n|\r#i', $cell_content ) || ' ' === $cell_content[0] || ' ' === substr( $cell_content, -1 ) ) {
186 + if ( 1 === preg_match( '#' . $delimiter . '|"|\n|\r#i', $cell_content ) || str_starts_with( $cell_content, ' ' ) || str_ends_with( $cell_content, ' ' ) ) {
183 187 // Escape single " as double "".
184 188 $cell_content = str_replace( '"', '""', $cell_content );
185 189 // Wrap string in "".
186 190 $cell_content = '"' . $cell_content . '"';
@@ -193,13 +197,13 @@
193 197 * Generate the HTML of a row.
194 198 *
195 199 * @since 1.0.0
196 200 *
197 - * @param array $row Cells of the row to be rendered.
198 - * @param string $tag HTML tag to use for the cells (td or th).
201 + * @param string[] $row Cells of the row to be rendered.
202 + * @param string $tag HTML tag to use for the cells (td or th).
199 203 * @return string HTML code for the row.
200 204 */
201 - protected function html_render_row( array $row, $tag ) {
205 + protected function html_render_row( array $row, string $tag ): string {
202 206 $output = "\t\t<tr>\n";
203 207 array_walk( $row, array( $this, 'html_wrap_and_escape' ), $tag );
204 208 $output .= implode( '', $row );
205 209 $output .= "\t\t</tr>\n";
@@ -210,18 +214,18 @@
210 214 * Wrap and escape a cell for HTML export.
211 215 *
212 216 * @since 1.0.0
213 217 *
214 - * @param string $cell_content Content of a cell.
215 - * @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().
216 - * @param string $html_tag HTML tag that shall be used for the cell.
218 + * @param string $cell_content Content of a cell.
219 + * @param int $column_idx Column index, or -1 if omitted. Unused, but defined to be able to use function as callback in array_walk().
220 + * @param string $html_tag HTML tag that shall be used for the cell.
217 221 */
218 - protected function html_wrap_and_escape( &$cell_content, $column_idx, $html_tag ) {
222 + protected function html_wrap_and_escape( string &$cell_content, int $column_idx, string $html_tag ): void {
219 223 /*
220 224 * Replace any & with &amp; that is not already an encoded entity (from function htmlentities2 in WP 2.8).
221 225 * A complete htmlentities2() or htmlspecialchars() would encode <HTML> tags, which we don't want.
222 226 */
223 - $cell_content = preg_replace( '/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,4};)/', '&amp;', $cell_content );
227 + $cell_content = (string) preg_replace( '/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,4};)/', '&amp;', $cell_content );
224 228 $cell_content = "\t\t\t<{$html_tag}>{$cell_content}</{$html_tag}>\n";
225 229 }
226 230
227 231 } // class TablePress_Export