Ticket #40364: 40364.12.diff
| File 40364.12.diff, 51.9 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 } 300 $site = wp_update_site( $blog_id, $details ); 316 301 317 $update_details[ $field ] = $details[ $field ]; 318 } 319 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', true ); 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 * @param array $defaults Associative array of site default values. 453 * See {@see wp_insert_site()} for the possibly included data. 454 */ 455 $data = apply_filters( 'wp_normalize_site_data', $data, $defaults ); 456 457 458 $validity = new WP_Error(); 459 460 /** 461 * Fires when data should be validated for a site prior to inserting or updating in the database. 462 * 463 * Plugins should amend the `$validity` object via its `WP_Error::add()` method. 464 * 465 * @since 5.0.0 466 * 467 * @param WP_Error $validity Error object to add validation errors to. 468 * @param array $data Associative array of complete site data. See {@see wp_insert_site()} 469 * for the included data. 470 * @param WP_Site|null $old_site The old site object if the data belongs to a site being updated, 471 * or null if it is a new site being inserted. 472 */ 473 do_action( 'wp_validate_site_data', $validity, $data, null ); 474 475 if ( ! empty( $validity->errors ) ) { 476 return $validity; 477 } 478 479 if ( false === $wpdb->insert( $wpdb->blogs, $data ) ) { 480 return new WP_Error( 'db_insert_error', __( 'Could not insert site into the database.' ), $wpdb->last_error ); 481 } 482 483 $new_site = get_site( $wpdb->insert_id ); 484 485 clean_blog_cache( $new_site ); 486 487 /** 488 * Fires once a site has been inserted into the database. 489 * 490 * @since 5.0.0 491 * 492 * @param WP_Site $new_site New site object. 493 */ 494 do_action( 'wp_insert_site', $new_site ); 495 496 return (int) $new_site->id; 497 } 498 499 /** 500 * Updates a site in the database. 501 * 502 * @since 5.0.0 503 * 504 * @global wpdb $wpdb WordPress database abstraction object. 505 * 506 * @param int $site_id ID of the site that should be updated. 507 * @param array $data Site data to update. See {@see wp_insert_site()} for the list of supported keys. 508 * @return int|WP_Error The updated site's ID on success, or error object on failure. 509 */ 510 function wp_update_site( $site_id, array $data ) { 511 global $wpdb; 512 513 $old_site = get_site( $site_id ); 514 if ( ! $old_site ) { 515 return new WP_Error( 'site_not_exist', __( 'Site does not exist.' ) ); 516 } 517 518 $defaults = $old_site->to_array(); 519 $defaults['network_id'] = (int) $defaults['site_id']; 520 $defaults['last_updated'] = current_time( 'mysql', true ); 521 unset( $defaults['blog_id'], $defaults['site_id'] ); 522 523 /** This filter is documented in wp-includes/ms-blogs.php */ 524 $data = apply_filters( 'wp_normalize_site_data', $data, $defaults ); 525 526 $validity = new WP_Error(); 527 528 /** This action is documented in wp-includes/ms-blogs.php */ 529 do_action( 'wp_validate_site_data', $validity, $data, $old_site ); 530 531 if ( ! empty( $validity->errors ) ) { 532 return $validity; 533 } 534 535 if ( false === $wpdb->update( $wpdb->blogs, $data, array( 'blog_id' => $old_site->id ) ) ) { 536 return new WP_Error( 'db_update_error', __( 'Could not update site in the database.' ), $wpdb->last_error ); 537 } 538 539 clean_blog_cache( $old_site ); 540 541 $new_site = get_site( $old_site->id ); 542 543 /** 544 * Fires once a site has been updated in the database. 545 * 546 * @since 5.0.0 547 * 548 * @param WP_Site $new_site New site object. 549 * @param WP_Site $old_site Old site object. 550 */ 551 do_action( 'wp_update_site', $new_site, $old_site ); 552 553 return (int) $new_site->id; 554 } 555 556 /** 557 * Deletes a site from the database. 558 * 559 * @since 5.0.0 560 * 561 * @global wpdb $wpdb WordPress database abstraction object. 562 * 563 * @param int $site_id ID of the site that should be deleted. 564 * @return WP_Site|WP_Error The deleted site object on success, or error object on failure. 565 */ 566 function wp_delete_site( $site_id ) { 567 global $wpdb; 568 569 $old_site = get_site( $site_id ); 570 if ( ! $old_site ) { 571 return new WP_Error( 'site_not_exist', __( 'Site does not exist.' ) ); 572 } 573 574 if ( false === $wpdb->delete( $wpdb->blogs, array( 'blog_id' => $old_site->id ) ) ) { 575 return new WP_Error( 'db_delete_error', __( 'Could not delete site from the database.' ), $wpdb->last_error ); 576 } 577 578 clean_blog_cache( $old_site ); 579 580 /** 581 * Fires once a site has been deleted from the database. 582 * 583 * @since 5.0.0 584 * 585 * @param WP_Site $old_site Deleted site object. 586 */ 587 do_action( 'wp_delete_site', $old_site ); 588 589 return $old_site; 590 } 591 592 /** 521 593 * Retrieves site data given a site ID or site object. 522 594 * 523 595 * Site data will be cached and returned after being passed through a filter. … … 688 760 } 689 761 690 762 /** 763 * Normalizes data for a site prior to inserting or updating in the database. 764 * 765 * @since 5.0.0 766 * 767 * @param array $data Associative array of site data passed to the respective function. 768 * See {@see wp_insert_site()} for the possibly included data. 769 * @param array $defaults Associative array of site default values. 770 * See {@see wp_insert_site()} for the possibly included data. 771 * 772 * @return array Normalized site data. 773 */ 774 function wp_normalize_site_data( array $data, array $defaults = array() ) { 775 // Maintain backward-compatibility with `$site_id` as network ID. 776 if ( isset( $data['site_id'] ) ) { 777 if ( ! empty( $data['site_id'] ) && empty( $data['network_id'] ) ) { 778 $data['network_id'] = $data['site_id']; 779 } 780 unset( $data['site_id'] ); 781 } 782 783 // Sanitize domain if passed. 784 if ( array_key_exists( 'domain', $data ) ) { 785 $data['domain'] = trim( $data['domain'] ); 786 $data['domain'] = preg_replace( '/\s+/', '', sanitize_user( $data['domain'], true ) ); 787 if ( is_subdomain_install() ) { 788 $data['domain'] = str_replace( '@', '', $data['domain'] ); 789 } 790 } 791 792 // Sanitize path if passed. 793 if ( array_key_exists( 'path', $data ) ) { 794 $data['path'] = trailingslashit( '/' . trim( $data['path'], '/' ) ); 795 } 796 797 // Sanitize network ID if passed. 798 if ( array_key_exists( 'network_id', $data ) ) { 799 $data['network_id'] = (int) $data['network_id']; 800 } 801 802 // Sanitize status fields if passed. 803 $status_fields = array( 'public', 'archived', 'mature', 'spam', 'deleted' ); 804 foreach ( $status_fields as $status_field ) { 805 if ( array_key_exists( $status_field, $data ) ) { 806 $data[ $status_field ] = (int) $data[ $status_field ]; 807 } 808 } 809 810 // Strip date fields if empty. 811 $date_fields = array( 'registered', 'last_updated' ); 812 foreach ( $date_fields as $date_field ) { 813 if ( ! array_key_exists( $date_field, $data ) ) { 814 continue; 815 } 816 817 if ( empty( $data[ $date_field ] ) || '0000-00-00 00:00:00' === $data[ $date_field ] ) { 818 unset( $data[ $date_field ] ); 819 } 820 } 821 822 $whitelist = array( 'domain', 'path', 'network_id', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' ); 823 $data = array_intersect_key( wp_parse_args( $data, $defaults ), array_flip( $whitelist ) ); 824 825 // Use the previously set network if a falsy network ID has been passed. 826 if ( empty( $data['network_id'] ) ) { 827 $data['network_id'] = $defaults['network_id']; 828 } 829 830 // Prepare for database. 831 $data['site_id'] = $data['network_id']; 832 unset( $data['network_id'] ); 833 834 return $data; 835 } 836 837 /** 838 * Validates data for a site prior to inserting or updating in the database. 839 * 840 * @since 5.0.0 841 * 842 * @param WP_Error $validity Error object, passed by reference. Will contain validation errors if 843 * any occurred. 844 * @param array $data Associative array of complete site data. See {@see wp_insert_site()} 845 * for the included data. 846 * @param WP_Site|null $old_site The old site object if the data belongs to a site being updated, 847 * or null if it is a new site being inserted. 848 */ 849 function wp_validate_site_data( $validity, $data, $old_site = null ) { 850 // A domain must always be present. 851 if ( empty( $data['domain'] ) ) { 852 $validity->add( 'site_empty_domain', __( 'Site domain must not be empty.' ) ); 853 } 854 855 // A path must always be present. 856 if ( empty( $data['path'] ) ) { 857 $validity->add( 'site_empty_path', __( 'Site path must not be empty.' ) ); 858 } 859 860 // A network ID must always be present. 861 if ( empty( $data['network_id'] ) ) { 862 $validity->add( 'site_empty_network_id', __( 'Site network ID must be provided.' ) ); 863 } 864 865 // Both registration and last updated dates must always be present and valid. 866 $date_fields = array( 'registered', 'last_updated' ); 867 foreach ( $date_fields as $date_field ) { 868 if ( empty( $data[ $date_field ] ) ) { 869 $validity->add( 'site_empty_' . $date_field, __( 'Both registration and last updated dates must be provided.' ) ); 870 break; 871 } 872 873 $month = substr( $data[ $date_field ], 5, 2 ); 874 $day = substr( $data[ $date_field ], 8, 2 ); 875 $year = substr( $data[ $date_field ], 0, 4 ); 876 $valid_date = wp_checkdate( $month, $day, $year, $data[ $date_field ] ); 877 if ( ! $valid_date ) { 878 $validity->add( 'site_invalid_' . $date_field, __( 'Both registration and last updated dates must be valid dates.' ) ); 879 break; 880 } 881 } 882 883 if ( ! empty( $validity->errors ) ) { 884 return; 885 } 886 887 // If a new site, or domain/path/network ID have changed, ensure uniqueness. 888 if ( ! $old_site 889 || $data['domain'] !== $old_site->domain 890 || $data['path'] !== $old_site->path 891 || $data['network_id'] !== $old_site->network_id 892 ) { 893 if ( domain_exists( $data['domain'], $data['path'], $data['network_id'] ) ) { 894 $validity->add( 'site_taken', __( 'Sorry, that site already exists!' ) ); 895 } 896 } 897 } 898 899 /** 691 900 * Retrieve option value for a given blog id based on name of option. 692 901 * 693 902 * If the option does not exist or does not have a value, then the return value … … 1193 1402 * Update a blog details field. 1194 1403 * 1195 1404 * @since MU (3.0.0) 1405 * @since 5.0.0 Use wp_update_site() internally. 1196 1406 * 1197 1407 * @global wpdb $wpdb WordPress database abstraction object. 1198 1408 * … … 1213 1423 return $value; 1214 1424 } 1215 1425 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 ); 1426 $result = wp_update_site( $blog_id, array( 1427 $pref => $value, 1428 ) ); 1222 1429 1223 if ( false === $result) {1430 if ( is_wp_error( $result ) ) { 1224 1431 return false; 1225 1432 } 1226 1433 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 1434 return $value; 1274 1435 } 1275 1436 … … 1537 1698 1538 1699 update_posts_count(); 1539 1700 } 1701 1702 /** 1703 * Updates the count of sites for a network based on a changed site. 1704 * 1705 * @since 5.0.0 1706 * 1707 * @param WP_Site $new_site The site object that has been inserted, updated or deleted. 1708 * @param WP_Site|null $old_site Optional. If $new_site has been updated, this must be the previous 1709 * state of that site. Default null. 1710 */ 1711 function wp_maybe_update_network_site_counts_on_update( $new_site, $old_site = null ) { 1712 if ( null === $old_site ) { 1713 wp_maybe_update_network_site_counts( $new_site->network_id ); 1714 return; 1715 } 1716 1717 if ( $new_site->network_id != $old_site->network_id ) { 1718 wp_maybe_update_network_site_counts( $new_site->network_id ); 1719 wp_maybe_update_network_site_counts( $old_site->network_id ); 1720 } 1721 } 1722 1723 /** 1724 * Triggers actions on site status updates. 1725 * 1726 * @since 5.0.0 1727 * 1728 * @param WP_Site $new_site The site object after the update. 1729 * @param WP_Site|null $old_site Optional. If $new_site has been updated, this must be the previous 1730 * state of that site. Default null. 1731 */ 1732 function wp_maybe_transition_site_statuses_on_update( $new_site, $old_site = null ) { 1733 $site_id = $new_site->id; 1734 1735 // Use the default values for a site if no previous state is given. 1736 if ( ! $old_site ) { 1737 $old_site = new WP_Site( new stdClass() ); 1738 } 1739 1740 if ( $new_site->spam != $old_site->spam ) { 1741 if ( $new_site->spam == 1 ) { 1742 1743 /** 1744 * Fires when the 'spam' status is added to a site. 1745 * 1746 * @since MU (3.0.0) 1747 * 1748 * @param int $site_id Site ID. 1749 */ 1750 do_action( 'make_spam_blog', $site_id ); 1751 } else { 1752 1753 /** 1754 * Fires when the 'spam' status is removed from a site. 1755 * 1756 * @since MU (3.0.0) 1757 * 1758 * @param int $site_id Site ID. 1759 */ 1760 do_action( 'make_ham_blog', $site_id ); 1761 } 1762 } 1763 1764 if ( $new_site->mature != $old_site->mature ) { 1765 if ( $new_site->mature == 1 ) { 1766 1767 /** 1768 * Fires when the 'mature' status is added to a site. 1769 * 1770 * @since 3.1.0 1771 * 1772 * @param int $site_id Site ID. 1773 */ 1774 do_action( 'mature_blog', $site_id ); 1775 } else { 1776 1777 /** 1778 * Fires when the 'mature' status is removed from a site. 1779 * 1780 * @since 3.1.0 1781 * 1782 * @param int $site_id Site ID. 1783 */ 1784 do_action( 'unmature_blog', $site_id ); 1785 } 1786 } 1787 1788 if ( $new_site->archived != $old_site->archived ) { 1789 if ( $new_site->archived == 1 ) { 1790 1791 /** 1792 * Fires when the 'archived' status is added to a site. 1793 * 1794 * @since MU (3.0.0) 1795 * 1796 * @param int $site_id Site ID. 1797 */ 1798 do_action( 'archive_blog', $site_id ); 1799 } else { 1800 1801 /** 1802 * Fires when the 'archived' status is removed from a site. 1803 * 1804 * @since MU (3.0.0) 1805 * 1806 * @param int $site_id Site ID. 1807 */ 1808 do_action( 'unarchive_blog', $site_id ); 1809 } 1810 } 1811 1812 if ( $new_site->deleted != $old_site->deleted ) { 1813 if ( $new_site->deleted == 1 ) { 1814 1815 /** 1816 * Fires when the 'deleted' status is added to a site. 1817 * 1818 * @since 3.5.0 1819 * 1820 * @param int $site_id Site ID. 1821 */ 1822 do_action( 'make_delete_blog', $site_id ); 1823 } else { 1824 1825 /** 1826 * Fires when the 'deleted' status is removed from a site. 1827 * 1828 * @since 3.5.0 1829 * 1830 * @param int $site_id Site ID. 1831 */ 1832 do_action( 'make_undelete_blog', $site_id ); 1833 } 1834 } 1835 1836 if ( $new_site->public != $old_site->public ) { 1837 1838 /** 1839 * Fires after the current blog's 'public' setting is updated. 1840 * 1841 * @since MU (3.0.0) 1842 * 1843 * @param int $site_id Site ID. 1844 * @param string $value The value of the site status. 1845 */ 1846 do_action( 'update_blog_public', $site_id, $new_site->public ); 1847 } 1848 } 1849 1850 /** 1851 * Cleans the necessary caches after specific site data has been updated. 1852 * 1853 * @since 5.0.0 1854 * 1855 * @param WP_Site $new_site The site object after the update. 1856 * @param WP_Site $old_site The site obejct prior to the update. 1857 */ 1858 function wp_maybe_clean_new_site_cache_on_update( $new_site, $old_site ) { 1859 if ( $old_site->domain !== $new_site->domain || $old_site->path !== $new_site->path ) { 1860 clean_blog_cache( $new_site ); 1861 } 1862 } 1863 1864 /** 1865 * Updates the `blog_public` option for a given site ID. 1866 * 1867 * @since 5.0.0 1868 * 1869 * @param int $site_id Site ID. 1870 * @param string $public The value of the site status. 1871 */ 1872 function wp_update_blog_public_option_on_site_update( $site_id, $public ) { 1873 update_blog_option( $site_id, 'blog_public', $public ); 1874 } -
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_filter( '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 1279 // Check if the domain has been used already. We should return an error message. 1290 1280 if ( domain_exists( $domain, $path, $network_id ) ) { 1291 1281 return new WP_Error( 'blog_taken', __( 'Sorry, that site already exists!' ) ); … … 1295 1285 wp_installing( true ); 1296 1286 } 1297 1287 1298 if ( ! $blog_id = insert_blog( $domain, $path, $network_id ) ) { 1299 return new WP_Error( 'insert_blog', __( 'Could not create site.' ) ); 1288 $site_data_whitelist = array( 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' ); 1289 1290 $site_data = array_merge( 1291 array( 1292 'domain' => $domain, 1293 'path' => $path, 1294 'network_id' => $network_id, 1295 ), 1296 array_intersect_key( 1297 $meta, 1298 array_flip( $site_data_whitelist ) 1299 ) 1300 ); 1301 1302 $meta = array_diff_key( $meta, array_flip( $site_data_whitelist ) ); 1303 1304 remove_action( 'update_blog_public', 'wp_update_blog_public_option_on_site_update', 1 ); 1305 $blog_id = wp_insert_site( $site_data ); 1306 add_action( 'update_blog_public', 'wp_update_blog_public_option_on_site_update', 1, 2 ); 1307 1308 if ( is_wp_error( $blog_id ) ) { 1309 return $blog_id; 1300 1310 } 1301 1311 1302 1312 switch_to_blog( $blog_id ); … … 1306 1316 add_user_to_blog( $blog_id, $user_id, 'administrator' ); 1307 1317 1308 1318 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 } 1319 update_option( $key, $value ); 1314 1320 } 1315 1321 1316 update_option( 'blog_public', (int) $ meta['public'] );1322 update_option( 'blog_public', (int) $site_data['public'] ); 1317 1323 1318 1324 if ( ! is_super_admin( $user_id ) && ! get_user_meta( $user_id, 'primary_blog', true ) ) { 1319 1325 update_user_meta( $user_id, 'primary_blog', $blog_id ); 1320 1326 } 1321 1327 1322 1328 restore_current_blog(); 1329 1330 $site = get_site( $blog_id ); 1331 1323 1332 /** 1324 1333 * Fires immediately after a new site is created. 1325 1334 * … … 1332 1341 * @param int $network_id Network ID. Only relevant on multi-network installations. 1333 1342 * @param array $meta Meta data. Used to set initial site options. 1334 1343 */ 1335 do_action( 'wpmu_new_blog', $blog_id, $user_id, $ domain, $path, $network_id, $meta );1344 do_action( 'wpmu_new_blog', $blog_id, $user_id, $site->domain, $site->path, $site->network_id, $meta ); 1336 1345 1337 1346 wp_cache_set( 'last_changed', microtime(), 'sites' ); 1338 1347 … … 1486 1495 } 1487 1496 1488 1497 /** 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 1498 * Install an empty blog. 1531 1499 * 1532 1500 * Creates the new blog tables and options. If calling this function … … 1538 1506 * @global wpdb $wpdb 1539 1507 * @global WP_Roles $wp_roles 1540 1508 * 1541 * @param int $blog_id The value returned by insert_blog().1509 * @param int $blog_id The value returned by wp_insert_site(). 1542 1510 * @param string $blog_title The title of the new site. 1543 1511 */ 1544 1512 function install_blog( $blog_id, $blog_title = '' ) { -
tests/phpunit/tests/multisite/site.php
10 10 */ 11 11 class Tests_Multisite_Site extends WP_UnitTestCase { 12 12 protected $suppress = false; 13 protected $site_status_hooks = array(); 13 14 protected static $network_ids; 14 15 protected static $site_ids; 15 16 … … 446 447 $this->assertEquals( '0', $blog->spam ); 447 448 $this->assertEquals( 1, $test_action_counter ); 448 449 449 // The action should fire if the status of 'spam' stays the same.450 // The action should not fire if the status of 'spam' stays the same. 450 451 update_blog_status( $blog_id, 'spam', 0 ); 451 452 $blog = get_site( $blog_id ); 452 453 453 454 $this->assertEquals( '0', $blog->spam ); 454 $this->assertEquals( 2, $test_action_counter );455 $this->assertEquals( 1, $test_action_counter ); 455 456 456 457 remove_action( 'make_ham_blog', array( $this, '_action_counter_cb' ), 10 ); 457 458 } … … 469 470 $this->assertEquals( '1', $blog->spam ); 470 471 $this->assertEquals( 1, $test_action_counter ); 471 472 472 // The action should fire if the status of 'spam' stays the same.473 // The action should not fire if the status of 'spam' stays the same. 473 474 update_blog_status( $blog_id, 'spam', 1 ); 474 475 $blog = get_site( $blog_id ); 475 476 476 477 $this->assertEquals( '1', $blog->spam ); 477 $this->assertEquals( 2, $test_action_counter );478 $this->assertEquals( 1, $test_action_counter ); 478 479 479 480 remove_action( 'make_spam_blog', array( $this, '_action_counter_cb' ), 10 ); 480 481 } … … 492 493 $this->assertEquals( '1', $blog->archived ); 493 494 $this->assertEquals( 1, $test_action_counter ); 494 495 495 // The action should fire if the status of 'archived' stays the same.496 // The action should not fire if the status of 'archived' stays the same. 496 497 update_blog_status( $blog_id, 'archived', 1 ); 497 498 $blog = get_site( $blog_id ); 498 499 499 500 $this->assertEquals( '1', $blog->archived ); 500 $this->assertEquals( 2, $test_action_counter );501 $this->assertEquals( 1, $test_action_counter ); 501 502 502 503 remove_action( 'archive_blog', array( $this, '_action_counter_cb' ), 10 ); 503 504 } … … 516 517 $this->assertEquals( '0', $blog->archived ); 517 518 $this->assertEquals( 1, $test_action_counter ); 518 519 519 // The action should fire if the status of 'archived' stays the same.520 // The action should not fire if the status of 'archived' stays the same. 520 521 update_blog_status( $blog_id, 'archived', 0 ); 521 522 $blog = get_site( $blog_id ); 522 523 $this->assertEquals( '0', $blog->archived ); 523 $this->assertEquals( 2, $test_action_counter );524 $this->assertEquals( 1, $test_action_counter ); 524 525 525 526 remove_action( 'unarchive_blog', array( $this, '_action_counter_cb' ), 10 ); 526 527 } … … 538 539 $this->assertEquals( '1', $blog->deleted ); 539 540 $this->assertEquals( 1, $test_action_counter ); 540 541 541 // The action should fire if the status of 'deleted' stays the same.542 // The action should not fire if the status of 'deleted' stays the same. 542 543 update_blog_status( $blog_id, 'deleted', 1 ); 543 544 $blog = get_site( $blog_id ); 544 545 545 546 $this->assertEquals( '1', $blog->deleted ); 546 $this->assertEquals( 2, $test_action_counter );547 $this->assertEquals( 1, $test_action_counter ); 547 548 548 549 remove_action( 'make_delete_blog', array( $this, '_action_counter_cb' ), 10 ); 549 550 } … … 562 563 $this->assertEquals( '0', $blog->deleted ); 563 564 $this->assertEquals( 1, $test_action_counter ); 564 565 565 // The action should fire if the status of 'deleted' stays the same.566 // The action should not fire if the status of 'deleted' stays the same. 566 567 update_blog_status( $blog_id, 'deleted', 0 ); 567 568 $blog = get_site( $blog_id ); 568 569 569 570 $this->assertEquals( '0', $blog->deleted ); 570 $this->assertEquals( 2, $test_action_counter );571 $this->assertEquals( 1, $test_action_counter ); 571 572 572 573 remove_action( 'make_undelete_blog', array( $this, '_action_counter_cb' ), 10 ); 573 574 } … … 585 586 $this->assertEquals( '1', $blog->mature ); 586 587 $this->assertEquals( 1, $test_action_counter ); 587 588 588 // The action should fire if the status of 'mature' stays the same.589 // The action should not fire if the status of 'mature' stays the same. 589 590 update_blog_status( $blog_id, 'mature', 1 ); 590 591 $blog = get_site( $blog_id ); 591 592 592 593 $this->assertEquals( '1', $blog->mature ); 593 $this->assertEquals( 2, $test_action_counter );594 $this->assertEquals( 1, $test_action_counter ); 594 595 595 596 remove_action( 'mature_blog', array( $this, '_action_counter_cb' ), 10 ); 596 597 } … … 609 610 $this->assertEquals( '0', $blog->mature ); 610 611 $this->assertEquals( 1, $test_action_counter ); 611 612 612 // The action should fire if the status of 'mature' stays the same.613 // The action should not fire if the status of 'mature' stays the same. 613 614 update_blog_status( $blog_id, 'mature', 0 ); 614 615 $blog = get_site( $blog_id ); 615 616 616 617 $this->assertEquals( '0', $blog->mature ); 617 $this->assertEquals( 2, $test_action_counter );618 $this->assertEquals( 1, $test_action_counter ); 618 619 619 620 remove_action( 'unmature_blog', array( $this, '_action_counter_cb' ), 10 ); 620 621 } … … 632 633 $this->assertEquals( '0', $blog->public ); 633 634 $this->assertEquals( 1, $test_action_counter ); 634 635 635 // The action should fire if the status of 'mature' stays the same.636 // The action should not fire if the status of 'mature' stays the same. 636 637 update_blog_status( $blog_id, 'public', 0 ); 637 638 $blog = get_site( $blog_id ); 638 639 639 640 $this->assertEquals( '0', $blog->public ); 640 $this->assertEquals( 2, $test_action_counter );641 $this->assertEquals( 1, $test_action_counter ); 641 642 642 643 remove_action( 'update_blog_public', array( $this, '_action_counter_cb' ), 10 ); 643 644 } … … 1259 1260 array( 'current_blog_%domain%%path%', 'site-options' ), 1260 1261 ); 1261 1262 } 1263 1264 /** 1265 * @ticket 40364 1266 * @dataProvider data_wp_insert_site 1267 */ 1268 public function test_wp_insert_site( $site_data, $expected_data ) { 1269 $site_id = wp_insert_site( $site_data ); 1270 1271 $this->assertInternalType( 'integer', $site_id ); 1272 1273 $site = get_site( $site_id ); 1274 foreach ( $expected_data as $key => $value ) { 1275 $this->assertEquals( $value, $site->$key ); 1276 } 1277 } 1278 1279 public function data_wp_insert_site() { 1280 return array( 1281 array( 1282 array( 1283 'domain' => 'example.com', 1284 ), 1285 array( 1286 'domain' => 'example.com', 1287 'path' => '/', 1288 'network_id' => 1, 1289 'public' => 1, 1290 'archived' => 0, 1291 'mature' => 0, 1292 'spam' => 0, 1293 'deleted' => 0, 1294 'lang_id' => 0, 1295 ), 1296 ), 1297 array( 1298 array( 1299 'domain' => 'example.com', 1300 'path' => '/foo', 1301 'network_id' => 2, 1302 ), array( 1303 'domain' => 'example.com', 1304 'path' => '/foo/', 1305 'network_id' => 2, 1306 ), 1307 ), 1308 array( 1309 array( 1310 'domain' => 'example.com', 1311 'path' => '/bar/', 1312 'site_id' => 2, 1313 'public' => 0, 1314 ), array( 1315 'domain' => 'example.com', 1316 'path' => '/bar/', 1317 'network_id' => 2, 1318 'public' => 0, 1319 ), 1320 ), 1321 array( 1322 array( 1323 'domain' => 'example.com', 1324 'path' => 'foobar', 1325 'public' => 0, 1326 'archived' => 1, 1327 'mature' => 1, 1328 'spam' => 1, 1329 'deleted' => 1, 1330 'lang_id' => 1, 1331 ), array( 1332 'domain' => 'example.com', 1333 'path' => '/foobar/', 1334 'public' => 0, 1335 'archived' => 1, 1336 'mature' => 1, 1337 'spam' => 1, 1338 'deleted' => 1, 1339 'lang_id' => 1, 1340 ), 1341 ), 1342 ); 1343 } 1344 1345 /** 1346 * @ticket 40364 1347 */ 1348 public function test_wp_insert_site_empty_domain() { 1349 $site_id = wp_insert_site( array( 'public' => 0 ) ); 1350 1351 $this->assertWPError( $site_id ); 1352 $this->assertSame( 'site_empty_domain', $site_id->get_error_code() ); 1353 } 1354 1355 /** 1356 * @ticket 40364 1357 * @dataProvider data_wp_update_site 1358 */ 1359 public function test_wp_update_site( $site_data, $expected_data ) { 1360 $site_id = self::factory()->blog->create(); 1361 1362 $old_site = get_site( $site_id ); 1363 1364 $result = wp_update_site( $site_id, $site_data ); 1365 1366 $this->assertSame( $site_id, $result ); 1367 1368 $new_site = get_site( $site_id ); 1369 foreach ( $new_site->to_array() as $key => $value ) { 1370 if ( isset( $expected_data[ $key ] ) ) { 1371 $this->assertEquals( $expected_data[ $key ], $value ); 1372 } elseif ( 'last_updated' === $key ) { 1373 $this->assertTrue( $old_site->last_updated <= $value ); 1374 } else { 1375 $this->assertEquals( $old_site->$key, $value ); 1376 } 1377 } 1378 } 1379 1380 public function data_wp_update_site() { 1381 return array( 1382 array( 1383 array( 1384 'domain' => 'example.com', 1385 'network_id' => 2, 1386 ), 1387 array( 1388 'domain' => 'example.com', 1389 'site_id' => 2, 1390 ), 1391 ), 1392 array( 1393 array( 1394 'path' => 'foo', 1395 ), 1396 array( 1397 'path' => '/foo/' 1398 ), 1399 ), 1400 array( 1401 array( 1402 'public' => 0, 1403 'archived' => 1, 1404 'mature' => 1, 1405 'spam' => 1, 1406 'deleted' => 1, 1407 'lang_id' => 1, 1408 ), 1409 array( 1410 'public' => 0, 1411 'archived' => 1, 1412 'mature' => 1, 1413 'spam' => 1, 1414 'deleted' => 1, 1415 'lang_id' => 1, 1416 ), 1417 ), 1418 ); 1419 } 1420 1421 /** 1422 * @ticket 40364 1423 */ 1424 public function test_wp_update_site_empty_domain() { 1425 $site_id = self::factory()->blog->create(); 1426 1427 $result = wp_update_site( $site_id, array( 'domain' => '' ) ); 1428 1429 $this->assertWPError( $result ); 1430 $this->assertSame( 'site_empty_domain', $result->get_error_code() ); 1431 } 1432 1433 /** 1434 * @ticket 40364 1435 */ 1436 public function test_wp_update_site_invalid_id() { 1437 $result = wp_update_site( 444444, array( 'domain' => 'example.com' ) ); 1438 1439 $this->assertWPError( $result ); 1440 $this->assertSame( 'site_not_exist', $result->get_error_code() ); 1441 } 1442 1443 /** 1444 * @ticket 40364 1445 */ 1446 public function test_wp_update_site_cleans_cache() { 1447 $site_id = self::factory()->blog->create(); 1448 $site1 = get_site( $site_id ); 1449 1450 $result = wp_update_site( $site_id, array( 'public' => 0 ) ); 1451 $site2 = get_site( $site_id ); 1452 1453 $result = wp_update_site( $site_id, array( 'public' => 1 ) ); 1454 $site3 = get_site( $site_id ); 1455 1456 $this->assertEquals( 1, $site1->public ); 1457 $this->assertEquals( 0, $site2->public ); 1458 $this->assertEquals( 1, $site3->public ); 1459 } 1460 1461 /** 1462 * @ticket 40364 1463 */ 1464 public function test_wp_delete_site() { 1465 $site_id = self::factory()->blog->create(); 1466 1467 $site = get_site( $site_id ); 1468 1469 $result = wp_delete_site( $site_id ); 1470 1471 $this->assertInstanceOf( 'WP_Site', $result ); 1472 $this->assertEquals( $result->to_array(), $site->to_array() ); 1473 } 1474 1475 /** 1476 * @ticket 40364 1477 */ 1478 public function test_wp_delete_site_invalid_id() { 1479 $result = wp_delete_site( 444444 ); 1480 1481 $this->assertWPError( $result ); 1482 $this->assertSame( 'site_not_exist', $result->get_error_code() ); 1483 } 1484 1485 /** 1486 * @ticket 40364 1487 * @dataProvider data_wp_normalize_site_data 1488 */ 1489 public function test_wp_normalize_site_data( $data, $expected ) { 1490 $result = wp_normalize_site_data( $data ); 1491 1492 $this->assertEqualSetsWithIndex( $expected, $result ); 1493 } 1494 1495 public function data_wp_normalize_site_data() { 1496 return array( 1497 array( 1498 array( 1499 'site_id' => 4, 1500 ), 1501 array( 1502 'network_id' => 4, 1503 ), 1504 ), 1505 array( 1506 array( 1507 'site_id' => 4, 1508 'network_id' => 5, 1509 ), 1510 array( 1511 'network_id' => 5, 1512 ), 1513 ), 1514 array( 1515 array( 1516 'network_id' => '4', 1517 ), 1518 array( 1519 'network_id' => 4, 1520 ), 1521 ), 1522 array( 1523 array( 1524 'domain' => 'invalid domain .com', 1525 'path' => 'foo', 1526 ), 1527 array( 1528 'domain' => 'invaliddomain.com', 1529 'path' => '/foo/', 1530 ), 1531 ), 1532 array( 1533 array( 1534 'domain' => '<yet>/another-invalid-domain.com', 1535 ), 1536 array( 1537 'domain' => 'another-invalid-domain.com', 1538 ), 1539 ), 1540 array( 1541 array( 1542 'path' => '', 1543 ), 1544 array( 1545 'path' => '/', 1546 ), 1547 ), 1548 array( 1549 array( 1550 'public' => '0', 1551 'archived' => '1', 1552 'mature' => '1', 1553 'spam' => true, 1554 'deleted' => true, 1555 ), 1556 array( 1557 'public' => 0, 1558 'archived' => 1, 1559 'mature' => 1, 1560 'spam' => 1, 1561 'deleted' => 1, 1562 ), 1563 ), 1564 array( 1565 array( 1566 'registered' => '', 1567 'last_updated' => '', 1568 ), 1569 array(), 1570 ), 1571 array( 1572 array( 1573 'registered' => '0000-00-00 00:00:00', 1574 'last_updated' => '0000-00-00 00:00:00', 1575 ), 1576 array(), 1577 ), 1578 ); 1579 } 1580 1581 /** 1582 * @ticket 40364 1583 * @dataProvider data_wp_validate_site_data 1584 */ 1585 public function test_wp_validate_site_data( $data, $expected_errors ) { 1586 $result = new WP_Error(); 1587 wp_validate_site_data( $result, $data ); 1588 1589 if ( empty( $expected_errors ) ) { 1590 $this->assertEmpty( $result->errors ); 1591 } else { 1592 $this->assertEqualSets( $expected_errors, array_keys( $result->errors ) ); 1593 } 1594 } 1595 1596 public function data_wp_validate_site_data() { 1597 $date = current_time( 'mysql', true ); 1598 1599 return array( 1600 array( 1601 array( 1602 'domain' => 'example-site.com', 1603 'path' => '/', 1604 'network_id' => 1, 1605 'registered' => $date, 1606 'last_updated' => $date, 1607 ), 1608 array(), 1609 ), 1610 array( 1611 array( 1612 'path' => '/', 1613 'network_id' => 1, 1614 'registered' => $date, 1615 'last_updated' => $date, 1616 ), 1617 array( 'site_empty_domain' ), 1618 ), 1619 array( 1620 array( 1621 'domain' => 'example-site.com', 1622 'network_id' => 1, 1623 'registered' => $date, 1624 'last_updated' => $date, 1625 ), 1626 array( 'site_empty_path' ), 1627 ), 1628 array( 1629 array( 1630 'domain' => 'example-site.com', 1631 'path' => '/', 1632 'registered' => $date, 1633 'last_updated' => $date, 1634 ), 1635 array( 'site_empty_network_id' ), 1636 ), 1637 array( 1638 array( 1639 'domain' => get_site()->domain, 1640 'path' => get_site()->path, 1641 'network_id' => get_site()->network_id, 1642 'registered' => $date, 1643 'last_updated' => $date, 1644 ), 1645 array( 'site_taken' ), 1646 ), 1647 array( 1648 array( 1649 'domain' => 'valid-domain.com', 1650 'path' => '/valid-path/', 1651 'network_id' => 1, 1652 'registered' => '', 1653 'last_updated' => $date, 1654 ), 1655 array( 'site_empty_registered' ), 1656 ), 1657 array( 1658 array( 1659 'domain' => 'valid-domain.com', 1660 'path' => '/valid-path/', 1661 'network_id' => 1, 1662 'registered' => $date, 1663 'last_updated' => '', 1664 ), 1665 array( 'site_empty_last_updated' ), 1666 ), 1667 array( 1668 array( 1669 'domain' => 'valid-domain.com', 1670 'path' => '/valid-path/', 1671 'network_id' => 1, 1672 'registered' => '2000-13-32 25:25:61', 1673 'last_updated' => $date, 1674 ), 1675 array( 'site_invalid_registered' ), 1676 ), 1677 array( 1678 array( 1679 'domain' => 'valid-domain.com', 1680 'path' => '/valid-path/', 1681 'network_id' => 1, 1682 'registered' => $date, 1683 'last_updated' => '2000-13-32 25:25:61', 1684 ), 1685 array( 'site_invalid_last_updated' ), 1686 ), 1687 ); 1688 } 1689 1690 /** 1691 * @ticket 40364 1692 */ 1693 public function test_site_dates_are_gmt() { 1694 $first_date = current_time( 'mysql', true ); 1695 $site_id = wp_insert_site( array( 1696 'domain' => 'valid-domain.com', 1697 'path' => '/valid-path/', 1698 'network_id' => 1, 1699 ) ); 1700 $this->assertInternalType( 'integer', $site_id ); 1701 1702 $site = get_site( $site_id ); 1703 $this->assertSame( $first_date, $site->registered ); 1704 $this->assertSame( $first_date, $site->last_updated ); 1705 1706 $second_date = current_time( 'mysql', true ); 1707 $site_id = wp_update_site( $site_id, array() ); 1708 $this->assertInternalType( 'integer', $site_id ); 1709 1710 $site = get_site( $site_id ); 1711 $this->assertSame( $first_date, $site->registered ); 1712 $this->assertSame( $second_date, $site->last_updated ); 1713 } 1714 1715 /** 1716 * @ticket 40364 1717 */ 1718 public function test_wp_delete_site_cleans_cache() { 1719 $site_id = self::factory()->blog->create(); 1720 1721 get_site( $site_id ); 1722 1723 wp_delete_site( $site_id ); 1724 1725 $this->assertNull( get_site( $site_id ) ); 1726 } 1727 1728 /** 1729 * @ticket 40364 1730 */ 1731 public function test_wp_update_site_cleans_old_cache_on_domain_change() { 1732 $old_domain = 'old.wordpress.org'; 1733 $new_domain = 'new.wordpress.org'; 1734 1735 $site = self::factory()->blog->create_and_get( array( 1736 'domain' => $old_domain, 1737 'path' => '/', 1738 ) ); 1739 1740 // Populate the caches. 1741 get_blog_details( array( 1742 'domain' => $old_domain, 1743 'path' => '/', 1744 ) ); 1745 get_blog_id_from_url( $old_domain, '/' ); 1746 get_blog_details( array( 1747 'domain' => $new_domain, 1748 'path' => '/', 1749 ) ); 1750 get_blog_id_from_url( $new_domain, '/' ); 1751 1752 wp_update_site( $site->id, array( 1753 'domain' => $new_domain, 1754 ) ); 1755 1756 $domain_path_key_old = md5( $old_domain . '/' ); 1757 $domain_path_key_new = md5( $new_domain . '/' ); 1758 1759 // Ensure all respective cache values are empty. 1760 $result = array( 1761 wp_cache_get( $domain_path_key_old, 'blog-lookup' ), 1762 wp_cache_get( $domain_path_key_old, 'blog-id-cache' ), 1763 wp_cache_get( 'current_blog_' . $old_domain, 'site-options' ), 1764 wp_cache_get( 'current_blog_' . $old_domain . '/', 'site-options' ), 1765 wp_cache_get( $domain_path_key_new, 'blog-lookup' ), 1766 wp_cache_get( $domain_path_key_new, 'blog-id-cache' ), 1767 wp_cache_get( 'current_blog_' . $new_domain, 'site-options' ), 1768 wp_cache_get( 'current_blog_' . $new_domain . '/', 'site-options' ), 1769 ); 1770 1771 $this->assertEmpty( array_filter( $result ) ); 1772 } 1773 1774 /** 1775 * @ticket 40364 1776 */ 1777 public function test_wp_update_site_cleans_old_cache_on_path_change() { 1778 $old_path = '/foo/'; 1779 $new_path = '/bar/'; 1780 1781 $site = self::factory()->blog->create_and_get( array( 1782 'domain' => 'test.wordpress.org', 1783 'path' => $old_path, 1784 ) ); 1785 1786 // Populate the caches. 1787 get_blog_details( array( 1788 'domain' => 'test.wordpress.org', 1789 'path' => $old_path, 1790 ) ); 1791 get_blog_id_from_url( 'test.wordpress.org', $old_path ); 1792 get_blog_details( array( 1793 'domain' => 'test.wordpress.org', 1794 'path' => $new_path, 1795 ) ); 1796 get_blog_id_from_url( 'test.wordpress.org', $new_path ); 1797 1798 wp_update_site( $site->id, array( 1799 'path' => $new_path, 1800 ) ); 1801 1802 $domain_path_key_old = md5( 'test.wordpress.org' . $old_path ); 1803 $domain_path_key_new = md5( 'test.wordpress.org' . $new_path ); 1804 1805 // Ensure all respective cache values are empty. 1806 $result = array( 1807 wp_cache_get( $domain_path_key_old, 'blog-lookup' ), 1808 wp_cache_get( $domain_path_key_old, 'blog-id-cache' ), 1809 wp_cache_get( 'current_blog_test.wordpress.org' . $old_path, 'site-options' ), 1810 wp_cache_get( $domain_path_key_new, 'blog-lookup' ), 1811 wp_cache_get( $domain_path_key_new, 'blog-id-cache' ), 1812 wp_cache_get( 'current_blog_test.wordpress.org' . $new_path, 'site-options' ), 1813 ); 1814 1815 $this->assertEmpty( array_filter( $result ) ); 1816 } 1817 1818 /** 1819 * @ticket 40364 1820 * @dataProvider data_site_status_hook_triggers 1821 */ 1822 public function test_site_status_hook_triggers( $insert_site_data, $expected_insert_hooks, $update_site_data, $expected_update_hooks ) { 1823 // First: Insert a site. 1824 $this->listen_to_site_status_hooks(); 1825 1826 $site_data = array_merge( array( 1827 'domain' => 'example-site.com', 1828 'path' => '/', 1829 ), $insert_site_data ); 1830 1831 $site_id = wp_insert_site( $site_data ); 1832 1833 $insert_expected = array_fill_keys( $expected_insert_hooks, $site_id ); 1834 $insert_result = $this->get_listen_to_site_status_hooks_result(); 1835 1836 // Second: Update that site. 1837 $this->listen_to_site_status_hooks(); 1838 1839 wp_update_site( $site_id, $update_site_data ); 1840 1841 $update_expected = array_fill_keys( $expected_update_hooks, $site_id ); 1842 $update_result = $this->get_listen_to_site_status_hooks_result(); 1843 1844 // Check both insert and update results. 1845 $this->assertEqualSetsWithIndex( $insert_expected, $insert_result ); 1846 $this->assertEqualSetsWithIndex( $update_expected, $update_result ); 1847 } 1848 1849 public function data_site_status_hook_triggers() { 1850 return array( 1851 array( 1852 array( 1853 'public' => 1, 1854 'archived' => 1, 1855 'mature' => 1, 1856 'spam' => 1, 1857 'deleted' => 1, 1858 ), 1859 array( 1860 'archive_blog', 1861 'mature_blog', 1862 'make_spam_blog', 1863 'make_delete_blog', 1864 ), 1865 array( 1866 'public' => 0, 1867 'archived' => 0, 1868 'mature' => 0, 1869 'spam' => 0, 1870 'deleted' => 0, 1871 ), 1872 array( 1873 'update_blog_public', 1874 'unarchive_blog', 1875 'unmature_blog', 1876 'make_ham_blog', 1877 'make_undelete_blog', 1878 ), 1879 ), 1880 array( 1881 array( 1882 'public' => 0, 1883 'archived' => 0, 1884 'mature' => 0, 1885 'spam' => 0, 1886 'deleted' => 0, 1887 ), 1888 array( 1889 'update_blog_public', 1890 ), 1891 array( 1892 'public' => 1, 1893 'archived' => 1, 1894 'mature' => 1, 1895 'spam' => 1, 1896 'deleted' => 1, 1897 ), 1898 array( 1899 'update_blog_public', 1900 'archive_blog', 1901 'mature_blog', 1902 'make_spam_blog', 1903 'make_delete_blog', 1904 ), 1905 ), 1906 array( 1907 array( 1908 'public' => 0, 1909 'archived' => 0, 1910 'mature' => 1, 1911 'spam' => 1, 1912 'deleted' => 1, 1913 ), 1914 array( 1915 'update_blog_public', 1916 'mature_blog', 1917 'make_spam_blog', 1918 'make_delete_blog', 1919 ), 1920 array( 1921 'public' => 0, 1922 'archived' => 1, 1923 'mature' => 1, 1924 'spam' => 1, 1925 'deleted' => 0, 1926 ), 1927 array( 1928 'archive_blog', 1929 'make_undelete_blog', 1930 ), 1931 ), 1932 ); 1933 } 1934 1935 private function listen_to_site_status_hooks() { 1936 $this->site_status_hooks = array(); 1937 1938 $hooknames = array( 1939 'make_spam_blog', 1940 'make_ham_blog', 1941 'mature_blog', 1942 'unmature_blog', 1943 'archive_blog', 1944 'unarchive_blog', 1945 'make_delete_blog', 1946 'make_undelete_blog', 1947 'update_blog_public', 1948 ); 1949 1950 foreach ( $hooknames as $hookname ) { 1951 add_action( $hookname, array( $this, 'action_site_status_hook' ), 10, 1 ); 1952 } 1953 } 1954 1955 private function get_listen_to_site_status_hooks_result() { 1956 $hooknames = array( 1957 'make_spam_blog', 1958 'make_ham_blog', 1959 'mature_blog', 1960 'unmature_blog', 1961 'archive_blog', 1962 'unarchive_blog', 1963 'make_delete_blog', 1964 'make_undelete_blog', 1965 'update_blog_public', 1966 ); 1967 1968 foreach ( $hooknames as $hookname ) { 1969 remove_action( $hookname, array( $this, 'action_site_status_hook' ), 10 ); 1970 } 1971 1972 return $this->site_status_hooks; 1973 } 1974 1975 public function action_site_status_hook( $site_id ) { 1976 $this->site_status_hooks[ current_action() ] = $site_id; 1977 } 1262 1978 } 1263 1979 1264 1980 endif;