| @@ -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 | * Export Table View class |
| 18 | + * | |
| 16 | 19 | * @package TablePress |
| 17 | 20 | * @subpackage Views |
| 18 | 21 | * @author Tobias Bäthge |
| 19 | 22 | * @since 1.0.0 |
| @@ -24,14 +27,21 @@ | ||
| 24 | 27 | * Set up the view with data and do things that are specific for this view. |
| 25 | 28 | * |
| 26 | 29 | * @since 1.0.0 |
| 27 | 30 | * |
| 28 | - * @param string $action Action for this view. | |
| 29 | - * @param array $data Data for this view. | |
| 31 | + * @param string $action Action for this view. | |
| 32 | + * @param array<string, mixed> $data Data for this view. | |
| 30 | 33 | */ |
| 31 | - public function setup( $action, array $data ) { | |
| 34 | + #[\Override] | |
| 35 | + public function setup( /* string */ $action, array $data ) /* : void */ { | |
| 36 | + // Don't use type hints in the method declaration to prevent PHP errors, as the method is inherited. | |
| 37 | + | |
| 32 | 38 | parent::setup( $action, $data ); |
| 33 | 39 | |
| 40 | + TablePress::enqueue_script( 'common', array( 'jquery-core', 'postbox' ) ); | |
| 41 | + | |
| 42 | + $this->add_text_box( 'no-javascript', array( $this, 'textbox_no_javascript' ), 'header' ); | |
| 43 | + | |
| 34 | 44 | $this->process_action_messages( array( |
| 35 | 45 | 'error_export' => __( 'Error: The export failed.', 'tablepress' ), |
| 36 | 46 | 'error_load_table' => __( 'Error: This table could not be loaded!', 'tablepress' ), |
| 37 | 47 | 'error_table_corrupted' => __( 'Error: The internal data of this table is corrupted!', 'tablepress' ), |
| @@ -41,24 +51,22 @@ | ||
| 41 | 51 | $this->add_text_box( 'head', array( $this, 'textbox_head' ), 'normal' ); |
| 42 | 52 | if ( 0 === $data['tables_count'] ) { |
| 43 | 53 | $this->add_meta_box( 'no-tables', __( 'Export Tables', 'tablepress' ), array( $this, 'postbox_no_tables' ), 'normal' ); |
| 44 | 54 | } else { |
| 45 | - $this->admin_page->enqueue_script( 'export', array( 'jquery' ) ); | |
| 55 | + TablePress::enqueue_script( 'export' ); | |
| 46 | 56 | $this->add_meta_box( 'export-form', __( 'Export Tables', 'tablepress' ), array( $this, 'postbox_export_form' ), 'normal' ); |
| 47 | - $this->data['submit_button_caption'] = _x( 'Download Export File', 'button', 'tablepress' ); | |
| 48 | - $this->add_text_box( 'submit', array( $this, 'textbox_submit_button' ), 'submit' ); | |
| 49 | 57 | } |
| 50 | 58 | } |
| 51 | 59 | |
| 52 | 60 | /** |
| 53 | - * Print the screen head text. | |
| 61 | + * Prints the screen head text. | |
| 54 | 62 | * |
| 55 | 63 | * @since 1.0.0 |
| 56 | 64 | * |
| 57 | - * @param array $data Data for this screen. | |
| 58 | - * @param array $box Information about the text box. | |
| 65 | + * @param array<string, mixed> $data Data for this screen. | |
| 66 | + * @param array<string, mixed> $box Information about the text box. | |
| 59 | 67 | */ |
| 60 | - public function textbox_head( array $data, array $box ) { | |
| 68 | + public function textbox_head( array $data, array $box ): void { | |
| 61 | 69 | ?> |
| 62 | 70 | <p> |
| 63 | 71 | <?php _e( 'Exporting a table allows you to use it in other programs, like spreadsheets applications.', 'tablepress' ); ?> |
| 64 | 72 | <?php _e( 'Regularly exporting tables is also recommended as a backup of your data.', 'tablepress' ); ?> |
| @@ -65,9 +73,9 @@ | ||
| 65 | 73 | </p> |
| 66 | 74 | <p> |
| 67 | 75 | <?php _e( 'To export, select the tables and the desired export format.', 'tablepress' ); ?> |
| 68 | 76 | <?php _e( 'If you choose more than one table, the exported files will automatically be stored in a ZIP archive file.', 'tablepress' ); ?> |
| 69 | - <br /> | |
| 77 | + <br> | |
| 70 | 78 | <?php _e( 'Be aware that for the CSV and HTML formats only the table data, but no table options are exported!', 'tablepress' ); ?> |
| 71 | 79 | <?php _e( 'For the JSON format, the table data and the table options are exported.', 'tablepress' ); ?> |
| 72 | 80 | </p> |
| 73 | 81 | <?php |
| @@ -73,120 +81,51 @@ | ||
| 73 | 81 | <?php |
| 74 | 82 | } |
| 75 | 83 | |
| 76 | 84 | /** |
| 77 | - * Print the content of the "No tables found" post meta box. | |
| 85 | + * Prints the content of the "No tables found" post meta box. | |
| 78 | 86 | * |
| 79 | 87 | * @since 1.0.0 |
| 80 | 88 | * |
| 81 | - * @param array $data Data for this screen. | |
| 82 | - * @param array $box Information about the meta box. | |
| 89 | + * @param array<string, mixed> $data Data for this screen. | |
| 90 | + * @param array<string, mixed> $box Information about the meta box. | |
| 83 | 91 | */ |
| 84 | - public function postbox_no_tables( array $data, array $box ) { | |
| 92 | + public function postbox_no_tables( array $data, array $box ): void { | |
| 85 | 93 | $add_url = TablePress::url( array( 'action' => 'add' ) ); |
| 86 | 94 | $import_url = TablePress::url( array( 'action' => 'import' ) ); |
| 87 | 95 | ?> |
| 88 | 96 | <p><?php _e( 'No tables found.', 'tablepress' ); ?></p> |
| 89 | - <p><?php printf( __( 'You should <a href="%1$s">add</a> or <a href="%2$s">import</a> a table to get started!', 'tablepress' ), $add_url, $import_url ); ?></p> | |
| 97 | + <p> | |
| 98 | + <?php | |
| 99 | + /* translators: %1$s: URL to add table page, %2$s: URL to import table page */ | |
| 100 | + printf( __( 'You should <a href="%1$s">add</a> or <a href="%2$s">import</a> a table to get started!', 'tablepress' ), $add_url, $import_url ); | |
| 101 | + ?> | |
| 102 | + </p> | |
| 90 | 103 | <?php |
| 91 | 104 | } |
| 92 | 105 | |
| 93 | 106 | /** |
| 94 | - * Print the content of the "Export Tables" post meta box. | |
| 107 | + * Prints the content of the "Export Tables" post meta box. | |
| 95 | 108 | * |
| 96 | 109 | * @since 1.0.0 |
| 97 | 110 | * |
| 98 | - * @param array $data Data for this screen. | |
| 99 | - * @param array $box Information about the meta box. | |
| 111 | + * @param array<string, mixed> $data Data for this screen. | |
| 112 | + * @param array<string, mixed> $box Information about the meta box. | |
| 100 | 113 | */ |
| 101 | - public function postbox_export_form( array $data, array $box ) { | |
| 102 | - ?> | |
| 103 | -<table class="tablepress-postbox-table fixed"> | |
| 104 | -<tbody> | |
| 105 | - <tr> | |
| 106 | - <th class="column-1 top-align" scope="row"> | |
| 107 | - <label for="tables-export"><?php _e( 'Tables to Export', 'tablepress' ); ?>:</label> | |
| 108 | - <?php | |
| 109 | - if ( $data['zip_support_available'] ) { | |
| 110 | - echo '<br /><br /><label for="tables-export-select-all"><input type="checkbox" id="tables-export-select-all"> ' . __( 'Select all', 'tablepress' ) . '</label>'; | |
| 111 | - } | |
| 112 | - ?> | |
| 113 | - </th> | |
| 114 | - <td class="column-2"> | |
| 115 | - <input type="hidden" name="export[tables_list]" id="tables-export-list" value="" /> | |
| 116 | - <?php | |
| 117 | - $select_size = $data['tables_count'] + 1; // to show at least one empty row in the select | |
| 118 | - $select_size = max( $select_size, 3 ); | |
| 119 | - $select_size = min( $select_size, 12 ); | |
| 120 | - $size_multiple = ( $data['zip_support_available'] ) ? " size=\"{$select_size}\" multiple=\"multiple\"" : ''; | |
| 121 | - ?> | |
| 122 | - <select id="tables-export" name="export[tables][]"<?php echo $size_multiple; ?>> | |
| 123 | - <?php | |
| 124 | - foreach ( $data['table_ids'] as $table_id ) { | |
| 125 | - // Load table, without table data, options, and visibility settings. | |
| 126 | - $table = TablePress::$model_table->load( $table_id, false, false ); | |
| 127 | - if ( ! current_user_can( 'tablepress_export_table', $table['id'] ) ) { | |
| 128 | - continue; | |
| 129 | - } | |
| 130 | - if ( '' === trim( $table['name'] ) ) { | |
| 131 | - $table['name'] = __( '(no name)', 'tablepress' ); | |
| 132 | - } | |
| 133 | - $text = esc_html( sprintf( __( 'ID %1$s: %2$s', 'tablepress' ), $table['id'], $table['name'] ) ); | |
| 134 | - $selected = selected( true, in_array( $table['id'], $data['export_ids'], true ), false ); | |
| 135 | - echo "<option{$selected} value=\"{$table['id']}\">{$text}</option>"; | |
| 136 | - } | |
| 137 | - ?> | |
| 138 | - </select> | |
| 139 | - <?php | |
| 140 | - if ( $data['zip_support_available'] ) { | |
| 141 | - echo '<br /><span class="description">' . __( 'You can select multiple tables by holding down the “Ctrl” key (Windows) or the “Command” key (Mac).', 'tablepress' ) . '</span>'; | |
| 142 | - } | |
| 143 | - ?> | |
| 144 | - </td> | |
| 145 | - </tr> | |
| 146 | - <tr> | |
| 147 | - <th class="column-1" scope="row"><label for="tables-export-format"><?php _e( 'Export Format', 'tablepress' ); ?>:</label></th> | |
| 148 | - <td class="column-2"> | |
| 149 | - <select id="tables-export-format" name="export[format]"> | |
| 150 | - <?php | |
| 151 | - foreach ( $data['export_formats'] as $format => $name ) { | |
| 152 | - $selected = selected( $format, $data['export_format'], false ); | |
| 153 | - echo "<option{$selected} value=\"{$format}\">{$name}</option>"; | |
| 154 | - } | |
| 155 | - ?> | |
| 156 | - </select> | |
| 157 | - </td> | |
| 158 | - </tr> | |
| 159 | - <tr> | |
| 160 | - <th class="column-1" scope="row"><label for="tables-export-csv-delimiter"><?php _e( 'CSV Delimiter', 'tablepress' ); ?>:</label></th> | |
| 161 | - <td class="column-2"> | |
| 162 | - <select id="tables-export-csv-delimiter" name="export[csv_delimiter]"> | |
| 163 | - <?php | |
| 164 | - foreach ( $data['csv_delimiters'] as $delimiter => $name ) { | |
| 165 | - $selected = selected( $delimiter, $data['csv_delimiter'], false ); | |
| 166 | - echo "<option{$selected} value=\"{$delimiter}\">{$name}</option>"; | |
| 167 | - } | |
| 168 | - ?> | |
| 169 | - </select> <span id="tables-export-csv-delimiter-description" class="description hide-if-js"><?php _e( '(Only needed for CSV export.)', 'tablepress' ); ?></span> | |
| 170 | - </td> | |
| 171 | - </tr> | |
| 172 | - <tr> | |
| 173 | - <th class="column-1" scope="row"><?php _e( 'ZIP file', 'tablepress' ); ?>:</th> | |
| 174 | - <td class="column-2"> | |
| 175 | - <?php | |
| 176 | - if ( $data['zip_support_available'] ) { | |
| 177 | - ?> | |
| 178 | - <input type="checkbox" id="tables-export-zip-file" name="export[zip_file]" value="true" /> | |
| 179 | - <label for="tables-export-zip-file"><?php _e( 'Create a ZIP archive.', 'tablepress' ); ?> <span id="tables-export-zip-file-description" class="description hide-if-js"><?php _e( '(Mandatory if more than one table is selected.)', 'tablepress' ); ?></span></label> | |
| 180 | - <?php | |
| 181 | - } else { | |
| 182 | - _e( 'Note: Support for ZIP file creation seems not to be available on this server.', 'tablepress' ); | |
| 183 | - } | |
| 184 | - ?> | |
| 185 | - </td> | |
| 186 | - </tr> | |
| 187 | -</tbody> | |
| 188 | -</table> | |
| 189 | - <?php | |
| 114 | + public function postbox_export_form( array $data, array $box ): void { | |
| 115 | + $this->print_script_data_json( | |
| 116 | + 'export', | |
| 117 | + array( | |
| 118 | + 'tables' => $data['tables'], | |
| 119 | + 'exportFormats' => $data['export_formats'], | |
| 120 | + 'csvDelimiters' => $data['csv_delimiters'], | |
| 121 | + 'zipSupportAvailable' => $data['zip_support_available'], | |
| 122 | + 'selectedTables' => $data['export_ids'], | |
| 123 | + 'exportFormat' => $data['export_format'], | |
| 124 | + 'csvDelimiter' => $data['csv_delimiter'], | |
| 125 | + ), | |
| 126 | + ); | |
| 127 | + | |
| 128 | + echo '<div id="tablepress-export-screen"></div>'; | |
| 190 | 129 | } |
| 191 | 130 | |
| 192 | 131 | } // class TablePress_Export_View |