| @@ -1,202 +1,177 @@ | ||
| 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 | -} | |
| 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 | +} | |