PluginProbe
TablePress – Tables in WordPress made easy / 3.0
TablePress – Tables in WordPress made easy v3.0
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 2.4.4 All 44 releases
tablepress / views / class-all-tables-list-table.php

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

638 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 // The `label-covers-full-cell` class on the <label> is kept for (some) backward compatibility with WordPress 6.3, and can be removed once TablePress requires WordPress 6.4.
178 '<input type="checkbox" id="cb-select-%1$s" name="table[]" value="%1$s"><label class="label-covers-full-cell" for="cb-select-%1$s"><span class="screen-reader-text">%2$s</span></label>',
179 esc_attr( $item['id'] ),
180 esc_html( sprintf( __( 'Select table “%s”', 'tablepress' ), $item['name'] ) ),
181 );
182 }
183
184 /**
185 * Render a cell in the "table_id" column.
186 *
187 * @since 1.0.0
188 *
189 * @param array<string, mixed> $item Data item for the current row.
190 * @return string HTML content of the cell.
191 */
192 protected function column_table_id( array $item ): string {
193 return esc_html( $item['id'] );
194 }
195
196 /**
197 * Render a cell in the "table_name" column.
198 *
199 * @since 1.0.0
200 *
201 * @param array<string, mixed> $item Data item for the current row.
202 * @return string HTML content of the cell.
203 */
204 protected function column_table_name( array $item ): string {
205 $user_can_edit_table = current_user_can( 'tablepress_edit_table', $item['id'] );
206 $user_can_copy_table = current_user_can( 'tablepress_copy_table', $item['id'] );
207 $user_can_export_table = current_user_can( 'tablepress_export_table', $item['id'] );
208 $user_can_delete_table = current_user_can( 'tablepress_delete_table', $item['id'] );
209 $user_can_preview_table = current_user_can( 'tablepress_preview_table', $item['id'] );
210
211 $edit_url = TablePress::url( array( 'action' => 'edit', 'table_id' => $item['id'] ) );
212 $copy_url = TablePress::url( array( 'action' => 'copy_table', 'item' => $item['id'], 'return' => 'list', 'return_item' => $item['id'] ), true, 'admin-post.php' );
213 $export_url = TablePress::url( array( 'action' => 'export', 'table_id' => $item['id'] ) );
214 $delete_url = TablePress::url( array( 'action' => 'delete_table', 'item' => $item['id'], 'return' => 'list', 'return_item' => $item['id'] ), true, 'admin-post.php' );
215 $preview_url = TablePress::url( array( 'action' => 'preview_table', 'item' => $item['id'], 'return' => 'list', 'return_item' => $item['id'] ), true, 'admin-post.php' );
216
217 if ( '' === trim( $item['name'] ) ) {
218 $item['name'] = __( '(no name)', 'tablepress' );
219 }
220
221 if ( $user_can_edit_table ) {
222 $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>';
223 } else {
224 $row_text = '<strong>' . esc_html( $item['name'] ) . '</strong>';
225 }
226
227 $row_actions = array();
228 if ( $user_can_edit_table ) {
229 $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' ) );
230 }
231 $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' ) );
232 if ( $user_can_copy_table ) {
233 $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' ) );
234 }
235 if ( $user_can_export_table ) {
236 $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' ) );
237 }
238 if ( $user_can_delete_table ) {
239 $row_actions['delete'] = sprintf( '<a href="%1$s" title="%2$s" class="delete-link">%3$s</a>', $delete_url, esc_attr( sprintf( __( 'Delete &#8220;%s&#8221;', 'tablepress' ), $item['name'] ) ), __( 'Delete', 'tablepress' ) );
240 }
241 if ( $user_can_preview_table ) {
242 $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' ) );
243 }
244
245 return $row_text . $this->row_actions( $row_actions );
246 }
247
248 /**
249 * Render a cell in the "table_description" column.
250 *
251 * @since 1.0.0
252 *
253 * @param array<string, mixed> $item Data item for the current row.
254 * @return string HTML content of the cell.
255 */
256 protected function column_table_description( array $item ): string {
257 if ( '' === trim( $item['description'] ) ) {
258 $item['description'] = __( '(no description)', 'tablepress' );
259 }
260 return esc_html( $item['description'] );
261 }
262
263 /**
264 * Render a cell in the "table_author" column.
265 *
266 * @since 1.0.0
267 *
268 * @param array<string, mixed> $item Data item for the current row.
269 * @return string HTML content of the cell.
270 */
271 protected function column_table_author( array $item ): string {
272 return TablePress::get_user_display_name( $item['author'] );
273 }
274
275 /**
276 * Render a cell in the "last_modified_by" column.
277 *
278 * @since 1.0.0
279 *
280 * @param array<string, mixed> $item Data item for the current row.
281 * @return string HTML content of the cell.
282 */
283 protected function column_table_last_modified_by( array $item ): string {
284 return TablePress::get_user_display_name( $item['options']['last_editor'] );
285 }
286
287 /**
288 * Render a cell in the "table_last_modified" column.
289 *
290 * @since 1.0.0
291 *
292 * @param array<string, mixed> $item Data item for the current row.
293 * @return string HTML content of the cell.
294 */
295 protected function column_table_last_modified( array $item ): string {
296 $modified_timestamp = date_create( $item['last_modified'], wp_timezone() );
297 if ( false === $modified_timestamp ) {
298 $modified_timestamp = $item['last_modified'];
299 } else {
300 $modified_timestamp = $modified_timestamp->getTimestamp();
301 }
302 $current_timestamp = time();
303 $time_diff = $current_timestamp - $modified_timestamp;
304 // Time difference is only shown up to one week.
305 if ( $time_diff >= 0 && $time_diff < WEEK_IN_SECONDS ) {
306 $time_diff = sprintf( __( '%s ago', 'default' ), human_time_diff( $modified_timestamp, $current_timestamp ) );
307 } else {
308 $time_diff = TablePress::format_datetime( $item['last_modified'], '<br>' );
309 }
310 $readable_time = TablePress::format_datetime( $item['last_modified'] );
311 return '<abbr title="' . esc_attr( $readable_time ) . '">' . $time_diff . '</abbr>';
312 }
313
314 /**
315 * Handles output for the default column.
316 *
317 * @since 1.8.0
318 *
319 * @param array<string, mixed> $item Data item for the current row.
320 * @param string $column_name Current column name.
321 */
322 #[\Override]
323 protected function column_default( /* array */ $item, /* string */ $column_name ): void {
324 // Don't use type hints in the method declaration to prevent PHP errors, as the method is inherited.
325
326 /**
327 * Fires inside each custom column of the TablePress list table.
328 *
329 * @since 1.8.0
330 *
331 * @param string $column_name Current column name.
332 * @param array<string, mixed> $item Data item for the current row.
333 */
334 do_action( 'manage_tablepress_list_custom_column', $column_name, $item );
335 }
336
337 /**
338 * Generates and display row actions links for the list table.
339 *
340 * The "Table Name" column already gets these, so return an empty string here to prevent them from being duplicated.
341 *
342 * @since 2.0.0
343 *
344 * @param object|array<string, mixed> $item The item being acted upon.
345 * @param string $column_name Current column name.
346 * @param string $primary Primary column name.
347 * @return string The row actions HTML, or an empty string if the current column is not the primary column.
348 */
349 #[\Override]
350 protected function handle_row_actions( /* object|array */ $item, /* string */ $column_name, /* string */ $primary ): string {
351 // Don't use type hints in the method declaration to prevent PHP errors, as the method is inherited.
352
353 return '';
354 }
355
356 /**
357 * Get a list (name => title) bulk actions that are available.
358 *
359 * @since 1.0.0
360 *
361 * @return array<string, string> Bulk actions for this table.
362 */
363 #[\Override]
364 protected function get_bulk_actions(): array {
365 $bulk_actions = array();
366
367 if ( current_user_can( 'tablepress_copy_tables' ) ) {
368 $bulk_actions['copy'] = _x( 'Copy', 'bulk action', 'tablepress' );
369 }
370 if ( current_user_can( 'tablepress_export_tables' ) ) {
371 $bulk_actions['export'] = _x( 'Export', 'bulk action', 'tablepress' );
372 }
373 if ( current_user_can( 'tablepress_delete_tables' ) ) {
374 $bulk_actions['delete'] = _x( 'Delete', 'bulk action', 'tablepress' );
375 }
376
377 return $bulk_actions;
378 }
379
380 /**
381 * Render the bulk actions dropdown.
382 *
383 * In comparison with parent class, this has modified HTML (especially no field named "action" as that's being used already)!
384 *
385 * @since 1.0.0
386 *
387 * @param 'top'|'bottom' $which The location of the bulk actions: 'top' or 'bottom'.
388 * This is designated as optional for backwards-compatibility.
389 */
390 #[\Override]
391 protected function bulk_actions( /* string */ $which = 'top' ): void {
392 // Don't use type hints in the method declaration to prevent PHP errors, as the method is inherited.
393
394 if ( ! isset( $this->_actions ) ) {
395 $this->_actions = $this->get_bulk_actions();
396 $no_new_actions = $this->_actions;
397 /** This filter is documented in the WordPress function WP_List_Table::bulk_actions() in wp-admin/includes/class-wp-list-table.php */
398 $this->_actions = apply_filters( 'bulk_actions-' . $this->screen->id, $this->_actions ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
399 $this->_actions = array_intersect_assoc( $this->_actions, $no_new_actions );
400 $two = '';
401 } else {
402 $two = '2';
403 }
404
405 if ( empty( $this->_actions ) ) {
406 return;
407 }
408
409 $name_id = "bulk-action-selector-{$which}";
410 echo "<label for='{$name_id}' class='screen-reader-text'>" . __( 'Select Bulk Action', 'tablepress' ) . "</label>\n";
411 echo "<select name='{$name_id}' id='{$name_id}'>\n";
412 echo "<option value='-1' selected='selected'>" . __( 'Bulk Actions', 'tablepress' ) . "</option>\n";
413 foreach ( $this->_actions as $name => $title ) {
414 echo "\t<option value='{$name}'>{$title}</option>\n";
415 }
416 echo "</select>\n";
417 submit_button( __( 'Apply', 'tablepress' ), 'action', '', false, array( 'id' => "doaction{$two}" ) );
418 echo "\n";
419 }
420
421 /**
422 * Holds the message to be displayed when there are no items in the table.
423 *
424 * @since 1.0.0
425 */
426 #[\Override]
427 public function no_items(): void {
428 _e( 'No tables found.', 'tablepress' );
429 if ( 0 === $this->items_count ) {
430 $user_can_add_tables = current_user_can( 'tablepress_add_tables' );
431 $user_can_import_tables = current_user_can( 'tablepress_import_tables' );
432
433 $add_url = TablePress::url( array( 'action' => 'add' ) );
434 $import_url = TablePress::url( array( 'action' => 'import' ) );
435
436 if ( $user_can_add_tables && $user_can_import_tables ) {
437 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 );
438 } elseif ( $user_can_add_tables ) {
439 echo ' ' . sprintf( __( 'You should <a href="%s">add</a> a table to get started!', 'tablepress' ), $add_url );
440 } elseif ( $user_can_import_tables ) {
441 echo ' ' . sprintf( __( 'You should <a href="%s">import</a> a table to get started!', 'tablepress' ), $import_url );
442 }
443 }
444 }
445
446 /**
447 * Generate the elements above or below the table (like bulk actions and pagination).
448 *
449 * 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.
450 *
451 * @since 1.0.0
452 *
453 * @param 'top'|'bottom' $which Location ("top" or "bottom").
454 */
455 #[\Override]
456 protected function display_tablenav( /* string */ $which ): void {
457 // Don't use type hints in the method declaration to prevent PHP errors, as the method is inherited.
458
459 ?>
460 <div class="tablenav <?php echo esc_attr( $which ); ?>">
461
462 <?php if ( $this->has_items() ) : ?>
463 <div class="alignleft actions bulkactions">
464 <?php $this->bulk_actions( $which ); ?>
465 </div>
466 <?php
467 endif;
468 $this->extra_tablenav( $which );
469
470 add_filter( 'ngettext_default', array( $this, 'change_pagination_items_string' ), 10, 5 );
471 $this->pagination( $which );
472 remove_filter( 'ngettext_default', array( $this, 'change_pagination_items_string' ), 10 );
473 ?>
474
475 <br class="clear">
476 </div>
477 <?php
478 }
479
480 /**
481 * Replaces the "%s item/%s items" string in the pagination with "%s table/%s tables".
482 *
483 * @since 2.0.0
484 *
485 * @param string $translation The current translation of a singular or plural form.
486 * @param string $single The text to be used if the number is singular.
487 * @param string $plural The text to be used if the number is plural.
488 * @param int $number The number to compare against to use either the singular or plural form.
489 * @param string $domain Text domain. Defaults to 'default'.
490 * @return string The changed translation.
491 */
492 public function change_pagination_items_string( string $translation, string $single, string $plural, int $number, string $domain ): string {
493 if ( '%s item' === $single && '%s items' === $plural ) {
494 $translation = _n( '%s table', '%s tables', $number, 'tablepress' );
495 }
496 return $translation;
497 }
498
499 /**
500 * Callback to determine whether the given $item contains the search term.
501 *
502 * @since 1.0.0
503 *
504 * @param string $item Table ID that shall be searched.
505 * @return bool Whether the search term was found or not.
506 */
507 protected function _search_callback( string $item ): bool {
508 static $term;
509 static $json_encoded_term;
510 if ( is_null( $term ) || is_null( $json_encoded_term ) ) {
511 $term = wp_unslash( $_GET['s'] );
512 $json_encoded_term = substr( wp_json_encode( $term, TABLEPRESS_JSON_OPTIONS ), 1, -1 ); // @phpstan-ignore argument.type
513 }
514
515 static $debug;
516 if ( is_null( $debug ) ) {
517 // Set debug variable to allow searching in corrupted tables.
518 $debug = isset( $_GET['debug'] ) ? ( 'true' === $_GET['debug'] ) : WP_DEBUG;
519 }
520
521 // Load table again, with data and options (for last_editor).
522 $item = TablePress::$model_table->load( $item, true, true );
523
524 if ( is_wp_error( $item ) ) {
525 return false;
526 }
527
528 // Don't search corrupted tables, except when debug mode is enabled via $_GET parameter or WP_DEBUG constant.
529 if ( ! $debug && isset( $item['is_corrupted'] ) && $item['is_corrupted'] ) {
530 return false;
531 }
532
533 // Search from easy to hard, so that "expensive" code maybe doesn't have to run.
534 if ( false !== stripos( $item['id'], (string) $term )
535 || false !== stripos( $item['name'], (string) $term )
536 || false !== stripos( $item['description'], (string) $term )
537 || false !== stripos( TablePress::get_user_display_name( $item['author'] ), (string) $term )
538 || false !== stripos( TablePress::get_user_display_name( $item['options']['last_editor'] ), (string) $term )
539 || false !== stripos( TablePress::format_datetime( $item['last_modified'] ), (string) $term )
540 || false !== stripos( wp_json_encode( $item['data'], TABLEPRESS_JSON_OPTIONS ), (string) $json_encoded_term ) ) { // @phpstan-ignore argument.type
541 return true;
542 }
543
544 return false;
545 }
546
547 /**
548 * Callback to for the array sort function.
549 *
550 * @since 1.0.0
551 *
552 * @param array<string, mixed> $item_a First item that shall be compared to.
553 * @param array<string, mixed> $item_b The second item for the comparison.
554 * @return int (-1, 0, 1) depending on which item sorts "higher".
555 */
556 protected function _order_callback( array $item_a, array $item_b ): int {
557 global $orderby, $order;
558
559 if ( 'last_modified_by' !== $orderby ) {
560 if ( $item_a[ $orderby ] === $item_b[ $orderby ] ) {
561 return 0;
562 }
563 } else { // phpcs:ignore Universal.ControlStructures.DisallowLonelyIf.Found
564 if ( $item_a['options']['last_editor'] === $item_b['options']['last_editor'] ) {
565 return 0;
566 }
567 }
568
569 // Certain fields require some extra work before being sortable.
570 switch ( $orderby ) {
571 case 'last_modified':
572 // Compare UNIX timestamps for "last modified", which actually is a mySQL datetime string.
573 $result = ( strtotime( $item_a['last_modified'] ) > strtotime( $item_b['last_modified'] ) ) ? 1 : -1;
574 break;
575 case 'author':
576 // Get the actual author name, plain value is just the user ID.
577 $result = strnatcasecmp( TablePress::get_user_display_name( $item_a['author'] ), TablePress::get_user_display_name( $item_b['author'] ) );
578 break;
579 case 'last_modified_by':
580 // Get the actual last editor name, plain value is just the user ID.
581 $result = strnatcasecmp( TablePress::get_user_display_name( $item_a['options']['last_editor'] ), TablePress::get_user_display_name( $item_b['options']['last_editor'] ) );
582 break;
583 default:
584 // Other fields (ID, name, description) are sorted as strings.
585 $result = strnatcasecmp( $item_a[ $orderby ], $item_b[ $orderby ] );
586 }
587
588 return ( 'asc' === $order ) ? $result : - $result;
589 }
590
591 /**
592 * Prepares the list of items for displaying, by maybe searching and sorting, and by doing pagination.
593 *
594 * @since 1.0.0
595 */
596 #[\Override]
597 public function prepare_items(): void {
598 global $orderby, $order, $s;
599 wp_reset_vars( array( 'orderby', 'order', 's' ) );
600
601 // Maybe search in the items.
602 if ( $s ) {
603 $this->items = array_filter( $this->items, array( $this, '_search_callback' ) );
604 }
605
606 // Load actual tables after search for less memory consumption.
607 foreach ( $this->items as &$item ) {
608 // Don't load data, but load table options for access to last_editor.
609 $item = TablePress::$model_table->load( $item, false, true );
610 }
611 unset( $item ); // Unset use-by-reference parameter of foreach loop.
612
613 // Maybe sort the items.
614 $_sortable_columns = $this->get_sortable_columns();
615 if ( $orderby && ! empty( $this->items ) && isset( $_sortable_columns[ "table_{$orderby}" ] ) ) {
616 usort( $this->items, array( $this, '_order_callback' ) );
617 }
618
619 // Number of records to show per page.
620 $per_page = $this->get_items_per_page( 'tablepress_list_per_page', 20 ); // Hard-coded, as in filter in Admin_Controller.
621 // Page number the user is currently viewing.
622 $current_page = $this->get_pagenum();
623 // Number of records in the array.
624 $total_items = count( $this->items );
625
626 // Slice items array to hold only items for the current page.
627 $this->items = array_slice( $this->items, ( ( $current_page - 1 ) * $per_page ), $per_page );
628
629 // Register pagination options and calculation results.
630 $this->set_pagination_args( array(
631 'total_items' => $total_items, // Total number of records/items.
632 'per_page' => $per_page, // Number of items per page.
633 'total_pages' => (int) ceil( $total_items / $per_page ), // Total number of pages.
634 ) );
635 }
636
637 } // class TablePress_All_Tables_List_Table
638