| | 1 | <?php |
| | 2 | /** |
| | 3 | * List Table API: WP_MS_My_Sites_List_Table class |
| | 4 | * |
| | 5 | * @package WordPress |
| | 6 | * @subpackage Administration |
| | 7 | * @since X.Y.Z |
| | 8 | */ |
| | 9 | |
| | 10 | /** |
| | 11 | * Core class used to implement displaying sites in a list table for the network admin. |
| | 12 | * |
| | 13 | * @since X.Y.Z |
| | 14 | * @access private |
| | 15 | * |
| | 16 | * @see WP_List_Table |
| | 17 | */ |
| | 18 | class WP_MS_My_Sites_List_Table extends WP_List_Table { |
| | 19 | |
| | 20 | /** |
| | 21 | * Constructor. |
| | 22 | * |
| | 23 | * @since X.Y.Z |
| | 24 | * |
| | 25 | * @see WP_List_Table::__construct() for more information on default arguments. |
| | 26 | * |
| | 27 | * @param array $args An associative array of arguments. |
| | 28 | */ |
| | 29 | public function __construct( $args = array() ) { |
| | 30 | parent::__construct( |
| | 31 | array( |
| | 32 | 'plural' => 'sites', |
| | 33 | 'screen' => isset( $args['screen'] ) ? $args['screen'] : null, |
| | 34 | ) |
| | 35 | ); |
| | 36 | } |
| | 37 | |
| | 38 | /** |
| | 39 | * @return bool |
| | 40 | */ |
| | 41 | public function ajax_user_can() { |
| | 42 | //return current_user_can( 'manage_sites' ); |
| | 43 | return current_user_can( 'read' ); |
| | 44 | } |
| | 45 | |
| | 46 | /** |
| | 47 | * Prepares the list of sites for display. |
| | 48 | * |
| | 49 | * @since X.Y.Z |
| | 50 | * |
| | 51 | * @global string $s |
| | 52 | * @global string $mode |
| | 53 | * @global wpdb $wpdb |
| | 54 | */ |
| | 55 | public function prepare_items() { |
| | 56 | global $s, $mode, $wpdb; |
| | 57 | |
| | 58 | if ( ! empty( $_REQUEST['mode'] ) ) { |
| | 59 | $mode = $_REQUEST['mode'] === 'excerpt' ? 'excerpt' : 'list'; |
| | 60 | set_user_setting( 'mysites_list_mode', $mode ); |
| | 61 | } else { |
| | 62 | $mode = get_user_setting( 'mysites_list_mode', 'list' ); |
| | 63 | } |
| | 64 | |
| | 65 | $per_page = $this->get_items_per_page( 'my_sites_per_page' ); |
| | 66 | |
| | 67 | $pagenum = $this->get_pagenum(); |
| | 68 | |
| | 69 | $s = isset( $_REQUEST['s'] ) ? wp_unslash( trim( $_REQUEST['s'] ) ) : ''; |
| | 70 | $wild = ''; |
| | 71 | if ( false !== strpos( $s, '*' ) ) { |
| | 72 | $wild = '*'; |
| | 73 | $s = trim( $s, '*' ); |
| | 74 | } |
| | 75 | |
| | 76 | /* |
| | 77 | * If the network is large and a search is not being performed, show only |
| | 78 | * the latest sites with no paging in order to avoid expensive count queries. |
| | 79 | */ |
| | 80 | if ( ! $s && wp_is_large_network() ) { |
| | 81 | if ( ! isset( $_REQUEST['orderby'] ) ) { |
| | 82 | $_GET['orderby'] = $_REQUEST['orderby'] = ''; |
| | 83 | } |
| | 84 | if ( ! isset( $_REQUEST['order'] ) ) { |
| | 85 | $_GET['order'] = $_REQUEST['order'] = 'DESC'; |
| | 86 | } |
| | 87 | } |
| | 88 | |
| | 89 | $user_site_ids = array(); |
| | 90 | $user_blogs = get_blogs_of_user( get_current_user_id() ); |
| | 91 | if ( $user_blogs ) { |
| | 92 | foreach ( $user_blogs as $user_blog ) { |
| | 93 | $user_site_ids[] = $user_blog->userblog_id; |
| | 94 | } |
| | 95 | } |
| | 96 | |
| | 97 | $args = array( |
| | 98 | 'number' => intval( $per_page ), |
| | 99 | 'offset' => intval( ( $pagenum - 1 ) * $per_page ), |
| | 100 | 'network_id' => get_current_network_id(), |
| | 101 | ); |
| | 102 | |
| | 103 | if( ! empty( $user_site_ids ) ) { |
| | 104 | $args['site__in'] = $user_site_ids; |
| | 105 | } |
| | 106 | |
| | 107 | if ( empty( $s ) ) { |
| | 108 | // Nothing to do. |
| | 109 | } elseif ( preg_match( '/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $s ) || |
| | 110 | preg_match( '/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.?$/', $s ) || |
| | 111 | preg_match( '/^[0-9]{1,3}\.[0-9]{1,3}\.?$/', $s ) || |
| | 112 | preg_match( '/^[0-9]{1,3}\.$/', $s ) ) { |
| | 113 | // IPv4 address |
| | 114 | $sql = $wpdb->prepare( "SELECT blog_id FROM {$wpdb->registration_log} WHERE {$wpdb->registration_log}.IP LIKE %s", $wpdb->esc_like( $s ) . ( ! empty( $wild ) ? '%' : '' ) ); |
| | 115 | $reg_blog_ids = $wpdb->get_col( $sql ); |
| | 116 | |
| | 117 | if ( $reg_blog_ids ) { |
| | 118 | if( ! empty( $user_site_ids ) ) { |
| | 119 | //$args['site__in'] = array_diff( $user_site_ids, $reg_blog_ids); |
| | 120 | $args['site__in'] = array_intersect( $user_site_ids, $reg_blog_ids); |
| | 121 | } else { |
| | 122 | $args['site__in'] = $reg_blog_ids; |
| | 123 | } |
| | 124 | //$args['site__in'] = $reg_blog_ids; |
| | 125 | } |
| | 126 | } elseif ( is_numeric( $s ) && empty( $wild ) ) { |
| | 127 | $args['ID'] = $s; |
| | 128 | } else { |
| | 129 | $args['search'] = $s; |
| | 130 | |
| | 131 | if ( ! is_subdomain_install() ) { |
| | 132 | $args['search_columns'] = array( 'path' ); |
| | 133 | } |
| | 134 | } |
| | 135 | |
| | 136 | $order_by = isset( $_REQUEST['orderby'] ) ? $_REQUEST['orderby'] : ''; |
| | 137 | if ( 'blogname' === $order_by ) { |
| | 138 | if ( is_subdomain_install() ) { |
| | 139 | $order_by = 'domain'; |
| | 140 | } else { |
| | 141 | $order_by = 'path'; |
| | 142 | } |
| | 143 | } elseif ( ! $order_by ) { |
| | 144 | $order_by = false; |
| | 145 | } |
| | 146 | |
| | 147 | $args['orderby'] = $order_by; |
| | 148 | |
| | 149 | if ( $order_by ) { |
| | 150 | $args['order'] = ( isset( $_REQUEST['order'] ) && 'DESC' === strtoupper( $_REQUEST['order'] ) ) ? 'DESC' : 'ASC'; |
| | 151 | } |
| | 152 | |
| | 153 | if ( wp_is_large_network() ) { |
| | 154 | $args['no_found_rows'] = true; |
| | 155 | } else { |
| | 156 | $args['no_found_rows'] = false; |
| | 157 | } |
| | 158 | |
| | 159 | /** |
| | 160 | * Filters the arguments for the site query in the sites list table. |
| | 161 | * |
| | 162 | * @since X.Y.Z |
| | 163 | * |
| | 164 | * @param array $args An array of get_sites() arguments. |
| | 165 | */ |
| | 166 | $args = apply_filters( 'ms_mysites_list_table_query_args', $args ); |
| | 167 | |
| | 168 | $_sites = get_sites( $args ); |
| | 169 | if ( is_array( $_sites ) ) { |
| | 170 | //TODO: Check if this should be uncommented |
| | 171 | //update_site_cache( $_sites ); |
| | 172 | |
| | 173 | $this->items = array_slice( $_sites, 0, $per_page ); |
| | 174 | } |
| | 175 | |
| | 176 | $total_sites = get_sites( |
| | 177 | array_merge( |
| | 178 | $args, array( |
| | 179 | 'count' => true, |
| | 180 | 'offset' => 0, |
| | 181 | 'number' => 0, |
| | 182 | ) |
| | 183 | ) |
| | 184 | ); |
| | 185 | |
| | 186 | $this->set_pagination_args( |
| | 187 | array( |
| | 188 | 'total_items' => $total_sites, |
| | 189 | 'per_page' => $per_page, |
| | 190 | ) |
| | 191 | ); |
| | 192 | } |
| | 193 | |
| | 194 | /** |
| | 195 | */ |
| | 196 | public function no_items() { |
| | 197 | _e( 'No sites found.' ); |
| | 198 | } |
| | 199 | |
| | 200 | /** |
| | 201 | * @global string $mode List table view mode. |
| | 202 | * |
| | 203 | * @param string $which |
| | 204 | */ |
| | 205 | protected function pagination( $which ) { |
| | 206 | global $mode; |
| | 207 | |
| | 208 | parent::pagination( $which ); |
| | 209 | |
| | 210 | if ( 'top' === $which ) { |
| | 211 | $this->view_switcher( $mode ); |
| | 212 | } |
| | 213 | } |
| | 214 | |
| | 215 | /** |
| | 216 | * @return array |
| | 217 | */ |
| | 218 | public function get_columns() { |
| | 219 | $sites_columns = array( |
| | 220 | 'blogname' => __( 'Blog name' ), |
| | 221 | ); |
| | 222 | |
| | 223 | /** |
| | 224 | * Filters the displayed site columns in Sites list table. |
| | 225 | * |
| | 226 | * @since MU (X.Y.Z) |
| | 227 | * |
| | 228 | * @param string[] $sites_columns An array of displayed site columns. Default 'cb', 'blogname', |
| | 229 | */ |
| | 230 | return apply_filters( 'wpmu_myblogs_columns', $sites_columns ); |
| | 231 | } |
| | 232 | |
| | 233 | /** |
| | 234 | * @return array |
| | 235 | */ |
| | 236 | protected function get_sortable_columns() { |
| | 237 | return array( |
| | 238 | 'blogname' => 'blogname', |
| | 239 | ); |
| | 240 | } |
| | 241 | |
| | 242 | |
| | 243 | /** |
| | 244 | * Handles the site name column output. |
| | 245 | * |
| | 246 | * @since X.Y.Z |
| | 247 | * |
| | 248 | * @global string $mode List table view mode. |
| | 249 | * |
| | 250 | * @param array $blog Current site. |
| | 251 | */ |
| | 252 | public function column_blogname( $blog ) { |
| | 253 | global $mode; |
| | 254 | switch_to_blog( $blog['blog_id'] ); |
| | 255 | $blogname = get_option( 'blogname' ); |
| | 256 | echo '<strong>'; |
| | 257 | echo '<a href="' . esc_url( get_admin_url( $blog['blog_id'] ) ) . '" class="edit">' . $blogname . '</a>'; |
| | 258 | echo '</strong>'; |
| | 259 | |
| | 260 | if ( 'list' !== $mode ) { |
| | 261 | printf( |
| | 262 | /* translators: 1: site name, 2: site tagline. */ |
| | 263 | __( ' – %1$s' ), |
| | 264 | '<em>' . get_option( 'blogdescription ' ) . '</em>' |
| | 265 | ); |
| | 266 | |
| | 267 | } |
| | 268 | echo apply_filters( 'myblogs_options', '', (object) $blog ); |
| | 269 | restore_current_blog(); |
| | 270 | } |
| | 271 | |
| | 272 | |
| | 273 | /** |
| | 274 | * Handles output for the default column. |
| | 275 | * |
| | 276 | * @since X.Y.Z |
| | 277 | * |
| | 278 | * @param array $blog Current site. |
| | 279 | * @param string $column_name Current column name. |
| | 280 | */ |
| | 281 | public function column_default( $blog, $column_name ) { |
| | 282 | /** |
| | 283 | * Fires for each registered custom column in the Sites list table. |
| | 284 | * |
| | 285 | * @since X.Y.Z |
| | 286 | * |
| | 287 | * @param string $column_name The name of the column to display. |
| | 288 | * @param int $blog_id The site ID. |
| | 289 | */ |
| | 290 | do_action( 'ms_mysites_custom_column', $column_name, $blog['blog_id'] ); |
| | 291 | } |
| | 292 | |
| | 293 | /** |
| | 294 | * @global string $mode |
| | 295 | */ |
| | 296 | public function display_rows() { |
| | 297 | foreach ( $this->items as $blog ) { |
| | 298 | $blog = $blog->to_array(); |
| | 299 | |
| | 300 | echo "<tr>"; |
| | 301 | $this->single_row_columns( $blog ); |
| | 302 | echo '</tr>'; |
| | 303 | } |
| | 304 | } |
| | 305 | |
| | 306 | /** |
| | 307 | * Gets the name of the default primary column. |
| | 308 | * |
| | 309 | * @since X.Y.Z |
| | 310 | * |
| | 311 | * @return string Name of the default primary column, in this case, 'blogname'. |
| | 312 | */ |
| | 313 | protected function get_default_primary_column_name() { |
| | 314 | return 'blogname'; |
| | 315 | } |
| | 316 | |
| | 317 | /** |
| | 318 | * Generates and displays row action links. |
| | 319 | * |
| | 320 | * @since X.Y.Z |
| | 321 | * |
| | 322 | * @param object $blog Site being acted upon. |
| | 323 | * @param string $column_name Current column name. |
| | 324 | * @param string $primary Primary column name. |
| | 325 | * @return string Row actions output. |
| | 326 | */ |
| | 327 | protected function handle_row_actions( $blog, $column_name, $primary ) { |
| | 328 | if ( $primary !== $column_name ) { |
| | 329 | return; |
| | 330 | } |
| | 331 | |
| | 332 | $blogname = untrailingslashit( $blog['domain'] . $blog['path'] ); |
| | 333 | |
| | 334 | // Preordered. |
| | 335 | $actions = array( |
| | 336 | 'backend' => '', |
| | 337 | 'visit' => '', |
| | 338 | ); |
| | 339 | |
| | 340 | $actions['backend'] = "<a href='" . esc_url( get_admin_url( $blog['blog_id'] ) ) . "' class='edit'>" . __( 'Dashboard' ) . '</a>'; |
| | 341 | $actions['visit'] = "<a href='" . esc_url( get_home_url( $blog['blog_id'], '/' ) ) . "' rel='bookmark'>" . __( 'Visit' ) . '</a>'; |
| | 342 | |
| | 343 | /** |
| | 344 | * Filters the row links displayed for each site on the My Sites screen. |
| | 345 | * |
| | 346 | * @since MU (3.0.0) |
| | 347 | * |
| | 348 | * @param string $actions The HTML site link markup. |
| | 349 | * @param object $user_blog An object containing the site data. |
| | 350 | */ |
| | 351 | |
| | 352 | $actions = apply_filters( 'myblogs_blog_actions', implode( ',' , array_filter( $actions ) ),(object) $blog); |
| | 353 | $actions = explode( ',' , $actions ); |
| | 354 | return $this->row_actions( $actions ); |
| | 355 | } |
| | 356 | } |