Ticket #40364: 40364.diff
File 40364.diff, 12.3 KB (added by , 8 years ago) |
---|
-
src/wp-admin/includes/ms.php
53 53 * Delete a site. 54 54 * 55 55 * @since 3.0.0 56 * @since 4.9.0 Now uses `wp_delete_site()` to delete the site row from the database. 56 57 * 57 58 * @global wpdb $wpdb WordPress database abstraction object. 58 59 * … … 127 128 $wpdb->query( "DROP TABLE IF EXISTS `$table`" ); 128 129 } 129 130 130 $wpdb->delete( $wpdb->blogs, array( 'blog_id' => $blog_id ));131 wp_delete_site( $blog ); 131 132 132 133 /** 133 134 * Filters the upload base directory to delete when the site is deleted. … … 169 170 if ( $dir != $top_dir) 170 171 @rmdir( $dir ); 171 172 } 172 173 clean_blog_cache( $blog );174 173 } 175 174 176 175 /** -
src/wp-includes/ms-blogs.php
295 295 * Update the details for a blog. Updates the blogs table for a given blog id. 296 296 * 297 297 * @since MU 298 * @since 4.9.0 Now wraps `wp_update_site()`. 298 299 * 299 300 * @global wpdb $wpdb WordPress database abstraction object. 300 301 * … … 311 312 if ( is_object($details) ) 312 313 $details = get_object_vars($details); 313 314 314 $current_details = get_site( $blog_id ); 315 if ( empty($current_details) ) 316 return false; 317 318 $current_details = get_object_vars($current_details); 319 320 $details = array_merge($current_details, $details); 321 $details['last_updated'] = current_time('mysql', true); 322 323 $update_details = array(); 324 $fields = array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id'); 325 foreach ( array_intersect( array_keys( $details ), $fields ) as $field ) { 326 if ( 'path' === $field ) { 327 $details[ $field ] = trailingslashit( '/' . trim( $details[ $field ], '/' ) ); 328 } 315 $site = get_site( $blog_id ); 316 foreach ( $details as $field => $value ) { 317 $site->$field = $value; 318 } 329 319 330 $update_details[ $field ] = $details[ $field ]; 320 $site = wp_update_site( $site ); 321 if ( is_wp_error( $site ) ) { 322 return false; 331 323 } 332 324 333 $result = $wpdb->update( $wpdb->blogs, $update_details, array('blog_id' => $blog_id) );325 //TODO: Do we need to run the 'refresh_blog_details' hook here for BC? 334 326 335 if ( false === $result ) 336 return false; 327 return true; 328 } 329 330 /** 331 * Runs hooks when one or more site status fields have been updated. 332 * 333 * @since 4.9.0 334 * 335 * @param WP_Site $existing_site Site object prior to the update. 336 * @param WP_Site $updated_site Site object after the update. 337 */ 338 function maybe_transition_site_statuses( $existing_site, $updated_site ) { 339 $site_id = $updated_site->id; 337 340 338 341 // If spam status changed, issue actions. 339 if ( $ details['spam'] != $current_details['spam']) {340 if ( $ details['spam']== 1 ) {342 if ( $updated_site->spam != $existing_site->spam ) { 343 if ( $updated_site->spam == 1 ) { 341 344 /** 342 * Fires when the 'spam' status is added to a blog.345 * Fires when the 'spam' status is added to a site. 343 346 * 344 347 * @since MU 345 348 * 346 * @param int $ blog_id BlogID.349 * @param int $site_id Site ID. 347 350 */ 348 do_action( 'make_spam_blog', $ blog_id );351 do_action( 'make_spam_blog', $site_id ); 349 352 } else { 350 353 /** 351 * Fires when the 'spam' status is removed from a blog.354 * Fires when the 'spam' status is removed from a site. 352 355 * 353 356 * @since MU 354 357 * 355 * @param int $ blog_id BlogID.358 * @param int $site_id Site ID. 356 359 */ 357 do_action( 'make_ham_blog', $ blog_id );360 do_action( 'make_ham_blog', $site_id ); 358 361 } 359 362 } 360 363 361 364 // If mature status changed, issue actions. 362 if ( $ details['mature'] != $current_details['mature']) {363 if ( $ details['mature']== 1 ) {365 if ( $updated_site->mature != $existing_site->mature ) { 366 if ( $updated_site->mature == 1 ) { 364 367 /** 365 * Fires when the 'mature' status is added to a blog.368 * Fires when the 'mature' status is added to a site. 366 369 * 367 370 * @since 3.1.0 368 371 * 369 * @param int $ blog_id BlogID.372 * @param int $site_id Site ID. 370 373 */ 371 do_action( 'mature_blog', $ blog_id );374 do_action( 'mature_blog', $site_id ); 372 375 } else { 373 376 /** 374 * Fires when the 'mature' status is removed from a blog.377 * Fires when the 'mature' status is removed from a site. 375 378 * 376 379 * @since 3.1.0 377 380 * 378 * @param int $ blog_id BlogID.381 * @param int $site_id Site ID. 379 382 */ 380 do_action( 'unmature_blog', $ blog_id );383 do_action( 'unmature_blog', $site_id ); 381 384 } 382 385 } 383 386 384 387 // If archived status changed, issue actions. 385 if ( $ details['archived'] != $current_details['archived']) {386 if ( $ details['archived']== 1 ) {388 if ( $updated_site->archived != $existing_site->archived ) { 389 if ( $updated_site->archived == 1 ) { 387 390 /** 388 * Fires when the 'archived' status is added to a blog.391 * Fires when the 'archived' status is added to a site. 389 392 * 390 393 * @since MU 391 394 * 392 * @param int $ blog_id BlogID.395 * @param int $site_id Site ID. 393 396 */ 394 do_action( 'archive_blog', $ blog_id );397 do_action( 'archive_blog', $site_id ); 395 398 } else { 396 399 /** 397 * Fires when the 'archived' status is removed from a blog.400 * Fires when the 'archived' status is removed from a site. 398 401 * 399 402 * @since MU 400 403 * 401 * @param int $ blog_id BlogID.404 * @param int $site_id Site ID. 402 405 */ 403 do_action( 'unarchive_blog', $ blog_id );406 do_action( 'unarchive_blog', $site_id ); 404 407 } 405 408 } 406 409 407 410 // If deleted status changed, issue actions. 408 if ( $ details['deleted'] != $current_details['deleted']) {409 if ( $ details['deleted']== 1 ) {411 if ( $updated_site->deleted != $existing_site->deleted ) { 412 if ( $updated_site->deleted == 1 ) { 410 413 /** 411 * Fires when the 'deleted' status is added to a blog.414 * Fires when the 'deleted' status is added to a site. 412 415 * 413 416 * @since 3.5.0 414 417 * 415 * @param int $ blog_id BlogID.418 * @param int $site_id Site ID. 416 419 */ 417 do_action( 'make_delete_blog', $ blog_id );420 do_action( 'make_delete_blog', $site_id ); 418 421 } else { 419 422 /** 420 * Fires when the 'deleted' status is removed from a blog.423 * Fires when the 'deleted' status is removed from a site. 421 424 * 422 425 * @since 3.5.0 423 426 * 424 * @param int $ blog_id BlogID.427 * @param int $site_id Site ID. 425 428 */ 426 do_action( 'make_undelete_blog', $ blog_id );429 do_action( 'make_undelete_blog', $site_id ); 427 430 } 428 431 } 429 432 430 if ( isset( $details['public'] ) ) { 431 switch_to_blog( $blog_id ); 432 update_option( 'blog_public', $details['public'] ); 433 //TODO: This should not be in here. It is an action in single-site context and database tables may not exist. 434 if ( $updated_site->public != $existing_site->public ) { 435 switch_to_blog( $site_id ); 436 update_option( 'blog_public', $updated_site->public ); 433 437 restore_current_blog(); 434 438 } 435 436 refresh_blog_details($blog_id);437 438 return true;439 439 } 440 440 441 441 /** … … 498 498 } 499 499 500 500 /** 501 * Inserts a new site into the database. 502 * 503 * @since 4.9.0 504 * 505 * @global wpdb $wpdb WordPress database abstraction object. 506 * 507 * @param WP_Site $site Site object to insert its data. Its $id property must not be 508 * set prior to inserting the site. 509 * @return WP_Site|WP_Error Modified site object with $id property set on success, or 510 * error object on failure. 511 */ 512 function wp_insert_site( $site ) { 513 global $wpdb; 514 515 if ( ! empty( $site->id ) ) { 516 return new WP_Error( 'site_already_exist', __( 'Cannot create existing site.' ) ); 517 } 518 519 if ( empty( $site->domain ) ) { 520 return new WP_Error( 'site_empty_domain', __( 'Site domain must not be empty.' ) ); 521 } 522 523 $site->path = trailingslashit( $site->path ); 524 525 if ( empty( $site->network_id ) ) { 526 $site->network_id = get_current_network_id(); 527 } 528 529 $site->registered = current_time( 'mysql' ); 530 $site->last_updated = $site->registered; 531 532 $args = $site->to_array(); 533 534 $fields = array( 'domain', 'path', 'site_id', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' ); 535 $args = array_intersect_key( $args, array_flip( $fields ) ); 536 537 $result = $wpdb->insert( $wpdb->blogs, $args ); 538 if ( ! $result ) { 539 return new WP_Error( 'could_not_insert_site', __( 'Site could not be inserted into the database.' ) ); 540 } 541 542 $site->id = $wpdb->insert_id; 543 544 clean_blog_cache( $site ); 545 546 //TODO: Pass $site->network_id to the function after #40384 is done. 547 wp_maybe_update_network_site_counts(); 548 549 return $site; 550 } 551 552 /** 553 * Updates an existing site in the database. 554 * 555 * @since 4.9.0 556 * 557 * @global wpdb $wpdb WordPress database abstraction object. 558 * 559 * @param WP_Site $site Site object to update its data. 560 * @return WP_Site|WP_Error Modified site object on success, or 561 * error object on failure. 562 */ 563 function wp_update_site( $site ) { 564 global $wpdb; 565 566 $existing_site = ! empty( $site->id ) ? get_site( $site->id ) : null; 567 if ( ! $existing_site ) { 568 return new WP_Error( 'site_not_exist', __( 'Cannot update not-existing site.' ) ); 569 } 570 571 if ( empty( $site->domain ) ) { 572 return new WP_Error( 'site_empty_domain', __( 'Site domain must not be empty.' ) ); 573 } 574 575 $site->path = trailingslashit( $site->path ); 576 577 if ( empty( $site->network_id ) ) { 578 $site->network_id = get_current_network_id(); 579 } 580 581 $site->last_updated = current_time( 'mysql' ); 582 583 $args = $site->to_array(); 584 585 $fields = array( 'domain', 'path', 'site_id', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' ); 586 $args = array_intersect_key( $args, array_flip( $fields ) ); 587 588 $result = $wpdb->update( $wpdb->blogs, $args, array( 'blog_id' => $site->id ) ); 589 if ( ! $result ) { 590 return new WP_Error( 'could_not_update_site', __( 'Site could not be updated in the database.' ) ); 591 } 592 593 maybe_transition_site_statuses( $existing_site, $site ); 594 595 clean_blog_cache( $site ); 596 597 return $site; 598 } 599 600 /** 601 * Deletes an existing site from the database. 602 * 603 * @since 4.9.0 604 * 605 * @global wpdb $wpdb WordPress database abstraction object. 606 * 607 * @param WP_Site $site Site object to delete its data. 608 * @return WP_Site|WP_Error Modified site object with $id property removed on success, or 609 * error object on failure. 610 */ 611 function wp_delete_site( $site ) { 612 global $wpdb; 613 614 $existing_site = ! empty( $site->id ) ? get_site( $site->id ) : null; 615 if ( ! $existing_site ) { 616 return new WP_Error( 'site_not_exist', __( 'Cannot update non-existing site.' ) ); 617 } 618 619 $result = $wpdb->delete( $wpdb->blogs, array( 'blog_id' => $site->id ) ); 620 if ( ! $result ) { 621 return new WP_Error( 'could_not_delete_site', __( 'Site could not be deleted from the database.' ) ); 622 } 623 624 clean_blog_cache( $site ); 625 626 //TODO: Pass $site->network_id to the function after #40384 is done. 627 wp_maybe_update_network_site_counts(); 628 629 $site->id = 0; 630 631 return $site; 632 } 633 634 /** 501 635 * Retrieves site data given a site ID or site object. 502 636 * 503 637 * Site data will be cached and returned after being passed through a filter. -
src/wp-includes/ms-functions.php
1344 1344 * the new blog's ID. It is the first step in creating a new blog. 1345 1345 * 1346 1346 * @since MU 1347 * 1348 * @global wpdb $wpdb WordPress database abstraction object. 1347 * @since 4.9.0 Now wraps `wp_insert_site()`. 1349 1348 * 1350 1349 * @param string $domain The domain of the new site. 1351 1350 * @param string $path The path of the new site. … … 1353 1352 * @return int|false The ID of the new row 1354 1353 */ 1355 1354 function insert_blog($domain, $path, $site_id) { 1356 global $wpdb; 1355 $site = new WP_Site( new stdClass() ); 1356 $site->domain = $domain; 1357 $site->path = $path; 1358 $site->network_id = $site_id; 1357 1359 1358 $path = trailingslashit($path); 1359 $site_id = (int) $site_id; 1360 1361 $result = $wpdb->insert( $wpdb->blogs, array('site_id' => $site_id, 'domain' => $domain, 'path' => $path, 'registered' => current_time('mysql')) ); 1362 if ( ! $result ) 1360 $site = wp_insert_site( $site ); 1361 if ( is_wp_error( $site ) ) { 1363 1362 return false; 1363 } 1364 1364 1365 $blog_id = $wpdb->insert_id; 1366 refresh_blog_details( $blog_id ); 1367 1368 wp_maybe_update_network_site_counts(); 1365 //TODO: Do we need to run the 'refresh_blog_details' hook here for BC? 1369 1366 1370 return $ blog_id;1367 return $site->id; 1371 1368 } 1372 1369 1373 1370 /**