-
Notifications
You must be signed in to change notification settings - Fork 30
/
ms-user-management.php
230 lines (184 loc) · 7.63 KB
/
ms-user-management.php
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
/*
Plugin Name: Multisite User Management
Plugin URI: http://github.com/thenbrent/multisite-user-management
Description: Running a WordPress network? You no longer need to manually add users to each of your sites.
Author: Brent Shepherd
Author URI: http://find.brentshepherd.com/
Network: true
Version: 1.1
License: GPLv2
Copyright (C) 2017 Brent Shepherd
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/**
* Adds the default roles for all sites to a user, specified by $user_id
*/
function msum_add_roles( $user_id ){
foreach( msum_get_blog_list( 0, 'all' ) as $key => $blog ) {
if( is_user_member_of_blog( $user_id, $blog[ 'blog_id' ] ) )
continue;
switch_to_blog( $blog[ 'blog_id' ] );
$role = get_option( 'msum_default_user_role', 'none' ); // if no default set, use 'none'
if( $role != 'none' )
add_user_to_blog( $blog[ 'blog_id' ], $user_id, $role );
restore_current_blog();
}
update_user_meta( $user_id, 'msum_has_caps', 'true' );
}
add_action( 'wpmu_activate_user', 'msum_add_roles', 10, 1 );
add_action( 'wpmu_new_user', 'msum_add_roles', 10, 1 );
add_action( 'user_register', 'msum_add_roles', 10, 1 );
/**
* Sometimes a user will be activated along with their blog, this function implements @see msum_add_roles
* on the 'wpmu_activate_blog' action.
*/
function msum_activate_blog_user( $blog_id, $user_id ){
msum_add_roles( $user_id, $blog_id );
}
add_action( 'wpmu_activate_blog', 'msum_activate_blog_user', 10, 2 );
/**
* The 'wpmu_activate_user', 'wpmu_new_user' & 'wpmu_activate_blog' actions can only be
* hooked if the plugin is in the mu-plugins folder, which is a PITA.
*
* This function calls @see msum_add_roles from the 'wp_login' action.
*/
function msum_maybe_add_roles( $user_login ) {
if ( function_exists( 'get_user_by' ) )
$userdata = get_user_by( 'login', $user_login );
else
$userdata = get_userdatabylogin( $user_login );
if( $userdata != false && get_user_meta( $userdata->ID, 'msum_has_caps', true ) != 'true' )
msum_add_roles( $userdata->ID );
}
add_action( 'wp_login', 'msum_maybe_add_roles', 10, 1 );
add_action( 'social_connect_login', 'msum_maybe_add_roles', 10, 1 );
/**
* Outputs the role selection boxes on the 'Network Admin | Settings' page.
*/
function msum_options(){
$blogs = msum_get_blog_list( 0, 'all' );
echo '<h3>' . __( 'Multisite User Management', 'msum' ). '</h3>';
if( empty( $blogs ) ) {
echo '<p><b>' . __( 'No sites available.', 'msum' ) . '</b></p>';
} else {
echo '<p>' . __( 'Select the default role for each of your sites.', 'msum' ) . '</p>';
echo '<p>' . __( 'New users will receive these roles when activating their account. Existing users will receive these roles only if they have the current default role or no role at all for each particular site.', 'msum' ) . '</p>';
echo '<table class="form-table">';
foreach( $blogs as $key => $blog ) {
switch_to_blog( $blog[ 'blog_id' ] );
?>
<tr valign="top">
<th scope="row"><?php echo get_bloginfo( 'name' ); ?></th>
<td>
<select name="msum_default_user_role[<?php echo $blog[ 'blog_id' ]; ?>]" id="msum_default_user_role[<?php echo $blog[ 'blog_id' ]; ?>]">
<option value="none"><?php _e( '-- None --', 'msum' )?></option>
<?php wp_dropdown_roles( get_option( 'msum_default_user_role' ) ); ?>
</select>
</td>
</tr>
<?php restore_current_blog();
}
echo '</table>';
}
echo '<p>' . __( '<b>Note:</b> only public, non-mature and non-dashboard sites appear here. Set the default role for the dashboard site above under <b>Dashboard Settings</b>.', 'msum' ) . '</p>';
}
add_action( 'wpmu_options', 'msum_options' );
/**
* Update Default Roles on submission of the multisite options page.
*/
function msum_options_update(){
if( !isset( $_POST[ 'msum_default_user_role' ] ) || !is_array( $_POST[ 'msum_default_user_role' ] ) )
return;
foreach( $_POST[ 'msum_default_user_role' ] as $blog_id => $new_role ) {
switch_to_blog( $blog_id );
$old_role = get_option( 'msum_default_user_role', 'none' ); // default to none
if( $old_role == $new_role ) {
restore_current_blog();
continue;
}
$blog_users = msum_get_users_with_role( $old_role );
foreach( $blog_users as $blog_user ) {
if( $old_role != 'none' )
remove_user_from_blog( $blog_user, $blog_id );
if( $new_role != 'none' )
add_user_to_blog( $blog_id, $blog_user, $new_role );
update_user_meta( $blog_user, 'msum_has_caps', 'true' );
}
update_option( 'msum_default_user_role', $new_role );
restore_current_blog();
}
}
add_action( 'update_wpmu_options', 'msum_options_update' );
/**
* Selects all the users with a given role and returns an array of the users' IDs.
*
* @param $role string The role to get the users for
* @return $users array Array of user IDs for those users with the given role.
*/
function msum_get_users_with_role( $role ) {
global $wpdb;
if( $role != 'none' ) {
$sql = $wpdb->prepare( "SELECT DISTINCT($wpdb->users.ID) FROM $wpdb->users
INNER JOIN $wpdb->usermeta ON $wpdb->users.ID = $wpdb->usermeta.user_id
WHERE $wpdb->usermeta.meta_key = '{$wpdb->prefix}capabilities'
AND $wpdb->usermeta.meta_value LIKE %s", '%' . $role . '%' );
} else { // get users without a role for current site
$sql = "SELECT DISTINCT($wpdb->users.ID) FROM $wpdb->users
WHERE $wpdb->users.ID NOT IN (
SELECT $wpdb->usermeta.user_id FROM $wpdb->usermeta
WHERE $wpdb->usermeta.meta_key = '{$wpdb->prefix}capabilities'
)";
}
$users = $wpdb->get_col( $sql );
if( $role == 'none' ) { // if we got all users without a capability for the site, that includes super admins
$super_users = get_super_admins();
foreach( $users as $key => $user ){ //never modify caps for super admins
if( is_super_admin( $user ) )
unset( $users[$key] );
}
}
return $users;
}
/**
* Clean up when plugin is deleted
*/
function msum_uninstall(){
foreach( msum_get_blog_list( 0, 'all' ) as $key => $blog ) {
switch_to_blog( $blog[ 'blog_id' ] );
delete_option( 'msum_default_user_role', $role );
restore_current_blog();
}
}
register_uninstall_hook( __FILE__, 'msum_uninstall' );
/**
* Based on the deprecated WPMU get_blog_list function.
*
* Except this function gets all blogs, even if they are marked as mature and private.
*/
function msum_get_blog_list( $start = 0, $num = 10 ) {
global $wpdb;
$blogs = $wpdb->get_results( $wpdb->prepare( "SELECT blog_id, domain, path FROM $wpdb->blogs WHERE site_id = %d AND archived = '0' AND spam = '0' AND deleted = '0' ORDER BY registered DESC", $wpdb->siteid ), ARRAY_A );
foreach ( (array) $blogs as $details ) {
$blog_list[ $details[ 'blog_id' ] ] = $details;
$blog_list[ $details[ 'blog_id' ] ]['postcount'] = $wpdb->get_var( "SELECT COUNT(ID) FROM " . $wpdb->get_blog_prefix( $details['blog_id'] ). "posts WHERE post_status='publish' AND post_type='post'" );
}
unset( $blogs );
$blogs = $blog_list;
if ( false == is_array( $blogs ) )
return array();
if ( $num == 'all' )
return array_slice( $blogs, $start, count( $blogs ) );
else
return array_slice( $blogs, $start, $num );
}