Skip to content

Commit 963d4a9

Browse files
committed
Extend WP_MS_Users_List_Table
1 parent 254273c commit 963d4a9

5 files changed

Lines changed: 146 additions & 26 deletions

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
55
On a WordPress Multisite, tries to avoid using [`switch_to_blog()` and `restore_current_blog()`](assets/switch-to-blog.png) when possible.
66

7-
Super Admin owns all sites, so:
7+
## Description
88

99
1. Short-circuit the `get_blogs_of_user()` function. We don't have to check if a site is own by the user, we want all sites.
1010
2. Extend `WP_MS_Sites_List_Table` and use bespoke `Super_Admin_Performance_Boost::get_admin_url()` and `Super_Admin_Performance_Boost::get_home_url()` funtions.
11-
3. Hide the `Sites` column in `WP_MS_Users_List_Table`, no point in [listing the sites](assets/all-sites.png) the super admins owns (they own 'em all)
11+
3. Extend `WP_MS_Users_List_Table`
12+
- For the Super Admin, hide their sites in the `Sites` column. No point in [listing the sites](assets/all-sites.png), the super admins own 'em all.
13+
- For rest of the users, use bespoke `Super_Admin_Performance_Boost::get_home_url()` funtions.
1214

1315
## Installation
1416

class-super-admin-performance-boost.php

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,8 @@ class Super_Admin_Performance_Boost {
2020
public function __construct() {
2121
add_filter( 'pre_get_blogs_of_user', [ $this, 'super_admin_get_blogs_of_user' ], 9, 3 );
2222
add_filter( 'wp_list_table_class_name', [ $this, 'super_admin_wp_list_table_class_name' ], 10, 2 );
23-
add_filter( 'wpmu_users_columns', [ $this, 'filter_wpmu_users_columns' ] );
2423
}
2524

26-
/**
27-
* Super Admin owns all site, no point in listing them, i,e,: Remove the 'blogs' column from the users list table.
28-
*
29-
* @param string[] $users_columns An array of user columns. Default 'cb', 'username', 'name', 'email', 'registered', 'blogs'.
30-
* @return string[] An array of user columns. Default 'cb', 'username', 'name', 'email', 'registered', 'blogs'.
31-
*/
32-
public function filter_wpmu_users_columns( array $users_columns ) : array {
33-
if ( ! \is_super_admin() ) {
34-
return $users_columns;
35-
}
36-
$users_columns = [
37-
'cb' => '<input type="checkbox" />',
38-
'username' => __( 'Username' ),
39-
'name' => __( 'Name' ),
40-
'email' => __( 'Email' ),
41-
'registered' => _x( 'Registered', 'user' ),
42-
// 'blogs' => __( 'Sites' ),
43-
];
44-
return $users_columns;
45-
}
4625

4726
/**
4827
* For the Super Admin, use a custom list table class.
@@ -58,6 +37,9 @@ public function super_admin_wp_list_table_class_name( string $class_name, array
5837
if ( 'WP_MS_Sites_List_Table' === $class_name ) {
5938
$class_name = 'Super_Admin_Sites_List_Table';
6039
}
40+
if ( 'WP_MS_Users_List_Table' === $class_name ) {
41+
$class_name = 'Super_Admin_Users_List_Table';
42+
}
6143

6244
return $class_name;
6345
}
@@ -74,7 +56,7 @@ public function super_admin_wp_list_table_class_name( string $class_name, array
7456
*/
7557
public function super_admin_get_blogs_of_user( ?array $sites, int $user_id, bool $all ) : ?array {
7658

77-
if ( ! \is_super_admin() ) {
59+
if ( ! \is_super_admin( $user_id ) ) {
7860
return $sites;
7961
}
8062
$_sites = \get_sites(

class-super-admin-sites-list-table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
/**
1616
* For the Super Admin, display a list of all sites on the network using
17-
* bespoke get_admin_url() and get_home_url().
17+
* bespoke Super_Admin_Performance_Boost::get_admin_url() and Super_Admin_Performance_Boost::get_home_url().
1818
*
1919
* @since 1.0.0
2020
*
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?php
2+
/**
3+
* Super Admin Performance Boost. Modify the list of users in the network admin.
4+
*
5+
* @package Super_Admin_Performance_Boost
6+
*/
7+
8+
if ( ! class_exists( 'WP_List_Table' ) ) {
9+
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
10+
}
11+
if ( ! class_exists( 'WP_MS_Users_List_Table' ) ) {
12+
require_once ABSPATH . 'wp-admin/includes/class-wp-ms-users-list-table.php';
13+
}
14+
15+
/**
16+
* For the Super Admin, If Super Admin, link to the sites.php page. Otherwise display a list to sites on the network using
17+
* bespoke Super_Admin_Performance_Boost::get_home_url().
18+
*
19+
* @since 1.0.0
20+
*
21+
* @see WP_MS_Users_List_Table
22+
*/
23+
class Super_Admin_Users_List_Table extends WP_MS_Users_List_Table {
24+
25+
26+
/**
27+
* If Super Admin, link to the sites.php page.
28+
*
29+
* @since 1.1.0
30+
*
31+
* @param WP_User $user
32+
* @param string $classes
33+
* @param string $data
34+
* @param string $primary
35+
*/
36+
protected function _column_blogs( $user, $classes, $data, $primary ) {
37+
echo '<td class="', $classes, ' has-row-actions" ', $data, '>';
38+
if ( is_super_admin( $user->ID ) ) {
39+
echo '<a href="' . esc_url( network_admin_url( 'sites.php' ) ) . '">' . __( 'Sites' ) . '</a>';
40+
} else {
41+
echo $this->column_blogs( $user );
42+
echo $this->handle_row_actions( $user, 'blogs', $primary );
43+
}
44+
echo '</td>';
45+
}
46+
47+
/**
48+
* Handles the sites column output. (Copied from WP_MS_Users_List_Table).
49+
* Modified to use Super_Admin_Performance_Boost::get_home_url().
50+
*
51+
* @since 4.3.0
52+
*
53+
* @param WP_User $user The current WP_User object.
54+
*/
55+
public function column_blogs( $user ) {
56+
$blogs = get_blogs_of_user( $user->ID, true );
57+
if ( ! is_array( $blogs ) ) {
58+
return;
59+
}
60+
61+
foreach ( $blogs as $site ) {
62+
if ( ! can_edit_network( $site->site_id ) ) {
63+
continue;
64+
}
65+
66+
$path = ( '/' === $site->path ) ? '' : $site->path;
67+
$site_classes = [ 'site - ' . $site->site_id ];
68+
/**
69+
* Filters the span class for a site listing on the mulisite user list table.
70+
*
71+
* @since 5.2.0
72+
*
73+
* @param string[] $site_classes Array of class names used within the span tag. Default "site-#" with the site's network ID .
74+
* @param int $site_id Site ID .
75+
* @param int $network_id Network ID .
76+
* @param WP_User $user WP_User object .
77+
*/
78+
$site_classes = apply_filters( 'ms_user_list_site_class', $site_classes, $site->userblog_id, $site->site_id, $user );
79+
if ( is_array( $site_classes ) && ! empty( $site_classes ) ) {
80+
$site_classes = array_map( 'sanitize_html_class', array_unique( $site_classes ) );
81+
echo '<span class="' . esc_attr( implode( ' ', $site_classes ) ) . '">';
82+
} else {
83+
echo '<span>';
84+
}
85+
echo '<a href="' . esc_url( network_admin_url( 'site-info.php?id=' . $site->userblog_id ) ) . '">' . str_replace( '.' . get_network()->domain, '', $site->domain . $path ) . '</a>';
86+
echo ' <small class="row-actions">';
87+
$actions = [];
88+
$actions['edit'] = '<a href="' . esc_url( network_admin_url( 'site-info.php?id=' . $site->userblog_id ) ) . '">' . __( 'Edit' ) . '</a>';
89+
90+
$class = '';
91+
if ( 1 === (int) $site->spam ) {
92+
$class .= 'site-spammed ';
93+
}
94+
if ( 1 === (int) $site->mature ) {
95+
$class .= 'site-mature ';
96+
}
97+
if ( 1 === (int) $site->deleted ) {
98+
$class .= 'site-deleted ';
99+
}
100+
if ( 1 === (int) $site->archived ) {
101+
$class .= 'site-archived ';
102+
}
103+
104+
$actions['view'] = '<a class="' . $class . '" href="' . esc_url( Super_Admin_Performance_Boost::get_home_url( $site->userblog_id ) ) . '">' . __( 'View' ) . '</a>';
105+
106+
/**
107+
* Filters the action links displayed next the sites a user belongs to
108+
* in the Network Admin Users list table.
109+
*
110+
* @since 3.1.0
111+
*
112+
* @param string[] $actions An array of action links to be displayed. Default 'Edit', 'View'.
113+
* @param int $userblog_id The site ID.
114+
*/
115+
$actions = apply_filters( 'ms_user_list_site_actions', $actions, $site->userblog_id );
116+
117+
$action_count = count( $actions );
118+
119+
$i = 0;
120+
121+
foreach ( $actions as $action => $link ) {
122+
++$i;
123+
124+
$separator = ( $i < $action_count ) ? ' | ' : '';
125+
126+
echo "<span class='$action'>{$link}{$separator}</span>";
127+
}
128+
129+
echo '</small></span><br />';
130+
}
131+
}
132+
133+
}

super-admin-performance-boost.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* Plugin URI: https://github.com/soderlind/super-admin-performance
1313
* GitHub Plugin URI: https://github.com/soderlind/super-admin-performance
1414
* Description: Try to aviod using switch_to_blog() and restore_current_blog() when possible.
15-
* Version: 1.0.1
15+
* Version: 1.1.0
1616
* Author: Per Soderlind
1717
* Author URI: https://soderlind.no
1818
* Network: true
@@ -30,6 +30,9 @@
3030
if ( ! class_exists( 'Super_Admin_Sites_List_Table' ) ) {
3131
require_once __DIR__ . '/class-super-admin-sites-list-table.php';
3232
}
33+
if ( ! class_exists( 'Super_Admin_Users_List_Table' ) ) {
34+
require_once __DIR__ . '/class-super-admin-users-list-table.php';
35+
}
3336

3437
if ( ! class_exists( 'Super_Admin_Performance_Boost' ) ) {
3538
require_once __DIR__ . '/class-super-admin-performance-boost.php';

0 commit comments

Comments
 (0)