Ticket #40364: 40364.6.diff
| File 40364.6.diff, 32.0 KB (added by , 8 years ago) |
|---|
-
src/wp-admin/includes/ms.php
53 53 * Delete a site. 54 54 * 55 55 * @since 3.0.0 56 * @since 5.0.0 Use wp_delete_site() internally to delete the site row from the database. 56 57 * 57 58 * @global wpdb $wpdb WordPress database abstraction object. 58 59 * … … 127 128 $wpdb->query( "DROP TABLE IF EXISTS `$table`" ); 128 129 } 129 130 130 $wpdb->delete( $wpdb->blogs, array( 'blog_id' => $blog_id ));131 wp_delete_site( $blog_id ); 131 132 132 133 /** 133 134 * Filters the upload base directory to delete when the site is deleted. -
src/wp-includes/ms-blogs.php
179 179 * Update the details for a blog. Updates the blogs table for a given blog id. 180 180 * 181 181 * @since MU (3.0.0) 182 * 183 * @global wpdb $wpdb WordPress database abstraction object. 182 * @since 5.0.0 Use wp_update_site() internally. 184 183 * 185 184 * @param int $blog_id Blog ID 186 185 * @param array $details Array of details keyed by blogs table field names. 187 186 * @return bool True if update succeeds, false otherwise. 188 187 */ 189 188 function update_blog_details( $blog_id, $details = array() ) { 190 global $wpdb;191 192 189 if ( empty($details) ) 193 190 return false; 194 191 195 192 if ( is_object($details) ) 196 193 $details = get_object_vars($details); 197 194 198 $ current_details = get_site( $blog_id);199 if ( empty($current_details) )195 $site = wp_update_site( $blog_id, $details ); 196 if ( is_wp_error( $site ) ) { 200 197 return false; 201 202 $current_details = get_object_vars($current_details);203 204 $details = array_merge($current_details, $details);205 $details['last_updated'] = current_time('mysql', true);206 207 $update_details = array();208 $fields = array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id');209 foreach ( array_intersect( array_keys( $details ), $fields ) as $field ) {210 if ( 'path' === $field ) {211 $details[ $field ] = trailingslashit( '/' . trim( $details[ $field ], '/' ) );212 }213 214 $update_details[ $field ] = $details[ $field ];215 }216 217 $result = $wpdb->update( $wpdb->blogs, $update_details, array('blog_id' => $blog_id) );218 219 if ( false === $result )220 return false;221 222 // If spam status changed, issue actions.223 if ( $details['spam'] != $current_details['spam'] ) {224 if ( $details['spam'] == 1 ) {225 /**226 * Fires when the 'spam' status is added to a blog.227 *228 * @since MU (3.0.0)229 *230 * @param int $blog_id Blog ID.231 */232 do_action( 'make_spam_blog', $blog_id );233 } else {234 /**235 * Fires when the 'spam' status is removed from a blog.236 *237 * @since MU (3.0.0)238 *239 * @param int $blog_id Blog ID.240 */241 do_action( 'make_ham_blog', $blog_id );242 }243 }244 245 // If mature status changed, issue actions.246 if ( $details['mature'] != $current_details['mature'] ) {247 if ( $details['mature'] == 1 ) {248 /**249 * Fires when the 'mature' status is added to a blog.250 *251 * @since 3.1.0252 *253 * @param int $blog_id Blog ID.254 */255 do_action( 'mature_blog', $blog_id );256 } else {257 /**258 * Fires when the 'mature' status is removed from a blog.259 *260 * @since 3.1.0261 *262 * @param int $blog_id Blog ID.263 */264 do_action( 'unmature_blog', $blog_id );265 }266 }267 268 // If archived status changed, issue actions.269 if ( $details['archived'] != $current_details['archived'] ) {270 if ( $details['archived'] == 1 ) {271 /**272 * Fires when the 'archived' status is added to a blog.273 *274 * @since MU (3.0.0)275 *276 * @param int $blog_id Blog ID.277 */278 do_action( 'archive_blog', $blog_id );279 } else {280 /**281 * Fires when the 'archived' status is removed from a blog.282 *283 * @since MU (3.0.0)284 *285 * @param int $blog_id Blog ID.286 */287 do_action( 'unarchive_blog', $blog_id );288 }289 }290 291 // If deleted status changed, issue actions.292 if ( $details['deleted'] != $current_details['deleted'] ) {293 if ( $details['deleted'] == 1 ) {294 /**295 * Fires when the 'deleted' status is added to a blog.296 *297 * @since 3.5.0298 *299 * @param int $blog_id Blog ID.300 */301 do_action( 'make_delete_blog', $blog_id );302 } else {303 /**304 * Fires when the 'deleted' status is removed from a blog.305 *306 * @since 3.5.0307 *308 * @param int $blog_id Blog ID.309 */310 do_action( 'make_undelete_blog', $blog_id );311 }312 198 } 313 199 314 if ( isset( $details['public'] ) ) {315 switch_to_blog( $blog_id );316 update_option( 'blog_public', $details['public'] );317 restore_current_blog();318 }319 320 clean_blog_cache( $blog_id );321 322 200 return true; 323 201 } 324 202 … … 411 289 } 412 290 413 291 /** 292 * Inserts a new site into the database. 293 * 294 * @since 5.0.0 295 * 296 * @global wpdb $wpdb WordPress database abstraction object. 297 * 298 * @param array $data { 299 * Data for the new site that should be inserted. 300 * 301 * @type string $domain Site domain. Must always be provided. 302 * @type string $path Site path. Default '/'. 303 * @type int $site_id The site's network ID. Default is the current network ID. 304 * @type string $registered When the site was registered, in SQL datetime format. Default is 305 * the current time. 306 * @type string $last_updated When the site was last updated, in SQL datetime format. Default is 307 * the value of $registered. 308 * @type int $public Whether the site is public. Default 1. 309 * @type int $archived Whether the site is archived. Default 0. 310 * @type int $mature Whether the site is mature. Default 0. 311 * @type int $spam Whether the site is spam. Default 0. 312 * @type int $deleted Whether the site is deleted. Default 0. 313 * @type int $lang_id The site's language ID. Currently unused. Default 0. 314 * } 315 * @return int|WP_Error The new site's ID on success, or error object on failure. 316 */ 317 function wp_insert_site( $data ) { 318 global $wpdb; 319 320 $now = current_time( 'mysql' ); 321 322 $defaults = array( 323 'domain' => '', 324 'path' => '', 325 'site_id' => 0, 326 'registered' => $now, 327 'last_updated' => $now, 328 'public' => 1, 329 'archived' => 0, 330 'mature' => 0, 331 'spam' => 0, 332 'deleted' => 0, 333 'lang_id' => 0, 334 ); 335 336 $compat_keys = array( 337 'network_id' => 'site_id', 338 ); 339 340 foreach ( $compat_keys as $compat_key => $original_compat_key ) { 341 if ( ! empty( $data[ $compat_key ] ) && empty( $data[ $original_compat_key ] ) ) { 342 $data[ $original_compat_key ] = $data[ $compat_key ]; 343 } 344 } 345 346 $data = array_intersect_key( wp_parse_args( $data, $defaults ), $defaults ); 347 348 $data['domain'] = trim( $data['domain'] ); 349 350 // A domain must always be present. 351 if ( empty( $data['domain'] ) ) { 352 return new WP_Error( 'site_empty_domain', __( 'Site domain must not be empty.' ) ); 353 } 354 355 $data['path'] = trailingslashit( '/' . trim( $data['path'], '/' ) ); 356 357 // Use the current network if none is set. 358 if ( empty( $data['site_id'] ) ) { 359 $data['site_id'] = get_current_network_id(); 360 } 361 362 if ( false === $wpdb->insert( $wpdb->blogs, $data ) ) { 363 return new WP_Error( 'db_insert_error', __( 'Could not insert site into the database.' ), $wpdb->last_error ); 364 } 365 366 $new_site = get_site( $wpdb->insert_id ); 367 368 clean_blog_cache( $new_site ); 369 370 /** 371 * Fires once a site has been inserted into the database. 372 * 373 * @since 5.0.0 374 * 375 * @param WP_Site $new_site New site object. 376 */ 377 do_action( 'wp_insert_site', $new_site ); 378 379 return (int) $new_site->id; 380 } 381 382 /** 383 * Updates a site in the database. 384 * 385 * @since 5.0.0 386 * 387 * @global wpdb $wpdb WordPress database abstraction object. 388 * 389 * @param int $site_id ID of the site that should be updated. 390 * @param array $data Site data to update. See wp_insert_site() for the list of supported keys. 391 * @return int|WP_Error The updated site's ID on success, or error object on failure. 392 */ 393 function wp_update_site( $site_id, $data ) { 394 global $wpdb; 395 396 $old_site = get_site( $site_id ); 397 if ( ! $old_site ) { 398 return new WP_Error( 'site_not_exist', __( 'Site does not exist.' ) ); 399 } 400 401 $defaults = $old_site->to_array(); 402 $defaults['last_updated'] = current_time( 'mysql' ); 403 unset( $defaults['blog_id'] ); 404 405 $compat_keys = array( 406 'network_id' => 'site_id', 407 ); 408 409 foreach ( $compat_keys as $compat_key => $original_compat_key ) { 410 if ( ! empty( $data[ $compat_key ] ) && empty( $data[ $original_compat_key ] ) ) { 411 $data[ $original_compat_key ] = $data[ $compat_key ]; 412 } 413 } 414 415 $whitelist = array( 'domain', 'path', 'site_id', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' ); 416 $data = array_intersect_key( wp_parse_args( $data, $defaults ), array_flip( $whitelist ) ); 417 418 $data['domain'] = trim( $data['domain'] ); 419 420 // A domain must always be present. 421 if ( empty( $data['domain'] ) ) { 422 return new WP_Error( 'site_empty_domain', __( 'Site domain must not be empty.' ) ); 423 } 424 425 $data['path'] = trailingslashit( '/' . trim( $data['path'], '/' ) ); 426 427 // Use the previously set network if a falsy network ID has been passed. 428 if ( empty( $data['site_id'] ) ) { 429 $data['site_id'] = $defaults['site_id']; 430 } 431 432 if ( false === $wpdb->update( $wpdb->blogs, $data, array( 'blog_id' => $old_site->id ) ) ) { 433 return new WP_Error( 'db_update_error', __( 'Could not update site in the database.' ), $wpdb->last_error ); 434 } 435 436 clean_blog_cache( $old_site ); 437 438 $new_site = get_site( $old_site->id ); 439 440 /** 441 * Fires once a site has been updated in the database. 442 * 443 * @since 5.0.0 444 * 445 * @param WP_Site $new_site New site object. 446 * @param WP_Site $old_site Old site object. 447 */ 448 do_action( 'wp_update_site', $new_site, $old_site ); 449 450 return (int) $new_site->id; 451 } 452 453 /** 454 * Deletes a site from the database. 455 * 456 * @since 5.0.0 457 * 458 * @global wpdb $wpdb WordPress database abstraction object. 459 * 460 * @param int $site_id ID of the site that should be deleted. 461 * @return WP_Site|WP_Error The deleted site object on success, or error object on failure. 462 */ 463 function wp_delete_site( $site_id ) { 464 global $wpdb; 465 466 $old_site = get_site( $site_id ); 467 if ( ! $old_site ) { 468 return new WP_Error( 'site_not_exist', __( 'Site does not exist.' ) ); 469 } 470 471 if ( false === $wpdb->delete( $wpdb->blogs, array( 'blog_id' => $old_site->id ) ) ) { 472 return new WP_Error( 'db_delete_error', __( 'Could not delete site from the database.' ), $wpdb->last_error ); 473 } 474 475 clean_blog_cache( $old_site ); 476 477 /** 478 * Fires once a site has been deleted from the database. 479 * 480 * @since 5.0.0 481 * 482 * @param WP_Site $old_site Deleted site object. 483 */ 484 do_action( 'wp_delete_site', $old_site ); 485 486 return $old_site; 487 } 488 489 /** 414 490 * Retrieves site data given a site ID or site object. 415 491 * 416 492 * Site data will be cached and returned after being passed through a filter. … … 1013 1089 * Update a blog details field. 1014 1090 * 1015 1091 * @since MU (3.0.0) 1092 * @since 5.0.0 Use wp_update_site() internally. 1016 1093 * 1017 1094 * @global wpdb $wpdb WordPress database abstraction object. 1018 1095 * … … 1031 1108 if ( ! in_array( $pref, array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id') ) ) 1032 1109 return $value; 1033 1110 1034 $result = $wpdb->update( $wpdb->blogs, array($pref => $value, 'last_updated' => current_time('mysql', true)), array('blog_id' => $blog_id) ); 1035 1036 if ( false === $result ) 1111 $result = wp_update_site( $blog_id, array( $pref => $value ) ); 1112 if ( is_wp_error( $result ) ) { 1037 1113 return false; 1038 1039 clean_blog_cache( $blog_id );1040 1041 if ( 'spam' == $pref ) {1042 if ( $value == 1 ) {1043 /** This filter is documented in wp-includes/ms-blogs.php */1044 do_action( 'make_spam_blog', $blog_id );1045 } else {1046 /** This filter is documented in wp-includes/ms-blogs.php */1047 do_action( 'make_ham_blog', $blog_id );1048 }1049 } elseif ( 'mature' == $pref ) {1050 if ( $value == 1 ) {1051 /** This filter is documented in wp-includes/ms-blogs.php */1052 do_action( 'mature_blog', $blog_id );1053 } else {1054 /** This filter is documented in wp-includes/ms-blogs.php */1055 do_action( 'unmature_blog', $blog_id );1056 }1057 } elseif ( 'archived' == $pref ) {1058 if ( $value == 1 ) {1059 /** This filter is documented in wp-includes/ms-blogs.php */1060 do_action( 'archive_blog', $blog_id );1061 } else {1062 /** This filter is documented in wp-includes/ms-blogs.php */1063 do_action( 'unarchive_blog', $blog_id );1064 }1065 } elseif ( 'deleted' == $pref ) {1066 if ( $value == 1 ) {1067 /** This filter is documented in wp-includes/ms-blogs.php */1068 do_action( 'make_delete_blog', $blog_id );1069 } else {1070 /** This filter is documented in wp-includes/ms-blogs.php */1071 do_action( 'make_undelete_blog', $blog_id );1072 }1073 } elseif ( 'public' == $pref ) {1074 /**1075 * Fires after the current blog's 'public' setting is updated.1076 *1077 * @since MU (3.0.0)1078 *1079 * @param int $blog_id Blog ID.1080 * @param string $value The value of blog status.1081 */1082 do_action( 'update_blog_public', $blog_id, $value ); // Moved here from update_blog_public().1083 1114 } 1084 1115 1085 1116 return $value; … … 1346 1377 1347 1378 update_posts_count(); 1348 1379 } 1380 1381 /** 1382 * Updates the count of sites for a network based on a changed site. 1383 * 1384 * @since 5.0.0 1385 * 1386 * @param WP_Site $new_site The site object that has been inserted, updated or deleted. 1387 * @param WP_Site|null $old_site Optional. If $new_site has been updated, this must be the previous 1388 * state of that site. Default null. 1389 */ 1390 function wp_maybe_update_network_site_counts_on_update( $new_site, $old_site = null ) { 1391 if ( null === $old_site ) { 1392 wp_maybe_update_network_site_counts( $new_site->network_id ); 1393 return; 1394 } 1395 1396 if ( $new_site->network_id != $old_site->network_id ) { 1397 wp_maybe_update_network_site_counts( $new_site->network_id ); 1398 wp_maybe_update_network_site_counts( $old_site->network_id ); 1399 } 1400 } 1401 1402 /** 1403 * Triggers actions on site status updates. 1404 * 1405 * @since 5.0.0 1406 * 1407 * @param WP_Site $new_site The site object after the update. 1408 * @param WP_Site $old_site The site obejct prior to the update. 1409 */ 1410 function wp_maybe_transition_site_statuses_on_update( $new_site, $old_site ) { 1411 $site_id = $new_site->id; 1412 1413 if ( $new_site->spam != $old_site->spam ) { 1414 if ( $new_site->spam == 1 ) { 1415 1416 /** 1417 * Fires when the 'spam' status is added to a site. 1418 * 1419 * @since MU (3.0.0) 1420 * 1421 * @param int $site_id Site ID. 1422 */ 1423 do_action( 'make_spam_blog', $site_id ); 1424 } else { 1425 1426 /** 1427 * Fires when the 'spam' status is removed from a site. 1428 * 1429 * @since MU (3.0.0) 1430 * 1431 * @param int $site_id Site ID. 1432 */ 1433 do_action( 'make_ham_blog', $site_id ); 1434 } 1435 } 1436 1437 if ( $new_site->mature != $old_site->mature ) { 1438 if ( $new_site->mature == 1 ) { 1439 1440 /** 1441 * Fires when the 'mature' status is added to a site. 1442 * 1443 * @since 3.1.0 1444 * 1445 * @param int $site_id Site ID. 1446 */ 1447 do_action( 'mature_blog', $site_id ); 1448 } else { 1449 1450 /** 1451 * Fires when the 'mature' status is removed from a site. 1452 * 1453 * @since 3.1.0 1454 * 1455 * @param int $site_id Site ID. 1456 */ 1457 do_action( 'unmature_blog', $site_id ); 1458 } 1459 } 1460 1461 if ( $new_site->archived != $old_site->archived ) { 1462 if ( $new_site->archived == 1 ) { 1463 1464 /** 1465 * Fires when the 'archived' status is added to a site. 1466 * 1467 * @since MU (3.0.0) 1468 * 1469 * @param int $site_id Site ID. 1470 */ 1471 do_action( 'archive_blog', $site_id ); 1472 } else { 1473 1474 /** 1475 * Fires when the 'archived' status is removed from a site. 1476 * 1477 * @since MU (3.0.0) 1478 * 1479 * @param int $site_id Site ID. 1480 */ 1481 do_action( 'unarchive_blog', $site_id ); 1482 } 1483 } 1484 1485 if ( $new_site->deleted != $old_site->deleted ) { 1486 if ( $new_site->deleted == 1 ) { 1487 1488 /** 1489 * Fires when the 'deleted' status is added to a site. 1490 * 1491 * @since 3.5.0 1492 * 1493 * @param int $site_id Site ID. 1494 */ 1495 do_action( 'make_delete_blog', $site_id ); 1496 } else { 1497 1498 /** 1499 * Fires when the 'deleted' status is removed from a site. 1500 * 1501 * @since 3.5.0 1502 * 1503 * @param int $site_id Site ID. 1504 */ 1505 do_action( 'make_undelete_blog', $site_id ); 1506 } 1507 } 1508 1509 if ( $new_site->public != $old_site->public ) { 1510 1511 /** 1512 * Fires after the current blog's 'public' setting is updated. 1513 * 1514 * @since MU (3.0.0) 1515 * 1516 * @param int $site_id Site ID. 1517 * @param string $value The value of the site status. 1518 */ 1519 do_action( 'update_blog_public', $site_id, $new_site->public ); 1520 } 1521 } 1522 1523 /** 1524 * Updates the `blog_public` option for a given site ID. 1525 * 1526 * @since 5.0.0 1527 * 1528 * @param int $site_id Site ID. 1529 * @param string $public The value of the site status. 1530 */ 1531 function wp_update_blog_public_option_on_site_update( $site_id, $public ) { 1532 update_blog_option( $site_id, 'blog_public', $public ); 1533 } -
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( 'update_blog_public', 'wp_update_blog_public_option_on_site_update', 1, 2 ); 44 49 45 50 // Register Nonce 46 51 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
1207 1207 wp_installing( true ); 1208 1208 } 1209 1209 1210 if ( ! $blog_id = insert_blog($domain, $path, $network_id) ) 1210 $blog_id = wp_insert_site( array( 1211 'domain' => $domain, 1212 'path' => $path, 1213 'network_id' => $network_id, 1214 ) ); 1215 1216 if ( is_wp_error( $blog_id ) ) { 1211 1217 return new WP_Error('insert_blog', __('Could not create site.')); 1218 } 1212 1219 1213 1220 switch_to_blog($blog_id); 1214 1221 install_blog($blog_id, $title); … … 1382 1389 } 1383 1390 1384 1391 /** 1385 * Store basic site info in the blogs table.1386 *1387 * This function creates a row in the wp_blogs table and returns1388 * the new blog's ID. It is the first step in creating a new blog.1389 *1390 * @since MU (3.0.0)1391 *1392 * @global wpdb $wpdb WordPress database abstraction object.1393 *1394 * @param string $domain The domain of the new site.1395 * @param string $path The path of the new site.1396 * @param int $network_id Unless you're running a multi-network installation, be sure to set this value to 1.1397 * @return int|false The ID of the new row1398 */1399 function insert_blog($domain, $path, $network_id) {1400 global $wpdb;1401 1402 $path = trailingslashit($path);1403 $network_id = (int) $network_id;1404 1405 $result = $wpdb->insert( $wpdb->blogs, array('site_id' => $network_id, 'domain' => $domain, 'path' => $path, 'registered' => current_time('mysql')) );1406 if ( ! $result )1407 return false;1408 1409 $blog_id = $wpdb->insert_id;1410 clean_blog_cache( $blog_id );1411 1412 wp_maybe_update_network_site_counts( $network_id );1413 1414 return $blog_id;1415 }1416 1417 /**1418 1392 * Install an empty blog. 1419 1393 * 1420 1394 * Creates the new blog tables and options. If calling this function … … 1426 1400 * @global wpdb $wpdb 1427 1401 * @global WP_Roles $wp_roles 1428 1402 * 1429 * @param int $blog_id The value returned by insert_blog().1403 * @param int $blog_id The value returned by wp_insert_site(). 1430 1404 * @param string $blog_title The title of the new site. 1431 1405 */ 1432 1406 function install_blog( $blog_id, $blog_title = '' ) { -
tests/phpunit/tests/multisite/site.php
433 433 $this->assertEquals( '0', $blog->spam ); 434 434 $this->assertEquals( 1, $test_action_counter ); 435 435 436 // The action should fire if the status of 'spam' stays the same.436 // The action should not fire if the status of 'spam' stays the same. 437 437 update_blog_status( $blog_id, 'spam', 0 ); 438 438 $blog = get_site( $blog_id ); 439 439 440 440 $this->assertEquals( '0', $blog->spam ); 441 $this->assertEquals( 2, $test_action_counter );441 $this->assertEquals( 1, $test_action_counter ); 442 442 443 443 remove_action( 'make_ham_blog', array( $this, '_action_counter_cb' ), 10 ); 444 444 } … … 456 456 $this->assertEquals( '1', $blog->spam ); 457 457 $this->assertEquals( 1, $test_action_counter ); 458 458 459 // The action should fire if the status of 'spam' stays the same.459 // The action should not fire if the status of 'spam' stays the same. 460 460 update_blog_status( $blog_id, 'spam', 1 ); 461 461 $blog = get_site( $blog_id ); 462 462 463 463 $this->assertEquals( '1', $blog->spam ); 464 $this->assertEquals( 2, $test_action_counter );464 $this->assertEquals( 1, $test_action_counter ); 465 465 466 466 remove_action( 'make_spam_blog', array( $this, '_action_counter_cb' ), 10 ); 467 467 } … … 479 479 $this->assertEquals( '1', $blog->archived ); 480 480 $this->assertEquals( 1, $test_action_counter ); 481 481 482 // The action should fire if the status of 'archived' stays the same.482 // The action should not fire if the status of 'archived' stays the same. 483 483 update_blog_status( $blog_id, 'archived', 1 ); 484 484 $blog = get_site( $blog_id ); 485 485 486 486 $this->assertEquals( '1', $blog->archived ); 487 $this->assertEquals( 2, $test_action_counter );487 $this->assertEquals( 1, $test_action_counter ); 488 488 489 489 remove_action( 'archive_blog', array( $this, '_action_counter_cb' ), 10 ); 490 490 } … … 503 503 $this->assertEquals( '0', $blog->archived ); 504 504 $this->assertEquals( 1, $test_action_counter ); 505 505 506 // The action should fire if the status of 'archived' stays the same.506 // The action should not fire if the status of 'archived' stays the same. 507 507 update_blog_status( $blog_id, 'archived', 0 ); 508 508 $blog = get_site( $blog_id ); 509 509 $this->assertEquals( '0', $blog->archived ); 510 $this->assertEquals( 2, $test_action_counter );510 $this->assertEquals( 1, $test_action_counter ); 511 511 512 512 remove_action( 'unarchive_blog', array( $this, '_action_counter_cb' ), 10 ); 513 513 } … … 525 525 $this->assertEquals( '1', $blog->deleted ); 526 526 $this->assertEquals( 1, $test_action_counter ); 527 527 528 // The action should fire if the status of 'deleted' stays the same.528 // The action should not fire if the status of 'deleted' stays the same. 529 529 update_blog_status( $blog_id, 'deleted', 1 ); 530 530 $blog = get_site( $blog_id ); 531 531 532 532 $this->assertEquals( '1', $blog->deleted ); 533 $this->assertEquals( 2, $test_action_counter );533 $this->assertEquals( 1, $test_action_counter ); 534 534 535 535 remove_action( 'make_delete_blog', array( $this, '_action_counter_cb' ), 10 ); 536 536 } … … 549 549 $this->assertEquals( '0', $blog->deleted ); 550 550 $this->assertEquals( 1, $test_action_counter ); 551 551 552 // The action should fire if the status of 'deleted' stays the same.552 // The action should not fire if the status of 'deleted' stays the same. 553 553 update_blog_status( $blog_id, 'deleted', 0 ); 554 554 $blog = get_site( $blog_id ); 555 555 556 556 $this->assertEquals( '0', $blog->deleted ); 557 $this->assertEquals( 2, $test_action_counter );557 $this->assertEquals( 1, $test_action_counter ); 558 558 559 559 remove_action( 'make_undelete_blog', array( $this, '_action_counter_cb' ), 10 ); 560 560 } … … 572 572 $this->assertEquals( '1', $blog->mature ); 573 573 $this->assertEquals( 1, $test_action_counter ); 574 574 575 // The action should fire if the status of 'mature' stays the same.575 // The action should not fire if the status of 'mature' stays the same. 576 576 update_blog_status( $blog_id, 'mature', 1 ); 577 577 $blog = get_site( $blog_id ); 578 578 579 579 $this->assertEquals( '1', $blog->mature ); 580 $this->assertEquals( 2, $test_action_counter );580 $this->assertEquals( 1, $test_action_counter ); 581 581 582 582 remove_action( 'mature_blog', array( $this, '_action_counter_cb' ), 10 ); 583 583 } … … 596 596 $this->assertEquals( '0', $blog->mature ); 597 597 $this->assertEquals( 1, $test_action_counter ); 598 598 599 // The action should fire if the status of 'mature' stays the same.599 // The action should not fire if the status of 'mature' stays the same. 600 600 update_blog_status( $blog_id, 'mature', 0 ); 601 601 $blog = get_site( $blog_id ); 602 602 603 603 $this->assertEquals( '0', $blog->mature ); 604 $this->assertEquals( 2, $test_action_counter );604 $this->assertEquals( 1, $test_action_counter ); 605 605 606 606 remove_action( 'unmature_blog', array( $this, '_action_counter_cb' ), 10 ); 607 607 } … … 611 611 $test_action_counter = 0; 612 612 613 613 $blog_id = self::factory()->blog->create(); 614 update_blog_details( $blog_id, array( 'public' => 1 ) ); 614 615 615 616 add_action( 'update_blog_public', array( $this, '_action_counter_cb' ), 10 ); 616 617 update_blog_status( $blog_id, 'public', 0 ); … … 619 620 $this->assertEquals( '0', $blog->public ); 620 621 $this->assertEquals( 1, $test_action_counter ); 621 622 622 // The action should fire if the status of 'mature' stays the same.623 // The action should not fire if the status of 'mature' stays the same. 623 624 update_blog_status( $blog_id, 'public', 0 ); 624 625 $blog = get_site( $blog_id ); 625 626 626 627 $this->assertEquals( '0', $blog->public ); 627 $this->assertEquals( 2, $test_action_counter );628 $this->assertEquals( 1, $test_action_counter ); 628 629 629 630 remove_action( 'update_blog_public', array( $this, '_action_counter_cb' ), 10 ); 630 631 } … … 1240 1241 array( 'current_blog_%domain%%path%', 'site-options' ), 1241 1242 ); 1242 1243 } 1244 1245 /** 1246 * @ticket 40364 1247 * @dataProvider data_wp_insert_site 1248 */ 1249 function test_wp_insert_site( $site_data, $expected_data ) { 1250 $site_id = wp_insert_site( $site_data ); 1251 1252 $this->assertInternalType( 'integer', $site_id ); 1253 1254 $site = get_site( $site_id ); 1255 foreach ( $expected_data as $key => $value ) { 1256 $this->assertEquals( $value, $site->$key ); 1257 } 1258 } 1259 1260 function data_wp_insert_site() { 1261 return array( 1262 array( 1263 array( 1264 'domain' => 'example.com', 1265 ), 1266 array( 1267 'domain' => 'example.com', 1268 'path' => '/', 1269 'network_id' => 1, 1270 'public' => 1, 1271 'archived' => 0, 1272 'mature' => 0, 1273 'spam' => 0, 1274 'deleted' => 0, 1275 'lang_id' => 0, 1276 ), 1277 ), 1278 array( 1279 array( 1280 'domain' => 'example.com', 1281 'path' => '/foo', 1282 'network_id' => 2, 1283 ), array( 1284 'domain' => 'example.com', 1285 'path' => '/foo/', 1286 'network_id' => 2, 1287 ), 1288 ), 1289 array( 1290 array( 1291 'domain' => 'example.com', 1292 'path' => '/bar/', 1293 'site_id' => 2, 1294 'public' => 0, 1295 ), array( 1296 'domain' => 'example.com', 1297 'path' => '/bar/', 1298 'network_id' => 2, 1299 'public' => 0, 1300 ), 1301 ), 1302 array( 1303 array( 1304 'domain' => 'example.com', 1305 'path' => 'foobar', 1306 'public' => 0, 1307 'archived' => 1, 1308 'mature' => 1, 1309 'spam' => 1, 1310 'deleted' => 1, 1311 'lang_id' => 1, 1312 ), array( 1313 'domain' => 'example.com', 1314 'path' => '/foobar/', 1315 'public' => 0, 1316 'archived' => 1, 1317 'mature' => 1, 1318 'spam' => 1, 1319 'deleted' => 1, 1320 'lang_id' => 1, 1321 ), 1322 ), 1323 ); 1324 } 1325 1326 /** 1327 * @ticket 40364 1328 */ 1329 function test_wp_insert_site_empty_domain() { 1330 $site_id = wp_insert_site( array( 'public' => 0 ) ); 1331 1332 $this->assertWPError( $site_id ); 1333 $this->assertSame( 'site_empty_domain', $site_id->get_error_code() ); 1334 } 1335 1336 /** 1337 * @ticket 40364 1338 * @dataProvider data_wp_update_site 1339 */ 1340 function test_wp_update_site( $site_data, $expected_data ) { 1341 $site_id = self::factory()->blog->create(); 1342 1343 $old_site = get_site( $site_id ); 1344 1345 $result = wp_update_site( $site_id, $site_data ); 1346 1347 $this->assertSame( $site_id, $result ); 1348 1349 $new_site = get_site( $site_id ); 1350 foreach ( $new_site->to_array() as $key => $value ) { 1351 if ( isset( $expected_data[ $key ] ) ) { 1352 $this->assertEquals( $expected_data[ $key ], $value ); 1353 } elseif ( 'last_updated' === $key ) { 1354 $this->assertTrue( $old_site->last_updated <= $value ); 1355 } else { 1356 $this->assertEquals( $old_site->$key, $value ); 1357 } 1358 } 1359 } 1360 1361 function data_wp_update_site() { 1362 return array( 1363 array( 1364 array( 1365 'domain' => 'example.com', 1366 'network_id' => 2, 1367 ), 1368 array( 1369 'domain' => 'example.com', 1370 'site_id' => 2, 1371 ), 1372 ), 1373 array( 1374 array( 1375 'path' => 'foo', 1376 ), 1377 array( 1378 'path' => '/foo/' 1379 ), 1380 ), 1381 array( 1382 array( 1383 'public' => 0, 1384 'archived' => 1, 1385 'mature' => 1, 1386 'spam' => 1, 1387 'deleted' => 1, 1388 'lang_id' => 1, 1389 ), 1390 array( 1391 'public' => 0, 1392 'archived' => 1, 1393 'mature' => 1, 1394 'spam' => 1, 1395 'deleted' => 1, 1396 'lang_id' => 1, 1397 ), 1398 ), 1399 ); 1400 } 1401 1402 /** 1403 * @ticket 40364 1404 */ 1405 function test_wp_update_site_empty_domain() { 1406 $site_id = self::factory()->blog->create(); 1407 1408 $result = wp_update_site( $site_id, array( 'domain' => '' ) ); 1409 1410 $this->assertWPError( $result ); 1411 $this->assertSame( 'site_empty_domain', $result->get_error_code() ); 1412 } 1413 1414 /** 1415 * @ticket 40364 1416 */ 1417 function test_wp_update_site_invalid_id() { 1418 $result = wp_update_site( 444444, array( 'domain' => 'example.com' ) ); 1419 1420 $this->assertWPError( $result ); 1421 $this->assertSame( 'site_not_exist', $result->get_error_code() ); 1422 } 1423 1424 /** 1425 * @ticket 40364 1426 */ 1427 function test_wp_update_site_cleans_cache() { 1428 $site_id = self::factory()->blog->create(); 1429 $site1 = get_site( $site_id ); 1430 1431 $result = wp_update_site( $site_id, array( 'public' => 0 ) ); 1432 $site2 = get_site( $site_id ); 1433 1434 $result = wp_update_site( $site_id, array( 'public' => 1 ) ); 1435 $site3 = get_site( $site_id ); 1436 1437 $this->assertEquals( 1, $site1->public ); 1438 $this->assertEquals( 0, $site2->public ); 1439 $this->assertEquals( 1, $site3->public ); 1440 } 1441 1442 /** 1443 * @ticket 40364 1444 */ 1445 function test_wp_delete_site() { 1446 $site_id = self::factory()->blog->create(); 1447 1448 $site = get_site( $site_id ); 1449 1450 $result = wp_delete_site( $site_id ); 1451 1452 $this->assertInstanceOf( 'WP_Site', $result ); 1453 $this->assertEquals( $result->to_array(), $site->to_array() ); 1454 } 1455 1456 /** 1457 * @ticket 40364 1458 */ 1459 function test_wp_delete_site_invalid_id() { 1460 $result = wp_delete_site( 444444 ); 1461 1462 $this->assertWPError( $result ); 1463 $this->assertSame( 'site_not_exist', $result->get_error_code() ); 1464 } 1465 1466 /** 1467 * @ticket 40364 1468 */ 1469 function test_wp_delete_site_cleans_cache() { 1470 $site_id = self::factory()->blog->create(); 1471 1472 get_site( $site_id ); 1473 1474 wp_delete_site( $site_id ); 1475 1476 $this->assertNull( get_site( $site_id ) ); 1477 } 1243 1478 } 1244 1479 1245 1480 endif;