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

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