PluginProbe
TablePress – Tables in WordPress made easy / 3.0
TablePress – Tables in WordPress made easy v3.0
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 | controllers/controller-admin.php +548 -1017 1.9.23.0 View file →
@@ -12,8 +12,9 @@
12 12 defined( 'ABSPATH' ) || die( 'No direct script access allowed!' );
13 13
14 14 /**
15 15 * Admin Controller class, extends Base Controller Class
16 + *
16 17 * @package TablePress
17 18 * @subpackage Controllers
18 19 * @author Tobias Bäthge
19 20 * @since 1.0.0
@@ -24,45 +25,28 @@
24 25 * Page hooks (i.e. names) WordPress uses for the TablePress admin screens,
25 26 * populated in add_admin_menu_entry().
26 27 *
27 28 * @since 1.0.0
28 - * @var array
29 + * @var string[]
29 30 */
30 - protected $page_hooks = array();
31 + protected array $page_hooks = array();
31 32
32 33 /**
33 34 * Actions that have a view and admin menu or nav tab menu entry.
34 35 *
35 36 * @since 1.0.0
36 - * @var array
37 + * @var array<string, array<string, bool|string>>
37 38 */
38 - protected $view_actions = array();
39 + protected array $view_actions = array();
39 40
40 41 /**
41 - * Whether language support has been loaded (to prevent doing it twice).
42 - *
43 - * @since 1.0.0
44 - * @var bool
45 - */
46 - protected $i18n_support_loaded = false;
47 -
48 - /**
49 42 * Instance of the TablePress Admin View that is rendered.
50 43 *
51 44 * @since 1.0.0
52 - * @var TablePress_View
53 45 */
54 - protected $view;
46 + protected \TablePress_View $view;
55 47
56 48 /**
57 - * Instance of the TablePress Importer.
58 - *
59 - * @since 1.0.0
60 - * @var TablePress_Import
61 - */
62 - protected $importer;
63 -
64 - /**
65 49 * Initialize the Admin Controller, determine location the admin menu, set up actions.
66 50 *
67 51 * @since 1.0.0
68 52 */
@@ -69,12 +53,15 @@
69 53 public function __construct() {
70 54 parent::__construct();
71 55
72 56 // Handler for changing the number of shown tables in the list of tables (via WP List Table class).
73 - add_filter( 'set-screen-option', array( $this, 'save_list_tables_screen_option' ), 10, 3 );
57 + add_filter( 'set_screen_option_tablepress_list_per_page', array( $this, 'save_list_tables_screen_option' ), 10, 3 );
74 58
75 59 add_action( 'admin_menu', array( $this, 'add_admin_menu_entry' ) );
76 60 add_action( 'admin_init', array( $this, 'add_admin_actions' ) );
61 +
62 + add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ) );
63 + add_action( 'enqueue_block_assets', array( $this, 'enqueue_block_assets' ) );
77 64 }
78 65
79 66 /**
80 67 * Handler for changing the number of shown tables in the list of tables (via WP List Table class).
@@ -80,19 +67,15 @@
80 67 * Handler for changing the number of shown tables in the list of tables (via WP List Table class).
81 68 *
82 69 * @since 1.0.0
83 70 *
84 - * @param bool $false Current value of the filter (probably bool false).
85 - * @param string $option Option in which the setting is stored.
86 - * @param int $value Current value of the setting.
87 - * @return bool|int False to not save the changed setting, or the int value to be saved.
71 + * @param mixed $screen_option Current value of the filter (probably bool false).
72 + * @param string $option Option in which the setting is stored.
73 + * @param int $value Current value of the setting.
74 + * @return int Changed value of the setting
88 75 */
89 - public function save_list_tables_screen_option( $false, $option, $value ) {
90 - if ( 'tablepress_list_per_page' === $option ) {
91 - return $value;
92 - } else {
93 - return $false;
94 - }
76 + public function save_list_tables_screen_option( /* mixed */ $screen_option, string $option, int $value ): int {
77 + return $value;
95 78 }
96 79
97 80 /**
98 81 * Add admin screens to the correct place in the admin menu.
@@ -98,13 +81,13 @@
98 81 * Add admin screens to the correct place in the admin menu.
99 82 *
100 83 * @since 1.0.0
101 84 */
102 - public function add_admin_menu_entry() {
85 + public function add_admin_menu_entry(): void {
103 86 // Callback for all menu entries.
104 87 $callback = array( $this, 'show_admin_page' );
105 88 /**
106 - * Filter the TablePress admin menu entry name.
89 + * Filters the TablePress admin menu entry name.
107 90 *
108 91 * @since 1.0.0
109 92 *
110 93 * @param string $entry_name The admin menu entry name. Default "TablePress".
@@ -110,28 +93,30 @@
110 93 * @param string $entry_name The admin menu entry name. Default "TablePress".
111 94 */
112 95 $admin_menu_entry_name = apply_filters( 'tablepress_admin_menu_entry_name', 'TablePress' );
113 96
114 - if ( $this->is_top_level_page ) {
115 - // Init i18n support here as translated strings for admin menu are needed already.
116 - $this->init_i18n_support();
117 - $this->init_view_actions(); // after init_i18n_support(), as it requires translation
118 - $min_access_cap = $this->view_actions['list']['required_cap'];
97 + $this->init_view_actions();
98 + $min_access_cap = $this->view_actions['list']['required_cap'];
119 99
120 - $icon_url = 'dashicons-list-view';
121 - switch ( $this->parent_page ) {
100 + if ( TablePress::$controller->is_top_level_page ) {
101 + $icon_url = 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyMCIgaGVpZ2h0PSIyMCIgdmlld0JveD0iLTMyIC0zMiA2NCA2NCIgZmlsbD0iI2ZmZiI+PHBhdGggZD0iTTAtMjUuODU0aC0yNS44NTR2NTEuNzA4aDUxLjcwOFYwSDIxdjIxaC00MnYtNDJIMFoiLz48cGF0aCBkPSJNLTE4LTE4aDEwdjEwaC0xMHpNLTE4LTVoMTBWNWgtMTB6TS01LTVINVY1SC01ek0tMTggOGgxMHYxMGgtMTB6TS01IDhINXYxMEgtNXpNOCA4aDEwdjEwSDh6TTUtMzFoNi4xOHY2LjE4SDV6TTE5LTI1aDYuMTh2Ni4xOEgxOXpNMC0xNWgzLjgydjMuODJIMHpNMTAtMjBoMy44MnYzLjgySDEwek0yNS0xMmgzLjgydjMuODJIMjV6TTgtMTNoMTB2MTBIOHoiLz48L3N2Zz4=';
102 + switch ( TablePress::$controller->parent_page ) {
122 103 case 'top':
123 - $position = 3; // position of Dashboard + 1
104 + $position = 3; // Position of Dashboard + 1.
124 105 break;
125 106 case 'bottom':
126 - $position = ( ++$GLOBALS['_wp_last_utility_menu'] );
107 + $position = isset( $GLOBALS['_wp_last_utility_menu'] ) ? ++$GLOBALS['_wp_last_utility_menu'] : 80;
127 108 break;
128 109 case 'middle':
129 110 default:
130 - $position = ( ++$GLOBALS['_wp_last_object_menu'] );
111 + $position = isset( $GLOBALS['_wp_last_object_menu'] ) ? ++$GLOBALS['_wp_last_object_menu'] : 25;
131 112 break;
132 113 }
133 - add_menu_page( 'TablePress', $admin_menu_entry_name, $min_access_cap, 'tablepress', $callback, $icon_url, $position );
114 + // Prevent overwriting existing menu entries.
115 + while ( isset( $GLOBALS['menu'][ $position ] ) ) {
116 + ++$position;
117 + }
118 + add_menu_page( 'TablePress', $admin_menu_entry_name, $min_access_cap, 'tablepress', $callback, $icon_url, $position ); // @phpstan-ignore argument.type
134 119 foreach ( $this->view_actions as $action => $entry ) {
135 120 if ( ! $entry['show_entry'] ) {
136 121 continue;
137 122 }
@@ -138,14 +123,20 @@
138 123 $slug = 'tablepress';
139 124 if ( 'list' !== $action ) {
140 125 $slug .= '_' . $action;
141 126 }
142 - $this->page_hooks[] = add_submenu_page( 'tablepress', sprintf( __( '%1$s &lsaquo; %2$s', 'tablepress' ), $entry['page_title'], 'TablePress' ), $entry['admin_menu_title'], $entry['required_cap'], $slug, $callback );
127 + // @phpstan-ignore argument.type, argument.type
128 + $page_hook = add_submenu_page( 'tablepress', sprintf( __( '%1$s &lsaquo; %2$s', 'tablepress' ), $entry['page_title'], 'TablePress' ), $entry['admin_menu_title'], $entry['required_cap'], $slug, $callback );
129 + if ( false !== $page_hook ) {
130 + $this->page_hooks[] = $page_hook;
131 + }
143 132 }
144 133 } else {
145 - $this->init_view_actions(); // no translation necessary here
146 - $min_access_cap = $this->view_actions['list']['required_cap'];
147 - $this->page_hooks[] = add_submenu_page( $this->parent_page, 'TablePress', $admin_menu_entry_name, $min_access_cap, 'tablepress', $callback );
134 + // @phpstan-ignore argument.type
135 + $page_hook = add_submenu_page( TablePress::$controller->parent_page, 'TablePress', $admin_menu_entry_name, $min_access_cap, 'tablepress', $callback );
136 + if ( false !== $page_hook ) {
137 + $this->page_hooks[] = $page_hook;
138 + }
148 139 }
149 140 }
150 141
151 142 /**
@@ -152,11 +143,11 @@
152 143 * Set up handlers for user actions in the backend that exceed plain viewing.
153 144 *
154 145 * @since 1.0.0
155 146 */
156 - public function add_admin_actions() {
147 + public function add_admin_actions(): void {
157 148 // Register the callbacks for processing action requests.
158 - $post_actions = array( 'list', 'add', 'edit', 'options', 'export', 'import' );
149 + $post_actions = array( 'list', 'add', 'options', 'export', 'import' );
159 150 $get_actions = array( 'hide_message', 'delete_table', 'copy_table', 'preview_table', 'editor_button_thickbox', 'uninstall_tablepress' );
160 151 foreach ( $post_actions as $action ) {
161 152 add_action( "admin_post_tablepress_{$action}", array( $this, "handle_post_action_{$action}" ) );
162 153 }
@@ -168,11 +159,20 @@
168 159 foreach ( $this->page_hooks as $page_hook ) {
169 160 add_action( "load-{$page_hook}", array( $this, 'load_admin_page' ) );
170 161 }
171 162
172 - $pages_with_editor_button = array( 'post.php', 'post-new.php' );
173 - foreach ( $pages_with_editor_button as $editor_page ) {
174 - add_action( "load-{$editor_page}", array( $this, 'add_editor_buttons' ) );
163 + /**
164 + * Filters whether the legacy editor button should be loaded on the post editing screen.
165 + *
166 + * @since 2.1.0
167 + *
168 + * @param bool $load_button Whether to load the legacy editor button. Default true.
169 + */
170 + if ( apply_filters( 'tablepress_add_legacy_editor_button', true ) ) {
171 + $pages_with_editor_button = array( 'post.php', 'post-new.php' );
172 + foreach ( $pages_with_editor_button as $editor_page ) {
173 + add_action( "load-{$editor_page}", array( $this, 'add_editor_buttons' ) );
174 + }
175 175 }
176 176
177 177 if ( ! is_network_admin() && ! is_user_admin() ) {
178 178 add_action( 'admin_bar_menu', array( $this, 'add_wp_admin_bar_new_content_menu_entry' ), 71 );
@@ -178,99 +178,187 @@
178 178 add_action( 'admin_bar_menu', array( $this, 'add_wp_admin_bar_new_content_menu_entry' ), 71 );
179 179 }
180 180
181 181 add_action( 'load-plugins.php', array( $this, 'plugins_page' ) );
182 - add_action( 'admin_print_styles-media-upload-popup', array( $this, 'add_media_upload_thickbox_css' ) );
182 + }
183 183
184 - // Add filters and actions for the integration into the WP WXR exporter and importer.
185 - add_action( 'wp_import_insert_post', array( TablePress::$model_table, 'add_table_id_on_wp_import' ), 10, 4 );
186 - add_filter( 'wp_import_post_meta', array( TablePress::$model_table, 'prevent_table_id_post_meta_import_on_wp_import' ), 10, 3 );
187 - add_filter( 'wxr_export_skip_postmeta', array( TablePress::$model_table, 'add_table_id_to_wp_export' ), 10, 3 );
184 + /**
185 + * Loads additional JavaScript code for the TablePress table block (in the block editor context).
186 + *
187 + * @since 2.2.0
188 + */
189 + public function enqueue_block_editor_assets(): void {
190 + /*
191 + * Register the `react-jsx-runtime` polyfill, if it is not already registered.
192 + * This is needed as a polyfill for WP < 6.6, and can be removed once WP 6.6 is the minimum requirement for TablePress.
193 + */
194 + if ( ! wp_script_is( 'react-jsx-runtime', 'registered' ) ) {
195 + wp_register_script( 'react-jsx-runtime', plugins_url( 'admin/js/react-jsx-runtime.min.js', TABLEPRESS__FILE__ ), array( 'react' ), TablePress::version, true );
196 + }
197 +
198 + // Add table information for the block editor to the page.
199 + $handle = generate_block_asset_handle( 'tablepress/table', 'editorScript' );
200 + $data = $this->get_block_editor_data();
201 + wp_add_inline_script( $handle, $data, 'before' );
188 202 }
189 203
190 204 /**
205 + * Loads additional CSS code for the TablePress table block (inside the block editor iframe).
206 + *
207 + * @since 2.2.0
208 + */
209 + public function enqueue_block_assets(): void {
210 + // Load the TablePress default CSS and the user's "Custom CSS" in the block editor iframe.
211 + if ( is_admin() ) {
212 + TablePress::$controller->enqueue_css();
213 + }
214 + }
215 +
216 + /**
217 + * Gets the inline data that is referenced by the Block Editor JavaScript code for the TablePress blocks.
218 + *
219 + * @since 2.0.0
220 + *
221 + * @return string JavaScript code for the Block Editor.
222 + */
223 + protected function get_block_editor_data(): string {
224 + $tables = array();
225 + // Load all table IDs without priming the post meta cache, as table options/visibility are not needed.
226 + $table_ids = TablePress::$model_table->load_all( false );
227 + foreach ( $table_ids as $table_id ) {
228 + // Load table, without table data, options, and visibility settings.
229 + $table = TablePress::$model_table->load( $table_id, false, false );
230 +
231 + // Skip tables that could not be loaded.
232 + if ( is_wp_error( $table ) ) {
233 + continue;
234 + }
235 +
236 + if ( '' === trim( $table['name'] ) ) {
237 + $table['name'] = __( '(no name)', 'tablepress' );
238 + }
239 + $tables[ $table_id ] = esc_html( $table['name'] );
240 + }
241 +
242 + /**
243 + * Filters the list of table IDs and names that is passed to the block editor, and is then used in the dropdown of the TablePress table block.
244 + *
245 + * @since 2.0.0
246 + *
247 + * @param array<string, string> $tables List of table names, the table ID is the array key.
248 + */
249 + $tables = apply_filters( 'tablepress_block_editor_tables_list', $tables );
250 +
251 + $tables = wp_json_encode( $tables, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES );
252 + if ( false === $tables ) {
253 + // JSON encoding failed, return an error object. Use a prefixed "_error" key to avoid conflicts with intentionally added "error" keys.
254 + $tables = '{ "_error": "The data could not be encoded to JSON!" }';
255 + }
256 + // Print the JSON data inside a `JSON.parse()` call in JS for speed gains, with necessary escaping of `\` and `'`.
257 + $tables = str_replace( array( '\\', "'" ), array( '\\\\', "\'" ), $tables );
258 +
259 + $shortcode = esc_js( TablePress::$shortcode );
260 +
261 + $template = TablePress::$model_table->get_table_template();
262 + $template = wp_json_encode( $template['options'], JSON_HEX_TAG | JSON_UNESCAPED_SLASHES );
263 + if ( false === $template ) {
264 + // JSON encoding failed, return an error object. Use a prefixed "_error" key to avoid conflicts with intentionally added "error" keys.
265 + $template = '{ "_error": "The data could not be encoded to JSON!" }';
266 + }
267 + // Print the JSON data inside a `JSON.parse()` call in JS for speed gains, with necessary escaping of `\` and `'`.
268 + $template = str_replace( array( '\\', "'" ), array( '\\\\', "\'" ), $template );
269 +
270 + /**
271 + * Filters whether the table block preview should be loaded via a <ServerSideRender> in the block editor.
272 + *
273 + * @since 2.0.0
274 + *
275 + * @param bool $load_block_preview Whether the table block preview should be loaded.
276 + */
277 + $load_block_preview = apply_filters( 'tablepress_show_block_editor_preview', true );
278 + $load_block_preview = (bool) $load_block_preview ? 'true' : 'false';
279 +
280 + $url = '';
281 + if ( current_user_can( 'tablepress_list_tables' ) ) {
282 + $url = TablePress::url( array( 'action' => 'list' ) );
283 + }
284 +
285 + return <<<JS
286 + // Ensure the global `tp` object exists.
287 + window.tp = window.tp || {};
288 + tp.url = '{$url}';
289 + tp.load_block_preview = {$load_block_preview};
290 + tp.table = {};
291 + tp.table.shortcode = '{$shortcode}';
292 + tp.table.template = JSON.parse( '{$template}' );
293 + tp.tables = JSON.parse( '{$tables}' );
294 + JS;
295 + }
296 +
297 + /**
191 298 * Register actions to add "Table" button to "HTML editor" and "Visual editor" toolbars.
192 299 *
193 300 * @since 1.0.0
194 301 */
195 - public function add_editor_buttons() {
302 + public function add_editor_buttons(): void {
196 303 if ( ! current_user_can( 'tablepress_list_tables' ) ) {
197 304 return;
198 305 }
199 306
200 - $this->init_i18n_support();
201 - add_thickbox(); // usually already loaded by media upload functions
307 + // Only load the toolbar integration if the Block Editor is not used.
308 + if ( TablePress::site_uses_block_editor() ) {
309 + return;
310 + }
311 +
312 + add_thickbox(); // The files are usually already loaded by media upload functions.
202 313 $admin_page = TablePress::load_class( 'TablePress_Admin_Page', 'class-admin-page-helper.php', 'classes' );
203 - $admin_page->enqueue_script( 'quicktags-button', array( 'quicktags', 'media-upload' ), array(
204 - 'editor_button' => array(
205 - 'caption' => __( 'Table', 'tablepress' ),
206 - 'title' => __( 'Insert a Table from TablePress', 'tablepress' ),
207 - 'thickbox_title' => __( 'Insert a Table from TablePress', 'tablepress' ),
208 - 'thickbox_url' => TablePress::url( array( 'action' => 'editor_button_thickbox' ), true, 'admin-post.php' ),
314 + $admin_page->enqueue_script(
315 + 'quicktags-button',
316 + array( 'quicktags', 'media-upload' ),
317 + array(
318 + 'editor_button' => array(
319 + 'caption' => __( 'Table', 'tablepress' ),
320 + 'title' => __( 'Insert a TablePress table', 'tablepress' ),
321 + 'thickbox_title' => __( 'Insert a TablePress table', 'tablepress' ),
322 + 'thickbox_url' => TablePress::url( array( 'action' => 'editor_button_thickbox' ), true, 'admin-post.php' ),
323 + ),
209 324 ),
210 - ) );
325 + );
211 326
212 327 // TinyMCE integration.
213 328 if ( user_can_richedit() ) {
214 329 add_filter( 'mce_external_plugins', array( $this, 'add_tinymce_plugin' ) );
215 330 add_filter( 'mce_buttons', array( $this, 'add_tinymce_button' ) );
216 - add_action( 'admin_print_styles', array( $this, 'add_tablepress_hidpi_css' ), 21 );
217 331 }
218 332 }
219 333
220 334 /**
221 - * Add "Table" button and separator to the TinyMCE toolbar.
335 + * Adds the "Table" button to the TinyMCE toolbar.
222 336 *
223 337 * @since 1.0.0
224 338 *
225 - * @param array $buttons Current set of buttons in the TinyMCE toolbar.
226 - * @return array Current set of buttons in the TinyMCE toolbar, including "Table" button.
339 + * @param string[] $buttons Current set of buttons in the TinyMCE toolbar.
340 + * @return string[] Extended set of buttons in the TinyMCE toolbar, including the "Table" button.
227 341 */
228 - public function add_tinymce_button( array $buttons ) {
342 + public function add_tinymce_button( array $buttons ): array {
229 343 $buttons[] = 'tablepress_insert_table';
230 344 return $buttons;
231 345 }
232 346
233 347 /**
234 - * Register "Table" button plugin to TinyMCE.
348 + * Registers the "Table" button plugin for the TinyMCE editor.
235 349 *
236 350 * @since 1.0.0
237 351 *
238 - * @param array $plugins Current set of registered TinyMCE plugins.
239 - * @return array Current set of registered TinyMCE plugins, including "Table" button plugin.
352 + * @param array<string, string> $plugins Current set of registered TinyMCE plugins.
353 + * @return array<string, string> Extended set of registered TinyMCE plugins, including the "Table" button plugin.
240 354 */
241 - public function add_tinymce_plugin( array $plugins ) {
242 - $suffix = SCRIPT_DEBUG ? '' : '.min';
243 - $js_file = "admin/js/tinymce-button{$suffix}.js";
244 - $plugins['tablepress_tinymce'] = plugins_url( $js_file, TABLEPRESS__FILE__ );
355 + public function add_tinymce_plugin( array $plugins ): array {
356 + $plugins['tablepress_tinymce'] = plugins_url( 'admin/js/build/tinymce-button.js', TABLEPRESS__FILE__ );
245 357 return $plugins;
246 358 }
247 359
248 360 /**
249 - * Print TablePress HiDPI CSS to the <head> for TinyMCE button.
250 - *
251 - * @since 1.0.0
252 - */
253 - public function add_tablepress_hidpi_css() {
254 - echo '<style type="text/css">@media print,(-webkit-min-device-pixel-ratio:1.25),(min-resolution:120dpi){';
255 - echo '#content_tablepress_insert_table span{background:url(' . plugins_url( 'admin/img/tablepress-editor-button-2x.png', TABLEPRESS__FILE__ ) . ') no-repeat 0 0;background-size:20px 20px}';
256 - echo '#content_tablepress_insert_table img{display:none}';
257 - echo '}</style>' . "\n";
258 - }
259 -
260 - /**
261 - * Print some CSS in the Media Upload Thickbox to fix some positioning issues.
262 - *
263 - * These will most likely not be fixed in core, as the old media uploader is deprecated.
264 - * They will be removed in TablePress, once the new media uploader is used.
265 - *
266 - * @since 1.4.0
267 - */
268 - public function add_media_upload_thickbox_css() {
269 - echo '<style type="text/css">#media-items,#media-upload #filter{width:auto!important}.media-item .describe input[type="text"],.media-item .describe textarea{width:100%!important}.media-item .image-editor input[type="text"]{width:3em!important}</style>' . "\n";
270 - }
271 -
272 - /**
273 361 * Add "TablePress Table" entry to "New" dropdown menu in the WP Admin Bar.
274 362 *
275 363 * @since 1.0.0
276 364 *
@@ -275,14 +363,18 @@
275 363 * @since 1.0.0
276 364 *
277 365 * @param WP_Admin_Bar $wp_admin_bar The current WP Admin Bar object.
278 366 */
279 - public function add_wp_admin_bar_new_content_menu_entry( $wp_admin_bar ) {
367 + public function add_wp_admin_bar_new_content_menu_entry( WP_Admin_Bar $wp_admin_bar ): void {
280 368 if ( ! current_user_can( 'tablepress_add_tables' ) ) {
281 369 return;
282 370 }
283 - // @TODO: Translation might not work, as textdomain might not yet be loaded here (for submenu entries).
284 - // Might need $this->init_i18n_support(); here.
371 +
372 + // Don't load TablePress assets on the Freemius opt-in/activation screen.
373 + if ( tb_tp_fs()->is_activation_mode() && tb_tp_fs()->is_activation_page() ) {
374 + return;
375 + }
376 +
285 377 $wp_admin_bar->add_menu( array(
286 378 'parent' => 'new-content',
287 379 'id' => 'new-tablepress-table',
288 380 'title' => __( 'TablePress Table', 'tablepress' ),
@@ -294,13 +386,24 @@
294 386 * Handle actions for loading of Plugins page.
295 387 *
296 388 * @since 1.0.0
297 389 */
298 - public function plugins_page() {
299 - $this->init_i18n_support();
390 + public function plugins_page(): void {
300 391 // Add additional links on Plugins page.
301 392 add_filter( 'plugin_action_links_' . TABLEPRESS_BASENAME, array( $this, 'add_plugin_action_links' ) );
302 393 add_filter( 'plugin_row_meta', array( $this, 'add_plugin_row_meta' ), 10, 2 );
394 + $incompatible_superseded_extensions = array(
395 + 'tablepress-datatables-alphabetsearch/tablepress-datatables-alphabetsearch.php',
396 + 'tablepress-datatables-column-filter-widgets/tablepress-datatables-column-filter-widgets.php',
397 + 'tablepress-datatables-columnfilter/tablepress-datatables-columnfilter.php',
398 + 'tablepress-datatables-fixedcolumns/tablepress-datatables-fixedcolumns.php',
399 + 'tablepress-datatables-row-details/tablepress-datatables-row-details.php',
400 + 'tablepress-datatables-rowgroup/tablepress-datatables-rowgroup.php',
401 + 'tablepress-responsive-tables/tablepress-responsive-tables.php',
402 + );
403 + foreach ( $incompatible_superseded_extensions as $plugin_file ) {
404 + add_action( "after_plugin_row_{$plugin_file}", array( $this, 'add_superseded_extension_meta_row' ), 10, 3 );
405 + }
303 406 }
304 407
305 408 /**
306 409 * Add links to the TablePress entry in the "Plugin" column on the Plugins page.
@@ -306,73 +409,128 @@
306 409 * Add links to the TablePress entry in the "Plugin" column on the Plugins page.
307 410 *
308 411 * @since 1.0.0
309 412 *
310 - * @param array $links List of links to print in the "Plugin" column on the Plugins page.
311 - * @return array Extended list of links to print in the "Plugin" column on the Plugins page.
413 + * @param string[] $links List of links to print in the "Plugin" column on the Plugins page.
414 + * @return string[] Extended list of links to print in the "Plugin" column on the Plugins page.
312 415 */
313 - public function add_plugin_action_links( array $links ) {
416 + public function add_plugin_action_links( array $links ): array {
314 417 if ( current_user_can( 'tablepress_list_tables' ) ) {
315 - $links[] = '<a href="' . TablePress::url() . '">' . __( 'Plugin page', 'tablepress' ) . '</a>';
418 + $links[] = '<a href="' . esc_url( TablePress::url() ) . '">' . __( 'Plugin page', 'tablepress' ) . '</a>';
316 419 }
317 420 return $links;
318 421 }
422 +
319 423 /**
320 424 * Add links to the TablePress entry in the "Description" column on the Plugins page.
321 425 *
322 426 * @since 1.0.0
323 427 *
324 - * @param array $links List of links to print in the "Description" column on the Plugins page.
325 - * @param string $file Name of the plugin.
326 - * @return array Extended list of links to print in the "Description" column on the Plugins page.
428 + * @param string[] $links List of links to print in the "Description" column on the Plugins page.
429 + * @param string $file Name of the plugin.
430 + * @return string[] Extended list of links to print in the "Description" column on the Plugins page.
327 431 */
328 - public function add_plugin_row_meta( array $links, $file ) {
432 + public function add_plugin_row_meta( array $links, string $file ): array {
329 433 if ( TABLEPRESS_BASENAME === $file ) {
330 434 $links[] = '<a href="https://tablepress.org/faq/" title="' . esc_attr__( 'Frequently Asked Questions', 'tablepress' ) . '">' . __( 'FAQ', 'tablepress' ) . '</a>';
331 435 $links[] = '<a href="https://tablepress.org/documentation/">' . __( 'Documentation', 'tablepress' ) . '</a>';
332 436 $links[] = '<a href="https://tablepress.org/support/">' . __( 'Support', 'tablepress' ) . '</a>';
333 - $links[] = '<a href="https://tablepress.org/donate/" title="' . esc_attr__( 'Support TablePress with your donation!', 'tablepress' ) . '"><strong>' . __( 'Donate', 'tablepress' ) . '</strong></a>';
437 + if ( ! TABLEPRESS_IS_PLAYGROUND_PREVIEW && tb_tp_fs()->is_free_plan() ) {
438 + $links[] = '<a href="https://tablepress.org/premium/?utm_source=plugin&utm_medium=textlink&utm_content=plugins-screen" title="' . esc_attr__( 'Check out the Premium version of TablePress!', 'tablepress' ) . '"><strong>' . __( 'Go Premium', 'tablepress' ) . '</strong></a>';
439 + }
334 440 }
335 441 return $links;
336 442 }
337 443
338 444 /**
445 + * Prints a superseded extension notice below certain TablePress Extension plugins' meta rows on the "Plugins" screen.
446 + *
447 + * @since 2.4.1
448 + *
449 + * @param string $plugin_file Path to the plugin file relative to the plugins directory.
450 + * @param array<int, string|string[]|bool> $plugin_data An array of plugin data.
451 + * @param string $status Status filter currently applied to the plugin list.
452 + */
453 + public function add_superseded_extension_meta_row( string $plugin_file, array $plugin_data, string $status ): void {
454 + if ( ! is_plugin_active( $plugin_file ) ) {
455 + return;
456 + }
457 + ?>
458 + <tr class="plugin-update-tr active">
459 + <td colspan="<?php echo esc_attr( $GLOBALS['wp_list_table']->get_column_count() ); ?>" class="plugin-update colspanchange">
460 + <div class="update-message notice inline notice-error notice-alt">
461 + <?php
462 + if ( tb_tp_fs()->is_free_plan() ) {
463 + echo '<p style="font-size:14px;">';
464 + _e( 'This TablePress Extension was retired.', 'tablepress' );
465 + echo ' ';
466 + _e( '<strong>The plugin does no longer work with TablePress 3</strong> and will no longer receive updates or support!', 'tablepress' );
467 + echo '<br>';
468 + _e( 'Keeping it activated can lead to errors on your website!', 'tablepress' );
469 + echo ' <strong>' . sprintf( __( '<a href="%s">Find out what you can do to continue using its features!</a>', 'tablepress' ), 'https://tablepress.org/upgrade-extensions/?utm_source=plugin&utm_medium=textlink&utm_content=plugins-list-table' ) . '</strong>';
470 + echo '</p>';
471 + }
472 + ?>
473 + <style>
474 + /* Remove the separator line between the plugin's and the notice's table row. */
475 + .plugins .active[data-plugin="<?php echo $plugin_file; ?>"] th,
476 + .plugins .active[data-plugin="<?php echo $plugin_file; ?>"] td {
477 + box-shadow: none;
478 + }
479 + /* Hide the plugin update row for the Extension as those won't work anymore anyways. */
480 + .plugins .plugin-update-tr[data-plugin="<?php echo $plugin_file; ?>"] {
481 + display: none;
482 + }
483 + </style>
484 + </div>
485 + </td>
486 + </tr>
487 + <?php
488 + }
489 +
490 + /**
339 491 * Prepare the rendering of an admin screen, by determining the current action, loading necessary data and initializing the view.
340 492 *
341 493 * @since 1.0.0
342 494 */
343 - public function load_admin_page() {
495 + public function load_admin_page(): void {
344 496 // Determine the action from either the GET parameter (for sub-menu entries, and the main admin menu entry).
345 - $action = ( ! empty( $_GET['action'] ) ) ? $_GET['action'] : 'list'; // default action is list
346 - if ( $this->is_top_level_page ) {
497 + $action = ( ! empty( $_GET['action'] ) ) ? $_GET['action'] : 'list'; // Default action is list.
498 + if ( TablePress::$controller->is_top_level_page ) {
347 499 // Or, for sub-menu entry of an admin menu "TablePress" entry, get it from the "page" GET parameter.
348 500 if ( 'tablepress' !== $_GET['page'] ) {
349 501 // Actions that are top-level entries, but don't have an action GET parameter (action is after last _ in string).
350 502 $action = substr( $_GET['page'], 11 ); // $_GET['page'] has the format 'tablepress_{$action}'
351 503 }
352 - } else {
353 - // Do this here in the else-part, instead of adding another if ( ! $this->is_top_level_page ) check.
354 - $this->init_i18n_support(); // done here, as for sub menu admin pages this is the first time translated strings are needed.
355 - $this->init_view_actions(); // for top-level menu entries, this has been done above, just like init_i18n_support().
356 504 }
357 505
358 506 // Check if action is a supported action, and whether the user is allowed to access this screen.
359 - if ( ! isset( $this->view_actions[ $action ] ) || ! current_user_can( $this->view_actions[ $action ]['required_cap'] ) ) {
360 - wp_die( __( 'You do not have sufficient permissions to access this page.', 'default' ), 403 );
507 + if ( ! isset( $this->view_actions[ $action ] ) || ! current_user_can( $this->view_actions[ $action ]['required_cap'] ) ) { // @phpstan-ignore argument.type (The array value for the capability is always a string.)
508 + wp_die( __( 'Sorry, you are not allowed to access this page.', 'default' ), 403 );
361 509 }
362 510
511 + // Don't load TablePress assets on the Freemius opt-in/activation screen.
512 + if ( tb_tp_fs()->is_activation_mode() && tb_tp_fs()->is_activation_page() ) {
513 + return;
514 + }
515 +
363 516 // Changes current screen ID and pagenow variable in JS, to enable automatic meta box JS handling.
364 517 set_current_screen( "tablepress_{$action}" );
365 - // Set the $typenow global to the current CPT ourselves, as WP_Screen::get() does not determine the CPT correctly.
366 - // This is necessary as the WP Admin Menu can otherwise highlight wrong entries, see https://github.com/TobiasBg/TablePress/issues/24.
518 +
519 + /*
520 + * Set the `$typenow` global to the current CPT ourselves, as `WP_Screen::get()` does not determine the CPT correctly.
521 + * This is necessary as the WP Admin Menu can otherwise highlight wrong entries, see https://github.com/TablePress/TablePress/issues/24.
522 + */
367 523 if ( isset( $_GET['post_type'] ) && post_type_exists( $_GET['post_type'] ) ) {
368 - $GLOBALS['typenow'] = $_GET['post_type'];
524 + $GLOBALS['typenow'] = $_GET['post_type']; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
369 525 }
370 526
371 527 // Pre-define some view data.
372 528 $data = array(
373 - 'view_actions' => $this->view_actions,
374 - 'message' => ( ! empty( $_GET['message'] ) ) ? $_GET['message'] : false,
529 + 'view_actions' => $this->view_actions,
530 + 'message' => ( ! empty( $_GET['message'] ) ) ? $_GET['message'] : false,
531 + 'error_details' => ( ! empty( $_GET['error_details'] ) ) ? $_GET['error_details'] : '',
532 + 'site_uses_block_editor' => TablePress::site_uses_block_editor(),
375 533 );
376 534
377 535 // Depending on the action, load more necessary data for the corresponding view.
378 536 switch ( $action ) {
@@ -379,30 +537,25 @@
379 537 case 'list':
380 538 $data['table_id'] = ( ! empty( $_GET['table_id'] ) ) ? $_GET['table_id'] : false;
381 539 // Prime the post meta cache for cached loading of last_editor.
382 540 $data['table_ids'] = TablePress::$model_table->load_all( true );
383 - $data['messages']['first_visit'] = TablePress::$model_options->get( 'message_first_visit' );
384 - if ( current_user_can( 'tablepress_import_tables_wptr' ) ) {
385 - // Check if WP-Table Reloaded is activated.
386 - $data['messages']['wp_table_reloaded_warning'] = is_plugin_active( 'wp-table-reloaded/wp-table-reloaded.php' );
387 - } else {
388 - $data['messages']['wp_table_reloaded_warning'] = false;
389 - }
390 - $data['messages']['plugin_update_message'] = TablePress::$model_options->get( 'message_plugin_update' );
391 - $data['messages']['donation_message'] = $this->maybe_show_donation_message();
541 + $data['messages']['donation_nag'] = $this->maybe_show_donation_message();
542 + $data['messages']['first_visit'] = ! $data['messages']['donation_nag'] && TablePress::$model_options->get( 'message_first_visit' );
543 + $data['messages']['plugin_update'] = TablePress::$model_options->get( 'message_plugin_update' );
544 + $data['messages']['superseded_extensions'] = current_user_can( 'manage_options' ) && TablePress::$model_options->get( 'message_superseded_extensions' );
392 545 $data['table_count'] = count( $data['table_ids'] );
393 546 break;
394 547 case 'about':
395 548 $data['first_activation'] = TablePress::$model_options->get( 'first_activation' );
396 - $exporter = TablePress::load_class( 'TablePress_Export', 'class-export.php', 'classes' );
397 - $data['zip_support_available'] = $exporter->zip_support_available;
398 549 break;
399 550 case 'options':
400 - // Maybe try saving "Custom CSS" to a file:
401 - // (called here, as the credentials form posts to this handler again, due to how request_filesystem_credentials() works)
551 + /*
552 + * Maybe try saving "Custom CSS" to a file:
553 + * (called here, as the credentials form posts to this handler again, due to how `request_filesystem_credentials()` works)
554 + */
402 555 if ( isset( $_GET['item'] ) && 'save_custom_css' === $_GET['item'] ) {
403 556 TablePress::check_nonce( 'options', $_GET['item'] ); // Nonce check here, as we don't have an explicit handler, and even viewing the screen needs to be checked.
404 - $action = 'options_custom_css'; // to load a different view
557 + $action = 'options_custom_css'; // To load a different view.
405 558 // Try saving "Custom CSS" to a file, otherwise this gets the HTML for the credentials form.
406 559 $tablepress_css = TablePress::load_class( 'TablePress_CSS', 'class-css.php', 'classes' );
407 560 $result = $tablepress_css->save_custom_css_to_file_plugin_options( TablePress::$model_options->get( 'custom_css' ), TablePress::$model_options->get( 'custom_css_minified' ) );
408 561 if ( is_string( $result ) ) {
@@ -416,9 +569,9 @@
416 569 'use_custom_css_file' => true,
417 570 'custom_css_version' => TablePress::$model_options->get( 'custom_css_version' ) + 1,
418 571 ) );
419 572 TablePress::redirect( array( 'action' => 'options', 'message' => 'success_save' ) );
420 - } else { // leaves only $result = false
573 + } else { // Leaves only $result === false.
421 574 TablePress::redirect( array( 'action' => 'options', 'message' => 'success_save_error_custom_css' ) );
422 575 }
423 576 break;
424 577 }
@@ -423,68 +576,88 @@
423 576 break;
424 577 }
425 578 $data['frontend_options']['use_custom_css'] = TablePress::$model_options->get( 'use_custom_css' );
426 579 $data['frontend_options']['custom_css'] = TablePress::$model_options->get( 'custom_css' );
427 - $data['user_options']['parent_page'] = $this->parent_page;
580 + $data['user_options']['parent_page'] = TablePress::$controller->parent_page;
428 581 break;
429 582 case 'edit':
430 - if ( ! empty( $_GET['table_id'] ) ) {
431 - // Load table, with table data, options, and visibility settings.
432 - $data['table'] = TablePress::$model_table->load( $_GET['table_id'], true, true );
433 - if ( is_wp_error( $data['table'] ) ) {
434 - TablePress::redirect( array( 'action' => 'list', 'message' => 'error_load_table' ) );
435 - }
436 - if ( ! current_user_can( 'tablepress_edit_table', $_GET['table_id'] ) ) {
437 - wp_die( __( 'You do not have sufficient permissions to access this page.', 'default' ), 403 );
438 - }
439 - } else {
583 + if ( empty( $_GET['table_id'] ) ) {
440 584 TablePress::redirect( array( 'action' => 'list', 'message' => 'error_no_table' ) );
441 585 }
586 + // Load table, with table data, options, and visibility settings.
587 + $data['table'] = TablePress::$model_table->load( $_GET['table_id'], true, true );
588 + if ( is_wp_error( $data['table'] ) ) {
589 + TablePress::redirect( array( 'action' => 'list', 'message' => 'error_load_table', 'error_details' => TablePress::get_wp_error_string( $data['table'] ) ) );
590 + }
591 + if ( ! current_user_can( 'tablepress_edit_table', $_GET['table_id'] ) ) {
592 + wp_die( __( 'Sorry, you are not allowed to access this page.', 'default' ), 403 );
593 + }
442 594 break;
443 595 case 'export':
444 596 // Load all table IDs without priming the post meta cache, as table options/visibility are not needed.
445 - $data['table_ids'] = TablePress::$model_table->load_all( false );
597 + $table_ids = TablePress::$model_table->load_all( false );
598 + $data['tables'] = array();
599 + foreach ( $table_ids as $table_id ) {
600 + if ( ! current_user_can( 'tablepress_export_table', $table_id ) ) {
601 + continue;
602 + }
603 + // Load table, without table data, options, and visibility settings.
604 + $table = TablePress::$model_table->load( $table_id, false, false );
605 +
606 + // Skip tables that could not be loaded.
607 + if ( is_wp_error( $table ) ) {
608 + continue;
609 + }
610 +
611 + $data['tables'][ $table['id'] ] = $table['name'];
612 + }
446 613 $data['tables_count'] = TablePress::$model_table->count_tables();
447 - if ( ! empty( $_GET['table_id'] ) ) {
448 - $data['export_ids'] = explode( ',', $_GET['table_id'] );
449 - } else {
450 - // Just show empty export form.
451 - $data['export_ids'] = array();
452 - }
614 + $data['export_ids'] = ( ! empty( $_GET['table_id'] ) ) ? explode( ',', $_GET['table_id'] ) : array();
453 615 $exporter = TablePress::load_class( 'TablePress_Export', 'class-export.php', 'classes' );
454 616 $data['zip_support_available'] = $exporter->zip_support_available;
455 617 $data['export_formats'] = $exporter->export_formats;
456 618 $data['csv_delimiters'] = $exporter->csv_delimiters;
457 - $data['export_format'] = ( ! empty( $_GET['export_format'] ) ) ? $_GET['export_format'] : false;
619 + $data['export_format'] = ( ! empty( $_GET['export_format'] ) ) ? $_GET['export_format'] : 'csv';
458 620 $data['csv_delimiter'] = ( ! empty( $_GET['csv_delimiter'] ) ) ? $_GET['csv_delimiter'] : _x( ',', 'Default CSV delimiter in the translated language (";", ",", or "tab")', 'tablepress' );
459 621 break;
460 622 case 'import':
461 623 // Load all table IDs without priming the post meta cache, as table options/visibility are not needed.
462 - $data['table_ids'] = TablePress::$model_table->load_all( false );
624 + $table_ids = TablePress::$model_table->load_all( false );
625 + $data['tables'] = array();
626 + foreach ( $table_ids as $table_id ) {
627 + if ( ! current_user_can( 'tablepress_edit_table', $table_id ) ) {
628 + continue;
629 + }
630 + // Load table, without table data, options, and visibility settings.
631 + $table = TablePress::$model_table->load( $table_id, false, false );
632 +
633 + // Skip tables that could not be loaded.
634 + if ( is_wp_error( $table ) ) {
635 + continue;
636 + }
637 +
638 + $data['tables'][ $table['id'] ] = $table['name'];
639 + }
640 + $data['table_ids'] = $table_ids; // Backward compatibility for the retired "Table Auto Update" Extension, which still relies on this variable name.
463 641 $data['tables_count'] = TablePress::$model_table->count_tables();
464 642 $importer = TablePress::load_class( 'TablePress_Import', 'class-import.php', 'classes' );
465 - $data['zip_support_available'] = $importer->zip_support_available;
466 - $data['html_import_support_available'] = $importer->html_import_support_available;
467 - $data['import_formats'] = $importer->import_formats;
468 - $data['import_format'] = ( ! empty( $_GET['import_format'] ) ) ? $_GET['import_format'] : false;
469 643 $data['import_type'] = ( ! empty( $_GET['import_type'] ) ) ? $_GET['import_type'] : 'add';
470 - $data['import_existing_table'] = ( ! empty( $_GET['import_existing_table'] ) ) ? $_GET['import_existing_table'] : false;
644 + $data['import_existing_table'] = ( ! empty( $_GET['import_existing_table'] ) ) ? $_GET['import_existing_table'] : '';
471 645 $data['import_source'] = ( ! empty( $_GET['import_source'] ) ) ? $_GET['import_source'] : 'file-upload';
472 - $data['import_url'] = ( ! empty( $_GET['import_url'] ) ) ? wp_unslash( $_GET['import_url'] ) : 'http://';
646 + $data['import_url'] = ( ! empty( $_GET['import_url'] ) ) ? wp_unslash( $_GET['import_url'] ) : 'https://';
473 647 $data['import_server'] = ( ! empty( $_GET['import_server'] ) ) ? wp_unslash( $_GET['import_server'] ) : ABSPATH;
474 - $data['import_form_field'] = ( ! empty( $_GET['import_form_field'] ) ) ? wp_unslash( $_GET['import_form_field'] ) : '';
475 - $data['wp_table_reloaded_installed'] = ( false !== get_option( 'wp_table_reloaded_options', false ) && false !== get_option( 'wp_table_reloaded_tables', false ) );
476 - $data['import_wp_table_reloaded_source'] = ( ! empty( $_GET['import_wp_table_reloaded_source'] ) ) ? $_GET['import_wp_table_reloaded_source'] : ( $data['wp_table_reloaded_installed'] ? 'db' : 'dump-file' );
648 + $data['import_form-field'] = ( ! empty( $_GET['import_form-field'] ) ) ? wp_unslash( $_GET['import_form-field'] ) : '';
649 + $data['legacy_import'] = ( ! empty( $_GET['legacy_import'] ) ) ? $_GET['legacy_import'] : 'false';
477 650 break;
478 651 }
479 652
480 653 /**
481 - * Filter the data that is passed to the current TablePress View.
654 + * Filters the data that is passed to the current TablePress View.
482 655 *
483 656 * @since 1.0.0
484 657 *
485 - * @param array $data Data for the view.
486 - * @param string $action The current action for the view.
658 + * @param array<string, mixed> $data Data for the view.
659 + * @param string $action The current action for the view.
487 660 */
488 661 $data = apply_filters( 'tablepress_view_data', $data, $action );
489 662
490 663 // Prepare and initialize the view.
@@ -495,33 +668,20 @@
495 668 * Render the view that has been initialized in load_admin_page() (called by WordPress when the actual page content is needed).
496 669 *
497 670 * @since 1.0.0
498 671 */
499 - public function show_admin_page() {
672 + public function show_admin_page(): void {
500 673 $this->view->render();
501 674 }
502 675
503 676 /**
504 - * Initialize i18n support, load plugin's textdomain, to retrieve correct translations.
677 + * Decides whether a message about Premium versions (previously, about donations) shall be shown on the "All Tables" screen, depending on passed days since installation and whether it was shown before.
505 678 *
506 679 * @since 1.0.0
507 - */
508 - protected function init_i18n_support() {
509 - if ( $this->i18n_support_loaded ) {
510 - return;
511 - }
512 - load_plugin_textdomain( 'tablepress', false, dirname( TABLEPRESS_BASENAME ) . '/i18n' );
513 - $this->i18n_support_loaded = true;
514 - }
515 -
516 - /**
517 - * Decide whether a donate message shall be shown on the "All Tables" screen, depending on passed days since installation and whether it was shown before.
518 680 *
519 - * @since 1.0.0
520 - *
521 - * @return bool Whether the donate message shall be shown on the "All Tables" screen.
681 + * @return bool Whether the message shall be shown on the "All Tables" screen.
522 682 */
523 - protected function maybe_show_donation_message() {
683 + protected function maybe_show_donation_message(): bool {
524 684 // Only show the message to plugin admins.
525 685 if ( ! current_user_can( 'tablepress_edit_options' ) ) {
526 686 return false;
527 687 }
@@ -531,9 +691,9 @@
531 691 }
532 692
533 693 // Determine, how long has the plugin been installed.
534 694 $seconds_installed = time() - TablePress::$model_options->get( 'first_activation' );
535 - return ( $seconds_installed > 30 * DAY_IN_SECONDS );
695 + return ( $seconds_installed > MONTH_IN_SECONDS / 2 );
536 696 }
537 697
538 698 /**
539 699 * Init list of actions that have a view with their titles/names/caps.
@@ -539,9 +699,9 @@
539 699 * Init list of actions that have a view with their titles/names/caps.
540 700 *
541 701 * @since 1.0.0
542 702 */
543 - protected function init_view_actions() {
703 + protected function init_view_actions(): void {
544 704 $this->view_actions = array(
545 705 'list' => array(
546 706 'show_entry' => true,
547 707 'page_title' => __( 'All Tables', 'tablepress' ),
@@ -593,13 +753,13 @@
593 753 ),
594 754 );
595 755
596 756 /**
597 - * Filter the available TablePres Views/Actions and their parameters.
757 + * Filters the available TablePres Views/Actions and their parameters.
598 758 *
599 759 * @since 1.0.0
600 760 *
601 - * @param array $view_actions The available Views/Actions and their parameters.
761 + * @param array<string, array<string, bool|string>> $view_actions The available Views/Actions and their parameters.
602 762 */
603 763 $this->view_actions = apply_filters( 'tablepress_admin_view_actions', $this->view_actions );
604 764 }
605 765
@@ -611,15 +771,15 @@
611 771 * Handle Bulk Actions (Copy, Export, Delete) on "All Tables" list screen.
612 772 *
613 773 * @since 1.0.0
614 774 */
615 - public function handle_post_action_list() {
775 + public function handle_post_action_list(): void {
616 776 TablePress::check_nonce( 'list' );
617 777
618 - if ( isset( $_POST['bulk-action-top'] ) && '-1' !== $_POST['bulk-action-top'] ) {
619 - $bulk_action = $_POST['bulk-action-top'];
620 - } elseif ( isset( $_POST['bulk-action-bottom'] ) && '-1' !== $_POST['bulk-action-bottom'] ) {
621 - $bulk_action = $_POST['bulk-action-bottom'];
778 + if ( isset( $_POST['bulk-action-selector-top'] ) && '-1' !== $_POST['bulk-action-selector-top'] ) {
779 + $bulk_action = $_POST['bulk-action-selector-top'];
780 + } elseif ( isset( $_POST['bulk-action-selector-bottom'] ) && '-1' !== $_POST['bulk-action-selector-bottom'] ) {
781 + $bulk_action = $_POST['bulk-action-selector-bottom'];
622 782 } else {
623 783 $bulk_action = false;
624 784 }
625 785
@@ -628,17 +788,16 @@
628 788 }
629 789
630 790 if ( empty( $_POST['table'] ) || ! is_array( $_POST['table'] ) ) {
631 791 TablePress::redirect( array( 'action' => 'list', 'message' => 'error_no_selection' ) );
632 - } else {
633 - $tables = wp_unslash( $_POST['table'] );
634 792 }
635 793
636 - $no_success = array(); // to store table IDs that failed
794 + $tables = wp_unslash( $_POST['table'] );
637 795
796 + $no_success = array(); // To store table IDs that failed.
797 +
638 798 switch ( $bulk_action ) {
639 799 case 'copy':
640 - $this->init_i18n_support(); // for the translation of "Copy of"
641 800 foreach ( $tables as $table_id ) {
642 801 if ( current_user_can( 'tablepress_copy_table', $table_id ) ) {
643 802 $copy_table_id = TablePress::$model_table->copy( $table_id );
644 803 if ( is_wp_error( $copy_table_id ) ) {
@@ -655,9 +814,9 @@
655 814 * To export, redirect to "Export" screen, with selected table IDs.
656 815 */
657 816 $table_ids = implode( ',', $tables );
658 817 TablePress::redirect( array( 'action' => 'export', 'table_id' => $table_ids ) );
659 - break;
818 + // break; // unreachable.
660 819 case 'delete':
661 820 foreach ( $tables as $table_id ) {
662 821 if ( current_user_can( 'tablepress_delete_table', $table_id ) ) {
663 822 $deleted = TablePress::$model_table->delete( $table_id );
@@ -670,9 +829,9 @@
670 829 }
671 830 break;
672 831 }
673 832
674 - if ( 0 !== count( $no_success ) ) { // @TODO: maybe pass this information to the view?
833 + if ( 0 !== count( $no_success ) ) { // @todo maybe pass this information to the view?
675 834 $message = "error_{$bulk_action}_not_all_tables";
676 835 } else {
677 836 $plural = ( count( $tables ) > 1 ) ? '_plural' : '';
678 837 $message = "success_{$bulk_action}{$plural}";
@@ -693,122 +852,36 @@
693 852 exit;
694 853 }
695 854
696 855 /**
697 - * Save a table after the "Edit" screen was submitted.
698 - *
699 - * @since 1.0.0
700 - */
701 - public function handle_post_action_edit() {
702 - if ( empty( $_POST['table'] ) || empty( $_POST['table']['id'] ) ) {
703 - TablePress::redirect( array( 'action' => 'list', 'message' => 'error_save' ) );
704 - } else {
705 - $edit_table = wp_unslash( $_POST['table'] );
706 - }
707 -
708 - TablePress::check_nonce( 'edit', $edit_table['id'], 'nonce-edit-table' );
709 -
710 - if ( ! current_user_can( 'tablepress_edit_table', $edit_table['id'] ) ) {
711 - wp_die( __( 'You do not have sufficient permissions to access this page.', 'default' ), 403 );
712 - }
713 -
714 - // Options array must exist, so that checkboxes can be evaluated.
715 - if ( empty( $edit_table['options'] ) ) {
716 - TablePress::redirect( array( 'action' => 'edit', 'table_id' => $edit_table['id'], 'message' => 'error_save' ) );
717 - }
718 -
719 - // Evaluate options that have a checkbox (only necessary in Admin Controller, where they might not be set (if unchecked)).
720 - $checkbox_options = array(
721 - // Table Options.
722 - 'table_head',
723 - 'table_foot',
724 - 'alternating_row_colors',
725 - 'row_hover',
726 - 'print_name',
727 - 'print_description',
728 - // DataTables JS Features.
729 - 'use_datatables',
730 - 'datatables_sort',
731 - 'datatables_filter',
732 - 'datatables_paginate',
733 - 'datatables_lengthchange',
734 - 'datatables_info',
735 - 'datatables_scrollx',
736 - );
737 - foreach ( $checkbox_options as $option ) {
738 - $edit_table['options'][ $option ] = ( isset( $edit_table['options'][ $option ] ) && 'true' === $edit_table['options'][ $option ] );
739 - }
740 -
741 - // Load table, without table data, but with options and visibility settings.
742 - $existing_table = TablePress::$model_table->load( $edit_table['id'], false, true );
743 - if ( is_wp_error( $existing_table ) ) { // @TODO: Maybe somehow load a new table here? (TablePress::$model_table->get_table_template())?
744 - TablePress::redirect( array( 'action' => 'edit', 'table_id' => $edit_table['id'], 'message' => 'error_save' ) );
745 - }
746 -
747 - // Check consistency of new table, and then merge with existing table.
748 - $table = TablePress::$model_table->prepare_table( $existing_table, $edit_table );
749 - if ( is_wp_error( $table ) ) {
750 - TablePress::redirect( array( 'action' => 'edit', 'table_id' => $edit_table['id'], 'message' => 'error_save' ) );
751 - }
752 -
753 - // DataTables Custom Commands can only be edit by trusted users.
754 - if ( ! current_user_can( 'unfiltered_html' ) ) {
755 - $table['options']['datatables_custom_commands'] = $existing_table['options']['datatables_custom_commands'];
756 - }
757 -
758 - // Save updated table.
759 - $saved = TablePress::$model_table->save( $table );
760 - if ( is_wp_error( $saved ) ) {
761 - TablePress::redirect( array( 'action' => 'edit', 'table_id' => $table['id'], 'message' => 'error_save' ) );
762 - }
763 -
764 - // Check if ID change is desired.
765 - if ( $table['id'] === $table['new_id'] ) { // if not, we are done
766 - TablePress::redirect( array( 'action' => 'edit', 'table_id' => $table['id'], 'message' => 'success_save' ) );
767 - }
768 -
769 - // Change table ID.
770 - if ( current_user_can( 'tablepress_edit_table_id', $table['id'] ) ) {
771 - $id_changed = TablePress::$model_table->change_table_id( $table['id'], $table['new_id'] );
772 - if ( ! is_wp_error( $id_changed ) ) {
773 - TablePress::redirect( array( 'action' => 'edit', 'table_id' => $table['new_id'], 'message' => 'success_save_success_id_change' ) );
774 - } else {
775 - TablePress::redirect( array( 'action' => 'edit', 'table_id' => $table['id'], 'message' => 'success_save_error_id_change' ) );
776 - }
777 - } else {
778 - TablePress::redirect( array( 'action' => 'edit', 'table_id' => $table['id'], 'message' => 'success_save_error_id_change' ) );
779 - }
780 - }
781 -
782 - /**
783 856 * Add a table, according to the parameters on the "Add new Table" screen.
784 857 *
785 858 * @since 1.0.0
786 859 */
787 - public function handle_post_action_add() {
860 + public function handle_post_action_add(): void {
788 861 TablePress::check_nonce( 'add' );
789 862
790 863 if ( ! current_user_can( 'tablepress_add_tables' ) ) {
791 - wp_die( __( 'You do not have sufficient permissions to access this page.', 'default' ), 403 );
864 + wp_die( __( 'Sorry, you are not allowed to access this page.', 'default' ), 403 );
792 865 }
793 866
794 867 if ( empty( $_POST['table'] ) || ! is_array( $_POST['table'] ) ) {
795 - TablePress::redirect( array( 'action' => 'add', 'message' => 'error_add' ) );
796 - } else {
797 - $add_table = wp_unslash( $_POST['table'] );
868 + TablePress::redirect( array( 'action' => 'add', 'message' => 'error_add', 'error_details' => 'The HTTP POST data is empty.' ) );
798 869 }
799 870
800 - // Perform sanity checks of posted data.
801 - $name = ( isset( $add_table['name'] ) ) ? $add_table['name'] : '';
802 - $description = ( isset( $add_table['description'] ) ) ? $add_table['description'] : '';
803 - if ( ! isset( $add_table['rows'] ) || ! isset( $add_table['columns'] ) ) {
804 - TablePress::redirect( array( 'action' => 'add', 'message' => 'error_add' ) );
871 + $add_table = wp_unslash( $_POST['table'] );
872 +
873 + // Perform confidence checks of posted data.
874 + $name = $add_table['name'] ?? '';
875 + $description = $add_table['description'] ?? '';
876 + if ( ! isset( $add_table['rows'], $add_table['columns'] ) ) {
877 + TablePress::redirect( array( 'action' => 'add', 'message' => 'error_add', 'error_details' => 'The HTTP POST data does not contain the table size.' ) );
805 878 }
806 879
807 880 $num_rows = absint( $add_table['rows'] );
808 881 $num_columns = absint( $add_table['columns'] );
809 882 if ( 0 === $num_rows || 0 === $num_columns ) {
810 - TablePress::redirect( array( 'action' => 'add', 'message' => 'error_add' ) );
883 + TablePress::redirect( array( 'action' => 'add', 'message' => 'error_add', 'error_details' => 'The table size is invalid.' ) );
811 884 }
812 885
813 886 // Create a new table array with information from the posted data.
814 887 $new_table = array(
@@ -822,15 +895,15 @@
822 895 );
823 896 // Merge this data into an empty table template.
824 897 $table = TablePress::$model_table->prepare_table( TablePress::$model_table->get_table_template(), $new_table, false );
825 898 if ( is_wp_error( $table ) ) {
826 - TablePress::redirect( array( 'action' => 'add', 'message' => 'error_add' ) );
899 + TablePress::redirect( array( 'action' => 'add', 'message' => 'error_add', 'error_details' => TablePress::get_wp_error_string( $table ) ) );
827 900 }
828 901
829 902 // Add the new table (and get its first ID).
830 903 $table_id = TablePress::$model_table->add( $table );
831 904 if ( is_wp_error( $table_id ) ) {
832 - TablePress::redirect( array( 'action' => 'add', 'message' => 'error_add' ) );
905 + TablePress::redirect( array( 'action' => 'add', 'message' => 'error_add', 'error_details' => TablePress::get_wp_error_string( $table_id ) ) );
833 906 }
834 907
835 908 TablePress::redirect( array( 'action' => 'edit', 'table_id' => $table_id, 'message' => 'success_add' ) );
836 909 }
@@ -839,21 +912,21 @@
839 912 * Save changed "Plugin Options".
840 913 *
841 914 * @since 1.0.0
842 915 */
843 - public function handle_post_action_options() {
916 + public function handle_post_action_options(): void {
844 917 TablePress::check_nonce( 'options' );
845 918
846 919 if ( ! current_user_can( 'tablepress_access_options_screen' ) ) {
847 - wp_die( __( 'You do not have sufficient permissions to access this page.', 'default' ), 403 );
920 + wp_die( __( 'Sorry, you are not allowed to access this page.', 'default' ), 403 );
848 921 }
849 922
850 923 if ( empty( $_POST['options'] ) || ! is_array( $_POST['options'] ) ) {
851 924 TablePress::redirect( array( 'action' => 'options', 'message' => 'error_save' ) );
852 - } else {
853 - $posted_options = wp_unslash( $_POST['options'] );
854 925 }
855 926
927 + $posted_options = wp_unslash( $_POST['options'] );
928 +
856 929 // Valid new options that will be merged into existing ones.
857 930 $new_options = array();
858 931
859 932 // Check each posted option value, and (maybe) add it to the new options.
@@ -858,18 +931,18 @@
858 931
859 932 // Check each posted option value, and (maybe) add it to the new options.
860 933 if ( ! empty( $posted_options['admin_menu_parent_page'] ) && '-' !== $posted_options['admin_menu_parent_page'] ) {
861 934 $new_options['admin_menu_parent_page'] = $posted_options['admin_menu_parent_page'];
862 - // Re-init parent information, as TablePress::redirect() URL might be wrong otherwise.
935 + // Re-init parent information, as `TablePress::redirect()` URL might be wrong otherwise.
863 936 /** This filter is documented in classes/class-controller.php */
864 - $this->parent_page = apply_filters( 'tablepress_admin_menu_parent_page', $posted_options['admin_menu_parent_page'] );
865 - $this->is_top_level_page = in_array( $this->parent_page, array( 'top', 'middle', 'bottom' ), true );
937 + TablePress::$controller->parent_page = apply_filters( 'tablepress_admin_menu_parent_page', $posted_options['admin_menu_parent_page'] );
938 + TablePress::$controller->is_top_level_page = in_array( TablePress::$controller->parent_page, array( 'top', 'middle', 'bottom' ), true );
866 939 }
867 940
868 941 // Custom CSS can only be saved if the user is allowed to do so.
869 942 $update_custom_css_files = false;
870 943 if ( current_user_can( 'tablepress_edit_options' ) ) {
871 - // Checkbox
944 + // Checkbox.
872 945 $new_options['use_custom_css'] = ( isset( $posted_options['use_custom_css'] ) && 'true' === $posted_options['use_custom_css'] );
873 946
874 947 if ( isset( $posted_options['custom_css'] ) ) {
875 948 $new_options['custom_css'] = $posted_options['custom_css'];
@@ -874,13 +947,20 @@
874 947 if ( isset( $posted_options['custom_css'] ) ) {
875 948 $new_options['custom_css'] = $posted_options['custom_css'];
876 949
877 950 $tablepress_css = TablePress::load_class( 'TablePress_CSS', 'class-css.php', 'classes' );
878 - // Sanitize and tidy up Custom CSS.
879 - $new_options['custom_css'] = $tablepress_css->sanitize_css( $new_options['custom_css'] );
880 - // Minify Custom CSS
881 - $new_options['custom_css_minified'] = $tablepress_css->minify_css( $new_options['custom_css'] );
882 951
952 + if ( '' !== $new_options['custom_css'] ) {
953 + // Update "Custom CSS" to use DataTables 2 variants instead of old DataTables 1.x CSS classes.
954 + $new_options['custom_css'] = TablePress::convert_datatables_api_data( $new_options['custom_css'] );
955 + // Sanitize and tidy up Custom CSS.
956 + $new_options['custom_css'] = $tablepress_css->sanitize_css( $new_options['custom_css'] );
957 + // Minify Custom CSS.
958 + $new_options['custom_css_minified'] = $tablepress_css->minify_css( $new_options['custom_css'] );
959 + } else {
960 + $new_options['custom_css_minified'] = '';
961 + }
962 +
883 963 // Maybe update CSS files as well.
884 964 $custom_css_file_contents = $tablepress_css->load_custom_css_from_file( 'normal' );
885 965 if ( false === $custom_css_file_contents ) {
886 966 $custom_css_file_contents = '';
@@ -911,28 +991,30 @@
911 991 * Export selected tables.
912 992 *
913 993 * @since 1.0.0
914 994 */
915 - public function handle_post_action_export() {
995 + public function handle_post_action_export(): void {
916 996 TablePress::check_nonce( 'export' );
917 997
918 998 if ( ! current_user_can( 'tablepress_export_tables' ) ) {
919 - wp_die( __( 'You do not have sufficient permissions to access this page.', 'default' ), 403 );
999 + wp_die( __( 'Sorry, you are not allowed to access this page.', 'default' ), 403 );
920 1000 }
921 1001
922 1002 if ( empty( $_POST['export'] ) || ! is_array( $_POST['export'] ) ) {
923 - TablePress::redirect( array( 'action' => 'export', 'message' => 'error_export' ) );
924 - } else {
925 - $export = wp_unslash( $_POST['export'] );
1003 + TablePress::redirect( array( 'action' => 'export', 'message' => 'error_export', 'error_details' => 'The HTTP POST data is empty.' ) );
926 1004 }
927 1005
1006 + $export = wp_unslash( $_POST['export'] );
1007 +
1008 + if ( empty( $export['tables_list'] ) ) {
1009 + TablePress::redirect( array( 'action' => 'export', 'message' => 'error_export', 'error_details' => 'The HTTP POST data does not contain tables.' ) );
1010 + }
1011 +
1012 + /** @var TablePress_Export $exporter */ // phpcs:ignore Generic.Commenting.DocComment.MissingShort
928 1013 $exporter = TablePress::load_class( 'TablePress_Export', 'class-export.php', 'classes' );
929 1014
930 - if ( empty( $export['tables'] ) ) {
931 - TablePress::redirect( array( 'action' => 'export', 'message' => 'error_export' ) );
932 - }
933 1015 if ( empty( $export['format'] ) || ! isset( $exporter->export_formats[ $export['format'] ] ) ) {
934 - TablePress::redirect( array( 'action' => 'export', 'message' => 'error_export' ) );
1016 + TablePress::redirect( array( 'action' => 'export', 'message' => 'error_export', 'error_details' => 'The export format is invalid.' ) );
935 1017 }
936 1018 if ( empty( $export['csv_delimiter'] ) ) {
937 1019 // Set a value, so that the variable exists.
938 1020 $export['csv_delimiter'] = '';
@@ -937,13 +1019,12 @@
937 1019 // Set a value, so that the variable exists.
938 1020 $export['csv_delimiter'] = '';
939 1021 }
940 1022 if ( 'csv' === $export['format'] && ! isset( $exporter->csv_delimiters[ $export['csv_delimiter'] ] ) ) {
941 - TablePress::redirect( array( 'action' => 'export', 'message' => 'error_export' ) );
1023 + TablePress::redirect( array( 'action' => 'export', 'message' => 'error_export', 'error_details' => 'The CSV delimiter is invalid.' ) );
942 1024 }
943 1025
944 - // Use list of tables from concatenated field if available (as that's hopefully not truncated by Suhosin, which is possible for $export['tables']).
945 - $tables = ( ! empty( $export['tables_list'] ) ) ? explode( ',', $export['tables_list'] ) : $export['tables'];
1026 + $tables = explode( ',', $export['tables_list'] );
946 1027
947 1028 // Determine if ZIP file support is available.
948 1029 if ( $exporter->zip_support_available
949 1030 && ( ( isset( $export['zip_file'] ) && 'true' === $export['zip_file'] ) || count( $tables ) > 1 ) ) {
@@ -953,49 +1034,64 @@
953 1034 $export_to_zip = false;
954 1035 }
955 1036
956 1037 if ( ! $export_to_zip ) {
957 - // This is only possible for one table, so take the first one.
1038 + // Exporting without a ZIP file is only possible for one table, so take the first one.
958 1039 if ( ! current_user_can( 'tablepress_export_table', $tables[0] ) ) {
959 - wp_die( __( 'You do not have sufficient permissions to access this page.', 'default' ), 403 );
1040 + wp_die( __( 'Sorry, you are not allowed to access this page.', 'default' ), 403 );
960 1041 }
961 1042 // Load table, with table data, options, and visibility settings.
962 1043 $table = TablePress::$model_table->load( $tables[0], true, true );
963 1044 if ( is_wp_error( $table ) ) {
964 - TablePress::redirect( array( 'action' => 'export', 'message' => 'error_load_table', 'export_format' => $export['format'], 'csv_delimiter' => $export['csv_delimiter'] ) );
1045 + TablePress::redirect( array( 'action' => 'export', 'message' => 'error_load_table', 'export_format' => $export['format'], 'csv_delimiter' => $export['csv_delimiter'], 'error_details' => TablePress::get_wp_error_string( $table ) ) );
965 1046 }
966 1047 if ( isset( $table['is_corrupted'] ) && $table['is_corrupted'] ) {
967 1048 TablePress::redirect( array( 'action' => 'export', 'message' => 'error_table_corrupted', 'export_format' => $export['format'], 'csv_delimiter' => $export['csv_delimiter'] ) );
968 1049 }
969 - $download_filename = sprintf( '%1$s-%2$s-%3$s.%4$s', $table['id'], $table['name'], date( 'Y-m-d' ), $export['format'] );
1050 + $download_filename = sprintf( '%1$s-%2$s-%3$s.%4$s', $table['id'], $table['name'], wp_date( 'Y-m-d' ), $export['format'] );
1051 + /**
1052 + * Filters the download filename of the exported table.
1053 + *
1054 + * @since 2.0.0
1055 + *
1056 + * @param string $download_filename The download filename of exported table.
1057 + * @param string $table_id Table ID of the exported table.
1058 + * @param string $table_name Table name of the exported table.
1059 + * @param string $export_format Format for the export ('csv', 'html', 'json', 'zip').
1060 + * @param bool $export_to_zip Whether the export is to a ZIP file (of multiple export files).
1061 + */
1062 + $download_filename = apply_filters( 'tablepress_export_filename', $download_filename, $table['id'], $table['name'], $export['format'], $export_to_zip );
970 1063 $download_filename = sanitize_file_name( $download_filename );
971 1064 // Export the table.
972 1065 $export_data = $exporter->export_table( $table, $export['format'], $export['csv_delimiter'] );
973 1066 /**
974 - * Filter the exported table data.
1067 + * Filters the exported table data.
975 1068 *
976 1069 * @since 1.6.0
977 1070 *
978 - * @param string $export_data The exported table data.
979 - * @param array $table Table to be exported.
980 - * @param string $export_format Format for the export ('csv', 'html', 'json').
981 - * @param string $csv_delimiter Delimiter for CSV export.
1071 + * @param string $export_data The exported table data.
1072 + * @param array<string, mixed> $table Table to be exported.
1073 + * @param string $export_format Format for the export ('csv', 'html', 'json').
1074 + * @param string $csv_delimiter Delimiter for CSV export.
982 1075 */
983 1076 $export_data = apply_filters( 'tablepress_export_data', $export_data, $table, $export['format'], $export['csv_delimiter'] );
984 1077 $download_data = $export_data;
985 1078 } else {
986 1079 // Zipping can use a lot of memory and execution time, but not this much hopefully.
987 - /** This filter is documented in the WordPress file wp-admin/admin.php */
988 - @ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', WP_MAX_MEMORY_LIMIT ) );
989 - @set_time_limit( 300 );
1080 + wp_raise_memory_limit( 'admin' );
1081 + if ( function_exists( 'set_time_limit' ) ) {
1082 + @set_time_limit( 300 ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
1083 + }
990 1084
991 1085 $zip_file = new ZipArchive();
992 - $download_filename = sprintf( 'tablepress-export-%1$s-%2$s.zip', date_i18n( 'Y-m-d-H-i-s' ), $export['format'] );
1086 + $download_filename = sprintf( 'tablepress-export-%1$s-%2$s.zip', wp_date( 'Y-m-d-H-i-s' ), $export['format'] );
1087 + /** This filter is documented in controllers/controller-admin.php */
1088 + $download_filename = apply_filters( 'tablepress_export_filename', $download_filename, '', '', $export['format'], $export_to_zip );
993 1089 $download_filename = sanitize_file_name( $download_filename );
994 1090 $full_filename = wp_tempnam( $download_filename );
995 - if ( true !== $zip_file->open( $full_filename, ZIPARCHIVE::OVERWRITE ) ) {
996 - @unlink( $full_filename );
997 - TablePress::redirect( array( 'action' => 'export', 'message' => 'error_create_zip_file', 'export_format' => $export['format'], 'csv_delimiter' => $export['csv_delimiter'] ) );
1091 + if ( true !== $zip_file->open( $full_filename, ZipArchive::OVERWRITE ) ) {
1092 + @unlink( $full_filename ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
1093 + TablePress::redirect( array( 'action' => 'export', 'message' => 'error_create_zip_file', 'export_format' => $export['format'], 'csv_delimiter' => $export['csv_delimiter'], 'error_details' => 'The ZIP file could not be opened for writing.' ) );
998 1094 }
999 1095
1000 1096 foreach ( $tables as $table_id ) {
1001 1097 // Don't export tables for which the user doesn't have the necessary export rights.
@@ -1014,24 +1110,31 @@
1014 1110 }
1015 1111 $export_data = $exporter->export_table( $table, $export['format'], $export['csv_delimiter'] );
1016 1112 /** This filter is documented in controllers/controller-admin.php */
1017 1113 $export_data = apply_filters( 'tablepress_export_data', $export_data, $table, $export['format'], $export['csv_delimiter'] );
1018 - $export_filename = sprintf( '%1$s-%2$s-%3$s.%4$s', $table['id'], $table['name'], date( 'Y-m-d' ), $export['format'] );
1114 + $export_filename = sprintf( '%1$s-%2$s-%3$s.%4$s', $table['id'], $table['name'], wp_date( 'Y-m-d' ), $export['format'] );
1115 + /** This filter is documented in controllers/controller-admin.php */
1116 + $export_filename = apply_filters( 'tablepress_export_filename', $export_filename, $table['id'], $table['name'], $export['format'], $export_to_zip );
1019 1117 $export_filename = sanitize_file_name( $export_filename );
1020 1118 $zip_file->addFromString( $export_filename, $export_data );
1021 1119 }
1022 1120
1023 1121 // If something went wrong, or no files were added to the ZIP file, bail out.
1024 - if ( ! ZIPARCHIVE::ER_OK === $zip_file->status || 0 === $zip_file->numFiles ) {
1122 + // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
1123 + if ( ZipArchive::ER_OK !== $zip_file->status || 0 === $zip_file->numFiles ) {
1025 1124 $zip_file->close();
1026 - @unlink( $full_filename );
1027 - TablePress::redirect( array( 'action' => 'export', 'message' => 'error_create_zip_file', 'export_format' => $export['format'], 'csv_delimiter' => $export['csv_delimiter'] ) );
1125 + @unlink( $full_filename ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
1126 + TablePress::redirect( array( 'action' => 'export', 'message' => 'error_create_zip_file', 'export_format' => $export['format'], 'csv_delimiter' => $export['csv_delimiter'], 'error_details' => 'The ZIP file could not be written or is empty.' ) );
1028 1127 }
1029 1128 $zip_file->close();
1030 1129
1031 1130 // Load contents of the ZIP file, to send it as a download.
1032 1131 $download_data = file_get_contents( $full_filename );
1033 - @unlink( $full_filename );
1132 + if ( false === $download_data ) {
1133 + @unlink( $full_filename ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
1134 + TablePress::redirect( array( 'action' => 'export', 'message' => 'error_create_zip_file', 'export_format' => $export['format'], 'csv_delimiter' => $export['csv_delimiter'], 'error_details' => 'The ZIP file content could not be read.' ) );
1135 + }
1136 + @unlink( $full_filename ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
1034 1137 }
1035 1138
1036 1139 // Send download headers for export file.
1037 1140 header( 'Content-Description: File Transfer' );
@@ -1043,9 +1146,9 @@
1043 1146 header( 'Pragma: public' );
1044 1147 header( 'Content-Length: ' . strlen( $download_data ) );
1045 1148 // $filetype = text/csv, text/html, application/json
1046 1149 // header( 'Content-Type: ' . $filetype. '; charset=' . get_option( 'blog_charset' ) );
1047 - @ob_end_clean();
1150 + @ob_end_clean(); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
1048 1151 flush();
1049 1152 echo $download_data;
1050 1153 exit;
1051 1154 }
@@ -1050,669 +1153,100 @@
1050 1153 exit;
1051 1154 }
1052 1155
1053 1156 /**
1054 - * Import data from either an existing source or WP-Table Reloaded.
1157 + * Import data from existing source (Upload, URL, Server, Direct input).
1055 1158 *
1056 1159 * @since 1.0.0
1057 1160 */
1058 - public function handle_post_action_import() {
1161 + public function handle_post_action_import(): void {
1059 1162 TablePress::check_nonce( 'import' );
1060 1163
1061 1164 if ( ! current_user_can( 'tablepress_import_tables' ) ) {
1062 - wp_die( __( 'You do not have sufficient permissions to access this page.', 'default' ), 403 );
1165 + wp_die( __( 'Sorry, you are not allowed to access this page.', 'default' ), 403 );
1063 1166 }
1064 1167
1065 1168 if ( empty( $_POST['import'] ) || ! is_array( $_POST['import'] ) ) {
1066 - TablePress::redirect( array( 'action' => 'import', 'message' => 'error_import' ) );
1067 - } else {
1068 - $import = wp_unslash( $_POST['import'] );
1169 + TablePress::redirect( array( 'action' => 'import', 'message' => 'error_import', 'error_details' => 'The HTTP POST data is empty.' ) );
1069 1170 }
1070 1171
1071 - // Determine if this is a regular import or an import from WP-Table Reloaded.
1072 - if ( isset( $_POST['submit_wp_table_reloaded_import'] ) && isset( $import['wp_table_reloaded'] ) && isset( $import['wp_table_reloaded']['source'] ) ) {
1073 - if ( ! current_user_can( 'tablepress_import_tables_wptr' ) ) {
1074 - wp_die( __( 'You do not have sufficient permissions to access this page.', 'default' ), 403 );
1075 - }
1172 + $import_config = wp_unslash( $_POST['import'] );
1076 1173
1077 - // Handle checkbox selections.
1078 - $import_tables = ( isset( $import['wp_table_reloaded']['tables'] ) && 'true' === $import['wp_table_reloaded']['tables'] );
1079 - $import_css = ( isset( $import['wp_table_reloaded']['css'] ) && 'true' === $import['wp_table_reloaded']['css'] );
1080 - if ( ! $import_tables && ! $import_css ) {
1081 - TablePress::redirect( array( 'action' => 'import', 'message' => 'error_wp_table_reloaded_nothing_selected' ) );
1082 - }
1083 -
1084 - if ( 'db' === $import['wp_table_reloaded']['source'] ) {
1085 - $this->_import_from_wp_table_reloaded_db( $import_tables, $import_css );
1086 - } else {
1087 - $this->_import_from_wp_table_reloaded_dump_file( $import_tables, $import_css );
1088 - }
1089 - } else {
1090 - $this->_import_tablepress_regular( $import );
1174 + if ( empty( $import_config['source'] ) ) {
1175 + TablePress::redirect( array( 'action' => 'import', 'message' => 'error_import', 'error_details' => 'The HTTP POST does not contain an import configuration.' ) );
1091 1176 }
1092 - }
1093 1177
1094 - /**
1095 - * Import data from existing source (Upload, URL, Server, Direct input).
1096 - *
1097 - * @since 1.0.0
1098 - *
1099 - * @param array $import Submitted form data.
1100 - */
1101 - protected function _import_tablepress_regular( array $import ) {
1102 - if ( ! isset( $import['type'] ) ) {
1103 - $import['type'] = 'add';
1104 - }
1105 - if ( ! isset( $import['existing_table'] ) ) {
1106 - $import['existing_table'] = '';
1107 - }
1108 - if ( ! isset( $import['source'] ) ) {
1109 - $import['source'] = '';
1110 - }
1111 -
1112 - // Check if a table to replace or append to was selected.
1113 - if ( in_array( $import['type'], array( 'replace', 'append' ), true ) && empty( $import['existing_table'] ) ) {
1114 - TablePress::redirect( array( 'action' => 'import', 'message' => 'error_import_no_existing_id', 'import_format' => $import['format'], 'import_type' => $import['type'], 'import_source' => $import['source'] ) );
1115 - }
1116 -
1117 - $import_error = true;
1118 - $unlink_file = false;
1119 - $import_data = array();
1120 - switch ( $import['source'] ) {
1121 - case 'file-upload':
1122 - if ( ! empty( $_FILES['import_file_upload'] ) && UPLOAD_ERR_OK === $_FILES['import_file_upload']['error'] ) {
1123 - $import_data['file_location'] = $_FILES['import_file_upload']['tmp_name'];
1124 - $import_data['file_name'] = $_FILES['import_file_upload']['name'];
1125 - // $_FILES['import_file_upload']['type'];
1126 - // $_FILES['import_file_upload']['size']
1127 - $import_error = false;
1128 - $unlink_file = true;
1129 - }
1130 - break;
1131 - case 'url':
1132 - if ( ! empty( $import['url'] ) && 'http://' !== $import['url'] ) {
1133 - // Check the host of the Import URL against a blacklist of hosts, which should not be accessible, e.g. for security considerations.
1134 - $host = wp_parse_url( $import['url'], PHP_URL_HOST );
1135 - $blocked_hosts = array(
1136 - '169.254.169.254' // AWS Meta-data API
1137 - );
1138 - if ( in_array( $host, $blocked_hosts, true ) ) {
1139 - $import_error = true;
1140 - break;
1141 - }
1142 -
1143 - // Download URL to local file.
1144 - $import_data['file_location'] = download_url( $import['url'] );
1145 - $import_data['file_name'] = $import['url'];
1146 - if ( ! is_wp_error( $import_data['file_location'] ) ) {
1147 - $import_error = false;
1148 - }
1149 - $unlink_file = true;
1150 - }
1151 - break;
1152 - case 'server':
1153 - if ( ! empty( $import['server'] ) && ABSPATH !== $import['server']
1154 - && ( ( ! is_multisite() && current_user_can( 'manage_options' ) ) || is_super_admin() )
1155 - ) {
1156 - // For security reasons, the `server` source is only available for administrators.
1157 - $import_data['file_location'] = $import['server'];
1158 - $import_data['file_name'] = pathinfo( $import['server'], PATHINFO_BASENAME );
1159 - if ( is_readable( $import['server'] ) ) {
1160 - $import_error = false;
1161 - }
1162 - }
1163 - break;
1164 - case 'form-field':
1165 - if ( ! empty( $import['form_field'] ) ) {
1166 - $import_data['file_location'] = '';
1167 - $import_data['file_name'] = __( 'Imported from Manual Input', 'tablepress' ); // Description of the table.
1168 - $import_data['data'] = $import['form_field'];
1169 - $import_error = false;
1170 - }
1171 - break;
1172 - }
1173 -
1174 - if ( $import_error ) {
1175 - if ( $unlink_file ) {
1176 - @unlink( $import_data['file_location'] );
1178 + // For security reasons, the "server" source is only available for super admins on multisite and admins on single sites.
1179 + if ( 'server' === $import_config['source'] ) {
1180 + if ( ! is_super_admin() && ! ( ! is_multisite() && current_user_can( 'manage_options' ) ) ) {
1181 + TablePress::redirect( array( 'action' => 'import', 'message' => 'error_import', 'error_details' => 'You do not have the required access rights.' ) );
1177 1182 }
1178 - TablePress::redirect( array( 'action' => 'import', 'message' => 'error_import_source_invalid', 'import_format' => $import['format'], 'import_type' => $import['type'], 'import_existing_table' => $import['existing_table'], 'import_source' => $import['source'] ) );
1179 1183 }
1180 1184
1181 - $this->importer = TablePress::load_class( 'TablePress_Import', 'class-import.php', 'classes' );
1182 -
1183 - if ( 'zip' === pathinfo( $import_data['file_name'], PATHINFO_EXTENSION ) ) {
1184 - // Determine if ZIP file support is available.
1185 - if ( ! $this->importer->zip_support_available ) {
1186 - if ( $unlink_file ) {
1187 - @unlink( $import_data['file_location'] );
1188 - }
1189 - TablePress::redirect( array( 'action' => 'import', 'message' => 'error_no_zip_import', 'import_format' => $import['format'], 'import_type' => $import['type'], 'import_existing_table' => $import['existing_table'], 'import_source' => $import['source'] ) );
1185 + // For security reasons, the "url" source is only available admins and editors via a custom capability.
1186 + if ( 'url' === $import_config['source'] ) {
1187 + if ( ! current_user_can( 'tablepress_import_tables_url' ) ) {
1188 + TablePress::redirect( array( 'action' => 'import', 'message' => 'error_import', 'error_details' => 'You do not have the required access rights.' ) );
1190 1189 }
1191 - $import_zip = true;
1192 - } else {
1193 - $import_zip = false;
1194 1190 }
1195 1191
1196 - if ( ! $import_zip ) {
1197 - if ( ! isset( $import_data['data'] ) ) {
1198 - $import_data['data'] = file_get_contents( $import_data['file_location'] );
1199 - }
1200 - if ( false === $import_data['data'] ) {
1201 - TablePress::redirect( array( 'action' => 'import', 'message' => 'error_import' ) );
1202 - }
1192 + // Move file upload data to the main import configuration.
1193 + $import_config['file-upload'] = $_FILES['import_file_upload'] ?? null;
1203 1194
1204 - $name = $import_data['file_name'];
1205 - $description = $import_data['file_name'];
1206 - $existing_table_id = ( in_array( $import['type'], array( 'replace', 'append' ), true ) && ! empty( $import['existing_table'] ) ) ? $import['existing_table'] : false;
1207 - $table_id = $this->_import_tablepress_table( $import['format'], $import_data['data'], $name, $description, $existing_table_id, $import['type'] );
1208 -
1209 - if ( $unlink_file ) {
1210 - @unlink( $import_data['file_location'] );
1211 - }
1212 -
1213 - if ( is_wp_error( $table_id ) ) {
1214 - TablePress::redirect( array( 'action' => 'import', 'message' => 'error_import_data' ) );
1215 - } else {
1216 - TablePress::redirect( array( 'action' => 'edit', 'table_id' => $table_id, 'message' => 'success_import' ) );
1217 - }
1218 - } else {
1219 - // Zipping can use a lot of memory and execution time, but not this much hopefully.
1220 - /** This filter is documented in the WordPress file wp-admin/admin.php */
1221 - @ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', WP_MAX_MEMORY_LIMIT ) );
1222 - @set_time_limit( 300 );
1223 -
1224 - $zip = new ZipArchive();
1225 - if ( true !== $zip->open( $import_data['file_location'], ZIPARCHIVE::CHECKCONS ) ) {
1226 - if ( $unlink_file ) {
1227 - @unlink( $import_data['file_location'] );
1228 - }
1229 - TablePress::redirect( array( 'action' => 'import', 'message' => 'error_import_zip_open' ) );
1230 - }
1231 -
1232 - $imported_files = array();
1233 - for ( $file_idx = 0; $file_idx < $zip->numFiles; $file_idx++ ) {
1234 - $file_name = $zip->getNameIndex( $file_idx );
1235 - // Skip directories.
1236 - if ( '/' === substr( $file_name, -1 ) ) {
1237 - continue;
1238 - }
1239 - // Skip the __MACOSX directory that Mac OSX adds to archives.
1240 - if ( '__MACOSX/' === substr( $file_name, 0, 9 ) ) {
1241 - continue;
1242 - }
1243 - $data = $zip->getFromIndex( $file_idx );
1244 - if ( false === $data ) {
1245 - continue;
1246 - }
1247 -
1248 - $name = $file_name;
1249 - $description = $file_name;
1250 - $existing_table_id = ( in_array( $import['type'], array( 'replace', 'append' ), true ) ) ? false : false; // @TODO: Find a way to extract the replace/append ID from the filename, maybe?
1251 - $table_id = $this->_import_tablepress_table( $import['format'], $data, $name, $description, $existing_table_id, 'add' );
1252 - if ( is_wp_error( $table_id ) ) {
1253 - continue;
1254 - } else {
1255 - $imported_files[] = $table_id;
1256 - }
1257 - };
1258 - $zip->close();
1259 -
1260 - if ( $unlink_file ) {
1261 - @unlink( $import_data['file_location'] );
1262 - }
1263 -
1264 - if ( count( $imported_files ) > 1 ) {
1265 - TablePress::redirect( array( 'action' => 'list', 'message' => 'success_import' ) );
1266 - } elseif ( 1 === count( $imported_files ) ) {
1267 - TablePress::redirect( array( 'action' => 'edit', 'table_id' => $imported_files[0], 'message' => 'success_import' ) );
1268 - } else {
1269 - TablePress::redirect( array( 'action' => 'import', 'message' => 'error_import_zip_content' ) );
1270 - }
1195 + // Check if the source data for the chosen import source is defined.
1196 + if ( empty( $import_config[ $import_config['source'] ] ) ) {
1197 + TablePress::redirect( array( 'action' => 'import', 'message' => 'error_import', 'error_details' => 'The HTTP POST data does not contain an import source.' ) );
1271 1198 }
1272 1199
1273 - }
1274 -
1275 - /**
1276 - * Import a table by either replacing an existing table or adding it as a new table.
1277 - *
1278 - * @since 1.0.0
1279 - *
1280 - * @param string $format Import format.
1281 - * @param string $data Data to import.
1282 - * @param string $name Name of the table.
1283 - * @param string $description Description of the table.
1284 - * @param bool|string $existing_table_id False if table shall be added new, ID of the table to be replaced or appended to otherwise.
1285 - * @param string $import_type What to do with the imported data: "add", "replace", "append".
1286 - * @return string|WP_Error WP_Error on error, table ID on success.
1287 - */
1288 - protected function _import_tablepress_table( $format, $data, $name, $description, $existing_table_id, $import_type ) {
1289 - $imported_table = $this->importer->import_table( $format, $data );
1290 - if ( false === $imported_table ) {
1291 - return new WP_Error( 'table_import_import_failed' );
1200 + // Set default values for non-essential configuration variables.
1201 + if ( ! isset( $import_config['type'] ) ) {
1202 + $import_config['type'] = 'add';
1292 1203 }
1293 -
1294 - if ( false === $existing_table_id ) {
1295 - $import_type = 'add';
1204 + if ( ! isset( $import_config['existing_table'] ) ) {
1205 + $import_config['existing_table'] = '';
1296 1206 }
1297 1207
1298 - // To be able to replace or append to a table, editing that table must be allowed.
1299 - if ( in_array( $import_type, array( 'replace', 'append' ), true ) && ! current_user_can( 'tablepress_edit_table', $existing_table_id ) ) {
1300 - return new WP_Error( 'table_import_replace_append_capability_check_failed' );
1301 - }
1208 + $import_config['legacy_import'] = ( isset( $import_config['legacy_import'] ) && 'true' === $import_config['legacy_import'] );
1302 1209
1303 - // Full JSON format table can contain a table ID, try to keep that.
1304 - $table_id_in_import = false;
1210 + $importer = TablePress::load_class( 'TablePress_Import', 'class-import.php', 'classes' );
1211 + $import = $importer->run( $import_config );
1305 1212
1306 - switch ( $import_type ) {
1307 - case 'add':
1308 - $existing_table = TablePress::$model_table->get_table_template();
1309 - // If name and description are imported from a new table, use those.
1310 - if ( isset( $imported_table['id'] ) ) {
1311 - $table_id_in_import = $imported_table['id'];
1312 - }
1313 - if ( ! isset( $imported_table['name'] ) ) {
1314 - $imported_table['name'] = $name;
1315 - }
1316 - if ( ! isset( $imported_table['description'] ) ) {
1317 - $imported_table['description'] = $description;
1318 - }
1319 - if ( isset( $imported_table['visibility'] ) && isset( $imported_table['visibility']['rows'] ) && isset( $imported_table['visibility']['columns'] ) ) {
1320 - $existing_table['visibility']['rows'] = $imported_table['visibility']['rows'];
1321 - $existing_table['visibility']['columns'] = $imported_table['visibility']['columns'];
1322 - }
1323 - break;
1324 - case 'replace':
1325 - // Load table, without table data, but with options and visibility settings.
1326 - $existing_table = TablePress::$model_table->load( $existing_table_id, false, true );
1327 - if ( is_wp_error( $existing_table ) ) {
1328 - // Add an error code to the existing WP_Error.
1329 - $existing_table->add( 'table_import_replace_table_load', '', $existing_table_id );
1330 - return $existing_table;
1331 - }
1332 - // Don't change name and description when a table is replaced.
1333 - $imported_table['name'] = $existing_table['name'];
1334 - $imported_table['description'] = $existing_table['description'];
1335 - if ( isset( $imported_table['visibility'] ) && isset( $imported_table['visibility']['rows'] ) && isset( $imported_table['visibility']['columns'] ) ) {
1336 - $existing_table['visibility']['rows'] = $imported_table['visibility']['rows'];
1337 - $existing_table['visibility']['columns'] = $imported_table['visibility']['columns'];
1338 - }
1339 - break;
1340 - case 'append':
1341 - // Load table, with table data, options, and visibility settings.
1342 - $existing_table = TablePress::$model_table->load( $existing_table_id, true, true );
1343 - if ( is_wp_error( $existing_table ) ) {
1344 - // Add an error code to the existing WP_Error.
1345 - $existing_table->add( 'table_import_append_table_load', '', $existing_table_id );
1346 - return $existing_table;
1347 - }
1348 - if ( isset( $existing_table['is_corrupted'] ) && $existing_table['is_corrupted'] ) {
1349 - return new WP_Error( 'table_import_append_table_load_corrupted', '', $existing_table_id );
1350 - }
1351 - // Don't change name and description when a table is appended to.
1352 - $imported_table['name'] = $existing_table['name'];
1353 - $imported_table['description'] = $existing_table['description'];
1354 - // Actual appending:
1355 - $imported_table['data'] = array_merge( $existing_table['data'], $imported_table['data'] );
1356 - $imported_table['data'] = $this->importer->pad_array_to_max_cols( $imported_table['data'] );
1357 - // Append visibility information for rows.
1358 - if ( isset( $imported_table['visibility'] ) && isset( $imported_table['visibility']['rows'] ) ) {
1359 - $existing_table['visibility']['rows'] = array_merge( $existing_table['visibility']['rows'], $imported_table['visibility']['rows'] );
1360 - }
1361 - // When appending, do not overwrite options.
1362 - if ( isset( $imported_table['options'] ) ) {
1363 - unset( $imported_table['options'] );
1364 - }
1365 - break;
1366 - default:
1367 - return new WP_Error( 'table_import_import_type_invalid', '', $import_type );
1368 - }
1369 -
1370 - // Merge new or existing table with information from the imported table.
1371 - $imported_table['id'] = $existing_table['id']; // will be false for new table or the existing table ID
1372 - // Cut visibility array (if the imported table is smaller), and pad correctly if imported table is bigger than existing table (or new template).
1373 - $num_rows = count( $imported_table['data'] );
1374 - $num_columns = count( $imported_table['data'][0] );
1375 - $imported_table['visibility'] = array(
1376 - 'rows' => array_pad( array_slice( $existing_table['visibility']['rows'], 0, $num_rows ), $num_rows, 1 ),
1377 - 'columns' => array_pad( array_slice( $existing_table['visibility']['columns'], 0, $num_columns ), $num_columns, 1 ),
1378 - );
1379 -
1380 - // Check if new data is ok.
1381 - $table = TablePress::$model_table->prepare_table( $existing_table, $imported_table, false );
1382 - if ( is_wp_error( $table ) ) {
1383 - // Add an error code to the existing WP_Error.
1384 - $table->add( 'table_import_table_prepare', '' );
1385 - return $table;
1386 - }
1387 -
1388 - // DataTables Custom Commands can only be edit by trusted users.
1389 - if ( ! current_user_can( 'unfiltered_html' ) ) {
1390 - $table['options']['datatables_custom_commands'] = $existing_table['options']['datatables_custom_commands'];
1391 - }
1392 -
1393 - // Replace existing table or add new table.
1394 - if ( in_array( $import_type, array( 'replace', 'append' ), true ) ) {
1395 - // Replace existing table with imported/appended table.
1396 - $table_id = TablePress::$model_table->save( $table );
1397 - } else {
1398 - // Add the imported table (and get its first ID).
1399 - $table_id = TablePress::$model_table->add( $table );
1400 - }
1401 -
1402 - if ( is_wp_error( $table_id ) ) {
1403 - // Add an error code to the existing WP_Error.
1404 - $table_id->add( 'table_import_table_save_or_add', '' );
1405 - return $table_id;
1406 - }
1407 -
1408 - // Try to use ID from imported file (e.g. in full JSON format table).
1409 - if ( false !== $table_id_in_import && $table_id !== $table_id_in_import && current_user_can( 'tablepress_edit_table_id', $table_id ) ) {
1410 - $id_changed = TablePress::$model_table->change_table_id( $table_id, $table_id_in_import );
1411 - if ( ! is_wp_error( $id_changed ) ) {
1412 - $table_id = $table_id_in_import;
1213 + if ( is_wp_error( $import ) || 0 < count( $import['errors'] ) ) {
1214 + $redirect_parameters = array(
1215 + 'action' => 'import',
1216 + 'message' => 'error_import',
1217 + 'import_type' => $import_config['type'],
1218 + 'import_existing_table' => $import_config['existing_table'],
1219 + 'import_source' => $import_config['source'],
1220 + 'legacy_import' => $import_config['legacy_import'],
1221 + );
1222 + if ( in_array( $import_config['source'], array( 'url', 'server' ), true ) ) {
1223 + $redirect_parameters[ "import_{$import_config['source']}" ] = $import_config[ $import_config['source'] ];
1413 1224 }
1414 - }
1415 -
1416 - return $table_id;
1417 - }
1418 -
1419 - /**
1420 - * Import data from WP-Table Reloaded from the WordPress database.
1421 - *
1422 - * @since 1.0.0
1423 - *
1424 - * @param bool $import_tables Whether tables shall be imported.
1425 - * @param bool $import_css Whether Plugin Options (only CSS related right now) shall be imported.
1426 - */
1427 - protected function _import_from_wp_table_reloaded_db( $import_tables, $import_css ) {
1428 - if ( false === get_option( 'wp_table_reloaded_options', false ) || false === get_option( 'wp_table_reloaded_tables', false ) ) {
1429 - TablePress::redirect( array( 'action' => 'import', 'message' => 'error_wp_table_reloaded_not_installed' ) );
1430 - }
1431 -
1432 - $wp_table_reloaded_options = get_option( 'wp_table_reloaded_options', false );
1433 - if ( empty( $wp_table_reloaded_options ) ) {
1434 - $wp_table_reloaded_options = array();
1435 - }
1436 -
1437 - // Import WP-Table Reloaded tables.
1438 - $not_imported_tables = $imported_tables = $imported_other_id_tables = array();
1439 - if ( $import_tables ) {
1440 - $wp_table_reloaded_tables_list = get_option( 'wp_table_reloaded_tables', array() );
1441 - foreach ( $wp_table_reloaded_tables_list as $wptr_table_id => $table_option_name ) {
1442 - $wptr_table = get_option( $table_option_name, array() );
1443 - $import_status = $this->_import_wp_table_reloaded_table( $wptr_table, $wp_table_reloaded_options );
1444 - switch ( $import_status ) {
1445 - case 0:
1446 - $not_imported_tables[] = $wptr_table_id;
1447 - break;
1448 - case 1:
1449 - $imported_tables[] = $wptr_table_id;
1450 - break;
1451 - case 2:
1452 - $imported_other_id_tables[] = $wptr_table_id;
1453 - break;
1225 + if ( is_wp_error( $import ) ) {
1226 + $redirect_parameters['error_details'] = TablePress::get_wp_error_string( $import );
1227 + } elseif ( 0 < count( $import['errors'] ) ) {
1228 + $wp_error_strings = array();
1229 + foreach ( $import['errors'] as $file ) {
1230 + $wp_error_strings[] = TablePress::get_wp_error_string( $file->error );
1454 1231 }
1232 + $redirect_parameters['error_details'] = implode( ', ', $wp_error_strings );
1455 1233 }
1234 + TablePress::redirect( $redirect_parameters );
1456 1235 }
1457 1236
1458 - // Import WP-Table Reloaded Plugin Options (currently only CSS related options).
1459 - $imported_css = false;
1460 - if ( $import_css ) {
1461 - $imported_css = $this->_import_wp_table_reloaded_plugin_options( $wp_table_reloaded_options );
1462 - }
1463 -
1464 - // @TODO: Better handling of the different cases of imported/imported-without-ID-change/not-imported tables.
1465 - if ( count( $imported_tables ) > 1 ) {
1466 - TablePress::redirect( array( 'action' => 'list', 'message' => 'success_import_wp_table_reloaded' ) );
1467 - } elseif ( 1 === count( $imported_tables ) ) {
1468 - TablePress::redirect( array( 'action' => 'edit', 'table_id' => $imported_tables[0], 'message' => 'success_import_wp_table_reloaded' ) );
1469 - }
1470 - if ( count( $imported_other_id_tables ) > 0 ) {
1471 - TablePress::redirect( array( 'action' => 'list', 'message' => 'success_import_wp_table_reloaded' ) );
1472 - } elseif ( $imported_css ) {
1473 - TablePress::redirect( array( 'action' => 'options', 'message' => 'success_import_wp_table_reloaded' ) );
1237 + // At this point, there were no import errors.
1238 + if ( count( $import['tables'] ) > 1 ) {
1239 + TablePress::redirect( array( 'action' => 'list', 'message' => 'success_import' ) );
1240 + } elseif ( 1 === count( $import['tables'] ) ) {
1241 + TablePress::redirect( array( 'action' => 'edit', 'table_id' => $import['tables'][0]['id'], 'message' => 'success_import' ) );
1474 1242 } else {
1475 - TablePress::redirect( array( 'action' => 'import', 'message' => 'error_import_wp_table_reloaded' ) );
1243 + TablePress::redirect( array( 'action' => 'import', 'message' => 'error_import', 'error_details' => 'The number of imported tables is invalid.' ) );
1476 1244 }
1477 1245 }
1478 1246
1479 - /**
1480 - * Import data from WP-Table Reloaded from a WP-Table Reloaded Dump File.
1481 - *
1482 - * @since 1.0.0
1483 - *
1484 - * @param bool $import_tables Whether tables shall be imported.
1485 - * @param bool $import_css Whether Plugin Options (only CSS related right now) shall be imported.
1486 - */
1487 - protected function _import_from_wp_table_reloaded_dump_file( $import_tables, $import_css ) {
1488 - if ( empty( $_FILES['import_wp_table_reloaded_file_upload'] ) || empty( $_FILES['import_wp_table_reloaded_file_upload']['tmp_name'] ) || UPLOAD_ERR_OK !== $_FILES['import_wp_table_reloaded_file_upload']['error'] ) {
1489 - TablePress::redirect( array( 'action' => 'import', 'message' => 'error_wp_table_reloaded_dump_file' ) );
1490 - }
1491 -
1492 - $dump_file = file_get_contents( $_FILES['import_wp_table_reloaded_file_upload']['tmp_name'] );
1493 - $dump_file = unserialize( $dump_file );
1494 - if ( empty( $dump_file ) ) {
1495 - @unlink( $_FILES['import_wp_table_reloaded_file_upload']['tmp_name'] );
1496 - TablePress::redirect( array( 'action' => 'import', 'message' => 'error_wp_table_reloaded_dump_file' ) );
1497 - }
1498 - if ( empty( $dump_file['options'] ) || ! is_array( $dump_file['options'] ) ) {
1499 - $dump_file['options'] = array();
1500 - }
1501 -
1502 - // Import WP-Table Reloaded tables.
1503 - $not_imported_tables = $imported_tables = $imported_other_id_tables = array();
1504 - if ( $import_tables && ! empty( $dump_file['tables'] ) ) {
1505 - foreach ( $dump_file['tables'] as $wptr_table_id => $wptr_table ) {
1506 - $import_status = $this->_import_wp_table_reloaded_table( $wptr_table, $dump_file['options'] );
1507 - switch ( $import_status ) {
1508 - case 0:
1509 - $not_imported_tables[] = $wptr_table_id;
1510 - break;
1511 - case 1:
1512 - $imported_tables[] = $wptr_table_id;
1513 - break;
1514 - case 2:
1515 - $imported_other_id_tables[] = $wptr_table_id;
1516 - break;
1517 - }
1518 - }
1519 - }
1520 -
1521 - // Import WP-Table Reloaded Plugin Options (currently only CSS related options).
1522 - $imported_css = false;
1523 - if ( $import_css ) {
1524 - $imported_css = $this->_import_wp_table_reloaded_plugin_options( $dump_file['options'] );
1525 - }
1526 -
1527 - @unlink( $_FILES['import_wp_table_reloaded_file_upload']['tmp_name'] );
1528 - // @TODO: Better handling of the different cases of imported/imported-without-ID-change/not-imported tables.
1529 - if ( count( $imported_tables ) > 1 ) {
1530 - TablePress::redirect( array( 'action' => 'list', 'message' => 'success_import_wp_table_reloaded' ) );
1531 - } elseif ( 1 === count( $imported_tables ) ) {
1532 - TablePress::redirect( array( 'action' => 'edit', 'table_id' => $imported_tables[0], 'message' => 'success_import_wp_table_reloaded' ) );
1533 - }
1534 - if ( count( $imported_other_id_tables ) > 0 ) {
1535 - TablePress::redirect( array( 'action' => 'list', 'message' => 'success_import_wp_table_reloaded' ) );
1536 - } elseif ( $imported_css ) {
1537 - TablePress::redirect( array( 'action' => 'options', 'message' => 'success_import_wp_table_reloaded' ) );
1538 - } else {
1539 - TablePress::redirect( array( 'action' => 'import', 'message' => 'error_import_wp_table_reloaded' ) );
1540 - }
1541 - }
1542 -
1543 - /**
1544 - * Import a WP-Table Reloaded table.
1545 - *
1546 - * @since 1.0.0
1547 - *
1548 - * @param array $wptr_table WP-Table Reloaded table.
1549 - * @param array $wp_table_reloaded_options WP-Table Reloaded Plugin Options.
1550 - * @return int Import status: 0=Import failed; 1=Imported with ID change; 2=Imported without ID change.
1551 - */
1552 - protected function _import_wp_table_reloaded_table( array $wptr_table, array $wp_table_reloaded_options ) {
1553 - if ( empty( $wptr_table ) ) {
1554 - return 0; // 0 means Import failed.
1555 - }
1556 -
1557 - // Perform sanity checks of imported table.
1558 - if ( ! isset( $wptr_table['name'] )
1559 - || ! isset( $wptr_table['description'] )
1560 - || empty( $wptr_table['data'] )
1561 - || empty( $wptr_table['options'] ) ) {
1562 - return 0; // 0 means Import failed.
1563 - }
1564 -
1565 - // Data is slashed in WP-Table Reloaded.
1566 - $wptr_table = wp_unslash( $wptr_table );
1567 -
1568 - // Table was loaded, import the data, table options, and visibility.
1569 - //
1570 - // Create a new table array with information from the imported table.
1571 - $new_table = array(
1572 - 'name' => $wptr_table['name'],
1573 - 'description' => $wptr_table['description'],
1574 - 'data' => $wptr_table['data'],
1575 - 'options' => array(),
1576 - 'visibility' => array(
1577 - 'rows' => array_fill( 0, count( $wptr_table['data'] ), 1 ),
1578 - 'columns' => array_fill( 0, count( $wptr_table['data'][0] ), 1 ),
1579 - ),
1580 - );
1581 - if ( isset( $wptr_table['last_modified'] ) ) {
1582 - $new_table['last_modified'] = $wptr_table['last_modified'];
1583 - }
1584 - if ( isset( $wptr_table['last_editor_id'] ) ) {
1585 - $new_table['author'] = $wptr_table['last_editor_id'];
1586 - }
1587 - if ( isset( $wptr_table['options']['last_editor_id'] ) ) {
1588 - $new_table['options']['last_editor'] = $wptr_table['last_editor_id'];
1589 - }
1590 - if ( isset( $wptr_table['options']['first_row_th'] ) ) {
1591 - $new_table['options']['table_head'] = $wptr_table['options']['first_row_th'];
1592 - }
1593 - if ( isset( $wptr_table['options']['table_footer'] ) ) {
1594 - $new_table['options']['table_foot'] = $wptr_table['options']['table_footer'];
1595 - }
1596 - if ( isset( $wptr_table['options']['custom_css_class'] ) ) {
1597 - $new_table['options']['extra_css_classes'] = $wptr_table['options']['custom_css_class'];
1598 - }
1599 - if ( isset( $wptr_table['options']['use_tablesorter'] ) && isset( $wp_table_reloaded_options['enable_tablesorter'] ) ) {
1600 - if ( $wp_table_reloaded_options['enable_tablesorter'] ) {
1601 - $new_table['options']['use_datatables'] = $wptr_table['options']['use_tablesorter'];
1602 - } else {
1603 - $new_table['options']['use_datatables'] = false;
1604 - }
1605 - }
1606 - // Array key is the same in both plugins for the following options.
1607 - foreach ( array(
1608 - 'alternating_row_colors',
1609 - 'row_hover',
1610 - 'print_name',
1611 - 'print_name_position',
1612 - 'print_description',
1613 - 'print_description_position',
1614 - 'datatables_sort',
1615 - 'datatables_filter',
1616 - 'datatables_paginate',
1617 - 'datatables_lengthchange',
1618 - 'datatables_paginate_entries',
1619 - 'datatables_info',
1620 - ) as $_option ) {
1621 - if ( isset( $wptr_table['options'][ $_option ] ) ) {
1622 - $new_table['options'][ $_option ] = $wptr_table['options'][ $_option ];
1623 - }
1624 - }
1625 - if ( isset( $wptr_table['options']['datatables_customcommands'] ) && current_user_can( 'unfiltered_html' ) ) {
1626 - $new_table['options']['datatables_custom_commands'] = $wptr_table['options']['datatables_customcommands'];
1627 - }
1628 - // Not imported: $wptr_table['options']['cache_table_output'].
1629 - // Not imported: $wptr_table['custom_fields'].
1630 -
1631 - // Fix visibility: WP-Table Reloaded uses 0 and 1 the other way around.
1632 - foreach ( array_keys( $wptr_table['visibility']['rows'], true ) as $row_idx ) {
1633 - $new_table['visibility']['rows'][ $row_idx ] = 0;
1634 - }
1635 - foreach ( array_keys( $wptr_table['visibility']['columns'], true ) as $column_idx ) {
1636 - $new_table['visibility']['columns'][ $column_idx ] = 0;
1637 - }
1638 -
1639 - // Merge this data into an empty table template.
1640 - $table = TablePress::$model_table->prepare_table( TablePress::$model_table->get_table_template(), $new_table, false );
1641 - if ( is_wp_error( $table ) ) {
1642 - return 0; // 0 means Import failed.
1643 - }
1644 -
1645 - // Add the new table (and get its first ID).
1646 - $tp_table_id = TablePress::$model_table->add( $table );
1647 - if ( is_wp_error( $tp_table_id ) ) {
1648 - return 0; // 0 means Import failed.
1649 - }
1650 -
1651 - // Change table ID to the ID the table had in WP-Table Reloaded (except if that ID is already taken).
1652 - $id_changed = TablePress::$model_table->change_table_id( $tp_table_id, $wptr_table['id'] );
1653 - if ( is_wp_error( $id_changed ) ) {
1654 - return 2; // 2 means Imported without ID change.
1655 - }
1656 -
1657 - return 1; // 1 means Imported with ID change.
1658 - }
1659 -
1660 - /**
1661 - * Import WP-Table Reloaded Plugin Options (currently just CSS related options).
1662 - *
1663 - * @since 1.0.0
1664 - *
1665 - * @param array $wp_table_reloaded_options Plugin Options of WP-Table Reloaded that shall be imported.
1666 - * @return bool Whether the import was successful or not (on at least on option).
1667 - */
1668 - protected function _import_wp_table_reloaded_plugin_options( array $wp_table_reloaded_options ) {
1669 - if ( ! current_user_can( 'tablepress_edit_options' ) ) {
1670 - return false;
1671 - }
1672 -
1673 - $imported_options = array();
1674 - $imported_options['use_custom_css'] = ( isset( $wp_table_reloaded_options['use_custom_css'] ) ) ? (bool) $wp_table_reloaded_options['use_custom_css'] : false;
1675 - if ( isset( $wp_table_reloaded_options['custom_css'] ) ) {
1676 - // Automatically convert WP-Table Reloaded Custom CSS to TablePress Custom CSS by search/replacing classes and IDs.
1677 - $imported_options['custom_css'] = wp_unslash( $wp_table_reloaded_options['custom_css'] ); // Be careful when removing this, as it might break CSS comments from old Dump Files.
1678 - $imported_options['custom_css'] = str_replace( '#wp-table-reloaded-id-', '#tablepress-', $imported_options['custom_css'] );
1679 - $imported_options['custom_css'] = str_replace( '-no-1', '', $imported_options['custom_css'] );
1680 - $imported_options['custom_css'] = str_replace( '.wp-table-reloaded-id-', '.tablepress-id-', $imported_options['custom_css'] );
1681 - $imported_options['custom_css'] = str_replace( '.wp-table-reloaded', '.tablepress', $imported_options['custom_css'] );
1682 -
1683 - $tablepress_css = TablePress::load_class( 'TablePress_CSS', 'class-css.php', 'classes' );
1684 - // Sanitize and tidy up Custom CSS.
1685 - $imported_options['custom_css'] = $tablepress_css->sanitize_css( $imported_options['custom_css'] );
1686 - // Minify Custom CSS.
1687 - $imported_options['custom_css_minified'] = $tablepress_css->minify_css( $imported_options['custom_css'] );
1688 -
1689 - // Maybe update CSS files as well.
1690 - //
1691 - // Only write files, if "Custom CSS" is to be used, and if there is "Custom CSS"
1692 - if ( $imported_options['use_custom_css'] && '' !== $imported_options['custom_css'] ) {
1693 - $result = $tablepress_css->save_custom_css_to_file( $imported_options['custom_css'], $imported_options['custom_css_minified'] );
1694 - // If saving was successful, use "Custom CSS" file.
1695 - $imported_options['use_custom_css_file'] = $result;
1696 - // If saving was successful, increase the "Custom CSS" version number for cache busting.
1697 - if ( $result ) {
1698 - $imported_options['custom_css_version'] = TablePress::$model_options->get( 'custom_css_version' ) + 1;
1699 - }
1700 - }
1701 - }
1702 -
1703 - // Save gathered imported options.
1704 - if ( empty( $imported_options ) ) {
1705 - return false;
1706 - }
1707 -
1708 - TablePress::$model_options->update( $imported_options );
1709 -
1710 - return true;
1711 - }
1712 -
1713 1247 /*
1714 - * Save GET actions.
1248 + * HTTP GET actions.
1715 1249 */
1716 1250
1717 1251 /**
1718 1252 * Hide a header message on an admin screen.
@@ -1718,14 +1252,14 @@
1718 1252 * Hide a header message on an admin screen.
1719 1253 *
1720 1254 * @since 1.0.0
1721 1255 */
1722 - public function handle_get_action_hide_message() {
1256 + public function handle_get_action_hide_message(): void {
1723 1257 $message_item = ! empty( $_GET['item'] ) ? $_GET['item'] : '';
1724 1258 TablePress::check_nonce( 'hide_message', $message_item );
1725 1259
1726 1260 if ( ! current_user_can( 'tablepress_list_tables' ) ) {
1727 - wp_die( __( 'You do not have sufficient permissions to access this page.', 'default' ), 403 );
1261 + wp_die( __( 'Sorry, you are not allowed to access this page.', 'default' ), 403 );
1728 1262 }
1729 1263
1730 1264 TablePress::$model_options->update( "message_{$message_item}", false );
1731 1265
@@ -1737,9 +1271,9 @@
1737 1271 * Delete a table.
1738 1272 *
1739 1273 * @since 1.0.0
1740 1274 */
1741 - public function handle_get_action_delete_table() {
1275 + public function handle_get_action_delete_table(): void {
1742 1276 $table_id = ( ! empty( $_GET['item'] ) ) ? $_GET['item'] : false;
1743 1277 TablePress::check_nonce( 'delete_table', $table_id );
1744 1278
1745 1279 $return = ! empty( $_GET['return'] ) ? $_GET['return'] : 'list';
@@ -1744,20 +1278,20 @@
1744 1278
1745 1279 $return = ! empty( $_GET['return'] ) ? $_GET['return'] : 'list';
1746 1280 $return_item = ! empty( $_GET['return_item'] ) ? $_GET['return_item'] : false;
1747 1281
1748 - // Nonce check should actually catch this already.
1282 + // The nonce check should actually catch this already.
1749 1283 if ( false === $table_id ) {
1750 1284 TablePress::redirect( array( 'action' => $return, 'message' => 'error_delete', 'table_id' => $return_item ) );
1751 1285 }
1752 1286
1753 1287 if ( ! current_user_can( 'tablepress_delete_table', $table_id ) ) {
1754 - wp_die( __( 'You do not have sufficient permissions to access this page.', 'default' ), 403 );
1288 + wp_die( __( 'Sorry, you are not allowed to access this page.', 'default' ), 403 );
1755 1289 }
1756 1290
1757 1291 $deleted = TablePress::$model_table->delete( $table_id );
1758 1292 if ( is_wp_error( $deleted ) ) {
1759 - TablePress::redirect( array( 'action' => $return, 'message' => 'error_delete', 'table_id' => $return_item ) );
1293 + TablePress::redirect( array( 'action' => $return, 'message' => 'error_delete', 'table_id' => $return_item, 'error_details' => TablePress::get_wp_error_string( $deleted ) ) );
1760 1294 }
1761 1295
1762 1296 /*
1763 1297 * Slightly more complex redirect method, to account for sort, search, and pagination in the WP_List_Table on the List View,
@@ -1778,9 +1312,9 @@
1778 1312 * Copy a table.
1779 1313 *
1780 1314 * @since 1.0.0
1781 1315 */
1782 - public function handle_get_action_copy_table() {
1316 + public function handle_get_action_copy_table(): void {
1783 1317 $table_id = ( ! empty( $_GET['item'] ) ) ? $_GET['item'] : false;
1784 1318 TablePress::check_nonce( 'copy_table', $table_id );
1785 1319
1786 1320 $return = ! empty( $_GET['return'] ) ? $_GET['return'] : 'list';
@@ -1785,25 +1319,22 @@
1785 1319
1786 1320 $return = ! empty( $_GET['return'] ) ? $_GET['return'] : 'list';
1787 1321 $return_item = ! empty( $_GET['return_item'] ) ? $_GET['return_item'] : false;
1788 1322
1789 - // Nonce check should actually catch this already.
1323 + // The nonce check should actually catch this already.
1790 1324 if ( false === $table_id ) {
1791 1325 TablePress::redirect( array( 'action' => $return, 'message' => 'error_copy', 'table_id' => $return_item ) );
1792 1326 }
1793 1327
1794 1328 if ( ! current_user_can( 'tablepress_copy_table', $table_id ) ) {
1795 - wp_die( __( 'You do not have sufficient permissions to access this page.', 'default' ), 403 );
1329 + wp_die( __( 'Sorry, you are not allowed to access this page.', 'default' ), 403 );
1796 1330 }
1797 1331
1798 - $this->init_i18n_support(); // for the translation of "Copy of".
1799 -
1800 1332 $copy_table_id = TablePress::$model_table->copy( $table_id );
1801 1333 if ( is_wp_error( $copy_table_id ) ) {
1802 - TablePress::redirect( array( 'action' => $return, 'message' => 'error_copy', 'table_id' => $return_item ) );
1803 - } else {
1804 - $return_item = $copy_table_id;
1334 + TablePress::redirect( array( 'action' => $return, 'message' => 'error_copy', 'table_id' => $return_item, 'error_details' => TablePress::get_wp_error_string( $copy_table_id ) ) );
1805 1335 }
1336 + $return_item = $copy_table_id;
1806 1337
1807 1338 /*
1808 1339 * Slightly more complex redirect method, to account for sort, search, and pagination in the WP_List_Table on the List View,
1809 1340 * but only if this action succeeds, to have everything fresh in the event of an error.
@@ -1823,14 +1354,12 @@
1823 1354 * Preview a table.
1824 1355 *
1825 1356 * @since 1.0.0
1826 1357 */
1827 - public function handle_get_action_preview_table() {
1358 + public function handle_get_action_preview_table(): void {
1828 1359 $table_id = ( ! empty( $_GET['item'] ) ) ? $_GET['item'] : false;
1829 1360 TablePress::check_nonce( 'preview_table', $table_id );
1830 1361
1831 - $this->init_i18n_support();
1832 -
1833 1362 // Nonce check should actually catch this already.
1834 1363 if ( false === $table_id ) {
1835 1364 wp_die( __( 'The preview could not be loaded.', 'tablepress' ), __( 'Preview', 'tablepress' ) );
1836 1365 }
@@ -1835,9 +1364,9 @@
1835 1364 wp_die( __( 'The preview could not be loaded.', 'tablepress' ), __( 'Preview', 'tablepress' ) );
1836 1365 }
1837 1366
1838 1367 if ( ! current_user_can( 'tablepress_preview_table', $table_id ) ) {
1839 - wp_die( __( 'You do not have sufficient permissions to access this page.', 'default' ), 403 );
1368 + wp_die( __( 'Sorry, you are not allowed to access this page.', 'default' ), 403 );
1840 1369 }
1841 1370
1842 1371 // Load table, with table data, options, and visibility settings.
1843 1372 $table = TablePress::$model_table->load( $table_id, true, true );
@@ -1844,10 +1373,12 @@
1844 1373 if ( is_wp_error( $table ) ) {
1845 1374 wp_die( __( 'The table could not be loaded.', 'tablepress' ), __( 'Preview', 'tablepress' ) );
1846 1375 }
1847 1376
1848 - // Sanitize all table data to remove unsafe HTML from the preview output.
1849 - $table = TablePress::$model_table->sanitize( $table );
1377 + // Sanitize all table data to remove unsafe HTML from the preview output, if the user is not allowed to work with unfiltered HTML.
1378 + if ( ! current_user_can( 'unfiltered_html' ) ) {
1379 + $table = TablePress::$model_table->sanitize( $table );
1380 + }
1850 1381
1851 1382 // Create a render class instance.
1852 1383 $_render = TablePress::load_class( 'TablePress_Render', 'class-render.php', 'classes' );
1853 1384 // Merge desired options with default render options (see TablePress_Controller_Frontend::shortcode_table()).
@@ -1856,18 +1387,22 @@
1856 1387 $default_render_options = apply_filters( 'tablepress_shortcode_table_default_shortcode_atts', $default_render_options );
1857 1388 $render_options = shortcode_atts( $default_render_options, $table['options'] );
1858 1389 /** This filter is documented in controllers/controller-frontend.php */
1859 1390 $render_options = apply_filters( 'tablepress_shortcode_table_shortcode_atts', $render_options );
1391 + $render_options['html_id'] = "tablepress-{$table['id']}";
1392 + $render_options['block_preview'] = true;
1860 1393 $_render->set_input( $table, $render_options );
1861 1394 $view_data = array(
1862 - 'table_id' => $table_id,
1863 - 'head_html' => $_render->get_preview_css(),
1864 - 'body_html' => $_render->get_output(),
1395 + 'table_id' => $table_id,
1396 + 'head_html' => $_render->get_preview_css(),
1397 + 'body_html' => $_render->get_output( 'html' ),
1398 + 'site_uses_block_editor' => TablePress::site_uses_block_editor(),
1865 1399 );
1866 1400
1867 1401 $custom_css = TablePress::$model_options->get( 'custom_css' );
1868 - if ( ! empty( $custom_css ) ) {
1869 - $view_data['head_html'] .= "<style type=\"text/css\">\n{$custom_css}\n</style>\n";
1402 + $use_custom_css = ( TablePress::$model_options->get( 'use_custom_css' ) && '' !== $custom_css );
1403 + if ( $use_custom_css ) {
1404 + $view_data['head_html'] .= "<style>\n{$custom_css}\n</style>\n";
1870 1405 }
1871 1406
1872 1407 // Prepare, initialize, and render the view.
1873 1408 $this->view = TablePress::load_view( 'preview_table', $view_data );
@@ -1874,21 +1409,19 @@
1874 1409 $this->view->render();
1875 1410 }
1876 1411
1877 1412 /**
1878 - * Show a list of tables in the Editor toolbar Thickbox (opened by TinyMCE or Quicktags button).
1413 + * Shows a list of tables in the Editor toolbar Thickbox (opened by TinyMCE or Quicktags button).
1879 1414 *
1880 1415 * @since 1.0.0
1881 1416 */
1882 - public function handle_get_action_editor_button_thickbox() {
1417 + public function handle_get_action_editor_button_thickbox(): void {
1883 1418 TablePress::check_nonce( 'editor_button_thickbox' );
1884 1419
1885 1420 if ( ! current_user_can( 'tablepress_list_tables' ) ) {
1886 - wp_die( __( 'You do not have sufficient permissions to access this page.', 'default' ), 403 );
1421 + wp_die( __( 'Sorry, you are not allowed to access this page.', 'default' ), 403 );
1887 1422 }
1888 1423
1889 - $this->init_i18n_support();
1890 -
1891 1424 $view_data = array(
1892 1425 // Load all table IDs without priming the post meta cache, as table options/visibility are not needed.
1893 1426 'table_ids' => TablePress::$model_table->load_all( false ),
1894 1427 );
@@ -1904,15 +1437,15 @@
1904 1437 * Uninstall TablePress, and delete all tables and options.
1905 1438 *
1906 1439 * @since 1.0.0
1907 1440 */
1908 - public function handle_get_action_uninstall_tablepress() {
1441 + public function handle_get_action_uninstall_tablepress(): void {
1909 1442 TablePress::check_nonce( 'uninstall_tablepress' );
1910 1443
1911 1444 $plugin = TABLEPRESS_BASENAME;
1912 1445
1913 1446 if ( ! current_user_can( 'deactivate_plugin', $plugin ) || ! current_user_can( 'tablepress_edit_options' ) || ! current_user_can( 'tablepress_delete_tables' ) || is_plugin_active_for_network( $plugin ) ) {
1914 - wp_die( __( 'You do not have sufficient permissions to access this page.', 'default' ), 403 );
1447 + wp_die( __( 'Sorry, you are not allowed to access this page.', 'default' ), 403 );
1915 1448 }
1916 1449
1917 1450 // Deactivate TablePress for the site (but not for the network).
1918 1451 deactivate_plugins( $plugin, false, false );
@@ -1926,11 +1459,9 @@
1926 1459
1927 1460 TablePress::$model_table->destroy();
1928 1461 TablePress::$model_options->destroy();
1929 1462
1930 - $this->init_i18n_support();
1931 -
1932 - $output = '<strong>' . __( 'TablePress was uninstalled successfully.', 'tablepress' ) . '</strong><br /><br />';
1463 + $output = '<strong>' . __( 'TablePress was uninstalled successfully.', 'tablepress' ) . '</strong><br><br>';
1933 1464 $output .= __( 'All tables, data, and options were deleted.', 'tablepress' );
1934 1465 if ( is_multisite() ) {
1935 1466 $output .= ' ' . __( 'You may now ask the network admin to delete the plugin&#8217;s folder <code>tablepress</code> from the server, if no other site in the network uses it.', 'tablepress' );
1936 1467 } else {
@@ -1937,9 +1468,9 @@
1937 1468 $output .= ' ' . __( 'You may now manually delete the plugin&#8217;s folder <code>tablepress</code> from the <code>plugins</code> directory on your server or use the &#8220;Delete&#8221; link for TablePress on the WordPress &#8220;Plugins&#8221; page.', 'tablepress' );
1938 1469 }
1939 1470 if ( $css_files_deleted ) {
1940 1471 $output .= ' ' . __( 'Your TablePress &#8220;Custom CSS&#8221; files have been deleted automatically.', 'tablepress' );
1941 - } else {
1472 + } else { // phpcs:ignore Universal.ControlStructures.DisallowLonelyIf.Found
1942 1473 if ( is_multisite() ) {
1943 1474 $output .= ' ' . __( 'Please also ask him to delete your TablePress &#8220;Custom CSS&#8221; files from the server.', 'tablepress' );
1944 1475 } else {
1945 1476 $output .= ' ' . __( 'You may now also delete your TablePress &#8220;Custom CSS&#8221; files in the <code>wp-content</code> folder.', 'tablepress' );