Username Changer - Version 2.0.2

Version Description

Download this release

Release Info

Developer ghost1227
Plugin Icon 128x128 Username Changer
Version 2.0.2
Comparing to
See all releases

Code changes from version 2.0.1 to 2.0.2

Files changed (2) hide show
  1. readme.txt +8 -2
  2. username-changer.php +58 -34
readme.txt CHANGED
@@ -1,10 +1,10 @@
1
  === Username Changer ===
2
- Contributors: Ghost1227
3
  Donate link: http://pledgie.com/campaigns/17372
4
  Tags: user, username, display name, changer
5
  Requires at least: 3.0
6
  Tested up to: 3.4.1
7
- Stable tag: 2.0.1
8
 
9
  A simple plugin to change registered usernames.
10
 
@@ -28,10 +28,16 @@ None yet
28
 
29
  == Changelog ==
30
 
 
 
 
31
  = Version 2.0.1 =
32
  * Added username sanitization
33
  * Minor code cleanup
34
 
 
 
 
35
  = Version 1.4 =
36
  * Include author url (nicename) in update process
37
 
1
  === Username Changer ===
2
+ Contributors: section214
3
  Donate link: http://pledgie.com/campaigns/17372
4
  Tags: user, username, display name, changer
5
  Requires at least: 3.0
6
  Tested up to: 3.4.1
7
+ Stable tag: 2.0.2
8
 
9
  A simple plugin to change registered usernames.
10
 
28
 
29
  == Changelog ==
30
 
31
+ = Version 2.0.2 =
32
+ * Added multisite support
33
+
34
  = Version 2.0.1 =
35
  * Added username sanitization
36
  * Minor code cleanup
37
 
38
+ = Version 2.0.0 =
39
+ * Converted to class-based structure
40
+
41
  = Version 1.4 =
42
  * Include author url (nicename) in update process
43
 
username-changer.php CHANGED
@@ -2,7 +2,7 @@
2
  /**
3
  * Plugin Name: Username Changer
4
  * Description: Lets you change usernames.
5
- * Version: 2.0.1
6
  * Author: Daniel J Griffiths
7
  * Author URI: http://section214.com
8
  * Text Domain: username-changer
@@ -111,6 +111,14 @@ if( !class_exists( 'Username_Changer' ) ) {
111
 
112
  // Add link to users.php
113
  add_filter( 'user_row_actions', array( &$this, 'username_changer_link' ), 10, 2 );
 
 
 
 
 
 
 
 
114
  }
115
 
116
 
@@ -151,7 +159,9 @@ if( !class_exists( 'Username_Changer' ) ) {
151
  */
