get_columns();
$hidden = $this->get_hidden_columns();
$sortable = $this->get_sortable_columns();
$data = $this->table_data();
usort( $data, [ &$this, 'sort_data' ] );
$per_page = 10;
$current_page = $this->get_pagenum();
$total_items = count( $data );
$this->set_pagination_args(
[
'total_items' => $total_items,
'per_page' => $per_page,
]
);
$data = array_slice( $data, ( ( $current_page - 1 ) * $per_page ), $per_page );
$this->_column_headers = [ $columns, $hidden, $sortable ];
$this->items = $data;
}
/**
* Override the parent columns method. Defines the columns to use in your listing table
*
* @return Array
*/
public function get_columns() {
return [
'id' => 'ID',
'title' => 'Title',
'url' => 'URL',
'activated' => 'License Status',
'firewall_status' => 'Firewall Status',
'migration' => 'Rerun Migration',
'edit' => 'Manage',
];
}
/**
* Define which columns are hidden
*
* @return Array
*/
public function get_hidden_columns() {
return [];
}
/**
* Define the sortable columns
*
* @return Array
*/
public function get_sortable_columns() {
return [ 'title' => [ 'title', false ] ];
}
/**
* Get the table data
*
* @return Array
*/
private function table_data() {
global $wpdb;
$data = [];
$free = get_option( 'patchstack_license_free', 0 ) == 1;
$nonce = wp_create_nonce( 'patchstack-migration' );
$blogs_ids = get_sites();
foreach ( $blogs_ids as $b ) {
$site_info = get_blog_details( $b->blog_id );
// Search functionality
$match = false;
if ( isset( $_GET['s'] ) ) {
if ( strpos( $b->blog_id, $_GET['s'] ) ) {
$match = true;
}
if ( strpos( $b->blogname, $_GET['s'] ) ) {
$match = true;
}
if ( strpos( $site_info->siteurl, $_GET['s'] ) ) {
$match = true;
}
}
if ( ! isset( $_GET['s'] ) || $match ) {
$is_firewall_enabled = get_blog_option( $b->blog_id, 'patchstack_basic_firewall' );
$is_activated = get_blog_option( $b->blog_id, 'patchstack_clientid', '' ) != '';
$prefix = $wpdb->get_blog_prefix( $b->blog_id );
// Determine if the site has any missing migrations.
$has_missing = false;
if ($is_activated) {
foreach( [ 'patchstack_firewall_log', 'patchstack_logic', 'patchstack_event_log' ] as $table ) {
$result = $wpdb->get_results("SHOW TABLES LIKE '" . $prefix . $table . "'");
if ( !$result ) {
$has_missing = true;
}
}
}
$data[] = [
'id' => (int) $b->blog_id,
'title' => esc_html( $b->blogname ),
'url' => '' . esc_url( $site_info->siteurl ) . '',
'activated' => $is_activated ? 'Activated' : 'Deactivated',
'firewall_status' => $is_firewall_enabled && ! $free ? 'Enabled' : 'Disabled',
'edit' => $is_activated ? 'Edit Settings' : '',
'migration' => '' . ($has_missing ? 'Run' : 'Rerun') . ' Database Migration',
];
}
}
return $data;
}
/**
* Define what data to show on each column of the table
*
* @param Array $item Data
* @param String $column_name - Current column name
*
* @return Mixed
*/
public function column_default( $item, $column_name ) {
switch ( $column_name ) {
case 'id':
case 'title':
case 'url':
case 'activated':
case 'firewall_status':
case 'migration':
case 'edit':
return $item[ $column_name ];
default:
return print_r( $item, true );
}
}
/**
* Allows you to sort the data by the variables set in the $_GET
*
* @param Array $a
* @param Array $b
* @return Mixed
*/
private function sort_data( $a, $b ) {
// Set defaults
$orderby = 'id';
$order = 'asc';
$columns = $this->get_columns();
// If orderby is set, use this as the sort column
if ( isset( $_GET['orderby'] ) && ! empty( $_GET['orderby'] ) && isset( $columns[$_GET['orderby']] ) ) {
$orderby = wp_filter_nohtml_kses( $_GET['orderby'] );
}
// If order is set use this as the order
if ( isset( $_GET['order'] ) && ! empty( $_GET['order'] ) && $_GET['order'] != 'asc' ) {
$order = 'desc';
}
$result = strcmp( $a[ $orderby ], $b[ $orderby ] );
if ( $order === 'asc' ) {
return $result;
}
return -$result;
}
}