Ticket #32796: 32796.diff
| File 32796.diff, 8.6 KB (added by , 11 years ago) |
|---|
-
src/wp-admin/includes/ms.php
175 175 * 176 176 * @since 3.0.0 177 177 * 178 * @todo Merge with wp_delete_user() ?179 *180 * @global wpdb $wpdb181 *182 178 * @param int $id The user ID. 183 179 * @return bool True if the user was deleted, otherwise false. 184 180 */ 185 181 function wpmu_delete_user( $id ) { 186 global $wpdb; 187 188 $id = (int) $id; 189 $user = new WP_User( $id ); 190 191 if ( !$user->exists() ) 192 return false; 193 /** 194 * Fires before a user is deleted from the network. 195 * 196 * @since MU 197 * 198 * @param int $id ID of the user about to be deleted from the network. 199 */ 200 do_action( 'wpmu_delete_user', $id ); 201 202 $blogs = get_blogs_of_user( $id ); 203 204 if ( ! empty( $blogs ) ) { 205 foreach ( $blogs as $blog ) { 206 switch_to_blog( $blog->userblog_id ); 207 remove_user_from_blog( $id, $blog->userblog_id ); 208 209 $post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author = %d", $id ) ); 210 foreach ( (array) $post_ids as $post_id ) { 211 wp_delete_post( $post_id ); 212 } 213 214 // Clean links 215 $link_ids = $wpdb->get_col( $wpdb->prepare( "SELECT link_id FROM $wpdb->links WHERE link_owner = %d", $id ) ); 216 217 if ( $link_ids ) { 218 foreach ( $link_ids as $link_id ) 219 wp_delete_link( $link_id ); 220 } 221 222 restore_current_blog(); 223 } 224 } 225 226 $meta = $wpdb->get_col( $wpdb->prepare( "SELECT umeta_id FROM $wpdb->usermeta WHERE user_id = %d", $id ) ); 227 foreach ( $meta as $mid ) 228 delete_metadata_by_mid( 'user', $mid ); 229 230 $wpdb->delete( $wpdb->users, array( 'ID' => $id ) ); 231 232 clean_user_cache( $user ); 233 234 /** This action is documented in wp-admin/includes/user.php */ 235 do_action( 'deleted_user', $id ); 236 237 return true; 182 return wp_delete_user( $id, null, 'network' ); 238 183 } 239 184 240 185 /** -
src/wp-admin/includes/user.php
266 266 * 267 267 * @global wpdb $wpdb 268 268 * 269 * @param int $id User ID. 270 * @param int $reassign Optional. Reassign posts and links to new User ID. 269 * @param int $id User ID. 270 * @param int $reassign Optional. Reassign posts and links to new User ID. 271 * @param string $context If single site, use site. If multisite, site or network. 271 272 * @return bool True when finished. 272 273 */ 273 function wp_delete_user( $id, $reassign = null ) {274 function wp_delete_user( $id, $reassign = null, $context = 'site' ) { 274 275 global $wpdb; 275 276 276 277 $id = (int) $id; 277 278 $user = new WP_User( $id ); 278 279 279 if ( ! $user->exists() )280 if ( ! $user->exists() ) { 280 281 return false; 282 } 281 283 282 284 // Normalize $reassign to null or a user ID. 'novalue' was an older default. 283 285 if ( 'novalue' === $reassign ) { … … 286 288 $reassign = (int) $reassign; 287 289 } 288 290 289 /** 290 * Fires immediately before a user is deleted from the database. 291 * 292 * @since 2.0.0 293 * 294 * @param int $id ID of the user to delete. 295 * @param int|null $reassign ID of the user to reassign posts and links to. 296 * Default null, for no reassignment. 297 */ 298 do_action( 'delete_user', $id, $reassign ); 299 300 if ( null === $reassign ) { 301 $post_types_to_delete = array(); 302 foreach ( get_post_types( array(), 'objects' ) as $post_type ) { 303 if ( $post_type->delete_with_user ) { 304 $post_types_to_delete[] = $post_type->name; 305 } elseif ( null === $post_type->delete_with_user && post_type_supports( $post_type->name, 'author' ) ) { 306 $post_types_to_delete[] = $post_type->name; 307 } 308 } 309 291 if ( is_multisite() && 'network' === $context ) { 310 292 /** 311 * Fi lter the list of post types to delete with a user.293 * Fires before a user is deleted from the network. 312 294 * 313 * @since 3.4.0295 * @since MU 314 296 * 315 * @param array $post_types_to_delete Post types to delete. 316 * @param int $id User ID. 297 * @param int $id ID of the user about to be deleted from the network. 317 298 */ 318 $post_types_to_delete = apply_filters( 'post_types_to_delete_with_user', $post_types_to_delete, $id ); 319 $post_types_to_delete = implode( "', '", $post_types_to_delete ); 320 $post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author = %d AND post_type IN ('$post_types_to_delete')", $id ) ); 321 if ( $post_ids ) { 322 foreach ( $post_ids as $post_id ) 323 wp_delete_post( $post_id ); 324 } 299 do_action( 'wpmu_delete_user', $id ); 300 } else { 301 /** 302 * Fires immediately before a user is deleted from the database. 303 * 304 * @since 2.0.0 305 * 306 * @param int $id ID of the user to delete. 307 * @param int|null $reassign ID of the user to reassign posts and links to. 308 * Default null, for no reassignment. 309 */ 310 do_action( 'delete_user', $id, $reassign ); 311 } 325 312 326 // Clean links 327 $link_ids = $wpdb->get_col( $wpdb->prepare("SELECT link_id FROM $wpdb->links WHERE link_owner = %d", $id) ); 313 if ( is_multisite() && 'site' === $context ) { 314 $blogs = array(); 315 $blogs[0] = get_blog_details( get_current_blog_id() ); 316 $blogs[0]->userblog_id = $blogs[0]->blog_id; 317 } else { 318 // Will retrieve the current site if not multisite. 319 $blogs = get_blogs_of_user( $id ); 320 } 328 321 329 if ( $link_ids) {330 foreach ( $link_ids as $link_id )331 wp_delete_link($link_id);322 foreach ( $blogs as $blog ) { 323 if ( is_multisite() ) { 324 switch_to_blog( $blog->userblog_id ); 332 325 } 333 } else { 334 $post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author = %d", $id ) ); 335 $wpdb->update( $wpdb->posts, array('post_author' => $reassign), array('post_author' => $id) ); 336 if ( ! empty( $post_ids ) ) { 337 foreach ( $post_ids as $post_id ) 338 clean_post_cache( $post_id ); 326 327 if ( null === $reassign ) { 328 $post_types_to_delete = array(); 329 foreach ( get_post_types( array(), 'objects' ) as $post_type ) { 330 if ( $post_type->delete_with_user ) { 331 $post_types_to_delete[] = $post_type->name; 332 } elseif ( null === $post_type->delete_with_user && post_type_supports( $post_type->name, 'author' ) ) { 333 $post_types_to_delete[] = $post_type->name; 334 } 335 } 336 337 /** 338 * Filter the list of post types to delete with a user. 339 * 340 * @since 3.4.0 341 * 342 * @param array $post_types_to_delete Post types to delete. 343 * @param int $id User ID. 344 */ 345 $post_types_to_delete = apply_filters( 'post_types_to_delete_with_user', $post_types_to_delete, $id ); 346 $post_types_to_delete = implode( "', '", $post_types_to_delete ); 347 $post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author = %d AND post_type IN ('$post_types_to_delete')", $id ) ); 348 if ( $post_ids ) { 349 foreach ( $post_ids as $post_id ) { 350 wp_delete_post( $post_id ); 351 } 352 } 353 354 // Clean links 355 $link_ids = $wpdb->get_col( $wpdb->prepare( "SELECT link_id FROM $wpdb->links WHERE link_owner = %d", $id ) ); 356 357 if ( $link_ids ) { 358 foreach ( $link_ids as $link_id ) { 359 wp_delete_link($link_id); 360 } 361 } 362 } else { 363 $post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author = %d", $id ) ); 364 $wpdb->update( $wpdb->posts, array( 'post_author' => $reassign ), array( 'post_author' => $id ) ); 365 if ( ! empty( $post_ids ) ) { 366 foreach ( $post_ids as $post_id ) { 367 clean_post_cache( $post_id ); 368 } 369 } 370 $link_ids = $wpdb->get_col( $wpdb->prepare( "SELECT link_id FROM $wpdb->links WHERE link_owner = %d", $id ) ); 371 $wpdb->update( $wpdb->links, array( 'link_owner' => $reassign ), array( 'link_owner' => $id ) ); 372 if ( ! empty( $link_ids ) ) { 373 foreach ( $link_ids as $link_id ) { 374 clean_bookmark_cache( $link_id ); 375 } 376 } 339 377 } 340 $link_ids = $wpdb->get_col( $wpdb->prepare("SELECT link_id FROM $wpdb->links WHERE link_owner = %d", $id) ); 341 $wpdb->update( $wpdb->links, array('link_owner' => $reassign), array('link_owner' => $id) ); 342 if ( ! empty( $link_ids ) ) { 343 foreach ( $link_ids as $link_id ) 344 clean_bookmark_cache( $link_id ); 378 379 if ( is_multisite() ) { 380 remove_user_from_blog( $id, $blog->userblog_id ); 381 restore_current_blog(); 345 382 } 346 383 } 347 384 348 385 // FINALLY, delete user 349 if ( is_multisite() ) { 350 remove_user_from_blog( $id, get_current_blog_id() ); 351 } else { 386 if ( ! is_multisite() || 'network' === $context ) { 352 387 $meta = $wpdb->get_col( $wpdb->prepare( "SELECT umeta_id FROM $wpdb->usermeta WHERE user_id = %d", $id ) ); 353 foreach ( $meta as $mid ) 388 foreach ( $meta as $mid ) { 354 389 delete_metadata_by_mid( 'user', $mid ); 390 } 355 391 356 392 $wpdb->delete( $wpdb->users, array( 'ID' => $id ) ); 357 393 }