| 317 | | |
| 318 | | $current_details = get_object_vars($current_details); |
| 319 | | |
| 320 | | $details = array_merge($current_details, $details); |
| 321 | | $details['last_updated'] = current_time('mysql', true); |
| 322 | | |
| 323 | | $update_details = array(); |
| 324 | | $fields = array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id'); |
| 325 | | foreach ( array_intersect( array_keys( $details ), $fields ) as $field ) { |
| 326 | | if ( 'path' === $field ) { |
| 327 | | $details[ $field ] = trailingslashit( '/' . trim( $details[ $field ], '/' ) ); |
| 328 | | } |
| 329 | | |
| 330 | | $update_details[ $field ] = $details[ $field ]; |
| 331 | | } |
| 332 | | |
| 333 | | $result = $wpdb->update( $wpdb->blogs, $update_details, array('blog_id' => $blog_id) ); |
| 334 | | |
| 335 | | if ( false === $result ) |
| 336 | | return false; |
| 337 | | |
| 338 | | // If spam status changed, issue actions. |
| 339 | | if ( $details['spam'] != $current_details['spam'] ) { |
| 340 | | if ( $details['spam'] == 1 ) { |
| 341 | | /** |
| 342 | | * Fires when the 'spam' status is added to a blog. |
| 343 | | * |
| 344 | | * @since MU (3.0.0) |
| 345 | | * |
| 346 | | * @param int $blog_id Blog ID. |
| 347 | | */ |
| 348 | | do_action( 'make_spam_blog', $blog_id ); |
| 349 | | } else { |
| 350 | | /** |
| 351 | | * Fires when the 'spam' status is removed from a blog. |
| 352 | | * |
| 353 | | * @since MU (3.0.0) |
| 354 | | * |
| 355 | | * @param int $blog_id Blog ID. |
| 356 | | */ |
| 357 | | do_action( 'make_ham_blog', $blog_id ); |
| 358 | | } |
| 359 | | } |
| 360 | | |
| 361 | | // If mature status changed, issue actions. |
| 362 | | if ( $details['mature'] != $current_details['mature'] ) { |
| 363 | | if ( $details['mature'] == 1 ) { |
| 364 | | /** |
| 365 | | * Fires when the 'mature' status is added to a blog. |
| 366 | | * |
| 367 | | * @since 3.1.0 |
| 368 | | * |
| 369 | | * @param int $blog_id Blog ID. |
| 370 | | */ |
| 371 | | do_action( 'mature_blog', $blog_id ); |
| 372 | | } else { |
| 373 | | /** |
| 374 | | * Fires when the 'mature' status is removed from a blog. |
| 375 | | * |
| 376 | | * @since 3.1.0 |
| 377 | | * |
| 378 | | * @param int $blog_id Blog ID. |
| 379 | | */ |
| 380 | | do_action( 'unmature_blog', $blog_id ); |
| 381 | | } |
| 382 | | } |
| 383 | | |
| 384 | | // If archived status changed, issue actions. |
| 385 | | if ( $details['archived'] != $current_details['archived'] ) { |
| 386 | | if ( $details['archived'] == 1 ) { |
| 387 | | /** |
| 388 | | * Fires when the 'archived' status is added to a blog. |
| 389 | | * |
| 390 | | * @since MU (3.0.0) |
| 391 | | * |
| 392 | | * @param int $blog_id Blog ID. |
| 393 | | */ |
| 394 | | do_action( 'archive_blog', $blog_id ); |
| 395 | | } else { |
| 396 | | /** |
| 397 | | * Fires when the 'archived' status is removed from a blog. |
| 398 | | * |
| 399 | | * @since MU (3.0.0) |
| 400 | | * |
| 401 | | * @param int $blog_id Blog ID. |
| 402 | | */ |
| 403 | | do_action( 'unarchive_blog', $blog_id ); |
| 404 | | } |
| 405 | | } |
| 406 | | |
| 407 | | // If deleted status changed, issue actions. |
| 408 | | if ( $details['deleted'] != $current_details['deleted'] ) { |
| 409 | | if ( $details['deleted'] == 1 ) { |
| 410 | | /** |
| 411 | | * Fires when the 'deleted' status is added to a blog. |
| 412 | | * |
| 413 | | * @since 3.5.0 |
| 414 | | * |
| 415 | | * @param int $blog_id Blog ID. |
| 416 | | */ |
| 417 | | do_action( 'make_delete_blog', $blog_id ); |
| 418 | | } else { |
| 419 | | /** |
| 420 | | * Fires when the 'deleted' status is removed from a blog. |
| 421 | | * |
| 422 | | * @since 3.5.0 |
| 423 | | * |
| 424 | | * @param int $blog_id Blog ID. |
| 425 | | */ |
| 426 | | do_action( 'make_undelete_blog', $blog_id ); |
| 427 | | } |
| 428 | | } |
| 429 | | |
| 430 | | if ( isset( $details['public'] ) ) { |
| 431 | | switch_to_blog( $blog_id ); |
| 432 | | update_option( 'blog_public', $details['public'] ); |
| 433 | | restore_current_blog(); |
| | 381 | * Inserts a new site into the database. |
| | 382 | * |
| | 383 | * @since 4.9.0 |
| | 384 | * |
| | 385 | * @global wpdb $wpdb WordPress database abstraction object. |
| | 386 | * |
| | 387 | * @param array $data { |
| | 388 | * Data for the new site that should be inserted. |
| | 389 | * |
| | 390 | * @type string $domain Site domain. Must always be provided. |
| | 391 | * @type string $path Site path. Default '/'. |
| | 392 | * @type int $site_id The site's network ID. Default is the current network ID. |
| | 393 | * @type string $registered When the site was registered, in SQL datetime format. Default is |
| | 394 | * the current time. |
| | 395 | * @type string $last_updated When the site was last updated, in SQL datetime format. Default is |
| | 396 | * the value of $registered. |
| | 397 | * @type int $public Whether the site is public. Default 1. |
| | 398 | * @type int $archived Whether the site is archived. Default 0. |
| | 399 | * @type int $mature Whether the site is mature. Default 0. |
| | 400 | * @type int $spam Whether the site is spam. Default 0. |
| | 401 | * @type int $deleted Whether the site is deleted. Default 0. |
| | 402 | * @type int $lang_id The site's language ID. Currently unused. Default 0. |
| | 403 | * } |
| | 404 | * @return int|WP_Error The new site's ID on success, or error object on failure. |
| | 405 | */ |
| | 406 | function wp_insert_site( $data ) { |
| | 407 | global $wpdb; |
| | 408 | |
| | 409 | $now = current_time( 'mysql' ); |
| | 410 | |
| | 411 | $defaults = array( |
| | 412 | 'domain' => '', |
| | 413 | 'path' => '', |
| | 414 | 'site_id' => 0, |
| | 415 | 'registered' => $now, |
| | 416 | 'last_updated' => $now, |
| | 417 | 'public' => 1, |
| | 418 | 'archived' => 0, |
| | 419 | 'mature' => 0, |
| | 420 | 'spam' => 0, |
| | 421 | 'deleted' => 0, |
| | 422 | 'lang_id' => 0, |
| | 423 | ); |
| | 424 | |
| | 425 | $compat_keys = array( |
| | 426 | 'network_id' => 'site_id', |
| | 427 | ); |
| | 428 | |
| | 429 | foreach ( $compat_keys as $compat_key => $original_compat_key ) { |
| | 430 | if ( ! empty( $data[ $compat_key ] ) && empty( $data[ $original_compat_key ] ) ) { |
| | 431 | $data[ $original_compat_key ] = $data[ $compat_key ]; |
| | 432 | } |
| | 433 | } |
| | 434 | |
| | 435 | $data = array_intersect_key( wp_parse_args( $data, $defaults ), $defaults ); |
| | 436 | |
| | 437 | $data['domain'] = trim( $data['domain'] ); |
| | 438 | |
| | 439 | // A domain must always be present. |
| | 440 | if ( empty( $data['domain'] ) ) { |
| | 441 | return new WP_Error( 'site_empty_domain', __( 'Site domain must not be empty.' ) ); |
| | 442 | } |
| | 443 | |
| | 444 | $data['path'] = trailingslashit( '/' . trim( $data['path'], '/' ) ); |
| | 445 | |
| | 446 | // Use the current network if none is set. |
| | 447 | if ( empty( $data['site_id'] ) ) { |
| | 448 | $data['site_id'] = get_current_network_id(); |
| | 449 | } |
| | 450 | |
| | 451 | if ( false === $wpdb->insert( $wpdb->blogs, $data ) ) { |
| | 452 | return new WP_Error( 'db_insert_error', __( 'Could not insert site into the database.' ), $wpdb->last_error ); |
| | 453 | } |
| | 454 | |
| | 455 | $new_site = get_site( $wpdb->insert_id ); |
| | 456 | |
| | 457 | clean_blog_cache( $new_site ); |
| | 458 | |
| | 459 | /** |
| | 460 | * Fires once a site has been inserted into the database. |
| | 461 | * |
| | 462 | * @since 4.9.0 |
| | 463 | * |
| | 464 | * @param WP_Site $new_site New site object. |
| | 465 | */ |
| | 466 | do_action( 'wp_insert_site', $new_site ); |
| | 467 | |
| | 468 | return (int) $new_site->id; |
| | 469 | } |
| | 470 | |
| | 471 | /** |
| | 472 | * Updates a site in the database. |
| | 473 | * |
| | 474 | * @since 4.9.0 |
| | 475 | * |
| | 476 | * @global wpdb $wpdb WordPress database abstraction object. |
| | 477 | * |
| | 478 | * @param int $site_id ID of the site that should be updated. |
| | 479 | * @param array $data Site data to update. See wp_insert_site() for the list of supported keys. |
| | 480 | * @return int|WP_Error The updated site's ID on success, or error object on failure. |
| | 481 | */ |
| | 482 | function wp_update_site( $site_id, $data ) { |
| | 483 | global $wpdb; |
| | 484 | |
| | 485 | $old_site = get_site( $site_id ); |
| | 486 | if ( ! $old_site ) { |
| | 487 | return new WP_Error( 'site_not_exist', __( 'Site does not exist.' ) ); |
| | 488 | } |
| | 489 | |
| | 490 | $defaults = $old_site->to_array(); |
| | 491 | $defaults['last_updated'] = current_time( 'mysql' ); |
| | 492 | unset( $defaults['blog_id'] ); |
| | 493 | |
| | 494 | $compat_keys = array( |
| | 495 | 'network_id' => 'site_id', |
| | 496 | ); |
| | 497 | |
| | 498 | foreach ( $compat_keys as $compat_key => $original_compat_key ) { |
| | 499 | if ( ! empty( $data[ $compat_key ] ) && empty( $data[ $original_compat_key ] ) ) { |
| | 500 | $data[ $original_compat_key ] = $data[ $compat_key ]; |
| | 501 | } |
| | 502 | } |
| | 503 | |
| | 504 | $data = array_intersect_key( wp_parse_args( $data, $defaults ), $defaults ); |
| | 505 | |
| | 506 | $data['domain'] = trim( $data['domain'] ); |
| | 507 | |
| | 508 | // A domain must always be present. |
| | 509 | if ( empty( $data['domain'] ) ) { |
| | 510 | return new WP_Error( 'site_empty_domain', __( 'Site domain must not be empty.' ) ); |
| | 511 | } |
| | 512 | |
| | 513 | $data['path'] = trailingslashit( '/' . trim( $data['path'], '/' ) ); |
| | 514 | |
| | 515 | // Use the previously set network if a falsy network ID has been passed. |
| | 516 | if ( empty( $data['site_id'] ) ) { |
| | 517 | $data['site_id'] = $defaults['site_id']; |
| | 518 | } |
| | 519 | |
| | 520 | if ( false === $wpdb->update( $wpdb->blogs, $data, array( 'blog_id' => $old_site->id ) ) ) { |
| | 521 | return new WP_Error( 'db_update_error', __( 'Could not update site in the database.' ), $wpdb->last_error ); |
| | 522 | } |
| | 523 | |
| | 524 | clean_blog_cache( $old_site ); |
| | 525 | |
| | 526 | $new_site = get_site( $old_site->id ); |
| | 527 | |
| | 528 | /** |
| | 529 | * Fires once a site has been updated in the database. |
| | 530 | * |
| | 531 | * @since 4.9.0 |
| | 532 | * |
| | 533 | * @param WP_Site $new_site New site object. |
| | 534 | * @param WP_Site $old_site Old site object. |
| | 535 | */ |
| | 536 | do_action( 'wp_update_site', $new_site, $old_site ); |
| | 537 | |
| | 538 | return (int) $new_site->id; |
| | 539 | } |
| | 540 | |
| | 541 | /** |
| | 542 | * Deletes a site from the database. |
| | 543 | * |
| | 544 | * @since 4.9.0 |
| | 545 | * |
| | 546 | * @global wpdb $wpdb WordPress database abstraction object. |
| | 547 | * |
| | 548 | * @param int $site_id ID of the site that should be deleted. |
| | 549 | * @return WP_Site|WP_Error The deleted site object on success, or error object on failure. |
| | 550 | */ |
| | 551 | function wp_delete_site( $site_id ) { |
| | 552 | global $wpdb; |
| | 553 | |
| | 554 | $old_site = get_site( $site_id ); |
| | 555 | if ( ! $old_site ) { |
| | 556 | return new WP_Error( 'site_not_exist', __( 'Site does not exist.' ) ); |
| | 557 | } |
| | 558 | |
| | 559 | if ( false === $wpdb->delete( $wpdb->blogs, array( 'blog_id' => $old_site->id ) ) ) { |
| | 560 | return new WP_Error( 'db_delete_error', __( 'Could not delete site from the database.' ), $wpdb->last_error ); |
| | 561 | } |
| | 562 | |
| | 563 | clean_blog_cache( $old_site ); |
| | 564 | |
| | 565 | /** |
| | 566 | * Fires once a site has been deleted from the database. |
| | 567 | * |
| | 568 | * @since 4.9.0 |
| | 569 | * |
| | 570 | * @param WP_Site $old_site Deleted site object. |
| | 571 | */ |
| | 572 | do_action( 'wp_delete_site', $old_site ); |
| | 573 | |
| | 574 | return $old_site; |
| | 575 | } |
| | 576 | |
| | 577 | /** |
| | 1385 | |
| | 1386 | /** |
| | 1387 | * Updates the count of sites for a network based on a changed site. |
| | 1388 | * |
| | 1389 | * @since 4.9.0 |
| | 1390 | * |
| | 1391 | * @param WP_Site $new_site The site object that has been inserted, updated or deleted. |
| | 1392 | * @param WP_Site|null $old_site Optional. If $new_site has been updated, this must be the previous |
| | 1393 | * state of that site. Default null. |
| | 1394 | */ |
| | 1395 | function wp_maybe_update_network_site_counts_on_update( $new_site, $old_site = null ) { |
| | 1396 | if ( null === $old_site ) { |
| | 1397 | wp_maybe_update_network_site_counts( $new_site->network_id ); |
| | 1398 | return; |
| | 1399 | } |
| | 1400 | |
| | 1401 | if ( $new_site->network_id != $old_site->network_id ) { |
| | 1402 | wp_maybe_update_network_site_counts( $new_site->network_id ); |
| | 1403 | wp_maybe_update_network_site_counts( $old_site->network_id ); |
| | 1404 | } |
| | 1405 | } |
| | 1406 | |
| | 1407 | /** |
| | 1408 | * Triggers actions on site status updates. |
| | 1409 | * |
| | 1410 | * @since 4.9.0 |
| | 1411 | * |
| | 1412 | * @param WP_Site $new_site The site object after the update. |
| | 1413 | * @param WP_Site $old_site The site obejct prior to the update. |
| | 1414 | */ |
| | 1415 | function wp_maybe_transition_site_statuses_on_update( $new_site, $old_site ) { |
| | 1416 | $site_id = $new_site->id; |
| | 1417 | |
| | 1418 | if ( $new_site->spam != $old_site->spam ) { |
| | 1419 | if ( $new_site->spam == 1 ) { |
| | 1420 | |
| | 1421 | /** |
| | 1422 | * Fires when the 'spam' status is added to a site. |
| | 1423 | * |
| | 1424 | * @since MU (3.0.0) |
| | 1425 | * |
| | 1426 | * @param int $site_id Site ID. |
| | 1427 | */ |
| | 1428 | do_action( 'make_spam_blog', $site_id ); |
| | 1429 | } else { |
| | 1430 | |
| | 1431 | /** |
| | 1432 | * Fires when the 'spam' status is removed from a site. |
| | 1433 | * |
| | 1434 | * @since MU (3.0.0) |
| | 1435 | * |
| | 1436 | * @param int $site_id Site ID. |
| | 1437 | */ |
| | 1438 | do_action( 'make_ham_blog', $site_id ); |
| | 1439 | } |
| | 1440 | } |
| | 1441 | |
| | 1442 | if ( $new_site->mature != $old_site->mature ) { |
| | 1443 | if ( $new_site->mature == 1 ) { |
| | 1444 | |
| | 1445 | /** |
| | 1446 | * Fires when the 'mature' status is added to a site. |
| | 1447 | * |
| | 1448 | * @since 3.1.0 |
| | 1449 | * |
| | 1450 | * @param int $site_id Site ID. |
| | 1451 | */ |
| | 1452 | do_action( 'mature_blog', $site_id ); |
| | 1453 | } else { |
| | 1454 | |
| | 1455 | /** |
| | 1456 | * Fires when the 'mature' status is removed from a site. |
| | 1457 | * |
| | 1458 | * @since 3.1.0 |
| | 1459 | * |
| | 1460 | * @param int $site_id Site ID. |
| | 1461 | */ |
| | 1462 | do_action( 'unmature_blog', $site_id ); |
| | 1463 | } |
| | 1464 | } |
| | 1465 | |
| | 1466 | if ( $new_site->archived != $old_site->archived ) { |
| | 1467 | if ( $new_site->archived == 1 ) { |
| | 1468 | |
| | 1469 | /** |
| | 1470 | * Fires when the 'archived' status is added to a site. |
| | 1471 | * |
| | 1472 | * @since MU (3.0.0) |
| | 1473 | * |
| | 1474 | * @param int $site_id Site ID. |
| | 1475 | */ |
| | 1476 | do_action( 'archive_blog', $site_id ); |
| | 1477 | } else { |
| | 1478 | |
| | 1479 | /** |
| | 1480 | * Fires when the 'archived' status is removed from a site. |
| | 1481 | * |
| | 1482 | * @since MU (3.0.0) |
| | 1483 | * |
| | 1484 | * @param int $site_id Site ID. |
| | 1485 | */ |
| | 1486 | do_action( 'unarchive_blog', $site_id ); |
| | 1487 | } |
| | 1488 | } |
| | 1489 | |
| | 1490 | if ( $new_site->deleted != $old_site->deleted ) { |
| | 1491 | if ( $new_site->deleted == 1 ) { |
| | 1492 | |
| | 1493 | /** |
| | 1494 | * Fires when the 'deleted' status is added to a site. |
| | 1495 | * |
| | 1496 | * @since 3.5.0 |
| | 1497 | * |
| | 1498 | * @param int $site_id Site ID. |
| | 1499 | */ |
| | 1500 | do_action( 'make_delete_blog', $site_id ); |
| | 1501 | } else { |
| | 1502 | |
| | 1503 | /** |
| | 1504 | * Fires when the 'deleted' status is removed from a site. |
| | 1505 | * |
| | 1506 | * @since 3.5.0 |
| | 1507 | * |
| | 1508 | * @param int $site_id Site ID. |
| | 1509 | */ |
| | 1510 | do_action( 'make_undelete_blog', $site_id ); |
| | 1511 | } |
| | 1512 | } |
| | 1513 | |
| | 1514 | if ( $new_site->public != $old_site->public ) { |
| | 1515 | if ( $new_site->public == 1 ) { |
| | 1516 | |
| | 1517 | /** |
| | 1518 | * Fires when the 'public' status is added to a site. |
| | 1519 | * |
| | 1520 | * @since 4.9.0 |
| | 1521 | * |
| | 1522 | * @param int $site_id Site ID. |
| | 1523 | */ |
| | 1524 | do_action( 'make_public_blog', $site_id ); |
| | 1525 | } else { |
| | 1526 | |
| | 1527 | /** |
| | 1528 | * Fires when the 'public' status is removed from a site. |
| | 1529 | * |
| | 1530 | * @since 4.9.0 |
| | 1531 | * |
| | 1532 | * @param int $site_id Site ID. |
| | 1533 | */ |
| | 1534 | do_action( 'make_private_blog', $site_id ); |
| | 1535 | } |
| | 1536 | } |
| | 1537 | } |
| | 1538 | |
| | 1539 | /** |
| | 1540 | * Sets the `blog_public` option to 1 for a given site ID. |
| | 1541 | * |
| | 1542 | * @since 4.9.0 |
| | 1543 | * |
| | 1544 | * @param int $site_id Site ID. |
| | 1545 | */ |
| | 1546 | function wp_make_blog_public_on_site_update( $site_id ) { |
| | 1547 | update_blog_option( $site_id, 'blog_public', 1 ); |
| | 1548 | } |
| | 1549 | |
| | 1550 | /** |
| | 1551 | * Sets the `blog_public` option to 0 for a given site ID. |
| | 1552 | * |
| | 1553 | * @since 4.9.0 |
| | 1554 | * |
| | 1555 | * @param int $site_id Site ID. |
| | 1556 | */ |
| | 1557 | function wp_make_blog_private_on_site_update( $site_id ) { |
| | 1558 | update_blog_option( $site_id, 'blog_public', 0 ); |
| | 1559 | } |