Skip to content

Commit a15d2ac

Browse files
authored
Merge pull request #888 from Kit/multi-value-table-refactor
Rename `Multi_Value_Table` class to `ConvertKit_WP_List_Table`
2 parents ff1e9c3 + dd5d433 commit a15d2ac

15 files changed

+1686
-327
lines changed
Lines changed: 324 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,324 @@
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 supported bulk actions.
27+
*
28+
* @var array
29+
*/
30+
private $bulk_actions = array();
31+
32+
/**
33+
* Holds the table columns.
34+
*
35+
* @var array
36+
*/
37+
private $columns = array();
38+
39+
/**
40+
* Holds the sortable table columns.
41+
*
42+
* @var array
43+
*/
44+
private $sortable_columns = array();
45+
46+
/**
47+
* Holds the total number of items in the table.
48+
*
49+
* @since 3.0.0
50+
*
51+
* @var int
52+
*/
53+
private $total_items = 0;
54+
55+
/**
56+
* Constructor.
57+
*
58+
* @since 1.0.0
59+
*/
60+
public function __construct() {
61+
62+
parent::__construct(
63+
array(
64+
'singular' => 'item',
65+
'plural' => 'items',
66+
'ajax' => false,
67+
)
68+
);
69+
70+
}
71+
72+
/**
73+
* Set default column attributes
74+
*
75+
* @since 1.0.0
76+
*
77+
* @param array $item A singular item (one full row's worth of data).
78+
* @param string $column_name The name/slug of the column to be processed.
79+
* @return string Text or HTML to be placed inside the column <td>
80+
*/
81+
public function column_default( $item, $column_name ) {
82+
83+
return $item[ $column_name ];
84+
85+
}
86+
87+
/**
88+
* Provide a callback function to render the checkbox column
89+
*
90+
* @param array $item A row's worth of data.
91+
* @return string The formatted string with a checkbox
92+
*/
93+
public function column_cb( $item ) {
94+
95+
return sprintf(
96+
'<input type="checkbox" name="%1$s[]" value="%2$s" />',
97+
$this->_args['singular'],
98+
$item['id']
99+
);
100+
101+
}
102+
103+
/**
104+
* Get the bulk actions for this table
105+
*
106+
* @return array Bulk actions
107+
*/
108+
public function get_bulk_actions() {
109+
110+
return $this->bulk_actions;
111+
112+
}
113+
114+
/**
115+
* Get a list of columns
116+
*
117+
* @return array
118+
*/
119+
public function get_columns() {
120+
121+
return $this->columns;
122+
123+
}
124+
125+
/**
126+
* Add a column to the table
127+
*
128+
* @param string $key Machine-readable column name.
129+
* @param string $title Title shown to the user.
130+
* @param boolean $sortable Whether or not this is sortable (defaults false).
131+
*/
132+
public function add_column( $key, $title, $sortable = false ) {
133+
134+
$this->columns[ $key ] = $title;
135+
136+
if ( $sortable ) {
137+
$this->sortable_columns[ $key ] = array( $key, false );
138+
}
139+
140+
}
141+
142+
/**
143+
* Add an item (row) to the table
144+
*
145+
* @param array $item A row's worth of data.
146+
*/
147+
public function add_item( $item ) {
148+
149+
array_push( $this->items, $item );
150+
151+
}
152+
153+
/**
154+
* Add multiple items to the table
155+
*
156+
* @since 3.0.0
157+
*
158+
* @param array $items Table rows.
159+
*/
160+
public function add_items( $items ) {
161+
162+
$this->items = $items;
163+
164+
}
165+
166+
/**
167+
* Set the total number of items available, which may
168+
* be greater than the number of items displayed.
169+
*
170+
* @since 3.0.0
171+
*
172+
* @param int $total_items Total number of items.
173+
*/
174+
public function set_total_items( $total_items ) {
175+
176+
$this->total_items = $total_items;
177+
178+
}
179+
180+
/**
181+
* Get the total number of items available, which may
182+
* be greater than the number of items displayed.
183+
*
184+
* @since 3.0.0
185+
*
186+
* @return int Total number of items.
187+
*/
188+
public function get_total_items() {
189+
190+
if ( $this->total_items ) {
191+
return $this->total_items;
192+
}
193+
194+
return count( $this->items );
195+
196+
}
197+
198+
/**
199+
* Add a bulk action to the table
200+
*
201+
* @param string $key Machine-readable action name.
202+
* @param string $name Title shown to the user.
203+
*/
204+
public function add_bulk_action( $key, $name ) {
205+
206+
$this->bulk_actions[ $key ] = $name;
207+
208+
}
209+
210+
/**
211+
* Define table columns and pagination for this WP_List_Table.
212+
*
213+
* @since 3.0.0
214+
*/
215+
public function prepare_items() {
216+
217+
// Set column headers.
218+
// If this isn't done, the table will not display.
219+
$this->_column_headers = array( $this->columns, array(), $this->sortable_columns );
220+
221+
}
222+
223+
/**
224+
* Reorder the data according to the sort parameters
225+
*
226+
* @param array $data Row data, unsorted.
227+
* @param string $order_by_default Default order by.
228+
* @param string $order_default Default order direction.
229+
*
230+
* @return array Row data, sorted
231+
*/
232+
public function reorder( $data, $order_by_default = 'title', $order_default = 'asc' ) {
233+
234+
usort(
235+
$data,
236+
function ( $a, $b ) use ( $order_by_default, $order_default ) {
237+
// Get order by and order.
238+
$orderby = $this->get_order_by( $order_by_default );
239+
$order = $this->get_order( $order_default );
240+
241+
$result = strcmp( $a[ $orderby ], $b[ $orderby ] ); // Determine sort order.
242+
return ( 'asc' === $order ) ? $result : -$result; // Send final sort direction to usort.
243+
244+
}
245+
);
246+
247+
return $data;
248+
249+
}
250+
251+
/**
252+
* Returns whether a search has been performed on the table.
253+
*
254+
* @since 3.0.0
255+
*
256+
* @return bool Search has been performed.
257+
*/
258+
public function is_search() {
259+
260+
return filter_has_var( INPUT_GET, 's' );
261+
262+
}
263+
264+
/**
265+
* Get the Search requested by the user
266+
*
267+
* @since 3.0.0
268+
*
269+
* @return string
270+
*/
271+
public function get_search() {
272+
273+
// Bail if nonce is not valid.
274+
if ( ! isset( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $_REQUEST['_wpnonce'] ) ), 'bulk-wp-to-social-log' ) ) {
275+
return '';
276+
}
277+
278+
if ( ! array_key_exists( 's', $_REQUEST ) ) {
279+
return '';
280+
}
281+
282+
return urldecode( sanitize_text_field( wp_unslash( $_REQUEST['s'] ) ) );
283+
284+
}
285+
286+
/**
287+
* Get the Order By requested by the user
288+
*
289+
* @since 3.0.0
290+
*
291+
* @param string $default_order_by Default order by.
292+
* @return string
293+
*/
294+
public function get_order_by( $default_order_by = 'title' ) {
295+
296+
// Don't nonce check because order by may not include a nonce if no search performed.
297+
if ( ! filter_has_var( INPUT_GET, 'orderby' ) ) {
298+
return $default_order_by;
299+
}
300+
301+
return sanitize_sql_orderby( filter_input( INPUT_GET, 'orderby', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) );
302+
303+
}
304+
305+
/**
306+
* Get the Order requested by the user
307+
*
308+
* @since 3.0.0
309+
*
310+
* @param string $default_order Default order.
311+
* @return string
312+
*/
313+
public function get_order( $default_order = 'DESC' ) {
314+
315+
// Don't nonce check because order may not include a nonce if no search performed.
316+
if ( ! filter_has_var( INPUT_GET, 'order' ) ) {
317+
return $default_order;
318+
}
319+
320+
return filter_input( INPUT_GET, 'order', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
321+
322+
}
323+
324+
}

0 commit comments

Comments
 (0)