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

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