Ticket #40364: 40364.9.diff
| File 40364.9.diff, 40.7 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 * … … 142 143 } 143 144 } 144 145 145 $wpdb->delete( $wpdb->blogs, array( 'blog_id' => $blog_id ));146 wp_delete_site( $blog_id ); 146 147 147 148 /** 148 149 * 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 … … 518 398 } 519 399 520 400 /** 401 * Inserts a new site into the database. 402 * 403 * @since 5.0.0 404 * 405 * @global wpdb $wpdb WordPress database abstraction object. 406 * 407 * @param array $data { 408 * Data for the new site that should be inserted. 409 * 410 * @type string $domain Site domain. Must always be provided. 411 * @type string $path Site path. Default '/'. 412 * @type int $network_id The site's network ID. Default is the current network ID. 413 * @type string $registered When the site was registered, in SQL datetime format. Default is 414 * the current time. 415 * @type string $last_updated When the site was last updated, in SQL datetime format. Default is 416 * the value of $registered. 417 * @type int $public Whether the site is public. Default 1. 418 * @type int $archived Whether the site is archived. Default 0. 419 * @type int $mature Whether the site is mature. Default 0. 420 * @type int $spam Whether the site is spam. Default 0. 421 * @type int $deleted Whether the site is deleted. Default 0. 422 * @type int $lang_id The site's language ID. Currently unused. Default 0. 423 * } 424 * @return int|WP_Error The new site's ID on success, or error object on failure. 425 */ 426 function wp_insert_site( array $data ) { 427 global $wpdb; 428 429 $now = current_time( 'mysql' ); 430 431 $defaults = array( 432 'domain' => '', 433 'path' => '/', 434 'network_id' => 0, 435 'registered' => $now, 436 'last_updated' => $now, 437 'public' => 1, 438 'archived' => 0, 439 'mature' => 0, 440 'spam' => 0, 441 'deleted' => 0, 442 'lang_id' => 0, 443 ); 444 445 /** 446 * Filters passed site data in order to normalize it. 447 * 448 * @since 5.0.0 449 * 450 * @param array $data Associative array of site data passed to the respective function. 451 * See {@see wp_insert_site()} for the possibly included data. 452 */ 453 $data = apply_filters( 'wp_normalize_site_data', $data ); 454 455 $data = array_intersect_key( wp_parse_args( $data, $defaults ), $defaults ); 456 457 // Use the current network if none is set. 458 if ( empty( $data['network_id'] ) ) { 459 $data['network_id'] = get_current_network_id(); 460 } 461 462 $validity = new WP_Error(); 463 464 /** 465 * Fires when data should be validated for a site prior to inserting or updating in the database. 466 * 467 * Plugins should amend the `$validity` object via its `WP_Error::add()` method. 468 * 469 * @since 5.0.0 470 * 471 * @param WP_Error $validity Error object to add validation errors to. 472 * @param array $data Associative array of complete site data. See {@see wp_insert_site()} 473 * for the included data. 474 * @param WP_Site|null $old_site The old site object if the data belongs to a site being updated, 475 * or null if it is a new site being inserted. 476 */ 477 do_action( 'wp_validate_site_data', $validity, $data, null ); 478 479 if ( ! empty( $validity->errors ) ) { 480 return $validity; 481 } 482 483 // Prepare for database. 484 $data['site_id'] = $data['network_id']; 485 unset( $data['network_id'] ); 486 487 if ( false === $wpdb->insert( $wpdb->blogs, $data ) ) { 488 return new WP_Error( 'db_insert_error', __( 'Could not insert site into the database.' ), $wpdb->last_error ); 489 } 490 491 $new_site = get_site( $wpdb->insert_id ); 492 493 clean_blog_cache( $new_site ); 494 495 /** 496 * Fires once a site has been inserted into the database. 497 * 498 * @since 5.0.0 499 * 500 * @param WP_Site $new_site New site object. 501 */ 502 do_action( 'wp_insert_site', $new_site ); 503 504 return (int) $new_site->id; 505 } 506 507 /** 508 * Updates a site in the database. 509 * 510 * @since 5.0.0 511 * 512 * @global wpdb $wpdb WordPress database abstraction object. 513 * 514 * @param int $site_id ID of the site that should be updated. 515 * @param array $data Site data to update. See {@see wp_insert_site()} for the list of supported keys. 516 * @return int|WP_Error The updated site's ID on success, or error object on failure. 517 */ 518 function wp_update_site( $site_id, array $data ) { 519 global $wpdb; 520 521 $old_site = get_site( $site_id ); 522 if ( ! $old_site ) { 523 return new WP_Error( 'site_not_exist', __( 'Site does not exist.' ) ); 524 } 525 526 $defaults = $old_site->to_array(); 527 $defaults['network_id'] = (int) $defaults['site_id']; 528 $defaults['last_updated'] = current_time( 'mysql' ); 529 unset( $defaults['blog_id'], $defaults['site_id'] ); 530 531 /** This filter is documented in wp-includes/ms-blogs.php */ 532 $data = apply_filters( 'wp_normalize_site_data', $data ); 533 534 $whitelist = array( 'domain', 'path', 'network_id', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' ); 535 $data = array_intersect_key( wp_parse_args( $data, $defaults ), array_flip( $whitelist ) ); 536 537 // Use the previously set network if a falsy network ID has been passed. 538 if ( empty( $data['network_id'] ) ) { 539 $data['network_id'] = $defaults['network_id']; 540 } 541 542 $validity = new WP_Error(); 543 544 /** This action is documented in wp-includes/ms-blogs.php */ 545 do_action( 'wp_validate_site_data', $validity, $data, $old_site ); 546 547 if ( ! empty( $validity->errors ) ) { 548 return $validity; 549 } 550 551 // Prepare for database. 552 $data['site_id'] = $data['network_id']; 553 unset( $data['network_id'] ); 554 555 if ( false === $wpdb->update( $wpdb->blogs, $data, array( 'blog_id' => $old_site->id ) ) ) { 556 return new WP_Error( 'db_update_error', __( 'Could not update site in the database.' ), $wpdb->last_error ); 557 } 558 559 clean_blog_cache( $old_site ); 560 561 $new_site = get_site( $old_site->id ); 562 563 /** 564 * Fires once a site has been updated in the database. 565 * 566 * @since 5.0.0 567 * 568 * @param WP_Site $new_site New site object. 569 * @param WP_Site $old_site Old site object. 570 */ 571 do_action( 'wp_update_site', $new_site, $old_site ); 572 573 return (int) $new_site->id; 574 } 575 576 /** 577 * Deletes a site from the database. 578 * 579 * @since 5.0.0 580 * 581 * @global wpdb $wpdb WordPress database abstraction object. 582 * 583 * @param int $site_id ID of the site that should be deleted. 584 * @return WP_Site|WP_Error The deleted site object on success, or error object on failure. 585 */ 586 function wp_delete_site( $site_id ) { 587 global $wpdb; 588 589 $old_site = get_site( $site_id ); 590 if ( ! $old_site ) { 591 return new WP_Error( 'site_not_exist', __( 'Site does not exist.' ) ); 592 } 593 594 if ( false === $wpdb->delete( $wpdb->blogs, array( 'blog_id' => $old_site->id ) ) ) { 595 return new WP_Error( 'db_delete_error', __( 'Could not delete site from the database.' ), $wpdb->last_error ); 596 } 597 598 clean_blog_cache( $old_site ); 599 600 /** 601 * Fires once a site has been deleted from the database. 602 * 603 * @since 5.0.0 604 * 605 * @param WP_Site $old_site Deleted site object. 606 */ 607 do_action( 'wp_delete_site', $old_site ); 608 609 return $old_site; 610 } 611 612 /** 521 613 * Retrieves site data given a site ID or site object. 522 614 * 523 615 * Site data will be cached and returned after being passed through a filter. … … 688 780 } 689 781 690 782 /** 783 * Normalizes data for a site prior to inserting or updating in the database. 784 * 785 * @since 5.0.0 786 * 787 * @param array $data Associative array of site data passed to the respective function. 788 * See {@see wp_insert_site()} for the possibly included data. 789 * @return array Normalized site data. 790 */ 791 function wp_normalize_site_data( $data ) { 792 // Maintain backward-compatibility with `$site_id` as network ID. 793 if ( ! empty( $data['site_id'] ) && empty( $data['network_id'] ) ) { 794 $data['network_id'] = $data['site_id']; 795 } 796 797 // Sanitize domain if passed. 798 if ( array_key_exists( 'domain', $data ) ) { 799 $data['domain'] = trim( $data['domain'] ); 800 $data['domain'] = preg_replace( '/\s+/', '', sanitize_user( $data['domain'], true ) ); 801 if ( is_subdomain_install() ) { 802 $data['domain'] = str_replace( '@', '', $data['domain'] ); 803 } 804 } 805 806 // Sanitize path if passed. 807 if ( array_key_exists( 'path', $data ) ) { 808 $data['path'] = trailingslashit( '/' . trim( $data['path'], '/' ) ); 809 } 810 811 // Sanitize network ID if passed. 812 if ( array_key_exists( 'network_id', $data ) ) { 813 $data['network_id'] = (int) $data['network_id']; 814 } 815 816 return $data; 817 } 818 819 /** 820 * Validates data for a site prior to inserting or updating in the database. 821 * 822 * @since 5.0.0 823 * 824 * @param WP_Error $validity Error object, passed by reference. Will contain validation errors if 825 * any occurred. 826 * @param array $data Associative array of complete site data. See {@see wp_insert_site()} 827 * for the included data. 828 * @param WP_Site|null $old_site The old site object if the data belongs to a site being updated, 829 * or null if it is a new site being inserted. 830 */ 831 function wp_validate_site_data( $validity, $data, $old_site = null ) { 832 // A domain must always be present. 833 if ( empty( $data['domain'] ) ) { 834 $validity->add( 'site_empty_domain', __( 'Site domain must not be empty.' ) ); 835 } 836 837 // A path must always be present. 838 if ( empty( $data['path'] ) ) { 839 $validity->add( 'site_empty_path', __( 'Site path must not be empty.' ) ); 840 } 841 842 // If a new site, or domain/path/network ID have changed, ensure uniqueness. 843 if ( ! $old_site 844 || $data['domain'] !== $old_site->domain 845 || $data['path'] !== $old_site->path 846 || $data['network_id'] !== $old_site->network_id 847 ) { 848 if ( domain_exists( $data['domain'], $data['path'], $data['network_id'] ) ) { 849 $validity->add( 'blog_taken', __( 'Sorry, that site already exists!' ) ); 850 } 851 } 852 } 853 854 /** 691 855 * Retrieve option value for a given blog id based on name of option. 692 856 * 693 857 * If the option does not exist or does not have a value, then the return value … … 1193 1357 * Update a blog details field. 1194 1358 * 1195 1359 * @since MU (3.0.0) 1360 * @since 5.0.0 Use wp_update_site() internally. 1196 1361 * 1197 1362 * @global wpdb $wpdb WordPress database abstraction object. 1198 1363 * … … 1213 1378 return $value; 1214 1379 } 1215 1380 1216 $result = $wpdb->update( 1217 $wpdb->blogs, array( 1218 $pref => $value, 1219 'last_updated' => current_time( 'mysql', true ), 1220 ), array( 'blog_id' => $blog_id ) 1221 ); 1381 $result = wp_update_site( $blog_id, array( 1382 $pref => $value, 1383 ) ); 1222 1384 1223 if ( false === $result) {1385 if ( is_wp_error( $result ) ) { 1224 1386 return false; 1225 1387 } 1226 1388 1227 clean_blog_cache( $blog_id );1228 1229 if ( 'spam' == $pref ) {1230 if ( $value == 1 ) {1231 /** This filter is documented in wp-includes/ms-blogs.php */1232 do_action( 'make_spam_blog', $blog_id );1233 } else {1234 /** This filter is documented in wp-includes/ms-blogs.php */1235 do_action( 'make_ham_blog', $blog_id );1236 }1237 } elseif ( 'mature' == $pref ) {1238 if ( $value == 1 ) {1239 /** This filter is documented in wp-includes/ms-blogs.php */1240 do_action( 'mature_blog', $blog_id );1241 } else {1242 /** This filter is documented in wp-includes/ms-blogs.php */1243 do_action( 'unmature_blog', $blog_id );1244 }1245 } elseif ( 'archived' == $pref ) {1246 if ( $value == 1 ) {1247 /** This filter is documented in wp-includes/ms-blogs.php */1248 do_action( 'archive_blog', $blog_id );1249 } else {1250 /** This filter is documented in wp-includes/ms-blogs.php */1251 do_action( 'unarchive_blog', $blog_id );1252 }1253 } elseif ( 'deleted' == $pref ) {1254 if ( $value == 1 ) {1255 /** This filter is documented in wp-includes/ms-blogs.php */1256 do_action( 'make_delete_blog', $blog_id );1257 } else {1258 /** This filter is documented in wp-includes/ms-blogs.php */1259 do_action( 'make_undelete_blog', $blog_id );1260 }1261 } elseif ( 'public' == $pref ) {1262 /**1263 * Fires after the current blog's 'public' setting is updated.1264 *1265 * @since MU (3.0.0)1266 *1267 * @param int $blog_id Blog ID.1268 * @param string $value The value of blog status.1269 */1270 do_action( 'update_blog_public', $blog_id, $value ); // Moved here from update_blog_public().1271 }1272 1273 1389 return $value; 1274 1390 } 1275 1391 … … 1537 1653 1538 1654 update_posts_count(); 1539 1655 } 1656 1657 /** 1658 * Updates the count of sites for a network based on a changed site. 1659 * 1660 * @since 5.0.0 1661 * 1662 * @param WP_Site $new_site The site object that has been inserted, updated or deleted. 1663 * @param WP_Site|null $old_site Optional. If $new_site has been updated, this must be the previous 1664 * state of that site. Default null. 1665 */ 1666 function wp_maybe_update_network_site_counts_on_update( $new_site, $old_site = null ) { 1667 if ( null === $old_site ) { 1668 wp_maybe_update_network_site_counts( $new_site->network_id ); 1669 return; 1670 } 1671 1672 if ( $new_site->network_id != $old_site->network_id ) { 1673 wp_maybe_update_network_site_counts( $new_site->network_id ); 1674 wp_maybe_update_network_site_counts( $old_site->network_id ); 1675 } 1676 } 1677 1678 /** 1679 * Triggers actions on site status updates. 1680 * 1681 * @since 5.0.0 1682 * 1683 * @param WP_Site $new_site The site object after the update. 1684 * @param WP_Site|null $old_site Optional. If $new_site has been updated, this must be the previous 1685 * state of that site. Default null. 1686 */ 1687 function wp_maybe_transition_site_statuses_on_update( $new_site, $old_site ) { 1688 $site_id = $new_site->id; 1689 1690 // Use the default values for a site if no previous state is given. 1691 if ( ! $old_site ) { 1692 $old_site = new WP_Site(); 1693 $old_site->public = 1; 1694 $old_site->archived = 0; 1695 $old_site->mature = 0; 1696 $old_site->spam = 0; 1697 $old_site->deleted = 0; 1698 } 1699 1700 if ( $new_site->spam != $old_site->spam ) { 1701 if ( $new_site->spam == 1 ) { 1702 1703 /** 1704 * Fires when the 'spam' status is added to a site. 1705 * 1706 * @since MU (3.0.0) 1707 * 1708 * @param int $site_id Site ID. 1709 */ 1710 do_action( 'make_spam_blog', $site_id ); 1711 } else { 1712 1713 /** 1714 * Fires when the 'spam' status is removed from a site. 1715 * 1716 * @since MU (3.0.0) 1717 * 1718 * @param int $site_id Site ID. 1719 */ 1720 do_action( 'make_ham_blog', $site_id ); 1721 } 1722 } 1723 1724 if ( $new_site->mature != $old_site->mature ) { 1725 if ( $new_site->mature == 1 ) { 1726 1727 /** 1728 * Fires when the 'mature' status is added to a site. 1729 * 1730 * @since 3.1.0 1731 * 1732 * @param int $site_id Site ID. 1733 */ 1734 do_action( 'mature_blog', $site_id ); 1735 } else { 1736 1737 /** 1738 * Fires when the 'mature' status is removed from a site. 1739 * 1740 * @since 3.1.0 1741 * 1742 * @param int $site_id Site ID. 1743 */ 1744 do_action( 'unmature_blog', $site_id ); 1745 } 1746 } 1747 1748 if ( $new_site->archived != $old_site->archived ) { 1749 if ( $new_site->archived == 1 ) { 1750 1751 /** 1752 * Fires when the 'archived' status is added to a site. 1753 * 1754 * @since MU (3.0.0) 1755 * 1756 * @param int $site_id Site ID. 1757 */ 1758 do_action( 'archive_blog', $site_id ); 1759 } else { 1760 1761 /** 1762 * Fires when the 'archived' status is removed from a site. 1763 * 1764 * @since MU (3.0.0) 1765 * 1766 * @param int $site_id Site ID. 1767 */ 1768 do_action( 'unarchive_blog', $site_id ); 1769 } 1770 } 1771 1772 if ( $new_site->deleted != $old_site->deleted ) { 1773 if ( $new_site->deleted == 1 ) { 1774 1775 /** 1776 * Fires when the 'deleted' status is added to a site. 1777 * 1778 * @since 3.5.0 1779 * 1780 * @param int $site_id Site ID. 1781 */ 1782 do_action( 'make_delete_blog', $site_id ); 1783 } else { 1784 1785 /** 1786 * Fires when the 'deleted' status is removed from a site. 1787 * 1788 * @since 3.5.0 1789 * 1790 * @param int $site_id Site ID. 1791 */ 1792 do_action( 'make_undelete_blog', $site_id ); 1793 } 1794 } 1795 1796 if ( $new_site->public != $old_site->public ) { 1797 1798 /** 1799 * Fires after the current blog's 'public' setting is updated. 1800 * 1801 * @since MU (3.0.0) 1802 * 1803 * @param int $site_id Site ID. 1804 * @param string $value The value of the site status. 1805 */ 1806 do_action( 'update_blog_public', $site_id, $new_site->public ); 1807 } 1808 } 1809 1810 /** 1811 * Cleans the necessary caches after specific site data has been updated. 1812 * 1813 * @since 5.0.0 1814 * 1815 * @param WP_Site $new_site The site object after the update. 1816 * @param WP_Site $old_site The site obejct prior to the update. 1817 */ 1818 function wp_maybe_clean_new_site_cache_on_update( $new_site, $old_site ) { 1819 if ( $old_site->domain !== $new_site->domain || $old_site->path !== $new_site->path ) { 1820 clean_blog_cache( $new_site ); 1821 } 1822 } 1823 1824 /** 1825 * Updates the `blog_public` option for a given site ID. 1826 * 1827 * @since 5.0.0 1828 * 1829 * @param int $site_id Site ID. 1830 * @param string $public The value of the site status. 1831 */ 1832 function wp_update_blog_public_option_on_site_update( $site_id, $public ) { 1833 update_blog_option( $site_id, 'blog_public', $public ); 1834 } -
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( 'wp_normalize_site_data', 'wp_normalize_site_data', 10, 1 ); 45 add_action( 'wp_validate_site_data', 'wp_validate_site_data', 10, 3 ); 46 add_action( 'wp_insert_site', 'wp_maybe_update_network_site_counts_on_update', 10, 1 ); 47 add_action( 'wp_update_site', 'wp_maybe_update_network_site_counts_on_update', 10, 2 ); 48 add_action( 'wp_delete_site', 'wp_maybe_update_network_site_counts_on_update', 10, 1 ); 49 add_action( 'wp_insert_site', 'wp_maybe_transition_site_statuses_on_update', 10, 1 ); 50 add_action( 'wp_update_site', 'wp_maybe_transition_site_statuses_on_update', 10, 2 ); 51 add_action( 'wp_update_site', 'wp_maybe_clean_new_site_cache_on_update', 10, 2 ); 52 add_action( 'update_blog_public', 'wp_update_blog_public_option_on_site_update', 1, 2 ); 44 53 45 54 // Register Nonce 46 55 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
1273 1273 ); 1274 1274 $meta = wp_parse_args( $meta, $defaults ); 1275 1275 1276 $domain = preg_replace( '/\s+/', '', sanitize_user( $domain, true ) );1277 1278 if ( is_subdomain_install() ) {1279 $domain = str_replace( '@', '', $domain );1280 }1281 1282 1276 $title = strip_tags( $title ); 1283 1277 $user_id = (int) $user_id; 1284 1278 1285 if ( empty( $path ) ) {1286 $path = '/';1287 }1288 1289 // Check if the domain has been used already. We should return an error message.1290 if ( domain_exists( $domain, $path, $network_id ) ) {1291 return new WP_Error( 'blog_taken', __( 'Sorry, that site already exists!' ) );1292 }1293 1294 1279 if ( ! wp_installing() ) { 1295 1280 wp_installing( true ); 1296 1281 } 1297 1282 1298 if ( ! $blog_id = insert_blog( $domain, $path, $network_id ) ) { 1299 return new WP_Error( 'insert_blog', __( 'Could not create site.' ) ); 1283 $site_data_whitelist = array( 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' ); 1284 1285 $site_data = array_merge( 1286 array( 1287 'domain' => $domain, 1288 'path' => $path, 1289 'network_id' => $network_id, 1290 ), 1291 array_intersect_key( 1292 $meta, 1293 array_flip( $site_data_whitelist ) 1294 ) 1295 ); 1296 1297 $meta = array_diff_key( $meta, array_flip( $site_data_whitelist ) ); 1298 1299 remove_action( 'update_blog_public', 'wp_update_blog_public_option_on_site_update', 1 ); 1300 $blog_id = wp_insert_site( $site_data ); 1301 add_action( 'update_blog_public', 'wp_update_blog_public_option_on_site_update', 1, 2 ); 1302 1303 if ( is_wp_error( $blog_id ) ) { 1304 return $blog_id; 1300 1305 } 1301 1306 1302 1307 switch_to_blog( $blog_id ); … … 1306 1311 add_user_to_blog( $blog_id, $user_id, 'administrator' ); 1307 1312 1308 1313 foreach ( $meta as $key => $value ) { 1309 if ( in_array( $key, array( 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' ) ) ) { 1310 update_blog_status( $blog_id, $key, $value ); 1311 } else { 1312 update_option( $key, $value ); 1313 } 1314 update_option( $key, $value ); 1314 1315 } 1315 1316 1316 update_option( 'blog_public', (int) $ meta['public'] );1317 update_option( 'blog_public', (int) $site_data['public'] ); 1317 1318 1318 1319 if ( ! is_super_admin( $user_id ) && ! get_user_meta( $user_id, 'primary_blog', true ) ) { 1319 1320 update_user_meta( $user_id, 'primary_blog', $blog_id ); 1320 1321 } 1321 1322 1322 1323 restore_current_blog(); 1324 1325 $site = get_site( $blog_id ); 1326 1323 1327 /** 1324 1328 * Fires immediately after a new site is created. 1325 1329 * … … 1332 1336 * @param int $network_id Network ID. Only relevant on multi-network installations. 1333 1337 * @param array $meta Meta data. Used to set initial site options. 1334 1338 */ 1335 do_action( 'wpmu_new_blog', $blog_id, $user_id, $ domain, $path, $network_id, $meta );1339 do_action( 'wpmu_new_blog', $blog_id, $user_id, $site->domain, $site->path, $site->network_id, $meta ); 1336 1340 1337 1341 wp_cache_set( 'last_changed', microtime(), 'sites' ); 1338 1342 … … 1486 1490 } 1487 1491 1488 1492 /** 1489 * Store basic site info in the blogs table.1490 *1491 * This function creates a row in the wp_blogs table and returns1492 * the new blog's ID. It is the first step in creating a new blog.1493 *1494 * @since MU (3.0.0)1495 *1496 * @global wpdb $wpdb WordPress database abstraction object.1497 *1498 * @param string $domain The domain of the new site.1499 * @param string $path The path of the new site.1500 * @param int $network_id Unless you're running a multi-network installation, be sure to set this value to 1.1501 * @return int|false The ID of the new row1502 */1503 function insert_blog( $domain, $path, $network_id ) {1504 global $wpdb;1505 1506 $path = trailingslashit( $path );1507 $network_id = (int) $network_id;1508 1509 $result = $wpdb->insert(1510 $wpdb->blogs, array(1511 'site_id' => $network_id,1512 'domain' => $domain,1513 'path' => $path,1514 'registered' => current_time( 'mysql' ),1515 )1516 );1517 if ( ! $result ) {1518 return false;1519 }1520 1521 $blog_id = $wpdb->insert_id;1522 clean_blog_cache( $blog_id );1523 1524 wp_maybe_update_network_site_counts( $network_id );1525 1526 return $blog_id;1527 }1528 1529 /**1530 1493 * Install an empty blog. 1531 1494 * 1532 1495 * Creates the new blog tables and options. If calling this function … … 1538 1501 * @global wpdb $wpdb 1539 1502 * @global WP_Roles $wp_roles 1540 1503 * 1541 * @param int $blog_id The value returned by insert_blog().1504 * @param int $blog_id The value returned by wp_insert_site(). 1542 1505 * @param string $blog_title The title of the new site. 1543 1506 */ 1544 1507 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;