<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[43548] trunk: Multisite: Complete the new CRUD API for managing sites.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/43548">43548</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/43548","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>flixos90</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2018-08-01 13:05:44 +0000 (Wed, 01 Aug 2018)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Multisite: Complete the new CRUD API for managing sites.
New functions `wp_insert_site( $data )`, `wp_update_site( $id, $data )` and `wp_delete_site( $id )` are introduced to manage site rows in the `wp_blogs` table, forming the new CRUD API together with the existing `get_site()` / `get_sites()`. The new API provides various benefits over the previously existing API, fixing several cache invalidation issues and being hook-driven so that normalization and validation of the passed data can be fully customized.
New hooks introduced as part of this are the actions `wp_insert_site`, `wp_update_site`, `wp_delete_site`, `wp_validate_site_data` and the filter `wp_normalize_site_data`.
At this point, `wp_insert_site()` does not handle setting up the site's database tables, and `wp_delete_site()` does not handle dropping the site's database tables, so the two can not yet be used directly as full replacements of `wpmu_create_blog()` and `wpmu_delete_blog()`. Managing the site's database tables will be added via hooks as part of the follow-up ticket <a href="https://core.trac.wordpress.org/ticket/41333">#41333</a>.
The existing functions `wpmu_create_blog()`, `update_blog_details()`, and `wpmu_delete_blog()` make use of the respective new counterpart and will be obsolete once <a href="https://core.trac.wordpress.org/ticket/41333">#41333</a> has been completed.
Props flixos90, jeremyfelt, spacedmonkey.
Fixes <a href="https://core.trac.wordpress.org/ticket/40364">#40364</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminincludesmsphp">trunk/src/wp-admin/includes/ms.php</a></li>
<li><a href="#trunksrcwpincludesmsblogsphp">trunk/src/wp-includes/ms-blogs.php</a></li>
<li><a href="#trunksrcwpincludesmsdefaultfiltersphp">trunk/src/wp-includes/ms-default-filters.php</a></li>
<li><a href="#trunksrcwpincludesmsdeprecatedphp">trunk/src/wp-includes/ms-deprecated.php</a></li>
<li><a href="#trunksrcwpincludesmsfunctionsphp">trunk/src/wp-includes/ms-functions.php</a></li>
<li><a href="#trunktestsphpunittestsmultisitesitephp">trunk/tests/phpunit/tests/multisite/site.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminincludesmsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/includes/ms.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/ms.php 2018-07-31 20:25:35 UTC (rev 43547)
+++ trunk/src/wp-admin/includes/ms.php 2018-08-01 13:05:44 UTC (rev 43548)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -56,6 +56,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * Delete a site.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 3.0.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 5.0.0 Use wp_delete_site() internally to delete the site row from the database.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @global wpdb $wpdb WordPress database abstraction object.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -142,7 +143,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $wpdb->delete( $wpdb->blogs, array( 'blog_id' => $blog_id ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ wp_delete_site( $blog_id );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Filters the upload base directory to delete when the site is deleted.
</span></span></pre></div>
<a id="trunksrcwpincludesmsblogsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/ms-blogs.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/ms-blogs.php 2018-07-31 20:25:35 UTC (rev 43547)
+++ trunk/src/wp-includes/ms-blogs.php 2018-08-01 13:05:44 UTC (rev 43548)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -297,132 +297,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $details = get_object_vars( $details );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $current_details = get_site( $blog_id );
- if ( empty( $current_details ) ) {
- return false;
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $site = wp_update_site( $blog_id, $details );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $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 ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( is_wp_error( $site ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> return false;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // 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 );
-
</del><span class="cx" style="display: block; padding: 0 10px"> return true;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -518,6 +398,170 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * 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;
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Retrieves site data given a site ID or site object.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * Site data will be cached and returned after being passed through a filter.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -688,6 +732,187 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * 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!' ) );
+ }
+ }
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Retrieve option value for a given blog id based on name of option.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * If the option does not exist or does not have a value, then the return value
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1193,6 +1418,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * Update a blog details field.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since MU (3.0.0)
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 5.0.0 Use wp_update_site() internally.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @global wpdb $wpdb WordPress database abstraction object.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1213,63 +1439,14 @@
</span><span class="cx" style="display: block; padding: 0 10px"> return $value;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $result = $wpdb->update(
- $wpdb->blogs, array(
- $pref => $value,
- 'last_updated' => current_time( 'mysql', true ),
- ), array( 'blog_id' => $blog_id )
- );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $result = wp_update_site( $blog_id, array(
+ $pref => $value,
+ ) );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( false === $result ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( is_wp_error( $result ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> return false;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 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().
- }
-
</del><span class="cx" style="display: block; padding: 0 10px"> return $value;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1537,3 +1714,177 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> update_posts_count();
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+/**
+ * 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 );
+}
</ins></span></pre></div>
<a id="trunksrcwpincludesmsdefaultfiltersphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/ms-default-filters.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/ms-default-filters.php 2018-07-31 20:25:35 UTC (rev 43547)
+++ trunk/src/wp-includes/ms-default-filters.php 2018-08-01 13:05:44 UTC (rev 43548)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -41,6 +41,15 @@
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'wpmu_new_blog', 'newblog_notify_siteadmin', 10, 2 );
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'wpmu_activate_blog', 'wpmu_welcome_notification', 10, 5 );
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'after_signup_site', 'wpmu_signup_blog_notification', 10, 7 );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+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 );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Register Nonce
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'signup_hidden_fields', 'signup_nonce_fields' );
</span></span></pre></div>
<a id="trunksrcwpincludesmsdeprecatedphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/ms-deprecated.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/ms-deprecated.php 2018-07-31 20:25:35 UTC (rev 43547)
+++ trunk/src/wp-includes/ms-deprecated.php 2018-08-01 13:05:44 UTC (rev 43548)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -546,3 +546,37 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> return isset( $current_user->$local_key );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+/**
+ * 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;
+}
</ins></span></pre></div>
<a id="trunksrcwpincludesmsfunctionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/ms-functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/ms-functions.php 2018-07-31 20:25:35 UTC (rev 43547)
+++ trunk/src/wp-includes/ms-functions.php 2018-08-01 13:05:44 UTC (rev 43548)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1273,19 +1273,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> $meta = wp_parse_args( $meta, $defaults );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $domain = preg_replace( '/\s+/', '', sanitize_user( $domain, true ) );
-
- if ( is_subdomain_install() ) {
- $domain = str_replace( '@', '', $domain );
- }
-
</del><span class="cx" style="display: block; padding: 0 10px"> $title = strip_tags( $title );
</span><span class="cx" style="display: block; padding: 0 10px"> $user_id = (int) $user_id;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( empty( $path ) ) {
- $path = '/';
- }
-
</del><span class="cx" style="display: block; padding: 0 10px"> // Check if the domain has been used already. We should return an error message.
</span><span class="cx" style="display: block; padding: 0 10px"> if ( domain_exists( $domain, $path, $network_id ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error( 'blog_taken', __( 'Sorry, that site already exists!' ) );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1295,8 +1285,28 @@
</span><span class="cx" style="display: block; padding: 0 10px"> wp_installing( true );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( ! $blog_id = insert_blog( $domain, $path, $network_id ) ) {
- return new WP_Error( 'insert_blog', __( 'Could not create site.' ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $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;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> switch_to_blog( $blog_id );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1306,14 +1316,10 @@
</span><span class="cx" style="display: block; padding: 0 10px"> add_user_to_blog( $blog_id, $user_id, 'administrator' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> foreach ( $meta as $key => $value ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( in_array( $key, array( 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' ) ) ) {
- update_blog_status( $blog_id, $key, $value );
- } else {
- update_option( $key, $value );
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ update_option( $key, $value );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- update_option( 'blog_public', (int) $meta['public'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ update_option( 'blog_public', (int) $site_data['public'] );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! is_super_admin( $user_id ) && ! get_user_meta( $user_id, 'primary_blog', true ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> update_user_meta( $user_id, 'primary_blog', $blog_id );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1320,6 +1326,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> restore_current_blog();
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ $site = get_site( $blog_id );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Fires immediately after a new site is created.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1332,7 +1341,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @param int $network_id Network ID. Only relevant on multi-network installations.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param array $meta Meta data. Used to set initial site options.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- do_action( 'wpmu_new_blog', $blog_id, $user_id, $domain, $path, $network_id, $meta );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ do_action( 'wpmu_new_blog', $blog_id, $user_id, $site->domain, $site->path, $site->network_id, $meta );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> wp_cache_set( 'last_changed', microtime(), 'sites' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1486,47 +1495,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * 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;
-}
-
-/**
</del><span class="cx" style="display: block; padding: 0 10px"> * Install an empty blog.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * Creates the new blog tables and options. If calling this function
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1538,7 +1506,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @global wpdb $wpdb
</span><span class="cx" style="display: block; padding: 0 10px"> * @global WP_Roles $wp_roles
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param int $blog_id The value returned by insert_blog().
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param int $blog_id The value returned by wp_insert_site().
</ins><span class="cx" style="display: block; padding: 0 10px"> * @param string $blog_title The title of the new site.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function install_blog( $blog_id, $blog_title = '' ) {
</span></span></pre></div>
<a id="trunktestsphpunittestsmultisitesitephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/multisite/site.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/multisite/site.php 2018-07-31 20:25:35 UTC (rev 43547)
+++ trunk/tests/phpunit/tests/multisite/site.php 2018-08-01 13:05:44 UTC (rev 43548)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -10,6 +10,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> class Tests_Multisite_Site extends WP_UnitTestCase {
</span><span class="cx" style="display: block; padding: 0 10px"> protected $suppress = false;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ protected $site_status_hooks = array();
</ins><span class="cx" style="display: block; padding: 0 10px"> protected static $network_ids;
</span><span class="cx" style="display: block; padding: 0 10px"> protected static $site_ids;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -446,12 +447,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( '0', $blog->spam );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( 1, $test_action_counter );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // The action should fire if the status of 'spam' stays the same.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // The action should not fire if the status of 'spam' stays the same.
</ins><span class="cx" style="display: block; padding: 0 10px"> update_blog_status( $blog_id, 'spam', 0 );
</span><span class="cx" style="display: block; padding: 0 10px"> $blog = get_site( $blog_id );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( '0', $blog->spam );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertEquals( 2, $test_action_counter );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertEquals( 1, $test_action_counter );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> remove_action( 'make_ham_blog', array( $this, '_action_counter_cb' ), 10 );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -469,12 +470,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( '1', $blog->spam );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( 1, $test_action_counter );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // The action should fire if the status of 'spam' stays the same.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // The action should not fire if the status of 'spam' stays the same.
</ins><span class="cx" style="display: block; padding: 0 10px"> update_blog_status( $blog_id, 'spam', 1 );
</span><span class="cx" style="display: block; padding: 0 10px"> $blog = get_site( $blog_id );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( '1', $blog->spam );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertEquals( 2, $test_action_counter );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertEquals( 1, $test_action_counter );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> remove_action( 'make_spam_blog', array( $this, '_action_counter_cb' ), 10 );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -492,12 +493,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( '1', $blog->archived );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( 1, $test_action_counter );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // The action should fire if the status of 'archived' stays the same.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // The action should not fire if the status of 'archived' stays the same.
</ins><span class="cx" style="display: block; padding: 0 10px"> update_blog_status( $blog_id, 'archived', 1 );
</span><span class="cx" style="display: block; padding: 0 10px"> $blog = get_site( $blog_id );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( '1', $blog->archived );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertEquals( 2, $test_action_counter );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertEquals( 1, $test_action_counter );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> remove_action( 'archive_blog', array( $this, '_action_counter_cb' ), 10 );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -516,11 +517,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( '0', $blog->archived );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( 1, $test_action_counter );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // The action should fire if the status of 'archived' stays the same.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // The action should not fire if the status of 'archived' stays the same.
</ins><span class="cx" style="display: block; padding: 0 10px"> update_blog_status( $blog_id, 'archived', 0 );
</span><span class="cx" style="display: block; padding: 0 10px"> $blog = get_site( $blog_id );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( '0', $blog->archived );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertEquals( 2, $test_action_counter );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertEquals( 1, $test_action_counter );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> remove_action( 'unarchive_blog', array( $this, '_action_counter_cb' ), 10 );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -538,12 +539,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( '1', $blog->deleted );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( 1, $test_action_counter );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // The action should fire if the status of 'deleted' stays the same.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // The action should not fire if the status of 'deleted' stays the same.
</ins><span class="cx" style="display: block; padding: 0 10px"> update_blog_status( $blog_id, 'deleted', 1 );
</span><span class="cx" style="display: block; padding: 0 10px"> $blog = get_site( $blog_id );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( '1', $blog->deleted );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertEquals( 2, $test_action_counter );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertEquals( 1, $test_action_counter );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> remove_action( 'make_delete_blog', array( $this, '_action_counter_cb' ), 10 );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -562,12 +563,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( '0', $blog->deleted );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( 1, $test_action_counter );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // The action should fire if the status of 'deleted' stays the same.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // The action should not fire if the status of 'deleted' stays the same.
</ins><span class="cx" style="display: block; padding: 0 10px"> update_blog_status( $blog_id, 'deleted', 0 );
</span><span class="cx" style="display: block; padding: 0 10px"> $blog = get_site( $blog_id );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( '0', $blog->deleted );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertEquals( 2, $test_action_counter );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertEquals( 1, $test_action_counter );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> remove_action( 'make_undelete_blog', array( $this, '_action_counter_cb' ), 10 );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -585,12 +586,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( '1', $blog->mature );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( 1, $test_action_counter );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // The action should fire if the status of 'mature' stays the same.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // The action should not fire if the status of 'mature' stays the same.
</ins><span class="cx" style="display: block; padding: 0 10px"> update_blog_status( $blog_id, 'mature', 1 );
</span><span class="cx" style="display: block; padding: 0 10px"> $blog = get_site( $blog_id );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( '1', $blog->mature );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertEquals( 2, $test_action_counter );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertEquals( 1, $test_action_counter );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> remove_action( 'mature_blog', array( $this, '_action_counter_cb' ), 10 );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -609,12 +610,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( '0', $blog->mature );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( 1, $test_action_counter );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // The action should fire if the status of 'mature' stays the same.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // The action should not fire if the status of 'mature' stays the same.
</ins><span class="cx" style="display: block; padding: 0 10px"> update_blog_status( $blog_id, 'mature', 0 );
</span><span class="cx" style="display: block; padding: 0 10px"> $blog = get_site( $blog_id );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( '0', $blog->mature );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertEquals( 2, $test_action_counter );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertEquals( 1, $test_action_counter );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> remove_action( 'unmature_blog', array( $this, '_action_counter_cb' ), 10 );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -632,12 +633,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( '0', $blog->public );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( 1, $test_action_counter );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // The action should fire if the status of 'mature' stays the same.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // The action should not fire if the status of 'mature' stays the same.
</ins><span class="cx" style="display: block; padding: 0 10px"> update_blog_status( $blog_id, 'public', 0 );
</span><span class="cx" style="display: block; padding: 0 10px"> $blog = get_site( $blog_id );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( '0', $blog->public );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertEquals( 2, $test_action_counter );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertEquals( 1, $test_action_counter );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> remove_action( 'update_blog_public', array( $this, '_action_counter_cb' ), 10 );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1259,6 +1260,738 @@
</span><span class="cx" style="display: block; padding: 0 10px"> array( 'current_blog_%domain%%path%', 'site-options' ),
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ /**
+ * @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' => '<yet>/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;
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> endif;
</span></span></pre>
</div>
</div>
</body>
</html>