Changeset 3048298
- Timestamp:
- 03/09/2024 04:09:47 PM (5 months ago)
- Location:
- wp-edit-username
- Files:
-
- 11 edited
Legend:
- Unmodified
- Added
- Removed
-
wp-edit-username/tags/1.0.5/admin/ajax-handler.php
r3047088 r3048298 5 5 function wpeu_update_user_name() 6 6 { 7 // --------------------------------------------------------- 8 // Check if current_user_can() functions exists in this scope 9 // --------------------------------------------------------- 10 if( ! function_exists( 'wp_get_current_user' ) ) 11 { 12 include( ABSPATH . "wp-includes/pluggable.php" ); 13 } 7 if( ! current_user_can( 'edit_users' ) ) die(); 14 8 15 if( ! current_user_can( 'edit_users' ) ) 16 { 17 return; 18 } 9 if ( isset( $_POST['wp_edit_username_nonce'] ) && wp_verify_nonce( $_POST['wp_edit_username_nonce'], 'wp_edit_username_action' ) ) 10 { 11 $new_username = sanitize_user( $_POST['new_username'] ); 12 13 $current_username = sanitize_user( $_POST['current_username'] ); 19 14 20 $_POST = array_map( 'trim', array_map( 'strip_tags', $_POST ));15 ); 21 16 22 extract( $_POST);17 ); 23 18 24 $response = array(); 19 // --------------------------------------------------------- 20 // check if new_username is empty 21 // --------------------------------------------------------- 22 if( empty( $new_username ) || empty( $current_username ) ) 23 { 24 $response['alert_message'] = __( 'Username Can Not be Empty!', 'wp-edit-username' ); 25 26 wp_send_json( $response ); die(); 27 } 25 28 26 27 // check if new_username is empty 28 29 if( empty( $new_username ) || empty( $current_username ) )30 31 $response['alert_message'] = __( 'Username Can Not be Empty!' );32 33 34 29 // --------------------------------------------------------- 30 // Check if username consists invalid illegal character 31 // --------------------------------------------------------- 32 _username ) ) 33 { 34 ' ); 35 36 wp_send_json( $response ); die(); 37 } 35 38 36 // --------------------------------------------------------- 37 // Check if username consists invalid illegal character 38 // --------------------------------------------------------- 39 if( ! validate_username( $new_username ) ) 40 { 41 $response['alert_message'] = __( 'Username can not have illegal characters' ); 42 43 wp_send_json( $response ); die(); 44 } 39 // --------------------------------------------------------- 40 // Filters the list of blacklisted usernames. 41 // 42 // @https://developer.wordpress.org/reference/hooks/illegal_user_logins/ 43 // --------------------------------------------------------- 44 $illegal_user_logins = array_map( 'strtolower', (array) apply_filters( 'illegal_user_logins', array() ) ); 45 45 46 // --------------------------------------------------------- 47 // Filters the list of blacklisted usernames. 48 // 49 // @https://developer.wordpress.org/reference/hooks/illegal_user_logins/ 50 // --------------------------------------------------------- 51 $illegal_user_logins = array_map( 'strtolower', (array) apply_filters( 'illegal_user_logins', array() ) ); 46 if ( in_array( $new_username, $illegal_user_logins ) ) 47 { 48 $response['alert_message'] = __( 'Sorry, that username is not allowed.', 'wp-edit-username' ); 49 50 wp_send_json( $response ); die(); 51 } 52 52 53 if ( in_array( $new_username, $illegal_user_logins ) ) 54 { 55 $response['alert_message'] = __( 'Sorry, that username is not allowed.' ); 56 57 wp_send_json( $response ); die(); 58 } 53 // --------------------------------------------------------- 54 // If $new_username already registered 55 // --------------------------------------------------------- 56 if( username_exists( $new_username ) ) 57 { 58 $response['alert_message'] = __( 'Sorry, that username already exists and not available.', 'wp-edit-username' ); 59 60 wp_send_json( $response ); die(); 61 } 59 62 60 // --------------------------------------------------------- 61 // If $new_username already registered 62 // --------------------------------------------------------- 63 if( username_exists( $new_username ) ) 64 { 65 $response['alert_message'] = __( 'Sorry, that username already exists and not available.' ); 66 67 wp_send_json( $response ); die(); 68 } 63 global $wpdb; 69 64 70 global $wpdb; 65 // --------------------------------------------------------- 66 // Change / Update Username With old one 67 // --------------------------------------------------------- 68 $query = $wpdb->prepare( "UPDATE $wpdb->users SET user_login = %s WHERE user_login = %s", $new_username, $current_username ); 69 70 $result = $wpdb->query( $query ); 71 71 72 // --------------------------------------------------------- 73 // Change / Update Username With old one 74 // --------------------------------------------------------- 75 $query = $wpdb->prepare( "UPDATE $wpdb->users SET user_login = %s WHERE user_login = %s", $new_username, $current_username ); 76 77 $result = $wpdb->query( $query ); 72 $options = get_option( 'wpeu_register_settings_fields' ); 78 73 79 80 81 74 if ( $result > 0 ) 75 { 76 $response['success_message'] = sprintf( 'Username Updated from <code>%s</code> to <code>%s</code>.', $current_username, $new_username ); 82 77 83 $options = get_option( 'wpeu_register_settings_fields' ); 78 if ( isset( $options['wpeu_send_email_field'] ) && $options['wpeu_send_email_field'] == 'on' ) 79 { 80 $user_email = $wpdb->get_var( $wpdb->prepare( "SELECT user_email FROM $wpdb->users WHERE user_login = %s", $new_username ) ); 84 81 85 if ( $options['wpeu_send_email_field'] == 'on' ) 86 { 87 $user_email = $wpdb->get_var( $wpdb->prepare( "SELECT user_email FROM $wpdb->users WHERE user_login = %s", $new_username ) ); 82 if ( isset( $options['wpeu_email_receiver_field'] ) ) 83 { 84 if ( $options['wpeu_email_receiver_field'] == 'user_only' ) 85 { 86 $to = array( sanitize_email( $user_email ) ); 87 } 88 elseif ( $options['wpeu_email_receiver_field'] == 'admin_only' ) 89 { 90 $to = array(); 91 92 $admins = get_users( 'role=Administrator' ); 93 94 foreach ( $admins as $admin ) 95 { 96 $to[] = sanitize_email( $admin->user_email ); 97 } 98 } 99 elseif ( $options['wpeu_email_receiver_field'] == 'admin_user' ) 100 { 101 $to = array( sanitize_email( $user_email ) ); 102 103 $admins = get_users( 'role=Administrator' ); 104 105 foreach ( $admins as $admin ) 106 { 107 $to[] = sanitize_email( $admin->user_email ); 108 } 109 } 110 } 88 111 89 if ( $options['wpeu_email_receiver_field'] == 'user_only' ) 90 { 91 $to = array( $user_email ); 92 } 93 elseif ( $options['wpeu_email_receiver_field'] == 'admin_only' ) 94 { 95 $to = array(); 96 97 $admins = get_users( 'role=Administrator' ); 98 99 foreach ( $admins as $admin ) 100 { 101 $to[] = $admin->user_email; 102 } 103 } 104 elseif ( $options['wpeu_email_receiver_field'] == 'admin_user' ) 105 { 106 $to = array( $user_email ); 107 108 $admins = get_users( 'role=Administrator' ); 109 110 foreach ( $admins as $admin ) 111 { 112 $to[] = $admin->user_email; 113 } 114 } 112 $subject = apply_filters( 'wp_username_changed_email_subject', $subject = $options['wpeu_email_subject_field'], $old_username, $new_username ); 115 113 116 $subject = apply_filters( "wp_username_changed_email_subject", $subject = $options['wpeu_email_subject_field'], $old_username, $new_username );114 _field'], $old_username, $new_username ); 117 115 118 $body = apply_filters( "wp_username_changed_email_body", $body = $options['wpeu_email_body_field'], $old_username, $new_username ); 116 $user = get_user_by( 'login', $new_username ); 117 118 if( $user ) 119 { 120 $body = str_replace( [ '{{first_name}}', '{{last_name}}', '{{display_name}}', '{{full_name}}', '{{old_username}}', '{{new_username}}' ], [ $user->first_name, $user->last_name, $user->display_name, $user->first_name . ' ' . $user->last_name, $current_username, $new_username ], $body ); 121 } 119 122 120 $user = get_user_by( 'login', $new_username ); 121 122 if( $user ) 123 { 124 $body = str_replace( [ '{{first_name}}', '{{last_name}}', '{{display_name}}', '{{full_name}}', '{{old_username}}', '{{new_username}}' ], [ $user->first_name, $user->last_name, $user->display_name, $user->first_name .' '. $user->last_name, $current_username, $new_username ], $body ); 125 } 123 $headers = array( 'Content-Type: text/html; charset=UTF-8' ); 126 124 127 $headers = array( 'Content-Type: text/html; charset=UTF-8' ); 125 foreach ( $to as $email ) 126 { 127 wp_mail( $email, $subject, $body, $headers ); 128 } 129 } 130 } 128 131 129 foreach ( $to as $email ) 130 { 131 wp_mail( $email, $subject, $body, $headers ); 132 } 133 } 134 } 132 // --------------------------------------------------------- 133 // Change / Update nicename if == username 134 // --------------------------------------------------------- 135 $query = $wpdb->prepare( "UPDATE $wpdb->users SET user_nicename = %s WHERE user_login = %s AND user_nicename = %s", $new_username, $new_username, $current_username ); 136 137 $wpdb->query( $query ); 135 138 136 137 // Change / Update nicename if == username138 139 $query = $wpdb->prepare( "UPDATE $wpdb->users SET user_nicename = %s WHERE user_login = %s AND user_nicename = %s", $new_username, $new_username, $current_username );140 141 139 // --------------------------------------------------------- 140 name if == username 141 // --------------------------------------------------------- 142 name = %s", $new_username, $new_username, $current_username ); 143 144 $wpdb->query( $query ); 142 145 143 // --------------------------------------------------------- 144 // Change / Update display name if == username 145 // --------------------------------------------------------- 146 $query = $wpdb->prepare( "UPDATE $wpdb->users SET display_name = %s WHERE user_login = %s AND display_name = %s", $new_username, $new_username, $current_username ); 147 148 $wpdb->query( $query ); 146 // --------------------------------------------------------- 147 // Update Username on Multisite 148 // --------------------------------------------------------- 149 if( is_multisite() ) 150 { 151 $super_admins = (array) get_site_option( 'site_admins', array( 'admin' ) ); 152 153 $array_key = array_search( $current_username, $super_admins ); 149 154 150 // --------------------------------------------------------- 151 // Update Username on Multisite 152 // --------------------------------------------------------- 153 if( is_multisite() ) 154 { 155 $super_admins = (array) get_site_option( 'site_admins', array( 'admin' ) ); 156 157 $array_key = array_search( $current_username, $super_admins ); 155 if( $array_key ) 156 { 157 $super_admins[ $array_key ] = $new_username; 158 } 159 160 update_site_option( 'site_admins' , $super_admins ); 161 } 162 } 163 else 164 { 165 $response['alert_message'] = __( 'Nonce verification failed.', 'wp-edit-username' ); 166 } 158 167 159 if( $array_key ) 160 { 161 $super_admins[ $array_key ] = $new_username; 162 } 163 164 update_site_option( 'site_admins' , $super_admins ); 165 } 166 167 wp_send_json( $response ); die(); 168 wp_send_json( $response ); die(); 168 169 } -
wp-edit-username/tags/1.0.5/admin/js/script.js
r2976213 r3048298 8 8 9 9 var $wpeu_new_username = $( "#wpeu_new_username" ); 10 11 10 12 11 13 var $wpeu_message = $( "#wpeu_message" ); … … 20 22 21 23 $update_user_name_tigger.removeAttr( 'disabled' ); 22 } 24 }); 23 25 24 26 $( "#cancel_button" ).click( function( event ) … … 27 29 28 30 $wpeu_message.empty().hide(); 29 } 31 }); 30 32 31 33 $wpeu_new_username.keyup( function( event ) … … 39 41 $update_user_name_tigger.removeAttr( 'disabled' ); 40 42 } 41 } 43 }); 42 44 43 45 $( document ).on( 'click', "#update_user_name_tigger", function( e ) … … 50 52 { 51 53 action : 'wpeu_update_user_name', 54 55 52 56 53 57 current_username : $input.val(), … … 76 80 $wpeu_message.addClass( 'alert-success' ).html( msg.success_message ).show(); 77 81 } 78 } 79 } 80 } 82 }); 83 }); 84 }); -
wp-edit-username/tags/1.0.5/admin/modal.php
r2976213 r3048298 20 20 </div> 21 21 <input type="text" class="form-control" id="wpeu_new_username" placeholder="<?php echo __( 'New Username' ); ?>" aria-label="Username" aria-describedby="basic-addon1"> 22 23 22 24 </div> 23 25 </div> -
wp-edit-username/tags/1.0.5/includes/settings.php
r2976213 r3048298 19 19 20 20 <div class="wrap"> 21 22 21 <h2 style="background-image: <?php echo $icon; ?>;background-repeat: no-repeat;background-position: left 12px;background-size: 25px;padding-left: 30px;"> 22 Edit Username Settings 23 23 </h2> 24 24 <form action="options.php" method="post"><?php 25 25 26 26 settings_fields( 'wpeu_settings_group' ); … … 28 28 do_settings_sections( 'wpeu_settings_section' ); 29 29 30 submit_button( 'Save Changes'); ?>30 submit_button(); ?> 31 31 32 32 </form> 33 33 </div> 34 34 <?php } … … 42 42 add_settings_section( 43 43 'wpeu_main_settings_section', 44 ' NotificationsSettings',44 ' Settings', 45 45 'wpeu_main_settings_description', 46 46 'wpeu_settings_section' … … 50 50 add_settings_field( 51 51 'wpeu_send_email_field_setting', 52 'Send Email ',52 'Send Email', 53 53 'wpeu_send_email_field_setting', 54 54 'wpeu_settings_section', … … 59 59 add_settings_field( 60 60 'wpeu_email_receiver_field', 61 'Send Emails To ',61 'Send Emails To', 62 62 'wpeu_email_receiver_field_setting', 63 63 'wpeu_settings_section', … … 68 68 add_settings_field( 69 69 'wpeu_email_subject_field', 70 'Email Subject ',70 'Email Subject', 71 71 'wpeu_email_subject_field_setting', 72 72 'wpeu_settings_section', … … 77 77 add_settings_field( 78 78 'wpeu_email_body_field', 79 'Email Body Text ',79 'Email Body Text', 80 80 'wpeu_email_body_field_setting', 81 81 'wpeu_settings_section', … … 96 96 $options = get_option( 'wpeu_register_settings_fields' ); 97 97 98 $options['wpeu_send_email_field'] = trim( $arr_input['wpeu_send_email_field'] );98 $options['wpeu_send_email_field'] = ( $arr_input['wpeu_send_email_field'] ); 99 99 100 $options['wpeu_email_receiver_field'] = trim( $arr_input['wpeu_email_receiver_field'] );100 $options['wpeu_email_receiver_field'] = ( $arr_input['wpeu_email_receiver_field'] ); 101 101 102 $options['wpeu_email_subject_field'] = trim( $arr_input['wpeu_email_subject_field'] );102 $options['wpeu_email_subject_field'] = ( $arr_input['wpeu_email_subject_field'] ); 103 103 104 $options['wpeu_email_body_field'] = trim( $arr_input['wpeu_email_body_field'] );104 $options['wpeu_email_body_field'] = ( $arr_input['wpeu_email_body_field'] ); 105 105 106 106 return $options; … … 111 111 $options = get_option( 'wpeu_register_settings_fields' ); ?> 112 112 113 113 <input type="checkbox" name="wpeu_register_settings_fields[wpeu_send_email_field]" <?php checked( 'on', $options['wpeu_send_email_field'],true); ?> /> 114 114 <?php } 115 115 … … 118 118 $options = get_option( 'wpeu_register_settings_fields' ); ?> 119 119 120 <input type="radio" name="wpeu_register_settings_fields[wpeu_email_receiver_field]" value="admin_only" <?php checked( 'admin_only' == $options['wpeu_email_receiver_field'] ); ?> /> Admins Only 121 122 <input type="radio" name="wpeu_register_settings_fields[wpeu_email_receiver_field]" value="user_only" <?php checked( 'user_only' == $options['wpeu_email_receiver_field'] ); ?> /> User Only 123 124 <input type="radio" name="wpeu_register_settings_fields[wpeu_email_receiver_field]" value="admin_user" <?php checked( 'admin_user' == $options['wpeu_email_receiver_field'] ); ?> /> Admins & User 120 <input type="radio" name="wpeu_register_settings_fields[wpeu_email_receiver_field]" value="admin_only" <?php checked( 'admin_only' == $options['wpeu_email_receiver_field'] ); ?> /> Admin Only 121 122 <input type="radio" name="wpeu_register_settings_fields[wpeu_email_receiver_field]" value="admin_user" <?php checked( 'admin_user' == $options['wpeu_email_receiver_field'] ); ?> /> Admin & User 125 123 <?php } 126 124 … … 129 127 $options = get_option( 'wpeu_register_settings_fields' ); ?> 130 128 131 <input type="text" class="widefat" name="wpeu_register_settings_fields[wpeu_email_subject_field]" value="<?php if ( isset( $options['wpeu_email_subject_field'] ) ){ echo $options['wpeu_email_subject_field']; } else { echo 'Username Changed!'; } ?>" />129 '; } ?>" /> 132 130 <?php } 133 131 … … 136 134 $options = get_option( 'wpeu_register_settings_fields' ); ?> 137 135 138 <textarea class="widefat" style="min-height: 150px;" name="wpeu_register_settings_fields[wpeu_email_body_field]" ><?php if ( isset( $options['wpeu_email_body_field'] ) ){ echo $options['wpeu_email_body_field']; } else { echo 'Your Username has been changed'; } ?></textarea> 139 140 <p>Available shortcodes are : <code>{{first_name}}</code> <code>{{last_name}}</code> <code>{{display_name}} <code>{{full_name}}</code> <code>{{old_username}}</code> <code>{{new_username}}</code></p> 136 <textarea class="widefat" style="min-height: 150px;" name="wpeu_register_settings_fields[wpeu_email_body_field]" ><?php if ( isset( $options['wpeu_email_body_field'] ) ){ echo esc_textarea( $options['wpeu_email_body_field'] ); } else { echo 'Your Username has been changed'; } ?></textarea> 141 137 <?php } -
wp-edit-username/tags/1.0.6/assets/js/script.js
r3047088 r3048298 8 8 9 9 var $wpeu_new_username = $( "#wpeu_new_username" ); 10 11 10 12 11 13 var $wpeu_message = $( "#wpeu_message" ); … … 49 51 var data = 50 52 { 51 action : 'wpeu_update_user_name',53 action : 'wpeu_update_user_name', 52 54 53 current_username : $input.val(),55 .val(), 54 56 55 new_username : $wpeu_new_username.val() 57 current_username : $input.val(), 58 59 new_username : $wpeu_new_username.val() 56 60 }; 57 61 -
wp-edit-username/tags/1.0.6/wp-edit-username.php
r3047088 r3048298 222 222 public function email_subject_field_setting() 223 223 { 224 ?><input type="text" class="widefat" name="wpeu_register_settings_fields[wpeu_email_subject_field]" value="<?php if ( isset( $this->options['wpeu_email_subject_field'] ) ){ echo esc_ html__( $this->options['wpeu_email_subject_field'] ); } else { _e( 'Username Changed!', 'wp-edit-username' ); } ?>" /><?php224 ?><input type="text" class="widefat" name="wpeu_register_settings_fields[wpeu_email_subject_field]" value="<?php if ( isset( $this->options['wpeu_email_subject_field'] ) ){ echo esc_( $this->options['wpeu_email_subject_field'] ); } else { _e( 'Username Changed!', 'wp-edit-username' ); } ?>" /><?php 225 225 } 226 226 … … 228 228 { 229 229 ?> 230 <textarea class="widefat" style="min-height: 150px;" name="wpeu_register_settings_fields[wpeu_email_body_field]" ><?php if ( isset( $this->options['wpeu_email_body_field'] ) ){ echo esc_ html( $this->options['wpeu_email_body_field'] ); } else { _e( 'Your Username has been changed', 'wp-edit-username' ); } ?></textarea>230 <textarea class="widefat" style="min-height: 150px;" name="wpeu_register_settings_fields[wpeu_email_body_field]" ><?php if ( isset( $this->options['wpeu_email_body_field'] ) ){ echo esc_( $this->options['wpeu_email_body_field'] ); } else { _e( 'Your Username has been changed', 'wp-edit-username' ); } ?></textarea> 231 231 232 232 <p><?php _e( 'Available shortcodes are', 'wp-edit-username' ); ?> : <code>{{first_name}}</code> <code>{{last_name}}</code> <code>{{display_name}}</code> <code>{{full_name}}</code> <code>{{old_username}}</code> <code>{{new_username}}</code></p> … … 277 277 </div> 278 278 <input type="text" class="form-control" id="wpeu_new_username" placeholder="<?php echo __( 'New Username', 'wp-edit-username' ); ?>" aria-label="Username" aria-describedby="basic-addon1"> 279 280 279 281 </div> 280 282 </div> … … 297 299 public function update_user_name() 298 300 { 301 302 303 304 299 305 if( ! current_user_can( 'edit_users' ) ) die(); 300 306 301 $_POST = array_map( 'trim', array_map( 'strip_tags', $_POST ) ); 302 303 extract( $_POST ); 304 305 $response = array(); 306 307 $to = array(); 308 309 $new_username = sanitize_user( $new_username ); 310 311 $current_username = sanitize_user( $current_username ); 312 313 // --------------------------------------------------------- 314 // check if new_username is empty 315 // --------------------------------------------------------- 316 if( empty( $new_username ) || empty( $current_username ) ) 307 if ( ! isset( $_POST['new_username'] ) && ! isset( $_POST['current_username'] ) ) 317 308 { 318 $response['alert_message'] = __( 'Username Can Not be Empty!', 'wp-edit-username' ); 309 $response['alert_message'] = __( 'Invalid fields!', 'wp-edit-username' ); 310 311 wp_send_json( $response ); die(); 312 } 313 314 if ( isset( $_POST['wp_edit_username_nonce'] ) && wp_verify_nonce( $_POST['wp_edit_username_nonce'], 'wp_edit_username_action' ) ) 315 { 316 $new_username = sanitize_user( $_POST['new_username'] ); 319 317 320 wp_send_json( $response ); die(); 321 } 322 323 // --------------------------------------------------------- 324 // Check if username consists invalid illegal character 325 // --------------------------------------------------------- 326 if( ! validate_username( $new_username ) ) 327 { 328 $response['alert_message'] = __( 'Username can not have illegal characters', 'wp-edit-username' ); 318 $current_username = sanitize_user( $_POST['current_username'] ); 319 320 // --------------------------------------------------------- 321 // check if new_username is empty 322 // --------------------------------------------------------- 323 if( empty( $new_username ) || empty( $current_username ) ) 324 { 325 $response['alert_message'] = __( 'Username Can Not be Empty!', 'wp-edit-username' ); 326 327 wp_send_json( $response ); die(); 328 } 329 330 // --------------------------------------------------------- 331 // Check if username consists invalid illegal character 332 // --------------------------------------------------------- 333 if( ! validate_username( $new_username ) ) 334 { 335 $response['alert_message'] = __( 'Username can not have illegal characters', 'wp-edit-username' ); 336 337 wp_send_json( $response ); die(); 338 } 339 340 // --------------------------------------------------------- 341 // Filters the list of blacklisted usernames. 342 // 343 // @https://developer.wordpress.org/reference/hooks/illegal_user_logins/ 344 // --------------------------------------------------------- 345 $illegal_user_logins = array_map( 'strtolower', (array) apply_filters( 'illegal_user_logins', array() ) ); 346 347 if ( in_array( $new_username, $illegal_user_logins ) ) 348 { 349 $response['alert_message'] = __( 'Sorry, that username is not allowed.', 'wp-edit-username' ); 350 351 wp_send_json( $response ); die(); 352 } 353 354 // --------------------------------------------------------- 355 // If $new_username already registered 356 // --------------------------------------------------------- 357 if( username_exists( $new_username ) ) 358 { 359 $response['alert_message'] = __( 'Sorry, that username already exists and not available.', 'wp-edit-username' ); 360 361 wp_send_json( $response ); die(); 362 } 363 364 global $wpdb; 365 366 // --------------------------------------------------------- 367 // Change / Update Username With old one 368 // --------------------------------------------------------- 369 $query = $wpdb->prepare( "UPDATE $wpdb->users SET user_login = %s WHERE user_login = %s", $new_username, $current_username ); 329 370 330 wp_send_json( $response ); die(); 331 } 332 333 // --------------------------------------------------------- 334 // Filters the list of blacklisted usernames. 335 // 336 // @https://developer.wordpress.org/reference/hooks/illegal_user_logins/ 337 // --------------------------------------------------------- 338 $illegal_user_logins = array_map( 'strtolower', (array) apply_filters( 'illegal_user_logins', array() ) ); 339 340 if ( in_array( $new_username, $illegal_user_logins ) ) 341 { 342 $response['alert_message'] = __( 'Sorry, that username is not allowed.', 'wp-edit-username' ); 343 344 wp_send_json( $response ); die(); 345 } 346 347 // --------------------------------------------------------- 348 // If $new_username already registered 349 // --------------------------------------------------------- 350 if( username_exists( $new_username ) ) 351 { 352 $response['alert_message'] = __( 'Sorry, that username already exists and not available.', 'wp-edit-username' ); 353 354 wp_send_json( $response ); die(); 355 } 356 357 global $wpdb; 358 359 // --------------------------------------------------------- 360 // Change / Update Username With old one 361 // --------------------------------------------------------- 362 $query = $wpdb->prepare( "UPDATE $wpdb->users SET user_login = %s WHERE user_login = %s", $new_username, $current_username ); 363 364 $result = $wpdb->query( $query ); 365 366 if ( $result > 0 ) 367 { 368 $response['success_message'] = sprintf( 'Username Updated from <code>%s</code> to <code>%s</code>.', $current_username, $new_username ); 369 370 if ( isset( $this->options['wpeu_send_email_field'] ) && $this->options['wpeu_send_email_field'] == 'on' ) 371 { 372 $user_email = $wpdb->get_var( $wpdb->prepare( "SELECT user_email FROM $wpdb->users WHERE user_login = %s", $new_username ) ); 373 374 if ( isset( $this->options['wpeu_email_receiver_field'] ) ) 375 { 376 if ( $this->options['wpeu_email_receiver_field'] == 'user_only' ) 377 { 378 $to = array( sanitize_email( $user_email ) ); 379 } 380 elseif ( $this->options['wpeu_email_receiver_field'] == 'admin_only' ) 381 { 382 $to = array(); 383 384 $admins = get_users( 'role=Administrator' ); 385 386 foreach ( $admins as $admin ) 371 $result = $wpdb->query( $query ); 372 373 if ( $result > 0 ) 374 { 375 $response['success_message'] = sprintf( 'Username Updated from <code>%s</code> to <code>%s</code>.', $current_username, $new_username ); 376 377 if ( isset( $this->options['wpeu_send_email_field'] ) && $this->options['wpeu_send_email_field'] == 'on' ) 378 { 379 $user_email = $wpdb->get_var( $wpdb->prepare( "SELECT user_email FROM $wpdb->users WHERE user_login = %s", $new_username ) ); 380 381 if ( isset( $this->options['wpeu_email_receiver_field'] ) ) 382 { 383 if ( $this->options['wpeu_email_receiver_field'] == 'user_only' ) 384 { 385 $to = array( sanitize_email( $user_email ) ); 386 } 387 elseif ( $this->options['wpeu_email_receiver_field'] == 'admin_only' ) 388 { 389 $to = array(); 390 391 $admins = get_users( 'role=Administrator' ); 392 393 foreach ( $admins as $admin ) 394 { 395 $to[] = sanitize_email( $admin->user_email ); 396 } 397 } 398 elseif ( $this->options['wpeu_email_receiver_field'] == 'admin_user' ) 387 399 { 388 $to[] = sanitize_email( $admin->user_email ); 400 $to = array( sanitize_email( $user_email ) ); 401 402 $admins = get_users( 'role=Administrator' ); 403 404 foreach ( $admins as $admin ) 405 { 406 $to[] = sanitize_email( $admin->user_email ); 407 } 389 408 } 390 409 } 391 elseif ( $this->options['wpeu_email_receiver_field'] == 'admin_user' ) 410 411 $subject = apply_filters( 'wp_username_changed_email_subject', $subject = $this->options['wpeu_email_subject_field'], $old_username, $new_username ); 412 413 $body = apply_filters( 'wp_username_changed_email_body', $body = $this->options['wpeu_email_body_field'], $old_username, $new_username ); 414 415 $user = get_user_by( 'login', $new_username ); 416 417 if( $user ) 392 418 { 393 $ to = array( sanitize_email( $user_email ));394 395 $admins = get_users( 'role=Administrator' ); 396 397 foreach ( $admins as $admin ) 398 {399 $to[] = sanitize_email( $admin->user_email );400 }419 $ ); 420 421 422 423 424 425 426 401 427 } 402 428 } 403 404 $subject = apply_filters( 'wp_username_changed_email_subject', $subject = $this->options['wpeu_email_subject_field'], $old_username, $new_username ); 405 406 $body = apply_filters( 'wp_username_changed_email_body', $body = $this->options['wpeu_email_body_field'], $old_username, $new_username ); 407 408 $user = get_user_by( 'login', $new_username ); 409 410 if( $user ) 429 } 430 431 // --------------------------------------------------------- 432 // Change / Update nicename if == username 433 // --------------------------------------------------------- 434 $query = $wpdb->prepare( "UPDATE $wpdb->users SET user_nicename = %s WHERE user_login = %s AND user_nicename = %s", $new_username, $new_username, $current_username ); 435 436 $wpdb->query( $query ); 437 438 // --------------------------------------------------------- 439 // Change / Update display name if == username 440 // --------------------------------------------------------- 441 $query = $wpdb->prepare( "UPDATE $wpdb->users SET display_name = %s WHERE user_login = %s AND display_name = %s", $new_username, $new_username, $current_username ); 442 443 $wpdb->query( $query ); 444 445 // --------------------------------------------------------- 446 // Update Username on Multisite 447 // --------------------------------------------------------- 448 if( is_multisite() ) 449 { 450 $super_admins = (array) get_site_option( 'site_admins', array( 'admin' ) ); 451 452 $array_key = array_search( $current_username, $super_admins ); 453 454 if( $array_key ) 411 455 { 412 $ body = str_replace( [ '{{first_name}}', '{{last_name}}', '{{display_name}}', '{{full_name}}', '{{old_username}}', '{{new_username}}' ], [ $user->first_name, $user->last_name, $user->display_name, $user->first_name . ' ' . $user->last_name, $current_username, $new_username ], $body );456 $; 413 457 } 414 415 $headers = array( 'Content-Type: text/html; charset=UTF-8' ); 416 417 foreach ( $to as $email ) 418 { 419 wp_mail( $email, $subject, $body, $headers ); 420 } 421 } 422 } 423 424 // --------------------------------------------------------- 425 // Change / Update nicename if == username 426 // --------------------------------------------------------- 427 $query = $wpdb->prepare( "UPDATE $wpdb->users SET user_nicename = %s WHERE user_login = %s AND user_nicename = %s", $new_username, $new_username, $current_username ); 428 429 $wpdb->query( $query ); 430 431 // --------------------------------------------------------- 432 // Change / Update display name if == username 433 // --------------------------------------------------------- 434 $query = $wpdb->prepare( "UPDATE $wpdb->users SET display_name = %s WHERE user_login = %s AND display_name = %s", $new_username, $new_username, $current_username ); 435 436 $wpdb->query( $query ); 437 438 // --------------------------------------------------------- 439 // Update Username on Multisite 440 // --------------------------------------------------------- 441 if( is_multisite() ) 458 459 update_site_option( 'site_admins' , $super_admins ); 460 } 461 } 462 else 442 463 { 443 $super_admins = (array) get_site_option( 'site_admins', array( 'admin' ) ); 444 445 $array_key = array_search( $current_username, $super_admins ); 446 447 if( $array_key ) 448 { 449 $super_admins[ $array_key ] = $new_username; 450 } 451 452 update_site_option( 'site_admins' , $super_admins ); 464 $response['alert_message'] = __( 'Nonce verification failed.', 'wp-edit-username' ); 453 465 } 454 466 -
wp-edit-username/trunk/admin/ajax-handler.php
r2721337 r3048298 5 5 function wpeu_update_user_name() 6 6 { 7 // --------------------------------------------------------- 8 // Check if current_user_can() functions exists in this scope 9 // --------------------------------------------------------- 10 if( ! function_exists( 'wp_get_current_user' ) ) 7 if( ! current_user_can( 'edit_users' ) ) die(); 8 9 if ( isset( $_POST['wp_edit_username_nonce'] ) && wp_verify_nonce( $_POST['wp_edit_username_nonce'], 'wp_edit_username_action' ) ) 11 10 { 12 include( ABSPATH . "wp-includes/pluggable.php" ); 13 } 11 $new_username = sanitize_user( $_POST['new_username'] ); 12 13 $current_username = sanitize_user( $_POST['current_username'] ); 14 14 15 if( ! current_user_can( 'edit_users' ) ) 16 { 17 return; 18 } 15 $response = array(); 19 16 20 $_POST = array_map( 'trim', array_map( 'strip_tags', $_POST ));17 ); 21 18 22 extract( $_POST ); 19 // --------------------------------------------------------- 20 // check if new_username is empty 21 // --------------------------------------------------------- 22 if( empty( $new_username ) || empty( $current_username ) ) 23 { 24 $response['alert_message'] = __( 'Username Can Not be Empty!', 'wp-edit-username' ); 25 26 wp_send_json( $response ); die(); 27 } 23 28 24 $response = array(); 29 // --------------------------------------------------------- 30 // Check if username consists invalid illegal character 31 // --------------------------------------------------------- 32 if( ! validate_username( $new_username ) ) 33 { 34 $response['alert_message'] = __( 'Username can not have illegal characters', 'wp-edit-username' ); 35 36 wp_send_json( $response ); die(); 37 } 25 38 26 // --------------------------------------------------------- 27 // check if new_username is empty 28 // --------------------------------------------------------- 29 if( empty( $new_username ) || empty( $current_username ) ) 30 { 31 $response['alert_message'] = __( 'Username Can Not be Empty!' ); 39 // --------------------------------------------------------- 40 // Filters the list of blacklisted usernames. 41 // 42 // @https://developer.wordpress.org/reference/hooks/illegal_user_logins/ 43 // --------------------------------------------------------- 44 $illegal_user_logins = array_map( 'strtolower', (array) apply_filters( 'illegal_user_logins', array() ) ); 45 46 if ( in_array( $new_username, $illegal_user_logins ) ) 47 { 48 $response['alert_message'] = __( 'Sorry, that username is not allowed.', 'wp-edit-username' ); 49 50 wp_send_json( $response ); die(); 51 } 52 53 // --------------------------------------------------------- 54 // If $new_username already registered 55 // --------------------------------------------------------- 56 if( username_exists( $new_username ) ) 57 { 58 $response['alert_message'] = __( 'Sorry, that username already exists and not available.', 'wp-edit-username' ); 59 60 wp_send_json( $response ); die(); 61 } 62 63 global $wpdb; 64 65 // --------------------------------------------------------- 66 // Change / Update Username With old one 67 // --------------------------------------------------------- 68 $query = $wpdb->prepare( "UPDATE $wpdb->users SET user_login = %s WHERE user_login = %s", $new_username, $current_username ); 32 69 33 wp_send_json( $response ); die(); 34 } 35 36 // --------------------------------------------------------- 37 // Check if username consists invalid illegal character 38 // --------------------------------------------------------- 39 if( ! validate_username( $new_username ) ) 40 { 41 $response['alert_message'] = __( 'Username can not have illegal characters' ); 42 43 wp_send_json( $response ); die(); 44 } 45 46 // --------------------------------------------------------- 47 // Filters the list of blacklisted usernames. 48 // 49 // @https://developer.wordpress.org/reference/hooks/illegal_user_logins/ 50 // --------------------------------------------------------- 51 $illegal_user_logins = array_map( 'strtolower', (array) apply_filters( 'illegal_user_logins', array() ) ); 52 53 if ( in_array( $new_username, $illegal_user_logins ) ) 54 { 55 $response['alert_message'] = __( 'Sorry, that username is not allowed.' ); 56 57 wp_send_json($response); die(); 58 } 59 60 // --------------------------------------------------------- 61 // If $new_username already registered 62 // --------------------------------------------------------- 63 if( username_exists( $new_username ) ) 64 { 65 $response['alert_message'] = __( 'Sorry, that username already exists and not available.' ); 66 67 wp_send_json( $response ); die(); 68 } 69 70 global $wpdb; 71 72 // --------------------------------------------------------- 73 // Change / Update Username With old one 74 // --------------------------------------------------------- 75 $query = $wpdb->prepare( "UPDATE $wpdb->users SET user_login = %s WHERE user_login = %s", $new_username, $current_username ); 76 77 $result = $wpdb->query( $query ); 78 79 if ( $result > 0 ) 80 { 81 $response['success_message'] = sprintf( 'Username Updated from <code>%s</code> to <code>%s</code>.', $current_username, $new_username ); 70 $result = $wpdb->query( $query ); 82 71 83 72 $options = get_option( 'wpeu_register_settings_fields' ); 84 73 85 if ( $ options['wpeu_send_email_field'] == 'on')74 if ( $ ) 86 75 { 87 $ email = $wpdb->get_var( $wpdb->prepare( "SELECT user_email FROM $wpdb->users WHERE user_login = %s", $new_username ));76 $ ); 88 77 89 if ( $options['wpeu_email_receiver_field'] == 'admin_only' ) 90 { 91 $to = array( $email ); 92 } 93 elseif ( $options['wpeu_email_receiver_field'] == 'admin_user' ) 78 if ( isset( $options['wpeu_send_email_field'] ) && $options['wpeu_send_email_field'] == 'on' ) 94 79 { 95 $to = array( $email ); 96 97 $admins = get_users( 'role=Administrator' ); 98 99 foreach ( $admins as $admin ) 80 $user_email = $wpdb->get_var( $wpdb->prepare( "SELECT user_email FROM $wpdb->users WHERE user_login = %s", $new_username ) ); 81 82 if ( isset( $options['wpeu_email_receiver_field'] ) ) 83 { 84 if ( $options['wpeu_email_receiver_field'] == 'user_only' ) 85 { 86 $to = array( sanitize_email( $user_email ) ); 87 } 88 elseif ( $options['wpeu_email_receiver_field'] == 'admin_only' ) 89 { 90 $to = array(); 91 92 $admins = get_users( 'role=Administrator' ); 93 94 foreach ( $admins as $admin ) 95 { 96 $to[] = sanitize_email( $admin->user_email ); 97 } 98 } 99 elseif ( $options['wpeu_email_receiver_field'] == 'admin_user' ) 100 { 101 $to = array( sanitize_email( $user_email ) ); 102 103 $admins = get_users( 'role=Administrator' ); 104 105 foreach ( $admins as $admin ) 106 { 107 $to[] = sanitize_email( $admin->user_email ); 108 } 109 } 110 } 111 112 $subject = apply_filters( 'wp_username_changed_email_subject', $subject = $options['wpeu_email_subject_field'], $old_username, $new_username ); 113 114 $body = apply_filters( 'wp_username_changed_email_body', $body = $options['wpeu_email_body_field'], $old_username, $new_username ); 115 116 $user = get_user_by( 'login', $new_username ); 117 118 if( $user ) 100 119 { 101 $to[] = $admin->user_email; 120 $body = str_replace( [ '{{first_name}}', '{{last_name}}', '{{display_name}}', '{{full_name}}', '{{old_username}}', '{{new_username}}' ], [ $user->first_name, $user->last_name, $user->display_name, $user->first_name . ' ' . $user->last_name, $current_username, $new_username ], $body ); 121 } 122 123 $headers = array( 'Content-Type: text/html; charset=UTF-8' ); 124 125 foreach ( $to as $email ) 126 { 127 wp_mail( $email, $subject, $body, $headers ); 102 128 } 103 129 } 130 104 131 105 $subject = apply_filters( "wp_username_changed_email_subject", $subject = $options['wpeu_email_subject_field'] ); 132 // --------------------------------------------------------- 133 // Change / Update nicename if == username 134 // --------------------------------------------------------- 135 $query = $wpdb->prepare( "UPDATE $wpdb->users SET user_nicename = %s WHERE user_login = %s AND user_nicename = %s", $new_username, $new_username, $current_username ); 136 137 $wpdb->query( $query ); 106 138 107 $body = apply_filters( "wp_username_changed_email_body", $body = $options['wpeu_email_body_field'] ); 139 // --------------------------------------------------------- 140 // Change / Update display name if == username 141 // --------------------------------------------------------- 142 $query = $wpdb->prepare( "UPDATE $wpdb->users SET display_name = %s WHERE user_login = %s AND display_name = %s", $new_username, $new_username, $current_username ); 143 144 $wpdb->query( $query ); 145 146 // --------------------------------------------------------- 147 // Update Username on Multisite 148 // --------------------------------------------------------- 149 if( is_multisite() ) 150 { 151 $super_admins = (array) get_site_option( 'site_admins', array( 'admin' ) ); 108 152 109 $ body .= __( " Old Username was : $old_username .\nNew Username is : $new_username");153 $ ); 110 154 111 $headers = array( 'Content-Type: text/html; charset=UTF-8' ); 112 113 foreach ( $to as $email ) 155 if( $array_key ) 114 156 { 115 wp_mail( $email, $subject, $body, $headers );157 ; 116 158 } 159 160 117 161 } 118 162 } 119 120 // --------------------------------------------------------- 121 // Change / Update nicename if == username 122 // --------------------------------------------------------- 123 $query = $wpdb->prepare( "UPDATE $wpdb->users SET user_nicename = %s WHERE user_login = %s AND user_nicename = %s", $new_username, $new_username, $current_username ); 124 125 $wpdb->query( $query ); 126 127 // --------------------------------------------------------- 128 // Change / Update display name if == username 129 // --------------------------------------------------------- 130 $query = $wpdb->prepare( "UPDATE $wpdb->users SET display_name = %s WHERE user_login = %s AND display_name = %s", $new_username, $new_username, $current_username ); 131 132 $wpdb->query( $query ); 133 134 // --------------------------------------------------------- 135 // Update Username on Multisite 136 // --------------------------------------------------------- 137 if( is_multisite() ) 163 else 138 164 { 139 $super_admins = (array) get_site_option( 'site_admins', array( 'admin' ) ); 140 141 $array_key = array_search( $current_username, $super_admins ); 142 143 if( $array_key ) 144 { 145 $super_admins[ $array_key ] = $new_username; 146 } 147 148 update_site_option( 'site_admins' , $super_admins ); 165 $response['alert_message'] = __( 'Nonce verification failed.', 'wp-edit-username' ); 149 166 } 150 167 -
wp-edit-username/trunk/admin/js/script.js
r2721335 r3048298 8 8 9 9 var $wpeu_new_username = $( "#wpeu_new_username" ); 10 11 10 12 11 13 var $wpeu_message = $( "#wpeu_message" ); … … 50 52 { 51 53 action : 'wpeu_update_user_name', 54 55 52 56 53 57 current_username : $input.val(), -
wp-edit-username/trunk/admin/modal.php
r2735092 r3048298 20 20 </div> 21 21 <input type="text" class="form-control" id="wpeu_new_username" placeholder="<?php echo __( 'New Username' ); ?>" aria-label="Username" aria-describedby="basic-addon1"> 22 23 22 24 </div> 23 25 </div> -
wp-edit-username/trunk/includes/settings.php
r2735092 r3048298 96 96 $options = get_option( 'wpeu_register_settings_fields' ); 97 97 98 $options['wpeu_send_email_field'] = trim( $arr_input['wpeu_send_email_field'] );98 $options['wpeu_send_email_field'] = ( $arr_input['wpeu_send_email_field'] ); 99 99 100 $options['wpeu_email_receiver_field'] = trim( $arr_input['wpeu_email_receiver_field'] );100 $options['wpeu_email_receiver_field'] = ( $arr_input['wpeu_email_receiver_field'] ); 101 101 102 $options['wpeu_email_subject_field'] = trim( $arr_input['wpeu_email_subject_field'] );102 $options['wpeu_email_subject_field'] = ( $arr_input['wpeu_email_subject_field'] ); 103 103 104 $options['wpeu_email_body_field'] = trim( $arr_input['wpeu_email_body_field'] );104 $options['wpeu_email_body_field'] = ( $arr_input['wpeu_email_body_field'] ); 105 105 106 106 return $options; … … 127 127 $options = get_option( 'wpeu_register_settings_fields' ); ?> 128 128 129 <input type="text" class="widefat" name="wpeu_register_settings_fields[wpeu_email_subject_field]" value="<?php if ( isset( $options['wpeu_email_subject_field'] ) ){ echo $options['wpeu_email_subject_field']; } else { echo 'subject'; } ?>" />129 <input type="text" class="widefat" name="wpeu_register_settings_fields[wpeu_email_subject_field]" value="<?php if ( isset( $options['wpeu_email_subject_field'] ) ){ echo ; } else { echo 'subject'; } ?>" /> 130 130 <?php } 131 131 … … 134 134 $options = get_option( 'wpeu_register_settings_fields' ); ?> 135 135 136 <textarea class="widefat" style="min-height: 150px;" name="wpeu_register_settings_fields[wpeu_email_body_field]" ><?php if ( isset( $options['wpeu_email_body_field'] ) ){ echo $options['wpeu_email_body_field']; } else { echo 'Your Username has been changed'; } ?></textarea>136 <textarea class="widefat" style="min-height: 150px;" name="wpeu_register_settings_fields[wpeu_email_body_field]" ><?php if ( isset( $options['wpeu_email_body_field'] ) ){ echo ; } else { echo 'Your Username has been changed'; } ?></textarea> 137 137 <?php } -
wp-edit-username/trunk/readme.txt
r3047088 r3048298 3 3 Tags: user,user-profile,profile-edit,edit,ajax,update,change-username,username 4 4 Requires at least: 5.6 5 Tested up to: 6. 35 Tested up to: 6. 6 6 Stable tag: 1.0.6 7 7 License: GPLv2
Note: See TracChangeset
for help on using the changeset viewer.