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

352 lines 10.8 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 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 public function get_columns(): array {
81 $columns = array(
82 // "name" is special in WP, which is why we prefix every entry here, to be safe!
83 'table_id' => __( 'ID', 'tablepress' ),
84 'table_name' => __( 'Table Name', 'tablepress' ),
85 'table_description' => __( 'Description', 'tablepress' ),
86 'table_action' => __( 'Action', 'tablepress' ),
87 );
88 return $columns;
89 }
90
91 /**
92 * Gets a list of columns that are sortable.
93 *
94 * Format: 'internal-name' => array( $field for $item[ $field ], true for already sorted ).
95 *
96 * @since 1.0.0
97 *
98 * @return array<string, array{string, bool}> List of sortable columns in this List Table.
99 */
100 protected function get_sortable_columns(): array {
101 // No sorting on the Empty List placeholder.
102 if ( ! $this->has_items() ) {
103 return array();
104 }
105
106 $sortable_columns = array(
107 'table_id' => array( 'id', true ), // true means its already sorted.
108 'table_name' => array( 'name', false ),
109 'table_description' => array( 'description', false ),
110 );
111 return $sortable_columns;
112 }
113
114 /**
115 * Gets the name of the default primary column.
116 *
117 * @since 1.7.0
118 *
119 * @return string Name of the default primary column, in this case, the table name.
120 */
121 protected function get_default_primary_column_name(): string {
122 return 'table_name';
123 }
124
125 /**
126 * Renders a cell in the "table_id" column.
127 *
128 * @since 1.0.0
129 *
130 * @param array<string, mixed> $item Data item for the current row.
131 * @return string HTML content of the cell.
132 */
133 protected function column_table_id( array $item ): string {
134 return esc_html( $item['id'] );
135 }
136
137 /**
138 * Renders a cell in the "table_name" column.
139 *
140 * @since 1.0.0
141 *
142 * @param array<string, mixed> $item Data item for the current row.
143 * @return string HTML content of the cell.
144 */
145 protected function column_table_name( array $item ): string {
146 if ( '' === trim( $item['name'] ) ) {
147 $item['name'] = __( '(no name)', 'tablepress' );
148 }
149 return esc_html( $item['name'] );
150 }
151
152 /**
153 * Renders a cell in the "table_description" 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 protected function column_table_description( array $item ): string {
161 if ( '' === trim( $item['description'] ) ) {
162 $item['description'] = __( '(no description)', 'tablepress' );
163 }
164 return esc_html( $item['description'] );
165 }
166
167 /**
168 * Renders a cell in the "table_action" column, i.e. the "Insert" link.
169 *
170 * @since 1.0.0
171 *
172 * @param array<string, mixed> $item Data item for the current row.
173 * @return string HTML content of the cell.
174 */
175 protected function column_table_action( array $item ): string {
176 return '<input type="button" class="insert-shortcode button" title="' . esc_attr( '[' . TablePress::$shortcode . " id={$item['id']} /]" ) . '" value="' . esc_attr__( 'Insert Shortcode', 'tablepress' ) . '" />';
177 }
178
179 /**
180 * Holds the message to be displayed when there are no items in the table.
181 *
182 * @since 1.0.0
183 */
184 public function no_items(): void {
185 _e( 'No tables found.', 'tablepress' );
186 if ( 0 === $this->items_count ) {
187 echo ' ' . __( 'You should add or import a table on the TablePress screens to get started!', 'tablepress' );
188 }
189 }
190
191 /**
192 * Generates the elements above or below the table (like bulk actions and pagination).
193 *
194 * In comparison with parent class, this has modified HTML (no nonce field), and a check whether there are items.
195 *
196 * @since 1.0.0
197 *
198 * @param 'top'|'bottom' $which Location ("top" or "bottom").
199 */
200 protected function display_tablenav( /* string */ $which ): void {
201 // Don't use type hints in the method declaration to prevent PHP errors, as the method is inherited.
202
203 if ( ! $this->has_items() ) {
204 return;
205 }
206 ?>
207 <div class="tablenav <?php echo esc_attr( $which ); ?>">
208 <div class="alignleft actions">
209 <?php $this->bulk_actions( $which ); ?>
210 </div>
211 <?php
212 $this->extra_tablenav( $which );
213
214 add_filter( 'ngettext_default', array( $this, 'change_pagination_items_string' ), 10, 5 );
215 $this->pagination( $which );
216 remove_filter( 'ngettext_default', array( $this, 'change_pagination_items_string' ), 10 );
217 ?>
218 <br class="clear" />
219 </div>
220 <?php
221 }
222
223 /**
224 * Replaces the "%s item/%s items" string in the pagination with "%s table/%s tables".
225 *
226 * @since 2.0.0
227 *
228 * @param string $translation The current translation of a singular or plural form.
229 * @param string $single The text to be used if the number is singular.
230 * @param string $plural The text to be used if the number is plural.
231 * @param int $number The number to compare against to use either the singular or plural form.
232 * @param string $domain Text domain. Defaults to 'default'.
233 * @return string The changed translation.
234 */
235 public function change_pagination_items_string( string $translation, string $single, string $plural, int $number, string $domain ): string {
236 if ( '%s item' === $single && '%s items' === $plural ) {
237 $translation = _n( '%s table', '%s tables', $number, 'tablepress' );
238 }
239 return $translation;
240 }
241
242 /**
243 * Callback to determine whether the given $item contains the search term.
244 *
245 * @since 1.0.0
246 *
247 * @param string $item Table ID that shall be searched.
248 * @return bool Whether the search term was found or not.
249 */
250 protected function _search_callback( string $item ): bool {
251 static $term;
252 static $json_encoded_term;
253 if ( is_null( $term ) || is_null( $json_encoded_term ) ) {
254 $term = wp_unslash( $_GET['s'] );
255 $json_encoded_term = substr( wp_json_encode( $term, TABLEPRESS_JSON_OPTIONS ), 1, -1 ); // @phpstan-ignore-line
256 }
257
258 // Load table again, with table data, but without options and visibility settings.
259 $item = TablePress::$model_table->load( $item, true, false );
260
261 if ( is_wp_error( $item ) ) {
262 return false;
263 }
264
265 // Don't search corrupted tables.
266 if ( isset( $item['is_corrupted'] ) && $item['is_corrupted'] ) {
267 return false;
268 }
269
270 // Search from easy to hard, so that "expensive" code maybe doesn't have to run.
271 if ( false !== stripos( $item['id'], $term )
272 || false !== stripos( $item['name'], $term )
273 || false !== stripos( $item['description'], $term )
274 || false !== stripos( TablePress::get_user_display_name( $item['author'] ), $term )
275 || false !== stripos( TablePress::format_datetime( $item['last_modified'] ), $term )
276 || false !== stripos( wp_json_encode( $item['data'], TABLEPRESS_JSON_OPTIONS ), $json_encoded_term ) ) { // @phpstan-ignore-line
277 return true;
278 }
279
280 return false;
281 }
282
283 /**
284 * Callback to for the array sort function.
285 *
286 * @since 1.0.0
287 *
288 * @param array<string, mixed> $item_a First item that shall be compared to.
289 * @param array<string, mixed> $item_b The second item for the comparison.
290 * @return int (-1, 0, 1) depending on which item sorts "higher".
291 */
292 protected function _order_callback( array $item_a, array $item_b ): int {
293 global $orderby, $order;
294
295 if ( $item_a[ $orderby ] === $item_b[ $orderby ] ) {
296 return 0;
297 }
298
299 // Fields in this list table are all strings.
300 $result = strnatcasecmp( $item_a[ $orderby ], $item_b[ $orderby ] );
301
302 return ( 'asc' === $order ) ? $result : - $result;
303 }
304
305 /**
306 * Prepares the list of items for displaying, by maybe searching and sorting, and by doing pagination.
307 *
308 * @since 1.0.0
309 */
310 public function prepare_items(): void {
311 global $orderby, $order, $s;
312 wp_reset_vars( array( 'orderby', 'order', 's' ) );
313
314 // Maybe search in the items.
315 if ( $s ) {
316 $this->items = array_filter( $this->items, array( $this, '_search_callback' ) );
317 }
318
319 // Load actual tables after search for less memory consumption.
320 foreach ( $this->items as &$item ) {
321 // Don't load data nor table options.
322 $item = TablePress::$model_table->load( $item, false, false );
323 }
324 // Break reference in foreach iterator.
325 unset( $item );
326
327 // Maybe sort the items.
328 $_sortable_columns = $this->get_sortable_columns();
329 if ( $orderby && ! empty( $this->items ) && isset( $_sortable_columns[ "table_{$orderby}" ] ) ) {
330 usort( $this->items, array( $this, '_order_callback' ) );
331 }
332
333 // Number of records to show per page.
334 $per_page = 20; // Hard-coded, as there's no possibility to change this in the Thickbox.
335 // Page number the user is currently viewing.
336 $current_page = $this->get_pagenum();
337 // Number of records in the array.
338 $total_items = count( $this->items );
339
340 // Slice items array to hold only items for the current page.
341 $this->items = array_slice( $this->items, ( ( $current_page - 1 ) * $per_page ), $per_page );
342
343 // Register pagination options and calculation results.
344 $this->set_pagination_args( array(
345 'total_items' => $total_items, // Total number of records/items.
346 'per_page' => $per_page, // Number of items per page.
347 'total_pages' => (int) ceil( $total_items / $per_page ), // Total number of pages.
348 ) );
349 }
350
351 } // class TablePress_Editor_Button_Thickbox_List_Table
352