PluginProbe
Patchstack – WordPress & Plugins Security / trunk
Patchstack – WordPress & Plugins Security vtrunk
2.3.7 trunk 2.1.0 2.1.1 2.1.10 2.1.11 2.1.12 2.1.13 2.1.14 2.1.15 2.1.16 2.1.17 2.1.18 2.1.19 2.1.2 2.1.20 2.1.21 2.1.22 2.1.23 2.1.24 2.1.25 2.1.3 2.1.4 2.1.5 2.1.6 All 49 releases
patchstack / includes / admin / multisite-table.php

multisite-table.php in Patchstack – WordPress & Plugins Security trunk, at includes/admin/multisite-table.php

203 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 = function_exists( 'get_sites' ) ? 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 $search = (string) $_GET['s'];
100 if ( strpos( (string) $b->blog_id, $search ) !== false ) {
101 $match = true;
102 }
103
104 if ( strpos( (string) $b->blogname, $search ) !== false ) {
105 $match = true;
106 }
107
108 if ( strpos( (string) $site_info->siteurl, $search ) !== false ) {
109 $match = true;
110 }
111 }
112
113 if ( ! isset( $_GET['s'] ) || $match ) {
114 $is_firewall_enabled = get_blog_option( $b->blog_id, 'patchstack_basic_firewall' );
115 $is_activated = get_blog_option( $b->blog_id, 'patchstack_clientid', '' ) != '';
116 $prefix = $wpdb->get_blog_prefix( $b->blog_id );
117
118 // Determine if the site has any missing migrations.
119 $has_missing = false;
120 if ($is_activated) {
121 foreach( [ 'patchstack_firewall_log', 'patchstack_logic', 'patchstack_event_log' ] as $table ) {
122 $result = $wpdb->get_results("SHOW TABLES LIKE '" . $prefix . $table . "'");
123 if ( !$result ) {
124 $has_missing = true;
125 }
126 }
127 }
128
129 $data[] = [
130 'id' => (int) $b->blog_id,
131 'title' => esc_html( $b->blogname ),
132 'url' => '<a href="' . esc_url( $site_info->siteurl ). '">' . esc_url( $site_info->siteurl ) . '</a>',
133 'activated' => $is_activated ? 'Activated' : 'Deactivated',
134 'edit' => '<a href="' . esc_url( $site_info->siteurl ). '/wp-admin/options-general.php?page=patchstack">Settings Page</a>',
135 'firewall_status' => $is_firewall_enabled && ! $free ? 'Enabled' : 'Disabled',
136 'migration' => '<a href="' . esc_url( add_query_arg(
137 [
138 'PatchstackNonce' => $nonce,
139 'site' => $b->blog_id
140 ]
141 ) ) . '">' . ($has_missing ? 'Run' : 'Rerun') . ' Database Migration</a>',
142 ];
143 }
144 }
145
146 return $data;
147 }
148
149 /**
150 * Define what data to show on each column of the table
151 *
152 * @param Array $item Data
153 * @param String $column_name - Current column name
154 *
155 * @return Mixed
156 */
157 public function column_default( $item, $column_name ) {
158 switch ( $column_name ) {
159 case 'id':
160 case 'title':
161 case 'url':
162 case 'activated':
163 case 'firewall_status':
164 case 'migration':
165 case 'edit':
166 return $item[ $column_name ];
167 default:
168 return print_r( $item, true );
169 }
170 }
171
172 /**
173 * Allows you to sort the data by the variables set in the $_GET
174 *
175 * @param Array $a
176 * @param Array $b
177 * @return Mixed
178 */
179 private function sort_data( $a, $b ) {
180 // Set defaults
181 $orderby = 'id';
182 $order = 'asc';
183 $columns = $this->get_columns();
184
185 // If orderby is set, use this as the sort column
186 if ( isset( $_GET['orderby'] ) && ! empty( $_GET['orderby'] ) && isset( $columns[$_GET['orderby']] ) ) {
187 $orderby = wp_filter_nohtml_kses( $_GET['orderby'] );
188 }
189
190 // If order is set use this as the order
191 if ( isset( $_GET['order'] ) && ! empty( $_GET['order'] ) && $_GET['order'] != 'asc' ) {
192 $order = 'desc';
193 }
194
195 $result = strcmp( $a[ $orderby ], $b[ $orderby ] );
196 if ( $order === 'asc' ) {
197 return $result;
198 }
199
200 return -$result;
201 }
202 }
203