<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[41225] trunk: Multisite: Introduce a `can_add_user_to_blog` filter to prevent adding a user to a site.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/41225">41225</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/41225","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>flixos90</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2017-08-03 21:40:02 +0000 (Thu, 03 Aug 2017)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Multisite: Introduce a `can_add_user_to_blog` filter to prevent adding a user to a site.
Under certain circumstances, it can be necessary that a user should not be added to a site, beyond the restrictions that WordPress core applies. With the new `can_add_user_to_blog` filter, plugin developers can run custom checks and return an error in case of a failure, that will prevent the user from being added.
The user-facing parts and the REST API route that interact with `add_user_to_blog()` have been adjusted accordingly to provide appropriate error feedback when a user could not be added to a site. Furthermore, two existing error feedback messages in the site admin's "New User" screen have been adjusted to properly show inside an error notice instead of a success notice.
Props jmdodd.
Fixes <a href="https://core.trac.wordpress.org/ticket/41101">#41101</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminnetworksiteusersphp">trunk/src/wp-admin/network/site-users.php</a></li>
<li><a href="#trunksrcwpadminusernewphp">trunk/src/wp-admin/user-new.php</a></li>
<li><a href="#trunksrcwpincludesmsfunctionsphp">trunk/src/wp-includes/ms-functions.php</a></li>
<li><a href="#trunksrcwpincludesrestapiendpointsclasswprestuserscontrollerphp">trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php</a></li>
<li><a href="#trunktestsphpunittestsrestapirestuserscontrollerphp">trunk/tests/phpunit/tests/rest-api/rest-users-controller.php</a></li>
<li><a href="#trunktestsphpunittestsusermultisitephp">trunk/tests/phpunit/tests/user/multisite.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminnetworksiteusersphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/network/site-users.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/network/site-users.php 2017-08-03 20:35:38 UTC (rev 41224)
+++ trunk/src/wp-admin/network/site-users.php 2017-08-03 21:40:02 UTC (rev 41225)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -66,16 +66,21 @@
</span><span class="cx" style="display: block; padding: 0 10px"> if ( false === $user_id ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $update = 'err_new_dup';
</span><span class="cx" style="display: block; padding: 0 10px"> } else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- add_user_to_blog( $id, $user_id, $_POST['new_role'] );
- $update = 'newuser';
- /**
- * Fires after a user has been created via the network site-users.php page.
- *
- * @since 4.4.0
- *
- * @param int $user_id ID of the newly created user.
- */
- do_action( 'network_site_users_created_user', $user_id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $result = add_user_to_blog( $id, $user_id, $_POST['new_role'] );
+
+ if ( is_wp_error( $result ) ) {
+ $update = 'err_add_fail';
+ } else {
+ $update = 'newuser';
+ /**
+ * Fires after a user has been created via the network site-users.php page.
+ *
+ * @since 4.4.0
+ *
+ * @param int $user_id ID of the newly created user.
+ */
+ do_action( 'network_site_users_created_user', $user_id );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> break;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -87,10 +92,15 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $newuser = $_POST['newuser'];
</span><span class="cx" style="display: block; padding: 0 10px"> $user = get_user_by( 'login', $newuser );
</span><span class="cx" style="display: block; padding: 0 10px"> if ( $user && $user->exists() ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( ! is_user_member_of_blog( $user->ID, $id ) )
- add_user_to_blog( $id, $user->ID, $_POST['new_role'] );
- else
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! is_user_member_of_blog( $user->ID, $id ) ) {
+ $result = add_user_to_blog( $id, $user->ID, $_POST['new_role'] );
+
+ if ( is_wp_error( $result ) ) {
+ $update = 'err_add_fail';
+ }
+ } else {
</ins><span class="cx" style="display: block; padding: 0 10px"> $update = 'err_add_member';
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> } else {
</span><span class="cx" style="display: block; padding: 0 10px"> $update = 'err_add_notfound';
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -223,6 +233,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> case 'err_add_member':
</span><span class="cx" style="display: block; padding: 0 10px"> echo '<div id="message" class="error notice is-dismissible"><p>' . __( 'User is already a member of this site.' ) . '</p></div>';
</span><span class="cx" style="display: block; padding: 0 10px"> break;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ case 'err_add_fail':
+ echo '<div id="message" class="error notice is-dismissible"><p>' . __( 'User could not be added to this site.' ) . '</p></div>';
+ break;
</ins><span class="cx" style="display: block; padding: 0 10px"> case 'err_add_notfound':
</span><span class="cx" style="display: block; padding: 0 10px"> echo '<div id="message" class="error notice is-dismissible"><p>' . __( 'Enter the username of an existing user.' ) . '</p></div>';
</span><span class="cx" style="display: block; padding: 0 10px"> break;
</span></span></pre></div>
<a id="trunksrcwpadminusernewphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/user-new.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/user-new.php 2017-08-03 20:35:38 UTC (rev 41224)
+++ trunk/src/wp-admin/user-new.php 2017-08-03 21:40:02 UTC (rev 41225)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -67,8 +67,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $redirect = add_query_arg( array('update' => 'addexisting'), 'user-new.php' );
</span><span class="cx" style="display: block; padding: 0 10px"> } else {
</span><span class="cx" style="display: block; padding: 0 10px"> if ( isset( $_POST[ 'noconfirmation' ] ) && current_user_can( 'manage_network_users' ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- add_existing_user_to_blog( array( 'user_id' => $user_id, 'role' => $_REQUEST[ 'role' ] ) );
- $redirect = add_query_arg( array( 'update' => 'addnoconfirmation' , 'user_id' => $user_id ), 'user-new.php' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $result = add_existing_user_to_blog( array( 'user_id' => $user_id, 'role' => $_REQUEST[ 'role' ] ) );
+
+ if ( ! is_wp_error( $result ) ) {
+ $redirect = add_query_arg( array( 'update' => 'addnoconfirmation', 'user_id' => $user_id ), 'user-new.php' );
+ } else {
+ $redirect = add_query_arg( array( 'update' => 'could_not_add' ), 'user-new.php' );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> } else {
</span><span class="cx" style="display: block; padding: 0 10px"> $newuser_key = substr( md5( $user_id ), 0, 5 );
</span><span class="cx" style="display: block; padding: 0 10px"> add_option( 'new_user_' . $newuser_key, array( 'user_id' => $user_id, 'email' => $user_details->user_email, 'role' => $_REQUEST[ 'role' ] ) );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -157,6 +162,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $new_user = wpmu_activate_signup( $key );
</span><span class="cx" style="display: block; padding: 0 10px"> if ( is_wp_error( $new_user ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $redirect = add_query_arg( array( 'update' => 'addnoconfirmation' ), 'user-new.php' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ } elseif ( ! is_user_member_of_blog( $new_user['user_id'] ) ) {
+ $redirect = add_query_arg( array( 'update' => 'created_could_not_add' ), 'user-new.php' );
</ins><span class="cx" style="display: block; padding: 0 10px"> } else {
</span><span class="cx" style="display: block; padding: 0 10px"> $redirect = add_query_arg( array( 'update' => 'addnoconfirmation', 'user_id' => $new_user['user_id'] ), 'user-new.php' );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -261,11 +268,17 @@
</span><span class="cx" style="display: block; padding: 0 10px"> case "addexisting":
</span><span class="cx" style="display: block; padding: 0 10px"> $messages[] = __('That user is already a member of this site.');
</span><span class="cx" style="display: block; padding: 0 10px"> break;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ case "could_not_add":
+ $add_user_errors = new WP_Error( 'could_not_add', __( 'That user could not be added to this site.' ) );
+ break;
+ case "created_could_not_add":
+ $add_user_errors = new WP_Error( 'created_could_not_add', __( 'User has been created, but could not be added to this site.' ) );
+ break;
</ins><span class="cx" style="display: block; padding: 0 10px"> case "does_not_exist":
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $messages[] = __('The requested user does not exist.');
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $add_user_errors = new WP_Error( 'does_not_exist', __( 'The requested user does not exist.' ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> break;
</span><span class="cx" style="display: block; padding: 0 10px"> case "enter_email":
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $messages[] = __('Please enter a valid email address.');
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $add_user_errors = new WP_Error( 'enter_email', __( 'Please enter a valid email address.' ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> break;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> } else {
</span></span></pre></div>
<a id="trunksrcwpincludesmsfunctionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/ms-functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/ms-functions.php 2017-08-03 20:35:38 UTC (rev 41224)
+++ trunk/src/wp-includes/ms-functions.php 2017-08-03 21:40:02 UTC (rev 41225)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -59,9 +59,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> } else {
</span><span class="cx" style="display: block; padding: 0 10px"> //TODO Review this call to add_user_to_blog too - to get here the user must have a role on this blog?
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- add_user_to_blog( $first_blog->userblog_id, $user_id, 'subscriber' );
- update_user_meta( $user_id, 'primary_blog', $first_blog->userblog_id );
- $primary = $first_blog;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $result = add_user_to_blog( $first_blog->userblog_id, $user_id, 'subscriber' );
+
+ if ( ! is_wp_error( $result ) ) {
+ update_user_meta( $user_id, 'primary_blog', $first_blog->userblog_id );
+ $primary = $first_blog;
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ( ! is_object( $primary ) ) || ( $primary->archived == 1 || $primary->spam == 1 || $primary->deleted == 1 ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -160,6 +163,29 @@
</span><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error( 'user_does_not_exist', __( 'The requested user does not exist.' ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * Filters whether a user should be added to a site.
+ *
+ * @since 4.9.0
+ *
+ * @param bool|WP_Error $retval True if the user should be added to the site, false
+ * or error object otherwise.
+ * @param int $user_id User ID.
+ * @param string $role User role.
+ * @param int $blog_id Site ID.
+ */
+ $can_add_user = apply_filters( 'can_add_user_to_blog', true, $user_id, $role, $blog_id );
+
+ if ( true !== $can_add_user ) {
+ restore_current_blog();
+
+ if ( is_wp_error( $can_add_user ) ) {
+ return $can_add_user;
+ }
+
+ return new WP_Error( 'user_cannot_be_added', __( 'User cannot be added to this site.' ) );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( !get_user_meta($user_id, 'primary_blog', true) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> update_user_meta($user_id, 'primary_blog', $blog_id);
</span><span class="cx" style="display: block; padding: 0 10px"> $site = get_site( $blog_id );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2081,15 +2107,19 @@
</span><span class="cx" style="display: block; padding: 0 10px"> if ( is_array( $details ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $blog_id = get_current_blog_id();
</span><span class="cx" style="display: block; padding: 0 10px"> $result = add_user_to_blog( $blog_id, $details[ 'user_id' ], $details[ 'role' ] );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- /**
- * Fires immediately after an existing user is added to a site.
- *
- * @since MU (3.0.0)
- *
- * @param int $user_id User ID.
- * @param mixed $result True on success or a WP_Error object if the user doesn't exist.
- */
- do_action( 'added_existing_user', $details['user_id'], $result );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ if ( ! is_wp_error( $result ) ) {
+ /**
+ * Fires immediately after an existing user is added to a site.
+ *
+ * @since MU (3.0.0)
+ *
+ * @param int $user_id User ID.
+ * @param mixed $result True on success or a WP_Error object if the user doesn't exist.
+ */
+ do_action( 'added_existing_user', $details['user_id'], $result );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> return $result;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2111,9 +2141,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> if ( !empty( $meta[ 'add_to_blog' ] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $blog_id = $meta[ 'add_to_blog' ];
</span><span class="cx" style="display: block; padding: 0 10px"> $role = $meta[ 'new_role' ];
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- remove_user_from_blog($user_id, get_network()->site_id); // remove user from main blog.
- add_user_to_blog( $blog_id, $user_id, $role );
- update_user_meta( $user_id, 'primary_blog', $blog_id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ remove_user_from_blog( $user_id, get_network()->site_id ); // remove user from main blog.
+
+ $result = add_user_to_blog( $blog_id, $user_id, $role );
+
+ if ( ! is_wp_error( $result ) ) {
+ update_user_meta( $user_id, 'primary_blog', $blog_id );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre></div>
<a id="trunksrcwpincludesrestapiendpointsclasswprestuserscontrollerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php 2017-08-03 20:35:38 UTC (rev 41224)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php 2017-08-03 21:40:02 UTC (rev 41225)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -492,7 +492,10 @@
</span><span class="cx" style="display: block; padding: 0 10px"> return $user_id;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- add_user_to_blog( get_site()->id, $user_id, '' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $result= add_user_to_blog( get_site()->id, $user_id, '' );
+ if ( is_wp_error( $result ) ) {
+ return $result;
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> } else {
</span><span class="cx" style="display: block; padding: 0 10px"> $user_id = wp_insert_user( wp_slash( (array) $user ) );
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre></div>
<a id="trunktestsphpunittestsrestapirestuserscontrollerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/rest-api/rest-users-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/rest-users-controller.php 2017-08-03 20:35:38 UTC (rev 41224)
+++ trunk/tests/phpunit/tests/rest-api/rest-users-controller.php 2017-08-03 21:40:02 UTC (rev 41225)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1021,8 +1021,32 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @ticket 41101
</ins><span class="cx" style="display: block; padding: 0 10px"> * @group ms-required
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_create_new_network_user_with_add_user_to_blog_failure() {
+ $this->allow_user_to_manage_multisite();
+
+ $params = array(
+ 'username' => 'testuser123',
+ 'password' => 'testpassword',
+ 'email' => 'test@example.com',
+ 'name' => 'Test User 123',
+ 'roles' => array( 'editor' ),
+ );
+
+ add_filter( 'can_add_user_to_blog', '__return_false' );
+
+ $request = new WP_REST_Request( 'POST', '/wp/v2/users' );
+ $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
+ $request->set_body_params( $params );
+ $response = $this->server->dispatch( $request );
+ $this->assertErrorResponse( 'user_cannot_be_added', $response );
+ }
+
+ /**
+ * @group ms-required
+ */
</ins><span class="cx" style="display: block; padding: 0 10px"> public function test_create_new_network_user_on_sub_site_adds_user_to_site() {
</span><span class="cx" style="display: block; padding: 0 10px"> $this->allow_user_to_manage_multisite();
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre></div>
<a id="trunktestsphpunittestsusermultisitephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/user/multisite.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/user/multisite.php 2017-08-03 20:35:38 UTC (rev 41224)
+++ trunk/tests/phpunit/tests/user/multisite.php 2017-08-03 21:40:02 UTC (rev 41225)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -398,6 +398,32 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @ticket 41101
+ */
+ public function test_should_fail_can_add_user_to_blog_filter() {
+ $site_id = self::factory()->blog->create();
+ $user_id = self::factory()->user->create();
+
+ add_filter( 'can_add_user_to_blog', '__return_false' );
+ $result = add_user_to_blog( $site_id, $user_id, 'subscriber' );
+
+ $this->assertWPError( $result );
+ }
+
+ /**
+ * @ticket 41101
+ */
+ public function test_should_succeed_can_add_user_to_blog_filter() {
+ $site_id = self::factory()->blog->create();
+ $user_id = self::factory()->user->create();
+
+ add_filter( 'can_add_user_to_blog', '__return_true' );
+ $result = add_user_to_blog( $site_id, $user_id, 'subscriber' );
+
+ $this->assertTrue( $result );
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * @ticket 23016
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function test_wp_roles_global_is_reset() {
</span></span></pre>
</div>
</div>
</body>
</html>