| 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 |
private $_bulk_actions = array(); |
| 21 |
private $_columns = array(); |
| 22 |
private $_sortable_columns = array(); |
| 23 |
private $_data = array(); |
| 24 |
|
| 25 |
function __construct() { |
| 26 |
global $status, $page; |
| 27 |
|
| 28 |
parent::__construct( array( |
| 29 |
'singular' => 'item', |
| 30 |
'plural' => 'items', |
| 31 |
'ajax' => false, |
| 32 |
) ); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Set default column attributes |
| 37 |
* |
| 38 |
* @param array $item A singular item (one full row's worth of data). |
| 39 |
* @param array $column_name The name/slug of the column to be processed. |
| 40 |
* @return string Text or HTML to be placed inside the column <td> |
| 41 |
*/ |
| 42 |
public function column_default( $item, $column_name ) { |
| 43 |
return $item[ $column_name ]; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Provide a callback function to render the checkbox column |
| 48 |
* |
| 49 |
* @param array $item A row's worth of data. |
| 50 |
* @return string The formatted string with a checkbox |
| 51 |
*/ |
| 52 |
public function column_cb( $item ) { |
| 53 |
return sprintf( |
| 54 |
'<input type="checkbox" name="%1$s[]" value="%2$s" />', |
| 55 |
$this->_args['singular'], |
| 56 |
$item['id'] |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Get the bulk actions for this table |
| 62 |
* |
| 63 |
* @return array Bulk actions |
| 64 |
*/ |
| 65 |
public function get_bulk_actions() { |
| 66 |
return $this->_bulk_actions; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Get a list of columns |
| 71 |
* |
| 72 |
* @return array |
| 73 |
*/ |
| 74 |
public function get_columns() { |
| 75 |
return $this->_columns; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Add a column to the table |
| 80 |
* |
| 81 |
* @param string $key Machine-readable column name. |
| 82 |
* @param string $title Title shown to the user. |
| 83 |
* @param boolean $sortable Whether or not this is sortable (defaults false) |
| 84 |
*/ |
| 85 |
public function add_column( $key, $title, $sortable = false ) { |
| 86 |
$this->_columns[ $key ] = $title; |
| 87 |
|
| 88 |
if ( $sortable ) { |
| 89 |
$this->_sortable_columns[ $key ] = array( $key, false ); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Add an item (row) to the table |
| 95 |
* |
| 96 |
* @param array $item A row's worth of data. |
| 97 |
*/ |
| 98 |
public function add_item( $item ) { |
| 99 |
array_push( $this->_data, $item ); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Add a bulk action to the table |
| 104 |
* |
| 105 |
* @param string $key Machine-readable action name |
| 106 |
* @param string $name Title shown to the user |
| 107 |
*/ |
| 108 |
public function add_bulk_action( $key, $name ) { |
| 109 |
$this->_bulk_actions[ $key ] = $name; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Prepares the items (rows) to be rendered |
| 114 |
*/ |
| 115 |
public function prepare_items() { |
| 116 |
$total_items = count( $this->_data ); |
| 117 |
$per_page = 25; |
| 118 |
|
| 119 |
$columns = $this->_columns; |
| 120 |
$hidden = array(); |
| 121 |
$sortable = $this->_sortable_columns; |
| 122 |
|
| 123 |
$this->_column_headers = array( $columns, $hidden, $sortable ); |
| 124 |
|
| 125 |
$current_page = $this->get_pagenum(); |
| 126 |
|
| 127 |
$sorted_data = $this->reorder( $this->_data ); |
| 128 |
|
| 129 |
$data = array_slice( $sorted_data, ( ( $current_page - 1 ) * $per_page ),$per_page ); |
| 130 |
|
| 131 |
$this->items = $data; |
| 132 |
|
| 133 |
$this->set_pagination_args( array( |
| 134 |
'total_items' => $total_items, |
| 135 |
'per_page' => $per_page, |
| 136 |
'total_pages' => ceil( $total_items / $per_page ), |
| 137 |
)); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Reorder the data according to the sort parameters |
| 142 |
* |
| 143 |
* @return array Row data, sorted |
| 144 |
*/ |
| 145 |
public function reorder( $data ) { |
| 146 |
function usort_reorder( $a, $b ) { |
| 147 |
|
| 148 |
if ( empty( $_REQUEST['orderby'] ) ) { // WPCS: CSRF ok. |
| 149 |
$orderby = 'title'; |
| 150 |
} else { |
| 151 |
$orderby = sanitize_text_field( wp_unslash( $_REQUEST['orderby'] ) ); // WPCS: CSRF ok. |
| 152 |
} |
| 153 |
|
| 154 |
if ( empty( $_REQUEST['order'] ) ) { // WPCS: CSRF ok. |
| 155 |
$order = 'asc'; |
| 156 |
} else { |
| 157 |
$order = sanitize_text_field( wp_unslash( $_REQUEST['order'] ) ); // WPCS: CSRF ok. |
| 158 |
} |
| 159 |
$result = strcmp( $a[ $orderby ], $b[ $orderby ] ); //Determine sort order. |
| 160 |
return ( 'asc' === $order ) ? $result : -$result; //Send final sort direction to usort. |
| 161 |
} |
| 162 |
usort( $data, 'usort_reorder' ); |
| 163 |
|
| 164 |
return $data; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
/** |
| 169 |
* Display the table without the nonce at the top. |
| 170 |
* |
| 171 |
* @since 3.1.0 |
| 172 |
* @access public |
| 173 |
*/ |
| 174 |
public function display_no_nonce() { |
| 175 |
$singular = $this->_args['singular']; |
| 176 |
|
| 177 |
$this->display_tablenav( 'bottom' ); |
| 178 |
|
| 179 |
$this->screen->render_screen_reader_content( 'heading_list' ); |
| 180 |
?> |
| 181 |
<table class="wp-list-table <?php echo implode( ' ', $this->get_table_classes() ); ?>"> |
| 182 |
<thead> |
| 183 |
<tr> |
| 184 |
<?php $this->print_column_headers(); ?> |
| 185 |
</tr> |
| 186 |
</thead> |
| 187 |
|
| 188 |
<tbody id="the-list"<?php |
| 189 |
if ( $singular ) { |
| 190 |
echo " data-wp-lists='list:$singular'"; |
| 191 |
} ?>> |
| 192 |
<?php $this->display_rows_or_placeholder(); ?> |
| 193 |
</tbody> |
| 194 |
|
| 195 |
<tfoot> |
| 196 |
<tr> |
| 197 |
<?php $this->print_column_headers( false ); ?> |
| 198 |
</tr> |
| 199 |
</tfoot> |
| 200 |
|
| 201 |
</table> |
| 202 |
<?php |
| 203 |
$this->display_tablenav( 'bottom' ); |
| 204 |
} |
| 205 |
} |
| 206 |
|