'item',
'plural' => 'items',
'ajax' => false,
)
);
}
/**
* Set default column attributes
*
* @since 1.0.0
*
* @param array $item A singular item (one full row's worth of data).
* @param string $column_name The name/slug of the column to be processed.
* @return string Text or HTML to be placed inside the column
*/
public function column_default( $item, $column_name ) {
return $item[ $column_name ];
}
/**
* Provide a callback function to render the checkbox column
*
* @param array $item A row's worth of data.
* @return string The formatted string with a checkbox
*/
public function column_cb( $item ) {
return sprintf(
'',
$this->_args['singular'],
$item['id']
);
}
/**
* Get the bulk actions for this table
*
* @return array Bulk actions
*/
public function get_bulk_actions() {
return $this->bulk_actions;
}
/**
* Get a list of columns
*
* @return array
*/
public function get_columns() {
return $this->columns;
}
/**
* Add a column to the table
*
* @param string $key Machine-readable column name.
* @param string $title Title shown to the user.
* @param boolean $sortable Whether or not this is sortable (defaults false).
*/
public function add_column( $key, $title, $sortable = false ) {
$this->columns[ $key ] = $title;
if ( $sortable ) {
$this->sortable_columns[ $key ] = array( $key, false );
}
}
/**
* Add an item (row) to the table
*
* @param array $item A row's worth of data.
*/
public function add_item( $item ) {
array_push( $this->data, $item );
}
/**
* Add a bulk action to the table
*
* @param string $key Machine-readable action name.
* @param string $name Title shown to the user.
*/
public function add_bulk_action( $key, $name ) {
$this->bulk_actions[ $key ] = $name;
}
/**
* Prepares the items (rows) to be rendered
*/
public function prepare_items() {
$total_items = count( $this->data );
$per_page = 25;
$columns = $this->columns;
$hidden = array();
$sortable = $this->sortable_columns;
$this->_column_headers = array( $columns, $hidden, $sortable );
$current_page = $this->get_pagenum();
$sorted_data = $this->reorder( $this->data );
$data = array_slice( $sorted_data, ( ( $current_page - 1 ) * $per_page ), $per_page );
$this->items = $data;
$this->set_pagination_args(
array(
'total_items' => $total_items,
'per_page' => $per_page,
'total_pages' => (int) ceil( $total_items / $per_page ),
)
);
}
/**
* Reorder the data according to the sort parameters
*
* @param array $data Row data, unsorted.
* @return array Row data, sorted
*/
public function reorder( $data ) {
usort(
$data,
function ( $a, $b ) {
if ( empty( $_REQUEST['orderby'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
$orderby = 'title';
} else {
$orderby = sanitize_sql_orderby( wp_unslash( $_REQUEST['orderby'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
}
if ( empty( $_REQUEST['order'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
$order = 'asc';
} else {
$order = sanitize_text_field( wp_unslash( $_REQUEST['order'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
}
$result = strcmp( $a[ $orderby ], $b[ $orderby ] ); // Determine sort order.
return ( 'asc' === $order ) ? $result : -$result; // Send final sort direction to usort.
}
);
return $data;
}
/**
* Display the table without the nonce at the top.
*
* @since 3.1.0
* @access public
*/
public function display_no_nonce() {
$singular = $this->_args['singular'];
$this->display_tablenav( 'bottom' );
$this->screen->render_screen_reader_content( 'heading_list' );
?>