152
  public function username_changer_link( $actions, $user ) {
153
  if( current_user_can( 'edit_users' ) ) {
154
- $actions[] = '<a href="' . admin_url( 'users.php?page=username_changer&id=' . $user->ID ) . '">' . __( 'Change Username', 'username-changer' ) . '</a>';
 
 
155
  }
156
 
157
  return $actions;
@@ -207,42 +217,54 @@ if( !class_exists( 'Username_Changer' ) ) {
207
  $new_username = $wpdb->escape( $new_username );
208
  $current_username = $wpdb->escape( $_POST['current_username'] );
209
 
210
- if( username_exists( $current_username ) && ( $new_username == $current_username ) ) {
211
- // Make sure username exists and username != new username
212
- echo '<div id="message" class="error"><p><strong>' . sprintf( __( 'Current Username and New Username cannot both be "%1$s"!', 'username-changer' ), $new_username ) . '</strong></p></div>';
213
- } elseif( username_exists( $current_username ) && username_exists( $new_username ) ) {
214
- // Make sure new username doesn't exist
215
- echo '<div id="message" class="error"><p><strong>' . sprintf( __( '"%1$s" cannot be changed to "%2$s", "%3$s" already exists!', 'username-changer' ), $current_username, $new_username, $new_username ) . '</strong></p></div>';
216
- } elseif( username_exists( $current_username ) && ( $new_username != $current_username ) ) {
217
- // Update username!
218
- $q = $wpdb->prepare( "UPDATE $wpdb->users SET user_login = %s WHERE user_login = %s", $new_username, $current_username );
219
- $qnn = $wpdb->prepare( "UPDATE $wpdb->users SET user_nicename = %s WHERE user_login = %s", $new_username, $new_username );
220
-
221
- // Check if display name is the same as username
222
- $usersql = $wpdb->prepare( "SELECT * from $wpdb->users WHERE user_login = %s", $current_username );
223
- $userinfo = $wpdb->get_row( $usersql );
224
-
225
- // If display name is the same as username, update both
226
- if( $current_username == $userinfo->display_name ) {
227
- $qdn = $wpdb->prepare( "UPDATE $wpdb->users SET display_name = %s WHERE user_login = %s", $new_username, $new_username );
228
- }
229
-
230
- if( false !== $wpdb->query( $q ) ) {
231
- $wpdb->query( $qnn );
 
 
 
232
 
233
- if( isset( $qdn ) ) {
234
- $wpdb->query( $qdn );
 
235
  }
236
 
237
- // If changing own username, display link to re-login
238
- if( $current_user->user_login == $current_username ) {
239
- echo '<div id="message" class="updated fade"><p><strong>' . sprintf( __( 'Username %1$s was changed to %2$s.&nbsp;&nbsp;Click <a href="%3$s">here</a> to log back in.', 'username-changer' ), $current_username, $new_username, wp_login_url() ) . '</strong></p></div>';
 
 
 
 
 
 
 
 
 
 
240
  } else {
241
- echo '<div id="message" class="updated fade"><p><strong>' . sprintf( __( 'Username %1$s was changed to %2$s.', 'username-changer' ), $current_username, $new_username ) . '</strong></p></div>';
 
242
  }
243
- } else {
244
- // If database error occurred, display it
245
- echo '<div id="message" class="error"><p><strong>' . sprintf( __( 'A database error occurred : %1$s', 'username-changer' ), $wpdb->last_error ) . '</strong></p></div>';
246
  }
247
  } else {
248
  // Warn if user doesn't exist (this should never happen!)
@@ -292,7 +314,9 @@ if( !class_exists( 'Username_Changer' ) ) {
292
 
293
  if( $userinfo ) {
294
  foreach( $userinfo as $userinfoObj ) {
295
- echo '<option value="' . esc_attr( $userinfoObj->user_login ) . '">' . esc_html( $userinfoObj->user_login ) . ' (' . esc_html( $userinfoObj->user_email ) . ')</option>';
 
 
296
  }
297
  }
298
 
2
  /**
3
  * Plugin Name: Username Changer
4
  * Description: Lets you change usernames.
5
+ * Version: 2.0.2
6
  * Author: Daniel J Griffiths
7
  * Author URI: http://section214.com
8
  * Text Domain: username-changer
111
 
112
  // Add link to users.php
113
  add_filter( 'user_row_actions', array( &$this, 'username_changer_link' ), 10, 2 );
114
+
115
+ if( is_multisite() ) {
116
+ // Add link to network/users.php
117
+ add_filter( 'ms_user_row_actions', array( &$this, 'username_changer_link' ), 10, 2 );
118
+
119
+ // Add network menu item
120
+ add_action( 'network_admin_menu', array( &$this, 'add_admin_menu' ) );
121
+ }
122
  }
123
 
124
 
159
  */
160
  public function username_changer_link( $actions, $user ) {
161
  if( current_user_can( 'edit_users' ) ) {
162
+ if( !is_multisite() || ( is_multisite() && !is_network_admin() && !user_can( $user->ID, 'manage_network' ) ) || ( is_multisite() && is_network_admin() ) ) {
163
+ $actions[] = '<a href="' . add_query_arg( array( 'page' => 'username_changer', 'id' => $user->ID ) ) . '">' . __( 'Change Username', 'username-changer' ) . '</a>';
164
+ }
165
  }
166
 
167
  return $actions;
217
  $new_username = $wpdb->escape( $new_username );
218
  $current_username = $wpdb->escape( $_POST['current_username'] );
219
 
220
+ if( username_exists( $current_username ) ) {
221
+ $current_user_data = get_user_by( 'login', $current_username );
222
+
223
+ if( $new_username == $current_username ) {
224
+ // Make sure username exists and username != new username
225
+ echo '<div id="message" class="error"><p><strong>' . sprintf( __( 'Current Username and New Username cannot both be "%1$s"!', 'username-changer' ), $new_username ) . '</strong></p></div>';
226
+ } elseif( username_exists( $new_username ) ) {
227
+ // Make sure new username doesn't exist
228
+ echo '<div id="message" class="error"><p><strong>' . sprintf( __( '"%1$s" cannot be changed to "%2$s", "%3$s" already exists!', 'username-changer' ), $current_username, $new_username, $new_username ) . '</strong></p></div>';
229
+ } elseif( is_multisite() && user_can( $current_user_data->id, 'manage_network' ) && !is_network_admin() ) {
230
+ // Super Admins must be changed from Network Dashboard
231
+ echo '<div id="message" class="error"><p><strong>' . __( '"Super Admin usernames must be changed from the Network Dashboard!', 'username-changer' ) . '</strong></p></div>';
232
+ } elseif( $new_username != $current_username ) {
233
+ // Update username!
234
+ $q = $wpdb->prepare( "UPDATE $wpdb->users SET user_login = %s WHERE user_login = %s", $new_username, $current_username );
235
+ $qnn = $wpdb->prepare( "UPDATE $wpdb->users SET user_nicename = %s WHERE user_login = %s", $new_username, $new_username );
236
+
237
+ // Check if display name is the same as username
238
+ $usersql = $wpdb->prepare( "SELECT * from $wpdb->users WHERE user_login = %s", $current_username );
239
+ $userinfo = $wpdb->get_row( $usersql );
240
+
241
+ // If display name is the same as username, update both
242
+ if( $current_username == $userinfo->display_name ) {
243
+ $qdn = $wpdb->prepare( "UPDATE $wpdb->users SET display_name = %s WHERE user_login = %s", $new_username, $new_username );
244
+ }
245
 
246
+ // If the user is a Super Admin, update their permissions
247
+ if( is_multisite() && is_super_admin( $current_user_data->id ) ) {
248
+ grant_super_admin( $current_user_data->id );
249
  }
250
 
251
+ if( false !== $wpdb->query( $q ) ) {
252
+ $wpdb->query( $qnn );
253
+
254
+ if( isset( $qdn ) ) {
255
+ $wpdb->query( $qdn );
256
+ }
257
+
258
+ // If changing own username, display link to re-login
259
+ if( $current_user->user_login == $current_username ) {
260
+ echo '<div id="message" class="updated fade"><p><strong>' . sprintf( __( 'Username %1$s was changed to %2$s.&nbsp;&nbsp;Click <a href="%3$s">here</a> to log back in.', 'username-changer' ), $current_username, $new_username, wp_login_url() ) . '</strong></p></div>';
261
+ } else {
262
+ echo '<div id="message" class="updated fade"><p><strong>' . sprintf( __( 'Username %1$s was changed to %2$s.', 'username-changer' ), $current_username, $new_username ) . '</strong></p></div>';
263
+ }
264
  } else {
265
+ // If database error occurred, display it
266
+ echo '<div id="message" class="error"><p><strong>' . sprintf( __( 'A database error occurred : %1$s', 'username-changer' ), $wpdb->last_error ) . '</strong></p></div>';
267
  }
 
 
 
268
  }
269
  } else {
270
  // Warn if user doesn't exist (this should never happen!)
314
 
315
  if( $userinfo ) {
316
  foreach( $userinfo as $userinfoObj ) {
317
+ if( !is_multisite() || ( is_multisite() && !is_network_admin() && !user_can( $userinfoObj->ID, 'manage_network' ) ) || ( is_multisite() && is_network_admin() ) ) {
318
+ echo '<option value="' . esc_attr( $userinfoObj->user_login ) . '">' . esc_html( $userinfoObj->user_login ) . ' (' . esc_html( $userinfoObj->user_email ) . ')</option>';
319
+ }
320
  }
321
  }
322