PluginProbe
TablePress – Tables in WordPress made easy / 3.3.3
TablePress – Tables in WordPress made easy v3.3.3
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
tablepress / views / class-editor-button-thickbox-list-table.php

class-editor-button-thickbox-list-table.php in TablePress – Tables in WordPress made easy 3.3.3, at views/class-editor-button-thickbox-list-table.php

360 lines 11.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Editor Button Thickbox List Table
4 *
5 * @package TablePress
6 * @subpackage Views
7 * @author Tobias Bäthge
8 * @since 2.0.0
9 */
10
11 // Prohibit direct script loading.
12 defined( 'ABSPATH' ) || die( 'No direct script access allowed!' );
13
14 /**
15 * TablePress Editor Button Thickbox List Table Class
16 *
17 * @package TablePress
18 * @subpackage Views
19 * @author Tobias Bäthge
20 * @since 1.0.0
21 */
22 class TablePress_Editor_Button_Thickbox_List_Table extends WP_List_Table {
23
24 /**
25 * Number of items of the initial data set (before sort, search, and pagination).
26 *
27 * @since 1.0.0
28 */
29 protected int $items_count = 0;
30
31 /**
32 * Initializes the List Table.
33 *
34 * @since 1.0.0
35 */
36 public function __construct() {
37 // @phpstan-ignore argument.type (WordPress Core's docblocks state wrong argument types in some places.)
38 parent::__construct( array(
39 'singular' => 'tablepress-table', // Singular name of the listed records.
40 'plural' => 'tablepress-editor-button-list', // Plural name of the listed records.
41 'ajax' => false, // Does this list table support AJAX?
42 'screen' => get_current_screen(), // WP_Screen object.
43 ) );
44 }
45
46 /**
47 * Sets the data items (here: tables) that are to be displayed by the List Tables, and their original count.
48 *
49 * @since 1.0.0
50 *
51 * @param string[] $items Tables to be displayed in the List Table.
52 */
53 public function set_items( array $items ): void {
54 $this->items = $items;
55 $this->items_count = count( $items );
56 }
57
58 /**
59 * Checks whether the user has permissions for certain AJAX actions.
60 * (not used, but must be implemented in this child class)
61 *
62 * @since 1.0.0
63 *
64 * @return bool true (Default value).
65 */
66 #[\Override]
67 public function ajax_user_can(): bool {
68 return true;
69 }
70
71 /**
72 * Gets a list of columns in this List Table.
73 *
74 * Format: 'internal-name' => 'Column Title'.
75 *
76 * @since 1.0.0
77 *
78 * @return array<string, string> List of columns in this List Table.
79 */
80 #[\Override]
81 public function get_columns(): array {
82 $columns = array(
83 // "name" is special in WP, which is why we prefix every entry here, to be safe!
84 'table_id' => __( 'ID', 'tablepress' ),
85 'table_name' => __( 'Table Name', 'tablepress' ),
86 'table_description' => __( 'Description', 'tablepress' ),
87 'table_action' => __( 'Action', 'tablepress' ),
88 );
89 return $columns;
90 }
91
92 /**
93 * Gets a list of columns that are sortable.
94 *
95 * Format: 'internal-name' => array( $field for $item[ $field ], true for already sorted ).
96 *
97 * @since 1.0.0
98 *
99 * @return array<string, array{string, bool}> List of sortable columns in this List Table.
100 */
101 #[\Override]
102 protected function get_sortable_columns(): array {
103 // No sorting on the Empty List placeholder.
104 if ( ! $this->has_items() ) {
105 return array();
106 }
107
108 $sortable_columns = array(
109 'table_id' => array( 'id', true ), // true means its already sorted.
110 'table_name' => array( 'name', false ),
111 'table_description' => array( 'description', false ),
112 );
113 return $sortable_columns;
114 }
115
116 /**
117 * Gets the name of the default primary column.
118 *
119 * @since 1.7.0
120 *
121 * @return string Name of the default primary column, in this case, the table name.
122 */
123 #[\Override]
124 protected function get_default_primary_column_name(): string {
125 return 'table_name';
126 }
127
128 /**
129 * Renders a cell in the "table_id" column.
130 *
131 * @since 1.0.0
132 *
133 * @param array<string, mixed> $item Data item for the current row.
134 * @return string HTML content of the cell.
135 */
136 protected function column_table_id( array $item ): string {
137 return esc_html( $item['id'] );
138 }
139
140 /**
141 * Renders a cell in the "table_name" column.
142 *
143 * @since 1.0.0
144 *
145 * @param array<string, mixed> $item Data item for the current row.
146 * @return string HTML content of the cell.
147 */
148 protected function column_table_name( array $item ): string {
149 if ( '' === trim( $item['name'] ) ) {
150 $item['name'] = __( '(no name)', 'tablepress' );
151 }
152 return esc_html( $item['name'] );
153 }
154
155 /**
156 * Renders a cell in the "table_description" column.
157 *
158 * @since 1.0.0
159 *
160 * @param array<string, mixed> $item Data item for the current row.
161 * @return string HTML content of the cell.
162 */
163 protected function column_table_description( array $item ): string {
164 if ( '' === trim( $item['description'] ) ) {
165 $item['description'] = __( '(no description)', 'tablepress' );
166 }
167 return esc_html( $item['description'] );
168 }
169
170 /**
171 * Renders a cell in the "table_action" column, i.e. the "Insert" link.
172 *
173 * @since 1.0.0
174 *
175 * @param array<string, mixed> $item Data item for the current row.
176 * @return string HTML content of the cell.
177 */
178 protected function column_table_action( array $item ): string {
179 return '<input type="button" class="insert-shortcode button button-compact" title="' . esc_attr( '[' . TablePress::$shortcode . " id={$item['id']} /]" ) . '" value="' . esc_attr__( 'Insert Shortcode', 'tablepress' ) . '">';
180 }
181
182 /**
183 * Holds the message to be displayed when there are no items in the table.
184 *
185 * @since 1.0.0
186 */
187 #[\Override]
188 public function no_items(): void {
189 _e( 'No tables found.', 'tablepress' );
190 if ( 0 === $this->items_count ) {
191 echo ' ' . __( 'You should add or import a table on the TablePress screens to get started!', 'tablepress' );
192 }
193 }
194
195 /**
196 * Generates the elements above or below the table (like bulk actions and pagination).
197 *
198 * In comparison with parent class, this has modified HTML (no nonce field), and a check whether there are items.
199 *
200 * @since 1.0.0
201 *
202 * @param 'top'|'bottom' $which Location ("top" or "bottom").
203 */
204 #[\Override]
205 protected function display_tablenav( /* string */ $which ): void {
206 // Don't use type hints in the method declaration to prevent PHP errors, as the method is inherited.
207
208 if ( ! $this->has_items() ) {
209 return;
210 }
211 ?>
212 <div class="tablenav <?php echo esc_attr( $which ); ?>">
213 <div class="alignleft actions">
214 <?php $this->bulk_actions( $which ); ?>
215 </div>
216 <?php
217 $this->extra_tablenav( $which );
218
219 add_filter( 'ngettext_default', array( $this, 'change_pagination_items_string' ), 10, 5 );
220 $this->pagination( $which );
221 remove_filter( 'ngettext_default', array( $this, 'change_pagination_items_string' ), 10 );
222 ?>
223 <br class="clear">
224 </div>
225 <?php
226 }
227
228 /**
229 * Replaces the "%s item/%s items" string in the pagination with "%s table/%s tables".
230 *
231 * @since 2.0.0
232 *
233 * @param string $translation The current translation of a singular or plural form.
234 * @param string $single The text to be used if the number is singular.
235 * @param string $plural The text to be used if the number is plural.
236 * @param int $number The number to compare against to use either the singular or plural form.
237 * @param string $domain Text domain. Defaults to 'default'.
238 * @return string The changed translation.
239 */
240 public function change_pagination_items_string( string $translation, string $single, string $plural, int $number, string $domain ): string {
241 if ( '%s item' === $single && '%s items' === $plural ) {
242 /* translators: %s: Number of tables */
243 $translation = _n( '%s table', '%s tables', $number, 'tablepress' );
244 }
245 return $translation;
246 }
247
248 /**
249 * Callback to determine whether the given $item contains the search term.
250 *
251 * @since 1.0.0
252 *
253 * @param string $item Table ID that shall be searched.
254 * @return bool Whether the search term was found or not.
255 */
256 protected function _search_callback( string $item ): bool {
257 static $term;
258 static $json_encoded_term;
259 if ( is_null( $term ) || is_null( $json_encoded_term ) ) {
260 $term = wp_unslash( $_GET['s'] );
261 $json_encoded_term = substr( wp_json_encode( $term, TABLEPRESS_JSON_OPTIONS ), 1, -1 ); // @phpstan-ignore argument.type
262 }
263
264 // Load table again, with table data, but without options and visibility settings.
265 $item = TablePress::$model_table->load( $item, true, false );
266
267 if ( is_wp_error( $item ) ) {
268 return false;
269 }
270
271 // Don't search corrupted tables.
272 if ( isset( $item['is_corrupted'] ) && $item['is_corrupted'] ) {
273 return false;
274 }
275
276 $fn_stripos = function_exists( 'mb_stripos' ) ? 'mb_stripos' : 'stripos';
277
278 // Search from easy to hard, so that "expensive" code maybe doesn't have to run.
279 if ( false !== $fn_stripos( $item['id'], (string) $term )
280 || false !== $fn_stripos( $item['name'], (string) $term )
281 || false !== $fn_stripos( $item['description'], (string) $term )
282 || false !== $fn_stripos( TablePress::get_user_display_name( $item['author'] ), (string) $term )
283 || false !== $fn_stripos( TablePress::format_datetime( $item['last_modified'] ), (string) $term )
284 || false !== $fn_stripos( wp_json_encode( $item['data'], TABLEPRESS_JSON_OPTIONS ), (string) $json_encoded_term ) ) { // @phpstan-ignore argument.type
285 return true;
286 }
287
288 return false;
289 }
290
291 /**
292 * Callback to for the array sort function.
293 *
294 * @since 1.0.0
295 *
296 * @param array<string, mixed> $item_a First item that shall be compared to.
297 * @param array<string, mixed> $item_b The second item for the comparison.
298 * @return int (-1, 0, 1) depending on which item sorts "higher".
299 */
300 protected function _order_callback( array $item_a, array $item_b ): int {
301 global $orderby, $order;
302
303 if ( $item_a[ $orderby ] === $item_b[ $orderby ] ) {
304 return 0;
305 }
306
307 // Fields in this list table are all strings.
308 $result = strnatcasecmp( $item_a[ $orderby ], $item_b[ $orderby ] );
309
310 return ( 'asc' === $order ) ? $result : - $result;
311 }
312
313 /**
314 * Prepares the list of items for displaying, by maybe searching and sorting, and by doing pagination.
315 *
316 * @since 1.0.0
317 */
318 #[\Override]
319 public function prepare_items(): void {
320 global $orderby, $order, $s;
321 wp_reset_vars( array( 'orderby', 'order', 's' ) );
322
323 // Maybe search in the items.
324 if ( $s ) {
325 $this->items = array_filter( $this->items, array( $this, '_search_callback' ) );
326 }
327
328 // Load actual tables after search for less memory consumption.
329 foreach ( $this->items as &$item ) {
330 // Don't load data nor table options.
331 $item = TablePress::$model_table->load( $item, false, false );
332 }
333 unset( $item ); // Unset use-by-reference parameter of foreach loop.
334
335 // Maybe sort the items.
336 $_sortable_columns = $this->get_sortable_columns();
337 if ( $orderby && ! empty( $this->items ) && isset( $_sortable_columns[ "table_{$orderby}" ] ) ) {
338 usort( $this->items, array( $this, '_order_callback' ) );
339 }
340
341 // Number of records to show per page.
342 $per_page = 20; // Hard-coded, as there's no possibility to change this in the Thickbox.
343 // Page number the user is currently viewing.
344 $current_page = $this->get_pagenum();
345 // Number of records in the array.
346 $total_items = count( $this->items );
347
348 // Slice items array to hold only items for the current page.
349 $this->items = array_slice( $this->items, ( ( $current_page - 1 ) * $per_page ), $per_page );
350
351 // Register pagination options and calculation results.
352 $this->set_pagination_args( array(
353 'total_items' => $total_items, // Total number of records/items.
354 'per_page' => $per_page, // Number of items per page.
355 'total_pages' => (int) ceil( $total_items / $per_page ), // Total number of pages.
356 ) );
357 }
358
359 } // class TablePress_Editor_Button_Thickbox_List_Table
360