Changeset 43548 for trunk/src/wp-includes/ms-blogs.php
- Timestamp:
- 08/01/2018 01:05:44 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/ms-blogs.php
r42836 r43548 298 298 } 299 299 300 $current_details = get_site( $blog_id ); 301 if ( empty( $current_details ) ) { 300 $site = wp_update_site( $blog_id, $details ); 301 302 if ( is_wp_error( $site ) ) { 302 303 return false; 303 304 } 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 ) );321 322 if ( false === $result ) {323 return false;324 }325 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 305 426 306 return true; … … 516 396 wp_cache_delete( $site_id, 'site-details' ); 517 397 wp_cache_delete( $site_id, 'blog-details' ); 398 } 399 400 /** 401 * Inserts a new site into the database. 402 * 403 * @since 5.0.0 404 * 405 * @global wpdb $wpdb WordPress database abstraction object. 406 * 407 * @param array $data { 408 * Data for the new site that should be inserted. 409 * 410 * @type string $domain Site domain. Default empty string. 411 * @type string $path Site path. Default '/'. 412 * @type int $network_id The site's network ID. Default is the current network ID. 413 * @type string $registered When the site was registered, in SQL datetime format. Default is 414 * the current time. 415 * @type string $last_updated When the site was last updated, in SQL datetime format. Default is 416 * the value of $registered. 417 * @type int $public Whether the site is public. Default 1. 418 * @type int $archived Whether the site is archived. Default 0. 419 * @type int $mature Whether the site is mature. Default 0. 420 * @type int $spam Whether the site is spam. Default 0. 421 * @type int $deleted Whether the site is deleted. Default 0. 422 * @type int $lang_id The site's language ID. Currently unused. Default 0. 423 * } 424 * @return int|WP_Error The new site's ID on success, or error object on failure. 425 */ 426 function wp_insert_site( array $data ) { 427 global $wpdb; 428 429 $now = current_time( 'mysql', true ); 430 431 $defaults = array( 432 'domain' => '', 433 'path' => '/', 434 'network_id' => get_current_network_id(), 435 'registered' => $now, 436 'last_updated' => $now, 437 'public' => 1, 438 'archived' => 0, 439 'mature' => 0, 440 'spam' => 0, 441 'deleted' => 0, 442 'lang_id' => 0, 443 ); 444 445 $data = wp_prepare_site_data( $data, $defaults ); 446 if ( is_wp_error( $data ) ) { 447 return $data; 448 } 449 450 if ( false === $wpdb->insert( $wpdb->blogs, $data ) ) { 451 return new WP_Error( 'db_insert_error', __( 'Could not insert site into the database.' ), $wpdb->last_error ); 452 } 453 454 $new_site = get_site( $wpdb->insert_id ); 455 456 clean_blog_cache( $new_site ); 457 458 /** 459 * Fires once a site has been inserted into the database. 460 * 461 * @since 5.0.0 462 * 463 * @param WP_Site $new_site New site object. 464 */ 465 do_action( 'wp_insert_site', $new_site ); 466 467 return (int) $new_site->id; 468 } 469 470 /** 471 * Updates a site in the database. 472 * 473 * @since 5.0.0 474 * 475 * @global wpdb $wpdb WordPress database abstraction object. 476 * 477 * @param int $site_id ID of the site that should be updated. 478 * @param array $data Site data to update. See {@see wp_insert_site()} for the list of supported keys. 479 * @return int|WP_Error The updated site's ID on success, or error object on failure. 480 */ 481 function wp_update_site( $site_id, array $data ) { 482 global $wpdb; 483 484 if ( empty( $site_id ) ) { 485 return new WP_Error( 'site_empty_id', __( 'Site ID must not be empty.' ) ); 486 } 487 488 $old_site = get_site( $site_id ); 489 if ( ! $old_site ) { 490 return new WP_Error( 'site_not_exist', __( 'Site does not exist.' ) ); 491 } 492 493 $defaults = $old_site->to_array(); 494 $defaults['network_id'] = (int) $defaults['site_id']; 495 $defaults['last_updated'] = current_time( 'mysql', true ); 496 unset( $defaults['blog_id'], $defaults['site_id'] ); 497 498 $data = wp_prepare_site_data( $data, $defaults, $old_site ); 499 if ( is_wp_error( $data ) ) { 500 return $data; 501 } 502 503 if ( false === $wpdb->update( $wpdb->blogs, $data, array( 'blog_id' => $old_site->id ) ) ) { 504 return new WP_Error( 'db_update_error', __( 'Could not update site in the database.' ), $wpdb->last_error ); 505 } 506 507 clean_blog_cache( $old_site ); 508 509 $new_site = get_site( $old_site->id ); 510 511 /** 512 * Fires once a site has been updated in the database. 513 * 514 * @since 5.0.0 515 * 516 * @param WP_Site $new_site New site object. 517 * @param WP_Site $old_site Old site object. 518 */ 519 do_action( 'wp_update_site', $new_site, $old_site ); 520 521 return (int) $new_site->id; 522 } 523 524 /** 525 * Deletes a site from the database. 526 * 527 * @since 5.0.0 528 * 529 * @global wpdb $wpdb WordPress database abstraction object. 530 * 531 * @param int $site_id ID of the site that should be deleted. 532 * @return WP_Site|WP_Error The deleted site object on success, or error object on failure. 533 */ 534 function wp_delete_site( $site_id ) { 535 global $wpdb; 536 537 if ( empty( $site_id ) ) { 538 return new WP_Error( 'site_empty_id', __( 'Site ID must not be empty.' ) ); 539 } 540 541 $old_site = get_site( $site_id ); 542 if ( ! $old_site ) { 543 return new WP_Error( 'site_not_exist', __( 'Site does not exist.' ) ); 544 } 545 546 if ( false === $wpdb->delete( $wpdb->blogs, array( 'blog_id' => $old_site->id ) ) ) { 547 return new WP_Error( 'db_delete_error', __( 'Could not delete site from the database.' ), $wpdb->last_error ); 548 } 549 550 clean_blog_cache( $old_site ); 551 552 /** 553 * Fires once a site has been deleted from the database. 554 * 555 * @since 5.0.0 556 * 557 * @param WP_Site $old_site Deleted site object. 558 */ 559 do_action( 'wp_delete_site', $old_site ); 560 561 return $old_site; 518 562 } 519 563 … … 689 733 690 734 /** 735 * Prepares site data for insertion or update in the database. 736 * 737 * @since 5.0.0 738 * 739 * @param array $data Associative array of site data passed to the respective function. 740 * See {@see wp_insert_site()} for the possibly included data. 741 * @param array $defaults Site data defaults to parse $data against. 742 * @param WP_Site|null $old_site Optional. Old site object if an update, or null if an insertion. 743 * Default null. 744 * @return array|WP_Error Site data ready for a database transaction, or WP_Error in case a validation 745 * error occurred. 746 */ 747 function wp_prepare_site_data( $data, $defaults, $old_site = null ) { 748 749 // Maintain backward-compatibility with `$site_id` as network ID. 750 if ( isset( $data['site_id'] ) ) { 751 if ( ! empty( $data['site_id'] ) && empty( $data['network_id'] ) ) { 752 $data['network_id'] = $data['site_id']; 753 } 754 unset( $data['site_id'] ); 755 } 756 757 /** 758 * Filters passed site data in order to normalize it. 759 * 760 * @since 5.0.0 761 * 762 * @param array $data Associative array of site data passed to the respective function. 763 * See {@see wp_insert_site()} for the possibly included data. 764 */ 765 $data = apply_filters( 'wp_normalize_site_data', $data ); 766 767 $whitelist = array( 'domain', 'path', 'network_id', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' ); 768 $data = array_intersect_key( wp_parse_args( $data, $defaults ), array_flip( $whitelist ) ); 769 770 $errors = new WP_Error(); 771 772 /** 773 * Fires when data should be validated for a site prior to inserting or updating in the database. 774 * 775 * Plugins should amend the `$errors` object via its `WP_Error::add()` method. 776 * 777 * @since 5.0.0 778 * 779 * @param WP_Error $errors Error object to add validation errors to. 780 * @param array $data Associative array of complete site data. See {@see wp_insert_site()} 781 * for the included data. 782 * @param WP_Site|null $old_site The old site object if the data belongs to a site being updated, 783 * or null if it is a new site being inserted. 784 */ 785 do_action( 'wp_validate_site_data', $errors, $data, $old_site ); 786 787 if ( ! empty( $errors->errors ) ) { 788 return $errors; 789 } 790 791 // Prepare for database. 792 $data['site_id'] = $data['network_id']; 793 unset( $data['network_id'] ); 794 795 return $data; 796 } 797 798 /** 799 * Normalizes data for a site prior to inserting or updating in the database. 800 * 801 * @since 5.0.0 802 * 803 * @param array $data Associative array of site data passed to the respective function. 804 * See {@see wp_insert_site()} for the possibly included data. 805 * @return array Normalized site data. 806 */ 807 function wp_normalize_site_data( $data ) { 808 // Sanitize domain if passed. 809 if ( array_key_exists( 'domain', $data ) ) { 810 $data['domain'] = trim( $data['domain'] ); 811 $data['domain'] = preg_replace( '/\s+/', '', sanitize_user( $data['domain'], true ) ); 812 if ( is_subdomain_install() ) { 813 $data['domain'] = str_replace( '@', '', $data['domain'] ); 814 } 815 } 816 817 // Sanitize path if passed. 818 if ( array_key_exists( 'path', $data ) ) { 819 $data['path'] = trailingslashit( '/' . trim( $data['path'], '/' ) ); 820 } 821 822 // Sanitize network ID if passed. 823 if ( array_key_exists( 'network_id', $data ) ) { 824 $data['network_id'] = (int) $data['network_id']; 825 } 826 827 // Sanitize status fields if passed. 828 $status_fields = array( 'public', 'archived', 'mature', 'spam', 'deleted' ); 829 foreach ( $status_fields as $status_field ) { 830 if ( array_key_exists( $status_field, $data ) ) { 831 $data[ $status_field ] = (int) $data[ $status_field ]; 832 } 833 } 834 835 // Strip date fields if empty. 836 $date_fields = array( 'registered', 'last_updated' ); 837 foreach ( $date_fields as $date_field ) { 838 if ( ! array_key_exists( $date_field, $data ) ) { 839 continue; 840 } 841 842 if ( empty( $data[ $date_field ] ) || '0000-00-00 00:00:00' === $data[ $date_field ] ) { 843 unset( $data[ $date_field ] ); 844 } 845 } 846 847 return $data; 848 } 849 850 /** 851 * Validates data for a site prior to inserting or updating in the database. 852 * 853 * @since 5.0.0 854 * 855 * @param WP_Error $errors Error object, passed by reference. Will contain validation errors if 856 * any occurred. 857 * @param array $data Associative array of complete site data. See {@see wp_insert_site()} 858 * for the included data. 859 * @param WP_Site|null $old_site The old site object if the data belongs to a site being updated, 860 * or null if it is a new site being inserted. 861 */ 862 function wp_validate_site_data( $errors, $data, $old_site = null ) { 863 // A domain must always be present. 864 if ( empty( $data['domain'] ) ) { 865 $errors->add( 'site_empty_domain', __( 'Site domain must not be empty.' ) ); 866 } 867 868 // A path must always be present. 869 if ( empty( $data['path'] ) ) { 870 $errors->add( 'site_empty_path', __( 'Site path must not be empty.' ) ); 871 } 872 873 // A network ID must always be present. 874 if ( empty( $data['network_id'] ) ) { 875 $errors->add( 'site_empty_network_id', __( 'Site network ID must be provided.' ) ); 876 } 877 878 // Both registration and last updated dates must always be present and valid. 879 $date_fields = array( 'registered', 'last_updated' ); 880 foreach ( $date_fields as $date_field ) { 881 if ( empty( $data[ $date_field ] ) ) { 882 $errors->add( 'site_empty_' . $date_field, __( 'Both registration and last updated dates must be provided.' ) ); 883 break; 884 } 885 886 // Allow '0000-00-00 00:00:00', although it be stripped out at this point. 887 if ( '0000-00-00 00:00:00' !== $data[ $date_field ] ) { 888 $month = substr( $data[ $date_field ], 5, 2 ); 889 $day = substr( $data[ $date_field ], 8, 2 ); 890 $year = substr( $data[ $date_field ], 0, 4 ); 891 $valid_date = wp_checkdate( $month, $day, $year, $data[ $date_field ] ); 892 if ( ! $valid_date ) { 893 $errors->add( 'site_invalid_' . $date_field, __( 'Both registration and last updated dates must be valid dates.' ) ); 894 break; 895 } 896 } 897 } 898 899 if ( ! empty( $errors->errors ) ) { 900 return; 901 } 902 903 // If a new site, or domain/path/network ID have changed, ensure uniqueness. 904 if ( ! $old_site 905 || $data['domain'] !== $old_site->domain 906 || $data['path'] !== $old_site->path 907 || $data['network_id'] !== $old_site->network_id 908 ) { 909 if ( domain_exists( $data['domain'], $data['path'], $data['network_id'] ) ) { 910 $errors->add( 'site_taken', __( 'Sorry, that site already exists!' ) ); 911 } 912 } 913 } 914 915 /** 691 916 * Retrieve option value for a given blog id based on name of option. 692 917 * … … 1194 1419 * 1195 1420 * @since MU (3.0.0) 1421 * @since 5.0.0 Use wp_update_site() internally. 1196 1422 * 1197 1423 * @global wpdb $wpdb WordPress database abstraction object. … … 1214 1440 } 1215 1441 1216 $result = $wpdb->update( 1217 $wpdb->blogs, array( 1218 $pref => $value, 1219 'last_updated' => current_time( 'mysql', true ), 1220 ), array( 'blog_id' => $blog_id ) 1221 ); 1222 1223 if ( false === $result ) { 1442 $result = wp_update_site( $blog_id, array( 1443 $pref => $value, 1444 ) ); 1445 1446 if ( is_wp_error( $result ) ) { 1224 1447 return false; 1225 }1226 1227 clean_blog_cache( $blog_id );1228 1229 if ( 'spam' == $pref ) {1230 if ( $value == 1 ) {1231 /** This filter is documented in wp-includes/ms-blogs.php */1232 do_action( 'make_spam_blog', $blog_id );1233 } else {1234 /** This filter is documented in wp-includes/ms-blogs.php */1235 do_action( 'make_ham_blog', $blog_id );1236 }1237 } elseif ( 'mature' == $pref ) {1238 if ( $value == 1 ) {1239 /** This filter is documented in wp-includes/ms-blogs.php */1240 do_action( 'mature_blog', $blog_id );1241 } else {1242 /** This filter is documented in wp-includes/ms-blogs.php */1243 do_action( 'unmature_blog', $blog_id );1244 }1245 } elseif ( 'archived' == $pref ) {1246 if ( $value == 1 ) {1247 /** This filter is documented in wp-includes/ms-blogs.php */1248 do_action( 'archive_blog', $blog_id );1249 } else {1250 /** This filter is documented in wp-includes/ms-blogs.php */1251 do_action( 'unarchive_blog', $blog_id );1252 }1253 } elseif ( 'deleted' == $pref ) {1254 if ( $value == 1 ) {1255 /** This filter is documented in wp-includes/ms-blogs.php */1256 do_action( 'make_delete_blog', $blog_id );1257 } else {1258 /** This filter is documented in wp-includes/ms-blogs.php */1259 do_action( 'make_undelete_blog', $blog_id );1260 }1261 } elseif ( 'public' == $pref ) {1262 /**1263 * Fires after the current blog's 'public' setting is updated.1264 *1265 * @since MU (3.0.0)1266 *1267 * @param int $blog_id Blog ID.1268 * @param string $value The value of blog status.1269 */1270 do_action( 'update_blog_public', $blog_id, $value ); // Moved here from update_blog_public().1271 1448 } 1272 1449 … … 1538 1715 update_posts_count(); 1539 1716 } 1717 1718 /** 1719 * Updates the count of sites for a network based on a changed site. 1720 * 1721 * @since 5.0.0 1722 * 1723 * @param WP_Site $new_site The site object that has been inserted, updated or deleted. 1724 * @param WP_Site|null $old_site Optional. If $new_site has been updated, this must be the previous 1725 * state of that site. Default null. 1726 */ 1727 function wp_maybe_update_network_site_counts_on_update( $new_site, $old_site = null ) { 1728 if ( null === $old_site ) { 1729 wp_maybe_update_network_site_counts( $new_site->network_id ); 1730 return; 1731 } 1732 1733 if ( $new_site->network_id != $old_site->network_id ) { 1734 wp_maybe_update_network_site_counts( $new_site->network_id ); 1735 wp_maybe_update_network_site_counts( $old_site->network_id ); 1736 } 1737 } 1738 1739 /** 1740 * Triggers actions on site status updates. 1741 * 1742 * @since 5.0.0 1743 * 1744 * @param WP_Site $new_site The site object after the update. 1745 * @param WP_Site|null $old_site Optional. If $new_site has been updated, this must be the previous 1746 * state of that site. Default null. 1747 */ 1748 function wp_maybe_transition_site_statuses_on_update( $new_site, $old_site = null ) { 1749 $site_id = $new_site->id; 1750 1751 // Use the default values for a site if no previous state is given. 1752 if ( ! $old_site ) { 1753 $old_site = new WP_Site( new stdClass() ); 1754 } 1755 1756 if ( $new_site->spam != $old_site->spam ) { 1757 if ( $new_site->spam == 1 ) { 1758 1759 /** 1760 * Fires when the 'spam' status is added to a site. 1761 * 1762 * @since MU (3.0.0) 1763 * 1764 * @param int $site_id Site ID. 1765 */ 1766 do_action( 'make_spam_blog', $site_id ); 1767 } else { 1768 1769 /** 1770 * Fires when the 'spam' status is removed from a site. 1771 * 1772 * @since MU (3.0.0) 1773 * 1774 * @param int $site_id Site ID. 1775 */ 1776 do_action( 'make_ham_blog', $site_id ); 1777 } 1778 } 1779 1780 if ( $new_site->mature != $old_site->mature ) { 1781 if ( $new_site->mature == 1 ) { 1782 1783 /** 1784 * Fires when the 'mature' status is added to a site. 1785 * 1786 * @since 3.1.0 1787 * 1788 * @param int $site_id Site ID. 1789 */ 1790 do_action( 'mature_blog', $site_id ); 1791 } else { 1792 1793 /** 1794 * Fires when the 'mature' status is removed from a site. 1795 * 1796 * @since 3.1.0 1797 * 1798 * @param int $site_id Site ID. 1799 */ 1800 do_action( 'unmature_blog', $site_id ); 1801 } 1802 } 1803 1804 if ( $new_site->archived != $old_site->archived ) { 1805 if ( $new_site->archived == 1 ) { 1806 1807 /** 1808 * Fires when the 'archived' status is added to a site. 1809 * 1810 * @since MU (3.0.0) 1811 * 1812 * @param int $site_id Site ID. 1813 */ 1814 do_action( 'archive_blog', $site_id ); 1815 } else { 1816 1817 /** 1818 * Fires when the 'archived' status is removed from a site. 1819 * 1820 * @since MU (3.0.0) 1821 * 1822 * @param int $site_id Site ID. 1823 */ 1824 do_action( 'unarchive_blog', $site_id ); 1825 } 1826 } 1827 1828 if ( $new_site->deleted != $old_site->deleted ) { 1829 if ( $new_site->deleted == 1 ) { 1830 1831 /** 1832 * Fires when the 'deleted' status is added to a site. 1833 * 1834 * @since 3.5.0 1835 * 1836 * @param int $site_id Site ID. 1837 */ 1838 do_action( 'make_delete_blog', $site_id ); 1839 } else { 1840 1841 /** 1842 * Fires when the 'deleted' status is removed from a site. 1843 * 1844 * @since 3.5.0 1845 * 1846 * @param int $site_id Site ID. 1847 */ 1848 do_action( 'make_undelete_blog', $site_id ); 1849 } 1850 } 1851 1852 if ( $new_site->public != $old_site->public ) { 1853 1854 /** 1855 * Fires after the current blog's 'public' setting is updated. 1856 * 1857 * @since MU (3.0.0) 1858 * 1859 * @param int $site_id Site ID. 1860 * @param string $value The value of the site status. 1861 */ 1862 do_action( 'update_blog_public', $site_id, $new_site->public ); 1863 } 1864 } 1865 1866 /** 1867 * Cleans the necessary caches after specific site data has been updated. 1868 * 1869 * @since 5.0.0 1870 * 1871 * @param WP_Site $new_site The site object after the update. 1872 * @param WP_Site $old_site The site obejct prior to the update. 1873 */ 1874 function wp_maybe_clean_new_site_cache_on_update( $new_site, $old_site ) { 1875 if ( $old_site->domain !== $new_site->domain || $old_site->path !== $new_site->path ) { 1876 clean_blog_cache( $new_site ); 1877 } 1878 } 1879 1880 /** 1881 * Updates the `blog_public` option for a given site ID. 1882 * 1883 * @since 5.0.0 1884 * 1885 * @param int $site_id Site ID. 1886 * @param string $public The value of the site status. 1887 */ 1888 function wp_update_blog_public_option_on_site_update( $site_id, $public ) { 1889 update_blog_option( $site_id, 'blog_public', $public ); 1890 }
Note: See TracChangeset
for help on using the changeset viewer.