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(); |
| 385 | * Inserts a new site into the database. |
| 386 | * |
| 387 | * @since 4.9.0 |
| 388 | * |
| 389 | * @global wpdb $wpdb WordPress database abstraction object. |
| 390 | * |
| 391 | * @param array $data { |
| 392 | * Data for the new site that should be inserted. |
| 393 | * |
| 394 | * @type string $domain Site domain. Must always be provided. |
| 395 | * @type string $path Site path. Default '/'. |
| 396 | * @type int $site_id The site's network ID. Default is the main network ID. |
| 397 | * @type string $registered When the site was registered, in SQL datetime format. Default is |
| 398 | * the current time. |
| 399 | * @type string $last_updated When the site was last updated, in SQL datetime format. Default is |
| 400 | * the value of $registered. |
| 401 | * @type int $public Whether the site is public. Default 1. |
| 402 | * @type int $archived Whether the site is archived. Default 0. |
| 403 | * @type int $mature Whether the site is mature. Default 0. |
| 404 | * @type int $spam Whether the site is spam. Default 0. |
| 405 | * @type int $deleted Whether the site is deleted. Default 0. |
| 406 | * @type int $lang_id The site's language ID. Currently unused. Default 0. |
| 407 | * } |
| 408 | * @return int|WP_Error The new site's ID on success, or error object on failure. |
| 409 | */ |
| 410 | function wp_insert_site( $data ) { |
| 411 | global $wpdb; |
| 412 | |
| 413 | $now = current_time( 'mysql' ); |
| 414 | |
| 415 | $defaults = array( |
| 416 | 'domain' => '', |
| 417 | 'path' => '', |
| 418 | 'site_id' => 0, |
| 419 | 'registered' => $now, |
| 420 | 'last_updated' => $now, |
| 421 | 'public' => 1, |
| 422 | 'archived' => 0, |
| 423 | 'mature' => 0, |
| 424 | 'spam' => 0, |
| 425 | 'deleted' => 0, |
| 426 | 'lang_id' => 0, |
| 427 | ); |
| 428 | |
| 429 | $compat_keys = array( |
| 430 | 'network_id' => 'site_id', |
| 431 | ); |
| 432 | |
| 433 | foreach ( $compat_keys as $compat_key => $original_compat_key ) { |
| 434 | if ( ! empty( $data[ $compat_key ] ) && empty( $data[ $original_compat_key ] ) ) { |
| 435 | $data[ $original_compat_key ] = $data[ $compat_key ]; |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | $data = array_intersect_key( wp_parse_args( $data, $defaults ), $defaults ); |
| 440 | |
| 441 | // A domain must always be present. |
| 442 | if ( empty( $data['domain'] ) ) { |
| 443 | return new WP_Error( 'site_empty_domain', __( 'Site domain must not be empty.' ) ); |
| 444 | } |
| 445 | |
| 446 | $data['path'] = trailingslashit( '/' . trim( $data['path'], '/' ) ); |
| 447 | |
| 448 | // Use the default network if none is set. |
| 449 | if ( empty( $data['site_id'] ) ) { |
| 450 | $data['site_id'] = get_main_network_id(); |
| 451 | } |
| 452 | |
| 453 | if ( false === $wpdb->insert( $wpdb->blogs, $data ) ) { |
| 454 | return new WP_Error( 'db_insert_error', __( 'Could not insert site into the database.' ), $wpdb->last_error ); |
| 455 | } |
| 456 | |
| 457 | $new_site = get_site( $wpdb->insert_id ); |
| 458 | |
| 459 | clean_blog_cache( $new_site ); |
| 460 | |
| 461 | /** |
| 462 | * Fires once a site has been inserted into the database. |
| 463 | * |
| 464 | * @since 4.9.0 |
| 465 | * |
| 466 | * @param WP_Site $new_site New site object. |
| 467 | */ |
| 468 | do_action( 'wp_insert_site', $new_site ); |
| 469 | |
| 470 | return (int) $new_site->id; |
| 471 | } |
| 472 | |
| 473 | /** |
| 474 | * Updates a site in the database. |
| 475 | * |
| 476 | * @since 4.9.0 |
| 477 | * |
| 478 | * @global wpdb $wpdb WordPress database abstraction object. |
| 479 | * |
| 480 | * @param int $site_id ID of the site that should be updated. |
| 481 | * @param array $data Site data to update. See wp_insert_site() for the list of supported keys. |
| 482 | * @return int|WP_Error The updated site's ID on success, or error object on failure. |
| 483 | */ |
| 484 | function wp_update_site( $site_id, $data ) { |
| 485 | global $wpdb; |
| 486 | |
| 487 | $old_site = get_site( $site_id ); |
| 488 | if ( ! $old_site ) { |
| 489 | return new WP_Error( 'site_not_exist', __( 'Site does not exist.' ) ); |
| 490 | } |
| 491 | |
| 492 | $defaults = $old_site->to_array(); |
| 493 | $defaults['last_updated'] = current_time( 'mysql' ); |
| 494 | unset( $defaults['blog_id'] ); |
| 495 | |
| 496 | $compat_keys = array( |
| 497 | 'network_id' => 'site_id', |
| 498 | ); |
| 499 | |
| 500 | foreach ( $compat_keys as $compat_key => $original_compat_key ) { |
| 501 | if ( ! empty( $data[ $compat_key ] ) && empty( $data[ $original_compat_key ] ) ) { |
| 502 | $data[ $original_compat_key ] = $data[ $compat_key ]; |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | $data = array_intersect_key( wp_parse_args( $data, $defaults ), $defaults ); |
| 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 default network if none is set. |
| 516 | if ( empty( $data['site_id'] ) ) { |
| 517 | $data['site_id'] = get_main_network_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 | } |