| 1 |
<?php |
| 2 |
/** |
| 3 |
* ConvertKit General Settings 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 |
* Class Multi_Value_Field_Table |
| 18 |
*/ |
| 19 |
class Multi_Value_Field_Table extends WP_List_Table { |
| 20 |
|
| 21 |
/** |
| 22 |
* Holds the supported bulk actions. |
| 23 |
* |
| 24 |
* @var array |
| 25 |
*/ |
| 26 |
private $bulk_actions = array(); |
| 27 |
|
| 28 |
/** |
| 29 |
* Holds the table columns. |
| 30 |
* |
| 31 |
* @var array |
| 32 |
*/ |
| 33 |
private $columns = array(); |
| 34 |
|
| 35 |
/** |
| 36 |
* Holds the sortable table columns. |
| 37 |
* |
| 38 |
* @var array |
| 39 |
*/ |
| 40 |
private $sortable_columns = array(); |
| 41 |
|
| 42 |
/** |
| 43 |
* Holds the table rows and their data. |
| 44 |
* |
| 45 |
* @var array |
| 46 |
*/ |
| 47 |
private $data = array(); |
| 48 |
|
| 49 |
/** |
| 50 |
* Constructor. |
| 51 |
* |
| 52 |
* @since 1.0.0 |
| 53 |
*/ |
| 54 |
public function __construct() { |
| 55 |
|
| 56 |
parent::__construct( |
| 57 |
array( |
| 58 |
'singular' => 'item', |
| 59 |
'plural' => 'items', |
| 60 |
'ajax' => false, |
| 61 |
) |
| 62 |
); |
| 63 |
|
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Set default column attributes |
| 68 |
* |
| 69 |
* @since 1.0.0 |
| 70 |
* |
| 71 |
* @param array $item A singular item (one full row's worth of data). |
| 72 |
* @param string $column_name The name/slug of the column to be processed. |
| 73 |
* @return string Text or HTML to be placed inside the column <td> |
| 74 |
*/ |
| 75 |
public function column_default( $item, $column_name ) { |
| 76 |
|
| 77 |
return $item[ $column_name ]; |
| 78 |
|
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Provide a callback function to render the checkbox column |
| 83 |
* |
| 84 |
* @param array $item A row's worth of data. |
| 85 |
* @return string The formatted string with a checkbox |
| 86 |
*/ |
| 87 |
public function column_cb( $item ) { |
| 88 |
|
| 89 |
return sprintf( |
| 90 |
'<input type="checkbox" name="%1$s[]" value="%2$s" />', |
| 91 |
$this->_args['singular'], |
| 92 |
$item['id'] |
| 93 |
); |
| 94 |
|
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Get the bulk actions for this table |
| 99 |
* |
| 100 |
* @return array Bulk actions |
| 101 |
*/ |
| 102 |
public function get_bulk_actions() { |
| 103 |
|
| 104 |
return $this->bulk_actions; |
| 105 |
|
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Get a list of columns |
| 110 |
* |
| 111 |
* @return array |
| 112 |
*/ |
| 113 |
public function get_columns() { |
| 114 |
|
| 115 |
return $this->columns; |
| 116 |
|
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Add a column to the table |
| 121 |
* |
| 122 |
* @param string $key Machine-readable column name. |
| 123 |
* @param string $title Title shown to the user. |
| 124 |
* @param boolean $sortable Whether or not this is sortable (defaults false). |
| 125 |
*/ |
| 126 |
public function add_column( $key, $title, $sortable = false ) { |
| 127 |
|
| 128 |
$this->columns[ $key ] = $title; |
| 129 |
|
| 130 |
if ( $sortable ) { |
| 131 |
$this->sortable_columns[ $key ] = array( $key, false ); |
| 132 |
} |
| 133 |
|
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Add an item (row) to the table |
| 138 |
* |
| 139 |
* @param array $item A row's worth of data. |
| 140 |
*/ |
| 141 |
public function add_item( $item ) { |
| 142 |
|
| 143 |
array_push( $this->data, $item ); |
| 144 |
|
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Add a bulk action to the table |
| 149 |
* |
| 150 |
* @param string $key Machine-readable action name. |
| 151 |
* @param string $name Title shown to the user. |
| 152 |
*/ |
| 153 |
public function add_bulk_action( $key, $name ) { |
| 154 |
|
| 155 |
$this->bulk_actions[ $key ] = $name; |
| 156 |
|
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Prepares the items (rows) to be rendered |
| 161 |
*/ |
| 162 |
public function prepare_items() { |
| 163 |
|
| 164 |
$total_items = count( $this->data ); |
| 165 |
$per_page = 25; |
| 166 |
|
| 167 |
$columns = $this->columns; |
| 168 |
$hidden = array(); |
| 169 |
$sortable = $this->sortable_columns; |
| 170 |
|
| 171 |
$this->_column_headers = array( $columns, $hidden, $sortable ); |
| 172 |
|
| 173 |
$current_page = $this->get_pagenum(); |
| 174 |
|
| 175 |
$sorted_data = $this->reorder( $this->data ); |
| 176 |
|
| 177 |
$data = array_slice( $sorted_data, ( ( $current_page - 1 ) * $per_page ), $per_page ); |
| 178 |
|
| 179 |
$this->items = $data; |
| 180 |
|
| 181 |
$this->set_pagination_args( |
| 182 |
array( |
| 183 |
'total_items' => $total_items, |
| 184 |
'per_page' => $per_page, |
| 185 |
'total_pages' => (int) ceil( $total_items / $per_page ), |
| 186 |
) |
| 187 |
); |
| 188 |
|
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Reorder the data according to the sort parameters |
| 193 |
* |
| 194 |
* @param array $data Row data, unsorted. |
| 195 |
* @return array Row data, sorted |
| 196 |
*/ |
| 197 |
public function reorder( $data ) { |
| 198 |
|
| 199 |
usort( |
| 200 |
$data, |
| 201 |
function ( $a, $b ) { |
| 202 |
|
| 203 |
if ( empty( $_REQUEST['orderby'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 204 |
$orderby = 'title'; |
| 205 |
} else { |
| 206 |
$orderby = sanitize_sql_orderby( wp_unslash( $_REQUEST['orderby'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
| 207 |
} |
| 208 |
|
| 209 |
if ( empty( $_REQUEST['order'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 210 |
$order = 'asc'; |
| 211 |
} else { |
| 212 |
$order = sanitize_text_field( wp_unslash( $_REQUEST['order'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
| 213 |
} |
| 214 |
$result = strcmp( $a[ $orderby ], $b[ $orderby ] ); // Determine sort order. |
| 215 |
return ( 'asc' === $order ) ? $result : -$result; // Send final sort direction to usort. |
| 216 |
|
| 217 |
} |
| 218 |
); |
| 219 |
|
| 220 |
return $data; |
| 221 |
|
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Display the table without the nonce at the top. |
| 226 |
* |
| 227 |
* @since 3.1.0 |
| 228 |
* @access public |
| 229 |
*/ |
| 230 |
public function display_no_nonce() { |
| 231 |
|
| 232 |
$singular = $this->_args['singular']; |
| 233 |
|
| 234 |
$this->display_tablenav( 'bottom' ); |
| 235 |
|
| 236 |
$this->screen->render_screen_reader_content( 'heading_list' ); |
| 237 |
?> |
| 238 |
<table class="wp-list-table <?php echo esc_attr( implode( ' ', $this->get_table_classes() ) ); ?>"> |
| 239 |
<thead> |
| 240 |
<tr> |
| 241 |
<?php $this->print_column_headers(); ?> |
| 242 |
</tr> |
| 243 |
</thead> |
| 244 |
|
| 245 |
<tbody id="the-list" |
| 246 |
<?php |
| 247 |
if ( $singular ) { |
| 248 |
echo " data-wp-lists='list:" . esc_attr( $singular ) . "'"; |
| 249 |
} |
| 250 |
?> |
| 251 |
> |
| 252 |
<?php $this->display_rows_or_placeholder(); ?> |
| 253 |
</tbody> |
| 254 |
|
| 255 |
<tfoot> |
| 256 |
<tr> |
| 257 |
<?php $this->print_column_headers( false ); ?> |
| 258 |
</tr> |
| 259 |
</tfoot> |
| 260 |
|
| 261 |
</table> |
| 262 |
<?php |
| 263 |
$this->display_tablenav( 'bottom' ); |
| 264 |
|
| 265 |
} |
| 266 |
} |
| 267 |
|