PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 1.3.8
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v1.3.8
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 / lib / multi_value_field_table.php

multi_value_field_table.php in Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages 1.3.8, at lib/multi_value_field_table.php

143 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if(!class_exists('WP_List_Table')){
4 require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
5 }
6
7 class MultiValueFieldTable extends WP_List_Table {
8 private $_bulk_actions = array();
9 private $_columns = array();
10 private $_sortable_columns = array();
11 private $_data = array();
12
13 function __construct() {
14 global $status, $page;
15
16 parent::__construct(array(
17 'singular' => 'item',
18 'plural' => 'items',
19 'ajax' => false
20 ));
21 }
22
23 /**
24 * Set default column attributes
25 *
26 * @param array $item A singular item (one full row's worth of data)
27 * @param array $column_name The name/slug of the column to be processed
28 * @return string Text or HTML to be placed inside the column <td>
29 */
30 public function column_default($item, $column_name) {
31 return $item[$column_name];
32 }
33
34 /**
35 * Provide a callback function to render the checkbox column
36 *
37 * @param array $item A row's worth of data
38 * @return string The formatted string with a checkbox
39 */
40 public function column_cb($item) {
41 return sprintf(
42 '<input type="checkbox" name="%1$s[]" value="%2$s" />',
43 $this->_args['singular'],
44 $item['id']
45 );
46 }
47
48 /**
49 * Get the bulk actions for this table
50 *
51 * @return array Bulk actions
52 */
53 public function get_bulk_actions() {
54 return $this->_bulk_actions;
55 }
56
57 /**
58 * Get a list of columns
59 *
60 * @return array
61 */
62 public function get_columns() {
63 return $this->_columns;
64 }
65
66 /**
67 * Add a column to the table
68 *
69 * @param string $key Machine-readable column name
70 * @param string $title Title shown to the user
71 * @param boolean $sortable Whether or not this is sortable (defaults false)
72 */
73 public function add_column($key, $title, $sortable = false) {
74 $this->_columns[$key] = $title;
75
76 if ($sortable) $this->_sortable_columns[$key] = array($key, false);
77 }
78
79 /**
80 * Add an item (row) to the table
81 *
82 * @param array $item A row's worth of data
83 */
84 public function add_item($item) {
85 array_push($this->_data, $item);
86 }
87
88 /**
89 * Add a bulk action to the table
90 *
91 * @param string $key Machine-readable action name
92 * @param string $name Title shown to the user
93 */
94 public function add_bulk_action($key, $name) {
95 $this->_bulk_actions[$key] = $name;
96 }
97
98 /**
99 * Prepares the items (rows) to be rendered
100 */
101 public function prepare_items() {
102 $total_items = count($this->_data);
103 $per_page = 25;
104
105 $columns = $this->_columns;
106 $hidden = array();
107 $sortable = $this->_sortable_columns;
108
109 $this->_column_headers = array($columns, $hidden, $sortable);
110
111 $current_page = $this->get_pagenum();
112
113 $sorted_data = $this->reorder($this->_data);
114
115 $data = array_slice($sorted_data, (($current_page-1)*$per_page),$per_page);
116
117 $this->items = $data;
118
119 $this->set_pagination_args(array(
120 'total_items' => $total_items,
121 'per_page' => $per_page,
122 'total_pages' => ceil($total_items/$per_page)
123 ));
124 }
125
126 /**
127 * Reorder the data according to the sort parameters
128 *
129 * @return array Row data, sorted
130 */
131 public function reorder($data) {
132 function usort_reorder($a, $b) {
133 $orderby = (!empty($_REQUEST['orderby'])) ? $_REQUEST['orderby'] : 'title'; //If no sort, default to title
134 $order = (!empty($_REQUEST['order'])) ? $_REQUEST['order'] : 'asc'; //If no order, default to asc
135 $result = strcmp($a[$orderby], $b[$orderby]); //Determine sort order
136 return ($order==='asc') ? $result : -$result; //Send final sort direction to usort
137 }
138 usort($data, 'usort_reorder');
139
140 return $data;
141 }
142 }
143