Ticket #40364: 40364.8.diff
| File 40364.8.diff, 39.0 KB (added by , 8 years ago) |
|---|
-
src/wp-admin/includes/ms.php
56 56 * Delete a site. 57 57 * 58 58 * @since 3.0.0 59 * @since 5.0.0 Use wp_delete_site() internally to delete the site row from the database. 59 60 * 60 61 * @global wpdb $wpdb WordPress database abstraction object. 61 62 * … … 135 136 $wpdb->query( "DROP TABLE IF EXISTS `$table`" ); 136 137 } 137 138 138 $wpdb->delete( $wpdb->blogs, array( 'blog_id' => $blog_id ));139 wp_delete_site( $blog_id ); 139 140 140 141 /** 141 142 * Filters the upload base directory to delete when the site is deleted. -
src/wp-includes/ms-blogs.php
297 297 $details = get_object_vars( $details ); 298 298 } 299 299 300 $current_details = get_site( $blog_id ); 301 if ( empty( $current_details ) ) { 302 return false; 303 } 304 305 $current_details = get_object_vars( $current_details ); 306 307 $details = array_merge( $current_details, $details ); 308 $details['last_updated'] = current_time( 'mysql', true ); 309 310 $update_details = array(); 311 $fields = array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' ); 312 foreach ( array_intersect( array_keys( $details ), $fields ) as $field ) { 313 if ( 'path' === $field ) { 314 $details[ $field ] = trailingslashit( '/' . trim( $details[ $field ], '/' ) ); 315 } 316 317 $update_details[ $field ] = $details[ $field ]; 318 } 300 $site = wp_update_site( $blog_id, $details ); 319 301 320 $result = $wpdb->update( $wpdb->blogs, $update_details, array( 'blog_id' => $blog_id ) ); 321 322 if ( false === $result ) { 302 if ( is_wp_error( $site ) ) { 323 303 return false; 324 304 } 325 305 326 // If spam status changed, issue actions.327 if ( $details['spam'] != $current_details['spam'] ) {328 if ( $details['spam'] == 1 ) {329 /**330 * Fires when the 'spam' status is added to a blog.331 *332 * @since MU (3.0.0)333 *334 * @param int $blog_id Blog ID.335 */336 do_action( 'make_spam_blog', $blog_id );337 } else {338 /**339 * Fires when the 'spam' status is removed from a blog.340 *341 * @since MU (3.0.0)342 *343 * @param int $blog_id Blog ID.344 */345 do_action( 'make_ham_blog', $blog_id );346 }347 }348 349 // If mature status changed, issue actions.350 if ( $details['mature'] != $current_details['mature'] ) {351 if ( $details['mature'] == 1 ) {352 /**353 * Fires when the 'mature' status is added to a blog.354 *355 * @since 3.1.0356 *357 * @param int $blog_id Blog ID.358 */359 do_action( 'mature_blog', $blog_id );360 } else {361 /**362 * Fires when the 'mature' status is removed from a blog.363 *364 * @since 3.1.0365 *366 * @param int $blog_id Blog ID.367 */368 do_action( 'unmature_blog', $blog_id );369 }370 }371 372 // If archived status changed, issue actions.373 if ( $details['archived'] != $current_details['archived'] ) {374 if ( $details['archived'] == 1 ) {375 /**376 * Fires when the 'archived' status is added to a blog.377 *378 * @since MU (3.0.0)379 *380 * @param int $blog_id Blog ID.381 */382 do_action( 'archive_blog', $blog_id );383 } else {384 /**385 * Fires when the 'archived' status is removed from a blog.386 *387 * @since MU (3.0.0)388 *389 * @param int $blog_id Blog ID.390 */391 do_action( 'unarchive_blog', $blog_id );392 }393 }394 395 // If deleted status changed, issue actions.396 if ( $details['deleted'] != $current_details['deleted'] ) {397 if ( $details['deleted'] == 1 ) {398 /**399 * Fires when the 'deleted' status is added to a blog.400 *401 * @since 3.5.0402 *403 * @param int $blog_id Blog ID.404 */405 do_action( 'make_delete_blog', $blog_id );406 } else {407 /**408 * Fires when the 'deleted' status is removed from a blog.409 *410 * @since 3.5.0411 *412 * @param int $blog_id Blog ID.413 */414 do_action( 'make_undelete_blog', $blog_id );415 }416 }417 418 if ( isset( $details['public'] ) ) {419 switch_to_blog( $blog_id );420 update_option( 'blog_public', $details['public'] );421 restore_current_blog();422 }423 424 clean_blog_cache( $blog_id );425 426 306 return true; 427 307 } 428 308 … … 517 397 } 518 398 519 399 /** 400 * Inserts a new site into the database. 401 * 402 * @since 5.0.0 403 * 404 * @global wpdb $wpdb WordPress database abstraction object. 405 * 406 * @param array $data { 407 * Data for the new site that should be inserted. 408 * 409 * @type string $domain Site domain. Must always be provided. 410 * @type string $path Site path. Default '/'. 411 * @type int $network_id The site's network ID. Default is the current network ID. 412 * @type string $registered When the site was registered, in SQL datetime format. Default is 413 * the current time. 414 * @type string $last_updated When the site was last updated, in SQL datetime format. Default is 415 * the value of $registered. 416 * @type int $public Whether the site is public. Default 1. 417 * @type int $archived Whether the site is archived. Default 0. 418 * @type int $mature Whether the site is mature. Default 0. 419 * @type int $spam Whether the site is spam. Default 0. 420 * @type int $deleted Whether the site is deleted. Default 0. 421 * @type int $lang_id The site's language ID. Currently unused. Default 0. 422 * } 423 * @return int|WP_Error The new site's ID on success, or error object on failure. 424 */ 425 function wp_insert_site( $data ) { 426 global $wpdb; 427 428 $now = current_time( 'mysql' ); 429 430 $defaults = array( 431 'domain' => '', 432 'path' => '/', 433 'network_id' => 0, 434 'registered' => $now, 435 'last_updated' => $now, 436 'public' => 1, 437 'archived' => 0, 438 'mature' => 0, 439 'spam' => 0, 440 'deleted' => 0, 441 'lang_id' => 0, 442 ); 443 444 // Maintain backward-compatibility with `$site_id` as network ID. 445 if ( ! empty( $data['site_id'] ) && empty( $data['network_id'] ) ) { 446 $data['network_id'] = $data['site_id']; 447 } 448 449 // Sanitize domain if passed. 450 if ( array_key_exists( 'domain', $data ) ) { 451 $data['domain'] = trim( $data['domain'] ); 452 $data['domain'] = preg_replace( '/\s+/', '', sanitize_user( $data['domain'], true ) ); 453 if ( is_subdomain_install() ) { 454 $data['domain'] = str_replace( '@', '', $data['domain'] ); 455 } 456 } 457 458 // Sanitize path if passed. 459 if ( array_key_exists( 'path', $data ) ) { 460 $data['path'] = trailingslashit( '/' . trim( $data['path'], '/' ) ); 461 } 462 463 // Sanitize network ID if passed. 464 if ( array_key_exists( 'network_id', $data ) ) { 465 $data['network_id'] = (int) $data['network_id']; 466 } 467 468 var_dump( $data ); 469 470 $data = array_intersect_key( wp_parse_args( $data, $defaults ), $defaults ); 471 472 // Use the current network if none is set. 473 if ( empty( $data['network_id'] ) ) { 474 $data['network_id'] = get_current_network_id(); 475 } 476 477 $validity = new WP_Error(); 478 479 /** 480 * Fires when data should be validated for a site prior to inserting or updating in the database. 481 * 482 * Plugins should amend the `$validity` object via its `WP_Error::add()` method. 483 * 484 * @since 5.0.0 485 * 486 * @param WP_Error $validity Error object to add validation errors to. 487 * @param array $data Associative array of complete site data. See {@see wp_insert_site()} 488 * for the included data. 489 * @param WP_Site|null $old_site The old site object if the data belongs to a site being updated, 490 * or null if it is a new site being inserted. 491 */ 492 do_action( 'validate_site_data', $validity, $data, null ); 493 494 if ( ! empty( $validity->errors ) ) { 495 return $validity; 496 } 497 498 // Prepare for database. 499 $data['site_id'] = $data['network_id']; 500 unset( $data['network_id'] ); 501 502 if ( false === $wpdb->insert( $wpdb->blogs, $data ) ) { 503 return new WP_Error( 'db_insert_error', __( 'Could not insert site into the database.' ), $wpdb->last_error ); 504 } 505 506 $new_site = get_site( $wpdb->insert_id ); 507 508 clean_blog_cache( $new_site ); 509 510 /** 511 * Fires once a site has been inserted into the database. 512 * 513 * @since 5.0.0 514 * 515 * @param WP_Site $new_site New site object. 516 */ 517 do_action( 'wp_insert_site', $new_site ); 518 519 return (int) $new_site->id; 520 } 521 522 /** 523 * Updates a site in the database. 524 * 525 * @since 5.0.0 526 * 527 * @global wpdb $wpdb WordPress database abstraction object. 528 * 529 * @param int $site_id ID of the site that should be updated. 530 * @param array $data Site data to update. See {@see wp_insert_site()} for the list of supported keys. 531 * @return int|WP_Error The updated site's ID on success, or error object on failure. 532 */ 533 function wp_update_site( $site_id, $data ) { 534 global $wpdb; 535 536 $old_site = get_site( $site_id ); 537 if ( ! $old_site ) { 538 return new WP_Error( 'site_not_exist', __( 'Site does not exist.' ) ); 539 } 540 541 $defaults = $old_site->to_array(); 542 $defaults['network_id'] = (int) $defaults['site_id']; 543 $defaults['last_updated'] = current_time( 'mysql' ); 544 unset( $defaults['blog_id'], $defaults['site_id'] ); 545 546 // Maintain backward-compatibility with `$site_id` as network ID. 547 if ( ! empty( $data['site_id'] ) && empty( $data['network_id'] ) ) { 548 $data['network_id'] = $data['site_id']; 549 } 550 551 // Sanitize domain if passed. 552 if ( array_key_exists( 'domain', $data ) ) { 553 $data['domain'] = trim( $data['domain'] ); 554 $data['domain'] = preg_replace( '/\s+/', '', sanitize_user( $data['domain'], true ) ); 555 if ( is_subdomain_install() ) { 556 $data['domain'] = str_replace( '@', '', $data['domain'] ); 557 } 558 } 559 560 // Sanitize path if passed. 561 if ( array_key_exists( 'path', $data ) ) { 562 $data['path'] = trailingslashit( '/' . trim( $data['path'], '/' ) ); 563 } 564 565 // Sanitize network ID if passed. 566 if ( array_key_exists( 'network_id', $data ) ) { 567 $data['network_id'] = (int) $data['network_id']; 568 } 569 570 $whitelist = array( 'domain', 'path', 'network_id', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' ); 571 $data = array_intersect_key( wp_parse_args( $data, $defaults ), array_flip( $whitelist ) ); 572 573 // Use the previously set network if a falsy network ID has been passed. 574 if ( empty( $data['network_id'] ) ) { 575 $data['network_id'] = $defaults['network_id']; 576 } 577 578 $validity = new WP_Error(); 579 580 /** This action is documented in wp-includes/ms-blogs.php */ 581 do_action( 'validate_site_data', $validity, $data, $old_site ); 582 583 if ( ! empty( $validity->errors ) ) { 584 return $validity; 585 } 586 587 // Prepare for database. 588 $data['site_id'] = $data['network_id']; 589 unset( $data['network_id'] ); 590 591 if ( false === $wpdb->update( $wpdb->blogs, $data, array( 'blog_id' => $old_site->id ) ) ) { 592 return new WP_Error( 'db_update_error', __( 'Could not update site in the database.' ), $wpdb->last_error ); 593 } 594 595 clean_blog_cache( $old_site ); 596 597 $new_site = get_site( $old_site->id ); 598 599 /** 600 * Fires once a site has been updated in the database. 601 * 602 * @since 5.0.0 603 * 604 * @param WP_Site $new_site New site object. 605 * @param WP_Site $old_site Old site object. 606 */ 607 do_action( 'wp_update_site', $new_site, $old_site ); 608 609 return (int) $new_site->id; 610 } 611 612 /** 613 * Deletes a site from the database. 614 * 615 * @since 5.0.0 616 * 617 * @global wpdb $wpdb WordPress database abstraction object. 618 * 619 * @param int $site_id ID of the site that should be deleted. 620 * @return WP_Site|WP_Error The deleted site object on success, or error object on failure. 621 */ 622 function wp_delete_site( $site_id ) { 623 global $wpdb; 624 625 $old_site = get_site( $site_id ); 626 if ( ! $old_site ) { 627 return new WP_Error( 'site_not_exist', __( 'Site does not exist.' ) ); 628 } 629 630 if ( false === $wpdb->delete( $wpdb->blogs, array( 'blog_id' => $old_site->id ) ) ) { 631 return new WP_Error( 'db_delete_error', __( 'Could not delete site from the database.' ), $wpdb->last_error ); 632 } 633 634 clean_blog_cache( $old_site ); 635 636 /** 637 * Fires once a site has been deleted from the database. 638 * 639 * @since 5.0.0 640 * 641 * @param WP_Site $old_site Deleted site object. 642 */ 643 do_action( 'wp_delete_site', $old_site ); 644 645 return $old_site; 646 } 647 648 /** 520 649 * Retrieves site data given a site ID or site object. 521 650 * 522 651 * Site data will be cached and returned after being passed through a filter. … … 659 788 } 660 789 661 790 /** 791 * Validates data for a site prior to inserting or updating in the database. 792 * 793 * @since 5.0.0 794 * 795 * @param WP_Error $validity Error object, passed by reference. Will contain validation errors if 796 * any occurred. 797 * @param array $data Associative array of complete site data. See {@see wp_insert_site()} 798 * for the included data. 799 * @param WP_Site|null $old_site The old site object if the data belongs to a site being updated, 800 * or null if it is a new site being inserted. 801 */ 802 function wp_validate_site_data( $validity, $data, $old_site = null ) { 803 // A domain must always be present. 804 if ( empty( $data['domain'] ) ) { 805 $validity->add( 'site_empty_domain', __( 'Site domain must not be empty.' ) ); 806 } 807 808 // If a new site, or domain/path/network ID have changed, ensure uniqueness. 809 if ( ! $old_site 810 || $data['domain'] !== $old_site->domain 811 || $data['path'] !== $old_site->path 812 || $data['network_id'] !== $old_site->network_id 813 ) { 814 if ( domain_exists( $data['domain'], $data['path'], $data['network_id'] ) ) { 815 $validity->add( 'blog_taken', __( 'Sorry, that site already exists!' ) ); 816 } 817 } 818 } 819 820 /** 662 821 * Retrieve option value for a given blog id based on name of option. 663 822 * 664 823 * If the option does not exist or does not have a value, then the return value … … 1016 1175 * Update a blog details field. 1017 1176 * 1018 1177 * @since MU (3.0.0) 1178 * @since 5.0.0 Use wp_update_site() internally. 1019 1179 * 1020 1180 * @global wpdb $wpdb WordPress database abstraction object. 1021 1181 * … … 1036 1196 return $value; 1037 1197 } 1038 1198 1039 $result = $wpdb->update( 1040 $wpdb->blogs, array( 1041 $pref => $value, 1042 'last_updated' => current_time( 'mysql', true ), 1043 ), array( 'blog_id' => $blog_id ) 1044 ); 1199 $result = wp_update_site( $blog_id, array( 1200 $pref => $value, 1201 ) ); 1045 1202 1046 if ( false === $result) {1203 if ( is_wp_error( $result ) ) { 1047 1204 return false; 1048 1205 } 1049 1206 1050 clean_blog_cache( $blog_id );1051 1052 if ( 'spam' == $pref ) {1053 if ( $value == 1 ) {1054 /** This filter is documented in wp-includes/ms-blogs.php */1055 do_action( 'make_spam_blog', $blog_id );1056 } else {1057 /** This filter is documented in wp-includes/ms-blogs.php */1058 do_action( 'make_ham_blog', $blog_id );1059 }1060 } elseif ( 'mature' == $pref ) {1061 if ( $value == 1 ) {1062 /** This filter is documented in wp-includes/ms-blogs.php */1063 do_action( 'mature_blog', $blog_id );1064 } else {1065 /** This filter is documented in wp-includes/ms-blogs.php */1066 do_action( 'unmature_blog', $blog_id );1067 }1068 } elseif ( 'archived' == $pref ) {1069 if ( $value == 1 ) {1070 /** This filter is documented in wp-includes/ms-blogs.php */1071 do_action( 'archive_blog', $blog_id );1072 } else {1073 /** This filter is documented in wp-includes/ms-blogs.php */1074 do_action( 'unarchive_blog', $blog_id );1075 }1076 } elseif ( 'deleted' == $pref ) {1077 if ( $value == 1 ) {1078 /** This filter is documented in wp-includes/ms-blogs.php */1079 do_action( 'make_delete_blog', $blog_id );1080 } else {1081 /** This filter is documented in wp-includes/ms-blogs.php */1082 do_action( 'make_undelete_blog', $blog_id );1083 }1084 } elseif ( 'public' == $pref ) {1085 /**1086 * Fires after the current blog's 'public' setting is updated.1087 *1088 * @since MU (3.0.0)1089 *1090 * @param int $blog_id Blog ID.1091 * @param string $value The value of blog status.1092 */1093 do_action( 'update_blog_public', $blog_id, $value ); // Moved here from update_blog_public().1094 }1095 1096 1207 return $value; 1097 1208 } 1098 1209 … … 1360 1471 1361 1472 update_posts_count(); 1362 1473 } 1474 1475 /** 1476 * Updates the count of sites for a network based on a changed site. 1477 * 1478 * @since 5.0.0 1479 * 1480 * @param WP_Site $new_site The site object that has been inserted, updated or deleted. 1481 * @param WP_Site|null $old_site Optional. If $new_site has been updated, this must be the previous 1482 * state of that site. Default null. 1483 */ 1484 function wp_maybe_update_network_site_counts_on_update( $new_site, $old_site = null ) { 1485 if ( null === $old_site ) { 1486 wp_maybe_update_network_site_counts( $new_site->network_id ); 1487 return; 1488 } 1489 1490 if ( $new_site->network_id != $old_site->network_id ) { 1491 wp_maybe_update_network_site_counts( $new_site->network_id ); 1492 wp_maybe_update_network_site_counts( $old_site->network_id ); 1493 } 1494 } 1495 1496 /** 1497 * Triggers actions on site status updates. 1498 * 1499 * @since 5.0.0 1500 * 1501 * @param WP_Site $new_site The site object after the update. 1502 * @param WP_Site $old_site The site obejct prior to the update. 1503 */ 1504 function wp_maybe_transition_site_statuses_on_update( $new_site, $old_site ) { 1505 $site_id = $new_site->id; 1506 1507 if ( $new_site->spam != $old_site->spam ) { 1508 if ( $new_site->spam == 1 ) { 1509 1510 /** 1511 * Fires when the 'spam' status is added to a site. 1512 * 1513 * @since MU (3.0.0) 1514 * 1515 * @param int $site_id Site ID. 1516 */ 1517 do_action( 'make_spam_blog', $site_id ); 1518 } else { 1519 1520 /** 1521 * Fires when the 'spam' status is removed from a site. 1522 * 1523 * @since MU (3.0.0) 1524 * 1525 * @param int $site_id Site ID. 1526 */ 1527 do_action( 'make_ham_blog', $site_id ); 1528 } 1529 } 1530 1531 if ( $new_site->mature != $old_site->mature ) { 1532 if ( $new_site->mature == 1 ) { 1533 1534 /** 1535 * Fires when the 'mature' status is added to a site. 1536 * 1537 * @since 3.1.0 1538 * 1539 * @param int $site_id Site ID. 1540 */ 1541 do_action( 'mature_blog', $site_id ); 1542 } else { 1543 1544 /** 1545 * Fires when the 'mature' status is removed from a site. 1546 * 1547 * @since 3.1.0 1548 * 1549 * @param int $site_id Site ID. 1550 */ 1551 do_action( 'unmature_blog', $site_id ); 1552 } 1553 } 1554 1555 if ( $new_site->archived != $old_site->archived ) { 1556 if ( $new_site->archived == 1 ) { 1557 1558 /** 1559 * Fires when the 'archived' status is added to a site. 1560 * 1561 * @since MU (3.0.0) 1562 * 1563 * @param int $site_id Site ID. 1564 */ 1565 do_action( 'archive_blog', $site_id ); 1566 } else { 1567 1568 /** 1569 * Fires when the 'archived' status is removed from a site. 1570 * 1571 * @since MU (3.0.0) 1572 * 1573 * @param int $site_id Site ID. 1574 */ 1575 do_action( 'unarchive_blog', $site_id ); 1576 } 1577 } 1578 1579 if ( $new_site->deleted != $old_site->deleted ) { 1580 if ( $new_site->deleted == 1 ) { 1581 1582 /** 1583 * Fires when the 'deleted' status is added to a site. 1584 * 1585 * @since 3.5.0 1586 * 1587 * @param int $site_id Site ID. 1588 */ 1589 do_action( 'make_delete_blog', $site_id ); 1590 } else { 1591 1592 /** 1593 * Fires when the 'deleted' status is removed from a site. 1594 * 1595 * @since 3.5.0 1596 * 1597 * @param int $site_id Site ID. 1598 */ 1599 do_action( 'make_undelete_blog', $site_id ); 1600 } 1601 } 1602 1603 if ( $new_site->public != $old_site->public ) { 1604 1605 /** 1606 * Fires after the current blog's 'public' setting is updated. 1607 * 1608 * @since MU (3.0.0) 1609 * 1610 * @param int $site_id Site ID. 1611 * @param string $value The value of the site status. 1612 */ 1613 do_action( 'update_blog_public', $site_id, $new_site->public ); 1614 } 1615 } 1616 1617 /** 1618 * Cleans the necessary caches after specific site data has been updated. 1619 * 1620 * @since 5.0.0 1621 * 1622 * @param WP_Site $new_site The site object after the update. 1623 * @param WP_Site $old_site The site obejct prior to the update. 1624 */ 1625 function wp_maybe_clean_old_site_cache_on_update( $new_site, $old_site ) { 1626 if ( $old_site->domain !== $new_site->domain || $old_site->path !== $new_site->path ) { 1627 clean_blog_cache( $new_site ); 1628 } 1629 } 1630 1631 /** 1632 * Updates the `blog_public` option for a given site ID. 1633 * 1634 * @since 5.0.0 1635 * 1636 * @param int $site_id Site ID. 1637 * @param string $public The value of the site status. 1638 */ 1639 function wp_update_blog_public_option_on_site_update( $site_id, $public ) { 1640 update_blog_option( $site_id, 'blog_public', $public ); 1641 } -
src/wp-includes/ms-default-filters.php
41 41 add_action( 'wpmu_new_blog', 'newblog_notify_siteadmin', 10, 2 ); 42 42 add_action( 'wpmu_activate_blog', 'wpmu_welcome_notification', 10, 5 ); 43 43 add_action( 'after_signup_site', 'wpmu_signup_blog_notification', 10, 7 ); 44 add_action( 'validate_site_data', 'wp_validate_site_data', 10, 3 ); 45 add_action( 'wp_insert_site', 'wp_maybe_update_network_site_counts_on_update', 10, 1 ); 46 add_action( 'wp_update_site', 'wp_maybe_update_network_site_counts_on_update', 10, 2 ); 47 add_action( 'wp_delete_site', 'wp_maybe_update_network_site_counts_on_update', 10, 1 ); 48 add_action( 'wp_update_site', 'wp_maybe_transition_site_statuses_on_update', 10, 2 ); 49 add_action( 'wp_update_site', 'wp_maybe_clean_old_site_cache_on_update', 10, 2 ); 50 add_action( 'update_blog_public', 'wp_update_blog_public_option_on_site_update', 1, 2 ); 44 51 45 52 // Register Nonce 46 53 add_action( 'signup_hidden_fields', 'signup_nonce_fields' ); -
src/wp-includes/ms-deprecated.php
546 546 547 547 return isset( $current_user->$local_key ); 548 548 } 549 550 /** 551 * Store basic site info in the blogs table. 552 * 553 * This function creates a row in the wp_blogs table and returns 554 * the new blog's ID. It is the first step in creating a new blog. 555 * 556 * @since MU (3.0.0) 557 * @deprecated 5.0.0 Use `wp_insert_site()` 558 * @see wp_insert_site() 559 * 560 * @param string $domain The domain of the new site. 561 * @param string $path The path of the new site. 562 * @param int $site_id Unless you're running a multi-network install, be sure to set this value to 1. 563 * @return int|false The ID of the new row 564 */ 565 function insert_blog($domain, $path, $site_id) { 566 _deprecated_function( __FUNCTION__, '5.0.0', 'wp_insert_site()' ); 567 568 $data = array( 569 'domain' => $domain, 570 'path' => $path, 571 'site_id' => $site_id, 572 ); 573 574 $site_id = wp_insert_site( $data ); 575 if ( is_wp_error( $site_id ) ) { 576 return false; 577 } 578 579 clean_blog_cache( $site_id ); 580 581 return $site_id; 582 } -
src/wp-includes/ms-functions.php
1272 1272 ); 1273 1273 $meta = wp_parse_args( $meta, $defaults ); 1274 1274 1275 $domain = preg_replace( '/\s+/', '', sanitize_user( $domain, true ) );1276 1277 if ( is_subdomain_install() ) {1278 $domain = str_replace( '@', '', $domain );1279 }1280 1281 1275 $title = strip_tags( $title ); 1282 1276 $user_id = (int) $user_id; 1283 1277 1284 if ( empty( $path ) ) {1285 $path = '/';1286 }1287 1288 // Check if the domain has been used already. We should return an error message.1289 if ( domain_exists( $domain, $path, $network_id ) ) {1290 return new WP_Error( 'blog_taken', __( 'Sorry, that site already exists!' ) );1291 }1292 1293 1278 if ( ! wp_installing() ) { 1294 1279 wp_installing( true ); 1295 1280 } 1296 1281 1297 if ( ! $blog_id = insert_blog( $domain, $path, $network_id ) ) { 1298 return new WP_Error( 'insert_blog', __( 'Could not create site.' ) ); 1282 $blog_id = wp_insert_site( array( 1283 'domain' => $domain, 1284 'path' => $path, 1285 'network_id' => $network_id, 1286 ) ); 1287 1288 if ( is_wp_error( $blog_id ) ) { 1289 return $blog_id; 1299 1290 } 1300 1291 1301 1292 switch_to_blog( $blog_id ); … … 1319 1310 } 1320 1311 1321 1312 restore_current_blog(); 1313 1314 $site = get_site( $blog_id ); 1315 1322 1316 /** 1323 1317 * Fires immediately after a new site is created. 1324 1318 * … … 1331 1325 * @param int $network_id Network ID. Only relevant on multi-network installations. 1332 1326 * @param array $meta Meta data. Used to set initial site options. 1333 1327 */ 1334 do_action( 'wpmu_new_blog', $blog_id, $user_id, $ domain, $path, $network_id, $meta );1328 do_action( 'wpmu_new_blog', $blog_id, $user_id, $site->domain, $site->path, $site->network_id, $meta ); 1335 1329 1336 1330 wp_cache_set( 'last_changed', microtime(), 'sites' ); 1337 1331 … … 1485 1479 } 1486 1480 1487 1481 /** 1488 * Store basic site info in the blogs table.1489 *1490 * This function creates a row in the wp_blogs table and returns1491 * the new blog's ID. It is the first step in creating a new blog.1492 *1493 * @since MU (3.0.0)1494 *1495 * @global wpdb $wpdb WordPress database abstraction object.1496 *1497 * @param string $domain The domain of the new site.1498 * @param string $path The path of the new site.1499 * @param int $network_id Unless you're running a multi-network installation, be sure to set this value to 1.1500 * @return int|false The ID of the new row1501 */1502 function insert_blog( $domain, $path, $network_id ) {1503 global $wpdb;1504 1505 $path = trailingslashit( $path );1506 $network_id = (int) $network_id;1507 1508 $result = $wpdb->insert(1509 $wpdb->blogs, array(1510 'site_id' => $network_id,1511 'domain' => $domain,1512 'path' => $path,1513 'registered' => current_time( 'mysql' ),1514 )1515 );1516 if ( ! $result ) {1517 return false;1518 }1519 1520 $blog_id = $wpdb->insert_id;1521 clean_blog_cache( $blog_id );1522 1523 wp_maybe_update_network_site_counts( $network_id );1524 1525 return $blog_id;1526 }1527 1528 /**1529 1482 * Install an empty blog. 1530 1483 * 1531 1484 * Creates the new blog tables and options. If calling this function … … 1537 1490 * @global wpdb $wpdb 1538 1491 * @global WP_Roles $wp_roles 1539 1492 * 1540 * @param int $blog_id The value returned by insert_blog().1493 * @param int $blog_id The value returned by wp_insert_site(). 1541 1494 * @param string $blog_title The title of the new site. 1542 1495 */ 1543 1496 function install_blog( $blog_id, $blog_title = '' ) { -
tests/phpunit/tests/multisite/site.php
446 446 $this->assertEquals( '0', $blog->spam ); 447 447 $this->assertEquals( 1, $test_action_counter ); 448 448 449 // The action should fire if the status of 'spam' stays the same.449 // The action should not fire if the status of 'spam' stays the same. 450 450 update_blog_status( $blog_id, 'spam', 0 ); 451 451 $blog = get_site( $blog_id ); 452 452 453 453 $this->assertEquals( '0', $blog->spam ); 454 $this->assertEquals( 2, $test_action_counter );454 $this->assertEquals( 1, $test_action_counter ); 455 455 456 456 remove_action( 'make_ham_blog', array( $this, '_action_counter_cb' ), 10 ); 457 457 } … … 469 469 $this->assertEquals( '1', $blog->spam ); 470 470 $this->assertEquals( 1, $test_action_counter ); 471 471 472 // The action should fire if the status of 'spam' stays the same.472 // The action should not fire if the status of 'spam' stays the same. 473 473 update_blog_status( $blog_id, 'spam', 1 ); 474 474 $blog = get_site( $blog_id ); 475 475 476 476 $this->assertEquals( '1', $blog->spam ); 477 $this->assertEquals( 2, $test_action_counter );477 $this->assertEquals( 1, $test_action_counter ); 478 478 479 479 remove_action( 'make_spam_blog', array( $this, '_action_counter_cb' ), 10 ); 480 480 } … … 492 492 $this->assertEquals( '1', $blog->archived ); 493 493 $this->assertEquals( 1, $test_action_counter ); 494 494 495 // The action should fire if the status of 'archived' stays the same.495 // The action should not fire if the status of 'archived' stays the same. 496 496 update_blog_status( $blog_id, 'archived', 1 ); 497 497 $blog = get_site( $blog_id ); 498 498 499 499 $this->assertEquals( '1', $blog->archived ); 500 $this->assertEquals( 2, $test_action_counter );500 $this->assertEquals( 1, $test_action_counter ); 501 501 502 502 remove_action( 'archive_blog', array( $this, '_action_counter_cb' ), 10 ); 503 503 } … … 516 516 $this->assertEquals( '0', $blog->archived ); 517 517 $this->assertEquals( 1, $test_action_counter ); 518 518 519 // The action should fire if the status of 'archived' stays the same.519 // The action should not fire if the status of 'archived' stays the same. 520 520 update_blog_status( $blog_id, 'archived', 0 ); 521 521 $blog = get_site( $blog_id ); 522 522 $this->assertEquals( '0', $blog->archived ); 523 $this->assertEquals( 2, $test_action_counter );523 $this->assertEquals( 1, $test_action_counter ); 524 524 525 525 remove_action( 'unarchive_blog', array( $this, '_action_counter_cb' ), 10 ); 526 526 } … … 538 538 $this->assertEquals( '1', $blog->deleted ); 539 539 $this->assertEquals( 1, $test_action_counter ); 540 540 541 // The action should fire if the status of 'deleted' stays the same.541 // The action should not fire if the status of 'deleted' stays the same. 542 542 update_blog_status( $blog_id, 'deleted', 1 ); 543 543 $blog = get_site( $blog_id ); 544 544 545 545 $this->assertEquals( '1', $blog->deleted ); 546 $this->assertEquals( 2, $test_action_counter );546 $this->assertEquals( 1, $test_action_counter ); 547 547 548 548 remove_action( 'make_delete_blog', array( $this, '_action_counter_cb' ), 10 ); 549 549 } … … 562 562 $this->assertEquals( '0', $blog->deleted ); 563 563 $this->assertEquals( 1, $test_action_counter ); 564 564 565 // The action should fire if the status of 'deleted' stays the same.565 // The action should not fire if the status of 'deleted' stays the same. 566 566 update_blog_status( $blog_id, 'deleted', 0 ); 567 567 $blog = get_site( $blog_id ); 568 568 569 569 $this->assertEquals( '0', $blog->deleted ); 570 $this->assertEquals( 2, $test_action_counter );570 $this->assertEquals( 1, $test_action_counter ); 571 571 572 572 remove_action( 'make_undelete_blog', array( $this, '_action_counter_cb' ), 10 ); 573 573 } … … 585 585 $this->assertEquals( '1', $blog->mature ); 586 586 $this->assertEquals( 1, $test_action_counter ); 587 587 588 // The action should fire if the status of 'mature' stays the same.588 // The action should not fire if the status of 'mature' stays the same. 589 589 update_blog_status( $blog_id, 'mature', 1 ); 590 590 $blog = get_site( $blog_id ); 591 591 592 592 $this->assertEquals( '1', $blog->mature ); 593 $this->assertEquals( 2, $test_action_counter );593 $this->assertEquals( 1, $test_action_counter ); 594 594 595 595 remove_action( 'mature_blog', array( $this, '_action_counter_cb' ), 10 ); 596 596 } … … 609 609 $this->assertEquals( '0', $blog->mature ); 610 610 $this->assertEquals( 1, $test_action_counter ); 611 611 612 // The action should fire if the status of 'mature' stays the same.612 // The action should not fire if the status of 'mature' stays the same. 613 613 update_blog_status( $blog_id, 'mature', 0 ); 614 614 $blog = get_site( $blog_id ); 615 615 616 616 $this->assertEquals( '0', $blog->mature ); 617 $this->assertEquals( 2, $test_action_counter );617 $this->assertEquals( 1, $test_action_counter ); 618 618 619 619 remove_action( 'unmature_blog', array( $this, '_action_counter_cb' ), 10 ); 620 620 } … … 632 632 $this->assertEquals( '0', $blog->public ); 633 633 $this->assertEquals( 1, $test_action_counter ); 634 634 635 // The action should fire if the status of 'mature' stays the same.635 // The action should not fire if the status of 'mature' stays the same. 636 636 update_blog_status( $blog_id, 'public', 0 ); 637 637 $blog = get_site( $blog_id ); 638 638 639 639 $this->assertEquals( '0', $blog->public ); 640 $this->assertEquals( 2, $test_action_counter );640 $this->assertEquals( 1, $test_action_counter ); 641 641 642 642 remove_action( 'update_blog_public', array( $this, '_action_counter_cb' ), 10 ); 643 643 } … … 1259 1259 array( 'current_blog_%domain%%path%', 'site-options' ), 1260 1260 ); 1261 1261 } 1262 1263 /** 1264 * @ticket 40364 1265 * @dataProvider data_wp_insert_site 1266 */ 1267 function test_wp_insert_site( $site_data, $expected_data ) { 1268 $site_id = wp_insert_site( $site_data ); 1269 1270 $this->assertInternalType( 'integer', $site_id ); 1271 1272 $site = get_site( $site_id ); 1273 foreach ( $expected_data as $key => $value ) { 1274 $this->assertEquals( $value, $site->$key ); 1275 } 1276 } 1277 1278 function data_wp_insert_site() { 1279 return array( 1280 array( 1281 array( 1282 'domain' => 'example.com', 1283 ), 1284 array( 1285 'domain' => 'example.com', 1286 'path' => '/', 1287 'network_id' => 1, 1288 'public' => 1, 1289 'archived' => 0, 1290 'mature' => 0, 1291 'spam' => 0, 1292 'deleted' => 0, 1293 'lang_id' => 0, 1294 ), 1295 ), 1296 array( 1297 array( 1298 'domain' => 'example.com', 1299 'path' => '/foo', 1300 'network_id' => 2, 1301 ), array( 1302 'domain' => 'example.com', 1303 'path' => '/foo/', 1304 'network_id' => 2, 1305 ), 1306 ), 1307 array( 1308 array( 1309 'domain' => 'example.com', 1310 'path' => '/bar/', 1311 'site_id' => 2, 1312 'public' => 0, 1313 ), array( 1314 'domain' => 'example.com', 1315 'path' => '/bar/', 1316 'network_id' => 2, 1317 'public' => 0, 1318 ), 1319 ), 1320 array( 1321 array( 1322 'domain' => 'example.com', 1323 'path' => 'foobar', 1324 'public' => 0, 1325 'archived' => 1, 1326 'mature' => 1, 1327 'spam' => 1, 1328 'deleted' => 1, 1329 'lang_id' => 1, 1330 ), array( 1331 'domain' => 'example.com', 1332 'path' => '/foobar/', 1333 'public' => 0, 1334 'archived' => 1, 1335 'mature' => 1, 1336 'spam' => 1, 1337 'deleted' => 1, 1338 'lang_id' => 1, 1339 ), 1340 ), 1341 ); 1342 } 1343 1344 /** 1345 * @ticket 40364 1346 */ 1347 function test_wp_insert_site_empty_domain() { 1348 $site_id = wp_insert_site( array( 'public' => 0 ) ); 1349 1350 $this->assertWPError( $site_id ); 1351 $this->assertSame( 'site_empty_domain', $site_id->get_error_code() ); 1352 } 1353 1354 /** 1355 * @ticket 40364 1356 * @dataProvider data_wp_update_site 1357 */ 1358 function test_wp_update_site( $site_data, $expected_data ) { 1359 $site_id = self::factory()->blog->create(); 1360 1361 $old_site = get_site( $site_id ); 1362 1363 $result = wp_update_site( $site_id, $site_data ); 1364 1365 $this->assertSame( $site_id, $result ); 1366 1367 $new_site = get_site( $site_id ); 1368 foreach ( $new_site->to_array() as $key => $value ) { 1369 if ( isset( $expected_data[ $key ] ) ) { 1370 $this->assertEquals( $expected_data[ $key ], $value ); 1371 } elseif ( 'last_updated' === $key ) { 1372 $this->assertTrue( $old_site->last_updated <= $value ); 1373 } else { 1374 $this->assertEquals( $old_site->$key, $value ); 1375 } 1376 } 1377 } 1378 1379 function data_wp_update_site() { 1380 return array( 1381 array( 1382 array( 1383 'domain' => 'example.com', 1384 'network_id' => 2, 1385 ), 1386 array( 1387 'domain' => 'example.com', 1388 'site_id' => 2, 1389 ), 1390 ), 1391 array( 1392 array( 1393 'path' => 'foo', 1394 ), 1395 array( 1396 'path' => '/foo/' 1397 ), 1398 ), 1399 array( 1400 array( 1401 'public' => 0, 1402 'archived' => 1, 1403 'mature' => 1, 1404 'spam' => 1, 1405 'deleted' => 1, 1406 'lang_id' => 1, 1407 ), 1408 array( 1409 'public' => 0, 1410 'archived' => 1, 1411 'mature' => 1, 1412 'spam' => 1, 1413 'deleted' => 1, 1414 'lang_id' => 1, 1415 ), 1416 ), 1417 ); 1418 } 1419 1420 /** 1421 * @ticket 40364 1422 */ 1423 function test_wp_update_site_empty_domain() { 1424 $site_id = self::factory()->blog->create(); 1425 1426 $result = wp_update_site( $site_id, array( 'domain' => '' ) ); 1427 1428 $this->assertWPError( $result ); 1429 $this->assertSame( 'site_empty_domain', $result->get_error_code() ); 1430 } 1431 1432 /** 1433 * @ticket 40364 1434 */ 1435 function test_wp_update_site_invalid_id() { 1436 $result = wp_update_site( 444444, array( 'domain' => 'example.com' ) ); 1437 1438 $this->assertWPError( $result ); 1439 $this->assertSame( 'site_not_exist', $result->get_error_code() ); 1440 } 1441 1442 /** 1443 * @ticket 40364 1444 */ 1445 function test_wp_update_site_cleans_cache() { 1446 $site_id = self::factory()->blog->create(); 1447 $site1 = get_site( $site_id ); 1448 1449 $result = wp_update_site( $site_id, array( 'public' => 0 ) ); 1450 $site2 = get_site( $site_id ); 1451 1452 $result = wp_update_site( $site_id, array( 'public' => 1 ) ); 1453 $site3 = get_site( $site_id ); 1454 1455 $this->assertEquals( 1, $site1->public ); 1456 $this->assertEquals( 0, $site2->public ); 1457 $this->assertEquals( 1, $site3->public ); 1458 } 1459 1460 /** 1461 * @ticket 40364 1462 */ 1463 function test_wp_delete_site() { 1464 $site_id = self::factory()->blog->create(); 1465 1466 $site = get_site( $site_id ); 1467 1468 $result = wp_delete_site( $site_id ); 1469 1470 $this->assertInstanceOf( 'WP_Site', $result ); 1471 $this->assertEquals( $result->to_array(), $site->to_array() ); 1472 } 1473 1474 /** 1475 * @ticket 40364 1476 */ 1477 function test_wp_delete_site_invalid_id() { 1478 $result = wp_delete_site( 444444 ); 1479 1480 $this->assertWPError( $result ); 1481 $this->assertSame( 'site_not_exist', $result->get_error_code() ); 1482 } 1483 1484 /** 1485 * @ticket 40364 1486 */ 1487 function test_wp_delete_site_cleans_cache() { 1488 $site_id = self::factory()->blog->create(); 1489 1490 get_site( $site_id ); 1491 1492 wp_delete_site( $site_id ); 1493 1494 $this->assertNull( get_site( $site_id ) ); 1495 } 1496 1497 /** 1498 * @ticket 40364 1499 */ 1500 function test_wp_update_site_cleans_old_cache_on_domain_change() { 1501 $old_domain = 'old.wordpress.org'; 1502 $new_domain = 'new.wordpress.org'; 1503 1504 $site = self::factory()->blog->create_and_get( array( 1505 'domain' => $old_domain, 1506 'path' => '/', 1507 ) ); 1508 1509 // Populate the caches. 1510 get_blog_details( array( 1511 'domain' => $old_domain, 1512 'path' => '/', 1513 ) ); 1514 get_blog_id_from_url( $old_domain, '/' ); 1515 get_blog_details( array( 1516 'domain' => $new_domain, 1517 'path' => '/', 1518 ) ); 1519 get_blog_id_from_url( $new_domain, '/' ); 1520 1521 wp_update_site( $site->id, array( 1522 'domain' => $new_domain, 1523 ) ); 1524 1525 $domain_path_key_old = md5( $old_domain . '/' ); 1526 $domain_path_key_new = md5( $new_domain . '/' ); 1527 1528 // Ensure all respective cache values are empty. 1529 $result = array( 1530 wp_cache_get( $domain_path_key_old, 'blog-lookup' ), 1531 wp_cache_get( $domain_path_key_old, 'blog-id-cache' ), 1532 wp_cache_get( 'current_blog_' . $old_domain, 'site-options' ), 1533 wp_cache_get( 'current_blog_' . $old_domain . '/', 'site-options' ), 1534 wp_cache_get( $domain_path_key_new, 'blog-lookup' ), 1535 wp_cache_get( $domain_path_key_new, 'blog-id-cache' ), 1536 wp_cache_get( 'current_blog_' . $new_domain, 'site-options' ), 1537 wp_cache_get( 'current_blog_' . $new_domain . '/', 'site-options' ), 1538 ); 1539 1540 $this->assertEmpty( array_filter( $result ) ); 1541 } 1542 1543 /** 1544 * @ticket 40364 1545 */ 1546 function test_wp_update_site_cleans_old_cache_on_path_change() { 1547 $old_path = '/foo/'; 1548 $new_path = '/bar/'; 1549 1550 $site = self::factory()->blog->create_and_get( array( 1551 'domain' => 'test.wordpress.org', 1552 'path' => $old_path, 1553 ) ); 1554 1555 // Populate the caches. 1556 get_blog_details( array( 1557 'domain' => 'test.wordpress.org', 1558 'path' => $old_path, 1559 ) ); 1560 get_blog_id_from_url( 'test.wordpress.org', $old_path ); 1561 get_blog_details( array( 1562 'domain' => 'test.wordpress.org', 1563 'path' => $new_path, 1564 ) ); 1565 get_blog_id_from_url( 'test.wordpress.org', $new_path ); 1566 1567 wp_update_site( $site->id, array( 1568 'path' => $new_path, 1569 ) ); 1570 1571 $domain_path_key_old = md5( 'test.wordpress.org' . $old_path ); 1572 $domain_path_key_new = md5( 'test.wordpress.org' . $new_path ); 1573 1574 // Ensure all respective cache values are empty. 1575 $result = array( 1576 wp_cache_get( $domain_path_key_old, 'blog-lookup' ), 1577 wp_cache_get( $domain_path_key_old, 'blog-id-cache' ), 1578 wp_cache_get( 'current_blog_test.wordpress.org' . $old_path, 'site-options' ), 1579 wp_cache_get( $domain_path_key_new, 'blog-lookup' ), 1580 wp_cache_get( $domain_path_key_new, 'blog-id-cache' ), 1581 wp_cache_get( 'current_blog_test.wordpress.org' . $new_path, 'site-options' ), 1582 ); 1583 1584 $this->assertEmpty( array_filter( $result ) ); 1262 1585 } 1586 } 1263 1587 1264 1588 endif;