-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-super-admin-performance-boost.php
More file actions
176 lines (154 loc) · 5.06 KB
/
class-super-admin-performance-boost.php
File metadata and controls
176 lines (154 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
/**
* Super Admin Performance Boost.
*
* @package Super_Admin_Performance_Boost
* @author Per Soderlind
* @since 1.0.0
*/
declare( strict_types = 1 );
/**
* Super Admin Performance Boost.
*/
class Super_Admin_Performance_Boost {
/**
* Constructor.
*/
public function __construct() {
add_filter( 'pre_get_blogs_of_user', [ $this, 'super_admin_get_blogs_of_user' ], 9, 3 );
add_filter( 'wp_list_table_class_name', [ $this, 'super_admin_wp_list_table_class_name' ], 10, 2 );
}
/**
* For the Super Admin, use a custom list table class.
*
* @param string $class_name The list table class to use.
* @param array $args An array containing _get_list_table() arguments.
* @return string The list table class to use.
*/
public function super_admin_wp_list_table_class_name( string $class_name, array $args ) : string {
if ( ! \is_super_admin() ) {
return $class_name;
}
if ( 'WP_MS_Sites_List_Table' === $class_name ) {
$class_name = 'Super_Admin_Sites_List_Table';
}
if ( 'WP_MS_Users_List_Table' === $class_name ) {
$class_name = 'Super_Admin_Users_List_Table';
}
return $class_name;
}
/**
* For the superadmin, speed up the loading of the get_blogs_of_user() function.
*
* Stores the blogname, siteurl, home, and post_count in the wp_blogmeta table.
*
* @param null|object[] $sites An array of site objects of which the user is a member.
* @param int $user_id User ID.
* @param bool $all Whether the returned array should contain all sites, including those marked 'deleted', 'archived', or 'spam'. Default false.
* @return null|object[] An array of site objects of which the user is a member.
*/
public function super_admin_get_blogs_of_user( ?array $sites, int $user_id, bool $all ) : ?array {
if ( ! \is_super_admin( $user_id ) ) {
return $sites;
}
$_sites = \get_sites(
[
'orderby' => 'path',
// 'number' => $this->load_increments,
// 'offset' => $offset,
'deleted' => '0',
'mature' => '0',
'archived' => '0',
'spam' => '0',
]
);
if ( empty( $_sites ) ) {
return $sites;
}
$sites = [];
foreach ( $_sites as $site ) {
/**
* Cast to array to avoid triggering the __get() method in the WP_Site class. This is a performance boost.
*
* The __get() method is triggered when accessing a property that does not exist in the class,
* and since it uses `switch default`, it will trigger a get_blog_details() for any property that does not exist.
*
* Below we try to use get_blog_deatis only once for each property per site.
*/
$s = (array) $site;
$blogname = get_site_meta( $s['blog_id'], 'blogname', true );
$siteurl = get_site_meta( $s['blog_id'], 'siteurl', true );
$home = get_site_meta( $s['blog_id'], 'home', true );
$post_count = get_site_meta( $s['blog_id'], 'post_count', true );
/**
* If get_site_meta() returns false, the blog ID is invalid (non-numeric, zero, or negative value).
*/
if ( false === $blogname || false === $siteurl || false === $home || false === $post_count ) {
continue;
}
/**
* If the blog ID is valid, but the meta value is empty, the meta value has not been set.
* In that case, we set the meta value.
*/
if ( '' === $blogname ) {
$blogname = get_blog_details( $s['blog_id'] )->blogname;
add_site_meta( $s['blog_id'], 'blogname', $blogname );
}
if ( '' === $siteurl ) {
$siteurl = get_blog_details( $s['blog_id'] )->siteurl;
add_site_meta( $s['blog_id'], 'siteurl', $siteurl );
}
if ( '' === $home ) {
$home = get_blog_details( $s['blog_id'] )->home;
add_site_meta( $s['blog_id'], 'home', $home );
}
if ( '' === $post_count ) {
$post_count = get_blog_details( $s['blog_id'] )->post_count;
add_site_meta( $s['blog_id'], 'post_count', $post_count );
}
/**
* Merge the site object with the meta values. Mimics the behavior of get_blogs_of_user().
*/
$sites[ $s['blog_id'] ] = (object) array_merge(
$s,
[
'userblog_id' => $s['blog_id'],
'blogname' => $blogname,
'siteurl' => $siteurl,
'home' => $home,
'post_count' => $post_count,
]
);
}
return $sites ?? null;
}
/**
* Get the admin URL for a given blog ID.
*
* @param int $blog_id The blog ID.
* @return string The admin URL.
*/
public static function get_admin_url( int $blog_id ) : string {
$siteurl = get_site_meta( $blog_id, 'siteurl', true );
if ( false === $siteurl ) {
$siteurl = get_blog_details( $blog_id )->siteurl;
add_site_meta( $blog_id, 'siteurl', $siteurl );
}
$admin_url = $siteurl . '/wp-admin/';
return $admin_url;
}
/**
* Get the home URL for a given blog ID.
*
* @param int $blog_id The blog ID.
* @return string The home URL.
*/
public static function get_home_url( int $blog_id ) : string {
$home_url = get_site_meta( $blog_id, 'home', true );
if ( false === $home_url ) {
$home_url = get_blog_details( $blog_id )->home;
add_site_meta( $blog_id, 'home', $home_url );
}
return $home_url;
}
}