-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.php
More file actions
128 lines (109 loc) · 3.14 KB
/
plugin.php
File metadata and controls
128 lines (109 loc) · 3.14 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
<?php
/*
* Plugin Name: Large Network Management
* Description: Optimize admin management of large networks
* Version: 0.1.1
* Author: Paul V. Biron/Sparrow Hawk Computing
* Author URI: https://sparrowhawkcomputing.com
* Plugin URI: https://github.com/pbiron/large-network-management
* GitHub Plugin URI: https://github.com/pbiron/large-network-management
* Release Asset: true
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Network: True
*/
defined( 'ABSPATH' ) || die;
require __DIR__ . '/vendor/autoload.php';
/**
* Main plugin class.
*
* @since 0.1.0
*
* @todo currently, most of the functionality modification only applies to
* super_admins. It might be useful to have it apply for any user that
* is a member of more than ~30 sites.
*/
class Large_Network_Management_Plugin extends _Large_Network_Management_Plugin {
/**
* Add hooks.
*
* @since 0.1.0
*
* @return void
*/
function add_hooks() {
// we are, by definition, a large network :-)
// we set priority to 9, so other plugins that use the default priority
// can also hook into 'wp_is_large_networ' after us.
add_filter( 'wp_is_large_network', '__return_true', 9 );
add_action( 'init', array( $this, 'setup' ) );
return;
}
/**
* Perform setup operations.
*
* Cannot be hooked earlier than init, to avoid calling `is_admin_bar_showing()` before
* the query is run (e.g., on the front-end).
*
* @since 0.1.0
*
* @return void
*
* @action init
*/
function setup() {
global $pagenow;
if ( ! wp_is_large_network() ) {
// not a large network, so nothing for us to do.
return;
}
if ( is_admin_bar_showing() ) {
Large_Network_Management_My_Sites::get_instance();
Large_Network_Management_Get_Blogs_Of_User::get_instance();
}
if ( is_network_admin() && 'users.php' === $pagenow ) {
Large_Network_Management_Network_Users::get_instance();
}
return;
}
/**
* Count the number of blogs a user has access to.
*
* @since 0.1.0
*
* @param int $user_id The User ID to count blogs for.
* @return int The number of blogs `$user_id` is a member of.
*
* @todo this method is currently not used. It _might_ be useful
* if/when we loosen the restriction of applying most of the functionality
* if this plugin to super_admins.
*/
static function count_blogs_of_user( $user_id ) {
global $wpdb;
$count = 0;
// the logic here is borrowed from get_blogs_of_user() where it gathers the
// site_id of all sites a user has access to (before it does the potentially
// expensive get_sites() on those ids.
$keys = get_user_meta( $user_id );
if ( empty( $keys ) ) {
return $count;
}
$keys = array_keys( $keys );
foreach ( $keys as $key ) {
if ( 'capabilities' !== substr( $key, -12 ) ) {
continue;
}
if ( $wpdb->base_prefix && 0 !== strpos( $key, $wpdb->base_prefix ) ) {
continue;
}
$site_id = str_replace( array( $wpdb->base_prefix, '_capabilities' ), '', $key );
if ( ! is_numeric( $site_id ) ) {
continue;
}
$count++;
}
return $count;
}
}
// instantiate ourselves.
Large_Network_Management_Plugin::get_instance();