Ticket #40364: 40364.7.diff
| File 40364.7.diff, 34.5 KB (added by , 8 years ago) |
|---|
-
src/wp-admin/includes/ms.php
56 56 * Delete a site. 57 57 * 58 58 * @since 3.0.0 59 * @since 5.0.0 Use wp_delete_site() internally to delete the site row from the database. 59 60 * 60 61 * @global wpdb $wpdb WordPress database abstraction object. 61 62 * … … 135 136 $wpdb->query( "DROP TABLE IF EXISTS `$table`" ); 136 137 } 137 138 138 $wpdb->delete( $wpdb->blogs, array( 'blog_id' => $blog_id ));139 wp_delete_site( $blog_id ); 139 140 140 141 /** 141 142 * Filters the upload base directory to delete when the site is deleted. -
src/wp-includes/ms-blogs.php
297 297 $details = get_object_vars( $details ); 298 298 } 299 299 300 $current_details = get_site( $blog_id ); 301 if ( empty( $current_details ) ) { 302 return false; 303 } 304 305 $current_details = get_object_vars( $current_details ); 306 307 $details = array_merge( $current_details, $details ); 308 $details['last_updated'] = current_time( 'mysql', true ); 309 310 $update_details = array(); 311 $fields = array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' ); 312 foreach ( array_intersect( array_keys( $details ), $fields ) as $field ) { 313 if ( 'path' === $field ) { 314 $details[ $field ] = trailingslashit( '/' . trim( $details[ $field ], '/' ) ); 315 } 316 317 $update_details[ $field ] = $details[ $field ]; 318 } 319 320 $result = $wpdb->update( $wpdb->blogs, $update_details, array( 'blog_id' => $blog_id ) ); 300 $site = wp_update_site( $blog_id, $details ); 321 301 322 if ( false === $result) {302 if ( is_wp_error( $site ) ) { 323 303 return false; 324 304 } 325 305 326 // If spam status changed, issue actions.327 if ( $details['spam'] != $current_details['spam'] ) {328 if ( $details['spam'] == 1 ) {329 /**330 * Fires when the 'spam' status is added to a blog.331 *332 * @since MU (3.0.0)333 *334 * @param int $blog_id Blog ID.335 */336 do_action( 'make_spam_blog', $blog_id );337 } else {338 /**339 * Fires when the 'spam' status is removed from a blog.340 *341 * @since MU (3.0.0)342 *343 * @param int $blog_id Blog ID.344 */345 do_action( 'make_ham_blog', $blog_id );346 }347 }348 349 // If mature status changed, issue actions.350 if ( $details['mature'] != $current_details['mature'] ) {351 if ( $details['mature'] == 1 ) {352 /**353 * Fires when the 'mature' status is added to a blog.354 *355 * @since 3.1.0356 *357 * @param int $blog_id Blog ID.358 */359 do_action( 'mature_blog', $blog_id );360 } else {361 /**362 * Fires when the 'mature' status is removed from a blog.363 *364 * @since 3.1.0365 *366 * @param int $blog_id Blog ID.367 */368 do_action( 'unmature_blog', $blog_id );369 }370 }371 372 // If archived status changed, issue actions.373 if ( $details['archived'] != $current_details['archived'] ) {374 if ( $details['archived'] == 1 ) {375 /**376 * Fires when the 'archived' status is added to a blog.377 *378 * @since MU (3.0.0)379 *380 * @param int $blog_id Blog ID.381 */382 do_action( 'archive_blog', $blog_id );383 } else {384 /**385 * Fires when the 'archived' status is removed from a blog.386 *387 * @since MU (3.0.0)388 *389 * @param int $blog_id Blog ID.390 */391 do_action( 'unarchive_blog', $blog_id );392 }393 }394 395 // If deleted status changed, issue actions.396 if ( $details['deleted'] != $current_details['deleted'] ) {397 if ( $details['deleted'] == 1 ) {398 /**399 * Fires when the 'deleted' status is added to a blog.400 *401 * @since 3.5.0402 *403 * @param int $blog_id Blog ID.404 */405 do_action( 'make_delete_blog', $blog_id );406 } else {407 /**408 * Fires when the 'deleted' status is removed from a blog.409 *410 * @since 3.5.0411 *412 * @param int $blog_id Blog ID.413 */414 do_action( 'make_undelete_blog', $blog_id );415 }416 }417 418 if ( isset( $details['public'] ) ) {419 switch_to_blog( $blog_id );420 update_option( 'blog_public', $details['public'] );421 restore_current_blog();422 }423 424 clean_blog_cache( $blog_id );425 426 306 return true; 427 307 } 428 308 … … 517 397 } 518 398 519 399 /** 400 * Inserts a new site into the database. 401 * 402 * @since 5.0.0 403 * 404 * @global wpdb $wpdb WordPress database abstraction object. 405 * 406 * @param array $data { 407 * Data for the new site that should be inserted. 408 * 409 * @type string $domain Site domain. Must always be provided. 410 * @type string $path Site path. Default '/'. 411 * @type int $site_id The site's network ID. Default is the current network ID. 412 * @type string $registered When the site was registered, in SQL datetime format. Default is 413 * the current time. 414 * @type string $last_updated When the site was last updated, in SQL datetime format. Default is 415 * the value of $registered. 416 * @type int $public Whether the site is public. Default 1. 417 * @type int $archived Whether the site is archived. Default 0. 418 * @type int $mature Whether the site is mature. Default 0. 419 * @type int $spam Whether the site is spam. Default 0. 420 * @type int $deleted Whether the site is deleted. Default 0. 421 * @type int $lang_id The site's language ID. Currently unused. Default 0. 422 * } 423 * @return int|WP_Error The new site's ID on success, or error object on failure. 424 */ 425 function wp_insert_site( $data ) { 426 global $wpdb; 427 428 $now = current_time( 'mysql' ); 429 430 $defaults = array( 431 'domain' => '', 432 'path' => '', 433 'site_id' => 0, 434 'registered' => $now, 435 'last_updated' => $now, 436 'public' => 1, 437 'archived' => 0, 438 'mature' => 0, 439 'spam' => 0, 440 'deleted' => 0, 441 'lang_id' => 0, 442 ); 443 444 $compat_keys = array( 445 'network_id' => 'site_id', 446 ); 447 448 foreach ( $compat_keys as $compat_key => $original_compat_key ) { 449 if ( ! empty( $data[ $compat_key ] ) && empty( $data[ $original_compat_key ] ) ) { 450 $data[ $original_compat_key ] = $data[ $compat_key ]; 451 } 452 } 453 454 $data = array_intersect_key( wp_parse_args( $data, $defaults ), $defaults ); 455 456 $data['domain'] = trim( $data['domain'] ); 457 458 // A domain must always be present. 459 if ( empty( $data['domain'] ) ) { 460 return new WP_Error( 'site_empty_domain', __( 'Site domain must not be empty.' ) ); 461 } 462 463 $data['path'] = trailingslashit( '/' . trim( $data['path'], '/' ) ); 464 465 // Use the current network if none is set. 466 if ( empty( $data['site_id'] ) ) { 467 $data['site_id'] = get_current_network_id(); 468 } 469 470 if ( false === $wpdb->insert( $wpdb->blogs, $data ) ) { 471 return new WP_Error( 'db_insert_error', __( 'Could not insert site into the database.' ), $wpdb->last_error ); 472 } 473 474 $new_site = get_site( $wpdb->insert_id ); 475 476 clean_blog_cache( $new_site ); 477 478 /** 479 * Fires once a site has been inserted into the database. 480 * 481 * @since 5.0.0 482 * 483 * @param WP_Site $new_site New site object. 484 */ 485 do_action( 'wp_insert_site', $new_site ); 486 487 return (int) $new_site->id; 488 } 489 490 /** 491 * Updates a site in the database. 492 * 493 * @since 5.0.0 494 * 495 * @global wpdb $wpdb WordPress database abstraction object. 496 * 497 * @param int $site_id ID of the site that should be updated. 498 * @param array $data Site data to update. See wp_insert_site() for the list of supported keys. 499 * @return int|WP_Error The updated site's ID on success, or error object on failure. 500 */ 501 function wp_update_site( $site_id, $data ) { 502 global $wpdb; 503 504 $old_site = get_site( $site_id ); 505 if ( ! $old_site ) { 506 return new WP_Error( 'site_not_exist', __( 'Site does not exist.' ) ); 507 } 508 509 $defaults = $old_site->to_array(); 510 $defaults['last_updated'] = current_time( 'mysql' ); 511 unset( $defaults['blog_id'] ); 512 513 $compat_keys = array( 514 'network_id' => 'site_id', 515 ); 516 517 foreach ( $compat_keys as $compat_key => $original_compat_key ) { 518 if ( ! empty( $data[ $compat_key ] ) && empty( $data[ $original_compat_key ] ) ) { 519 $data[ $original_compat_key ] = $data[ $compat_key ]; 520 } 521 } 522 523 $whitelist = array( 'domain', 'path', 'site_id', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' ); 524 $data = array_intersect_key( wp_parse_args( $data, $defaults ), array_flip( $whitelist ) ); 525 526 $data['domain'] = trim( $data['domain'] ); 527 528 // A domain must always be present. 529 if ( empty( $data['domain'] ) ) { 530 return new WP_Error( 'site_empty_domain', __( 'Site domain must not be empty.' ) ); 531 } 532 533 $data['path'] = trailingslashit( '/' . trim( $data['path'], '/' ) ); 534 535 // Use the previously set network if a falsy network ID has been passed. 536 if ( empty( $data['site_id'] ) ) { 537 $data['site_id'] = $defaults['site_id']; 538 } 539 540 if ( false === $wpdb->update( $wpdb->blogs, $data, array( 'blog_id' => $old_site->id ) ) ) { 541 return new WP_Error( 'db_update_error', __( 'Could not update site in the database.' ), $wpdb->last_error ); 542 } 543 544 clean_blog_cache( $old_site ); 545 546 $new_site = get_site( $old_site->id ); 547 548 /** 549 * Fires once a site has been updated in the database. 550 * 551 * @since 5.0.0 552 * 553 * @param WP_Site $new_site New site object. 554 * @param WP_Site $old_site Old site object. 555 */ 556 do_action( 'wp_update_site', $new_site, $old_site ); 557 558 return (int) $new_site->id; 559 } 560 561 /** 562 * Deletes a site from the database. 563 * 564 * @since 5.0.0 565 * 566 * @global wpdb $wpdb WordPress database abstraction object. 567 * 568 * @param int $site_id ID of the site that should be deleted. 569 * @return WP_Site|WP_Error The deleted site object on success, or error object on failure. 570 */ 571 function wp_delete_site( $site_id ) { 572 global $wpdb; 573 574 $old_site = get_site( $site_id ); 575 if ( ! $old_site ) { 576 return new WP_Error( 'site_not_exist', __( 'Site does not exist.' ) ); 577 } 578 579 if ( false === $wpdb->delete( $wpdb->blogs, array( 'blog_id' => $old_site->id ) ) ) { 580 return new WP_Error( 'db_delete_error', __( 'Could not delete site from the database.' ), $wpdb->last_error ); 581 } 582 583 clean_blog_cache( $old_site ); 584 585 /** 586 * Fires once a site has been deleted from the database. 587 * 588 * @since 5.0.0 589 * 590 * @param WP_Site $old_site Deleted site object. 591 */ 592 do_action( 'wp_delete_site', $old_site ); 593 594 return $old_site; 595 } 596 597 /** 520 598 * Retrieves site data given a site ID or site object. 521 599 * 522 600 * Site data will be cached and returned after being passed through a filter. … … 1016 1094 * Update a blog details field. 1017 1095 * 1018 1096 * @since MU (3.0.0) 1097 * @since 5.0.0 Use wp_update_site() internally. 1019 1098 * 1020 1099 * @global wpdb $wpdb WordPress database abstraction object. 1021 1100 * … … 1036 1115 return $value; 1037 1116 } 1038 1117 1039 $result = $wpdb->update( 1040 $wpdb->blogs, array( 1041 $pref => $value, 1042 'last_updated' => current_time( 'mysql', true ), 1043 ), array( 'blog_id' => $blog_id ) 1044 ); 1118 $result = wp_update_site( $blog_id, array( 1119 $pref => $value, 1120 ) ); 1045 1121 1046 if ( false === $result) {1122 if ( is_wp_error( $result ) ) { 1047 1123 return false; 1048 1124 } 1049 1125 1050 clean_blog_cache( $blog_id );1051 1052 if ( 'spam' == $pref ) {1053 if ( $value == 1 ) {1054 /** This filter is documented in wp-includes/ms-blogs.php */1055 do_action( 'make_spam_blog', $blog_id );1056 } else {1057 /** This filter is documented in wp-includes/ms-blogs.php */1058 do_action( 'make_ham_blog', $blog_id );1059 }1060 } elseif ( 'mature' == $pref ) {1061 if ( $value == 1 ) {1062 /** This filter is documented in wp-includes/ms-blogs.php */1063 do_action( 'mature_blog', $blog_id );1064 } else {1065 /** This filter is documented in wp-includes/ms-blogs.php */1066 do_action( 'unmature_blog', $blog_id );1067 }1068 } elseif ( 'archived' == $pref ) {1069 if ( $value == 1 ) {1070 /** This filter is documented in wp-includes/ms-blogs.php */1071 do_action( 'archive_blog', $blog_id );1072 } else {1073 /** This filter is documented in wp-includes/ms-blogs.php */1074 do_action( 'unarchive_blog', $blog_id );1075 }1076 } elseif ( 'deleted' == $pref ) {1077 if ( $value == 1 ) {1078 /** This filter is documented in wp-includes/ms-blogs.php */1079 do_action( 'make_delete_blog', $blog_id );1080 } else {1081 /** This filter is documented in wp-includes/ms-blogs.php */1082 do_action( 'make_undelete_blog', $blog_id );1083 }1084 } elseif ( 'public' == $pref ) {1085 /**1086 * Fires after the current blog's 'public' setting is updated.1087 *1088 * @since MU (3.0.0)1089 *1090 * @param int $blog_id Blog ID.1091 * @param string $value The value of blog status.1092 */1093 do_action( 'update_blog_public', $blog_id, $value ); // Moved here from update_blog_public().1094 }1095 1096 1126 return $value; 1097 1127 } 1098 1128 … … 1360 1390 1361 1391 update_posts_count(); 1362 1392 } 1393 1394 /** 1395 * Updates the count of sites for a network based on a changed site. 1396 * 1397 * @since 5.0.0 1398 * 1399 * @param WP_Site $new_site The site object that has been inserted, updated or deleted. 1400 * @param WP_Site|null $old_site Optional. If $new_site has been updated, this must be the previous 1401 * state of that site. Default null. 1402 */ 1403 function wp_maybe_update_network_site_counts_on_update( $new_site, $old_site = null ) { 1404 if ( null === $old_site ) { 1405 wp_maybe_update_network_site_counts( $new_site->network_id ); 1406 return; 1407 } 1408 1409 if ( $new_site->network_id != $old_site->network_id ) { 1410 wp_maybe_update_network_site_counts( $new_site->network_id ); 1411 wp_maybe_update_network_site_counts( $old_site->network_id ); 1412 } 1413 } 1414 1415 /** 1416 * Triggers actions on site status updates. 1417 * 1418 * @since 5.0.0 1419 * 1420 * @param WP_Site $new_site The site object after the update. 1421 * @param WP_Site $old_site The site obejct prior to the update. 1422 */ 1423 function wp_maybe_transition_site_statuses_on_update( $new_site, $old_site ) { 1424 $site_id = $new_site->id; 1425 1426 if ( $new_site->spam != $old_site->spam ) { 1427 if ( $new_site->spam == 1 ) { 1428 1429 /** 1430 * Fires when the 'spam' status is added to a site. 1431 * 1432 * @since MU (3.0.0) 1433 * 1434 * @param int $site_id Site ID. 1435 */ 1436 do_action( 'make_spam_blog', $site_id ); 1437 } else { 1438 1439 /** 1440 * Fires when the 'spam' status is removed from a site. 1441 * 1442 * @since MU (3.0.0) 1443 * 1444 * @param int $site_id Site ID. 1445 */ 1446 do_action( 'make_ham_blog', $site_id ); 1447 } 1448 } 1449 1450 if ( $new_site->mature != $old_site->mature ) { 1451 if ( $new_site->mature == 1 ) { 1452 1453 /** 1454 * Fires when the 'mature' status is added to a site. 1455 * 1456 * @since 3.1.0 1457 * 1458 * @param int $site_id Site ID. 1459 */ 1460 do_action( 'mature_blog', $site_id ); 1461 } else { 1462 1463 /** 1464 * Fires when the 'mature' status is removed from a site. 1465 * 1466 * @since 3.1.0 1467 * 1468 * @param int $site_id Site ID. 1469 */ 1470 do_action( 'unmature_blog', $site_id ); 1471 } 1472 } 1473 1474 if ( $new_site->archived != $old_site->archived ) { 1475 if ( $new_site->archived == 1 ) { 1476 1477 /** 1478 * Fires when the 'archived' status is added to a site. 1479 * 1480 * @since MU (3.0.0) 1481 * 1482 * @param int $site_id Site ID. 1483 */ 1484 do_action( 'archive_blog', $site_id ); 1485 } else { 1486 1487 /** 1488 * Fires when the 'archived' status is removed from a site. 1489 * 1490 * @since MU (3.0.0) 1491 * 1492 * @param int $site_id Site ID. 1493 */ 1494 do_action( 'unarchive_blog', $site_id ); 1495 } 1496 } 1497 1498 if ( $new_site->deleted != $old_site->deleted ) { 1499 if ( $new_site->deleted == 1 ) { 1500 1501 /** 1502 * Fires when the 'deleted' status is added to a site. 1503 * 1504 * @since 3.5.0 1505 * 1506 * @param int $site_id Site ID. 1507 */ 1508 do_action( 'make_delete_blog', $site_id ); 1509 } else { 1510 1511 /** 1512 * Fires when the 'deleted' status is removed from a site. 1513 * 1514 * @since 3.5.0 1515 * 1516 * @param int $site_id Site ID. 1517 */ 1518 do_action( 'make_undelete_blog', $site_id ); 1519 } 1520 } 1521 1522 if ( $new_site->public != $old_site->public ) { 1523 1524 /** 1525 * Fires after the current blog's 'public' setting is updated. 1526 * 1527 * @since MU (3.0.0) 1528 * 1529 * @param int $site_id Site ID. 1530 * @param string $value The value of the site status. 1531 */ 1532 do_action( 'update_blog_public', $site_id, $new_site->public ); 1533 } 1534 } 1535 1536 /** 1537 * Cleans the necessary caches after specific site data has been updated. 1538 * 1539 * @since 5.0.0 1540 * 1541 * @param WP_Site $new_site The site object after the update. 1542 * @param WP_Site $old_site The site obejct prior to the update. 1543 */ 1544 function wp_maybe_clean_old_site_cache_on_update( $new_site, $old_site ) { 1545 if ( $old_site->domain !== $new_site->domain || $old_site->path !== $new_site->path ) { 1546 clean_blog_cache( $new_site ); 1547 } 1548 } 1549 1550 /** 1551 * Updates the `blog_public` option for a given site ID. 1552 * 1553 * @since 5.0.0 1554 * 1555 * @param int $site_id Site ID. 1556 * @param string $public The value of the site status. 1557 */ 1558 function wp_update_blog_public_option_on_site_update( $site_id, $public ) { 1559 update_blog_option( $site_id, 'blog_public', $public ); 1560 } -
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_insert_site', 'wp_maybe_update_network_site_counts_on_update', 10, 1 ); 45 add_action( 'wp_update_site', 'wp_maybe_update_network_site_counts_on_update', 10, 2 ); 46 add_action( 'wp_delete_site', 'wp_maybe_update_network_site_counts_on_update', 10, 1 ); 47 add_action( 'wp_update_site', 'wp_maybe_transition_site_statuses_on_update', 10, 2 ); 48 add_action( 'wp_update_site', 'wp_maybe_clean_old_site_cache_on_update', 10, 2 ); 49 add_action( 'update_blog_public', 'wp_update_blog_public_option_on_site_update', 1, 2 ); 44 50 45 51 // Register Nonce 46 52 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
1294 1294 wp_installing( true ); 1295 1295 } 1296 1296 1297 if ( ! $blog_id = insert_blog( $domain, $path, $network_id ) ) { 1297 $blog_id = wp_insert_site( array( 1298 'domain' => $domain, 1299 'path' => $path, 1300 'network_id' => $network_id, 1301 ) ); 1302 1303 if ( is_wp_error( $blog_id ) ) { 1298 1304 return new WP_Error( 'insert_blog', __( 'Could not create site.' ) ); 1299 1305 } 1300 1306 … … 1485 1491 } 1486 1492 1487 1493 /** 1488 * Store basic site info in the blogs table.1489 *1490 * This function creates a row in the wp_blogs table and returns1491 * the new blog's ID. It is the first step in creating a new blog.1492 *1493 * @since MU (3.0.0)1494 *1495 * @global wpdb $wpdb WordPress database abstraction object.1496 *1497 * @param string $domain The domain of the new site.1498 * @param string $path The path of the new site.1499 * @param int $network_id Unless you're running a multi-network installation, be sure to set this value to 1.1500 * @return int|false The ID of the new row1501 */1502 function insert_blog( $domain, $path, $network_id ) {1503 global $wpdb;1504 1505 $path = trailingslashit( $path );1506 $network_id = (int) $network_id;1507 1508 $result = $wpdb->insert(1509 $wpdb->blogs, array(1510 'site_id' => $network_id,1511 'domain' => $domain,1512 'path' => $path,1513 'registered' => current_time( 'mysql' ),1514 )1515 );1516 if ( ! $result ) {1517 return false;1518 }1519 1520 $blog_id = $wpdb->insert_id;1521 clean_blog_cache( $blog_id );1522 1523 wp_maybe_update_network_site_counts( $network_id );1524 1525 return $blog_id;1526 }1527 1528 /**1529 1494 * Install an empty blog. 1530 1495 * 1531 1496 * Creates the new blog tables and options. If calling this function … … 1537 1502 * @global wpdb $wpdb 1538 1503 * @global WP_Roles $wp_roles 1539 1504 * 1540 * @param int $blog_id The value returned by insert_blog().1505 * @param int $blog_id The value returned by wp_insert_site(). 1541 1506 * @param string $blog_title The title of the new site. 1542 1507 */ 1543 1508 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;