| 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.0 |
| 252 | | * |
| 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.0 |
| 261 | | * |
| 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.0 |
| 298 | | * |
| 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.0 |
| 307 | | * |
| 308 | | * @param int $blog_id Blog ID. |
| 309 | | */ |
| 310 | | do_action( 'make_undelete_blog', $blog_id ); |
| 311 | | } |
| 312 | | } |
| 313 | | |
| 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 | | |
| | 292 | * Inserts a new site into the database. |
| | 293 | * |
| | 294 | * @since 4.9.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 4.9.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 4.9.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 | $data = array_intersect_key( wp_parse_args( $data, $defaults ), $defaults ); |
| | 416 | |
| | 417 | $data['domain'] = trim( $data['domain'] ); |
| | 418 | |
| | 419 | // A domain must always be present. |
| | 420 | if ( empty( $data['domain'] ) ) { |
| | 421 | return new WP_Error( 'site_empty_domain', __( 'Site domain must not be empty.' ) ); |
| | 422 | } |
| | 423 | |
| | 424 | $data['path'] = trailingslashit( '/' . trim( $data['path'], '/' ) ); |
| | 425 | |
| | 426 | // Use the previously set network if a falsy network ID has been passed. |
| | 427 | if ( empty( $data['site_id'] ) ) { |
| | 428 | $data['site_id'] = $defaults['site_id']; |
| | 429 | } |
| | 430 | |
| | 431 | if ( false === $wpdb->update( $wpdb->blogs, $data, array( 'blog_id' => $old_site->id ) ) ) { |
| | 432 | return new WP_Error( 'db_update_error', __( 'Could not update site in the database.' ), $wpdb->last_error ); |
| | 433 | } |
| | 434 | |
| | 435 | clean_blog_cache( $old_site ); |
| | 436 | |
| | 437 | $new_site = get_site( $old_site->id ); |
| | 438 | |
| | 439 | /** |
| | 440 | * Fires once a site has been updated in the database. |
| | 441 | * |
| | 442 | * @since 4.9.0 |
| | 443 | * |
| | 444 | * @param WP_Site $new_site New site object. |
| | 445 | * @param WP_Site $old_site Old site object. |
| | 446 | */ |
| | 447 | do_action( 'wp_update_site', $new_site, $old_site ); |
| | 448 | |
| | 449 | return (int) $new_site->id; |
| | 450 | } |
| | 451 | |
| | 452 | /** |
| | 453 | * Deletes a site from the database. |
| | 454 | * |
| | 455 | * @since 4.9.0 |
| | 456 | * |
| | 457 | * @global wpdb $wpdb WordPress database abstraction object. |
| | 458 | * |
| | 459 | * @param int $site_id ID of the site that should be deleted. |
| | 460 | * @return WP_Site|WP_Error The deleted site object on success, or error object on failure. |
| | 461 | */ |
| | 462 | function wp_delete_site( $site_id ) { |
| | 463 | global $wpdb; |
| | 464 | |
| | 465 | $old_site = get_site( $site_id ); |
| | 466 | if ( ! $old_site ) { |
| | 467 | return new WP_Error( 'site_not_exist', __( 'Site does not exist.' ) ); |
| | 468 | } |
| | 469 | |
| | 470 | if ( false === $wpdb->delete( $wpdb->blogs, array( 'blog_id' => $old_site->id ) ) ) { |
| | 471 | return new WP_Error( 'db_delete_error', __( 'Could not delete site from the database.' ), $wpdb->last_error ); |
| | 472 | } |
| | 473 | |
| | 474 | clean_blog_cache( $old_site ); |
| | 475 | |
| | 476 | /** |
| | 477 | * Fires once a site has been deleted from the database. |
| | 478 | * |
| | 479 | * @since 4.9.0 |
| | 480 | * |
| | 481 | * @param WP_Site $old_site Deleted site object. |
| | 482 | */ |
| | 483 | do_action( 'wp_delete_site', $old_site ); |
| | 484 | |
| | 485 | return $old_site; |
| | 486 | } |
| | 487 | |
| | 488 | /** |
| | 1424 | |
| | 1425 | /** |
| | 1426 | * Updates the count of sites for a network based on a changed site. |
| | 1427 | * |
| | 1428 | * @since 4.9.0 |
| | 1429 | * |
| | 1430 | * @param WP_Site $new_site The site object that has been inserted, updated or deleted. |
| | 1431 | * @param WP_Site|null $old_site Optional. If $new_site has been updated, this must be the previous |
| | 1432 | * state of that site. Default null. |
| | 1433 | */ |
| | 1434 | function wp_maybe_update_network_site_counts_on_update( $new_site, $old_site = null ) { |
| | 1435 | if ( null === $old_site ) { |
| | 1436 | wp_maybe_update_network_site_counts( $new_site->network_id ); |
| | 1437 | return; |
| | 1438 | } |
| | 1439 | |
| | 1440 | if ( $new_site->network_id != $old_site->network_id ) { |
| | 1441 | wp_maybe_update_network_site_counts( $new_site->network_id ); |
| | 1442 | wp_maybe_update_network_site_counts( $old_site->network_id ); |
| | 1443 | } |
| | 1444 | } |
| | 1445 | |
| | 1446 | /** |
| | 1447 | * Triggers actions on site status updates. |
| | 1448 | * |
| | 1449 | * @since 4.9.0 |
| | 1450 | * |
| | 1451 | * @param WP_Site $new_site The site object after the update. |
| | 1452 | * @param WP_Site $old_site The site obejct prior to the update. |
| | 1453 | */ |
| | 1454 | function wp_maybe_transition_site_statuses_on_update( $new_site, $old_site ) { |
| | 1455 | $site_id = $new_site->id; |
| | 1456 | |
| | 1457 | if ( $new_site->spam != $old_site->spam ) { |
| | 1458 | if ( $new_site->spam == 1 ) { |
| | 1459 | |
| | 1460 | /** |
| | 1461 | * Fires when the 'spam' status is added to a site. |
| | 1462 | * |
| | 1463 | * @since MU (3.0.0) |
| | 1464 | * |
| | 1465 | * @param int $site_id Site ID. |
| | 1466 | */ |
| | 1467 | do_action( 'make_spam_blog', $site_id ); |
| | 1468 | } else { |
| | 1469 | |
| | 1470 | /** |
| | 1471 | * Fires when the 'spam' status is removed from a site. |
| | 1472 | * |
| | 1473 | * @since MU (3.0.0) |
| | 1474 | * |
| | 1475 | * @param int $site_id Site ID. |
| | 1476 | */ |
| | 1477 | do_action( 'make_ham_blog', $site_id ); |
| | 1478 | } |
| | 1479 | } |
| | 1480 | |
| | 1481 | if ( $new_site->mature != $old_site->mature ) { |
| | 1482 | if ( $new_site->mature == 1 ) { |
| | 1483 | |
| | 1484 | /** |
| | 1485 | * Fires when the 'mature' status is added to a site. |
| | 1486 | * |
| | 1487 | * @since 3.1.0 |
| | 1488 | * |
| | 1489 | * @param int $site_id Site ID. |
| | 1490 | */ |
| | 1491 | do_action( 'mature_blog', $site_id ); |
| | 1492 | } else { |
| | 1493 | |
| | 1494 | /** |
| | 1495 | * Fires when the 'mature' status is removed from a site. |
| | 1496 | * |
| | 1497 | * @since 3.1.0 |
| | 1498 | * |
| | 1499 | * @param int $site_id Site ID. |
| | 1500 | */ |
| | 1501 | do_action( 'unmature_blog', $site_id ); |
| | 1502 | } |
| | 1503 | } |
| | 1504 | |
| | 1505 | if ( $new_site->archived != $old_site->archived ) { |
| | 1506 | if ( $new_site->archived == 1 ) { |
| | 1507 | |
| | 1508 | /** |
| | 1509 | * Fires when the 'archived' status is added to a site. |
| | 1510 | * |
| | 1511 | * @since MU (3.0.0) |
| | 1512 | * |
| | 1513 | * @param int $site_id Site ID. |
| | 1514 | */ |
| | 1515 | do_action( 'archive_blog', $site_id ); |
| | 1516 | } else { |
| | 1517 | |
| | 1518 | /** |
| | 1519 | * Fires when the 'archived' status is removed from a site. |
| | 1520 | * |
| | 1521 | * @since MU (3.0.0) |
| | 1522 | * |
| | 1523 | * @param int $site_id Site ID. |
| | 1524 | */ |
| | 1525 | do_action( 'unarchive_blog', $site_id ); |
| | 1526 | } |
| | 1527 | } |
| | 1528 | |
| | 1529 | if ( $new_site->deleted != $old_site->deleted ) { |
| | 1530 | if ( $new_site->deleted == 1 ) { |
| | 1531 | |
| | 1532 | /** |
| | 1533 | * Fires when the 'deleted' status is added to a site. |
| | 1534 | * |
| | 1535 | * @since 3.5.0 |
| | 1536 | * |
| | 1537 | * @param int $site_id Site ID. |
| | 1538 | */ |
| | 1539 | do_action( 'make_delete_blog', $site_id ); |
| | 1540 | } else { |
| | 1541 | |
| | 1542 | /** |
| | 1543 | * Fires when the 'deleted' status is removed from a site. |
| | 1544 | * |
| | 1545 | * @since 3.5.0 |
| | 1546 | * |
| | 1547 | * @param int $site_id Site ID. |
| | 1548 | */ |
| | 1549 | do_action( 'make_undelete_blog', $site_id ); |
| | 1550 | } |
| | 1551 | } |
| | 1552 | |
| | 1553 | if ( $new_site->public != $old_site->public ) { |
| | 1554 | if ( $new_site->public == 1 ) { |
| | 1555 | |
| | 1556 | /** |
| | 1557 | * Fires when the 'public' status is added to a site. |
| | 1558 | * |
| | 1559 | * @since 4.9.0 |
| | 1560 | * |
| | 1561 | * @param int $site_id Site ID. |
| | 1562 | */ |
| | 1563 | do_action( 'make_public_blog', $site_id ); |
| | 1564 | } else { |
| | 1565 | |
| | 1566 | /** |
| | 1567 | * Fires when the 'public' status is removed from a site. |
| | 1568 | * |
| | 1569 | * @since 4.9.0 |
| | 1570 | * |
| | 1571 | * @param int $site_id Site ID. |
| | 1572 | */ |
| | 1573 | do_action( 'make_private_blog', $site_id ); |
| | 1574 | } |
| | 1575 | } |
| | 1576 | } |
| | 1577 | |
| | 1578 | /** |
| | 1579 | * Sets the `blog_public` option to 1 for a given site ID. |
| | 1580 | * |
| | 1581 | * @since 4.9.0 |
| | 1582 | * |
| | 1583 | * @param int $site_id Site ID. |
| | 1584 | */ |
| | 1585 | function wp_make_blog_public_on_site_update( $site_id ) { |
| | 1586 | update_blog_option( $site_id, 'blog_public', 1 ); |
| | 1587 | } |
| | 1588 | |
| | 1589 | /** |
| | 1590 | * Sets the `blog_public` option to 0 for a given site ID. |
| | 1591 | * |
| | 1592 | * @since 4.9.0 |
| | 1593 | * |
| | 1594 | * @param int $site_id Site ID. |
| | 1595 | */ |
| | 1596 | function wp_make_blog_private_on_site_update( $site_id ) { |
| | 1597 | update_blog_option( $site_id, 'blog_public', 0 ); |
| | 1598 | } |