Ticket #41101: 41101.2.diff
File 41101.2.diff, 8.6 KB (added by , 7 years ago) |
---|
-
src/wp-admin/network/site-users.php
66 66 if ( false === $user_id ) { 67 67 $update = 'err_new_dup'; 68 68 } else { 69 add_user_to_blog( $id, $user_id, $_POST['new_role'] ); 70 $update = 'newuser'; 71 /** 72 * Fires after a user has been created via the network site-users.php page. 73 * 74 * @since 4.4.0 75 * 76 * @param int $user_id ID of the newly created user. 77 */ 78 do_action( 'network_site_users_created_user', $user_id ); 69 if ( is_wp_error( add_user_to_blog( $id, $user_id, $_POST['new_role'] ) ) ) { 70 $update = 'err_add_fail'; 71 } else { 72 $update = 'newuser'; 73 /** 74 * Fires after a user has been created via the network site-users.php page. 75 * 76 * @since 4.4.0 77 * 78 * @param int $user_id ID of the newly created user. 79 */ 80 do_action( 'network_site_users_created_user', $user_id ); 81 } 79 82 } 80 83 } 81 84 break; … … 87 90 $newuser = $_POST['newuser']; 88 91 $user = get_user_by( 'login', $newuser ); 89 92 if ( $user && $user->exists() ) { 90 if ( ! is_user_member_of_blog( $user->ID, $id ) ) 91 add_user_to_blog( $id, $user->ID, $_POST['new_role'] ); 92 else 93 if ( ! is_user_member_of_blog( $user->ID, $id ) ) { 94 if ( is_wp_error( add_user_to_blog( $id, $user->ID, $_POST['new_role'] ) ) ) { 95 $update = 'err_add_fail'; 96 } 97 } else { 93 98 $update = 'err_add_member'; 99 } 94 100 } else { 95 101 $update = 'err_add_notfound'; 96 102 } … … 223 229 case 'err_add_member': 224 230 echo '<div id="message" class="error notice is-dismissible"><p>' . __( 'User is already a member of this site.' ) . '</p></div>'; 225 231 break; 232 case 'err_add_fail': 233 echo '<div id="message" class="error notice is-dismissible"><p>' . __( 'User could not be added to this site.' ) . '</p></div>'; 234 break; 226 235 case 'err_add_notfound': 227 236 echo '<div id="message" class="error notice is-dismissible"><p>' . __( 'Enter the username of an existing user.' ) . '</p></div>'; 228 237 break; -
src/wp-admin/user-new.php
67 67 $redirect = add_query_arg( array('update' => 'addexisting'), 'user-new.php' ); 68 68 } else { 69 69 if ( isset( $_POST[ 'noconfirmation' ] ) && current_user_can( 'manage_network_users' ) ) { 70 add_existing_user_to_blog( array( 'user_id' => $user_id, 'role' => $_REQUEST[ 'role' ] ) ); 71 $redirect = add_query_arg( array( 'update' => 'addnoconfirmation' , 'user_id' => $user_id ), 'user-new.php' ); 70 if ( ! is_wp_error( add_existing_user_to_blog( array( 'user_id' => $user_id, 'role' => $_REQUEST[ 'role' ] ) ) ) ) { 71 $redirect = add_query_arg( array( 'update' => 'addnoconfirmation' , 'user_id' => $user_id ), 'user-new.php' ); 72 } else { 73 wp_redirect( add_query_arg( array( 'update' => 'could_not_add' ), 'user-new.php' ) ); 74 die(); 75 } 72 76 } else { 73 77 $newuser_key = substr( md5( $user_id ), 0, 5 ); 74 78 add_option( 'new_user_' . $newuser_key, array( 'user_id' => $user_id, 'email' => $user_details->user_email, 'role' => $_REQUEST[ 'role' ] ) ); … … 261 265 case "addexisting": 262 266 $messages[] = __('That user is already a member of this site.'); 263 267 break; 268 case "could_not_add": 269 $messages[] = __( 'That user could not be added to this site.' ); 270 break; 264 271 case "does_not_exist": 265 272 $messages[] = __('The requested user does not exist.'); 266 273 break; -
src/wp-includes/ms-functions.php
59 59 } 60 60 } else { 61 61 //TODO Review this call to add_user_to_blog too - to get here the user must have a role on this blog? 62 add_user_to_blog( $first_blog->userblog_id, $user_id, 'subscriber' ); 63 update_user_meta( $user_id, 'primary_blog', $first_blog->userblog_id ); 64 $primary = $first_blog; 62 if ( ! is_wp_error( add_user_to_blog( $first_blog->userblog_id, $user_id, 'subscriber' ) ) ) { 63 update_user_meta( $user_id, 'primary_blog', $first_blog->userblog_id ); 64 $primary = $first_blog; 65 } 65 66 } 66 67 67 68 if ( ( ! is_object( $primary ) ) || ( $primary->archived == 1 || $primary->spam == 1 || $primary->deleted == 1 ) ) { … … 160 161 return new WP_Error( 'user_does_not_exist', __( 'The requested user does not exist.' ) ); 161 162 } 162 163 164 /** 165 * Filters whether a user should be added to a blog. 166 * 167 * @since 168 * 169 * @param bool $retval True if the user should be added to the blog, otherwise WP_Error. 170 * @param int $user_id User ID. 171 * @param string $role User role. 172 * @param int $blog_id Blog ID. 173 */ 174 $ok_to_add = apply_filters( 'can_add_user_to_blog', true, $user_id, $role, $blog_id ); 175 if ( true !== $ok_to_add ) { 176 restore_current_blog(); 177 if ( is_wp_error( $ok_to_add ) ) { 178 return $ok_to_add; 179 } 180 return new WP_Error( 'user_cannot_be_added', __( 'The user cannot be added to the blog.' ) ); 181 } 182 163 183 if ( !get_user_meta($user_id, 'primary_blog', true) ) { 164 184 update_user_meta($user_id, 'primary_blog', $blog_id); 165 185 $site = get_site( $blog_id ); … … 2081 2101 if ( is_array( $details ) ) { 2082 2102 $blog_id = get_current_blog_id(); 2083 2103 $result = add_user_to_blog( $blog_id, $details[ 'user_id' ], $details[ 'role' ] ); 2084 /** 2085 * Fires immediately after an existing user is added to a site. 2086 * 2087 * @since MU 2088 * 2089 * @param int $user_id User ID. 2090 * @param mixed $result True on success or a WP_Error object if the user doesn't exist. 2091 */ 2092 do_action( 'added_existing_user', $details['user_id'], $result ); 2104 if ( ! is_wp_error( $result ) ) { 2105 /** 2106 * Fires immediately after an existing user is added to a site. 2107 * 2108 * @since MU 2109 * 2110 * @param int $user_id User ID. 2111 * @param mixed $result True on success or a WP_Error object if the user doesn't exist. 2112 */ 2113 do_action( 'added_existing_user', $details['user_id'], $result ); 2114 } 2093 2115 return $result; 2094 2116 } 2095 2117 } … … 2111 2133 if ( !empty( $meta[ 'add_to_blog' ] ) ) { 2112 2134 $blog_id = $meta[ 'add_to_blog' ]; 2113 2135 $role = $meta[ 'new_role' ]; 2114 remove_user_from_blog($user_id, get_network()->site_id); // remove user from main blog. 2115 add_user_to_blog( $blog_id, $user_id, $role ); 2116 update_user_meta( $user_id, 'primary_blog', $blog_id ); 2136 remove_user_from_blog( $user_id, get_network()->site_id ); // remove user from main blog. 2137 if ( ! is_wp_error( add_user_to_blog( $blog_id, $user_id, $role ) ) ) { 2138 update_user_meta( $user_id, 'primary_blog', $blog_id ); 2139 } 2117 2140 } 2118 2141 } 2119 2142 -
src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php
503 503 return $user_id; 504 504 } 505 505 506 add_user_to_blog( get_site()->id, $user_id, '' ); 506 $result= add_user_to_blog( get_site()->id, $user_id, '' ); 507 if ( is_wp_error( $result ) ) { 508 return $result; 509 } 507 510 } else { 508 511 $user_id = wp_insert_user( wp_slash( (array) $user ) ); 509 512 -
tests/phpunit/tests/user/multisite.php
398 398 } 399 399 400 400 /** 401 * @ticket 41101 402 */ 403 public function test_should_fail_can_add_user_to_blog_filter() { 404 $site_id = self::factory()->blog->create(); 405 $user_id = self::factory()->user->create(); 406 407 add_filter( 'can_add_user_to_blog', '__return_false', 10, 4 ); 408 $result = add_user_to_blog( $site_id, $user_id, 'subscriber' ); 409 410 wpmu_delete_blog( $site_id ); 411 wpmu_delete_user( $user_id ); 412 413 $this->assertWPError( $result ); 414 } 415 416 /** 417 * @ticket 41101 418 */ 419 public function test_should_succeed_can_add_user_to_blog_filter() { 420 $site_id = self::factory()->blog->create(); 421 $user_id = self::factory()->user->create(); 422 423 add_filter( 'can_add_user_to_blog', '__return_true', 10, 4 ); 424 $result = add_user_to_blog( $site_id, $user_id, 'subscriber' ); 425 426 wpmu_delete_blog( $site_id ); 427 wpmu_delete_user( $user_id ); 428 429 $this->assertTrue( $result ); 430 } 431 432 /** 401 433 * @ticket 23016 402 434 */ 403 435 public function test_wp_roles_global_is_reset() {