wp/wp-admin/user-new.php
changeset 22 8c2e4d02f4ef
parent 21 48c4eec2b7e6
equal deleted inserted replaced
21:48c4eec2b7e6 22:8c2e4d02f4ef
    32 if ( isset( $_REQUEST['action'] ) && 'adduser' === $_REQUEST['action'] ) {
    32 if ( isset( $_REQUEST['action'] ) && 'adduser' === $_REQUEST['action'] ) {
    33 	check_admin_referer( 'add-user', '_wpnonce_add-user' );
    33 	check_admin_referer( 'add-user', '_wpnonce_add-user' );
    34 
    34 
    35 	$user_details = null;
    35 	$user_details = null;
    36 	$user_email   = wp_unslash( $_REQUEST['email'] );
    36 	$user_email   = wp_unslash( $_REQUEST['email'] );
       
    37 
    37 	if ( str_contains( $user_email, '@' ) ) {
    38 	if ( str_contains( $user_email, '@' ) ) {
    38 		$user_details = get_user_by( 'email', $user_email );
    39 		$user_details = get_user_by( 'email', $user_email );
    39 	} else {
    40 	} else {
    40 		if ( current_user_can( 'manage_network_users' ) ) {
    41 		if ( current_user_can( 'manage_network_users' ) ) {
    41 			$user_details = get_user_by( 'login', $user_email );
    42 			$user_details = get_user_by( 'login', $user_email );
    61 	// Adding an existing user to this blog.
    62 	// Adding an existing user to this blog.
    62 	$new_user_email = array();
    63 	$new_user_email = array();
    63 	$redirect       = 'user-new.php';
    64 	$redirect       = 'user-new.php';
    64 	$username       = $user_details->user_login;
    65 	$username       = $user_details->user_login;
    65 	$user_id        = $user_details->ID;
    66 	$user_id        = $user_details->ID;
    66 	if ( null != $username && array_key_exists( $blog_id, get_blogs_of_user( $user_id ) ) ) {
    67 
       
    68 	if ( array_key_exists( $blog_id, get_blogs_of_user( $user_id ) ) ) {
    67 		$redirect = add_query_arg( array( 'update' => 'addexisting' ), 'user-new.php' );
    69 		$redirect = add_query_arg( array( 'update' => 'addexisting' ), 'user-new.php' );
    68 	} else {
    70 	} else {
    69 		if ( isset( $_POST['noconfirmation'] ) && current_user_can( 'manage_network_users' ) ) {
    71 		if ( isset( $_POST['noconfirmation'] ) && current_user_can( 'manage_network_users' ) ) {
       
    72 
       
    73 			wp_ensure_editable_role( $_REQUEST['role'] );
       
    74 
    70 			$result = add_existing_user_to_blog(
    75 			$result = add_existing_user_to_blog(
    71 				array(
    76 				array(
    72 					'user_id' => $user_id,
    77 					'user_id' => $user_id,
    73 					'role'    => $_REQUEST['role'],
    78 					'role'    => $_REQUEST['role'],
    74 				)
    79 				)
   176 			}
   181 			}
   177 
   182 
   178 			$redirect = add_query_arg( array( 'update' => 'add' ), 'user-new.php' );
   183 			$redirect = add_query_arg( array( 'update' => 'add' ), 'user-new.php' );
   179 		}
   184 		}
   180 	}
   185 	}
       
   186 
   181 	wp_redirect( $redirect );
   187 	wp_redirect( $redirect );
   182 	die();
   188 	die();
   183 } elseif ( isset( $_REQUEST['action'] ) && 'createuser' === $_REQUEST['action'] ) {
   189 } elseif ( isset( $_REQUEST['action'] ) && 'createuser' === $_REQUEST['action'] ) {
   184 	check_admin_referer( 'create-user', '_wpnonce_create-user' );
   190 	check_admin_referer( 'create-user', '_wpnonce_create-user' );
   185 
   191 
   200 			if ( current_user_can( 'list_users' ) ) {
   206 			if ( current_user_can( 'list_users' ) ) {
   201 				$redirect = 'users.php?update=add&id=' . $user_id;
   207 				$redirect = 'users.php?update=add&id=' . $user_id;
   202 			} else {
   208 			} else {
   203 				$redirect = add_query_arg( 'update', 'add', 'user-new.php' );
   209 				$redirect = add_query_arg( 'update', 'add', 'user-new.php' );
   204 			}
   210 			}
       
   211 
   205 			wp_redirect( $redirect );
   212 			wp_redirect( $redirect );
   206 			die();
   213 			die();
   207 		}
   214 		}
   208 	} else {
   215 	} else {
   209 		// Adding a new user to this site.
   216 		// Adding a new user to this site.
   210 		$new_user_email = wp_unslash( $_REQUEST['email'] );
   217 		$new_user_email = wp_unslash( $_REQUEST['email'] );
   211 		$user_details   = wpmu_validate_user_signup( $_REQUEST['user_login'], $new_user_email );
   218 		$user_details   = wpmu_validate_user_signup( $_REQUEST['user_login'], $new_user_email );
       
   219 
   212 		if ( is_wp_error( $user_details['errors'] ) && $user_details['errors']->has_errors() ) {
   220 		if ( is_wp_error( $user_details['errors'] ) && $user_details['errors']->has_errors() ) {
   213 			$add_user_errors = $user_details['errors'];
   221 			$add_user_errors = $user_details['errors'];
   214 		} else {
   222 		} else {
   215 			/** This filter is documented in wp-includes/user.php */
   223 			/** This filter is documented in wp-includes/user.php */
   216 			$new_user_login = apply_filters( 'pre_user_login', sanitize_user( wp_unslash( $_REQUEST['user_login'] ), true ) );
   224 			$new_user_login = apply_filters( 'pre_user_login', sanitize_user( wp_unslash( $_REQUEST['user_login'] ), true ) );
       
   225 
   217 			if ( isset( $_POST['noconfirmation'] ) && current_user_can( 'manage_network_users' ) ) {
   226 			if ( isset( $_POST['noconfirmation'] ) && current_user_can( 'manage_network_users' ) ) {
   218 				add_filter( 'wpmu_signup_user_notification', '__return_false' );  // Disable confirmation email.
   227 				add_filter( 'wpmu_signup_user_notification', '__return_false' );  // Disable confirmation email.
   219 				add_filter( 'wpmu_welcome_user_notification', '__return_false' ); // Disable welcome email.
   228 				add_filter( 'wpmu_welcome_user_notification', '__return_false' ); // Disable welcome email.
   220 			}
   229 			}
       
   230 
       
   231 			wp_ensure_editable_role( $_REQUEST['role'] );
       
   232 
   221 			wpmu_signup_user(
   233 			wpmu_signup_user(
   222 				$new_user_login,
   234 				$new_user_login,
   223 				$new_user_email,
   235 				$new_user_email,
   224 				array(
   236 				array(
   225 					'add_to_blog' => get_current_blog_id(),
   237 					'add_to_blog' => get_current_blog_id(),
   226 					'new_role'    => $_REQUEST['role'],
   238 					'new_role'    => $_REQUEST['role'],
   227 				)
   239 				)
   228 			);
   240 			);
       
   241 
   229 			if ( isset( $_POST['noconfirmation'] ) && current_user_can( 'manage_network_users' ) ) {
   242 			if ( isset( $_POST['noconfirmation'] ) && current_user_can( 'manage_network_users' ) ) {
   230 				$key      = $wpdb->get_var( $wpdb->prepare( "SELECT activation_key FROM {$wpdb->signups} WHERE user_login = %s AND user_email = %s", $new_user_login, $new_user_email ) );
   243 				$key      = $wpdb->get_var( $wpdb->prepare( "SELECT activation_key FROM {$wpdb->signups} WHERE user_login = %s AND user_email = %s", $new_user_login, $new_user_email ) );
   231 				$new_user = wpmu_activate_signup( $key );
   244 				$new_user = wpmu_activate_signup( $key );
   232 				if ( is_wp_error( $new_user ) ) {
   245 				if ( is_wp_error( $new_user ) ) {
   233 					$redirect = add_query_arg( array( 'update' => 'addnoconfirmation' ), 'user-new.php' );
   246 					$redirect = add_query_arg( array( 'update' => 'addnoconfirmation' ), 'user-new.php' );
   243 					);
   256 					);
   244 				}
   257 				}
   245 			} else {
   258 			} else {
   246 				$redirect = add_query_arg( array( 'update' => 'newuserconfirmation' ), 'user-new.php' );
   259 				$redirect = add_query_arg( array( 'update' => 'newuserconfirmation' ), 'user-new.php' );
   247 			}
   260 			}
       
   261 
   248 			wp_redirect( $redirect );
   262 			wp_redirect( $redirect );
   249 			die();
   263 			die();
   250 		}
   264 		}
   251 	}
   265 	}
   252 }
   266 }
   253 
   267 
   254 // Used in the HTML title tag.
   268 // Used in the HTML title tag.
   255 $title       = __( 'Add New User' );
   269 $title       = __( 'Add User' );
   256 $parent_file = 'users.php';
   270 $parent_file = 'users.php';
   257 
   271 
   258 $do_both = false;
   272 $do_both = false;
   259 if ( is_multisite() && current_user_can( 'promote_users' ) && current_user_can( 'create_users' ) ) {
   273 if ( is_multisite() && current_user_can( 'promote_users' ) && current_user_can( 'create_users' ) ) {
   260 	$do_both = true;
   274 	$do_both = true;
   261 }
   275 }
   262 
   276 
   263 $help = '<p>' . __( 'To add a new user to your site, fill in the form on this screen and click the Add New User button at the bottom.' ) . '</p>';
   277 $help = '<p>' . __( 'To add a new user to your site, fill in the form on this screen and click the Add User button at the bottom.' ) . '</p>';
   264 
   278 
   265 if ( is_multisite() ) {
   279 if ( is_multisite() ) {
   266 	$help .= '<p>' . __( 'Because this is a multisite installation, you may add accounts that already exist on the Network by specifying a username or email, and defining a role. For more options, such as specifying a password, you have to be a Network Administrator and use the hover link under an existing user&#8217;s name to Edit the user profile under Network Admin > All Users.' ) . '</p>' .
   280 	$help .= '<p>' . __( 'Because this is a multisite installation, you may add accounts that already exist on the Network by specifying a username or email, and defining a role. For more options, such as specifying a password, you have to be a Network Administrator and use the hover link under an existing user&#8217;s name to Edit the user profile under Network Admin > All Users.' ) . '</p>' .
   267 	'<p>' . __( 'New users will receive an email letting them know they&#8217;ve been added as a user for your site. This email will also contain their password. Check the box if you do not want the user to receive a welcome email.' ) . '</p>';
   281 	'<p>' . __( 'New users will receive an email letting them know they&#8217;ve been added as a user for your site. This email will also contain their password. Check the box if you do not want the user to receive a welcome email.' ) . '</p>';
   268 } else {
   282 } else {
   269 	$help .= '<p>' . __( 'New users are automatically assigned a password, which they can change after logging in. You can view or edit the assigned password by clicking the Show Password button. The username cannot be changed once the user has been added.' ) . '</p>' .
   283 	$help .= '<p>' . __( 'New users are automatically assigned a password, which they can change after logging in. You can view or edit the assigned password by clicking the Show Password button. The username cannot be changed once the user has been added.' ) . '</p>' .
   270 
   284 
   271 	'<p>' . __( 'By default, new users will receive an email letting them know they&#8217;ve been added as a user for your site. This email will also contain a password reset link. Uncheck the box if you do not want to send the new user a welcome email.' ) . '</p>';
   285 	'<p>' . __( 'By default, new users will receive an email letting them know they&#8217;ve been added as a user for your site. This email will also contain a password reset link. Uncheck the box if you do not want to send the new user a welcome email.' ) . '</p>';
   272 }
   286 }
   273 
   287 
   274 $help .= '<p>' . __( 'Remember to click the Add New User button at the bottom of this screen when you are finished.' ) . '</p>';
   288 $help .= '<p>' . __( 'Remember to click the Add User button at the bottom of this screen when you are finished.' ) . '</p>';
   275 
   289 
   276 get_current_screen()->add_help_tab(
   290 get_current_screen()->add_help_tab(
   277 	array(
   291 	array(
   278 		'id'      => 'overview',
   292 		'id'      => 'overview',
   279 		'title'   => __( 'Overview' ),
   293 		'title'   => __( 'Overview' ),
   372 ?>
   386 ?>
   373 <div class="wrap">
   387 <div class="wrap">
   374 <h1 id="add-new-user">
   388 <h1 id="add-new-user">
   375 <?php
   389 <?php
   376 if ( current_user_can( 'create_users' ) ) {
   390 if ( current_user_can( 'create_users' ) ) {
   377 	_e( 'Add New User' );
   391 	_e( 'Add User' );
   378 } elseif ( current_user_can( 'promote_users' ) ) {
   392 } elseif ( current_user_can( 'promote_users' ) ) {
   379 	_e( 'Add Existing User' );
   393 	_e( 'Add Existing User' );
   380 }
   394 }
   381 ?>
   395 ?>
   382 </h1>
   396 </h1>
   496 	<?php
   510 	<?php
   497 } // End if is_multisite().
   511 } // End if is_multisite().
   498 
   512 
   499 if ( current_user_can( 'create_users' ) ) {
   513 if ( current_user_can( 'create_users' ) ) {
   500 	if ( $do_both ) {
   514 	if ( $do_both ) {
   501 		echo '<h2 id="create-new-user">' . __( 'Add New User' ) . '</h2>';
   515 		echo '<h2 id="create-new-user">' . __( 'Add User' ) . '</h2>';
   502 	}
   516 	}
   503 	?>
   517 	?>
   504 <p><?php _e( 'Create a brand new user and add them to this site.' ); ?></p>
   518 <p><?php _e( 'Create a brand new user and add them to this site.' ); ?></p>
   505 <form method="post" name="createuser" id="createuser" class="validate" novalidate="novalidate"
   519 <form method="post" name="createuser" id="createuser" class="validate" novalidate="novalidate"
   506 	<?php
   520 	<?php
   648 	<?php
   662 	<?php
   649 	/** This action is documented in wp-admin/user-new.php */
   663 	/** This action is documented in wp-admin/user-new.php */
   650 	do_action( 'user_new_form', 'add-new-user' );
   664 	do_action( 'user_new_form', 'add-new-user' );
   651 	?>
   665 	?>
   652 
   666 
   653 	<?php submit_button( __( 'Add New User' ), 'primary', 'createuser', true, array( 'id' => 'createusersub' ) ); ?>
   667 	<?php submit_button( __( 'Add User' ), 'primary', 'createuser', true, array( 'id' => 'createusersub' ) ); ?>
   654 
   668 
   655 </form>
   669 </form>
   656 <?php } // End if current_user_can( 'create_users' ). ?>
   670 <?php } // End if current_user_can( 'create_users' ). ?>
   657 </div>
   671 </div>
   658 <?php
   672 <?php