| @@ -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,37 +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 | 42 | * Instance of the TablePress Admin View that is rendered. |
| 42 | 43 | * |
| 43 | 44 | * @since 1.0.0 |
| 44 | - * @var TablePress_View | |
| 45 | 45 | */ |
| 46 | - protected $view; | |
| 46 | + protected \TablePress_View $view; | |
| 47 | 47 | |
| 48 | 48 | /** |
| 49 | - * Instance of the TablePress Importer. | |
| 50 | - * | |
| 51 | - * @since 1.0.0 | |
| 52 | - * @var TablePress_Import | |
| 53 | - */ | |
| 54 | - protected $importer; | |
| 55 | - | |
| 56 | - /** | |
| 57 | 49 | * Initialize the Admin Controller, determine location the admin menu, set up actions. |
| 58 | 50 | * |
| 59 | 51 | * @since 1.0.0 |
| 60 | 52 | */ |
| @@ -61,12 +53,15 @@ | ||
| 61 | 53 | public function __construct() { |
| 62 | 54 | parent::__construct(); |
| 63 | 55 | |
| 64 | 56 | // Handler for changing the number of shown tables in the list of tables (via WP List Table class). |
| 65 | - 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 ); | |
| 66 | 58 | |
| 67 | 59 | add_action( 'admin_menu', array( $this, 'add_admin_menu_entry' ) ); |
| 68 | 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' ) ); | |
| 69 | 64 | } |
| 70 | 65 | |
| 71 | 66 | /** |
| 72 | 67 | * Handler for changing the number of shown tables in the list of tables (via WP List Table class). |
| @@ -72,19 +67,15 @@ | ||
| 72 | 67 | * Handler for changing the number of shown tables in the list of tables (via WP List Table class). |
| 73 | 68 | * |
| 74 | 69 | * @since 1.0.0 |
| 75 | 70 | * |
| 76 | - * @param bool $false Current value of the filter (probably bool false). | |
| 77 | - * @param string $option Option in which the setting is stored. | |
| 78 | - * @param int $value Current value of the setting. | |
| 79 | - * @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 | |
| 80 | 75 | */ |
| 81 | - public function save_list_tables_screen_option( $false, $option, $value ) { | |
| 82 | - if ( 'tablepress_list_per_page' === $option ) { | |
| 83 | - return $value; | |
| 84 | - } else { | |
| 85 | - return $false; | |
| 86 | - } | |
| 76 | + public function save_list_tables_screen_option( /* mixed */ $screen_option, string $option, int $value ): int { | |
| 77 | + return $value; | |
| 87 | 78 | } |
| 88 | 79 | |
| 89 | 80 | /** |
| 90 | 81 | * Add admin screens to the correct place in the admin menu. |
| @@ -90,13 +81,13 @@ | ||
| 90 | 81 | * Add admin screens to the correct place in the admin menu. |
| 91 | 82 | * |
| 92 | 83 | * @since 1.0.0 |
| 93 | 84 | */ |
| 94 | - public function add_admin_menu_entry() { | |
| 85 | + public function add_admin_menu_entry(): void { | |
| 95 | 86 | // Callback for all menu entries. |
| 96 | 87 | $callback = array( $this, 'show_admin_page' ); |
| 97 | 88 | /** |
| 98 | - * Filter the TablePress admin menu entry name. | |
| 89 | + * Filters the TablePress admin menu entry name. | |
| 99 | 90 | * |
| 100 | 91 | * @since 1.0.0 |
| 101 | 92 | * |
| 102 | 93 | * @param string $entry_name The admin menu entry name. Default "TablePress". |
| @@ -105,23 +96,27 @@ | ||
| 105 | 96 | |
| 106 | 97 | $this->init_view_actions(); |
| 107 | 98 | $min_access_cap = $this->view_actions['list']['required_cap']; |
| 108 | 99 | |
| 109 | - if ( $this->is_top_level_page ) { | |
| 110 | - $icon_url = 'dashicons-list-view'; | |
| 111 | - 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 ) { | |
| 112 | 103 | case 'top': |
| 113 | - $position = 3; // position of Dashboard + 1 | |
| 104 | + $position = 3; // Position of Dashboard + 1. | |
| 114 | 105 | break; |
| 115 | 106 | case 'bottom': |
| 116 | - $position = ( ++$GLOBALS['_wp_last_utility_menu'] ); | |
| 107 | + $position = isset( $GLOBALS['_wp_last_utility_menu'] ) ? ++$GLOBALS['_wp_last_utility_menu'] : 80; | |
| 117 | 108 | break; |
| 118 | 109 | case 'middle': |
| 119 | 110 | default: |
| 120 | - $position = ( ++$GLOBALS['_wp_last_object_menu'] ); | |
| 111 | + $position = isset( $GLOBALS['_wp_last_object_menu'] ) ? ++$GLOBALS['_wp_last_object_menu'] : 25; | |
| 121 | 112 | break; |
| 122 | 113 | } |
| 123 | - 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 | |
| 124 | 119 | foreach ( $this->view_actions as $action => $entry ) { |
| 125 | 120 | if ( ! $entry['show_entry'] ) { |
| 126 | 121 | continue; |
| 127 | 122 | } |
| @@ -128,12 +123,20 @@ | ||
| 128 | 123 | $slug = 'tablepress'; |
| 129 | 124 | if ( 'list' !== $action ) { |
| 130 | 125 | $slug .= '_' . $action; |
| 131 | 126 | } |
| 132 | - $this->page_hooks[] = add_submenu_page( 'tablepress', sprintf( __( '%1$s ‹ %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 ‹ %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 | + } | |
| 133 | 132 | } |
| 134 | 133 | } else { |
| 135 | - $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 | + } | |
| 136 | 139 | } |
| 137 | 140 | } |
| 138 | 141 | |
| 139 | 142 | /** |
| @@ -140,11 +143,11 @@ | ||
| 140 | 143 | * Set up handlers for user actions in the backend that exceed plain viewing. |
| 141 | 144 | * |
| 142 | 145 | * @since 1.0.0 |
| 143 | 146 | */ |
| 144 | - public function add_admin_actions() { | |
| 147 | + public function add_admin_actions(): void { | |
| 145 | 148 | // Register the callbacks for processing action requests. |
| 146 | - $post_actions = array( 'list', 'add', 'edit', 'options', 'export', 'import' ); | |
| 149 | + $post_actions = array( 'list', 'add', 'options', 'export', 'import' ); | |
| 147 | 150 | $get_actions = array( 'hide_message', 'delete_table', 'copy_table', 'preview_table', 'editor_button_thickbox', 'uninstall_tablepress' ); |
| 148 | 151 | foreach ( $post_actions as $action ) { |
| 149 | 152 | add_action( "admin_post_tablepress_{$action}", array( $this, "handle_post_action_{$action}" ) ); |
| 150 | 153 | } |
| @@ -156,11 +159,20 @@ | ||
| 156 | 159 | foreach ( $this->page_hooks as $page_hook ) { |
| 157 | 160 | add_action( "load-{$page_hook}", array( $this, 'load_admin_page' ) ); |
| 158 | 161 | } |
| 159 | 162 | |
| 160 | - $pages_with_editor_button = array( 'post.php', 'post-new.php' ); | |
| 161 | - foreach ( $pages_with_editor_button as $editor_page ) { | |
| 162 | - 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 | + } | |
| 163 | 175 | } |
| 164 | 176 | |
| 165 | 177 | if ( ! is_network_admin() && ! is_user_admin() ) { |
| 166 | 178 | add_action( 'admin_bar_menu', array( $this, 'add_wp_admin_bar_new_content_menu_entry' ), 71 ); |
| @@ -166,93 +178,187 @@ | ||
| 166 | 178 | add_action( 'admin_bar_menu', array( $this, 'add_wp_admin_bar_new_content_menu_entry' ), 71 ); |
| 167 | 179 | } |
| 168 | 180 | |
| 169 | 181 | add_action( 'load-plugins.php', array( $this, 'plugins_page' ) ); |
| 182 | + } | |
| 170 | 183 | |
| 171 | - // Add filters and actions for the integration into the WP WXR exporter and importer. | |
| 172 | - add_action( 'wp_import_insert_post', array( TablePress::$model_table, 'add_table_id_on_wp_import' ), 10, 4 ); | |
| 173 | - add_filter( 'wp_import_post_meta', array( TablePress::$model_table, 'prevent_table_id_post_meta_import_on_wp_import' ), 10, 3 ); | |
| 174 | - 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' ); | |
| 175 | 202 | } |
| 176 | 203 | |
| 177 | 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 | + /** | |
| 178 | 298 | * Register actions to add "Table" button to "HTML editor" and "Visual editor" toolbars. |
| 179 | 299 | * |
| 180 | 300 | * @since 1.0.0 |
| 181 | 301 | */ |
| 182 | - public function add_editor_buttons() { | |
| 302 | + public function add_editor_buttons(): void { | |
| 183 | 303 | if ( ! current_user_can( 'tablepress_list_tables' ) ) { |
| 184 | 304 | return; |
| 185 | 305 | } |
| 186 | 306 | |
| 187 | - /* | |
| 188 | - * Only load the toolbar integration when the Classic Editor plugin (https://wordpress.org/plugins/classic-editor/) is activated. | |
| 189 | - * Without it, the Block Editor user interface is used, which can not directly use these buttons. | |
| 190 | - */ | |
| 191 | - if ( ! class_exists( 'Classic_Editor' ) ) { | |
| 307 | + // Only load the toolbar integration if the Block Editor is not used. | |
| 308 | + if ( TablePress::site_uses_block_editor() ) { | |
| 192 | 309 | return; |
| 193 | 310 | } |
| 194 | 311 | |
| 195 | - add_thickbox(); // usually already loaded by media upload functions | |
| 312 | + add_thickbox(); // The files are usually already loaded by media upload functions. | |
| 196 | 313 | $admin_page = TablePress::load_class( 'TablePress_Admin_Page', 'class-admin-page-helper.php', 'classes' ); |
| 197 | - $admin_page->enqueue_script( 'quicktags-button', array( 'quicktags', 'media-upload' ), array( | |
| 198 | - 'editor_button' => array( | |
| 199 | - 'caption' => __( 'Table', 'tablepress' ), | |
| 200 | - 'title' => __( 'Insert a Table from TablePress', 'tablepress' ), | |
| 201 | - 'thickbox_title' => __( 'Insert a Table from TablePress', 'tablepress' ), | |
| 202 | - '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 | + ), | |
| 203 | 324 | ), |
| 204 | - ) ); | |
| 325 | + ); | |
| 205 | 326 | |
| 206 | 327 | // TinyMCE integration. |
| 207 | 328 | if ( user_can_richedit() ) { |
| 208 | 329 | add_filter( 'mce_external_plugins', array( $this, 'add_tinymce_plugin' ) ); |
| 209 | 330 | add_filter( 'mce_buttons', array( $this, 'add_tinymce_button' ) ); |
| 210 | - add_action( 'admin_print_styles', array( $this, 'add_tablepress_hidpi_css' ), 21 ); | |
| 211 | 331 | } |
| 212 | 332 | } |
| 213 | 333 | |
| 214 | 334 | /** |
| 215 | - * Add "Table" button and separator to the TinyMCE toolbar. | |
| 335 | + * Adds the "Table" button to the TinyMCE toolbar. | |
| 216 | 336 | * |
| 217 | 337 | * @since 1.0.0 |
| 218 | 338 | * |
| 219 | - * @param array $buttons Current set of buttons in the TinyMCE toolbar. | |
| 220 | - * @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. | |
| 221 | 341 | */ |
| 222 | - public function add_tinymce_button( array $buttons ) { | |
| 342 | + public function add_tinymce_button( array $buttons ): array { | |
| 223 | 343 | $buttons[] = 'tablepress_insert_table'; |
| 224 | 344 | return $buttons; |
| 225 | 345 | } |
| 226 | 346 | |
| 227 | 347 | /** |
| 228 | - * Register "Table" button plugin to TinyMCE. | |
| 348 | + * Registers the "Table" button plugin for the TinyMCE editor. | |
| 229 | 349 | * |
| 230 | 350 | * @since 1.0.0 |
| 231 | 351 | * |
| 232 | - * @param array $plugins Current set of registered TinyMCE plugins. | |
| 233 | - * @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. | |
| 234 | 354 | */ |
| 235 | - public function add_tinymce_plugin( array $plugins ) { | |
| 236 | - $suffix = SCRIPT_DEBUG ? '' : '.min'; | |
| 237 | - $js_file = "admin/js/tinymce-button{$suffix}.js"; | |
| 238 | - $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__ ); | |
| 239 | 357 | return $plugins; |
| 240 | 358 | } |
| 241 | 359 | |
| 242 | 360 | /** |
| 243 | - * Print TablePress HiDPI CSS to the <head> for TinyMCE button. | |
| 244 | - * | |
| 245 | - * @since 1.0.0 | |
| 246 | - */ | |
| 247 | - public function add_tablepress_hidpi_css() { | |
| 248 | - echo '<style type="text/css">@media print,(-webkit-min-device-pixel-ratio:1.25),(min-resolution:120dpi){'; | |
| 249 | - 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}'; | |
| 250 | - echo '#content_tablepress_insert_table img{display:none}'; | |
| 251 | - echo '}</style>' . "\n"; | |
| 252 | - } | |
| 253 | - | |
| 254 | - /** | |
| 255 | 361 | * Add "TablePress Table" entry to "New" dropdown menu in the WP Admin Bar. |
| 256 | 362 | * |
| 257 | 363 | * @since 1.0.0 |
| 258 | 364 | * |
| @@ -257,13 +363,18 @@ | ||
| 257 | 363 | * @since 1.0.0 |
| 258 | 364 | * |
| 259 | 365 | * @param WP_Admin_Bar $wp_admin_bar The current WP Admin Bar object. |
| 260 | 366 | */ |
| 261 | - 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 { | |
| 262 | 368 | if ( ! current_user_can( 'tablepress_add_tables' ) ) { |
| 263 | 369 | return; |
| 264 | 370 | } |
| 265 | 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 | + | |
| 266 | 377 | $wp_admin_bar->add_menu( array( |
| 267 | 378 | 'parent' => 'new-content', |
| 268 | 379 | 'id' => 'new-tablepress-table', |
| 269 | 380 | 'title' => __( 'TablePress Table', 'tablepress' ), |
| @@ -275,12 +386,24 @@ | ||
| 275 | 386 | * Handle actions for loading of Plugins page. |
| 276 | 387 | * |
| 277 | 388 | * @since 1.0.0 |
| 278 | 389 | */ |
| 279 | - public function plugins_page() { | |
| 390 | + public function plugins_page(): void { | |
| 280 | 391 | // Add additional links on Plugins page. |
| 281 | 392 | add_filter( 'plugin_action_links_' . TABLEPRESS_BASENAME, array( $this, 'add_plugin_action_links' ) ); |
| 282 | 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 | + } | |
| 283 | 406 | } |
| 284 | 407 | |
| 285 | 408 | /** |
| 286 | 409 | * Add links to the TablePress entry in the "Plugin" column on the Plugins page. |
| @@ -286,45 +409,94 @@ | ||
| 286 | 409 | * Add links to the TablePress entry in the "Plugin" column on the Plugins page. |
| 287 | 410 | * |
| 288 | 411 | * @since 1.0.0 |
| 289 | 412 | * |
| 290 | - * @param array $links List of links to print in the "Plugin" column on the Plugins page. | |
| 291 | - * @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. | |
| 292 | 415 | */ |
| 293 | - public function add_plugin_action_links( array $links ) { | |
| 416 | + public function add_plugin_action_links( array $links ): array { | |
| 294 | 417 | if ( current_user_can( 'tablepress_list_tables' ) ) { |
| 295 | - $links[] = '<a href="' . TablePress::url() . '">' . __( 'Plugin page', 'tablepress' ) . '</a>'; | |
| 418 | + $links[] = '<a href="' . esc_url( TablePress::url() ) . '">' . __( 'Plugin page', 'tablepress' ) . '</a>'; | |
| 296 | 419 | } |
| 297 | 420 | return $links; |
| 298 | 421 | } |
| 422 | + | |
| 299 | 423 | /** |
| 300 | 424 | * Add links to the TablePress entry in the "Description" column on the Plugins page. |
| 301 | 425 | * |
| 302 | 426 | * @since 1.0.0 |
| 303 | 427 | * |
| 304 | - * @param array $links List of links to print in the "Description" column on the Plugins page. | |
| 305 | - * @param string $file Name of the plugin. | |
| 306 | - * @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. | |
| 307 | 431 | */ |
| 308 | - public function add_plugin_row_meta( array $links, $file ) { | |
| 432 | + public function add_plugin_row_meta( array $links, string $file ): array { | |
| 309 | 433 | if ( TABLEPRESS_BASENAME === $file ) { |
| 310 | 434 | $links[] = '<a href="https://tablepress.org/faq/" title="' . esc_attr__( 'Frequently Asked Questions', 'tablepress' ) . '">' . __( 'FAQ', 'tablepress' ) . '</a>'; |
| 311 | 435 | $links[] = '<a href="https://tablepress.org/documentation/">' . __( 'Documentation', 'tablepress' ) . '</a>'; |
| 312 | 436 | $links[] = '<a href="https://tablepress.org/support/">' . __( 'Support', 'tablepress' ) . '</a>'; |
| 313 | - $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 | + } | |
| 314 | 440 | } |
| 315 | 441 | return $links; |
| 316 | 442 | } |
| 317 | 443 | |
| 318 | 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 | + /** | |
| 319 | 491 | * Prepare the rendering of an admin screen, by determining the current action, loading necessary data and initializing the view. |
| 320 | 492 | * |
| 321 | 493 | * @since 1.0.0 |
| 322 | 494 | */ |
| 323 | - public function load_admin_page() { | |
| 495 | + public function load_admin_page(): void { | |
| 324 | 496 | // Determine the action from either the GET parameter (for sub-menu entries, and the main admin menu entry). |
| 325 | - $action = ( ! empty( $_GET['action'] ) ) ? $_GET['action'] : 'list'; // default action is list | |
| 326 | - 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 ) { | |
| 327 | 499 | // Or, for sub-menu entry of an admin menu "TablePress" entry, get it from the "page" GET parameter. |
| 328 | 500 | if ( 'tablepress' !== $_GET['page'] ) { |
| 329 | 501 | // Actions that are top-level entries, but don't have an action GET parameter (action is after last _ in string). |
| 330 | 502 | $action = substr( $_GET['page'], 11 ); // $_GET['page'] has the format 'tablepress_{$action}' |
| @@ -331,24 +503,34 @@ | ||
| 331 | 503 | } |
| 332 | 504 | } |
| 333 | 505 | |
| 334 | 506 | // Check if action is a supported action, and whether the user is allowed to access this screen. |
| 335 | - if ( ! isset( $this->view_actions[ $action ] ) || ! current_user_can( $this->view_actions[ $action ]['required_cap'] ) ) { | |
| 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.) | |
| 336 | 508 | wp_die( __( 'Sorry, you are not allowed to access this page.', 'default' ), 403 ); |
| 337 | 509 | } |
| 338 | 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 | + | |
| 339 | 516 | // Changes current screen ID and pagenow variable in JS, to enable automatic meta box JS handling. |
| 340 | 517 | set_current_screen( "tablepress_{$action}" ); |
| 341 | - // Set the $typenow global to the current CPT ourselves, as WP_Screen::get() does not determine the CPT correctly. | |
| 342 | - // 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 | + */ | |
| 343 | 523 | if ( isset( $_GET['post_type'] ) && post_type_exists( $_GET['post_type'] ) ) { |
| 344 | - $GLOBALS['typenow'] = $_GET['post_type']; | |
| 524 | + $GLOBALS['typenow'] = $_GET['post_type']; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited | |
| 345 | 525 | } |
| 346 | 526 | |
| 347 | 527 | // Pre-define some view data. |
| 348 | 528 | $data = array( |
| 349 | - 'view_actions' => $this->view_actions, | |
| 350 | - '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(), | |
| 351 | 533 | ); |
| 352 | 534 | |
| 353 | 535 | // Depending on the action, load more necessary data for the corresponding view. |
| 354 | 536 | switch ( $action ) { |
| @@ -355,26 +537,25 @@ | ||
| 355 | 537 | case 'list': |
| 356 | 538 | $data['table_id'] = ( ! empty( $_GET['table_id'] ) ) ? $_GET['table_id'] : false; |
| 357 | 539 | // Prime the post meta cache for cached loading of last_editor. |
| 358 | 540 | $data['table_ids'] = TablePress::$model_table->load_all( true ); |
| 359 | - $data['messages']['first_visit'] = TablePress::$model_options->get( 'message_first_visit' ); | |
| 360 | - // Check if WP-Table Reloaded is activated and show a warning. | |
| 361 | - $data['messages']['wp_table_reloaded_warning'] = is_plugin_active( 'wp-table-reloaded/wp-table-reloaded.php' ); | |
| 362 | - $data['messages']['plugin_update_message'] = TablePress::$model_options->get( 'message_plugin_update' ); | |
| 363 | - $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' ); | |
| 364 | 545 | $data['table_count'] = count( $data['table_ids'] ); |
| 365 | 546 | break; |
| 366 | 547 | case 'about': |
| 367 | 548 | $data['first_activation'] = TablePress::$model_options->get( 'first_activation' ); |
| 368 | - $exporter = TablePress::load_class( 'TablePress_Export', 'class-export.php', 'classes' ); | |
| 369 | - $data['zip_support_available'] = $exporter->zip_support_available; | |
| 370 | 549 | break; |
| 371 | 550 | case 'options': |
| 372 | - // Maybe try saving "Custom CSS" to a file: | |
| 373 | - // (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 | + */ | |
| 374 | 555 | if ( isset( $_GET['item'] ) && 'save_custom_css' === $_GET['item'] ) { |
| 375 | 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. |
| 376 | - $action = 'options_custom_css'; // to load a different view | |
| 557 | + $action = 'options_custom_css'; // To load a different view. | |
| 377 | 558 | // Try saving "Custom CSS" to a file, otherwise this gets the HTML for the credentials form. |
| 378 | 559 | $tablepress_css = TablePress::load_class( 'TablePress_CSS', 'class-css.php', 'classes' ); |
| 379 | 560 | $result = $tablepress_css->save_custom_css_to_file_plugin_options( TablePress::$model_options->get( 'custom_css' ), TablePress::$model_options->get( 'custom_css_minified' ) ); |
| 380 | 561 | if ( is_string( $result ) ) { |
| @@ -388,9 +569,9 @@ | ||
| 388 | 569 | 'use_custom_css_file' => true, |
| 389 | 570 | 'custom_css_version' => TablePress::$model_options->get( 'custom_css_version' ) + 1, |
| 390 | 571 | ) ); |
| 391 | 572 | TablePress::redirect( array( 'action' => 'options', 'message' => 'success_save' ) ); |
| 392 | - } else { // leaves only $result = false | |
| 573 | + } else { // Leaves only $result === false. | |
| 393 | 574 | TablePress::redirect( array( 'action' => 'options', 'message' => 'success_save_error_custom_css' ) ); |
| 394 | 575 | } |
| 395 | 576 | break; |
| 396 | 577 | } |
| @@ -395,66 +576,88 @@ | ||
| 395 | 576 | break; |
| 396 | 577 | } |
| 397 | 578 | $data['frontend_options']['use_custom_css'] = TablePress::$model_options->get( 'use_custom_css' ); |
| 398 | 579 | $data['frontend_options']['custom_css'] = TablePress::$model_options->get( 'custom_css' ); |
| 399 | - $data['user_options']['parent_page'] = $this->parent_page; | |
| 580 | + $data['user_options']['parent_page'] = TablePress::$controller->parent_page; | |
| 400 | 581 | break; |
| 401 | 582 | case 'edit': |
| 402 | - if ( ! empty( $_GET['table_id'] ) ) { | |
| 403 | - // Load table, with table data, options, and visibility settings. | |
| 404 | - $data['table'] = TablePress::$model_table->load( $_GET['table_id'], true, true ); | |
| 405 | - if ( is_wp_error( $data['table'] ) ) { | |
| 406 | - TablePress::redirect( array( 'action' => 'list', 'message' => 'error_load_table' ) ); | |
| 407 | - } | |
| 408 | - if ( ! current_user_can( 'tablepress_edit_table', $_GET['table_id'] ) ) { | |
| 409 | - wp_die( __( 'Sorry, you are not allowed to access this page.', 'default' ), 403 ); | |
| 410 | - } | |
| 411 | - } else { | |
| 583 | + if ( empty( $_GET['table_id'] ) ) { | |
| 412 | 584 | TablePress::redirect( array( 'action' => 'list', 'message' => 'error_no_table' ) ); |
| 413 | 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 | + } | |
| 414 | 594 | break; |
| 415 | 595 | case 'export': |
| 416 | 596 | // Load all table IDs without priming the post meta cache, as table options/visibility are not needed. |
| 417 | - $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 | + } | |
| 418 | 613 | $data['tables_count'] = TablePress::$model_table->count_tables(); |
| 419 | - if ( ! empty( $_GET['table_id'] ) ) { | |
| 420 | - $data['export_ids'] = explode( ',', $_GET['table_id'] ); | |
| 421 | - } else { | |
| 422 | - // Just show empty export form. | |
| 423 | - $data['export_ids'] = array(); | |
| 424 | - } | |
| 614 | + $data['export_ids'] = ( ! empty( $_GET['table_id'] ) ) ? explode( ',', $_GET['table_id'] ) : array(); | |
| 425 | 615 | $exporter = TablePress::load_class( 'TablePress_Export', 'class-export.php', 'classes' ); |
| 426 | 616 | $data['zip_support_available'] = $exporter->zip_support_available; |
| 427 | 617 | $data['export_formats'] = $exporter->export_formats; |
| 428 | 618 | $data['csv_delimiters'] = $exporter->csv_delimiters; |
| 429 | - $data['export_format'] = ( ! empty( $_GET['export_format'] ) ) ? $_GET['export_format'] : false; | |
| 619 | + $data['export_format'] = ( ! empty( $_GET['export_format'] ) ) ? $_GET['export_format'] : 'csv'; | |
| 430 | 620 | $data['csv_delimiter'] = ( ! empty( $_GET['csv_delimiter'] ) ) ? $_GET['csv_delimiter'] : _x( ',', 'Default CSV delimiter in the translated language (";", ",", or "tab")', 'tablepress' ); |
| 431 | 621 | break; |
| 432 | 622 | case 'import': |
| 433 | 623 | // Load all table IDs without priming the post meta cache, as table options/visibility are not needed. |
| 434 | - $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. | |
| 435 | 641 | $data['tables_count'] = TablePress::$model_table->count_tables(); |
| 436 | 642 | $importer = TablePress::load_class( 'TablePress_Import', 'class-import.php', 'classes' ); |
| 437 | - $data['zip_support_available'] = $importer->zip_support_available; | |
| 438 | - $data['html_import_support_available'] = $importer->html_import_support_available; | |
| 439 | - $data['import_formats'] = $importer->import_formats; | |
| 440 | - $data['import_format'] = ( ! empty( $_GET['import_format'] ) ) ? $_GET['import_format'] : false; | |
| 441 | 643 | $data['import_type'] = ( ! empty( $_GET['import_type'] ) ) ? $_GET['import_type'] : 'add'; |
| 442 | - $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'] : ''; | |
| 443 | 645 | $data['import_source'] = ( ! empty( $_GET['import_source'] ) ) ? $_GET['import_source'] : 'file-upload'; |
| 444 | - $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://'; | |
| 445 | 647 | $data['import_server'] = ( ! empty( $_GET['import_server'] ) ) ? wp_unslash( $_GET['import_server'] ) : ABSPATH; |
| 446 | - $data['import_form_field'] = ( ! empty( $_GET['import_form_field'] ) ) ? wp_unslash( $_GET['import_form_field'] ) : ''; | |
| 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'; | |
| 447 | 650 | break; |
| 448 | 651 | } |
| 449 | 652 | |
| 450 | 653 | /** |
| 451 | - * Filter the data that is passed to the current TablePress View. | |
| 654 | + * Filters the data that is passed to the current TablePress View. | |
| 452 | 655 | * |
| 453 | 656 | * @since 1.0.0 |
| 454 | 657 | * |
| 455 | - * @param array $data Data for the view. | |
| 456 | - * @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. | |
| 457 | 660 | */ |
| 458 | 661 | $data = apply_filters( 'tablepress_view_data', $data, $action ); |
| 459 | 662 | |
| 460 | 663 | // Prepare and initialize the view. |
| @@ -465,20 +668,20 @@ | ||
| 465 | 668 | * Render the view that has been initialized in load_admin_page() (called by WordPress when the actual page content is needed). |
| 466 | 669 | * |
| 467 | 670 | * @since 1.0.0 |
| 468 | 671 | */ |
| 469 | - public function show_admin_page() { | |
| 672 | + public function show_admin_page(): void { | |
| 470 | 673 | $this->view->render(); |
| 471 | 674 | } |
| 472 | 675 | |
| 473 | 676 | /** |
| 474 | - * 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. | |
| 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. | |
| 475 | 678 | * |
| 476 | 679 | * @since 1.0.0 |
| 477 | 680 | * |
| 478 | - * @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. | |
| 479 | 682 | */ |
| 480 | - protected function maybe_show_donation_message() { | |
| 683 | + protected function maybe_show_donation_message(): bool { | |
| 481 | 684 | // Only show the message to plugin admins. |
| 482 | 685 | if ( ! current_user_can( 'tablepress_edit_options' ) ) { |
| 483 | 686 | return false; |
| 484 | 687 | } |
| @@ -488,9 +691,9 @@ | ||
| 488 | 691 | } |
| 489 | 692 | |
| 490 | 693 | // Determine, how long has the plugin been installed. |
| 491 | 694 | $seconds_installed = time() - TablePress::$model_options->get( 'first_activation' ); |
| 492 | - return ( $seconds_installed > 30 * DAY_IN_SECONDS ); | |
| 695 | + return ( $seconds_installed > MONTH_IN_SECONDS / 2 ); | |
| 493 | 696 | } |
| 494 | 697 | |
| 495 | 698 | /** |
| 496 | 699 | * Init list of actions that have a view with their titles/names/caps. |
| @@ -496,9 +699,9 @@ | ||
| 496 | 699 | * Init list of actions that have a view with their titles/names/caps. |
| 497 | 700 | * |
| 498 | 701 | * @since 1.0.0 |
| 499 | 702 | */ |
| 500 | - protected function init_view_actions() { | |
| 703 | + protected function init_view_actions(): void { | |
| 501 | 704 | $this->view_actions = array( |
| 502 | 705 | 'list' => array( |
| 503 | 706 | 'show_entry' => true, |
| 504 | 707 | 'page_title' => __( 'All Tables', 'tablepress' ), |
| @@ -550,13 +753,13 @@ | ||
| 550 | 753 | ), |
| 551 | 754 | ); |
| 552 | 755 | |
| 553 | 756 | /** |
| 554 | - * Filter the available TablePres Views/Actions and their parameters. | |
| 757 | + * Filters the available TablePres Views/Actions and their parameters. | |
| 555 | 758 | * |
| 556 | 759 | * @since 1.0.0 |
| 557 | 760 | * |
| 558 | - * @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. | |
| 559 | 762 | */ |
| 560 | 763 | $this->view_actions = apply_filters( 'tablepress_admin_view_actions', $this->view_actions ); |
| 561 | 764 | } |
| 562 | 765 | |
| @@ -568,15 +771,15 @@ | ||
| 568 | 771 | * Handle Bulk Actions (Copy, Export, Delete) on "All Tables" list screen. |
| 569 | 772 | * |
| 570 | 773 | * @since 1.0.0 |
| 571 | 774 | */ |
| 572 | - public function handle_post_action_list() { | |
| 775 | + public function handle_post_action_list(): void { | |
| 573 | 776 | TablePress::check_nonce( 'list' ); |
| 574 | 777 | |
| 575 | - if ( isset( $_POST['bulk-action-top'] ) && '-1' !== $_POST['bulk-action-top'] ) { | |
| 576 | - $bulk_action = $_POST['bulk-action-top']; | |
| 577 | - } elseif ( isset( $_POST['bulk-action-bottom'] ) && '-1' !== $_POST['bulk-action-bottom'] ) { | |
| 578 | - $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']; | |
| 579 | 782 | } else { |
| 580 | 783 | $bulk_action = false; |
| 581 | 784 | } |
| 582 | 785 | |
| @@ -585,14 +788,14 @@ | ||
| 585 | 788 | } |
| 586 | 789 | |
| 587 | 790 | if ( empty( $_POST['table'] ) || ! is_array( $_POST['table'] ) ) { |
| 588 | 791 | TablePress::redirect( array( 'action' => 'list', 'message' => 'error_no_selection' ) ); |
| 589 | - } else { | |
| 590 | - $tables = wp_unslash( $_POST['table'] ); | |
| 591 | 792 | } |
| 592 | 793 | |
| 593 | - $no_success = array(); // to store table IDs that failed | |
| 794 | + $tables = wp_unslash( $_POST['table'] ); | |
| 594 | 795 | |
| 796 | + $no_success = array(); // To store table IDs that failed. | |
| 797 | + | |
| 595 | 798 | switch ( $bulk_action ) { |
| 596 | 799 | case 'copy': |
| 597 | 800 | foreach ( $tables as $table_id ) { |
| 598 | 801 | if ( current_user_can( 'tablepress_copy_table', $table_id ) ) { |
| @@ -611,9 +814,9 @@ | ||
| 611 | 814 | * To export, redirect to "Export" screen, with selected table IDs. |
| 612 | 815 | */ |
| 613 | 816 | $table_ids = implode( ',', $tables ); |
| 614 | 817 | TablePress::redirect( array( 'action' => 'export', 'table_id' => $table_ids ) ); |
| 615 | - break; | |
| 818 | + // break; // unreachable. | |
| 616 | 819 | case 'delete': |
| 617 | 820 | foreach ( $tables as $table_id ) { |
| 618 | 821 | if ( current_user_can( 'tablepress_delete_table', $table_id ) ) { |
| 619 | 822 | $deleted = TablePress::$model_table->delete( $table_id ); |
| @@ -626,9 +829,9 @@ | ||
| 626 | 829 | } |
| 627 | 830 | break; |
| 628 | 831 | } |
| 629 | 832 | |
| 630 | - 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? | |
| 631 | 834 | $message = "error_{$bulk_action}_not_all_tables"; |
| 632 | 835 | } else { |
| 633 | 836 | $plural = ( count( $tables ) > 1 ) ? '_plural' : ''; |
| 634 | 837 | $message = "success_{$bulk_action}{$plural}"; |
| @@ -649,99 +852,13 @@ | ||
| 649 | 852 | exit; |
| 650 | 853 | } |
| 651 | 854 | |
| 652 | 855 | /** |
| 653 | - * Save a table after the "Edit" screen was submitted. | |
| 654 | - * | |
| 655 | - * @since 1.0.0 | |
| 656 | - */ | |
| 657 | - public function handle_post_action_edit() { | |
| 658 | - if ( empty( $_POST['table'] ) || empty( $_POST['table']['id'] ) ) { | |
| 659 | - TablePress::redirect( array( 'action' => 'list', 'message' => 'error_save' ) ); | |
| 660 | - } else { | |
| 661 | - $edit_table = wp_unslash( $_POST['table'] ); | |
| 662 | - } | |
| 663 | - | |
| 664 | - TablePress::check_nonce( 'edit', $edit_table['id'], 'nonce-edit-table' ); | |
| 665 | - | |
| 666 | - if ( ! current_user_can( 'tablepress_edit_table', $edit_table['id'] ) ) { | |
| 667 | - wp_die( __( 'Sorry, you are not allowed to access this page.', 'default' ), 403 ); | |
| 668 | - } | |
| 669 | - | |
| 670 | - // Options array must exist, so that checkboxes can be evaluated. | |
| 671 | - if ( empty( $edit_table['options'] ) ) { | |
| 672 | - TablePress::redirect( array( 'action' => 'edit', 'table_id' => $edit_table['id'], 'message' => 'error_save' ) ); | |
| 673 | - } | |
| 674 | - | |
| 675 | - // Evaluate options that have a checkbox (only necessary in Admin Controller, where they might not be set (if unchecked)). | |
| 676 | - $checkbox_options = array( | |
| 677 | - // Table Options. | |
| 678 | - 'table_head', | |
| 679 | - 'table_foot', | |
| 680 | - 'alternating_row_colors', | |
| 681 | - 'row_hover', | |
| 682 | - 'print_name', | |
| 683 | - 'print_description', | |
| 684 | - // DataTables JS Features. | |
| 685 | - 'use_datatables', | |
| 686 | - 'datatables_sort', | |
| 687 | - 'datatables_filter', | |
| 688 | - 'datatables_paginate', | |
| 689 | - 'datatables_lengthchange', | |
| 690 | - 'datatables_info', | |
| 691 | - 'datatables_scrollx', | |
| 692 | - ); | |
| 693 | - foreach ( $checkbox_options as $option ) { | |
| 694 | - $edit_table['options'][ $option ] = ( isset( $edit_table['options'][ $option ] ) && 'true' === $edit_table['options'][ $option ] ); | |
| 695 | - } | |
| 696 | - | |
| 697 | - // Load table, without table data, but with options and visibility settings. | |
| 698 | - $existing_table = TablePress::$model_table->load( $edit_table['id'], false, true ); | |
| 699 | - if ( is_wp_error( $existing_table ) ) { // @TODO: Maybe somehow load a new table here? (TablePress::$model_table->get_table_template())? | |
| 700 | - TablePress::redirect( array( 'action' => 'edit', 'table_id' => $edit_table['id'], 'message' => 'error_save' ) ); | |
| 701 | - } | |
| 702 | - | |
| 703 | - // Check consistency of new table, and then merge with existing table. | |
| 704 | - $table = TablePress::$model_table->prepare_table( $existing_table, $edit_table ); | |
| 705 | - if ( is_wp_error( $table ) ) { | |
| 706 | - TablePress::redirect( array( 'action' => 'edit', 'table_id' => $edit_table['id'], 'message' => 'error_save' ) ); | |
| 707 | - } | |
| 708 | - | |
| 709 | - // DataTables Custom Commands can only be edit by trusted users. | |
| 710 | - if ( ! current_user_can( 'unfiltered_html' ) ) { | |
| 711 | - $table['options']['datatables_custom_commands'] = $existing_table['options']['datatables_custom_commands']; | |
| 712 | - } | |
| 713 | - | |
| 714 | - // Save updated table. | |
| 715 | - $saved = TablePress::$model_table->save( $table ); | |
| 716 | - if ( is_wp_error( $saved ) ) { | |
| 717 | - TablePress::redirect( array( 'action' => 'edit', 'table_id' => $table['id'], 'message' => 'error_save' ) ); | |
| 718 | - } | |
| 719 | - | |
| 720 | - // Check if ID change is desired. | |
| 721 | - if ( $table['id'] === $table['new_id'] ) { // if not, we are done | |
| 722 | - TablePress::redirect( array( 'action' => 'edit', 'table_id' => $table['id'], 'message' => 'success_save' ) ); | |
| 723 | - } | |
| 724 | - | |
| 725 | - // Change table ID. | |
| 726 | - if ( current_user_can( 'tablepress_edit_table_id', $table['id'] ) ) { | |
| 727 | - $id_changed = TablePress::$model_table->change_table_id( $table['id'], $table['new_id'] ); | |
| 728 | - if ( ! is_wp_error( $id_changed ) ) { | |
| 729 | - TablePress::redirect( array( 'action' => 'edit', 'table_id' => $table['new_id'], 'message' => 'success_save_success_id_change' ) ); | |
| 730 | - } else { | |
| 731 | - TablePress::redirect( array( 'action' => 'edit', 'table_id' => $table['id'], 'message' => 'success_save_error_id_change' ) ); | |
| 732 | - } | |
| 733 | - } else { | |
| 734 | - TablePress::redirect( array( 'action' => 'edit', 'table_id' => $table['id'], 'message' => 'success_save_error_id_change' ) ); | |
| 735 | - } | |
| 736 | - } | |
| 737 | - | |
| 738 | - /** | |
| 739 | 856 | * Add a table, according to the parameters on the "Add new Table" screen. |
| 740 | 857 | * |
| 741 | 858 | * @since 1.0.0 |
| 742 | 859 | */ |
| 743 | - public function handle_post_action_add() { | |
| 860 | + public function handle_post_action_add(): void { | |
| 744 | 861 | TablePress::check_nonce( 'add' ); |
| 745 | 862 | |
| 746 | 863 | if ( ! current_user_can( 'tablepress_add_tables' ) ) { |
| 747 | 864 | wp_die( __( 'Sorry, you are not allowed to access this page.', 'default' ), 403 ); |
| @@ -747,24 +864,24 @@ | ||
| 747 | 864 | wp_die( __( 'Sorry, you are not allowed to access this page.', 'default' ), 403 ); |
| 748 | 865 | } |
| 749 | 866 | |
| 750 | 867 | if ( empty( $_POST['table'] ) || ! is_array( $_POST['table'] ) ) { |
| 751 | - TablePress::redirect( array( 'action' => 'add', 'message' => 'error_add' ) ); | |
| 752 | - } else { | |
| 753 | - $add_table = wp_unslash( $_POST['table'] ); | |
| 868 | + TablePress::redirect( array( 'action' => 'add', 'message' => 'error_add', 'error_details' => 'The HTTP POST data is empty.' ) ); | |
| 754 | 869 | } |
| 755 | 870 | |
| 756 | - // Perform sanity checks of posted data. | |
| 757 | - $name = ( isset( $add_table['name'] ) ) ? $add_table['name'] : ''; | |
| 758 | - $description = ( isset( $add_table['description'] ) ) ? $add_table['description'] : ''; | |
| 759 | - if ( ! isset( $add_table['rows'] ) || ! isset( $add_table['columns'] ) ) { | |
| 760 | - 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.' ) ); | |
| 761 | 878 | } |
| 762 | 879 | |
| 763 | 880 | $num_rows = absint( $add_table['rows'] ); |
| 764 | 881 | $num_columns = absint( $add_table['columns'] ); |
| 765 | 882 | if ( 0 === $num_rows || 0 === $num_columns ) { |
| 766 | - TablePress::redirect( array( 'action' => 'add', 'message' => 'error_add' ) ); | |
| 883 | + TablePress::redirect( array( 'action' => 'add', 'message' => 'error_add', 'error_details' => 'The table size is invalid.' ) ); | |
| 767 | 884 | } |
| 768 | 885 | |
| 769 | 886 | // Create a new table array with information from the posted data. |
| 770 | 887 | $new_table = array( |
| @@ -778,15 +895,15 @@ | ||
| 778 | 895 | ); |
| 779 | 896 | // Merge this data into an empty table template. |
| 780 | 897 | $table = TablePress::$model_table->prepare_table( TablePress::$model_table->get_table_template(), $new_table, false ); |
| 781 | 898 | if ( is_wp_error( $table ) ) { |
| 782 | - 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 ) ) ); | |
| 783 | 900 | } |
| 784 | 901 | |
| 785 | 902 | // Add the new table (and get its first ID). |
| 786 | 903 | $table_id = TablePress::$model_table->add( $table ); |
| 787 | 904 | if ( is_wp_error( $table_id ) ) { |
| 788 | - 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 ) ) ); | |
| 789 | 906 | } |
| 790 | 907 | |
| 791 | 908 | TablePress::redirect( array( 'action' => 'edit', 'table_id' => $table_id, 'message' => 'success_add' ) ); |
| 792 | 909 | } |
| @@ -795,9 +912,9 @@ | ||
| 795 | 912 | * Save changed "Plugin Options". |
| 796 | 913 | * |
| 797 | 914 | * @since 1.0.0 |
| 798 | 915 | */ |
| 799 | - public function handle_post_action_options() { | |
| 916 | + public function handle_post_action_options(): void { | |
| 800 | 917 | TablePress::check_nonce( 'options' ); |
| 801 | 918 | |
| 802 | 919 | if ( ! current_user_can( 'tablepress_access_options_screen' ) ) { |
| 803 | 920 | wp_die( __( 'Sorry, you are not allowed to access this page.', 'default' ), 403 ); |
| @@ -804,12 +921,12 @@ | ||
| 804 | 921 | } |
| 805 | 922 | |
| 806 | 923 | if ( empty( $_POST['options'] ) || ! is_array( $_POST['options'] ) ) { |
| 807 | 924 | TablePress::redirect( array( 'action' => 'options', 'message' => 'error_save' ) ); |
| 808 | - } else { | |
| 809 | - $posted_options = wp_unslash( $_POST['options'] ); | |
| 810 | 925 | } |
| 811 | 926 | |
| 927 | + $posted_options = wp_unslash( $_POST['options'] ); | |
| 928 | + | |
| 812 | 929 | // Valid new options that will be merged into existing ones. |
| 813 | 930 | $new_options = array(); |
| 814 | 931 | |
| 815 | 932 | // Check each posted option value, and (maybe) add it to the new options. |
| @@ -814,18 +931,18 @@ | ||
| 814 | 931 | |
| 815 | 932 | // Check each posted option value, and (maybe) add it to the new options. |
| 816 | 933 | if ( ! empty( $posted_options['admin_menu_parent_page'] ) && '-' !== $posted_options['admin_menu_parent_page'] ) { |
| 817 | 934 | $new_options['admin_menu_parent_page'] = $posted_options['admin_menu_parent_page']; |
| 818 | - // 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. | |
| 819 | 936 | /** This filter is documented in classes/class-controller.php */ |
| 820 | - $this->parent_page = apply_filters( 'tablepress_admin_menu_parent_page', $posted_options['admin_menu_parent_page'] ); | |
| 821 | - $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 ); | |
| 822 | 939 | } |
| 823 | 940 | |
| 824 | 941 | // Custom CSS can only be saved if the user is allowed to do so. |
| 825 | 942 | $update_custom_css_files = false; |
| 826 | 943 | if ( current_user_can( 'tablepress_edit_options' ) ) { |
| 827 | - // Checkbox | |
| 944 | + // Checkbox. | |
| 828 | 945 | $new_options['use_custom_css'] = ( isset( $posted_options['use_custom_css'] ) && 'true' === $posted_options['use_custom_css'] ); |
| 829 | 946 | |
| 830 | 947 | if ( isset( $posted_options['custom_css'] ) ) { |
| 831 | 948 | $new_options['custom_css'] = $posted_options['custom_css']; |
| @@ -830,13 +947,20 @@ | ||
| 830 | 947 | if ( isset( $posted_options['custom_css'] ) ) { |
| 831 | 948 | $new_options['custom_css'] = $posted_options['custom_css']; |
| 832 | 949 | |
| 833 | 950 | $tablepress_css = TablePress::load_class( 'TablePress_CSS', 'class-css.php', 'classes' ); |
| 834 | - // Sanitize and tidy up Custom CSS. | |
| 835 | - $new_options['custom_css'] = $tablepress_css->sanitize_css( $new_options['custom_css'] ); | |
| 836 | - // Minify Custom CSS | |
| 837 | - $new_options['custom_css_minified'] = $tablepress_css->minify_css( $new_options['custom_css'] ); | |
| 838 | 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 | + | |
| 839 | 963 | // Maybe update CSS files as well. |
| 840 | 964 | $custom_css_file_contents = $tablepress_css->load_custom_css_from_file( 'normal' ); |
| 841 | 965 | if ( false === $custom_css_file_contents ) { |
| 842 | 966 | $custom_css_file_contents = ''; |
| @@ -867,9 +991,9 @@ | ||
| 867 | 991 | * Export selected tables. |
| 868 | 992 | * |
| 869 | 993 | * @since 1.0.0 |
| 870 | 994 | */ |
| 871 | - public function handle_post_action_export() { | |
| 995 | + public function handle_post_action_export(): void { | |
| 872 | 996 | TablePress::check_nonce( 'export' ); |
| 873 | 997 | |
| 874 | 998 | if ( ! current_user_can( 'tablepress_export_tables' ) ) { |
| 875 | 999 | wp_die( __( 'Sorry, you are not allowed to access this page.', 'default' ), 403 ); |
| @@ -875,20 +999,22 @@ | ||
| 875 | 999 | wp_die( __( 'Sorry, you are not allowed to access this page.', 'default' ), 403 ); |
| 876 | 1000 | } |
| 877 | 1001 | |
| 878 | 1002 | if ( empty( $_POST['export'] ) || ! is_array( $_POST['export'] ) ) { |
| 879 | - TablePress::redirect( array( 'action' => 'export', 'message' => 'error_export' ) ); | |
| 880 | - } else { | |
| 881 | - $export = wp_unslash( $_POST['export'] ); | |
| 1003 | + TablePress::redirect( array( 'action' => 'export', 'message' => 'error_export', 'error_details' => 'The HTTP POST data is empty.' ) ); | |
| 882 | 1004 | } |
| 883 | 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 | |
| 884 | 1013 | $exporter = TablePress::load_class( 'TablePress_Export', 'class-export.php', 'classes' ); |
| 885 | 1014 | |
| 886 | - if ( empty( $export['tables'] ) ) { | |
| 887 | - TablePress::redirect( array( 'action' => 'export', 'message' => 'error_export' ) ); | |
| 888 | - } | |
| 889 | 1015 | if ( empty( $export['format'] ) || ! isset( $exporter->export_formats[ $export['format'] ] ) ) { |
| 890 | - TablePress::redirect( array( 'action' => 'export', 'message' => 'error_export' ) ); | |
| 1016 | + TablePress::redirect( array( 'action' => 'export', 'message' => 'error_export', 'error_details' => 'The export format is invalid.' ) ); | |
| 891 | 1017 | } |
| 892 | 1018 | if ( empty( $export['csv_delimiter'] ) ) { |
| 893 | 1019 | // Set a value, so that the variable exists. |
| 894 | 1020 | $export['csv_delimiter'] = ''; |
| @@ -893,13 +1019,12 @@ | ||
| 893 | 1019 | // Set a value, so that the variable exists. |
| 894 | 1020 | $export['csv_delimiter'] = ''; |
| 895 | 1021 | } |
| 896 | 1022 | if ( 'csv' === $export['format'] && ! isset( $exporter->csv_delimiters[ $export['csv_delimiter'] ] ) ) { |
| 897 | - TablePress::redirect( array( 'action' => 'export', 'message' => 'error_export' ) ); | |
| 1023 | + TablePress::redirect( array( 'action' => 'export', 'message' => 'error_export', 'error_details' => 'The CSV delimiter is invalid.' ) ); | |
| 898 | 1024 | } |
| 899 | 1025 | |
| 900 | - // Use list of tables from concatenated field if available (as that's hopefully not truncated by Suhosin, which is possible for $export['tables']). | |
| 901 | - $tables = ( ! empty( $export['tables_list'] ) ) ? explode( ',', $export['tables_list'] ) : $export['tables']; | |
| 1026 | + $tables = explode( ',', $export['tables_list'] ); | |
| 902 | 1027 | |
| 903 | 1028 | // Determine if ZIP file support is available. |
| 904 | 1029 | if ( $exporter->zip_support_available |
| 905 | 1030 | && ( ( isset( $export['zip_file'] ) && 'true' === $export['zip_file'] ) || count( $tables ) > 1 ) ) { |
| @@ -909,9 +1034,9 @@ | ||
| 909 | 1034 | $export_to_zip = false; |
| 910 | 1035 | } |
| 911 | 1036 | |
| 912 | 1037 | if ( ! $export_to_zip ) { |
| 913 | - // 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. | |
| 914 | 1039 | if ( ! current_user_can( 'tablepress_export_table', $tables[0] ) ) { |
| 915 | 1040 | wp_die( __( 'Sorry, you are not allowed to access this page.', 'default' ), 403 ); |
| 916 | 1041 | } |
| 917 | 1042 | // Load table, with table data, options, and visibility settings. |
| @@ -916,42 +1041,57 @@ | ||
| 916 | 1041 | } |
| 917 | 1042 | // Load table, with table data, options, and visibility settings. |
| 918 | 1043 | $table = TablePress::$model_table->load( $tables[0], true, true ); |
| 919 | 1044 | if ( is_wp_error( $table ) ) { |
| 920 | - 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 ) ) ); | |
| 921 | 1046 | } |
| 922 | 1047 | if ( isset( $table['is_corrupted'] ) && $table['is_corrupted'] ) { |
| 923 | 1048 | TablePress::redirect( array( 'action' => 'export', 'message' => 'error_table_corrupted', 'export_format' => $export['format'], 'csv_delimiter' => $export['csv_delimiter'] ) ); |
| 924 | 1049 | } |
| 925 | - $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 ); | |
| 926 | 1063 | $download_filename = sanitize_file_name( $download_filename ); |
| 927 | 1064 | // Export the table. |
| 928 | 1065 | $export_data = $exporter->export_table( $table, $export['format'], $export['csv_delimiter'] ); |
| 929 | 1066 | /** |
| 930 | - * Filter the exported table data. | |
| 1067 | + * Filters the exported table data. | |
| 931 | 1068 | * |
| 932 | 1069 | * @since 1.6.0 |
| 933 | 1070 | * |
| 934 | - * @param string $export_data The exported table data. | |
| 935 | - * @param array $table Table to be exported. | |
| 936 | - * @param string $export_format Format for the export ('csv', 'html', 'json'). | |
| 937 | - * @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. | |
| 938 | 1075 | */ |
| 939 | 1076 | $export_data = apply_filters( 'tablepress_export_data', $export_data, $table, $export['format'], $export['csv_delimiter'] ); |
| 940 | 1077 | $download_data = $export_data; |
| 941 | 1078 | } else { |
| 942 | 1079 | // Zipping can use a lot of memory and execution time, but not this much hopefully. |
| 943 | - /** This filter is documented in the WordPress file wp-admin/admin.php */ | |
| 944 | - @ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', WP_MAX_MEMORY_LIMIT ) ); | |
| 945 | - @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 | + } | |
| 946 | 1084 | |
| 947 | 1085 | $zip_file = new ZipArchive(); |
| 948 | - $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 ); | |
| 949 | 1089 | $download_filename = sanitize_file_name( $download_filename ); |
| 950 | 1090 | $full_filename = wp_tempnam( $download_filename ); |
| 951 | - if ( true !== $zip_file->open( $full_filename, ZIPARCHIVE::OVERWRITE ) ) { | |
| 952 | - @unlink( $full_filename ); | |
| 953 | - 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.' ) ); | |
| 954 | 1094 | } |
| 955 | 1095 | |
| 956 | 1096 | foreach ( $tables as $table_id ) { |
| 957 | 1097 | // Don't export tables for which the user doesn't have the necessary export rights. |
| @@ -970,24 +1110,31 @@ | ||
| 970 | 1110 | } |
| 971 | 1111 | $export_data = $exporter->export_table( $table, $export['format'], $export['csv_delimiter'] ); |
| 972 | 1112 | /** This filter is documented in controllers/controller-admin.php */ |
| 973 | 1113 | $export_data = apply_filters( 'tablepress_export_data', $export_data, $table, $export['format'], $export['csv_delimiter'] ); |
| 974 | - $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 ); | |
| 975 | 1117 | $export_filename = sanitize_file_name( $export_filename ); |
| 976 | 1118 | $zip_file->addFromString( $export_filename, $export_data ); |
| 977 | 1119 | } |
| 978 | 1120 | |
| 979 | 1121 | // If something went wrong, or no files were added to the ZIP file, bail out. |
| 980 | - 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 ) { | |
| 981 | 1124 | $zip_file->close(); |
| 982 | - @unlink( $full_filename ); | |
| 983 | - 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.' ) ); | |
| 984 | 1127 | } |
| 985 | 1128 | $zip_file->close(); |
| 986 | 1129 | |
| 987 | 1130 | // Load contents of the ZIP file, to send it as a download. |
| 988 | 1131 | $download_data = file_get_contents( $full_filename ); |
| 989 | - @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 | |
| 990 | 1137 | } |
| 991 | 1138 | |
| 992 | 1139 | // Send download headers for export file. |
| 993 | 1140 | header( 'Content-Description: File Transfer' ); |
| @@ -999,9 +1146,9 @@ | ||
| 999 | 1146 | header( 'Pragma: public' ); |
| 1000 | 1147 | header( 'Content-Length: ' . strlen( $download_data ) ); |
| 1001 | 1148 | // $filetype = text/csv, text/html, application/json |
| 1002 | 1149 | // header( 'Content-Type: ' . $filetype. '; charset=' . get_option( 'blog_charset' ) ); |
| 1003 | - @ob_end_clean(); | |
| 1150 | + @ob_end_clean(); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged | |
| 1004 | 1151 | flush(); |
| 1005 | 1152 | echo $download_data; |
| 1006 | 1153 | exit; |
| 1007 | 1154 | } |
| @@ -1010,9 +1157,9 @@ | ||
| 1010 | 1157 | * Import data from existing source (Upload, URL, Server, Direct input). |
| 1011 | 1158 | * |
| 1012 | 1159 | * @since 1.0.0 |
| 1013 | 1160 | */ |
| 1014 | - public function handle_post_action_import() { | |
| 1161 | + public function handle_post_action_import(): void { | |
| 1015 | 1162 | TablePress::check_nonce( 'import' ); |
| 1016 | 1163 | |
| 1017 | 1164 | if ( ! current_user_can( 'tablepress_import_tables' ) ) { |
| 1018 | 1165 | wp_die( __( 'Sorry, you are not allowed to access this page.', 'default' ), 403 ); |
| @@ -1018,335 +1165,88 @@ | ||
| 1018 | 1165 | wp_die( __( 'Sorry, you are not allowed to access this page.', 'default' ), 403 ); |
| 1019 | 1166 | } |
| 1020 | 1167 | |
| 1021 | 1168 | if ( empty( $_POST['import'] ) || ! is_array( $_POST['import'] ) ) { |
| 1022 | - TablePress::redirect( array( 'action' => 'import', 'message' => 'error_import' ) ); | |
| 1023 | - } else { | |
| 1024 | - $import = wp_unslash( $_POST['import'] ); | |
| 1169 | + TablePress::redirect( array( 'action' => 'import', 'message' => 'error_import', 'error_details' => 'The HTTP POST data is empty.' ) ); | |
| 1025 | 1170 | } |
| 1026 | 1171 | |
| 1027 | - if ( ! isset( $import['type'] ) ) { | |
| 1028 | - $import['type'] = 'add'; | |
| 1029 | - } | |
| 1030 | - if ( ! isset( $import['existing_table'] ) ) { | |
| 1031 | - $import['existing_table'] = ''; | |
| 1032 | - } | |
| 1033 | - if ( ! isset( $import['source'] ) ) { | |
| 1034 | - $import['source'] = ''; | |
| 1035 | - } | |
| 1172 | + $import_config = wp_unslash( $_POST['import'] ); | |
| 1036 | 1173 | |
| 1037 | - $import_error = true; | |
| 1038 | - $unlink_file = false; | |
| 1039 | - $import_data = array(); | |
| 1040 | - switch ( $import['source'] ) { | |
| 1041 | - case 'file-upload': | |
| 1042 | - if ( ! empty( $_FILES['import_file_upload'] ) && UPLOAD_ERR_OK === $_FILES['import_file_upload']['error'] ) { | |
| 1043 | - $import_data['file_location'] = $_FILES['import_file_upload']['tmp_name']; | |
| 1044 | - $import_data['file_name'] = $_FILES['import_file_upload']['name']; | |
| 1045 | - // $_FILES['import_file_upload']['type']; | |
| 1046 | - // $_FILES['import_file_upload']['size'] | |
| 1047 | - $import_error = false; | |
| 1048 | - $unlink_file = true; | |
| 1049 | - } | |
| 1050 | - break; | |
| 1051 | - case 'url': | |
| 1052 | - if ( ! empty( $import['url'] ) && 'http://' !== $import['url'] ) { | |
| 1053 | - // Check the host of the Import URL against a blacklist of hosts, which should not be accessible, e.g. for security considerations. | |
| 1054 | - $host = wp_parse_url( $import['url'], PHP_URL_HOST ); | |
| 1055 | - $blocked_hosts = array( | |
| 1056 | - '169.254.169.254' // AWS Meta-data API | |
| 1057 | - ); | |
| 1058 | - if ( in_array( $host, $blocked_hosts, true ) ) { | |
| 1059 | - $import_error = true; | |
| 1060 | - break; | |
| 1061 | - } | |
| 1062 | - | |
| 1063 | - // Download URL to local file. | |
| 1064 | - $import_data['file_location'] = download_url( $import['url'] ); | |
| 1065 | - $import_data['file_name'] = $import['url']; | |
| 1066 | - if ( ! is_wp_error( $import_data['file_location'] ) ) { | |
| 1067 | - $import_error = false; | |
| 1068 | - } | |
| 1069 | - $unlink_file = true; | |
| 1070 | - } | |
| 1071 | - break; | |
| 1072 | - case 'server': | |
| 1073 | - if ( ! empty( $import['server'] ) && ABSPATH !== $import['server'] | |
| 1074 | - && ( ( ! is_multisite() && current_user_can( 'manage_options' ) ) || is_super_admin() ) | |
| 1075 | - ) { | |
| 1076 | - // For security reasons, the `server` source is only available for administrators. | |
| 1077 | - $import_data['file_location'] = $import['server']; | |
| 1078 | - $import_data['file_name'] = pathinfo( $import['server'], PATHINFO_BASENAME ); | |
| 1079 | - if ( is_readable( $import['server'] ) ) { | |
| 1080 | - $import_error = false; | |
| 1081 | - } | |
| 1082 | - } | |
| 1083 | - break; | |
| 1084 | - case 'form-field': | |
| 1085 | - if ( ! empty( $import['form_field'] ) ) { | |
| 1086 | - $import_data['file_location'] = ''; | |
| 1087 | - $import_data['file_name'] = __( 'Imported from Manual Input', 'tablepress' ); // Description of the table. | |
| 1088 | - $import_data['data'] = $import['form_field']; | |
| 1089 | - $import_error = false; | |
| 1090 | - } | |
| 1091 | - break; | |
| 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.' ) ); | |
| 1092 | 1176 | } |
| 1093 | 1177 | |
| 1094 | - if ( $import_error ) { | |
| 1095 | - if ( $unlink_file ) { | |
| 1096 | - @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.' ) ); | |
| 1097 | 1182 | } |
| 1098 | - 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'] ) ); | |
| 1099 | 1183 | } |
| 1100 | 1184 | |
| 1101 | - $this->importer = TablePress::load_class( 'TablePress_Import', 'class-import.php', 'classes' ); | |
| 1102 | - | |
| 1103 | - $import_zip = ( 'zip' === pathinfo( $import_data['file_name'], PATHINFO_EXTENSION ) ); | |
| 1104 | - | |
| 1105 | - // Determine if ZIP file support is available. | |
| 1106 | - if ( $import_zip && ! $this->importer->zip_support_available ) { | |
| 1107 | - if ( $unlink_file ) { | |
| 1108 | - @unlink( $import_data['file_location'] ); | |
| 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.' ) ); | |
| 1109 | 1189 | } |
| 1110 | - 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'] ) ); | |
| 1111 | 1190 | } |
| 1112 | 1191 | |
| 1113 | - if ( ! $import_zip ) { | |
| 1114 | - // Check if a table to replace or append to was selected (which is only necessary for import from non-ZIP files). | |
| 1115 | - if ( in_array( $import['type'], array( 'replace', 'append' ), true ) && empty( $import['existing_table'] ) ) { | |
| 1116 | - if ( $unlink_file ) { | |
| 1117 | - @unlink( $import_data['file_location'] ); | |
| 1118 | - } | |
| 1119 | - TablePress::redirect( array( 'action' => 'import', 'message' => 'error_import_no_existing_id', 'import_format' => $import['format'], 'import_type' => $import['type'], 'import_source' => $import['source'] ) ); | |
| 1120 | - } | |
| 1192 | + // Move file upload data to the main import configuration. | |
| 1193 | + $import_config['file-upload'] = $_FILES['import_file_upload'] ?? null; | |
| 1121 | 1194 | |
| 1122 | - if ( ! isset( $import_data['data'] ) ) { | |
| 1123 | - $import_data['data'] = file_get_contents( $import_data['file_location'] ); | |
| 1124 | - } | |
| 1125 | - if ( false === $import_data['data'] ) { | |
| 1126 | - TablePress::redirect( array( 'action' => 'import', 'message' => 'error_import' ) ); | |
| 1127 | - } | |
| 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.' ) ); | |
| 1198 | + } | |
| 1128 | 1199 | |
| 1129 | - $name = $import_data['file_name']; | |
| 1130 | - $description = $import_data['file_name']; | |
| 1131 | - $existing_table_id = ( in_array( $import['type'], array( 'replace', 'append' ), true ) && ! empty( $import['existing_table'] ) ) ? $import['existing_table'] : false; | |
| 1132 | - $table_id = $this->_import_tablepress_table( $import['format'], $import_data['data'], $name, $description, $existing_table_id, $import['type'] ); | |
| 1200 | + // Set default values for non-essential configuration variables. | |
| 1201 | + if ( ! isset( $import_config['type'] ) ) { | |
| 1202 | + $import_config['type'] = 'add'; | |
| 1203 | + } | |
| 1204 | + if ( ! isset( $import_config['existing_table'] ) ) { | |
| 1205 | + $import_config['existing_table'] = ''; | |
| 1206 | + } | |
| 1133 | 1207 | |
| 1134 | - if ( $unlink_file ) { | |
| 1135 | - @unlink( $import_data['file_location'] ); | |
| 1136 | - } | |
| 1208 | + $import_config['legacy_import'] = ( isset( $import_config['legacy_import'] ) && 'true' === $import_config['legacy_import'] ); | |
| 1137 | 1209 | |
| 1138 | - if ( is_wp_error( $table_id ) ) { | |
| 1139 | - TablePress::redirect( array( 'action' => 'import', 'message' => 'error_import_data' ) ); | |
| 1140 | - } else { | |
| 1141 | - TablePress::redirect( array( 'action' => 'edit', 'table_id' => $table_id, 'message' => 'success_import' ) ); | |
| 1142 | - } | |
| 1143 | - } else { | |
| 1144 | - // Zipping can use a lot of memory and execution time, but not this much hopefully. | |
| 1145 | - /** This filter is documented in the WordPress file wp-admin/admin.php */ | |
| 1146 | - @ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', WP_MAX_MEMORY_LIMIT ) ); | |
| 1147 | - @set_time_limit( 300 ); | |
| 1210 | + $importer = TablePress::load_class( 'TablePress_Import', 'class-import.php', 'classes' ); | |
| 1211 | + $import = $importer->run( $import_config ); | |
| 1148 | 1212 | |
| 1149 | - $zip = new ZipArchive(); | |
| 1150 | - if ( true !== $zip->open( $import_data['file_location'], ZIPARCHIVE::CHECKCONS ) ) { | |
| 1151 | - if ( $unlink_file ) { | |
| 1152 | - @unlink( $import_data['file_location'] ); | |
| 1153 | - } | |
| 1154 | - TablePress::redirect( array( 'action' => 'import', 'message' => 'error_import_zip_open' ) ); | |
| 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'] ]; | |
| 1155 | 1224 | } |
| 1156 | - | |
| 1157 | - $imported_files = array(); | |
| 1158 | - for ( $file_idx = 0; $file_idx < $zip->numFiles; $file_idx++ ) { | |
| 1159 | - $file_name = $zip->getNameIndex( $file_idx ); | |
| 1160 | - // Skip directories. | |
| 1161 | - if ( '/' === substr( $file_name, -1 ) ) { | |
| 1162 | - continue; | |
| 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 ); | |
| 1163 | 1231 | } |
| 1164 | - // Skip the __MACOSX directory that Mac OSX adds to archives. | |
| 1165 | - if ( '__MACOSX/' === substr( $file_name, 0, 9 ) ) { | |
| 1166 | - continue; | |
| 1167 | - } | |
| 1168 | - $data = $zip->getFromIndex( $file_idx ); | |
| 1169 | - if ( false === $data ) { | |
| 1170 | - continue; | |
| 1171 | - } | |
| 1172 | - | |
| 1173 | - $name = $file_name; | |
| 1174 | - $description = $file_name; | |
| 1175 | - $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? For the JSON format, a check is done after the import. | |
| 1176 | - $table_id = $this->_import_tablepress_table( $import['format'], $data, $name, $description, $existing_table_id, $import['type'] ); | |
| 1177 | - if ( is_wp_error( $table_id ) ) { | |
| 1178 | - continue; | |
| 1179 | - } else { | |
| 1180 | - $imported_files[] = $table_id; | |
| 1181 | - } | |
| 1182 | - }; | |
| 1183 | - $zip->close(); | |
| 1184 | - | |
| 1185 | - if ( $unlink_file ) { | |
| 1186 | - @unlink( $import_data['file_location'] ); | |
| 1232 | + $redirect_parameters['error_details'] = implode( ', ', $wp_error_strings ); | |
| 1187 | 1233 | } |
| 1188 | - | |
| 1189 | - if ( count( $imported_files ) > 1 ) { | |
| 1190 | - TablePress::redirect( array( 'action' => 'list', 'message' => 'success_import' ) ); | |
| 1191 | - } elseif ( 1 === count( $imported_files ) ) { | |
| 1192 | - TablePress::redirect( array( 'action' => 'edit', 'table_id' => $imported_files[0], 'message' => 'success_import' ) ); | |
| 1193 | - } else { | |
| 1194 | - TablePress::redirect( array( 'action' => 'import', 'message' => 'error_import_zip_content' ) ); | |
| 1195 | - } | |
| 1234 | + TablePress::redirect( $redirect_parameters ); | |
| 1196 | 1235 | } |
| 1197 | 1236 | |
| 1198 | - } | |
| 1199 | - | |
| 1200 | - /** | |
| 1201 | - * Import a table by either replacing an existing table or adding it as a new table. | |
| 1202 | - * | |
| 1203 | - * @since 1.0.0 | |
| 1204 | - * | |
| 1205 | - * @param string $format Import format. | |
| 1206 | - * @param string $data Data to import. | |
| 1207 | - * @param string $name Name of the table. | |
| 1208 | - * @param string $description Description of the table. | |
| 1209 | - * @param bool|string $existing_table_id False if table shall be added new, ID of the table to be replaced or appended to otherwise. | |
| 1210 | - * @param string $import_type What to do with the imported data: "add", "replace", "append". | |
| 1211 | - * @return string|WP_Error WP_Error on error, table ID on success. | |
| 1212 | - */ | |
| 1213 | - protected function _import_tablepress_table( $format, $data, $name, $description, $existing_table_id, $import_type ) { | |
| 1214 | - $imported_table = $this->importer->import_table( $format, $data ); | |
| 1215 | - if ( false === $imported_table ) { | |
| 1216 | - return new WP_Error( 'table_import_import_failed' ); | |
| 1217 | - } | |
| 1218 | - | |
| 1219 | - // Full JSON format table can contain a table ID, try to keep that. | |
| 1220 | - $table_id_in_import = isset( $imported_table['id'] ) ? $imported_table['id'] : false; | |
| 1221 | - | |
| 1222 | - // If no ID for an existing table was specified in the import form, we add the imported table, | |
| 1223 | - // except for replacing and appending of JSON files in ZIP archives, where we try to use the imported table ID. | |
| 1224 | - if ( false === $existing_table_id ) { | |
| 1225 | - if ( false !== $table_id_in_import && TablePress::$model_table->table_exists( $table_id_in_import ) ) { | |
| 1226 | - $existing_table_id = $table_id_in_import; | |
| 1227 | - } else { | |
| 1228 | - $import_type = 'add'; | |
| 1229 | - } | |
| 1230 | - } | |
| 1231 | - | |
| 1232 | - // To be able to replace or append to a table, editing that table must be allowed. | |
| 1233 | - if ( in_array( $import_type, array( 'replace', 'append' ), true ) && ! current_user_can( 'tablepress_edit_table', $existing_table_id ) ) { | |
| 1234 | - return new WP_Error( 'table_import_replace_append_capability_check_failed' ); | |
| 1235 | - } | |
| 1236 | - | |
| 1237 | - switch ( $import_type ) { | |
| 1238 | - case 'add': | |
| 1239 | - $existing_table = TablePress::$model_table->get_table_template(); | |
| 1240 | - // If name and description are imported from a new table, use those. | |
| 1241 | - if ( ! isset( $imported_table['name'] ) ) { | |
| 1242 | - $imported_table['name'] = $name; | |
| 1243 | - } | |
| 1244 | - if ( ! isset( $imported_table['description'] ) ) { | |
| 1245 | - $imported_table['description'] = $description; | |
| 1246 | - } | |
| 1247 | - if ( isset( $imported_table['visibility'] ) && isset( $imported_table['visibility']['rows'] ) && isset( $imported_table['visibility']['columns'] ) ) { | |
| 1248 | - $existing_table['visibility']['rows'] = $imported_table['visibility']['rows']; | |
| 1249 | - $existing_table['visibility']['columns'] = $imported_table['visibility']['columns']; | |
| 1250 | - } | |
| 1251 | - break; | |
| 1252 | - case 'replace': | |
| 1253 | - // Load table, without table data, but with options and visibility settings. | |
| 1254 | - $existing_table = TablePress::$model_table->load( $existing_table_id, false, true ); | |
| 1255 | - if ( is_wp_error( $existing_table ) ) { | |
| 1256 | - // Add an error code to the existing WP_Error. | |
| 1257 | - $existing_table->add( 'table_import_replace_table_load', '', $existing_table_id ); | |
| 1258 | - return $existing_table; | |
| 1259 | - } | |
| 1260 | - // Don't change name and description when a table is replaced. | |
| 1261 | - $imported_table['name'] = $existing_table['name']; | |
| 1262 | - $imported_table['description'] = $existing_table['description']; | |
| 1263 | - if ( isset( $imported_table['visibility'] ) && isset( $imported_table['visibility']['rows'] ) && isset( $imported_table['visibility']['columns'] ) ) { | |
| 1264 | - $existing_table['visibility']['rows'] = $imported_table['visibility']['rows']; | |
| 1265 | - $existing_table['visibility']['columns'] = $imported_table['visibility']['columns']; | |
| 1266 | - } | |
| 1267 | - break; | |
| 1268 | - case 'append': | |
| 1269 | - // Load table, with table data, options, and visibility settings. | |
| 1270 | - $existing_table = TablePress::$model_table->load( $existing_table_id, true, true ); | |
| 1271 | - if ( is_wp_error( $existing_table ) ) { | |
| 1272 | - // Add an error code to the existing WP_Error. | |
| 1273 | - $existing_table->add( 'table_import_append_table_load', '', $existing_table_id ); | |
| 1274 | - return $existing_table; | |
| 1275 | - } | |
| 1276 | - if ( isset( $existing_table['is_corrupted'] ) && $existing_table['is_corrupted'] ) { | |
| 1277 | - return new WP_Error( 'table_import_append_table_load_corrupted', '', $existing_table_id ); | |
| 1278 | - } | |
| 1279 | - // Don't change name and description when a table is appended to. | |
| 1280 | - $imported_table['name'] = $existing_table['name']; | |
| 1281 | - $imported_table['description'] = $existing_table['description']; | |
| 1282 | - // Actual appending: | |
| 1283 | - $imported_table['data'] = array_merge( $existing_table['data'], $imported_table['data'] ); | |
| 1284 | - $imported_table['data'] = $this->importer->pad_array_to_max_cols( $imported_table['data'] ); | |
| 1285 | - // Append visibility information for rows. | |
| 1286 | - if ( isset( $imported_table['visibility'] ) && isset( $imported_table['visibility']['rows'] ) ) { | |
| 1287 | - $existing_table['visibility']['rows'] = array_merge( $existing_table['visibility']['rows'], $imported_table['visibility']['rows'] ); | |
| 1288 | - } | |
| 1289 | - // When appending, do not overwrite options. | |
| 1290 | - if ( isset( $imported_table['options'] ) ) { | |
| 1291 | - unset( $imported_table['options'] ); | |
| 1292 | - } | |
| 1293 | - break; | |
| 1294 | - default: | |
| 1295 | - return new WP_Error( 'table_import_import_type_invalid', '', $import_type ); | |
| 1296 | - } | |
| 1297 | - | |
| 1298 | - // Merge new or existing table with information from the imported table. | |
| 1299 | - $imported_table['id'] = $existing_table['id']; // will be false for new table or the existing table ID | |
| 1300 | - // Cut visibility array (if the imported table is smaller), and pad correctly if imported table is bigger than existing table (or new template). | |
| 1301 | - $num_rows = count( $imported_table['data'] ); | |
| 1302 | - $num_columns = count( $imported_table['data'][0] ); | |
| 1303 | - $imported_table['visibility'] = array( | |
| 1304 | - 'rows' => array_pad( array_slice( $existing_table['visibility']['rows'], 0, $num_rows ), $num_rows, 1 ), | |
| 1305 | - 'columns' => array_pad( array_slice( $existing_table['visibility']['columns'], 0, $num_columns ), $num_columns, 1 ), | |
| 1306 | - ); | |
| 1307 | - | |
| 1308 | - // Check if new data is ok. | |
| 1309 | - $table = TablePress::$model_table->prepare_table( $existing_table, $imported_table, false ); | |
| 1310 | - if ( is_wp_error( $table ) ) { | |
| 1311 | - // Add an error code to the existing WP_Error. | |
| 1312 | - $table->add( 'table_import_table_prepare', '' ); | |
| 1313 | - return $table; | |
| 1314 | - } | |
| 1315 | - | |
| 1316 | - // DataTables Custom Commands can only be edit by trusted users. | |
| 1317 | - if ( ! current_user_can( 'unfiltered_html' ) ) { | |
| 1318 | - $table['options']['datatables_custom_commands'] = $existing_table['options']['datatables_custom_commands']; | |
| 1319 | - } | |
| 1320 | - | |
| 1321 | - // Replace existing table or add new table. | |
| 1322 | - if ( in_array( $import_type, array( 'replace', 'append' ), true ) ) { | |
| 1323 | - // Replace existing table with imported/appended table. | |
| 1324 | - $table_id = TablePress::$model_table->save( $table ); | |
| 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' ) ); | |
| 1325 | 1242 | } else { |
| 1326 | - // Add the imported table (and get its first ID). | |
| 1327 | - $table_id = TablePress::$model_table->add( $table ); | |
| 1243 | + TablePress::redirect( array( 'action' => 'import', 'message' => 'error_import', 'error_details' => 'The number of imported tables is invalid.' ) ); | |
| 1328 | 1244 | } |
| 1329 | - | |
| 1330 | - if ( is_wp_error( $table_id ) ) { | |
| 1331 | - // Add an error code to the existing WP_Error. | |
| 1332 | - $table_id->add( 'table_import_table_save_or_add', '' ); | |
| 1333 | - return $table_id; | |
| 1334 | - } | |
| 1335 | - | |
| 1336 | - // Try to use ID from imported file (e.g. in full JSON format table). | |
| 1337 | - if ( false !== $table_id_in_import && $table_id !== $table_id_in_import && current_user_can( 'tablepress_edit_table_id', $table_id ) ) { | |
| 1338 | - $id_changed = TablePress::$model_table->change_table_id( $table_id, $table_id_in_import ); | |
| 1339 | - if ( ! is_wp_error( $id_changed ) ) { | |
| 1340 | - $table_id = $table_id_in_import; | |
| 1341 | - } | |
| 1342 | - } | |
| 1343 | - | |
| 1344 | - return $table_id; | |
| 1345 | 1245 | } |
| 1346 | 1246 | |
| 1347 | 1247 | /* |
| 1348 | - * Save GET actions. | |
| 1248 | + * HTTP GET actions. | |
| 1349 | 1249 | */ |
| 1350 | 1250 | |
| 1351 | 1251 | /** |
| 1352 | 1252 | * Hide a header message on an admin screen. |
| @@ -1352,9 +1252,9 @@ | ||
| 1352 | 1252 | * Hide a header message on an admin screen. |
| 1353 | 1253 | * |
| 1354 | 1254 | * @since 1.0.0 |
| 1355 | 1255 | */ |
| 1356 | - public function handle_get_action_hide_message() { | |
| 1256 | + public function handle_get_action_hide_message(): void { | |
| 1357 | 1257 | $message_item = ! empty( $_GET['item'] ) ? $_GET['item'] : ''; |
| 1358 | 1258 | TablePress::check_nonce( 'hide_message', $message_item ); |
| 1359 | 1259 | |
| 1360 | 1260 | if ( ! current_user_can( 'tablepress_list_tables' ) ) { |
| @@ -1371,9 +1271,9 @@ | ||
| 1371 | 1271 | * Delete a table. |
| 1372 | 1272 | * |
| 1373 | 1273 | * @since 1.0.0 |
| 1374 | 1274 | */ |
| 1375 | - public function handle_get_action_delete_table() { | |
| 1275 | + public function handle_get_action_delete_table(): void { | |
| 1376 | 1276 | $table_id = ( ! empty( $_GET['item'] ) ) ? $_GET['item'] : false; |
| 1377 | 1277 | TablePress::check_nonce( 'delete_table', $table_id ); |
| 1378 | 1278 | |
| 1379 | 1279 | $return = ! empty( $_GET['return'] ) ? $_GET['return'] : 'list'; |
| @@ -1378,9 +1278,9 @@ | ||
| 1378 | 1278 | |
| 1379 | 1279 | $return = ! empty( $_GET['return'] ) ? $_GET['return'] : 'list'; |
| 1380 | 1280 | $return_item = ! empty( $_GET['return_item'] ) ? $_GET['return_item'] : false; |
| 1381 | 1281 | |
| 1382 | - // Nonce check should actually catch this already. | |
| 1282 | + // The nonce check should actually catch this already. | |
| 1383 | 1283 | if ( false === $table_id ) { |
| 1384 | 1284 | TablePress::redirect( array( 'action' => $return, 'message' => 'error_delete', 'table_id' => $return_item ) ); |
| 1385 | 1285 | } |
| 1386 | 1286 | |
| @@ -1389,9 +1289,9 @@ | ||
| 1389 | 1289 | } |
| 1390 | 1290 | |
| 1391 | 1291 | $deleted = TablePress::$model_table->delete( $table_id ); |
| 1392 | 1292 | if ( is_wp_error( $deleted ) ) { |
| 1393 | - 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 ) ) ); | |
| 1394 | 1294 | } |
| 1395 | 1295 | |
| 1396 | 1296 | /* |
| 1397 | 1297 | * Slightly more complex redirect method, to account for sort, search, and pagination in the WP_List_Table on the List View, |
| @@ -1412,9 +1312,9 @@ | ||
| 1412 | 1312 | * Copy a table. |
| 1413 | 1313 | * |
| 1414 | 1314 | * @since 1.0.0 |
| 1415 | 1315 | */ |
| 1416 | - public function handle_get_action_copy_table() { | |
| 1316 | + public function handle_get_action_copy_table(): void { | |
| 1417 | 1317 | $table_id = ( ! empty( $_GET['item'] ) ) ? $_GET['item'] : false; |
| 1418 | 1318 | TablePress::check_nonce( 'copy_table', $table_id ); |
| 1419 | 1319 | |
| 1420 | 1320 | $return = ! empty( $_GET['return'] ) ? $_GET['return'] : 'list'; |
| @@ -1419,9 +1319,9 @@ | ||
| 1419 | 1319 | |
| 1420 | 1320 | $return = ! empty( $_GET['return'] ) ? $_GET['return'] : 'list'; |
| 1421 | 1321 | $return_item = ! empty( $_GET['return_item'] ) ? $_GET['return_item'] : false; |
| 1422 | 1322 | |
| 1423 | - // Nonce check should actually catch this already. | |
| 1323 | + // The nonce check should actually catch this already. | |
| 1424 | 1324 | if ( false === $table_id ) { |
| 1425 | 1325 | TablePress::redirect( array( 'action' => $return, 'message' => 'error_copy', 'table_id' => $return_item ) ); |
| 1426 | 1326 | } |
| 1427 | 1327 | |
| @@ -1430,12 +1330,11 @@ | ||
| 1430 | 1330 | } |
| 1431 | 1331 | |
| 1432 | 1332 | $copy_table_id = TablePress::$model_table->copy( $table_id ); |
| 1433 | 1333 | if ( is_wp_error( $copy_table_id ) ) { |
| 1434 | - TablePress::redirect( array( 'action' => $return, 'message' => 'error_copy', 'table_id' => $return_item ) ); | |
| 1435 | - } else { | |
| 1436 | - $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 ) ) ); | |
| 1437 | 1335 | } |
| 1336 | + $return_item = $copy_table_id; | |
| 1438 | 1337 | |
| 1439 | 1338 | /* |
| 1440 | 1339 | * Slightly more complex redirect method, to account for sort, search, and pagination in the WP_List_Table on the List View, |
| 1441 | 1340 | * but only if this action succeeds, to have everything fresh in the event of an error. |
| @@ -1455,9 +1354,9 @@ | ||
| 1455 | 1354 | * Preview a table. |
| 1456 | 1355 | * |
| 1457 | 1356 | * @since 1.0.0 |
| 1458 | 1357 | */ |
| 1459 | - public function handle_get_action_preview_table() { | |
| 1358 | + public function handle_get_action_preview_table(): void { | |
| 1460 | 1359 | $table_id = ( ! empty( $_GET['item'] ) ) ? $_GET['item'] : false; |
| 1461 | 1360 | TablePress::check_nonce( 'preview_table', $table_id ); |
| 1462 | 1361 | |
| 1463 | 1362 | // Nonce check should actually catch this already. |
| @@ -1488,18 +1387,22 @@ | ||
| 1488 | 1387 | $default_render_options = apply_filters( 'tablepress_shortcode_table_default_shortcode_atts', $default_render_options ); |
| 1489 | 1388 | $render_options = shortcode_atts( $default_render_options, $table['options'] ); |
| 1490 | 1389 | /** This filter is documented in controllers/controller-frontend.php */ |
| 1491 | 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; | |
| 1492 | 1393 | $_render->set_input( $table, $render_options ); |
| 1493 | 1394 | $view_data = array( |
| 1494 | - 'table_id' => $table_id, | |
| 1495 | - 'head_html' => $_render->get_preview_css(), | |
| 1496 | - '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(), | |
| 1497 | 1399 | ); |
| 1498 | 1400 | |
| 1499 | 1401 | $custom_css = TablePress::$model_options->get( 'custom_css' ); |
| 1500 | - if ( ! empty( $custom_css ) ) { | |
| 1501 | - $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"; | |
| 1502 | 1405 | } |
| 1503 | 1406 | |
| 1504 | 1407 | // Prepare, initialize, and render the view. |
| 1505 | 1408 | $this->view = TablePress::load_view( 'preview_table', $view_data ); |
| @@ -1506,13 +1409,13 @@ | ||
| 1506 | 1409 | $this->view->render(); |
| 1507 | 1410 | } |
| 1508 | 1411 | |
| 1509 | 1412 | /** |
| 1510 | - * 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). | |
| 1511 | 1414 | * |
| 1512 | 1415 | * @since 1.0.0 |
| 1513 | 1416 | */ |
| 1514 | - public function handle_get_action_editor_button_thickbox() { | |
| 1417 | + public function handle_get_action_editor_button_thickbox(): void { | |
| 1515 | 1418 | TablePress::check_nonce( 'editor_button_thickbox' ); |
| 1516 | 1419 | |
| 1517 | 1420 | if ( ! current_user_can( 'tablepress_list_tables' ) ) { |
| 1518 | 1421 | wp_die( __( 'Sorry, you are not allowed to access this page.', 'default' ), 403 ); |
| @@ -1534,9 +1437,9 @@ | ||
| 1534 | 1437 | * Uninstall TablePress, and delete all tables and options. |
| 1535 | 1438 | * |
| 1536 | 1439 | * @since 1.0.0 |
| 1537 | 1440 | */ |
| 1538 | - public function handle_get_action_uninstall_tablepress() { | |
| 1441 | + public function handle_get_action_uninstall_tablepress(): void { | |
| 1539 | 1442 | TablePress::check_nonce( 'uninstall_tablepress' ); |
| 1540 | 1443 | |
| 1541 | 1444 | $plugin = TABLEPRESS_BASENAME; |
| 1542 | 1445 | |
| @@ -1556,9 +1459,9 @@ | ||
| 1556 | 1459 | |
| 1557 | 1460 | TablePress::$model_table->destroy(); |
| 1558 | 1461 | TablePress::$model_options->destroy(); |
| 1559 | 1462 | |
| 1560 | - $output = '<strong>' . __( 'TablePress was uninstalled successfully.', 'tablepress' ) . '</strong><br /><br />'; | |
| 1463 | + $output = '<strong>' . __( 'TablePress was uninstalled successfully.', 'tablepress' ) . '</strong><br><br>'; | |
| 1561 | 1464 | $output .= __( 'All tables, data, and options were deleted.', 'tablepress' ); |
| 1562 | 1465 | if ( is_multisite() ) { |
| 1563 | 1466 | $output .= ' ' . __( 'You may now ask the network admin to delete the plugin’s folder <code>tablepress</code> from the server, if no other site in the network uses it.', 'tablepress' ); |
| 1564 | 1467 | } else { |
| @@ -1565,9 +1468,9 @@ | ||
| 1565 | 1468 | $output .= ' ' . __( 'You may now manually delete the plugin’s folder <code>tablepress</code> from the <code>plugins</code> directory on your server or use the “Delete” link for TablePress on the WordPress “Plugins” page.', 'tablepress' ); |
| 1566 | 1469 | } |
| 1567 | 1470 | if ( $css_files_deleted ) { |
| 1568 | 1471 | $output .= ' ' . __( 'Your TablePress “Custom CSS” files have been deleted automatically.', 'tablepress' ); |
| 1569 | - } else { | |
| 1472 | + } else { // phpcs:ignore Universal.ControlStructures.DisallowLonelyIf.Found | |
| 1570 | 1473 | if ( is_multisite() ) { |
| 1571 | 1474 | $output .= ' ' . __( 'Please also ask him to delete your TablePress “Custom CSS” files from the server.', 'tablepress' ); |
| 1572 | 1475 | } else { |
| 1573 | 1476 | $output .= ' ' . __( 'You may now also delete your TablePress “Custom CSS” files in the <code>wp-content</code> folder.', 'tablepress' ); |