| 1 |
<?php |
| 2 |
|
| 3 |
// Do not allow the file to be called directly. |
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
// WP_List_Table is not loaded automatically so we need to load it in our application |
| 9 |
if ( ! class_exists( 'WP_List_Table' ) ) { |
| 10 |
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Create a new table class that will extend the WP_List_Table. |
| 15 |
*/ |
| 16 |
class Patchstack_Network_Sites_Table extends WP_List_Table { |
| 17 |
|
| 18 |
/** |
| 19 |
* Prepare the items for the table to process |
| 20 |
* |
| 21 |
* @return Void |
| 22 |
*/ |
| 23 |
public function prepare_items() { |
| 24 |
$columns = $this->get_columns(); |
| 25 |
$hidden = $this->get_hidden_columns(); |
| 26 |
$sortable = $this->get_sortable_columns(); |
| 27 |
$data = $this->table_data(); |
| 28 |
|
| 29 |
usort( $data, array( &$this, 'sort_data' ) ); |
| 30 |
$per_page = 10; |
| 31 |
$current_page = $this->get_pagenum(); |
| 32 |
$total_items = count( $data ); |
| 33 |
|
| 34 |
$this->set_pagination_args( |
| 35 |
array( |
| 36 |
'total_items' => $total_items, |
| 37 |
'per_page' => $per_page, |
| 38 |
) |
| 39 |
); |
| 40 |
|
| 41 |
$data = array_slice( $data, ( ( $current_page - 1 ) * $per_page ), $per_page ); |
| 42 |
$this->_column_headers = array( $columns, $hidden, $sortable ); |
| 43 |
$this->items = $data; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Override the parent columns method. Defines the columns to use in your listing table |
| 48 |
* |
| 49 |
* @return Array |
| 50 |
*/ |
| 51 |
public function get_columns() { |
| 52 |
return array( |
| 53 |
'id' => 'ID', |
| 54 |
'title' => 'Title', |
| 55 |
'url' => 'URL', |
| 56 |
'activated' => 'License Status', |
| 57 |
'firewall_status' => 'Firewall Status', |
| 58 |
'edit' => 'Manage', |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Define which columns are hidden |
| 64 |
* |
| 65 |
* @return Array |
| 66 |
*/ |
| 67 |
public function get_hidden_columns() { |
| 68 |
return array(); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Define the sortable columns |
| 73 |
* |
| 74 |
* @return Array |
| 75 |
*/ |
| 76 |
public function get_sortable_columns() { |
| 77 |
return array( 'title' => array( 'title', false ) ); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Get the table data |
| 82 |
* |
| 83 |
* @return Array |
| 84 |
*/ |
| 85 |
private function table_data() { |
| 86 |
$data = array(); |
| 87 |
|
| 88 |
$blogs_ids = get_sites(); |
| 89 |
foreach ( $blogs_ids as $b ) { |
| 90 |
$site_info = get_blog_details( $b->blog_id ); |
| 91 |
|
| 92 |
// Search functionality |
| 93 |
$match = false; |
| 94 |
if ( isset( $_GET['s'] ) ) { |
| 95 |
if ( strpos( $b->blog_id, $_GET['s'] ) ) { |
| 96 |
$match = true; |
| 97 |
} |
| 98 |
|
| 99 |
if ( strpos( $b->blogname, $_GET['s'] ) ) { |
| 100 |
$match = true; |
| 101 |
} |
| 102 |
|
| 103 |
if ( strpos( $site_info->siteurl, $_GET['s'] ) ) { |
| 104 |
$match = true; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
if ( ! isset( $_GET['s'] ) || $match ) { |
| 109 |
$is_firewall_enabled = get_blog_option( $b->blog_id, 'patchstack_basic_firewall' ); |
| 110 |
$is_activated = get_blog_option( $b->blog_id, 'patchstack_clientid', '' ) != ''; |
| 111 |
|
| 112 |
$data[] = array( |
| 113 |
'id' => (int) $b->blog_id, |
| 114 |
'title' => esc_html( $b->blogname ), |
| 115 |
'url' => '<a href="' . esc_url( $site_info->siteurl ). '">' . esc_url( $site_info->siteurl ) . '</a>', |
| 116 |
'activated' => $is_activated ? 'Activated' : 'Deactivated', |
| 117 |
'firewall_status' => $is_firewall_enabled ? 'Enabled' : 'Disabled', |
| 118 |
'edit' => $is_activated ? '<a href="' . esc_url( get_admin_url( $b->blog_id ) ) . 'options-general.php?page=patchstack">Edit Settings</a>' : '', |
| 119 |
); |
| 120 |
} |
| 121 |
} |
| 122 |
return $data; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Define what data to show on each column of the table |
| 127 |
* |
| 128 |
* @param Array $item Data |
| 129 |
* @param String $column_name - Current column name |
| 130 |
* |
| 131 |
* @return Mixed |
| 132 |
*/ |
| 133 |
public function column_default( $item, $column_name ) { |
| 134 |
switch ( $column_name ) { |
| 135 |
case 'id': |
| 136 |
case 'title': |
| 137 |
case 'url': |
| 138 |
case 'activated': |
| 139 |
case 'firewall_status': |
| 140 |
case 'edit': |
| 141 |
return $item[ $column_name ]; |
| 142 |
default: |
| 143 |
return print_r( $item, true ); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Allows you to sort the data by the variables set in the $_GET |
| 149 |
* |
| 150 |
* @param Array $a |
| 151 |
* @param Array $b |
| 152 |
* @return Mixed |
| 153 |
*/ |
| 154 |
private function sort_data( $a, $b ) { |
| 155 |
// Set defaults |
| 156 |
$orderby = 'id'; |
| 157 |
$order = 'asc'; |
| 158 |
$columns = $this->get_columns(); |
| 159 |
|
| 160 |
// If orderby is set, use this as the sort column |
| 161 |
if ( isset( $_GET['orderby'] ) && ! empty( $_GET['orderby'] ) && isset( $columns[$_GET['orderby']] ) ) { |
| 162 |
$orderby = wp_filter_nohtml_kses( $_GET['orderby'] ); |
| 163 |
} |
| 164 |
|
| 165 |
// If order is set use this as the order |
| 166 |
if ( isset( $_GET['order'] ) && ! empty( $_GET['order'] ) && $_GET['order'] != 'asc' ) { |
| 167 |
$order = 'desc'; |
| 168 |
} |
| 169 |
|
| 170 |
$result = strcmp( $a[ $orderby ], $b[ $orderby ] ); |
| 171 |
if ( $order === 'asc' ) { |
| 172 |
return $result; |
| 173 |
} |
| 174 |
|
| 175 |
return -$result; |
| 176 |
} |
| 177 |
} |
| 178 |
|