PluginProbe
TablePress – Tables in WordPress made easy / 3.4
TablePress – Tables in WordPress made easy v3.4
3.4 3.3.4 3.3.3 3.3.2 3.3.1 trunk 1.12 1.14 1.9.2 2.0.4 2.1.7 2.1.8 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3 2.3.1 2.3.2 2.4 2.4.1 2.4.2 2.4.3 All 45 releases
← All changes | views/view-editor_button_thickbox.php +54 -392 1.12 → 3.4 View file →
@@ -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 * Editor Button Thickbox List View class
18 + *
16 19 * @package TablePress
17 20 * @subpackage Views
18 21 * @author Tobias Bäthge
19 22 * @since 1.0.0
@@ -23,14 +26,13 @@
23 26 /**
24 27 * Object for the Editor Button Thickbox List Table.
25 28 *
26 29 * @since 1.0.0
27 - * @var TablePress_Editor_Button_Thickbox_List_Table
28 30 */
29 - protected $wp_list_table;
31 + protected \TablePress_Editor_Button_Thickbox_List_Table $wp_list_table;
30 32
31 33 /**
32 - * Initialize the View class.
34 + * Initializes the View class.
33 35 *
34 36 * @since 1.0.0
35 37 */
36 38 public function __construct() {
@@ -37,68 +39,58 @@
37 39 // Intentionally left empty, to void code from parent::__construct().
38 40 }
39 41
40 42 /**
41 - * Set up the view with data and do things that are specific for this view.
43 + * Sets up the view with data and do things that are specific for this view.
42 44 *
43 45 * @since 1.0.0
44 46 *
45 - * @param string $action Action for this view.
46 - * @param array $data Data for this view.
47 + * @param string $action Action for this view.
48 + * @param array<string, mixed> $data Data for this view.
47 49 */
48 - public function setup( $action, array $data ) {
50 + #[\Override]
51 + public function setup( /* string */ $action, array $data ) /* : void */ {
52 + // Don't use type hints in the method declaration to prevent PHP errors, as the method is inherited.
53 +
49 54 $this->action = $action;
50 55 $this->data = $data;
51 56
52 - $this->wp_list_table = new TablePress_Editor_Button_Thickbox_List_Table();
57 + $this->wp_list_table = TablePress::load_class( 'TablePress_Editor_Button_Thickbox_List_Table', 'class-editor-button-thickbox-list-table.php', 'views' );
58 +
53 59 $this->wp_list_table->set_items( $this->data['table_ids'] );
54 60 $this->wp_list_table->prepare_items();
55 61 }
56 62
57 63 /**
58 - * Render the current view.
64 + * Renders the current view.
59 65 *
60 66 * @since 1.0.0
61 67 */
62 - public function render() {
68 + #[\Override]
69 + public function render(): void {
63 70 _wp_admin_html_begin();
64 71
65 72 wp_print_styles( 'colors' );
66 - wp_print_scripts( 'jquery' );
67 -?>
68 -<title><?php printf( __( '%1$s &lsaquo; %2$s', 'tablepress' ), __( 'List of Tables', 'tablepress' ), 'TablePress' ); ?></title>
69 -<style type="text/css">
70 -/* Account for .wp-toolbar */
73 + wp_print_scripts( 'jquery-core' );
74 + ?>
75 +<title><?php /* translators: %1$s: Page title, %2$s: Plugin name */ printf( __( '%1$s &lsaquo; %2$s', 'tablepress' ), __( 'List of Tables', 'tablepress' ), 'TablePress' ); ?></title>
76 +<style>
77 +/* Account for .wp-toolbar. */
71 78 html {
72 79 padding-top: 0 !important;
73 80 }
74 -body {
75 - margin: 0 0 15px 15px;
81 +#tablepress-page {
82 + margin: 0 15px;
83 + padding: 0 0 15px 0;
76 84 }
77 -
78 85 #tablepress-page .subtitle {
79 86 float: left;
80 87 padding: 10px 0 0;
81 88 }
82 -
83 -/* Fix pagination layout */
84 -#tablepress-page .tablenav-pages {
85 - text-align: left;
89 +#tablepress-page p.search-box {
90 + position: relative;
86 91 }
87 -#tablepress-page .tablenav .tablenav-pages a {
88 - padding: 5px 12px;
89 - font-size: 16px;
90 -}
91 -#tablepress-page .tablenav-pages .pagination-links .paging-input {
92 - font-size: 16px;
93 -}
94 -
95 -#tablepress-page .tablenav-pages .pagination-links .current-page {
96 - padding: 4px;
97 - font-size: 16px;
98 -}
99 -
100 -/* Width and font weight for the columns */
92 +/* Width and font weight for the columns. */
101 93 .tablepress-editor-button-list thead .column-table_id {
102 94 width: 50px;
103 95 }
104 96 .tablepress-editor-button-list tbody .column-table_id,
@@ -107,9 +99,9 @@
107 99 }
108 100 .tablepress-editor-button-list thead .column-table_action {
109 101 min-width: 150px;
110 102 }
111 -/* Responsiveness on the All Tables screen */
103 +/* Responsiveness on the All Tables screen. */
112 104 @media screen and (max-width: 782px) {
113 105 .tablepress-editor-button-list .column-table_id {
114 106 display: none !important;
115 107 padding: 3px 8px 3px 35%;
@@ -114,383 +106,53 @@
114 106 display: none !important;
115 107 padding: 3px 8px 3px 35%;
116 108 }
117 109 }
118 -
119 -/* Shortcode input field */
120 -#tablepress-page .table-shortcode-inline {
121 - background: transparent;
122 - border: none;
123 - color: #333333;
124 - width: 120px;
125 - margin: 0;
126 - padding: 0;
127 - font-weight: bold;
128 - box-shadow: none;
129 - text-align: center;
130 -}
131 -<?php if ( is_rtl() ) : ?>
110 + <?php if ( is_rtl() ) : ?>
132 111 /* RTL CSS */
133 -body.rtl {
134 - margin: 0 15px 15px 0;
135 -}
136 112 .rtl #tablepress-page .subtitle {
137 113 float: right;
138 114 }
139 -.rtl #tablepress-page .table-shortcode-inline {
140 - width: 125px;
141 - font-size: 13px;
142 -}
143 115 <?php endif; ?>
144 116 </style>
145 117 </head>
146 -<body class="wp-admin wp-core-ui js iframe<?php echo is_rtl() ? ' rtl' : ''; ?>">
118 +<body class="wp-admin wp-core-ui js iframe <?php echo 'admin-color-' . sanitize_html_class( get_user_option( 'admin_color' ), 'modern' ) . ( is_rtl() ? ' rtl' : '' ); ?>">
147 119 <div id="tablepress-page" class="wrap">
148 -<h1><?php printf( __( '%1$s &lsaquo; %2$s', 'tablepress' ), __( 'List of Tables', 'tablepress' ), 'TablePress' ); ?></h1>
120 +<h1><?php /* translators: %1$s: Page title, %2$s: Plugin name */ printf( __( '%1$s &lsaquo; %2$s', 'tablepress' ), __( 'List of Tables', 'tablepress' ), 'TablePress' ); ?></h1>
149 121 <div id="poststuff">
150 -<p>
151 -<?php _e( 'This is a list of all available tables.', 'tablepress' ); ?> <?php _e( 'You may insert a table into a post or page here.', 'tablepress' ); ?>
152 -</p><p>
153 -<?php printf( __( 'Click the &#8220;%1$s&#8221; button for the desired table to automatically insert the<br />corresponding Shortcode (%2$s) into the editor.', 'tablepress' ), __( 'Insert Shortcode', 'tablepress' ), '<input type="text" class="table-shortcode table-shortcode-inline ltr" value="' . esc_attr( '[' . TablePress::$shortcode . ' id=<ID> /]' ) . '" readonly="readonly" />' ); ?>
154 -</p>
155 -<?php
156 - if ( ! empty( $_GET['s'] ) ) {
157 - printf( '<span class="subtitle">' . __( 'Search results for &#8220;%s&#8221;', 'tablepress' ) . '</span>', esc_html( wp_unslash( $_GET['s'] ) ) );
158 - }
159 -?>
160 -<form method="get" action="">
161 - <input type="hidden" name="action" value="tablepress_<?php echo $this->action; ?>" />
162 -<?php
163 - wp_nonce_field( TablePress::nonce( $this->action ), '_wpnonce', false );
164 - $this->wp_list_table->search_box( __( 'Search Tables', 'tablepress' ), 'tables_search' );
165 -?>
122 +<p><?php _e( 'This is a list of your tables.', 'tablepress' ); ?> <?php _e( 'You may insert a table into a post or page here.', 'tablepress' ); ?></p>
123 +<p><?php printf( __( 'Click the “%1$s” button for the desired table to automatically insert its Shortcode into the editor.', 'tablepress' ), __( 'Insert Shortcode', 'tablepress' ) ); ?></p>
124 + <?php
125 + if ( ! empty( $_GET['s'] ) ) {
126 + printf( '<span class="subtitle">' . __( 'Search results for &#8220;%s&#8221;', 'tablepress' ) . '</span>', esc_html( wp_unslash( $_GET['s'] ) ) );
127 + }
128 + ?>
129 +<form method="get">
130 + <input type="hidden" name="action" value="tablepress_<?php echo $this->action; ?>">
131 + <?php
132 + wp_nonce_field( TablePress::nonce( $this->action ), '_wpnonce', false );
133 + $this->wp_list_table->search_box( __( 'Search Tables', 'tablepress' ), 'tables_search' );
134 + ?>
166 135 </form>
167 - <?php $this->wp_list_table->display(); ?>
136 + <?php $this->wp_list_table->display(); ?>
168 137 </div>
169 138 </div>
170 -<script type="text/javascript">
171 -jQuery( document ).ready( function( $ ) {
172 - // Toggle list table rows on small screens
139 +<script>
140 +jQuery( function( $ ) {
141 + // Toggle list table rows on small screens.
173 142 $( '.tablepress-editor-button-list' )
174 - .on( 'click', '.toggle-row', function() {
175 - $( this ).closest( 'tr' ).toggleClass( 'is-expanded' );
143 + .on( 'click', 'tr', function() {
144 + this.classList.toggle( 'is-expanded' );
176 145 })
177 146 .on( 'click', '.insert-shortcode', function() {
178 - var win = window.dialogArguments || opener || parent || top;
179 - win.send_to_editor( $(this).attr( 'title' ) );
147 + const win = window.dialogArguments || opener || parent || top;
148 + win.send_to_editor( this.title );
180 149 } );
181 150 } );
182 151 </script>
183 152 </body>
184 153 </html>
185 -<?php
154 + <?php
186 155 }
187 156
188 157 } // class TablePress_Editor_Button_View
189 158
190 -/**
191 - * TablePress Editor Button Thickbox List Table Class
192 - * @package TablePress
193 - * @subpackage Views
194 - * @author Tobias Bäthge
195 - * @link https://codex.wordpress.org/Class_Reference/WP_List_Table
196 - * @since 1.0.0
197 - */
198 -class TablePress_Editor_Button_Thickbox_List_Table extends WP_List_Table {
199 -
200 - /**
201 - * Number of items of the initial data set (before sort, search, and pagination).
202 - *
203 - * @since 1.0.0
204 - * @var int
205 - */
206 - protected $items_count = 0;
207 -
208 - /**
209 - * Initialize the List Table.
210 - *
211 - * @since 1.0.0
212 - */
213 - public function __construct() {
214 - parent::__construct( array(
215 - 'singular' => 'tablepress-table', // Singular name of the listed records.
216 - 'plural' => 'tablepress-editor-button-list', // Plural name of the listed records.
217 - 'ajax' => false, // Does this list table support AJAX?
218 - 'screen' => get_current_screen(), // WP_Screen object.
219 - ) );
220 - }
221 -
222 - /**
223 - * Set the data items (here: tables) that are to be displayed by the List Tables, and their original count.
224 - *
225 - * @since 1.0.0
226 - *
227 - * @param array $items Tables to be displayed in the List Table.
228 - */
229 - public function set_items( array $items ) {
230 - $this->items = $items;
231 - $this->items_count = count( $items );
232 - }
233 -
234 - /**
235 - * Check whether the user has permissions for certain AJAX actions.
236 - * (not used, but must be implemented in this child class)
237 - *
238 - * @since 1.0.0
239 - *
240 - * @return bool true (Default value).
241 - */
242 - public function ajax_user_can() {
243 - return true;
244 - }
245 -
246 - /**
247 - * Get a list of columns in this List Table.
248 - *
249 - * Format: 'internal-name' => 'Column Title'.
250 - *
251 - * @since 1.0.0
252 - *
253 - * @return array List of columns in this List Table.
254 - */
255 - public function get_columns() {
256 - $columns = array(
257 - // "name" is special in WP, which is why we prefix every entry here, to be safe!
258 - 'table_id' => __( 'ID', 'tablepress' ),
259 - 'table_name' => __( 'Table Name', 'tablepress' ),
260 - 'table_description' => __( 'Description', 'tablepress' ),
261 - 'table_action' => __( 'Action', 'tablepress' ),
262 - );
263 - return $columns;
264 - }
265 -
266 - /**
267 - * Get a list of columns that are sortable.
268 - *
269 - * Format: 'internal-name' => array( $field for $item[ $field ], true for already sorted ).
270 - *
271 - * @since 1.0.0
272 - *
273 - * @return array List of sortable columns in this List Table.
274 - */
275 - protected function get_sortable_columns() {
276 - // No sorting on the Empty List placeholder.
277 - if ( ! $this->has_items() ) {
278 - return array();
279 - }
280 -
281 - $sortable_columns = array(
282 - 'table_id' => array( 'id', true ), // true means its already sorted
283 - 'table_name' => array( 'name', false ),
284 - 'table_description' => array( 'description', false ),
285 - );
286 - return $sortable_columns;
287 - }
288 -
289 - /**
290 - * Gets the name of the default primary column.
291 - *
292 - * @since 1.7.0
293 - *
294 - * @return string Name of the default primary column, in this case, the table name.
295 - */
296 - protected function get_default_primary_column_name() {
297 - return 'table_name';
298 - }
299 -
300 - /**
301 - * Render a cell in the "table_id" column.
302 - *
303 - * @since 1.0.0
304 - *
305 - * @param array $item Data item for the current row.
306 - * @return string HTML content of the cell.
307 - */
308 - protected function column_table_id( array $item ) {
309 - return esc_html( $item['id'] );
310 - }
311 -
312 - /**
313 - * Render a cell in the "table_name" column.
314 - *
315 - * @since 1.0.0
316 - *
317 - * @param array $item Data item for the current row.
318 - * @return string HTML content of the cell.
319 - */
320 - protected function column_table_name( array $item ) {
321 - if ( '' === trim( $item['name'] ) ) {
322 - $item['name'] = __( '(no name)', 'tablepress' );
323 - }
324 - return esc_html( $item['name'] );
325 - }
326 -
327 - /**
328 - * Render a cell in the "table_description" column.
329 - *
330 - * @since 1.0.0
331 - *
332 - * @param array $item Data item for the current row.
333 - * @return string HTML content of the cell.
334 - */
335 - protected function column_table_description( array $item ) {
336 - if ( '' === trim( $item['description'] ) ) {
337 - $item['description'] = __( '(no description)', 'tablepress' );
338 - }
339 - return esc_html( $item['description'] );
340 - }
341 -
342 - /**
343 - * Render a cell in the "table_action" column, i.e. the "Insert" link.
344 - *
345 - * @since 1.0.0
346 - *
347 - * @param array $item Data item for the current row.
348 - * @return string HTML content of the cell.
349 - */
350 - protected function column_table_action( array $item ) {
351 - return '<input type="button" class="insert-shortcode button" title="' . esc_attr( '[' . TablePress::$shortcode . " id={$item['id']} /]" ) . '" value="' . esc_attr__( 'Insert Shortcode', 'tablepress' ) . '" />';
352 - }
353 -
354 - /**
355 - * Holds the message to be displayed when there are no items in the table.
356 - *
357 - * @since 1.0.0
358 - */
359 - public function no_items() {
360 - _e( 'No tables found.', 'tablepress' );
361 - if ( 0 === $this->items_count ) {
362 - echo ' ' . __( 'You should add or import a table on the TablePress screens to get started!', 'tablepress' );
363 - }
364 - }
365 -
366 - /**
367 - * Generate the elements above or below the table (like bulk actions and pagination).
368 - *
369 - * In comparison with parent class, this has modified HTML (no nonce field), and a check whether there are items.
370 - *
371 - * @since 1.0.0
372 - *
373 - * @param string $which Location ("top" or "bottom").
374 - */
375 - protected function display_tablenav( $which ) {
376 - if ( ! $this->has_items() ) {
377 - return;
378 - }
379 - ?>
380 - <div class="tablenav <?php echo esc_attr( $which ); ?>">
381 - <div class="alignleft actions">
382 - <?php $this->bulk_actions( $which ); ?>
383 - </div>
384 - <?php
385 - $this->extra_tablenav( $which );
386 - $this->pagination( $which );
387 - ?>
388 - <br class="clear" />
389 - </div>
390 - <?php
391 - }
392 -
393 - /**
394 - * Callback to determine whether the given $item contains the search term.
395 - *
396 - * @since 1.0.0
397 - *
398 - * @param string $item Table ID that shall be searched.
399 - * @return bool Whether the search term was found or not.
400 - */
401 - protected function _search_callback( $item ) {
402 - static $term, $json_encoded_term;
403 - if ( is_null( $term ) || is_null( $json_encoded_term ) ) {
404 - $term = wp_unslash( $_GET['s'] );
405 - $json_encoded_term = substr( wp_json_encode( $term, TABLEPRESS_JSON_OPTIONS ), 1, -1 );
406 - }
407 -
408 - // Load table again, with table data, but without options and visibility settings.
409 - $item = TablePress::$model_table->load( $item, true, false );
410 -
411 - if ( isset( $item['is_corrupted'] ) && $item['is_corrupted'] ) {
412 - return false; // Don't search corrupted tables
413 - }
414 -
415 - // Search from easy to hard, so that "expensive" code maybe doesn't have to run.
416 - if ( false !== stripos( $item['id'], $term )
417 - || false !== stripos( $item['name'], $term )
418 - || false !== stripos( $item['description'], $term )
419 - || false !== stripos( TablePress::get_user_display_name( $item['author'] ), $term )
420 - || false !== stripos( TablePress::format_datetime( $item['last_modified'], 'mysql', ' ' ), $term )
421 - || false !== stripos( wp_json_encode( $item['data'], TABLEPRESS_JSON_OPTIONS ), $json_encoded_term ) ) {
422 - return true;
423 - }
424 -
425 - return false;
426 - }
427 -
428 - /**
429 - * Callback to for the array sort function.
430 - *
431 - * @since 1.0.0
432 - *
433 - * @param array $item_a First item that shall be compared to.
434 - * @param array $item_b The second item for the comparison.
435 - * @return int (-1, 0, 1) depending on which item sorts "higher".
436 - */
437 - protected function _order_callback( array $item_a, array $item_b ) {
438 - global $orderby, $order;
439 -
440 - if ( $item_a[ $orderby ] === $item_b[ $orderby ] ) {
441 - return 0;
442 - }
443 -
444 - // Fields in this list table are all strings.
445 - $result = strnatcasecmp( $item_a[ $orderby ], $item_b[ $orderby ] );
446 -
447 - return ( 'asc' === $order ) ? $result : - $result;
448 - }
449 -
450 - /**
451 - * Prepares the list of items for displaying, by maybe searching and sorting, and by doing pagination.
452 - *
453 - * @since 1.0.0
454 - */
455 - public function prepare_items() {
456 - global $orderby, $order, $s;
457 - wp_reset_vars( array( 'orderby', 'order', 's' ) );
458 -
459 - // Maybe search in the items.
460 - if ( $s ) {
461 - $this->items = array_filter( $this->items, array( $this, '_search_callback' ) );
462 - }
463 -
464 - // Load actual tables after search for less memory consumption.
465 - foreach ( $this->items as &$item ) {
466 - // Don't load data nor table options.
467 - $item = TablePress::$model_table->load( $item, false, false );
468 - }
469 - // Break reference in foreach iterator.
470 - unset( $item );
471 -
472 - // Maybe sort the items.
473 - $_sortable_columns = $this->get_sortable_columns();
474 - if ( $orderby && ! empty( $this->items ) && isset( $_sortable_columns[ "table_{$orderby}" ] ) ) {
475 - usort( $this->items, array( $this, '_order_callback' ) );
476 - }
477 -
478 - // Number of records to show per page.
479 - $per_page = 15; // hard-coded, as there's no possibility to change this in the Thickbox
480 - // Page number the user is currently viewing.
481 - $current_page = $this->get_pagenum();
482 - // Number of records in the array.
483 - $total_items = count( $this->items );
484 -
485 - // Slice items array to hold only items for the current page.
486 - $this->items = array_slice( $this->items, ( ( $current_page - 1 ) * $per_page ), $per_page );
487 -
488 - // Register pagination options and calculation results.
489 - $this->set_pagination_args( array(
490 - 'total_items' => $total_items, // Total number of records/items
491 - 'per_page' => $per_page, // Number of items per page
492 - 'total_pages' => ceil( $total_items / $per_page ), // Total number of pages
493 - ) );
494 - }
495 -
496 -} // class TablePress_Editor_Button_Thickbox_List_Table