Make WordPress Core

Ticket #40364: 40364.14.diff

File 40364.14.diff, 52.8 KB (added by flixos90, 5 years ago)
  • src/wp-admin/includes/ms.php

     
    5656 * Delete a site.
    5757 *
    5858 * @since 3.0.0
     59 * @since 5.0.0 Use wp_delete_site() internally to delete the site row from the database.
    5960 *
    6061 * @global wpdb $wpdb WordPress database abstraction object.
    6162 *
     
    142143                        }
    143144                }
    144145
    145                 $wpdb->delete( $wpdb->blogs, array( 'blog_id' => $blog_id ) );
     146                wp_delete_site( $blog_id );
    146147
    147148                /**
    148149                 * Filters the upload base directory to delete when the site is deleted.
  • src/wp-includes/ms-blogs.php

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

     
    4141add_action( 'wpmu_new_blog', 'newblog_notify_siteadmin', 10, 2 );
    4242add_action( 'wpmu_activate_blog', 'wpmu_welcome_notification', 10, 5 );
    4343add_action( 'after_signup_site', 'wpmu_signup_blog_notification', 10, 7 );
     44add_filter( 'wp_normalize_site_data', 'wp_normalize_site_data', 10, 1 );
     45add_action( 'wp_validate_site_data', 'wp_validate_site_data', 10, 3 );
     46add_action( 'wp_insert_site', 'wp_maybe_update_network_site_counts_on_update', 10, 1 );
     47add_action( 'wp_update_site', 'wp_maybe_update_network_site_counts_on_update', 10, 2 );
     48add_action( 'wp_delete_site', 'wp_maybe_update_network_site_counts_on_update', 10, 1 );
     49add_action( 'wp_insert_site', 'wp_maybe_transition_site_statuses_on_update', 10, 1 );
     50add_action( 'wp_update_site', 'wp_maybe_transition_site_statuses_on_update', 10, 2 );
     51add_action( 'wp_update_site', 'wp_maybe_clean_new_site_cache_on_update', 10, 2 );
     52add_action( 'update_blog_public', 'wp_update_blog_public_option_on_site_update', 1, 2 );
    4453
    4554// Register Nonce
    4655add_action( 'signup_hidden_fields', 'signup_nonce_fields' );
  • src/wp-includes/ms-deprecated.php

     
    546546
    547547        return isset( $current_user->$local_key );
    548548}
     549
     550/**
     551 * Store basic site info in the blogs table.
     552 *
     553 * This function creates a row in the wp_blogs table and returns
     554 * the new blog's ID. It is the first step in creating a new blog.
     555 *
     556 * @since MU (3.0.0)
     557 * @deprecated 5.0.0 Use `wp_insert_site()`
     558 * @see wp_insert_site()
     559 *
     560 * @param string $domain  The domain of the new site.
     561 * @param string $path    The path of the new site.
     562 * @param int    $site_id Unless you're running a multi-network install, be sure to set this value to 1.
     563 * @return int|false The ID of the new row
     564 */
     565function insert_blog($domain, $path, $site_id) {
     566        _deprecated_function( __FUNCTION__, '5.0.0', 'wp_insert_site()' );
     567
     568        $data = array(
     569                'domain'  => $domain,
     570                'path'    => $path,
     571                'site_id' => $site_id,
     572        );
     573
     574        $site_id = wp_insert_site( $data );
     575        if ( is_wp_error( $site_id ) ) {
     576                return false;
     577        }
     578
     579        clean_blog_cache( $site_id );
     580
     581        return $site_id;
     582}
  • src/wp-includes/ms-functions.php

     
    12731273        );
    12741274        $meta     = wp_parse_args( $meta, $defaults );
    12751275
    1276         $domain = preg_replace( '/\s+/', '', sanitize_user( $domain, true ) );
    1277 
    1278         if ( is_subdomain_install() ) {
    1279                 $domain = str_replace( '@', '', $domain );
    1280         }
    1281 
    12821276        $title   = strip_tags( $title );
    12831277        $user_id = (int) $user_id;
    12841278
    1285         if ( empty( $path ) ) {
    1286                 $path = '/';
    1287         }
    1288 
    12891279        // Check if the domain has been used already. We should return an error message.
    12901280        if ( domain_exists( $domain, $path, $network_id ) ) {
    12911281                return new WP_Error( 'blog_taken', __( 'Sorry, that site already exists!' ) );
     
    12951285                wp_installing( true );
    12961286        }
    12971287
    1298         if ( ! $blog_id = insert_blog( $domain, $path, $network_id ) ) {
    1299                 return new WP_Error( 'insert_blog', __( 'Could not create site.' ) );
     1288        $site_data_whitelist = array( 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' );
     1289
     1290        $site_data = array_merge(
     1291                array(
     1292                        'domain'     => $domain,
     1293                        'path'       => $path,
     1294                        'network_id' => $network_id,
     1295                ),
     1296                array_intersect_key(
     1297                        $meta,
     1298                        array_flip( $site_data_whitelist )
     1299                )
     1300        );
     1301
     1302        $meta = array_diff_key( $meta, array_flip( $site_data_whitelist ) );
     1303
     1304        remove_action( 'update_blog_public', 'wp_update_blog_public_option_on_site_update', 1 );
     1305        $blog_id = wp_insert_site( $site_data );
     1306        add_action( 'update_blog_public', 'wp_update_blog_public_option_on_site_update', 1, 2 );
     1307
     1308        if ( is_wp_error( $blog_id ) ) {
     1309                return $blog_id;
    13001310        }
    13011311
    13021312        switch_to_blog( $blog_id );
     
    13061316        add_user_to_blog( $blog_id, $user_id, 'administrator' );
    13071317
    13081318        foreach ( $meta as $key => $value ) {
    1309                 if ( in_array( $key, array( 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' ) ) ) {
    1310                         update_blog_status( $blog_id, $key, $value );
    1311                 } else {
    1312                         update_option( $key, $value );
    1313                 }
     1319                update_option( $key, $value );
    13141320        }
    13151321
    1316         update_option( 'blog_public', (int) $meta['public'] );
     1322        update_option( 'blog_public', (int) $site_data['public'] );
    13171323
    13181324        if ( ! is_super_admin( $user_id ) && ! get_user_meta( $user_id, 'primary_blog', true ) ) {
    13191325                update_user_meta( $user_id, 'primary_blog', $blog_id );
    13201326        }
    13211327
    13221328        restore_current_blog();
     1329
     1330        $site = get_site( $blog_id );
     1331
    13231332        /**
    13241333         * Fires immediately after a new site is created.
    13251334         *
     
    13321341         * @param int    $network_id Network ID. Only relevant on multi-network installations.
    13331342         * @param array  $meta       Meta data. Used to set initial site options.
    13341343         */
    1335         do_action( 'wpmu_new_blog', $blog_id, $user_id, $domain, $path, $network_id, $meta );
     1344        do_action( 'wpmu_new_blog', $blog_id, $user_id, $site->domain, $site->path, $site->network_id, $meta );
    13361345
    13371346        wp_cache_set( 'last_changed', microtime(), 'sites' );
    13381347
     
    14861495}
    14871496
    14881497/**
    1489  * Store basic site info in the blogs table.
    1490  *
    1491  * This function creates a row in the wp_blogs table and returns
    1492  * the new blog's ID. It is the first step in creating a new blog.
    1493  *
    1494  * @since MU (3.0.0)
    1495  *
    1496  * @global wpdb $wpdb WordPress database abstraction object.
    1497  *
    1498  * @param string $domain     The domain of the new site.
    1499  * @param string $path       The path of the new site.
    1500  * @param int    $network_id Unless you're running a multi-network installation, be sure to set this value to 1.
    1501  * @return int|false The ID of the new row
    1502  */
    1503 function insert_blog( $domain, $path, $network_id ) {
    1504         global $wpdb;
    1505 
    1506         $path       = trailingslashit( $path );
    1507         $network_id = (int) $network_id;
    1508 
    1509         $result = $wpdb->insert(
    1510                 $wpdb->blogs, array(
    1511                         'site_id'    => $network_id,
    1512                         'domain'     => $domain,
    1513                         'path'       => $path,
    1514                         'registered' => current_time( 'mysql' ),
    1515                 )
    1516         );
    1517         if ( ! $result ) {
    1518                 return false;
    1519         }
    1520 
    1521         $blog_id = $wpdb->insert_id;
    1522         clean_blog_cache( $blog_id );
    1523 
    1524         wp_maybe_update_network_site_counts( $network_id );
    1525 
    1526         return $blog_id;
    1527 }
    1528 
    1529 /**
    15301498 * Install an empty blog.
    15311499 *
    15321500 * Creates the new blog tables and options. If calling this function
     
    15381506 * @global wpdb     $wpdb
    15391507 * @global WP_Roles $wp_roles
    15401508 *
    1541  * @param int    $blog_id    The value returned by insert_blog().
     1509 * @param int    $blog_id    The value returned by wp_insert_site().
    15421510 * @param string $blog_title The title of the new site.
    15431511 */
    15441512function install_blog( $blog_id, $blog_title = '' ) {
  • tests/phpunit/tests/multisite/site.php

     
    1010         */
    1111        class Tests_Multisite_Site extends WP_UnitTestCase {
    1212                protected $suppress = false;
     13                protected $site_status_hooks = array();
    1314                protected static $network_ids;
    1415                protected static $site_ids;
    1516
     
    446447                        $this->assertEquals( '0', $blog->spam );
    447448                        $this->assertEquals( 1, $test_action_counter );
    448449
    449                         // The action should fire if the status of 'spam' stays the same.
     450                        // The action should not fire if the status of 'spam' stays the same.
    450451                        update_blog_status( $blog_id, 'spam', 0 );
    451452                        $blog = get_site( $blog_id );
    452453
    453454                        $this->assertEquals( '0', $blog->spam );
    454                         $this->assertEquals( 2, $test_action_counter );
     455                        $this->assertEquals( 1, $test_action_counter );
    455456
    456457                        remove_action( 'make_ham_blog', array( $this, '_action_counter_cb' ), 10 );
    457458                }
     
    469470                        $this->assertEquals( '1', $blog->spam );
    470471                        $this->assertEquals( 1, $test_action_counter );
    471472
    472                         // The action should fire if the status of 'spam' stays the same.
     473                        // The action should not fire if the status of 'spam' stays the same.
    473474                        update_blog_status( $blog_id, 'spam', 1 );
    474475                        $blog = get_site( $blog_id );
    475476
    476477                        $this->assertEquals( '1', $blog->spam );
    477                         $this->assertEquals( 2, $test_action_counter );
     478                        $this->assertEquals( 1, $test_action_counter );
    478479
    479480                        remove_action( 'make_spam_blog', array( $this, '_action_counter_cb' ), 10 );
    480481                }
     
    492493                        $this->assertEquals( '1', $blog->archived );
    493494                        $this->assertEquals( 1, $test_action_counter );
    494495
    495                         // The action should fire if the status of 'archived' stays the same.
     496                        // The action should not fire if the status of 'archived' stays the same.
    496497                        update_blog_status( $blog_id, 'archived', 1 );
    497498                        $blog = get_site( $blog_id );
    498499
    499500                        $this->assertEquals( '1', $blog->archived );
    500                         $this->assertEquals( 2, $test_action_counter );
     501                        $this->assertEquals( 1, $test_action_counter );
    501502
    502503                        remove_action( 'archive_blog', array( $this, '_action_counter_cb' ), 10 );
    503504                }
     
    516517                        $this->assertEquals( '0', $blog->archived );
    517518                        $this->assertEquals( 1, $test_action_counter );
    518519
    519                         // The action should fire if the status of 'archived' stays the same.
     520                        // The action should not fire if the status of 'archived' stays the same.
    520521                        update_blog_status( $blog_id, 'archived', 0 );
    521522                        $blog = get_site( $blog_id );
    522523                        $this->assertEquals( '0', $blog->archived );
    523                         $this->assertEquals( 2, $test_action_counter );
     524                        $this->assertEquals( 1, $test_action_counter );
    524525
    525526                        remove_action( 'unarchive_blog', array( $this, '_action_counter_cb' ), 10 );
    526527                }
     
    538539                        $this->assertEquals( '1', $blog->deleted );
    539540                        $this->assertEquals( 1, $test_action_counter );
    540541
    541                         // The action should fire if the status of 'deleted' stays the same.
     542                        // The action should not fire if the status of 'deleted' stays the same.
    542543                        update_blog_status( $blog_id, 'deleted', 1 );
    543544                        $blog = get_site( $blog_id );
    544545
    545546                        $this->assertEquals( '1', $blog->deleted );
    546                         $this->assertEquals( 2, $test_action_counter );
     547                        $this->assertEquals( 1, $test_action_counter );
    547548
    548549                        remove_action( 'make_delete_blog', array( $this, '_action_counter_cb' ), 10 );
    549550                }
     
    562563                        $this->assertEquals( '0', $blog->deleted );
    563564                        $this->assertEquals( 1, $test_action_counter );
    564565
    565                         // The action should fire if the status of 'deleted' stays the same.
     566                        // The action should not fire if the status of 'deleted' stays the same.
    566567                        update_blog_status( $blog_id, 'deleted', 0 );
    567568                        $blog = get_site( $blog_id );
    568569
    569570                        $this->assertEquals( '0', $blog->deleted );
    570                         $this->assertEquals( 2, $test_action_counter );
     571                        $this->assertEquals( 1, $test_action_counter );
    571572
    572573                        remove_action( 'make_undelete_blog', array( $this, '_action_counter_cb' ), 10 );
    573574                }
     
    585586                        $this->assertEquals( '1', $blog->mature );
    586587                        $this->assertEquals( 1, $test_action_counter );
    587588
    588                         // The action should fire if the status of 'mature' stays the same.
     589                        // The action should not fire if the status of 'mature' stays the same.
    589590                        update_blog_status( $blog_id, 'mature', 1 );
    590591                        $blog = get_site( $blog_id );
    591592
    592593                        $this->assertEquals( '1', $blog->mature );
    593                         $this->assertEquals( 2, $test_action_counter );
     594                        $this->assertEquals( 1, $test_action_counter );
    594595
    595596                        remove_action( 'mature_blog', array( $this, '_action_counter_cb' ), 10 );
    596597                }
     
    609610                        $this->assertEquals( '0', $blog->mature );
    610611                        $this->assertEquals( 1, $test_action_counter );
    611612
    612                         // The action should fire if the status of 'mature' stays the same.
     613                        // The action should not fire if the status of 'mature' stays the same.
    613614                        update_blog_status( $blog_id, 'mature', 0 );
    614615                        $blog = get_site( $blog_id );
    615616
    616617                        $this->assertEquals( '0', $blog->mature );
    617                         $this->assertEquals( 2, $test_action_counter );
     618                        $this->assertEquals( 1, $test_action_counter );
    618619
    619620                        remove_action( 'unmature_blog', array( $this, '_action_counter_cb' ), 10 );
    620621                }
     
    632633                        $this->assertEquals( '0', $blog->public );
    633634                        $this->assertEquals( 1, $test_action_counter );
    634635
    635                         // The action should fire if the status of 'mature' stays the same.
     636                        // The action should not fire if the status of 'mature' stays the same.
    636637                        update_blog_status( $blog_id, 'public', 0 );
    637638                        $blog = get_site( $blog_id );
    638639
    639640                        $this->assertEquals( '0', $blog->public );
    640                         $this->assertEquals( 2, $test_action_counter );
     641                        $this->assertEquals( 1, $test_action_counter );
    641642
    642643                        remove_action( 'update_blog_public', array( $this, '_action_counter_cb' ), 10 );
    643644                }
     
    12591260                                array( 'current_blog_%domain%%path%', 'site-options' ),
    12601261                        );
    12611262                }
     1263
     1264                /**
     1265                 * @ticket 40364
     1266                 * @dataProvider data_wp_insert_site
     1267                 */
     1268                public function test_wp_insert_site( $site_data, $expected_data ) {
     1269                        $site_id = wp_insert_site( $site_data );
     1270
     1271                        $this->assertInternalType( 'integer', $site_id );
     1272
     1273                        $site = get_site( $site_id );
     1274                        foreach ( $expected_data as $key => $value ) {
     1275                                $this->assertEquals( $value, $site->$key );
     1276                        }
     1277                }
     1278
     1279                public function data_wp_insert_site() {
     1280                        return array(
     1281                                array(
     1282                                        array(
     1283                                                'domain' => 'example.com',
     1284                                        ),
     1285                                        array(
     1286                                                'domain'     => 'example.com',
     1287                                                'path'       => '/',
     1288                                                'network_id' => 1,
     1289                                                'public'     => 1,
     1290                                                'archived'   => 0,
     1291                                                'mature'     => 0,
     1292                                                'spam'       => 0,
     1293                                                'deleted'    => 0,
     1294                                                'lang_id'    => 0,
     1295                                        ),
     1296                                ),
     1297                                array(
     1298                                        array(
     1299                                                'domain'     => 'example.com',
     1300                                                'path'       => '/foo',
     1301                                                'network_id' => 2,
     1302                                        ),
     1303                                        array(
     1304                                                'domain'     => 'example.com',
     1305                                                'path'       => '/foo/',
     1306                                                'network_id' => 2,
     1307                                        ),
     1308                                ),
     1309                                array(
     1310                                        array(
     1311                                                'domain'  => 'example.com',
     1312                                                'path'    => '/bar/',
     1313                                                'site_id' => 2,
     1314                                        ),
     1315                                        array(
     1316                                                'domain'     => 'example.com',
     1317                                                'path'       => '/bar/',
     1318                                                'network_id' => 2,
     1319                                        ),
     1320                                ),
     1321                                array(
     1322                                        array(
     1323                                                'domain'     => 'example.com',
     1324                                                'path'       => '/bar/',
     1325                                                'site_id'    => 2,
     1326                                                'network_id' => 3,
     1327                                        ),
     1328                                        array(
     1329                                                'domain'     => 'example.com',
     1330                                                'path'       => '/bar/',
     1331                                                'network_id' => 3,
     1332                                        ),
     1333                                ),
     1334                                array(
     1335                                        array(
     1336                                                'domain'   => 'example.com',
     1337                                                'path'     => 'foobar',
     1338                                                'public'   => 0,
     1339                                                'archived' => 1,
     1340                                                'mature'   => 1,
     1341                                                'spam'     => 1,
     1342                                                'deleted'  => 1,
     1343                                                'lang_id'  => 1,
     1344                                        ),
     1345                                        array(
     1346                                                'domain'   => 'example.com',
     1347                                                'path'     => '/foobar/',
     1348                                                'public'   => 0,
     1349                                                'archived' => 1,
     1350                                                'mature'   => 1,
     1351                                                'spam'     => 1,
     1352                                                'deleted'  => 1,
     1353                                                'lang_id'  => 1,
     1354                                        ),
     1355                                ),
     1356                        );
     1357                }
     1358
     1359                /**
     1360                 * @ticket 40364
     1361                 */
     1362                public function test_wp_insert_site_empty_domain() {
     1363                        $site_id = wp_insert_site( array( 'public' => 0 ) );
     1364
     1365                        $this->assertWPError( $site_id );
     1366                        $this->assertSame( 'site_empty_domain', $site_id->get_error_code() );
     1367                }
     1368
     1369                /**
     1370                 * @ticket 40364
     1371                 * @dataProvider data_wp_update_site
     1372                 */
     1373                public function test_wp_update_site( $site_data, $expected_data ) {
     1374                        $site_id = self::factory()->blog->create();
     1375
     1376                        $old_site = get_site( $site_id );
     1377
     1378                        $result = wp_update_site( $site_id, $site_data );
     1379
     1380                        $this->assertSame( $site_id, $result );
     1381
     1382                        $new_site = get_site( $site_id );
     1383                        foreach ( $new_site->to_array() as $key => $value ) {
     1384                                if ( isset( $expected_data[ $key ] ) ) {
     1385                                        $this->assertEquals( $expected_data[ $key ], $value );
     1386                                } elseif ( 'last_updated' === $key ) {
     1387                                        $this->assertTrue( $old_site->last_updated <= $value );
     1388                                } else {
     1389                                        $this->assertEquals( $old_site->$key, $value );
     1390                                }
     1391                        }
     1392                }
     1393
     1394                public function data_wp_update_site() {
     1395                        return array(
     1396                                array(
     1397                                        array(
     1398                                                'domain'     => 'example.com',
     1399                                                'network_id' => 2,
     1400                                        ),
     1401                                        array(
     1402                                                'domain'  => 'example.com',
     1403                                                'site_id' => 2,
     1404                                        ),
     1405                                ),
     1406                                array(
     1407                                        array(
     1408                                                'path' => 'foo',
     1409                                        ),
     1410                                        array(
     1411                                                'path' => '/foo/',
     1412                                        ),
     1413                                ),
     1414                                array(
     1415                                        array(
     1416                                                'public'   => 0,
     1417                                                'archived' => 1,
     1418                                                'mature'   => 1,
     1419                                                'spam'     => 1,
     1420                                                'deleted'  => 1,
     1421                                                'lang_id'  => 1,
     1422                                        ),
     1423                                        array(
     1424                                                'public'   => 0,
     1425                                                'archived' => 1,
     1426                                                'mature'   => 1,
     1427                                                'spam'     => 1,
     1428                                                'deleted'  => 1,
     1429                                                'lang_id'  => 1,
     1430                                        ),
     1431                                ),
     1432                        );
     1433                }
     1434
     1435                /**
     1436                 * @ticket 40364
     1437                 */
     1438                public function test_wp_update_site_empty_domain() {
     1439                        $site_id = self::factory()->blog->create();
     1440
     1441                        $result = wp_update_site( $site_id, array( 'domain' => '' ) );
     1442
     1443                        $this->assertWPError( $result );
     1444                        $this->assertSame( 'site_empty_domain', $result->get_error_code() );
     1445                }
     1446
     1447                /**
     1448                 * @ticket 40364
     1449                 */
     1450                public function test_wp_update_site_invalid_id() {
     1451                        $result = wp_update_site( 444444, array( 'domain' => 'example.com' ) );
     1452
     1453                        $this->assertWPError( $result );
     1454                        $this->assertSame( 'site_not_exist', $result->get_error_code() );
     1455                }
     1456
     1457                /**
     1458                 * @ticket 40364
     1459                 */
     1460                public function test_wp_update_site_cleans_cache() {
     1461                        $site_id = self::factory()->blog->create();
     1462                        $site1 = get_site( $site_id );
     1463
     1464                        $result = wp_update_site( $site_id, array( 'public' => 0 ) );
     1465                        $site2 = get_site( $site_id );
     1466
     1467                        $result = wp_update_site( $site_id, array( 'public' => 1 ) );
     1468                        $site3 = get_site( $site_id );
     1469
     1470                        $this->assertEquals( 1, $site1->public );
     1471                        $this->assertEquals( 0, $site2->public );
     1472                        $this->assertEquals( 1, $site3->public );
     1473                }
     1474
     1475                /**
     1476                 * @ticket 40364
     1477                 */
     1478                public function test_wp_delete_site() {
     1479                        $site_id = self::factory()->blog->create();
     1480
     1481                        $site = get_site( $site_id );
     1482
     1483                        $result = wp_delete_site( $site_id );
     1484
     1485                        $this->assertInstanceOf( 'WP_Site', $result );
     1486                        $this->assertEquals( $result->to_array(), $site->to_array() );
     1487                }
     1488
     1489                /**
     1490                 * @ticket 40364
     1491                 */
     1492                public function test_wp_delete_site_invalid_id() {
     1493                        $result = wp_delete_site( 444444 );
     1494
     1495                        $this->assertWPError( $result );
     1496                        $this->assertSame( 'site_not_exist', $result->get_error_code() );
     1497                }
     1498
     1499                /**
     1500                 * @ticket 40364
     1501                 * @dataProvider data_wp_normalize_site_data
     1502                 */
     1503                public function test_wp_normalize_site_data( $data, $expected ) {
     1504                        $result = wp_normalize_site_data( $data );
     1505
     1506                        $this->assertEqualSetsWithIndex( $expected, $result );
     1507                }
     1508
     1509                public function data_wp_normalize_site_data() {
     1510                        return array(
     1511                                array(
     1512                                        array(
     1513                                                'network_id' => '4',
     1514                                        ),
     1515                                        array(
     1516                                                'network_id' => 4,
     1517                                        ),
     1518                                ),
     1519                                array(
     1520                                        array(
     1521                                                'domain' => 'invalid domain .com',
     1522                                                'path'   => 'foo',
     1523                                        ),
     1524                                        array(
     1525                                                'domain' => 'invaliddomain.com',
     1526                                                'path'   => '/foo/',
     1527                                        ),
     1528                                ),
     1529                                array(
     1530                                        array(
     1531                                                'domain' => '<yet>/another-invalid-domain.com',
     1532                                        ),
     1533                                        array(
     1534                                                'domain' => 'another-invalid-domain.com',
     1535                                        ),
     1536                                ),
     1537                                array(
     1538                                        array(
     1539                                                'path' => '',
     1540                                        ),
     1541                                        array(
     1542                                                'path' => '/',
     1543                                        ),
     1544                                ),
     1545                                array(
     1546                                        array(
     1547                                                'public'   => '0',
     1548                                                'archived' => '1',
     1549                                                'mature'   => '1',
     1550                                                'spam'     => true,
     1551                                                'deleted'  => true,
     1552                                        ),
     1553                                        array(
     1554                                                'public'   => 0,
     1555                                                'archived' => 1,
     1556                                                'mature'   => 1,
     1557                                                'spam'     => 1,
     1558                                                'deleted'  => 1,
     1559                                        ),
     1560                                ),
     1561                                array(
     1562                                        array(
     1563                                                'registered'   => '',
     1564                                                'last_updated' => '',
     1565                                        ),
     1566                                        array(),
     1567                                ),
     1568                                array(
     1569                                        array(
     1570                                                'registered'   => '0000-00-00 00:00:00',
     1571                                                'last_updated' => '0000-00-00 00:00:00',
     1572                                        ),
     1573                                        array(),
     1574                                ),
     1575                        );
     1576                }
     1577
     1578                /**
     1579                 * @ticket 40364
     1580                 * @dataProvider data_wp_validate_site_data
     1581                 */
     1582                public function test_wp_validate_site_data( $data, $expected_errors ) {
     1583                        $result = new WP_Error();
     1584                        wp_validate_site_data( $result, $data );
     1585
     1586                        if ( empty( $expected_errors ) ) {
     1587                                $this->assertEmpty( $result->errors );
     1588                        } else {
     1589                                $this->assertEqualSets( $expected_errors, array_keys( $result->errors ) );
     1590                        }
     1591                }
     1592
     1593                public function data_wp_validate_site_data() {
     1594                        $date = current_time( 'mysql', true );
     1595
     1596                        return array(
     1597                                array(
     1598                                        array(
     1599                                                'domain'       => 'example-site.com',
     1600                                                'path'         => '/',
     1601                                                'network_id'   => 1,
     1602                                                'registered'   => $date,
     1603                                                'last_updated' => $date,
     1604                                        ),
     1605                                        array(),
     1606                                ),
     1607                                array(
     1608                                        array(
     1609                                                'path'         => '/',
     1610                                                'network_id'   => 1,
     1611                                                'registered'   => $date,
     1612                                                'last_updated' => $date,
     1613                                        ),
     1614                                        array( 'site_empty_domain' ),
     1615                                ),
     1616                                array(
     1617                                        array(
     1618                                                'domain'       => 'example-site.com',
     1619                                                'network_id'   => 1,
     1620                                                'registered'   => $date,
     1621                                                'last_updated' => $date,
     1622                                        ),
     1623                                        array( 'site_empty_path' ),
     1624                                ),
     1625                                array(
     1626                                        array(
     1627                                                'domain'       => 'example-site.com',
     1628                                                'path'         => '/',
     1629                                                'registered'   => $date,
     1630                                                'last_updated' => $date,
     1631                                        ),
     1632                                        array( 'site_empty_network_id' ),
     1633                                ),
     1634                                array(
     1635                                        array(
     1636                                                'domain'       => get_site()->domain,
     1637                                                'path'         => get_site()->path,
     1638                                                'network_id'   => get_site()->network_id,
     1639                                                'registered'   => $date,
     1640                                                'last_updated' => $date,
     1641                                        ),
     1642                                        array( 'site_taken' ),
     1643                                ),
     1644                                array(
     1645                                        array(
     1646                                                'domain'       => 'valid-domain.com',
     1647                                                'path'         => '/valid-path/',
     1648                                                'network_id'   => 1,
     1649                                                'registered'   => '',
     1650                                                'last_updated' => $date,
     1651                                        ),
     1652                                        array( 'site_empty_registered' ),
     1653                                ),
     1654                                array(
     1655                                        array(
     1656                                                'domain'       => 'valid-domain.com',
     1657                                                'path'         => '/valid-path/',
     1658                                                'network_id'   => 1,
     1659                                                'registered'   => $date,
     1660                                                'last_updated' => '',
     1661                                        ),
     1662                                        array( 'site_empty_last_updated' ),
     1663                                ),
     1664                                array(
     1665                                        array(
     1666                                                'domain'       => 'valid-domain.com',
     1667                                                'path'         => '/valid-path/',
     1668                                                'network_id'   => 1,
     1669                                                'registered'   => '2000-13-32 25:25:61',
     1670                                                'last_updated' => $date,
     1671                                        ),
     1672                                        array( 'site_invalid_registered' ),
     1673                                ),
     1674                                array(
     1675                                        array(
     1676                                                'domain'       => 'valid-domain.com',
     1677                                                'path'         => '/valid-path/',
     1678                                                'network_id'   => 1,
     1679                                                'registered'   => $date,
     1680                                                'last_updated' => '2000-13-32 25:25:61',
     1681                                        ),
     1682                                        array( 'site_invalid_last_updated' ),
     1683                                ),
     1684                                array(
     1685                                        array(
     1686                                                'domain'       => 'valid-domain.com',
     1687                                                'path'         => '/valid-path/',
     1688                                                'network_id'   => 1,
     1689                                                'registered'   => '0000-00-00 00:00:00',
     1690                                                'last_updated' => $date,
     1691                                        ),
     1692                                        array(),
     1693                                ),
     1694                                array(
     1695                                        array(
     1696                                                'domain'       => 'valid-domain.com',
     1697                                                'path'         => '/valid-path/',
     1698                                                'network_id'   => 1,
     1699                                                'registered'   => $date,
     1700                                                'last_updated' => '0000-00-00 00:00:00',
     1701                                        ),
     1702                                        array(),
     1703                                ),
     1704                        );
     1705                }
     1706
     1707                /**
     1708                 * @ticket 40364
     1709                 */
     1710                public function test_site_dates_are_gmt() {
     1711                        $first_date = current_time( 'mysql', true );
     1712                        $site_id    = wp_insert_site( array(
     1713                                'domain'     => 'valid-domain.com',
     1714                                'path'       => '/valid-path/',
     1715                                'network_id' => 1,
     1716                        ) );
     1717                        $this->assertInternalType( 'integer', $site_id );
     1718
     1719                        $site = get_site( $site_id );
     1720                        $this->assertSame( $first_date, $site->registered );
     1721                        $this->assertSame( $first_date, $site->last_updated );
     1722
     1723                        $second_date = current_time( 'mysql', true );
     1724                        $site_id     = wp_update_site( $site_id, array() );
     1725                        $this->assertInternalType( 'integer', $site_id );
     1726
     1727                        $site = get_site( $site_id );
     1728                        $this->assertSame( $first_date, $site->registered );
     1729                        $this->assertSame( $second_date, $site->last_updated );
     1730                }
     1731
     1732                /**
     1733                 * @ticket 40364
     1734                 */
     1735                public function test_wp_delete_site_cleans_cache() {
     1736                        $site_id = self::factory()->blog->create();
     1737
     1738                        get_site( $site_id );
     1739
     1740                        wp_delete_site( $site_id );
     1741
     1742                        $this->assertNull( get_site( $site_id ) );
     1743                }
     1744
     1745                /**
     1746                 * @ticket 40364
     1747                 */
     1748                public function test_wp_update_site_cleans_old_cache_on_domain_change() {
     1749                        $old_domain = 'old.wordpress.org';
     1750                        $new_domain = 'new.wordpress.org';
     1751
     1752                        $site = self::factory()->blog->create_and_get( array(
     1753                                'domain' => $old_domain,
     1754                                'path'   => '/',
     1755                        ) );
     1756
     1757                        // Populate the caches.
     1758                        get_blog_details( array(
     1759                                'domain' => $old_domain,
     1760                                'path'   => '/',
     1761                        ) );
     1762                        get_blog_id_from_url( $old_domain, '/' );
     1763                        get_blog_details( array(
     1764                                'domain' => $new_domain,
     1765                                'path'   => '/',
     1766                        ) );
     1767                        get_blog_id_from_url( $new_domain, '/' );
     1768
     1769                        wp_update_site( $site->id, array(
     1770                                'domain' => $new_domain,
     1771                        ) );
     1772
     1773                        $domain_path_key_old = md5( $old_domain . '/' );
     1774                        $domain_path_key_new = md5( $new_domain . '/' );
     1775
     1776                        // Ensure all respective cache values are empty.
     1777                        $result = array(
     1778                                wp_cache_get( $domain_path_key_old, 'blog-lookup' ),
     1779                                wp_cache_get( $domain_path_key_old, 'blog-id-cache' ),
     1780                                wp_cache_get( 'current_blog_' . $old_domain, 'site-options' ),
     1781                                wp_cache_get( 'current_blog_' . $old_domain . '/', 'site-options' ),
     1782                                wp_cache_get( $domain_path_key_new, 'blog-lookup' ),
     1783                                wp_cache_get( $domain_path_key_new, 'blog-id-cache' ),
     1784                                wp_cache_get( 'current_blog_' . $new_domain, 'site-options' ),
     1785                                wp_cache_get( 'current_blog_' . $new_domain . '/', 'site-options' ),
     1786                        );
     1787
     1788                        $this->assertEmpty( array_filter( $result ) );
     1789                }
     1790
     1791                /**
     1792                 * @ticket 40364
     1793                 */
     1794                public function test_wp_update_site_cleans_old_cache_on_path_change() {
     1795                        $old_path = '/foo/';
     1796                        $new_path = '/bar/';
     1797
     1798                        $site = self::factory()->blog->create_and_get( array(
     1799                                'domain' => 'test.wordpress.org',
     1800                                'path'   => $old_path,
     1801                        ) );
     1802
     1803                        // Populate the caches.
     1804                        get_blog_details( array(
     1805                                'domain' => 'test.wordpress.org',
     1806                                'path'   => $old_path,
     1807                        ) );
     1808                        get_blog_id_from_url( 'test.wordpress.org', $old_path );
     1809                        get_blog_details( array(
     1810                                'domain' => 'test.wordpress.org',
     1811                                'path'   => $new_path,
     1812                        ) );
     1813                        get_blog_id_from_url( 'test.wordpress.org', $new_path );
     1814
     1815                        wp_update_site( $site->id, array(
     1816                                'path' => $new_path,
     1817                        ) );
     1818
     1819                        $domain_path_key_old = md5( 'test.wordpress.org' . $old_path );
     1820                        $domain_path_key_new = md5( 'test.wordpress.org' . $new_path );
     1821
     1822                        // Ensure all respective cache values are empty.
     1823                        $result = array(
     1824                                wp_cache_get( $domain_path_key_old, 'blog-lookup' ),
     1825                                wp_cache_get( $domain_path_key_old, 'blog-id-cache' ),
     1826                                wp_cache_get( 'current_blog_test.wordpress.org' . $old_path, 'site-options' ),
     1827                                wp_cache_get( $domain_path_key_new, 'blog-lookup' ),
     1828                                wp_cache_get( $domain_path_key_new, 'blog-id-cache' ),
     1829                                wp_cache_get( 'current_blog_test.wordpress.org' . $new_path, 'site-options' ),
     1830                        );
     1831
     1832                        $this->assertEmpty( array_filter( $result ) );
     1833                }
     1834
     1835                /**
     1836                 * @ticket 40364
     1837                 * @dataProvider data_site_status_hook_triggers
     1838                 */
     1839                public function test_site_status_hook_triggers( $insert_site_data, $expected_insert_hooks, $update_site_data, $expected_update_hooks ) {
     1840                        // First: Insert a site.
     1841                        $this->listen_to_site_status_hooks();
     1842
     1843                        $site_data = array_merge( array(
     1844                                'domain' => 'example-site.com',
     1845                                'path'   => '/',
     1846                        ), $insert_site_data );
     1847
     1848                        $site_id = wp_insert_site( $site_data );
     1849
     1850                        $insert_expected = array_fill_keys( $expected_insert_hooks, $site_id );
     1851                        $insert_result   = $this->get_listen_to_site_status_hooks_result();
     1852
     1853                        // Second: Update that site.
     1854                        $this->listen_to_site_status_hooks();
     1855
     1856                        wp_update_site( $site_id, $update_site_data );
     1857
     1858                        $update_expected = array_fill_keys( $expected_update_hooks, $site_id );
     1859                        $update_result   = $this->get_listen_to_site_status_hooks_result();
     1860
     1861                        // Check both insert and update results.
     1862                        $this->assertEqualSetsWithIndex( $insert_expected, $insert_result );
     1863                        $this->assertEqualSetsWithIndex( $update_expected, $update_result );
     1864                }
     1865
     1866                public function data_site_status_hook_triggers() {
     1867                        return array(
     1868                                array(
     1869                                        array(
     1870                                                'public'   => 1,
     1871                                                'archived' => 1,
     1872                                                'mature'   => 1,
     1873                                                'spam'     => 1,
     1874                                                'deleted'  => 1,
     1875                                        ),
     1876                                        array(
     1877                                                'archive_blog',
     1878                                                'mature_blog',
     1879                                                'make_spam_blog',
     1880                                                'make_delete_blog',
     1881                                        ),
     1882                                        array(
     1883                                                'public'   => 0,
     1884                                                'archived' => 0,
     1885                                                'mature'   => 0,
     1886                                                'spam'     => 0,
     1887                                                'deleted'  => 0,
     1888                                        ),
     1889                                        array(
     1890                                                'update_blog_public',
     1891                                                'unarchive_blog',
     1892                                                'unmature_blog',
     1893                                                'make_ham_blog',
     1894                                                'make_undelete_blog',
     1895                                        ),
     1896                                ),
     1897                                array(
     1898                                        array(
     1899                                                'public'   => 0,
     1900                                                'archived' => 0,
     1901                                                'mature'   => 0,
     1902                                                'spam'     => 0,
     1903                                                'deleted'  => 0,
     1904                                        ),
     1905                                        array(
     1906                                                'update_blog_public',
     1907                                        ),
     1908                                        array(
     1909                                                'public'   => 1,
     1910                                                'archived' => 1,
     1911                                                'mature'   => 1,
     1912                                                'spam'     => 1,
     1913                                                'deleted'  => 1,
     1914                                        ),
     1915                                        array(
     1916                                                'update_blog_public',
     1917                                                'archive_blog',
     1918                                                'mature_blog',
     1919                                                'make_spam_blog',
     1920                                                'make_delete_blog',
     1921                                        ),
     1922                                ),
     1923                                array(
     1924                                        array(
     1925                                                'public'   => 0,
     1926                                                'archived' => 0,
     1927                                                'mature'   => 1,
     1928                                                'spam'     => 1,
     1929                                                'deleted'  => 1,
     1930                                        ),
     1931                                        array(
     1932                                                'update_blog_public',
     1933                                                'mature_blog',
     1934                                                'make_spam_blog',
     1935                                                'make_delete_blog',
     1936                                        ),
     1937                                        array(
     1938                                                'public'   => 0,
     1939                                                'archived' => 1,
     1940                                                'mature'   => 1,
     1941                                                'spam'     => 1,
     1942                                                'deleted'  => 0,
     1943                                        ),
     1944                                        array(
     1945                                                'archive_blog',
     1946                                                'make_undelete_blog',
     1947                                        ),
     1948                                ),
     1949                        );
     1950                }
     1951
     1952                private function listen_to_site_status_hooks() {
     1953                        $this->site_status_hooks = array();
     1954
     1955                        $hooknames = array(
     1956                                'make_spam_blog',
     1957                                'make_ham_blog',
     1958                                'mature_blog',
     1959                                'unmature_blog',
     1960                                'archive_blog',
     1961                                'unarchive_blog',
     1962                                'make_delete_blog',
     1963                                'make_undelete_blog',
     1964                                'update_blog_public',
     1965                        );
     1966
     1967                        foreach ( $hooknames as $hookname ) {
     1968                                add_action( $hookname, array( $this, 'action_site_status_hook' ), 10, 1 );
     1969                        }
     1970                }
     1971
     1972                private function get_listen_to_site_status_hooks_result() {
     1973                        $hooknames = array(
     1974                                'make_spam_blog',
     1975                                'make_ham_blog',
     1976                                'mature_blog',
     1977                                'unmature_blog',
     1978                                'archive_blog',
     1979                                'unarchive_blog',
     1980                                'make_delete_blog',
     1981                                'make_undelete_blog',
     1982                                'update_blog_public',
     1983                        );
     1984
     1985                        foreach ( $hooknames as $hookname ) {
     1986                                remove_action( $hookname, array( $this, 'action_site_status_hook' ), 10 );
     1987                        }
     1988
     1989                        return $this->site_status_hooks;
     1990                }
     1991
     1992                public function action_site_status_hook( $site_id ) {
     1993                        $this->site_status_hooks[ current_action() ] = $site_id;
     1994                }
    12621995        }
    12631996
    12641997endif;