PluginProbe
TablePress – Tables in WordPress made easy / 2.3.1
TablePress – Tables in WordPress made easy v2.3.1
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 2.3.1, at views/class-editor-button-thickbox-list-table.php

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