| 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, [ &$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 |
[ |
| 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 = [ $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 [ |
| 53 |
'id' => 'ID', |
| 54 |
'title' => 'Title', |
| 55 |
'url' => 'URL', |
| 56 |
'activated' => 'License Status', |
| 57 |
'edit' => 'Settings Page', |
| 58 |
'firewall_status' => 'Firewall Status', |
| 59 |
'migration' => 'Rerun Migration' |
| 60 |
]; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Define which columns are hidden |
| 65 |
* |
| 66 |
* @return Array |
| 67 |
*/ |
| 68 |
public function get_hidden_columns() { |
| 69 |
return []; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Define the sortable columns |
| 74 |
* |
| 75 |
* @return Array |
| 76 |
*/ |
| 77 |
public function get_sortable_columns() { |
| 78 |
return [ 'title' => [ 'title', false ] ]; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Get the table data |
| 83 |
* |
| 84 |
* @return Array |
| 85 |
*/ |
| 86 |
private function table_data() { |
| 87 |
global $wpdb; |
| 88 |
$data = []; |
| 89 |
$free = get_option( 'patchstack_license_free', 0 ) == 1; |
| 90 |
$nonce = wp_create_nonce( 'patchstack-migration' ); |
| 91 |
|
| 92 |
$blogs_ids = get_sites(); |
| 93 |
foreach ( $blogs_ids as $b ) { |
| 94 |
$site_info = get_blog_details( $b->blog_id ); |
| 95 |
|
| 96 |
// Search functionality |
| 97 |
$match = false; |
| 98 |
if ( isset( $_GET['s'] ) ) { |
| 99 |
if ( strpos( $b->blog_id, $_GET['s'] ) ) { |
| 100 |
$match = true; |
| 101 |
} |
| 102 |
|
| 103 |
if ( strpos( $b->blogname, $_GET['s'] ) ) { |
| 104 |
$match = true; |
| 105 |
} |
| 106 |
|
| 107 |
if ( strpos( $site_info->siteurl, $_GET['s'] ) ) { |
| 108 |
$match = true; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
if ( ! isset( $_GET['s'] ) || $match ) { |
| 113 |
$is_firewall_enabled = get_blog_option( $b->blog_id, 'patchstack_basic_firewall' ); |
| 114 |
$is_activated = get_blog_option( $b->blog_id, 'patchstack_clientid', '' ) != ''; |
| 115 |
$prefix = $wpdb->get_blog_prefix( $b->blog_id ); |
| 116 |
|
| 117 |
// Determine if the site has any missing migrations. |
| 118 |
$has_missing = false; |
| 119 |
if ($is_activated) { |
| 120 |
foreach( [ 'patchstack_firewall_log', 'patchstack_logic', 'patchstack_event_log' ] as $table ) { |
| 121 |
$result = $wpdb->get_results("SHOW TABLES LIKE '" . $prefix . $table . "'"); |
| 122 |
if ( !$result ) { |
| 123 |
$has_missing = true; |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
$data[] = [ |
| 129 |
'id' => (int) $b->blog_id, |
| 130 |
'title' => esc_html( $b->blogname ), |
| 131 |
'url' => '<a href="' . esc_url( $site_info->siteurl ). '">' . esc_url( $site_info->siteurl ) . '</a>', |
| 132 |
'activated' => $is_activated ? 'Activated' : 'Deactivated', |
| 133 |
'edit' => '<a href="' . esc_url( $site_info->siteurl ). '/wp-admin/options-general.php?page=patchstack">Settings Page</a>', |
| 134 |
'firewall_status' => $is_firewall_enabled && ! $free ? 'Enabled' : 'Disabled', |
| 135 |
'migration' => '<a href="' . esc_url( add_query_arg( |
| 136 |
[ |
| 137 |
'PatchstackNonce' => $nonce, |
| 138 |
'site' => $b->blog_id |
| 139 |
] |
| 140 |
) ) . '">' . ($has_missing ? 'Run' : 'Rerun') . ' Database Migration</a>', |
| 141 |
]; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
return $data; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Define what data to show on each column of the table |
| 150 |
* |
| 151 |
* @param Array $item Data |
| 152 |
* @param String $column_name - Current column name |
| 153 |
* |
| 154 |
* @return Mixed |
| 155 |
*/ |
| 156 |
public function column_default( $item, $column_name ) { |
| 157 |
switch ( $column_name ) { |
| 158 |
case 'id': |
| 159 |
case 'title': |
| 160 |
case 'url': |
| 161 |
case 'activated': |
| 162 |
case 'firewall_status': |
| 163 |
case 'migration': |
| 164 |
case 'edit': |
| 165 |
return $item[ $column_name ]; |
| 166 |
default: |
| 167 |
return print_r( $item, true ); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Allows you to sort the data by the variables set in the $_GET |
| 173 |
* |
| 174 |
* @param Array $a |
| 175 |
* @param Array $b |
| 176 |
* @return Mixed |
| 177 |
*/ |
| 178 |
private function sort_data( $a, $b ) { |
| 179 |
// Set defaults |
| 180 |
$orderby = 'id'; |
| 181 |
$order = 'asc'; |
| 182 |
$columns = $this->get_columns(); |
| 183 |
|
| 184 |
// If orderby is set, use this as the sort column |
| 185 |
if ( isset( $_GET['orderby'] ) && ! empty( $_GET['orderby'] ) && isset( $columns[$_GET['orderby']] ) ) { |
| 186 |
$orderby = wp_filter_nohtml_kses( $_GET['orderby'] ); |
| 187 |
} |
| 188 |
|
| 189 |
// If order is set use this as the order |
| 190 |
if ( isset( $_GET['order'] ) && ! empty( $_GET['order'] ) && $_GET['order'] != 'asc' ) { |
| 191 |
$order = 'desc'; |
| 192 |
} |
| 193 |
|
| 194 |
$result = strcmp( $a[ $orderby ], $b[ $orderby ] ); |
| 195 |
if ( $order === 'asc' ) { |
| 196 |
return $result; |
| 197 |
} |
| 198 |
|
| 199 |
return -$result; |
| 200 |
} |
| 201 |
} |
| 202 |
|