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-all-tables-list-table.php

class-all-tables-list-table.php in TablePress – Tables in WordPress made easy 3.3.3, at views/class-all-tables-list-table.php

643 lines 23.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * List Tables View 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 All Tables List Table class
16 *
17 * @package TablePress
18 * @subpackage Views
19 * @author Tobias Bäthge
20 * @since 1.0.0
21 */
22 class TablePress_All_Tables_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 * Cached bulk actions.
33 *
34 * This property has to be declared here, as it's `private` in the parent class
35 * and thus can't be inherited.
36 *
37 * @since 1.0.0
38 * @var array<string, string>
39 */
40 protected array $_actions; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore
41
42 /**
43 * Initialize the List Table.
44 *
45 * @since 1.0.0
46 */
47 public function __construct() {
48 $screen = get_current_screen();
49
50 // Hide "Last Modified By" column by default.
51 if ( false === get_user_option( "manage{$screen->id}columnshidden" ) ) { // @phpstan-ignore property.nonObject
52 update_user_option( get_current_user_id(), "manage{$screen->id}columnshidden", array( 'table_last_modified_by' ), true ); // @phpstan-ignore property.nonObject
53 }
54
55 // @phpstan-ignore argument.type (WordPress Core's docblocks state wrong argument types in some places.)
56 parent::__construct( array(
57 'singular' => 'tablepress-table', // Singular name of the listed records.
58 'plural' => 'tablepress-all-tables', // Plural name of the listed records.
59 'ajax' => false, // Does this list table support AJAX?
60 'screen' => $screen, // WP_Screen object.
61 ) );
62 }
63
64 /**
65 * Set the data items (here: tables) that are to be displayed by the List Tables, and their original count.
66 *
67 * @since 1.0.0
68 *
69 * @param string[] $items Tables to be displayed in the List Table.
70 */
71 public function set_items( array $items ): void {
72 $this->items = $items;
73 $this->items_count = count( $items );
74 }
75
76 /**
77 * Check whether the user has permissions for certain AJAX actions.
78 * (not used, but must be implemented in this child class)
79 *
80 * @since 1.0.0
81 *
82 * @return bool true (Default value).
83 */
84 #[\Override]
85 public function ajax_user_can(): bool {
86 return true;
87 }
88
89 /**
90 * Get a list of columns in this List Table.
91 *
92 * Format: 'internal-name' => 'Column Title'.
93 *
94 * @since 1.0.0
95 *
96 * @return array<string, string> List of columns in this List Table.
97 */
98 #[\Override]
99 public function get_columns(): array {
100 $columns = array(
101 'cb' => $this->has_items() ? '<input type="checkbox">' : '', // Checkbox for "Select all", but only if there are items in the table.
102 // "name" is special in WP, which is why we prefix every entry here, to be safe!
103 'table_id' => __( 'ID', 'tablepress' ),
104 'table_name' => __( 'Table Name', 'tablepress' ),
105 'table_description' => __( 'Description', 'tablepress' ),
106 'table_author' => __( 'Author', 'tablepress' ),
107 'table_last_modified_by' => __( 'Last Modified By', 'tablepress' ),
108 'table_last_modified' => __( 'Last Modified', 'tablepress' ),
109 );
110 return $columns;
111 }
112
113 /**
114 * Get a list of columns that are sortable.
115 *
116 * Format: 'internal-name' => array( $field for $item[ $field ], true for already sorted ).
117 *
118 * @since 1.0.0
119 *
120 * @return array<string, array{string, bool}> List of sortable columns in this List Table.
121 */
122 #[\Override]
123 protected function get_sortable_columns(): array {
124 // No sorting on the Empty List placeholder.
125 if ( ! $this->has_items() ) {
126 return array();
127 }
128
129 $sortable_columns = array(
130 'table_id' => array( 'id', true ), // true means its already sorted.
131 'table_name' => array( 'name', false ),
132 'table_description' => array( 'description', false ),
133 'table_author' => array( 'author', false ),
134 'table_last_modified_by' => array( 'last_modified_by', false ),
135 'table_last_modified' => array( 'last_modified', false ),
136 );
137 return $sortable_columns;
138 }
139
140 /**
141 * Gets the name of the default primary column.
142 *
143 * @since 1.7.0
144 *
145 * @return string Name of the default primary column, in this case, the table name.
146 */
147 #[\Override]
148 protected function get_default_primary_column_name(): string {
149 return 'table_name';
150 }
151
152 /**
153 * Render a cell in the "cb" column.
154 *
155 * @since 1.0.0
156 *
157 * @param array<string, mixed> $item Data item for the current row.
158 * @return string HTML content of the cell.
159 */
160 #[\Override]
161 protected function column_cb( /* array */ $item ): string {
162 // Don't use type hints in the method declaration to prevent PHP errors, as the method is inherited.
163
164 $user_can_copy_table = current_user_can( 'tablepress_copy_table', $item['id'] );
165 $user_can_delete_table = current_user_can( 'tablepress_delete_table', $item['id'] );
166 $user_can_export_table = current_user_can( 'tablepress_export_table', $item['id'] );
167
168 if ( ! ( $user_can_copy_table || $user_can_delete_table || $user_can_export_table ) ) {
169 return '';
170 }
171
172 if ( '' === trim( $item['name'] ) ) {
173 $item['name'] = __( '(no name)', 'tablepress' );
174 }
175
176 return sprintf(
177 '<input type="checkbox" id="cb-select-%1$s" name="table[]" value="%1$s"><label for="cb-select-%1$s"><span class="screen-reader-text">%2$s</span></label>',
178 esc_attr( $item['id'] ),
179 esc_html( sprintf( __( 'Select table “%s”', 'tablepress' ), $item['name'] ) ),
180 );
181 }
182
183 /**
184 * Render a cell in the "table_id" column.
185 *
186 * @since 1.0.0
187 *
188 * @param array<string, mixed> $item Data item for the current row.
189 * @return string HTML content of the cell.
190 */
191 protected function column_table_id( array $item ): string {
192 return esc_html( $item['id'] );
193 }
194
195 /**
196 * Render a cell in the "table_name" column.
197 *
198 * @since 1.0.0
199 *
200 * @param array<string, mixed> $item Data item for the current row.
201 * @return string HTML content of the cell.
202 */
203 protected function column_table_name( array $item ): string {
204 $user_can_edit_table = current_user_can( 'tablepress_edit_table', $item['id'] );
205 $user_can_copy_table = current_user_can( 'tablepress_copy_table', $item['id'] );
206 $user_can_export_table = current_user_can( 'tablepress_export_table', $item['id'] );
207 $user_can_delete_table = current_user_can( 'tablepress_delete_table', $item['id'] );
208 $user_can_preview_table = current_user_can( 'tablepress_preview_table', $item['id'] );
209
210 $edit_url = TablePress::url( array( 'action' => 'edit', 'table_id' => $item['id'] ) );
211 $copy_url = TablePress::url( array( 'action' => 'copy_table', 'item' => $item['id'], 'return' => 'list', 'return_item' => $item['id'] ), true, 'admin-post.php' );
212 $export_url = TablePress::url( array( 'action' => 'export', 'table_id' => $item['id'] ) );
213 $delete_url = TablePress::url( array( 'action' => 'delete_table', 'item' => $item['id'], 'return' => 'list', 'return_item' => $item['id'] ), true, 'admin-post.php' );
214 $preview_url = TablePress::url( array( 'action' => 'preview_table', 'item' => $item['id'], 'return' => 'list', 'return_item' => $item['id'] ), true, 'admin-post.php' );
215
216 if ( '' === trim( $item['name'] ) ) {
217 $item['name'] = __( '(no name)', 'tablepress' );
218 }
219
220 if ( $user_can_edit_table ) {
221 $row_text = '<strong><a title="' . esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;', 'tablepress' ), esc_attr( $item['name'] ) ) ) . '" class="row-title" href="' . $edit_url . '">' . esc_html( $item['name'] ) . '</a></strong>';
222 } else {
223 $row_text = '<strong>' . esc_html( $item['name'] ) . '</strong>';
224 }
225
226 $row_actions = array();
227 if ( $user_can_edit_table ) {
228 $row_actions['edit'] = sprintf( '<a href="%1$s" title="%2$s">%3$s</a>', $edit_url, esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;', 'tablepress' ), $item['name'] ) ), __( 'Edit', 'tablepress' ) );
229 }
230 $row_actions['shortcode hide-if-no-js'] = sprintf( '<a href="%1$s" title="%2$s">%3$s</a>', '#', esc_attr( '[' . TablePress::$shortcode . " id={$item['id']} /]" ), __( 'Show Shortcode', 'tablepress' ) );
231 if ( $user_can_copy_table ) {
232 $row_actions['copy'] = sprintf( '<a href="%1$s" title="%2$s">%3$s</a>', $copy_url, esc_attr( sprintf( __( 'Copy &#8220;%s&#8221;', 'tablepress' ), $item['name'] ) ), __( 'Copy', 'tablepress' ) );
233 }
234 if ( $user_can_export_table ) {
235 $row_actions['export'] = sprintf( '<a href="%1$s" title="%2$s">%3$s</a>', $export_url, esc_attr( sprintf( __( 'Export &#8220;%s&#8221;', 'tablepress' ), $item['name'] ) ), _x( 'Export', 'row action', 'tablepress' ) );
236 }
237 if ( $user_can_delete_table ) {
238 $row_actions['delete'] = sprintf( '<a href="%1$s" title="%2$s">%3$s</a>', $delete_url, esc_attr( sprintf( __( 'Delete “%1$s” (ID %2$s)', 'tablepress' ), $item['name'], $item['id'] ) ), __( 'Delete', 'tablepress' ) );
239 }
240 if ( $user_can_preview_table ) {
241 $row_actions['table-preview'] = sprintf( '<a href="%1$s" title="%2$s">%3$s</a>', $preview_url, esc_attr( sprintf( __( 'Preview of table “%1$s” (ID %2$s)', 'tablepress' ), $item['name'], $item['id'] ) ), __( 'Preview', 'tablepress' ) );
242 }
243
244 return $row_text . $this->row_actions( $row_actions );
245 }
246
247 /**
248 * Render a cell in the "table_description" column.
249 *
250 * @since 1.0.0
251 *
252 * @param array<string, mixed> $item Data item for the current row.
253 * @return string HTML content of the cell.
254 */
255 protected function column_table_description( array $item ): string {
256 if ( '' === trim( $item['description'] ) ) {
257 $item['description'] = __( '(no description)', 'tablepress' );
258 }
259 return esc_html( $item['description'] );
260 }
261
262 /**
263 * Render a cell in the "table_author" column.
264 *
265 * @since 1.0.0
266 *
267 * @param array<string, mixed> $item Data item for the current row.
268 * @return string HTML content of the cell.
269 */
270 protected function column_table_author( array $item ): string {
271 return TablePress::get_user_display_name( $item['author'] );
272 }
273
274 /**
275 * Render a cell in the "last_modified_by" column.
276 *
277 * @since 1.0.0
278 *
279 * @param array<string, mixed> $item Data item for the current row.
280 * @return string HTML content of the cell.
281 */
282 protected function column_table_last_modified_by( array $item ): string {
283 return TablePress::get_user_display_name( $item['options']['last_editor'] );
284 }
285
286 /**
287 * Render a cell in the "table_last_modified" column.
288 *
289 * @since 1.0.0
290 *
291 * @param array<string, mixed> $item Data item for the current row.
292 * @return string HTML content of the cell.
293 */
294 protected function column_table_last_modified( array $item ): string {
295 $modified_timestamp = date_create( $item['last_modified'], wp_timezone() );
296 if ( false === $modified_timestamp ) {
297 $modified_timestamp = $item['last_modified'];
298 } else {
299 $modified_timestamp = $modified_timestamp->getTimestamp();
300 }
301 $current_timestamp = time();
302 $time_diff = $current_timestamp - $modified_timestamp;
303 // Time difference is only shown up to one week.
304 if ( $time_diff >= 0 && $time_diff < WEEK_IN_SECONDS ) {
305 $time_diff = sprintf( __( '%s ago', 'default' ), human_time_diff( $modified_timestamp, $current_timestamp ) );
306 } else {
307 $time_diff = TablePress::format_datetime( $item['last_modified'], '<br>' );
308 }
309 $readable_time = TablePress::format_datetime( $item['last_modified'] );
310 return '<abbr title="' . esc_attr( $readable_time ) . '">' . $time_diff . '</abbr>';
311 }
312
313 /**
314 * Handles output for the default column.
315 *
316 * @since 1.8.0
317 *
318 * @param array<string, mixed> $item Data item for the current row.
319 * @param string $column_name Current column name.
320 */
321 #[\Override]
322 protected function column_default( /* array */ $item, /* string */ $column_name ): void {
323 // Don't use type hints in the method declaration to prevent PHP errors, as the method is inherited.
324
325 /**
326 * Fires inside each custom column of the TablePress list table.
327 *
328 * @since 1.8.0
329 *
330 * @param string $column_name Current column name.
331 * @param array<string, mixed> $item Data item for the current row.
332 */
333 do_action( 'manage_tablepress_list_custom_column', $column_name, $item );
334 }
335
336 /**
337 * Generates and display row actions links for the list table.
338 *
339 * The "Table Name" column already gets these, so return an empty string here to prevent them from being duplicated.
340 *
341 * @since 2.0.0
342 *
343 * @param object|array<string, mixed> $item The item being acted upon.
344 * @param string $column_name Current column name.
345 * @param string $primary Primary column name.
346 * @return string The row actions HTML, or an empty string if the current column is not the primary column.
347 */
348 #[\Override]
349 protected function handle_row_actions( /* object|array */ $item, /* string */ $column_name, /* string */ $primary ): string {
350 // Don't use type hints in the method declaration to prevent PHP errors, as the method is inherited.
351
352 return '';
353 }
354
355 /**
356 * Get a list (name => title) bulk actions that are available.
357 *
358 * @since 1.0.0
359 *
360 * @return array<string, string> Bulk actions for this table.
361 */
362 #[\Override]
363 protected function get_bulk_actions(): array {
364 $bulk_actions = array();
365
366 if ( current_user_can( 'tablepress_copy_tables' ) ) {
367 $bulk_actions['copy'] = _x( 'Copy', 'bulk action', 'tablepress' );
368 }
369 if ( current_user_can( 'tablepress_export_tables' ) ) {
370 $bulk_actions['export'] = _x( 'Export', 'bulk action', 'tablepress' );
371 }
372 if ( current_user_can( 'tablepress_delete_tables' ) ) {
373 $bulk_actions['delete'] = _x( 'Delete', 'bulk action', 'tablepress' );
374 }
375
376 return $bulk_actions;
377 }
378
379 /**
380 * Render the bulk actions dropdown.
381 *
382 * In comparison with parent class, this has modified HTML (especially no field named "action" as that's being used already)!
383 *
384 * @since 1.0.0
385 *
386 * @param 'top'|'bottom' $which The location of the bulk actions: 'top' or 'bottom'.
387 * This is designated as optional for backwards-compatibility.
388 */
389 #[\Override]
390 protected function bulk_actions( /* string */ $which = 'top' ): void {
391 // Don't use type hints in the method declaration to prevent PHP errors, as the method is inherited.
392
393 if ( ! isset( $this->_actions ) ) {
394 $this->_actions = $this->get_bulk_actions();
395 $no_new_actions = $this->_actions;
396 /** This filter is documented in the WordPress function WP_List_Table::bulk_actions() in wp-admin/includes/class-wp-list-table.php */
397 $this->_actions = apply_filters( 'bulk_actions-' . $this->screen->id, $this->_actions ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
398 $this->_actions = array_intersect_assoc( $this->_actions, $no_new_actions );
399 $two = '';
400 } else {
401 $two = '2';
402 }
403
404 if ( empty( $this->_actions ) ) {
405 return;
406 }
407
408 $name_id = "bulk-action-selector-{$which}";
409 echo "<label for='{$name_id}' class='screen-reader-text'>" .
410 /* translators: Hidden accessibility text. */
411 __( 'Select bulk action', 'default' ) .
412 '</label>';
413 echo "<select name='{$name_id}' id='{$name_id}'>\n";
414 echo "<option value='-1' selected='selected'>" . __( 'Bulk actions', 'default' ) . "</option>\n";
415 foreach ( $this->_actions as $name => $title ) {
416 echo "\t<option value='{$name}'>{$title}</option>\n";
417 }
418 echo "</select>\n";
419 submit_button( __( 'Apply', 'default' ), 'action', 'bulk_action', false, array( 'id' => "doaction{$two}" ) );
420 echo "\n";
421 }
422
423 /**
424 * Holds the message to be displayed when there are no items in the table.
425 *
426 * @since 1.0.0
427 */
428 #[\Override]
429 public function no_items(): void {
430 _e( 'No tables found.', 'tablepress' );
431 if ( 0 === $this->items_count ) {
432 $user_can_add_tables = current_user_can( 'tablepress_add_tables' );
433 $user_can_import_tables = current_user_can( 'tablepress_import_tables' );
434
435 $add_url = TablePress::url( array( 'action' => 'add' ) );
436 $import_url = TablePress::url( array( 'action' => 'import' ) );
437
438 if ( $user_can_add_tables && $user_can_import_tables ) {
439 echo ' ' . sprintf( __( '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 );
440 } elseif ( $user_can_add_tables ) {
441 echo ' ' . sprintf( __( 'You should <a href="%s">add</a> a table to get started!', 'tablepress' ), $add_url );
442 } elseif ( $user_can_import_tables ) {
443 echo ' ' . sprintf( __( 'You should <a href="%s">import</a> a table to get started!', 'tablepress' ), $import_url );
444 }
445 }
446 }
447
448 /**
449 * Generate the elements above or below the table (like bulk actions and pagination).
450 *
451 * In comparison with parent class, this has no nonce field in its HTML code and a one-time filter around the pagination call, to change a string.
452 *
453 * @since 1.0.0
454 *
455 * @param 'top'|'bottom' $which Location ("top" or "bottom").
456 */
457 #[\Override]
458 protected function display_tablenav( /* string */ $which ): void {
459 // Don't use type hints in the method declaration to prevent PHP errors, as the method is inherited.
460
461 ?>
462 <div class="tablenav <?php echo esc_attr( $which ); ?>">
463
464 <?php if ( $this->has_items() ) : ?>
465 <div class="alignleft actions bulkactions">
466 <?php $this->bulk_actions( $which ); ?>
467 </div>
468 <?php
469 endif;
470 $this->extra_tablenav( $which );
471
472 add_filter( 'ngettext_default', array( $this, 'change_pagination_items_string' ), 10, 5 );
473 $this->pagination( $which );
474 remove_filter( 'ngettext_default', array( $this, 'change_pagination_items_string' ), 10 );
475 ?>
476
477 <br class="clear">
478 </div>
479 <?php
480 }
481
482 /**
483 * Replaces the "%s item/%s items" string in the pagination with "%s table/%s tables".
484 *
485 * @since 2.0.0
486 *
487 * @param string $translation The current translation of a singular or plural form.
488 * @param string $single The text to be used if the number is singular.
489 * @param string $plural The text to be used if the number is plural.
490 * @param int $number The number to compare against to use either the singular or plural form.
491 * @param string $domain Text domain. Defaults to 'default'.
492 * @return string The changed translation.
493 */
494 public function change_pagination_items_string( string $translation, string $single, string $plural, int $number, string $domain ): string {
495 if ( '%s item' === $single && '%s items' === $plural ) {
496 /* translators: %s: Number of tables */
497 $translation = _n( '%s table', '%s tables', $number, 'tablepress' );
498 }
499 return $translation;
500 }
501
502 /**
503 * Callback to determine whether the given $item contains the search term.
504 *
505 * @since 1.0.0
506 *
507 * @param string $item Table ID that shall be searched.
508 * @return bool Whether the search term was found or not.
509 */
510 protected function _search_callback( string $item ): bool {
511 static $term;
512 static $json_encoded_term;
513 if ( is_null( $term ) || is_null( $json_encoded_term ) ) {
514 $term = wp_unslash( $_GET['s'] );
515 $json_encoded_term = substr( wp_json_encode( $term, TABLEPRESS_JSON_OPTIONS ), 1, -1 ); // @phpstan-ignore argument.type
516 }
517
518 static $debug;
519 if ( is_null( $debug ) ) {
520 // Set debug variable to allow searching in corrupted tables.
521 $debug = isset( $_GET['debug'] ) ? ( 'true' === $_GET['debug'] ) : WP_DEBUG;
522 }
523
524 // Load table again, with data and options (for last_editor).
525 $item = TablePress::$model_table->load( $item, true, true );
526
527 if ( is_wp_error( $item ) ) {
528 return false;
529 }
530
531 // Don't search corrupted tables, except when debug mode is enabled via $_GET parameter or WP_DEBUG constant.
532 if ( ! $debug && isset( $item['is_corrupted'] ) && $item['is_corrupted'] ) {
533 return false;
534 }
535
536 $fn_stripos = function_exists( 'mb_stripos' ) ? 'mb_stripos' : 'stripos';
537
538 // Search from easy to hard, so that "expensive" code maybe doesn't have to run.
539 if ( false !== $fn_stripos( $item['id'], (string) $term )
540 || false !== $fn_stripos( $item['name'], (string) $term )
541 || false !== $fn_stripos( $item['description'], (string) $term )
542 || false !== $fn_stripos( TablePress::get_user_display_name( $item['author'] ), (string) $term )
543 || false !== $fn_stripos( TablePress::get_user_display_name( $item['options']['last_editor'] ), (string) $term )
544 || false !== $fn_stripos( TablePress::format_datetime( $item['last_modified'] ), (string) $term )
545 || false !== $fn_stripos( wp_json_encode( $item['data'], TABLEPRESS_JSON_OPTIONS ), (string) $json_encoded_term ) ) { // @phpstan-ignore argument.type
546 return true;
547 }
548
549 return false;
550 }
551
552 /**
553 * Callback to for the array sort function.
554 *
555 * @since 1.0.0
556 *
557 * @param array<string, mixed> $item_a First item that shall be compared to.
558 * @param array<string, mixed> $item_b The second item for the comparison.
559 * @return int (-1, 0, 1) depending on which item sorts "higher".
560 */
561 protected function _order_callback( array $item_a, array $item_b ): int {
562 global $orderby, $order;
563
564 if ( 'last_modified_by' !== $orderby ) {
565 if ( $item_a[ $orderby ] === $item_b[ $orderby ] ) {
566 return 0;
567 }
568 } else { // phpcs:ignore Universal.ControlStructures.DisallowLonelyIf.Found
569 if ( $item_a['options']['last_editor'] === $item_b['options']['last_editor'] ) {
570 return 0;
571 }
572 }
573
574 // Certain fields require some extra work before being sortable.
575 switch ( $orderby ) {
576 case 'last_modified':
577 // Compare UNIX timestamps for "last modified", which actually is a mySQL datetime string.
578 $result = ( strtotime( $item_a['last_modified'] ) > strtotime( $item_b['last_modified'] ) ) ? 1 : -1;
579 break;
580 case 'author':
581 // Get the actual author name, plain value is just the user ID.
582 $result = strnatcasecmp( TablePress::get_user_display_name( $item_a['author'] ), TablePress::get_user_display_name( $item_b['author'] ) );
583 break;
584 case 'last_modified_by':
585 // Get the actual last editor name, plain value is just the user ID.
586 $result = strnatcasecmp( TablePress::get_user_display_name( $item_a['options']['last_editor'] ), TablePress::get_user_display_name( $item_b['options']['last_editor'] ) );
587 break;
588 default:
589 // Other fields (ID, name, description) are sorted as strings.
590 $result = strnatcasecmp( $item_a[ $orderby ], $item_b[ $orderby ] );
591 }
592
593 return ( 'asc' === $order ) ? $result : - $result;
594 }
595
596 /**
597 * Prepares the list of items for displaying, by maybe searching and sorting, and by doing pagination.
598 *
599 * @since 1.0.0
600 */
601 #[\Override]
602 public function prepare_items(): void {
603 global $orderby, $order, $s;
604 wp_reset_vars( array( 'orderby', 'order', 's' ) );
605
606 // Maybe search in the items.
607 if ( $s ) {
608 $this->items = array_filter( $this->items, array( $this, '_search_callback' ) );
609 }
610
611 // Load actual tables after search for less memory consumption.
612 foreach ( $this->items as &$item ) {
613 // Don't load data, but load table options for access to last_editor.
614 $item = TablePress::$model_table->load( $item, false, true );
615 }
616 unset( $item ); // Unset use-by-reference parameter of foreach loop.
617
618 // Maybe sort the items.
619 $_sortable_columns = $this->get_sortable_columns();
620 if ( $orderby && ! empty( $this->items ) && isset( $_sortable_columns[ "table_{$orderby}" ] ) ) {
621 usort( $this->items, array( $this, '_order_callback' ) );
622 }
623
624 // Number of records to show per page.
625 $per_page = $this->get_items_per_page( 'tablepress_list_per_page', 20 ); // Hard-coded, as in filter in Admin_Controller.
626 // Page number the user is currently viewing.
627 $current_page = $this->get_pagenum();
628 // Number of records in the array.
629 $total_items = count( $this->items );
630
631 // Slice items array to hold only items for the current page.
632 $this->items = array_slice( $this->items, ( ( $current_page - 1 ) * $per_page ), $per_page );
633
634 // Register pagination options and calculation results.
635 $this->set_pagination_args( array(
636 'total_items' => $total_items, // Total number of records/items.
637 'per_page' => $per_page, // Number of items per page.
638 'total_pages' => (int) ceil( $total_items / $per_page ), // Total number of pages.
639 ) );
640 }
641
642 } // class TablePress_All_Tables_List_Table
643