diff --git a/src/wp-admin/includes/ms.php b/src/wp-admin/includes/ms.php index f46b7f336e..c5b73c1752 100644 --- a/src/wp-admin/includes/ms.php +++ b/src/wp-admin/includes/ms.php @@ -56,6 +56,7 @@ function check_upload_size( $file ) { * Delete a site. * * @since 3.0.0 + * @since 5.0.0 Use wp_delete_site() internally to delete the site row from the database. * * @global wpdb $wpdb WordPress database abstraction object. * @@ -142,7 +143,7 @@ function wpmu_delete_blog( $blog_id, $drop = false ) { } } - $wpdb->delete( $wpdb->blogs, array( 'blog_id' => $blog_id ) ); + wp_delete_site( $blog_id ); /** * Filters the upload base directory to delete when the site is deleted. diff --git a/src/wp-includes/ms-blogs.php b/src/wp-includes/ms-blogs.php index 9282833ac4..ec60a200d7 100644 --- a/src/wp-includes/ms-blogs.php +++ b/src/wp-includes/ms-blogs.php @@ -297,132 +297,12 @@ function update_blog_details( $blog_id, $details = array() ) { $details = get_object_vars( $details ); } - $current_details = get_site( $blog_id ); - if ( empty( $current_details ) ) { + $site = wp_update_site( $blog_id, $details ); + + if ( is_wp_error( $site ) ) { return false; } - $current_details = get_object_vars( $current_details ); - - $details = array_merge( $current_details, $details ); - $details['last_updated'] = current_time( 'mysql', true ); - - $update_details = array(); - $fields = array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' ); - foreach ( array_intersect( array_keys( $details ), $fields ) as $field ) { - if ( 'path' === $field ) { - $details[ $field ] = trailingslashit( '/' . trim( $details[ $field ], '/' ) ); - } - - $update_details[ $field ] = $details[ $field ]; - } - - $result = $wpdb->update( $wpdb->blogs, $update_details, array( 'blog_id' => $blog_id ) ); - - if ( false === $result ) { - return false; - } - - // If spam status changed, issue actions. - if ( $details['spam'] != $current_details['spam'] ) { - if ( $details['spam'] == 1 ) { - /** - * Fires when the 'spam' status is added to a blog. - * - * @since MU (3.0.0) - * - * @param int $blog_id Blog ID. - */ - do_action( 'make_spam_blog', $blog_id ); - } else { - /** - * Fires when the 'spam' status is removed from a blog. - * - * @since MU (3.0.0) - * - * @param int $blog_id Blog ID. - */ - do_action( 'make_ham_blog', $blog_id ); - } - } - - // If mature status changed, issue actions. - if ( $details['mature'] != $current_details['mature'] ) { - if ( $details['mature'] == 1 ) { - /** - * Fires when the 'mature' status is added to a blog. - * - * @since 3.1.0 - * - * @param int $blog_id Blog ID. - */ - do_action( 'mature_blog', $blog_id ); - } else { - /** - * Fires when the 'mature' status is removed from a blog. - * - * @since 3.1.0 - * - * @param int $blog_id Blog ID. - */ - do_action( 'unmature_blog', $blog_id ); - } - } - - // If archived status changed, issue actions. - if ( $details['archived'] != $current_details['archived'] ) { - if ( $details['archived'] == 1 ) { - /** - * Fires when the 'archived' status is added to a blog. - * - * @since MU (3.0.0) - * - * @param int $blog_id Blog ID. - */ - do_action( 'archive_blog', $blog_id ); - } else { - /** - * Fires when the 'archived' status is removed from a blog. - * - * @since MU (3.0.0) - * - * @param int $blog_id Blog ID. - */ - do_action( 'unarchive_blog', $blog_id ); - } - } - - // If deleted status changed, issue actions. - if ( $details['deleted'] != $current_details['deleted'] ) { - if ( $details['deleted'] == 1 ) { - /** - * Fires when the 'deleted' status is added to a blog. - * - * @since 3.5.0 - * - * @param int $blog_id Blog ID. - */ - do_action( 'make_delete_blog', $blog_id ); - } else { - /** - * Fires when the 'deleted' status is removed from a blog. - * - * @since 3.5.0 - * - * @param int $blog_id Blog ID. - */ - do_action( 'make_undelete_blog', $blog_id ); - } - } - - if ( isset( $details['public'] ) ) { - switch_to_blog( $blog_id ); - update_option( 'blog_public', $details['public'] ); - restore_current_blog(); - } - - clean_blog_cache( $blog_id ); - return true; } @@ -517,6 +397,170 @@ function clean_site_details_cache( $site_id = 0 ) { wp_cache_delete( $site_id, 'blog-details' ); } +/** + * Inserts a new site into the database. + * + * @since 5.0.0 + * + * @global wpdb $wpdb WordPress database abstraction object. + * + * @param array $data { + * Data for the new site that should be inserted. + * + * @type string $domain Site domain. Default empty string. + * @type string $path Site path. Default '/'. + * @type int $network_id The site's network ID. Default is the current network ID. + * @type string $registered When the site was registered, in SQL datetime format. Default is + * the current time. + * @type string $last_updated When the site was last updated, in SQL datetime format. Default is + * the value of $registered. + * @type int $public Whether the site is public. Default 1. + * @type int $archived Whether the site is archived. Default 0. + * @type int $mature Whether the site is mature. Default 0. + * @type int $spam Whether the site is spam. Default 0. + * @type int $deleted Whether the site is deleted. Default 0. + * @type int $lang_id The site's language ID. Currently unused. Default 0. + * } + * @return int|WP_Error The new site's ID on success, or error object on failure. + */ +function wp_insert_site( array $data ) { + global $wpdb; + + $now = current_time( 'mysql', true ); + + $defaults = array( + 'domain' => '', + 'path' => '/', + 'network_id' => get_current_network_id(), + 'registered' => $now, + 'last_updated' => $now, + 'public' => 1, + 'archived' => 0, + 'mature' => 0, + 'spam' => 0, + 'deleted' => 0, + 'lang_id' => 0, + ); + + $data = wp_prepare_site_data( $data, $defaults ); + if ( is_wp_error( $data ) ) { + return $data; + } + + if ( false === $wpdb->insert( $wpdb->blogs, $data ) ) { + return new WP_Error( 'db_insert_error', __( 'Could not insert site into the database.' ), $wpdb->last_error ); + } + + $new_site = get_site( $wpdb->insert_id ); + + clean_blog_cache( $new_site ); + + /** + * Fires once a site has been inserted into the database. + * + * @since 5.0.0 + * + * @param WP_Site $new_site New site object. + */ + do_action( 'wp_insert_site', $new_site ); + + return (int) $new_site->id; +} + +/** + * Updates a site in the database. + * + * @since 5.0.0 + * + * @global wpdb $wpdb WordPress database abstraction object. + * + * @param int $site_id ID of the site that should be updated. + * @param array $data Site data to update. See {@see wp_insert_site()} for the list of supported keys. + * @return int|WP_Error The updated site's ID on success, or error object on failure. + */ +function wp_update_site( $site_id, array $data ) { + global $wpdb; + + if ( empty( $site_id ) ) { + return new WP_Error( 'site_empty_id', __( 'Site ID must not be empty.' ) ); + } + + $old_site = get_site( $site_id ); + if ( ! $old_site ) { + return new WP_Error( 'site_not_exist', __( 'Site does not exist.' ) ); + } + + $defaults = $old_site->to_array(); + $defaults['network_id'] = (int) $defaults['site_id']; + $defaults['last_updated'] = current_time( 'mysql', true ); + unset( $defaults['blog_id'], $defaults['site_id'] ); + + $data = wp_prepare_site_data( $data, $defaults, $old_site ); + if ( is_wp_error( $data ) ) { + return $data; + } + + if ( false === $wpdb->update( $wpdb->blogs, $data, array( 'blog_id' => $old_site->id ) ) ) { + return new WP_Error( 'db_update_error', __( 'Could not update site in the database.' ), $wpdb->last_error ); + } + + clean_blog_cache( $old_site ); + + $new_site = get_site( $old_site->id ); + + /** + * Fires once a site has been updated in the database. + * + * @since 5.0.0 + * + * @param WP_Site $new_site New site object. + * @param WP_Site $old_site Old site object. + */ + do_action( 'wp_update_site', $new_site, $old_site ); + + return (int) $new_site->id; +} + +/** + * Deletes a site from the database. + * + * @since 5.0.0 + * + * @global wpdb $wpdb WordPress database abstraction object. + * + * @param int $site_id ID of the site that should be deleted. + * @return WP_Site|WP_Error The deleted site object on success, or error object on failure. + */ +function wp_delete_site( $site_id ) { + global $wpdb; + + if ( empty( $site_id ) ) { + return new WP_Error( 'site_empty_id', __( 'Site ID must not be empty.' ) ); + } + + $old_site = get_site( $site_id ); + if ( ! $old_site ) { + return new WP_Error( 'site_not_exist', __( 'Site does not exist.' ) ); + } + + if ( false === $wpdb->delete( $wpdb->blogs, array( 'blog_id' => $old_site->id ) ) ) { + return new WP_Error( 'db_delete_error', __( 'Could not delete site from the database.' ), $wpdb->last_error ); + } + + clean_blog_cache( $old_site ); + + /** + * Fires once a site has been deleted from the database. + * + * @since 5.0.0 + * + * @param WP_Site $old_site Deleted site object. + */ + do_action( 'wp_delete_site', $old_site ); + + return $old_site; +} + /** * Retrieves site data given a site ID or site object. * @@ -687,6 +731,187 @@ function get_sites( $args = array() ) { return $query->query( $args ); } +/** + * Prepares site data for insertion or update in the database. + * + * @since 5.0.0 + * + * @param array $data Associative array of site data passed to the respective function. + * See {@see wp_insert_site()} for the possibly included data. + * @param array $defaults Site data defaults to parse $data against. + * @param WP_Site|null $old_site Optional. Old site object if an update, or null if an insertion. + * Default null. + * @return array|WP_Error Site data ready for a database transaction, or WP_Error in case a validation + * error occurred. + */ +function wp_prepare_site_data( $data, $defaults, $old_site = null ) { + + // Maintain backward-compatibility with `$site_id` as network ID. + if ( isset( $data['site_id'] ) ) { + if ( ! empty( $data['site_id'] ) && empty( $data['network_id'] ) ) { + $data['network_id'] = $data['site_id']; + } + unset( $data['site_id'] ); + } + + /** + * Filters passed site data in order to normalize it. + * + * @since 5.0.0 + * + * @param array $data Associative array of site data passed to the respective function. + * See {@see wp_insert_site()} for the possibly included data. + */ + $data = apply_filters( 'wp_normalize_site_data', $data ); + + $whitelist = array( 'domain', 'path', 'network_id', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' ); + $data = array_intersect_key( wp_parse_args( $data, $defaults ), array_flip( $whitelist ) ); + + $errors = new WP_Error(); + + /** + * Fires when data should be validated for a site prior to inserting or updating in the database. + * + * Plugins should amend the `$errors` object via its `WP_Error::add()` method. + * + * @since 5.0.0 + * + * @param WP_Error $errors Error object to add validation errors to. + * @param array $data Associative array of complete site data. See {@see wp_insert_site()} + * for the included data. + * @param WP_Site|null $old_site The old site object if the data belongs to a site being updated, + * or null if it is a new site being inserted. + */ + do_action( 'wp_validate_site_data', $errors, $data, $old_site ); + + if ( ! empty( $errors->errors ) ) { + return $errors; + } + + // Prepare for database. + $data['site_id'] = $data['network_id']; + unset( $data['network_id'] ); + + return $data; +} + +/** + * Normalizes data for a site prior to inserting or updating in the database. + * + * @since 5.0.0 + * + * @param array $data Associative array of site data passed to the respective function. + * See {@see wp_insert_site()} for the possibly included data. + * @return array Normalized site data. + */ +function wp_normalize_site_data( $data ) { + // Sanitize domain if passed. + if ( array_key_exists( 'domain', $data ) ) { + $data['domain'] = trim( $data['domain'] ); + $data['domain'] = preg_replace( '/\s+/', '', sanitize_user( $data['domain'], true ) ); + if ( is_subdomain_install() ) { + $data['domain'] = str_replace( '@', '', $data['domain'] ); + } + } + + // Sanitize path if passed. + if ( array_key_exists( 'path', $data ) ) { + $data['path'] = trailingslashit( '/' . trim( $data['path'], '/' ) ); + } + + // Sanitize network ID if passed. + if ( array_key_exists( 'network_id', $data ) ) { + $data['network_id'] = (int) $data['network_id']; + } + + // Sanitize status fields if passed. + $status_fields = array( 'public', 'archived', 'mature', 'spam', 'deleted' ); + foreach ( $status_fields as $status_field ) { + if ( array_key_exists( $status_field, $data ) ) { + $data[ $status_field ] = (int) $data[ $status_field ]; + } + } + + // Strip date fields if empty. + $date_fields = array( 'registered', 'last_updated' ); + foreach ( $date_fields as $date_field ) { + if ( ! array_key_exists( $date_field, $data ) ) { + continue; + } + + if ( empty( $data[ $date_field ] ) || '0000-00-00 00:00:00' === $data[ $date_field ] ) { + unset( $data[ $date_field ] ); + } + } + + return $data; +} + +/** + * Validates data for a site prior to inserting or updating in the database. + * + * @since 5.0.0 + * + * @param WP_Error $errors Error object, passed by reference. Will contain validation errors if + * any occurred. + * @param array $data Associative array of complete site data. See {@see wp_insert_site()} + * for the included data. + * @param WP_Site|null $old_site The old site object if the data belongs to a site being updated, + * or null if it is a new site being inserted. + */ +function wp_validate_site_data( $errors, $data, $old_site = null ) { + // A domain must always be present. + if ( empty( $data['domain'] ) ) { + $errors->add( 'site_empty_domain', __( 'Site domain must not be empty.' ) ); + } + + // A path must always be present. + if ( empty( $data['path'] ) ) { + $errors->add( 'site_empty_path', __( 'Site path must not be empty.' ) ); + } + + // A network ID must always be present. + if ( empty( $data['network_id'] ) ) { + $errors->add( 'site_empty_network_id', __( 'Site network ID must be provided.' ) ); + } + + // Both registration and last updated dates must always be present and valid. + $date_fields = array( 'registered', 'last_updated' ); + foreach ( $date_fields as $date_field ) { + if ( empty( $data[ $date_field ] ) ) { + $errors->add( 'site_empty_' . $date_field, __( 'Both registration and last updated dates must be provided.' ) ); + break; + } + + // Allow '0000-00-00 00:00:00', although it be stripped out at this point. + if ( '0000-00-00 00:00:00' !== $data[ $date_field ] ) { + $month = substr( $data[ $date_field ], 5, 2 ); + $day = substr( $data[ $date_field ], 8, 2 ); + $year = substr( $data[ $date_field ], 0, 4 ); + $valid_date = wp_checkdate( $month, $day, $year, $data[ $date_field ] ); + if ( ! $valid_date ) { + $errors->add( 'site_invalid_' . $date_field, __( 'Both registration and last updated dates must be valid dates.' ) ); + break; + } + } + } + + if ( ! empty( $errors->errors ) ) { + return; + } + + // If a new site, or domain/path/network ID have changed, ensure uniqueness. + if ( ! $old_site + || $data['domain'] !== $old_site->domain + || $data['path'] !== $old_site->path + || $data['network_id'] !== $old_site->network_id + ) { + if ( domain_exists( $data['domain'], $data['path'], $data['network_id'] ) ) { + $errors->add( 'site_taken', __( 'Sorry, that site already exists!' ) ); + } + } +} + /** * Retrieve option value for a given blog id based on name of option. * @@ -1193,6 +1418,7 @@ function update_archived( $id, $archived ) { * Update a blog details field. * * @since MU (3.0.0) + * @since 5.0.0 Use wp_update_site() internally. * * @global wpdb $wpdb WordPress database abstraction object. * @@ -1213,63 +1439,14 @@ function update_blog_status( $blog_id, $pref, $value, $deprecated = null ) { return $value; } - $result = $wpdb->update( - $wpdb->blogs, array( - $pref => $value, - 'last_updated' => current_time( 'mysql', true ), - ), array( 'blog_id' => $blog_id ) - ); + $result = wp_update_site( $blog_id, array( + $pref => $value, + ) ); - if ( false === $result ) { + if ( is_wp_error( $result ) ) { return false; } - clean_blog_cache( $blog_id ); - - if ( 'spam' == $pref ) { - if ( $value == 1 ) { - /** This filter is documented in wp-includes/ms-blogs.php */ - do_action( 'make_spam_blog', $blog_id ); - } else { - /** This filter is documented in wp-includes/ms-blogs.php */ - do_action( 'make_ham_blog', $blog_id ); - } - } elseif ( 'mature' == $pref ) { - if ( $value == 1 ) { - /** This filter is documented in wp-includes/ms-blogs.php */ - do_action( 'mature_blog', $blog_id ); - } else { - /** This filter is documented in wp-includes/ms-blogs.php */ - do_action( 'unmature_blog', $blog_id ); - } - } elseif ( 'archived' == $pref ) { - if ( $value == 1 ) { - /** This filter is documented in wp-includes/ms-blogs.php */ - do_action( 'archive_blog', $blog_id ); - } else { - /** This filter is documented in wp-includes/ms-blogs.php */ - do_action( 'unarchive_blog', $blog_id ); - } - } elseif ( 'deleted' == $pref ) { - if ( $value == 1 ) { - /** This filter is documented in wp-includes/ms-blogs.php */ - do_action( 'make_delete_blog', $blog_id ); - } else { - /** This filter is documented in wp-includes/ms-blogs.php */ - do_action( 'make_undelete_blog', $blog_id ); - } - } elseif ( 'public' == $pref ) { - /** - * Fires after the current blog's 'public' setting is updated. - * - * @since MU (3.0.0) - * - * @param int $blog_id Blog ID. - * @param string $value The value of blog status. - */ - do_action( 'update_blog_public', $blog_id, $value ); // Moved here from update_blog_public(). - } - return $value; } @@ -1537,3 +1714,177 @@ function _update_posts_count_on_transition_post_status( $new_status, $old_status update_posts_count(); } + +/** + * Updates the count of sites for a network based on a changed site. + * + * @since 5.0.0 + * + * @param WP_Site $new_site The site object that has been inserted, updated or deleted. + * @param WP_Site|null $old_site Optional. If $new_site has been updated, this must be the previous + * state of that site. Default null. + */ +function wp_maybe_update_network_site_counts_on_update( $new_site, $old_site = null ) { + if ( null === $old_site ) { + wp_maybe_update_network_site_counts( $new_site->network_id ); + return; + } + + if ( $new_site->network_id != $old_site->network_id ) { + wp_maybe_update_network_site_counts( $new_site->network_id ); + wp_maybe_update_network_site_counts( $old_site->network_id ); + } +} + +/** + * Triggers actions on site status updates. + * + * @since 5.0.0 + * + * @param WP_Site $new_site The site object after the update. + * @param WP_Site|null $old_site Optional. If $new_site has been updated, this must be the previous + * state of that site. Default null. + */ +function wp_maybe_transition_site_statuses_on_update( $new_site, $old_site = null ) { + $site_id = $new_site->id; + + // Use the default values for a site if no previous state is given. + if ( ! $old_site ) { + $old_site = new WP_Site( new stdClass() ); + } + + if ( $new_site->spam != $old_site->spam ) { + if ( $new_site->spam == 1 ) { + + /** + * Fires when the 'spam' status is added to a site. + * + * @since MU (3.0.0) + * + * @param int $site_id Site ID. + */ + do_action( 'make_spam_blog', $site_id ); + } else { + + /** + * Fires when the 'spam' status is removed from a site. + * + * @since MU (3.0.0) + * + * @param int $site_id Site ID. + */ + do_action( 'make_ham_blog', $site_id ); + } + } + + if ( $new_site->mature != $old_site->mature ) { + if ( $new_site->mature == 1 ) { + + /** + * Fires when the 'mature' status is added to a site. + * + * @since 3.1.0 + * + * @param int $site_id Site ID. + */ + do_action( 'mature_blog', $site_id ); + } else { + + /** + * Fires when the 'mature' status is removed from a site. + * + * @since 3.1.0 + * + * @param int $site_id Site ID. + */ + do_action( 'unmature_blog', $site_id ); + } + } + + if ( $new_site->archived != $old_site->archived ) { + if ( $new_site->archived == 1 ) { + + /** + * Fires when the 'archived' status is added to a site. + * + * @since MU (3.0.0) + * + * @param int $site_id Site ID. + */ + do_action( 'archive_blog', $site_id ); + } else { + + /** + * Fires when the 'archived' status is removed from a site. + * + * @since MU (3.0.0) + * + * @param int $site_id Site ID. + */ + do_action( 'unarchive_blog', $site_id ); + } + } + + if ( $new_site->deleted != $old_site->deleted ) { + if ( $new_site->deleted == 1 ) { + + /** + * Fires when the 'deleted' status is added to a site. + * + * @since 3.5.0 + * + * @param int $site_id Site ID. + */ + do_action( 'make_delete_blog', $site_id ); + } else { + + /** + * Fires when the 'deleted' status is removed from a site. + * + * @since 3.5.0 + * + * @param int $site_id Site ID. + */ + do_action( 'make_undelete_blog', $site_id ); + } + } + + if ( $new_site->public != $old_site->public ) { + + /** + * Fires after the current blog's 'public' setting is updated. + * + * @since MU (3.0.0) + * + * @param int $site_id Site ID. + * @param string $value The value of the site status. + */ + do_action( 'update_blog_public', $site_id, $new_site->public ); + } +} + +/** + * Cleans the necessary caches after specific site data has been updated. + * + * @since 5.0.0 + * + * @param WP_Site $new_site The site object after the update. + * @param WP_Site $old_site The site obejct prior to the update. + */ +function wp_maybe_clean_new_site_cache_on_update( $new_site, $old_site ) { + if ( $old_site->domain !== $new_site->domain || $old_site->path !== $new_site->path ) { + clean_blog_cache( $new_site ); + } +} + +/** + * Updates the `blog_public` option for a given site ID. + * + * @since 5.0.0 + * + * @param int $site_id Site ID. + * @param string $public The value of the site status. + */ +function wp_update_blog_public_option_on_site_update( $site_id, $public ) { + update_blog_option( $site_id, 'blog_public', $public ); +} diff --git a/src/wp-includes/ms-default-filters.php b/src/wp-includes/ms-default-filters.php index d0e2866252..4542eccf1f 100644 --- a/src/wp-includes/ms-default-filters.php +++ b/src/wp-includes/ms-default-filters.php @@ -41,6 +41,15 @@ add_action( 'wpmu_new_blog', 'wpmu_log_new_registrations', 10, 2 ); add_action( 'wpmu_new_blog', 'newblog_notify_siteadmin', 10, 2 ); add_action( 'wpmu_activate_blog', 'wpmu_welcome_notification', 10, 5 ); add_action( 'after_signup_site', 'wpmu_signup_blog_notification', 10, 7 ); +add_filter( 'wp_normalize_site_data', 'wp_normalize_site_data', 10, 1 ); +add_action( 'wp_validate_site_data', 'wp_validate_site_data', 10, 3 ); +add_action( 'wp_insert_site', 'wp_maybe_update_network_site_counts_on_update', 10, 1 ); +add_action( 'wp_update_site', 'wp_maybe_update_network_site_counts_on_update', 10, 2 ); +add_action( 'wp_delete_site', 'wp_maybe_update_network_site_counts_on_update', 10, 1 ); +add_action( 'wp_insert_site', 'wp_maybe_transition_site_statuses_on_update', 10, 1 ); +add_action( 'wp_update_site', 'wp_maybe_transition_site_statuses_on_update', 10, 2 ); +add_action( 'wp_update_site', 'wp_maybe_clean_new_site_cache_on_update', 10, 2 ); +add_action( 'update_blog_public', 'wp_update_blog_public_option_on_site_update', 1, 2 ); // Register Nonce add_action( 'signup_hidden_fields', 'signup_nonce_fields' ); diff --git a/src/wp-includes/ms-deprecated.php b/src/wp-includes/ms-deprecated.php index e1f917678d..2b9ff493bb 100644 --- a/src/wp-includes/ms-deprecated.php +++ b/src/wp-includes/ms-deprecated.php @@ -546,3 +546,37 @@ function is_user_option_local( $key, $user_id = 0, $blog_id = 0 ) { return isset( $current_user->$local_key ); } + +/** + * Store basic site info in the blogs table. + * + * This function creates a row in the wp_blogs table and returns + * the new blog's ID. It is the first step in creating a new blog. + * + * @since MU (3.0.0) + * @deprecated 5.0.0 Use `wp_insert_site()` + * @see wp_insert_site() + * + * @param string $domain The domain of the new site. + * @param string $path The path of the new site. + * @param int $site_id Unless you're running a multi-network install, be sure to set this value to 1. + * @return int|false The ID of the new row + */ +function insert_blog($domain, $path, $site_id) { + _deprecated_function( __FUNCTION__, '5.0.0', 'wp_insert_site()' ); + + $data = array( + 'domain' => $domain, + 'path' => $path, + 'site_id' => $site_id, + ); + + $site_id = wp_insert_site( $data ); + if ( is_wp_error( $site_id ) ) { + return false; + } + + clean_blog_cache( $site_id ); + + return $site_id; +} diff --git a/src/wp-includes/ms-functions.php b/src/wp-includes/ms-functions.php index f2dbbc750f..6e5233222b 100644 --- a/src/wp-includes/ms-functions.php +++ b/src/wp-includes/ms-functions.php @@ -1273,19 +1273,9 @@ function wpmu_create_blog( $domain, $path, $title, $user_id, $meta = array(), $n ); $meta = wp_parse_args( $meta, $defaults ); - $domain = preg_replace( '/\s+/', '', sanitize_user( $domain, true ) ); - - if ( is_subdomain_install() ) { - $domain = str_replace( '@', '', $domain ); - } - $title = strip_tags( $title ); $user_id = (int) $user_id; - if ( empty( $path ) ) { - $path = '/'; - } - // Check if the domain has been used already. We should return an error message. if ( domain_exists( $domain, $path, $network_id ) ) { return new WP_Error( 'blog_taken', __( 'Sorry, that site already exists!' ) ); @@ -1295,8 +1285,28 @@ function wpmu_create_blog( $domain, $path, $title, $user_id, $meta = array(), $n wp_installing( true ); } - if ( ! $blog_id = insert_blog( $domain, $path, $network_id ) ) { - return new WP_Error( 'insert_blog', __( 'Could not create site.' ) ); + $site_data_whitelist = array( 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' ); + + $site_data = array_merge( + array( + 'domain' => $domain, + 'path' => $path, + 'network_id' => $network_id, + ), + array_intersect_key( + $meta, + array_flip( $site_data_whitelist ) + ) + ); + + $meta = array_diff_key( $meta, array_flip( $site_data_whitelist ) ); + + remove_action( 'update_blog_public', 'wp_update_blog_public_option_on_site_update', 1 ); + $blog_id = wp_insert_site( $site_data ); + add_action( 'update_blog_public', 'wp_update_blog_public_option_on_site_update', 1, 2 ); + + if ( is_wp_error( $blog_id ) ) { + return $blog_id; } switch_to_blog( $blog_id ); @@ -1306,20 +1316,19 @@ function wpmu_create_blog( $domain, $path, $title, $user_id, $meta = array(), $n add_user_to_blog( $blog_id, $user_id, 'administrator' ); foreach ( $meta as $key => $value ) { - if ( in_array( $key, array( 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' ) ) ) { - update_blog_status( $blog_id, $key, $value ); - } else { - update_option( $key, $value ); - } + update_option( $key, $value ); } - update_option( 'blog_public', (int) $meta['public'] ); + update_option( 'blog_public', (int) $site_data['public'] ); if ( ! is_super_admin( $user_id ) && ! get_user_meta( $user_id, 'primary_blog', true ) ) { update_user_meta( $user_id, 'primary_blog', $blog_id ); } restore_current_blog(); + + $site = get_site( $blog_id ); + /** * Fires immediately after a new site is created. * @@ -1332,7 +1341,7 @@ function wpmu_create_blog( $domain, $path, $title, $user_id, $meta = array(), $n * @param int $network_id Network ID. Only relevant on multi-network installations. * @param array $meta Meta data. Used to set initial site options. */ - do_action( 'wpmu_new_blog', $blog_id, $user_id, $domain, $path, $network_id, $meta ); + do_action( 'wpmu_new_blog', $blog_id, $user_id, $site->domain, $site->path, $site->network_id, $meta ); wp_cache_set( 'last_changed', microtime(), 'sites' ); @@ -1485,47 +1494,6 @@ function domain_exists( $domain, $path, $network_id = 1 ) { return apply_filters( 'domain_exists', $result, $domain, $path, $network_id ); } -/** - * Store basic site info in the blogs table. - * - * This function creates a row in the wp_blogs table and returns - * the new blog's ID. It is the first step in creating a new blog. - * - * @since MU (3.0.0) - * - * @global wpdb $wpdb WordPress database abstraction object. - * - * @param string $domain The domain of the new site. - * @param string $path The path of the new site. - * @param int $network_id Unless you're running a multi-network installation, be sure to set this value to 1. - * @return int|false The ID of the new row - */ -function insert_blog( $domain, $path, $network_id ) { - global $wpdb; - - $path = trailingslashit( $path ); - $network_id = (int) $network_id; - - $result = $wpdb->insert( - $wpdb->blogs, array( - 'site_id' => $network_id, - 'domain' => $domain, - 'path' => $path, - 'registered' => current_time( 'mysql' ), - ) - ); - if ( ! $result ) { - return false; - } - - $blog_id = $wpdb->insert_id; - clean_blog_cache( $blog_id ); - - wp_maybe_update_network_site_counts( $network_id ); - - return $blog_id; -} - /** * Install an empty blog. * @@ -1538,7 +1506,7 @@ function insert_blog( $domain, $path, $network_id ) { * @global wpdb $wpdb * @global WP_Roles $wp_roles * - * @param int $blog_id The value returned by insert_blog(). + * @param int $blog_id The value returned by wp_insert_site(). * @param string $blog_title The title of the new site. */ function install_blog( $blog_id, $blog_title = '' ) { diff --git a/tests/phpunit/tests/multisite/site.php b/tests/phpunit/tests/multisite/site.php index 6971f034c6..802d371312 100644 --- a/tests/phpunit/tests/multisite/site.php +++ b/tests/phpunit/tests/multisite/site.php @@ -10,6 +10,7 @@ if ( is_multisite() ) : */ class Tests_Multisite_Site extends WP_UnitTestCase { protected $suppress = false; + protected $site_status_hooks = array(); protected static $network_ids; protected static $site_ids; @@ -446,12 +447,12 @@ if ( is_multisite() ) : $this->assertEquals( '0', $blog->spam ); $this->assertEquals( 1, $test_action_counter ); - // The action should fire if the status of 'spam' stays the same. + // The action should not fire if the status of 'spam' stays the same. update_blog_status( $blog_id, 'spam', 0 ); $blog = get_site( $blog_id ); $this->assertEquals( '0', $blog->spam ); - $this->assertEquals( 2, $test_action_counter ); + $this->assertEquals( 1, $test_action_counter ); remove_action( 'make_ham_blog', array( $this, '_action_counter_cb' ), 10 ); } @@ -469,12 +470,12 @@ if ( is_multisite() ) : $this->assertEquals( '1', $blog->spam ); $this->assertEquals( 1, $test_action_counter ); - // The action should fire if the status of 'spam' stays the same. + // The action should not fire if the status of 'spam' stays the same. update_blog_status( $blog_id, 'spam', 1 ); $blog = get_site( $blog_id ); $this->assertEquals( '1', $blog->spam ); - $this->assertEquals( 2, $test_action_counter ); + $this->assertEquals( 1, $test_action_counter ); remove_action( 'make_spam_blog', array( $this, '_action_counter_cb' ), 10 ); } @@ -492,12 +493,12 @@ if ( is_multisite() ) : $this->assertEquals( '1', $blog->archived ); $this->assertEquals( 1, $test_action_counter ); - // The action should fire if the status of 'archived' stays the same. + // The action should not fire if the status of 'archived' stays the same. update_blog_status( $blog_id, 'archived', 1 ); $blog = get_site( $blog_id ); $this->assertEquals( '1', $blog->archived ); - $this->assertEquals( 2, $test_action_counter ); + $this->assertEquals( 1, $test_action_counter ); remove_action( 'archive_blog', array( $this, '_action_counter_cb' ), 10 ); } @@ -516,11 +517,11 @@ if ( is_multisite() ) : $this->assertEquals( '0', $blog->archived ); $this->assertEquals( 1, $test_action_counter ); - // The action should fire if the status of 'archived' stays the same. + // The action should not fire if the status of 'archived' stays the same. update_blog_status( $blog_id, 'archived', 0 ); $blog = get_site( $blog_id ); $this->assertEquals( '0', $blog->archived ); - $this->assertEquals( 2, $test_action_counter ); + $this->assertEquals( 1, $test_action_counter ); remove_action( 'unarchive_blog', array( $this, '_action_counter_cb' ), 10 ); } @@ -538,12 +539,12 @@ if ( is_multisite() ) : $this->assertEquals( '1', $blog->deleted ); $this->assertEquals( 1, $test_action_counter ); - // The action should fire if the status of 'deleted' stays the same. + // The action should not fire if the status of 'deleted' stays the same. update_blog_status( $blog_id, 'deleted', 1 ); $blog = get_site( $blog_id ); $this->assertEquals( '1', $blog->deleted ); - $this->assertEquals( 2, $test_action_counter ); + $this->assertEquals( 1, $test_action_counter ); remove_action( 'make_delete_blog', array( $this, '_action_counter_cb' ), 10 ); } @@ -562,12 +563,12 @@ if ( is_multisite() ) : $this->assertEquals( '0', $blog->deleted ); $this->assertEquals( 1, $test_action_counter ); - // The action should fire if the status of 'deleted' stays the same. + // The action should not fire if the status of 'deleted' stays the same. update_blog_status( $blog_id, 'deleted', 0 ); $blog = get_site( $blog_id ); $this->assertEquals( '0', $blog->deleted ); - $this->assertEquals( 2, $test_action_counter ); + $this->assertEquals( 1, $test_action_counter ); remove_action( 'make_undelete_blog', array( $this, '_action_counter_cb' ), 10 ); } @@ -585,12 +586,12 @@ if ( is_multisite() ) : $this->assertEquals( '1', $blog->mature ); $this->assertEquals( 1, $test_action_counter ); - // The action should fire if the status of 'mature' stays the same. + // The action should not fire if the status of 'mature' stays the same. update_blog_status( $blog_id, 'mature', 1 ); $blog = get_site( $blog_id ); $this->assertEquals( '1', $blog->mature ); - $this->assertEquals( 2, $test_action_counter ); + $this->assertEquals( 1, $test_action_counter ); remove_action( 'mature_blog', array( $this, '_action_counter_cb' ), 10 ); } @@ -609,12 +610,12 @@ if ( is_multisite() ) : $this->assertEquals( '0', $blog->mature ); $this->assertEquals( 1, $test_action_counter ); - // The action should fire if the status of 'mature' stays the same. + // The action should not fire if the status of 'mature' stays the same. update_blog_status( $blog_id, 'mature', 0 ); $blog = get_site( $blog_id ); $this->assertEquals( '0', $blog->mature ); - $this->assertEquals( 2, $test_action_counter ); + $this->assertEquals( 1, $test_action_counter ); remove_action( 'unmature_blog', array( $this, '_action_counter_cb' ), 10 ); } @@ -632,12 +633,12 @@ if ( is_multisite() ) : $this->assertEquals( '0', $blog->public ); $this->assertEquals( 1, $test_action_counter ); - // The action should fire if the status of 'mature' stays the same. + // The action should not fire if the status of 'mature' stays the same. update_blog_status( $blog_id, 'public', 0 ); $blog = get_site( $blog_id ); $this->assertEquals( '0', $blog->public ); - $this->assertEquals( 2, $test_action_counter ); + $this->assertEquals( 1, $test_action_counter ); remove_action( 'update_blog_public', array( $this, '_action_counter_cb' ), 10 ); } @@ -1259,6 +1260,738 @@ if ( is_multisite() ) : array( 'current_blog_%domain%%path%', 'site-options' ), ); } + + /** + * @ticket 40364 + * @dataProvider data_wp_insert_site + */ + public function test_wp_insert_site( $site_data, $expected_data ) { + $site_id = wp_insert_site( $site_data ); + + $this->assertInternalType( 'integer', $site_id ); + + $site = get_site( $site_id ); + foreach ( $expected_data as $key => $value ) { + $this->assertEquals( $value, $site->$key ); + } + } + + public function data_wp_insert_site() { + return array( + array( + array( + 'domain' => 'example.com', + ), + array( + 'domain' => 'example.com', + 'path' => '/', + 'network_id' => 1, + 'public' => 1, + 'archived' => 0, + 'mature' => 0, + 'spam' => 0, + 'deleted' => 0, + 'lang_id' => 0, + ), + ), + array( + array( + 'domain' => 'example.com', + 'path' => '/foo', + 'network_id' => 2, + ), + array( + 'domain' => 'example.com', + 'path' => '/foo/', + 'network_id' => 2, + ), + ), + array( + array( + 'domain' => 'example.com', + 'path' => '/bar/', + 'site_id' => 2, + ), + array( + 'domain' => 'example.com', + 'path' => '/bar/', + 'network_id' => 2, + ), + ), + array( + array( + 'domain' => 'example.com', + 'path' => '/bar/', + 'site_id' => 2, + 'network_id' => 3, + ), + array( + 'domain' => 'example.com', + 'path' => '/bar/', + 'network_id' => 3, + ), + ), + array( + array( + 'domain' => 'example.com', + 'path' => 'foobar', + 'public' => 0, + 'archived' => 1, + 'mature' => 1, + 'spam' => 1, + 'deleted' => 1, + 'lang_id' => 1, + ), + array( + 'domain' => 'example.com', + 'path' => '/foobar/', + 'public' => 0, + 'archived' => 1, + 'mature' => 1, + 'spam' => 1, + 'deleted' => 1, + 'lang_id' => 1, + ), + ), + ); + } + + /** + * @ticket 40364 + */ + public function test_wp_insert_site_empty_domain() { + $site_id = wp_insert_site( array( 'public' => 0 ) ); + + $this->assertWPError( $site_id ); + $this->assertSame( 'site_empty_domain', $site_id->get_error_code() ); + } + + /** + * @ticket 40364 + * @dataProvider data_wp_update_site + */ + public function test_wp_update_site( $site_data, $expected_data ) { + $site_id = self::factory()->blog->create(); + + $old_site = get_site( $site_id ); + + $result = wp_update_site( $site_id, $site_data ); + + $this->assertSame( $site_id, $result ); + + $new_site = get_site( $site_id ); + foreach ( $new_site->to_array() as $key => $value ) { + if ( isset( $expected_data[ $key ] ) ) { + $this->assertEquals( $expected_data[ $key ], $value ); + } elseif ( 'last_updated' === $key ) { + $this->assertTrue( $old_site->last_updated <= $value ); + } else { + $this->assertEquals( $old_site->$key, $value ); + } + } + } + + public function data_wp_update_site() { + return array( + array( + array( + 'domain' => 'example.com', + 'network_id' => 2, + ), + array( + 'domain' => 'example.com', + 'site_id' => 2, + ), + ), + array( + array( + 'path' => 'foo', + ), + array( + 'path' => '/foo/', + ), + ), + array( + array( + 'public' => 0, + 'archived' => 1, + 'mature' => 1, + 'spam' => 1, + 'deleted' => 1, + 'lang_id' => 1, + ), + array( + 'public' => 0, + 'archived' => 1, + 'mature' => 1, + 'spam' => 1, + 'deleted' => 1, + 'lang_id' => 1, + ), + ), + ); + } + + /** + * @ticket 40364 + */ + public function test_wp_update_site_empty_domain() { + $site_id = self::factory()->blog->create(); + + $result = wp_update_site( $site_id, array( 'domain' => '' ) ); + + $this->assertWPError( $result ); + $this->assertSame( 'site_empty_domain', $result->get_error_code() ); + } + + /** + * @ticket 40364 + */ + public function test_wp_update_site_invalid_id() { + $result = wp_update_site( 444444, array( 'domain' => 'example.com' ) ); + + $this->assertWPError( $result ); + $this->assertSame( 'site_not_exist', $result->get_error_code() ); + } + + /** + * @ticket 40364 + */ + public function test_wp_update_site_cleans_cache() { + $site_id = self::factory()->blog->create(); + $site1 = get_site( $site_id ); + + $result = wp_update_site( $site_id, array( 'public' => 0 ) ); + $site2 = get_site( $site_id ); + + $result = wp_update_site( $site_id, array( 'public' => 1 ) ); + $site3 = get_site( $site_id ); + + $this->assertEquals( 1, $site1->public ); + $this->assertEquals( 0, $site2->public ); + $this->assertEquals( 1, $site3->public ); + } + + /** + * @ticket 40364 + */ + public function test_wp_delete_site() { + $site_id = self::factory()->blog->create(); + + $site = get_site( $site_id ); + + $result = wp_delete_site( $site_id ); + + $this->assertInstanceOf( 'WP_Site', $result ); + $this->assertEquals( $result->to_array(), $site->to_array() ); + } + + /** + * @ticket 40364 + */ + public function test_wp_delete_site_invalid_id() { + $result = wp_delete_site( 444444 ); + + $this->assertWPError( $result ); + $this->assertSame( 'site_not_exist', $result->get_error_code() ); + } + + /** + * @ticket 40364 + * @dataProvider data_wp_normalize_site_data + */ + public function test_wp_normalize_site_data( $data, $expected ) { + $result = wp_normalize_site_data( $data ); + + $this->assertEqualSetsWithIndex( $expected, $result ); + } + + public function data_wp_normalize_site_data() { + return array( + array( + array( + 'network_id' => '4', + ), + array( + 'network_id' => 4, + ), + ), + array( + array( + 'domain' => 'invalid domain .com', + 'path' => 'foo', + ), + array( + 'domain' => 'invaliddomain.com', + 'path' => '/foo/', + ), + ), + array( + array( + 'domain' => '/another-invalid-domain.com', + ), + array( + 'domain' => 'another-invalid-domain.com', + ), + ), + array( + array( + 'path' => '', + ), + array( + 'path' => '/', + ), + ), + array( + array( + 'public' => '0', + 'archived' => '1', + 'mature' => '1', + 'spam' => true, + 'deleted' => true, + ), + array( + 'public' => 0, + 'archived' => 1, + 'mature' => 1, + 'spam' => 1, + 'deleted' => 1, + ), + ), + array( + array( + 'registered' => '', + 'last_updated' => '', + ), + array(), + ), + array( + array( + 'registered' => '0000-00-00 00:00:00', + 'last_updated' => '0000-00-00 00:00:00', + ), + array(), + ), + ); + } + + /** + * @ticket 40364 + * @dataProvider data_wp_validate_site_data + */ + public function test_wp_validate_site_data( $data, $expected_errors ) { + $result = new WP_Error(); + wp_validate_site_data( $result, $data ); + + if ( empty( $expected_errors ) ) { + $this->assertEmpty( $result->errors ); + } else { + $this->assertEqualSets( $expected_errors, array_keys( $result->errors ) ); + } + } + + public function data_wp_validate_site_data() { + $date = current_time( 'mysql', true ); + + return array( + array( + array( + 'domain' => 'example-site.com', + 'path' => '/', + 'network_id' => 1, + 'registered' => $date, + 'last_updated' => $date, + ), + array(), + ), + array( + array( + 'path' => '/', + 'network_id' => 1, + 'registered' => $date, + 'last_updated' => $date, + ), + array( 'site_empty_domain' ), + ), + array( + array( + 'domain' => 'example-site.com', + 'network_id' => 1, + 'registered' => $date, + 'last_updated' => $date, + ), + array( 'site_empty_path' ), + ), + array( + array( + 'domain' => 'example-site.com', + 'path' => '/', + 'registered' => $date, + 'last_updated' => $date, + ), + array( 'site_empty_network_id' ), + ), + array( + array( + 'domain' => get_site()->domain, + 'path' => get_site()->path, + 'network_id' => get_site()->network_id, + 'registered' => $date, + 'last_updated' => $date, + ), + array( 'site_taken' ), + ), + array( + array( + 'domain' => 'valid-domain.com', + 'path' => '/valid-path/', + 'network_id' => 1, + 'registered' => '', + 'last_updated' => $date, + ), + array( 'site_empty_registered' ), + ), + array( + array( + 'domain' => 'valid-domain.com', + 'path' => '/valid-path/', + 'network_id' => 1, + 'registered' => $date, + 'last_updated' => '', + ), + array( 'site_empty_last_updated' ), + ), + array( + array( + 'domain' => 'valid-domain.com', + 'path' => '/valid-path/', + 'network_id' => 1, + 'registered' => '2000-13-32 25:25:61', + 'last_updated' => $date, + ), + array( 'site_invalid_registered' ), + ), + array( + array( + 'domain' => 'valid-domain.com', + 'path' => '/valid-path/', + 'network_id' => 1, + 'registered' => $date, + 'last_updated' => '2000-13-32 25:25:61', + ), + array( 'site_invalid_last_updated' ), + ), + array( + array( + 'domain' => 'valid-domain.com', + 'path' => '/valid-path/', + 'network_id' => 1, + 'registered' => '0000-00-00 00:00:00', + 'last_updated' => $date, + ), + array(), + ), + array( + array( + 'domain' => 'valid-domain.com', + 'path' => '/valid-path/', + 'network_id' => 1, + 'registered' => $date, + 'last_updated' => '0000-00-00 00:00:00', + ), + array(), + ), + ); + } + + /** + * @ticket 40364 + */ + public function test_site_dates_are_gmt() { + $first_date = current_time( 'mysql', true ); + $site_id = wp_insert_site( array( + 'domain' => 'valid-domain.com', + 'path' => '/valid-path/', + 'network_id' => 1, + ) ); + $this->assertInternalType( 'integer', $site_id ); + + $site = get_site( $site_id ); + $this->assertSame( $first_date, $site->registered ); + $this->assertSame( $first_date, $site->last_updated ); + + $second_date = current_time( 'mysql', true ); + $site_id = wp_update_site( $site_id, array() ); + $this->assertInternalType( 'integer', $site_id ); + + $site = get_site( $site_id ); + $this->assertSame( $first_date, $site->registered ); + $this->assertSame( $second_date, $site->last_updated ); + } + + /** + * @ticket 40364 + */ + public function test_wp_delete_site_cleans_cache() { + $site_id = self::factory()->blog->create(); + + get_site( $site_id ); + + wp_delete_site( $site_id ); + + $this->assertNull( get_site( $site_id ) ); + } + + /** + * @ticket 40364 + */ + public function test_wp_update_site_cleans_old_cache_on_domain_change() { + $old_domain = 'old.wordpress.org'; + $new_domain = 'new.wordpress.org'; + + $site = self::factory()->blog->create_and_get( array( + 'domain' => $old_domain, + 'path' => '/', + ) ); + + // Populate the caches. + get_blog_details( array( + 'domain' => $old_domain, + 'path' => '/', + ) ); + get_blog_id_from_url( $old_domain, '/' ); + get_blog_details( array( + 'domain' => $new_domain, + 'path' => '/', + ) ); + get_blog_id_from_url( $new_domain, '/' ); + + wp_update_site( $site->id, array( + 'domain' => $new_domain, + ) ); + + $domain_path_key_old = md5( $old_domain . '/' ); + $domain_path_key_new = md5( $new_domain . '/' ); + + // Ensure all respective cache values are empty. + $result = array( + wp_cache_get( $domain_path_key_old, 'blog-lookup' ), + wp_cache_get( $domain_path_key_old, 'blog-id-cache' ), + wp_cache_get( 'current_blog_' . $old_domain, 'site-options' ), + wp_cache_get( 'current_blog_' . $old_domain . '/', 'site-options' ), + wp_cache_get( $domain_path_key_new, 'blog-lookup' ), + wp_cache_get( $domain_path_key_new, 'blog-id-cache' ), + wp_cache_get( 'current_blog_' . $new_domain, 'site-options' ), + wp_cache_get( 'current_blog_' . $new_domain . '/', 'site-options' ), + ); + + $this->assertEmpty( array_filter( $result ) ); + } + + /** + * @ticket 40364 + */ + public function test_wp_update_site_cleans_old_cache_on_path_change() { + $old_path = '/foo/'; + $new_path = '/bar/'; + + $site = self::factory()->blog->create_and_get( array( + 'domain' => 'test.wordpress.org', + 'path' => $old_path, + ) ); + + // Populate the caches. + get_blog_details( array( + 'domain' => 'test.wordpress.org', + 'path' => $old_path, + ) ); + get_blog_id_from_url( 'test.wordpress.org', $old_path ); + get_blog_details( array( + 'domain' => 'test.wordpress.org', + 'path' => $new_path, + ) ); + get_blog_id_from_url( 'test.wordpress.org', $new_path ); + + wp_update_site( $site->id, array( + 'path' => $new_path, + ) ); + + $domain_path_key_old = md5( 'test.wordpress.org' . $old_path ); + $domain_path_key_new = md5( 'test.wordpress.org' . $new_path ); + + // Ensure all respective cache values are empty. + $result = array( + wp_cache_get( $domain_path_key_old, 'blog-lookup' ), + wp_cache_get( $domain_path_key_old, 'blog-id-cache' ), + wp_cache_get( 'current_blog_test.wordpress.org' . $old_path, 'site-options' ), + wp_cache_get( $domain_path_key_new, 'blog-lookup' ), + wp_cache_get( $domain_path_key_new, 'blog-id-cache' ), + wp_cache_get( 'current_blog_test.wordpress.org' . $new_path, 'site-options' ), + ); + + $this->assertEmpty( array_filter( $result ) ); + } + + /** + * @ticket 40364 + * @dataProvider data_site_status_hook_triggers + */ + public function test_site_status_hook_triggers( $insert_site_data, $expected_insert_hooks, $update_site_data, $expected_update_hooks ) { + // First: Insert a site. + $this->listen_to_site_status_hooks(); + + $site_data = array_merge( array( + 'domain' => 'example-site.com', + 'path' => '/', + ), $insert_site_data ); + + $site_id = wp_insert_site( $site_data ); + + $insert_expected = array_fill_keys( $expected_insert_hooks, $site_id ); + $insert_result = $this->get_listen_to_site_status_hooks_result(); + + // Second: Update that site. + $this->listen_to_site_status_hooks(); + + wp_update_site( $site_id, $update_site_data ); + + $update_expected = array_fill_keys( $expected_update_hooks, $site_id ); + $update_result = $this->get_listen_to_site_status_hooks_result(); + + // Check both insert and update results. + $this->assertEqualSetsWithIndex( $insert_expected, $insert_result ); + $this->assertEqualSetsWithIndex( $update_expected, $update_result ); + } + + public function data_site_status_hook_triggers() { + return array( + array( + array( + 'public' => 1, + 'archived' => 1, + 'mature' => 1, + 'spam' => 1, + 'deleted' => 1, + ), + array( + 'archive_blog', + 'mature_blog', + 'make_spam_blog', + 'make_delete_blog', + ), + array( + 'public' => 0, + 'archived' => 0, + 'mature' => 0, + 'spam' => 0, + 'deleted' => 0, + ), + array( + 'update_blog_public', + 'unarchive_blog', + 'unmature_blog', + 'make_ham_blog', + 'make_undelete_blog', + ), + ), + array( + array( + 'public' => 0, + 'archived' => 0, + 'mature' => 0, + 'spam' => 0, + 'deleted' => 0, + ), + array( + 'update_blog_public', + ), + array( + 'public' => 1, + 'archived' => 1, + 'mature' => 1, + 'spam' => 1, + 'deleted' => 1, + ), + array( + 'update_blog_public', + 'archive_blog', + 'mature_blog', + 'make_spam_blog', + 'make_delete_blog', + ), + ), + array( + array( + 'public' => 0, + 'archived' => 0, + 'mature' => 1, + 'spam' => 1, + 'deleted' => 1, + ), + array( + 'update_blog_public', + 'mature_blog', + 'make_spam_blog', + 'make_delete_blog', + ), + array( + 'public' => 0, + 'archived' => 1, + 'mature' => 1, + 'spam' => 1, + 'deleted' => 0, + ), + array( + 'archive_blog', + 'make_undelete_blog', + ), + ), + ); + } + + private function listen_to_site_status_hooks() { + $this->site_status_hooks = array(); + + $hooknames = array( + 'make_spam_blog', + 'make_ham_blog', + 'mature_blog', + 'unmature_blog', + 'archive_blog', + 'unarchive_blog', + 'make_delete_blog', + 'make_undelete_blog', + 'update_blog_public', + ); + + foreach ( $hooknames as $hookname ) { + add_action( $hookname, array( $this, 'action_site_status_hook' ), 10, 1 ); + } + } + + private function get_listen_to_site_status_hooks_result() { + $hooknames = array( + 'make_spam_blog', + 'make_ham_blog', + 'mature_blog', + 'unmature_blog', + 'archive_blog', + 'unarchive_blog', + 'make_delete_blog', + 'make_undelete_blog', + 'update_blog_public', + ); + + foreach ( $hooknames as $hookname ) { + remove_action( $hookname, array( $this, 'action_site_status_hook' ), 10 ); + } + + return $this->site_status_hooks; + } + + public function action_site_status_hook( $site_id ) { + $this->site_status_hooks[ current_action() ] = $site_id; + } } endif;