| @@ -7,13 +7,16 @@ | ||
| 7 | 7 | * @author Tobias Bäthge |
| 8 | 8 | * @since 1.0.0 |
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | +declare(strict_types=1); | |
| 12 | + | |
| 11 | 13 | // Prohibit direct script loading. |
| 12 | 14 | defined( 'ABSPATH' ) || die( 'No direct script access allowed!' ); |
| 13 | 15 | |
| 14 | 16 | /** |
| 15 | 17 | * Import Table View class |
| 18 | + * | |
| 16 | 19 | * @package TablePress |
| 17 | 20 | * @subpackage Views |
| 18 | 21 | * @author Tobias Bäthge |
| 19 | 22 | * @since 1.0.0 |
| @@ -20,32 +23,47 @@ | ||
| 20 | 23 | */ |
| 21 | 24 | class TablePress_Import_View extends TablePress_View { |
| 22 | 25 | |
| 23 | 26 | /** |
| 27 | + * List of WP feature pointers for this view. | |
| 28 | + * | |
| 29 | + * @since 2.0.0 | |
| 30 | + * @var string[] | |
| 31 | + */ | |
| 32 | + protected array $wp_pointers = array( 'tp20_import_drag_drop_detect_format' ); | |
| 33 | + | |
| 34 | + /** | |
| 24 | 35 | * Set up the view with data and do things that are specific for this view. |
| 25 | 36 | * |
| 26 | 37 | * @since 1.0.0 |
| 27 | 38 | * |
| 28 | - * @param string $action Action for this view. | |
| 29 | - * @param array $data Data for this view. | |
| 39 | + * @param string $action Action for this view. | |
| 40 | + * @param array<string, mixed> $data Data for this view. | |
| 30 | 41 | */ |
| 31 | - public function setup( $action, array $data ) { | |
| 42 | + #[\Override] | |
| 43 | + public function setup( /* string */ $action, array $data ) /* : void */ { | |
| 44 | + // Don't use type hints in the method declaration to prevent PHP errors, as the method is inherited. | |
| 45 | + | |
| 32 | 46 | parent::setup( $action, $data ); |
| 33 | 47 | |
| 34 | - $this->admin_page->enqueue_script( 'import', array( 'jquery' ) ); | |
| 48 | + TablePress::enqueue_script( 'common', array( 'jquery-core', 'postbox' ) ); | |
| 35 | 49 | |
| 50 | + $this->add_text_box( 'no-javascript', array( $this, 'textbox_no_javascript' ), 'header' ); | |
| 51 | + | |
| 52 | + TablePress::enqueue_style( 'import' ); | |
| 53 | + TablePress::enqueue_script( 'import' ); | |
| 54 | + | |
| 36 | 55 | $this->process_action_messages( array( |
| 37 | - 'error_import' => __( 'Error: The import failed.', 'tablepress' ), | |
| 38 | - 'error_no_zip_import' => __( 'Error: Import of ZIP files is not available on this server.', 'tablepress' ), | |
| 39 | - 'error_import_zip_open' => __( 'Error: The ZIP file could not be opened.', 'tablepress' ), | |
| 40 | - 'error_import_zip_content' => __( 'Error: The data in the ZIP file is invalid.', 'tablepress' ), | |
| 41 | - 'error_import_no_existing_id' => __( 'Error: You selected to replace or append to an existing table, but did not select a table.', 'tablepress' ), | |
| 42 | - 'error_import_source_invalid' => __( 'Error: The source for the import is invalid or could not be accessed.', 'tablepress' ), | |
| 43 | - 'error_import_data' => __( 'Error: The data for the import is invalid.', 'tablepress' ), | |
| 56 | + 'error_import' => __( 'Error: The import failed.', 'tablepress' ), | |
| 44 | 57 | ) ); |
| 45 | 58 | |
| 46 | 59 | $this->add_text_box( 'head', array( $this, 'textbox_head' ), 'normal' ); |
| 47 | 60 | $this->add_meta_box( 'import-form', __( 'Import Tables', 'tablepress' ), array( $this, 'postbox_import_form' ), 'normal' ); |
| 61 | + $screen = get_current_screen(); | |
| 62 | + add_filter( "postbox_classes_{$screen->id}_tablepress_{$this->action}-import-form", array( $this, 'postbox_classes' ) ); // @phpstan-ignore property.nonObject | |
| 63 | + if ( ! TABLEPRESS_IS_PLAYGROUND_PREVIEW ) { | |
| 64 | + $this->add_meta_box( 'tables-auto-import', __( 'Automatic Periodic Table Import', 'tablepress' ), array( $this, 'postbox_auto_import' ), 'additional' ); | |
| 65 | + } | |
| 48 | 66 | } |
| 49 | 67 | |
| 50 | 68 | /** |
| 51 | 69 | * Print the screen head text. |
| @@ -51,145 +69,107 @@ | ||
| 51 | 69 | * Print the screen head text. |
| 52 | 70 | * |
| 53 | 71 | * @since 1.0.0 |
| 54 | 72 | * |
| 55 | - * @param array $data Data for this screen. | |
| 56 | - * @param array $box Information about the text box. | |
| 73 | + * @param array<string, mixed> $data Data for this screen. | |
| 74 | + * @param array<string, mixed> $box Information about the text box. | |
| 57 | 75 | */ |
| 58 | - public function textbox_head( array $data, array $box ) { | |
| 76 | + public function textbox_head( array $data, array $box ): void { | |
| 59 | 77 | ?> |
| 60 | 78 | <p> |
| 61 | - <?php _e( 'TablePress can import tables from existing data, like from a CSV, XLS, or XLSX file from a spreadsheet application (e.g. Excel), an HTML file resembling a webpage, or its own JSON format.', 'tablepress' ); ?> | |
| 79 | + <?php _e( 'TablePress can import tables from common spreadsheet applications, like XLSX files from Excel, or CSV, ODS, HTML, and JSON files.', 'tablepress' ); ?> | |
| 62 | 80 | </p> |
| 63 | 81 | <p> |
| 64 | - <?php _e( 'To import a table, select and enter the import source in the following form.', 'tablepress' ); ?> | |
| 65 | - <?php | |
| 66 | - if ( 0 < $data['tables_count'] ) { | |
| 67 | - _e( 'You can also choose to import it as a new table, to replace an existing table, or to append the rows to an existing table.', 'tablepress' ); | |
| 68 | - } | |
| 69 | - ?> | |
| 82 | + <?php _e( 'To import tables, select and enter the import source in the following form.', 'tablepress' ); ?> | |
| 83 | + <?php _e( 'You can also choose to import it as a new table, to replace an existing table, or to append the rows to an existing table.', 'tablepress' ); ?> | |
| 70 | 84 | </p> |
| 71 | 85 | <?php |
| 72 | 86 | } |
| 73 | 87 | |
| 74 | 88 | /** |
| 75 | - * Print the content of the "Import Tables" post meta box. | |
| 89 | + * Prints the content of the "Import Tables" post meta box. | |
| 76 | 90 | * |
| 77 | 91 | * @since 1.0.0 |
| 78 | 92 | * |
| 79 | - * @param array $data Data for this screen. | |
| 80 | - * @param array $box Information about the meta box. | |
| 93 | + * @param array<string, mixed> $data Data for this screen. | |
| 94 | + * @param array<string, mixed> $box Information about the meta box. | |
| 81 | 95 | */ |
| 82 | - public function postbox_import_form( array $data, array $box ) { | |
| 83 | - ?> | |
| 84 | -<table class="tablepress-postbox-table fixed"> | |
| 85 | -<tbody> | |
| 86 | - <tr id="row-import-source"> | |
| 87 | - <th class="column-1" scope="row" id="import-source-header"><?php _e( 'Import Source', 'tablepress' ); ?>:</th> | |
| 88 | - <td class="column-2"> | |
| 89 | - <label for="tables-import-source-file-upload"><input name="import[source]" id="tables-import-source-file-upload" type="radio" aria-labelledby="import-source-header" value="file-upload"<?php checked( $data['import_source'], 'file-upload', true ); ?> /> <?php _e( 'File Upload', 'tablepress' ); ?></label> | |
| 90 | - <label for="tables-import-source-url"><input name="import[source]" id="tables-import-source-url" type="radio" aria-labelledby="import-source-header" value="url"<?php checked( $data['import_source'], 'url', true ); ?> /> <?php _e( 'URL', 'tablepress' ); ?></label> | |
| 91 | - <?php if ( ( ! is_multisite() && current_user_can( 'manage_options' ) ) || is_super_admin() ) { ?> | |
| 92 | - <label for="tables-import-source-server"><input name="import[source]" id="tables-import-source-server" type="radio" aria-labelledby="import-source-header" value="server"<?php checked( $data['import_source'], 'server', true ); ?> /> <?php _e( 'File on server', 'tablepress' ); ?></label> | |
| 93 | - <?php } ?> | |
| 94 | - <label for="tables-import-source-form-field"><input name="import[source]" id="tables-import-source-form-field" type="radio" aria-labelledby="import-source-header" value="form-field"<?php checked( $data['import_source'], 'form-field', true ); ?> /> <?php _e( 'Manual Input', 'tablepress' ); ?></label> | |
| 95 | - </td> | |
| 96 | - </tr> | |
| 97 | - <tr id="row-import-source-file-upload" class="bottom-border"> | |
| 98 | - <th class="column-1 top-align" scope="row"><label for="tables-import-file-upload"><?php _e( 'Select file', 'tablepress' ); ?>:</label></th> | |
| 99 | - <td class="column-2"> | |
| 100 | - <input name="import_file_upload" id="tables-import-file-upload" type="file" class="large-text" style="box-sizing: border-box;" /> | |
| 101 | - <?php | |
| 102 | - if ( $data['zip_support_available'] ) { | |
| 103 | - echo '<br /><span class="description">' . __( 'You can import multiple tables by placing them in a ZIP file.', 'tablepress' ) . '</span>'; | |
| 104 | - } | |
| 96 | + public function postbox_import_form( array $data, array $box ): void { | |
| 97 | + $this->print_script_data_json( | |
| 98 | + 'import', | |
| 99 | + array( | |
| 100 | + 'tables' => $data['tables'], | |
| 101 | + 'importSource' => $data['import_source'], | |
| 102 | + 'importType' => $data['import_type'], | |
| 103 | + 'importUrl' => $data['import_url'], | |
| 104 | + 'importServer' => $data['import_server'], | |
| 105 | + 'importFormField' => $data['import_form-field'], | |
| 106 | + 'importExistingTable' => $data['import_existing_table'], | |
| 107 | + 'showImportSourceServer' => ( ( ! is_multisite() && current_user_can( 'manage_options' ) ) || is_super_admin() ), | |
| 108 | + 'showImportSourceUrl' => current_user_can( 'tablepress_import_tables_url' ), | |
| 109 | + 'legacyImport' => $data['legacy_import'], | |
| 110 | + ), | |
| 111 | + ); | |
| 112 | + | |
| 113 | + echo '<div id="tablepress-import-screen"></div>'; | |
| 114 | + } | |
| 115 | + | |
| 116 | + /** | |
| 117 | + * Adds the "no-validation-highlighting" class to the "Import Tables" post meta box. | |
| 118 | + * | |
| 119 | + * @since 2.2.0 | |
| 120 | + * | |
| 121 | + * @param string[] $classes The array of postbox classes. | |
| 122 | + * @return string[] The modified array of postbox classes. | |
| 123 | + */ | |
| 124 | + public function postbox_classes( array $classes ): array { | |
| 125 | + $classes[] = 'no-validation-highlighting'; | |
| 126 | + return $classes; | |
| 127 | + } | |
| 128 | + | |
| 129 | + /** | |
| 130 | + * Prints the content of the "Automatic Periodic Table Import Screen" post meta box. | |
| 131 | + * | |
| 132 | + * @since 2.2.0 | |
| 133 | + * | |
| 134 | + * @param array<string, mixed> $data Data for this screen. | |
| 135 | + * @param array<string, mixed> $box Information about the meta box. | |
| 136 | + * | |
| 137 | + * @phpstan-ignore missingType.return (The method is extended elsewhere and can't have type hints.) | |
| 138 | + */ | |
| 139 | + public function postbox_auto_import( /* array */ $data, /* array */ $box ) /* : void */ { | |
| 140 | + // Don't use type hints in the method declaration, as the method is extended in the TablePress Table Auto Update Extension which is no longer updated. | |
| 141 | + | |
| 142 | + if ( tb_tp_fs()->is_free_plan() ) : | |
| 105 | 143 | ?> |
| 106 | - </td> | |
| 107 | - </tr> | |
| 108 | - <tr id="row-import-source-url" class="bottom-border"> | |
| 109 | - <th class="column-1 top-align" scope="row"><label for="tables-import-url"><?php _e( 'File URL', 'tablepress' ); ?>:</label></th> | |
| 110 | - <td class="column-2"> | |
| 111 | - <input type="text" name="import[url]" id="tables-import-url" class="large-text" value="<?php echo esc_attr( $data['import_url'] ); ?>" /> | |
| 144 | + <p style="font-size:14px;"> | |
| 145 | + <span class="dashicons dashicons-info-outline"></span> | |
| 146 | + <strong><?php _e( 'Pro Tip:', 'tablepress' ); ?></strong> | |
| 147 | + <?php | |
| 148 | + /* translators: %1$s: URL to TablePress website, %2$s: Module name */ | |
| 149 | + printf( __( 'You can automate the import of tables from URLs or server files with the <a href="%1$s">“%2$s” premium feature</a>!', 'tablepress' ), 'https://tablepress.org/modules/automatic-periodic-table-import/?utm_source=plugin&utm_medium=textlink&utm_content=import-screen', __( 'Automatic Periodic Table Import', 'tablepress' ) ); | |
| 150 | + ?> | |
| 151 | + </p> | |
| 112 | 152 | <?php |
| 113 | - if ( $data['zip_support_available'] ) { | |
| 114 | - echo '<br /><span class="description">' . __( 'You can import multiple tables by placing them in a ZIP file.', 'tablepress' ) . '</span>'; | |
| 115 | - } | |
| 116 | - ?> | |
| 117 | - </td> | |
| 118 | - </tr> | |
| 119 | - <?php if ( ( ! is_multisite() && current_user_can( 'manage_options' ) ) || is_super_admin() ) { ?> | |
| 120 | - <tr id="row-import-source-server" class="bottom-border"> | |
| 121 | - <th class="column-1 top-align" scope="row"><label for="tables-import-server"><?php _e( 'Server Path to file', 'tablepress' ); ?>:</label></th> | |
| 122 | - <td class="column-2"> | |
| 123 | - <input type="text" name="import[server]" id="tables-import-server" class="large-text" value="<?php echo esc_attr( $data['import_server'] ); ?>" /> | |
| 124 | - <?php | |
| 125 | - if ( $data['zip_support_available'] ) { | |
| 126 | - echo '<br /><span class="description">' . __( 'You can import multiple tables by placing them in a ZIP file.', 'tablepress' ) . '</span>'; | |
| 127 | - } | |
| 128 | - ?> | |
| 129 | - </td> | |
| 130 | - </tr> | |
| 131 | - <?php } ?> | |
| 132 | - <tr id="row-import-source-form-field" class="bottom-border"> | |
| 133 | - <th class="column-1 top-align" scope="row"><label for="tables-import-form-field"><?php _e( 'Import data', 'tablepress' ); ?>:</label></th> | |
| 134 | - <td class="column-2"> | |
| 135 | - <textarea name="import[form_field]" id="tables-import-form-field" rows="15" cols="40" class="large-text"><?php echo esc_textarea( $data['import_form_field'] ); ?></textarea> | |
| 136 | - </td> | |
| 137 | - </tr> | |
| 138 | - <tr class="top-border bottom-border"> | |
| 139 | - <th class="column-1" scope="row"><label for="tables-import-format"><?php _e( 'Import Format', 'tablepress' ); ?>:</label></th> | |
| 140 | - <td class="column-2"> | |
| 141 | - <select id="tables-import-format" name="import[format]"> | |
| 142 | - <?php | |
| 143 | - foreach ( $data['import_formats'] as $format => $name ) { | |
| 144 | - $selected = selected( $format, $data['import_format'], false ); | |
| 145 | - echo "<option{$selected} value=\"{$format}\">{$name}</option>"; | |
| 146 | - } | |
| 147 | - ?> | |
| 148 | - </select> | |
| 149 | - <?php | |
| 150 | - if ( ! $data['html_import_support_available'] ) { | |
| 151 | - echo '<br /><span class="description">' . __( 'Import of HTML files is not available on your server.', 'tablepress' ) . '</span>'; | |
| 152 | - } | |
| 153 | - ?> | |
| 154 | - </td> | |
| 155 | - </tr> | |
| 156 | - <tr id="row-import-type" class="top-border"> | |
| 157 | - <th class="column-1" scope="row" id="import-type-header"><?php _e( 'Add, Replace, or Append?', 'tablepress' ); ?>:</th> | |
| 158 | - <td class="column-2"> | |
| 159 | - <label for="tables-import-type-add"><input name="import[type]" id="tables-import-type-add" type="radio" aria-labelledby="import-type-header" value="add"<?php checked( $data['import_type'], 'add', true ); ?> /> <?php _e( 'Add as new table', 'tablepress' ); ?></label> | |
| 160 | - <label for="tables-import-type-replace"><input name="import[type]" id="tables-import-type-replace" type="radio" aria-labelledby="import-type-header" value="replace"<?php checked( $data['import_type'], 'replace', true ); ?><?php disabled( $data['tables_count'] > 0, false, true ); ?> /> <?php _e( 'Replace existing table', 'tablepress' ); ?></label> | |
| 161 | - <label for="tables-import-type-append"><input name="import[type]" id="tables-import-type-append" type="radio" aria-labelledby="import-type-header" value="append"<?php checked( $data['import_type'], 'append', true ); ?><?php disabled( $data['tables_count'] > 0, false, true ); ?> /> <?php _e( 'Append rows to existing table', 'tablepress' ); ?></label> | |
| 162 | - </td> | |
| 163 | - </tr> | |
| 164 | - <tr id="row-import-existing-table" class="bottom-border"> | |
| 165 | - <th class="column-1" scope="row"><label for="tables-import-existing-table"><?php _e( 'Table to replace or append to', 'tablepress' ); ?>:</label></th> | |
| 166 | - <td class="column-2"> | |
| 167 | - <select id="tables-import-existing-table" name="import[existing_table]"<?php disabled( $data['tables_count'] > 0, false, true ); ?>> | |
| 168 | - <option value=""><?php _e( '— Select —', 'tablepress' ); ?></option> | |
| 169 | - <?php | |
| 170 | - foreach ( $data['table_ids'] as $table_id ) { | |
| 171 | - $table = TablePress::$model_table->load( $table_id, false, false ); // Load table, without table data, options, and visibility settings | |
| 172 | - if ( ! current_user_can( 'tablepress_edit_table', $table['id'] ) ) { | |
| 173 | - continue; | |
| 174 | - } | |
| 175 | - if ( '' === trim( $table['name'] ) ) { | |
| 176 | - $table['name'] = __( '(no name)', 'tablepress' ); | |
| 177 | - } | |
| 178 | - $text = esc_html( sprintf( __( 'ID %1$s: %2$s', 'tablepress' ), $table['id'], $table['name'] ) ); | |
| 179 | - $selected = selected( $table['id'], $data['import_existing_table'], false ); | |
| 180 | - echo "<option{$selected} value=\"{$table['id']}\">{$text}</option>"; | |
| 181 | - } | |
| 182 | - ?> | |
| 183 | - </select> | |
| 184 | - </td> | |
| 185 | - </tr> | |
| 186 | - <tr class="top-border"> | |
| 187 | - <td class="column-1"></td> | |
| 188 | - <td class="column-2"><input type="submit" value="<?php echo esc_attr_x( 'Import', 'button', 'tablepress' ); ?>" class="button button-primary button-large" name="submit" /></td> | |
| 189 | - </tr> | |
| 190 | -</tbody> | |
| 191 | -</table> | |
| 192 | - <?php | |
| 153 | + endif; | |
| 154 | + } | |
| 155 | + | |
| 156 | + /** | |
| 157 | + * Sets the content for the WP feature pointer about the drag and drop import and format detection on the "Import" screen. | |
| 158 | + * | |
| 159 | + * @since 2.0.0 | |
| 160 | + */ | |
| 161 | + public function wp_pointer_tp20_import_drag_drop_detect_format(): void { | |
| 162 | + $content = '<h3>' . __( 'TablePress feature: Drag and Drop Import with Format Detection', 'tablepress' ) . '</h3>'; | |
| 163 | + $content .= '<p>' . __( 'Did you know?', 'tablepress' ) . ' ' . __( 'To import tables, you can simply drag and drop your spreadsheet files into this area and TablePress will automatically detect the file format!', 'tablepress' ) . '</p>'; | |
| 164 | + | |
| 165 | + $this->print_wp_pointer_js( | |
| 166 | + 'tp20_import_drag_drop_detect_format', | |
| 167 | + '#tables-import-file-upload-dropzone span', | |
| 168 | + array( | |
| 169 | + 'content' => $content, | |
| 170 | + 'position' => array( 'edge' => 'bottom', 'align' => 'center' ), | |
| 171 | + ), | |
| 172 | + ); | |
| 193 | 173 | } |
| 194 | 174 | |
| 195 | 175 | } // class TablePress_Import_View |