PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / trunk
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages vtrunk
3.4.1 3.4.0 3.3.9 3.3.8 3.3.7 3.3.6 3.3.5 3.3.4 3.3.3 3.3.2 3.3.1 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 2.3.0 2.3.1 2.3.2 2.3.3 All 194 releases
convertkit / admin / class-convertkit-wp-list-table.php

class-convertkit-wp-list-table.php in Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages trunk, at admin/class-convertkit-wp-list-table.php

549 lines 11.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ConvertKit WP_List_Table class.
4 *
5 * @package ConvertKit
6 * @author ConvertKit
7 */
8
9 /**
10 * Include WP_List_Table if not defined.
11 */
12 if ( ! class_exists( 'WP_List_Table' ) ) {
13 require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
14 }
15
16 /**
17 * Displays rows of data (such as settings) in a WP_List_Table.
18 * Mainly used for Contact Form 7, Forminator and WishList Member settings screens.
19 *
20 * @package ConvertKit
21 * @author ConvertKit
22 */
23 class ConvertKit_WP_List_Table extends WP_List_Table {
24
25 /**
26 * Holds the page query parameter.
27 *
28 * @since 3.0.0
29 *
30 * @var bool|string
31 */
32 private $page = false;
33
34 /**
35 * Holds the tab query parameter.
36 *
37 * @since 3.0.0
38 *
39 * @var bool|string
40 */
41 private $tab = false;
42
43 /**
44 * Holds the supported bulk actions.
45 *
46 * @var array
47 */
48 private $bulk_actions = array();
49
50 /**
51 * Holds the supported filters.
52 *
53 * @since 3.0.1
54 *
55 * @var array
56 */
57 private $filters = array();
58
59 /**
60 * Holds the table columns.
61 *
62 * @var array
63 */
64 private $columns = array();
65
66 /**
67 * Holds the sortable table columns.
68 *
69 * @var array
70 */
71 private $sortable_columns = array();
72
73 /**
74 * Holds the total number of items in the table.
75 *
76 * @since 3.0.0
77 *
78 * @var int
79 */
80 private $total_items = 0;
81
82 /**
83 * Holds the key for the items per page options.
84 *
85 * @since 3.0.0
86 *
87 * @var string
88 */
89 private $items_per_page_screen_options_key = 'convertkit_form_entries_per_page';
90
91 /**
92 * Constructor.
93 *
94 * @since 1.0.0
95 *
96 * @param bool|string $page Page query parameter.
97 * @param bool|string $tab Tab query parameter.
98 */
99 public function __construct( $page = false, $tab = false ) {
100
101 $this->page = $page;
102 $this->tab = $tab;
103
104 parent::__construct(
105 array(
106 'singular' => 'convertkit-item',
107 'plural' => 'convertkit-items',
108 'ajax' => false,
109 )
110 );
111
112 }
113
114 /**
115 * Set default column attributes
116 *
117 * @since 1.0.0
118 *
119 * @param array $item A singular item (one full row's worth of data).
120 * @param string $column_name The name/slug of the column to be processed.
121 * @return string Text or HTML to be placed inside the column <td>
122 */
123 public function column_default( $item, $column_name ) {
124
125 return $item[ $column_name ];
126
127 }
128
129 /**
130 * Provide a callback function to render the checkbox column
131 *
132 * @param array $item A row's worth of data.
133 * @return string The formatted string with a checkbox
134 */
135 public function column_cb( $item ) {
136
137 return sprintf(
138 '<input type="checkbox" name="%1$s[]" id="cb-select-%2$s" value="%3$s" />',
139 $this->_args['plural'],
140 $item['id'],
141 $item['id']
142 );
143
144 }
145
146 /**
147 * Get the bulk actions for this table
148 *
149 * @return array Bulk actions
150 */
151 public function get_bulk_actions() {
152
153 return $this->bulk_actions;
154
155 }
156
157 /**
158 * Displays the search input field.
159 *
160 * @since 3.0.0
161 *
162 * @param string $text The 'submit' button label.
163 * @param string $input_id ID attribute value for the search input field.
164 */
165 public function search_box( $text, $input_id ) {
166
167 ?>
168 <p class="search-box">
169 <label class="screen-reader-text" for="<?php echo esc_attr( $input_id ); ?>"><?php echo esc_attr( $text ); ?>:</label>
170 <input type="search" id="<?php echo esc_attr( $input_id ); ?>" name="s" value="<?php _admin_search_query(); ?>" placeholder="<?php esc_attr_e( 'Search', 'convertkit' ); ?>" />
171 <?php submit_button( $text, 'secondary', 'submit', false, array( 'id' => 'search-submit' ) ); ?>
172 </p>
173 <?php
174 if ( $this->page ) {
175 ?>
176 <input type="hidden" name="page" value="<?php echo esc_attr( $this->page ); ?>" />
177 <?php
178 }
179 if ( $this->tab ) {
180 ?>
181 <input type="hidden" name="tab" value="<?php echo esc_attr( $this->tab ); ?>" />
182 <?php
183 }
184
185 }
186
187 /**
188 * Get a list of columns
189 *
190 * @return array
191 */
192 public function get_columns() {
193
194 return $this->columns;
195
196 }
197
198 /**
199 * Add a column to the table
200 *
201 * @param string $key Machine-readable column name.
202 * @param string $title Title shown to the user.
203 * @param boolean $sortable Whether or not this is sortable (defaults false).
204 */
205 public function add_column( $key, $title, $sortable = false ) {
206
207 $this->columns[ $key ] = $title;
208
209 if ( $sortable ) {
210 $this->sortable_columns[ $key ] = array( $key, false );
211 }
212
213 }
214
215 /**
216 * Add an item (row) to the table
217 *
218 * @param array $item A row's worth of data.
219 */
220 public function add_item( $item ) {
221
222 array_push( $this->items, $item );
223
224 }
225
226 /**
227 * Add multiple items to the table
228 *
229 * @since 3.0.0
230 *
231 * @param array $items Table rows.
232 */
233 public function add_items( $items ) {
234
235 $this->items = $items;
236
237 }
238
239 /**
240 * Set the total number of items available, which may
241 * be greater than the number of items displayed.
242 *
243 * @since 3.0.0
244 *
245 * @param int $total_items Total number of items.
246 */
247 public function set_total_items( $total_items ) {
248
249 $this->total_items = $total_items;
250
251 }
252
253 /**
254 * Set the key that stores the user's preference for the number of items per page
255 * to display, defined in the Screen Options.
256 *
257 * @since 3.0.0
258 *
259 * @param string $items_per_page_screen_options_key Key.
260 */
261 public function set_items_per_page_screen_options_key( $items_per_page_screen_options_key ) {
262
263 $this->items_per_page_screen_options_key = $items_per_page_screen_options_key;
264
265 }
266
267 /**
268 * Get the total number of items available, which may
269 * be greater than the number of items displayed.
270 *
271 * @since 3.0.0
272 *
273 * @return int Total number of items.
274 */
275 public function get_total_items() {
276
277 if ( $this->total_items ) {
278 return $this->total_items;
279 }
280
281 return count( $this->items );
282
283 }
284
285 /**
286 * Add a bulk action to the table
287 *
288 * @param string $key Machine-readable action name.
289 * @param string $name Title shown to the user.
290 */
291 public function add_bulk_action( $key, $name ) {
292
293 $this->bulk_actions[ $key ] = $name;
294
295 }
296
297 /**
298 * Add a filter to the table
299 *
300 * @since 3.0.1
301 *
302 * @param string $key Filter name.
303 * @param string $label Filter label.
304 * @param array $options Filter options.
305 */
306 public function add_filter( $key, $label, $options ) {
307
308 $this->filters[ $key ] = array(
309 'label' => $label,
310 'options' => $options,
311 );
312
313 }
314
315 /**
316 * Define table columns and pagination for this WP_List_Table.
317 *
318 * @since 3.0.0
319 */
320 public function prepare_items() {
321
322 // Set column headers.
323 // If this isn't done, the table will not display.
324 $this->_column_headers = array( $this->columns, array(), $this->sortable_columns );
325
326 // If no items per page options key is set, return, as it means
327 // this table won't paginate.
328 if ( ! $this->items_per_page_screen_options_key ) {
329 return;
330 }
331
332 // Set pagination args so WP_List_Table knows what to render.
333 $total_items = $this->get_total_items();
334 $per_page = $this->get_items_per_page( $this->items_per_page_screen_options_key, 25 );
335 $total_pages = (int) ceil( $total_items / $per_page );
336
337 $this->set_pagination_args(
338 array(
339 'total_items' => $total_items,
340 'per_page' => $per_page,
341 'total_pages' => $total_pages,
342 )
343 );
344
345 }
346
347 /**
348 * Generate the table navigation above or below the table
349 *
350 * @since 3.0.0
351 *
352 * @param string $which The location of the bulk actions: 'top' or 'bottom'.
353 * This is designated as optional for backward compatibility.
354 */
355 protected function display_tablenav( $which ) {
356
357 // Define a nonce for search submissions.
358 if ( 'top' === $which ) {
359 wp_nonce_field( 'bulk-' . $this->_args['plural'] );
360 }
361 ?>
362 <div class="tablenav <?php echo esc_attr( $which ); ?>">
363 <div class="alignleft actions bulkactions">
364 <?php
365 $this->bulk_actions( $which );
366 ?>
367 </div>
368 <?php
369 $this->extra_tablenav( $which );
370 $this->pagination( $which );
371 $this->filters( $which );
372 ?>
373
374 <br class="clear" />
375 </div>
376 <?php
377
378 }
379
380 /**
381 * Display the filters
382 *
383 * @since 3.0.1
384 *
385 * @param string $which The location of the bulk actions: 'top' or 'bottom'.
386 */
387 public function filters( $which ) {
388
389 // Don't output filters if not on the top.
390 if ( 'top' !== $which ) {
391 return;
392 }
393
394 // Don't output filters if no filters are defined.
395 if ( ! $this->filters ) {
396 return;
397 }
398
399 ?>
400 <div class="alignleft actions filters">
401 <?php
402 foreach ( $this->filters as $filter_key => $filter ) {
403 ?>
404 <select name="filters[<?php echo esc_attr( $filter_key ); ?>]">
405 <option value=""><?php echo esc_html( $filter['label'] ); ?></option>
406 <?php
407 foreach ( $filter['options'] as $option_key => $option_value ) {
408 ?>
409 <option value="<?php echo esc_attr( $option_key ); ?>"<?php selected( $option_key, $this->get_filter( $filter_key ) ); ?>><?php echo esc_attr( $option_value ); ?></option>
410 <?php
411 }
412 ?>
413 </select>
414 <?php
415 }
416
417 submit_button( __( 'Filter', 'convertkit' ), '', 'filter_action', false );
418 ?>
419 </div>
420 <?php
421
422 }
423
424 /**
425 * Reorder the data according to the sort parameters
426 *
427 * @param array $data Row data, unsorted.
428 * @param string $order_by_default Default order by.
429 * @param string $order_default Default order direction.
430 *
431 * @return array Row data, sorted
432 */
433 public function reorder( $data, $order_by_default = 'title', $order_default = 'asc' ) {
434
435 usort(
436 $data,
437 function ( $a, $b ) use ( $order_by_default, $order_default ) {
438 // Get order by and order.
439 $orderby = $this->get_order_by( $order_by_default );
440 $order = $this->get_order( $order_default );
441
442 $result = strcmp( $a[ $orderby ], $b[ $orderby ] ); // Determine sort order.
443 return ( 'asc' === $order ) ? $result : -$result; // Send final sort direction to usort.
444
445 }
446 );
447
448 return $data;
449
450 }
451
452 /**
453 * Returns whether a search has been performed on the table.
454 *
455 * @since 3.0.0
456 *
457 * @return bool Search has been performed.
458 */
459 public function is_search() {
460
461 return filter_has_var( INPUT_GET, 's' );
462
463 }
464
465 /**
466 * Get the Search requested by the user
467 *
468 * @since 3.0.0
469 *
470 * @return string
471 */
472 public function get_search() {
473
474 // Bail if nonce is not valid.
475 if ( ! isset( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $_REQUEST['_wpnonce'] ) ), 'bulk-' . $this->_args['plural'] ) ) {
476 return '';
477 }
478
479 if ( ! array_key_exists( 's', $_REQUEST ) ) {
480 return '';
481 }
482
483 return urldecode( sanitize_text_field( wp_unslash( $_REQUEST['s'] ) ) );
484
485 }
486
487 /**
488 * Get the filter requested by the user
489 *
490 * @since 3.0.1
491 *
492 * @param string $key Filter key.
493 * @return string
494 */
495 public function get_filter( $key ) {
496
497 // Bail if nonce is not valid.
498 if ( ! isset( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $_REQUEST['_wpnonce'] ) ), 'bulk-' . $this->_args['plural'] ) ) {
499 return '';
500 }
501
502 if ( ! array_key_exists( 'filters', $_REQUEST ) || ! array_key_exists( $key, $_REQUEST['filters'] ) ) {
503 return '';
504 }
505
506 return urldecode( sanitize_text_field( wp_unslash( $_REQUEST['filters'][ $key ] ) ) );
507
508 }
509
510 /**
511 * Get the Order By requested by the user
512 *
513 * @since 3.0.0
514 *
515 * @param string $default_order_by Default order by.
516 * @return string
517 */
518 public function get_order_by( $default_order_by = 'title' ) {
519
520 // Don't nonce check because order by may not include a nonce if no search performed.
521 if ( ! filter_has_var( INPUT_GET, 'orderby' ) ) {
522 return $default_order_by;
523 }
524
525 return sanitize_sql_orderby( filter_input( INPUT_GET, 'orderby', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) );
526
527 }
528
529 /**
530 * Get the Order requested by the user
531 *
532 * @since 3.0.0
533 *
534 * @param string $default_order Default order.
535 * @return string
536 */
537 public function get_order( $default_order = 'DESC' ) {
538
539 // Don't nonce check because order may not include a nonce if no search performed.
540 if ( ! filter_has_var( INPUT_GET, 'order' ) ) {
541 return $default_order;
542 }
543
544 return filter_input( INPUT_GET, 'order', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
545
546 }
547
548 }
549