Make WordPress Core

Ticket #40364: 40364.11.diff

File 40364.11.diff, 51.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. Must always be provided.
     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'   => 0,
     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        /**
     446         * Filters passed site data in order to normalize it.
     447         *
     448         * @since 5.0.0
     449         *
     450         * @param array $data Associative array of site data passed to the respective function.
     451         *                    See {@see wp_insert_site()} for the possibly included data.
     452         */
     453        $data = apply_filters( 'wp_normalize_site_data', $data );
     454
     455        $data = array_intersect_key( wp_parse_args( $data, $defaults ), $defaults );
     456
     457        // Use the current network if none is set.
     458        if ( empty( $data['network_id'] ) ) {
     459                $data['network_id'] = get_current_network_id();
     460        }
     461
     462        $validity = new WP_Error();
     463
     464        /**
     465         * Fires when data should be validated for a site prior to inserting or updating in the database.
     466         *
     467         * Plugins should amend the `$validity` object via its `WP_Error::add()` method.
     468         *
     469         * @since 5.0.0
     470         *
     471         * @param WP_Error     $validity Error object to add validation errors to.
     472         * @param array        $data     Associative array of complete site data. See {@see wp_insert_site()}
     473         *                               for the included data.
     474         * @param WP_Site|null $old_site The old site object if the data belongs to a site being updated,
     475         *                               or null if it is a new site being inserted.
     476         */
     477        do_action( 'wp_validate_site_data', $validity, $data, null );
     478
     479        if ( ! empty( $validity->errors ) ) {
     480                return $validity;
     481        }
     482
     483        // Prepare for database.
     484        $data['site_id'] = $data['network_id'];
     485        unset( $data['network_id'] );
     486
     487        if ( false === $wpdb->insert( $wpdb->blogs, $data ) ) {
     488                return new WP_Error( 'db_insert_error', __( 'Could not insert site into the database.' ), $wpdb->last_error );
     489        }
     490
     491        $new_site = get_site( $wpdb->insert_id );
     492
     493        clean_blog_cache( $new_site );
     494
     495        /**
     496         * Fires once a site has been inserted into the database.
     497         *
     498         * @since 5.0.0
     499         *
     500         * @param WP_Site $new_site New site object.
     501         */
     502        do_action( 'wp_insert_site', $new_site );
     503
     504        return (int) $new_site->id;
     505}
     506
     507/**
     508 * Updates a site in the database.
     509 *
     510 * @since 5.0.0
     511 *
     512 * @global wpdb $wpdb WordPress database abstraction object.
     513 *
     514 * @param int   $site_id ID of the site that should be updated.
     515 * @param array $data    Site data to update. See {@see wp_insert_site()} for the list of supported keys.
     516 * @return int|WP_Error The updated site's ID on success, or error object on failure.
     517 */
     518function wp_update_site( $site_id, array $data ) {
     519        global $wpdb;
     520
     521        $old_site = get_site( $site_id );
     522        if ( ! $old_site ) {
     523                return new WP_Error( 'site_not_exist', __( 'Site does not exist.' ) );
     524        }
     525
     526        $defaults                 = $old_site->to_array();
     527        $defaults['network_id']   = (int) $defaults['site_id'];
     528        $defaults['last_updated'] = current_time( 'mysql', true );
     529        unset( $defaults['blog_id'], $defaults['site_id'] );
     530
     531        /** This filter is documented in wp-includes/ms-blogs.php */
     532        $data = apply_filters( 'wp_normalize_site_data', $data );
     533
     534        $whitelist = array( 'domain', 'path', 'network_id', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' );
     535        $data = array_intersect_key( wp_parse_args( $data, $defaults ), array_flip( $whitelist ) );
     536
     537        // Use the previously set network if a falsy network ID has been passed.
     538        if ( empty( $data['network_id'] ) ) {
     539                $data['network_id'] = $defaults['network_id'];
     540        }
     541
     542        $validity = new WP_Error();
     543
     544        /** This action is documented in wp-includes/ms-blogs.php */
     545        do_action( 'wp_validate_site_data', $validity, $data, $old_site );
     546
     547        if ( ! empty( $validity->errors ) ) {
     548                return $validity;
     549        }
     550
     551        // Prepare for database.
     552        $data['site_id'] = $data['network_id'];
     553        unset( $data['network_id'] );
     554
     555        if ( false === $wpdb->update( $wpdb->blogs, $data, array( 'blog_id' => $old_site->id ) ) ) {
     556                return new WP_Error( 'db_update_error', __( 'Could not update site in the database.' ), $wpdb->last_error );
     557        }
     558
     559        clean_blog_cache( $old_site );
     560
     561        $new_site = get_site( $old_site->id );
     562
     563        /**
     564         * Fires once a site has been updated in the database.
     565         *
     566         * @since 5.0.0
     567         *
     568         * @param WP_Site $new_site New site object.
     569         * @param WP_Site $old_site Old site object.
     570         */
     571        do_action( 'wp_update_site', $new_site, $old_site );
     572
     573        return (int) $new_site->id;
     574}
     575
     576/**
     577 * Deletes a site from the database.
     578 *
     579 * @since 5.0.0
     580 *
     581 * @global wpdb $wpdb WordPress database abstraction object.
     582 *
     583 * @param int $site_id ID of the site that should be deleted.
     584 * @return WP_Site|WP_Error The deleted site object on success, or error object on failure.
     585 */
     586function wp_delete_site( $site_id ) {
     587        global $wpdb;
     588
     589        $old_site = get_site( $site_id );
     590        if ( ! $old_site ) {
     591                return new WP_Error( 'site_not_exist', __( 'Site does not exist.' ) );
     592        }
     593
     594        if ( false === $wpdb->delete( $wpdb->blogs, array( 'blog_id' => $old_site->id ) ) ) {
     595                return new WP_Error( 'db_delete_error', __( 'Could not delete site from the database.' ), $wpdb->last_error );
     596        }
     597
     598        clean_blog_cache( $old_site );
     599
     600        /**
     601         * Fires once a site has been deleted from the database.
     602         *
     603         * @since 5.0.0
     604         *
     605         * @param WP_Site $old_site Deleted site object.
     606         */
     607        do_action( 'wp_delete_site', $old_site );
     608
     609        return $old_site;
     610}
     611
     612/**
    521613 * Retrieves site data given a site ID or site object.
    522614 *
    523615 * Site data will be cached and returned after being passed through a filter.
     
    688780}
    689781
    690782/**
     783 * Normalizes data for a site prior to inserting or updating in the database.
     784 *
     785 * @since 5.0.0
     786 *
     787 * @param array $data Associative array of site data passed to the respective function.
     788 *                    See {@see wp_insert_site()} for the possibly included data.
     789 * @return array Normalized site data.
     790 */
     791function wp_normalize_site_data( $data ) {
     792        // Maintain backward-compatibility with `$site_id` as network ID.
     793        if ( isset( $data['site_id'] ) ) {
     794                if ( ! empty( $data['site_id'] ) && empty( $data['network_id'] ) ) {
     795                        $data['network_id'] = $data['site_id'];
     796                }
     797                unset( $data['site_id'] );
     798        }
     799
     800        // Sanitize domain if passed.
     801        if ( array_key_exists( 'domain', $data ) ) {
     802                $data['domain'] = trim( $data['domain'] );
     803                $data['domain'] = preg_replace( '/\s+/', '', sanitize_user( $data['domain'], true ) );
     804                if ( is_subdomain_install() ) {
     805                        $data['domain'] = str_replace( '@', '', $data['domain'] );
     806                }
     807        }
     808
     809        // Sanitize path if passed.
     810        if ( array_key_exists( 'path', $data ) ) {
     811                $data['path'] = trailingslashit( '/' . trim( $data['path'], '/' ) );
     812        }
     813
     814        // Sanitize network ID if passed.
     815        if ( array_key_exists( 'network_id', $data ) ) {
     816                $data['network_id'] = (int) $data['network_id'];
     817        }
     818
     819        // Sanitize status fields if passed.
     820        $status_fields = array( 'public', 'archived', 'mature', 'spam', 'deleted' );
     821        foreach ( $status_fields as $status_field ) {
     822                if ( array_key_exists( $status_field, $data ) ) {
     823                        $data[ $status_field ] = (int) $data[ $status_field ];
     824                }
     825        }
     826
     827        // Strip date fields if empty.
     828        $date_fields = array( 'registered', 'last_updated' );
     829        foreach ( $date_fields as $date_field ) {
     830                if ( ! array_key_exists( $date_field, $data ) ) {
     831                        continue;
     832                }
     833
     834                if ( empty( $data[ $date_field ] ) || '0000-00-00 00:00:00' === $data[ $date_field ] ) {
     835                        unset( $data[ $date_field ] );
     836                }
     837        }
     838
     839        return $data;
     840}
     841
     842/**
     843 * Validates data for a site prior to inserting or updating in the database.
     844 *
     845 * @since 5.0.0
     846 *
     847 * @param WP_Error     $validity Error object, passed by reference. Will contain validation errors if
     848 *                               any occurred.
     849 * @param array        $data     Associative array of complete site data. See {@see wp_insert_site()}
     850 *                               for the included data.
     851 * @param WP_Site|null $old_site The old site object if the data belongs to a site being updated,
     852 *                               or null if it is a new site being inserted.
     853 */
     854function wp_validate_site_data( $validity, $data, $old_site = null ) {
     855        // A domain must always be present.
     856        if ( empty( $data['domain'] ) ) {
     857                $validity->add( 'site_empty_domain', __( 'Site domain must not be empty.' ) );
     858        }
     859
     860        // A path must always be present.
     861        if ( empty( $data['path'] ) ) {
     862                $validity->add( 'site_empty_path', __( 'Site path must not be empty.' ) );
     863        }
     864
     865        // A network ID must always be present.
     866        if ( empty( $data['network_id'] ) ) {
     867                $validity->add( 'site_empty_network_id', __( 'Site network ID must be provided.' ) );
     868        }
     869
     870        // Both registration and last updated dates must always be present and valid.
     871        $date_fields = array( 'registered', 'last_updated' );
     872        foreach ( $date_fields as $date_field ) {
     873                if ( empty( $data[ $date_field ] ) ) {
     874                        $validity->add( 'site_empty_' . $date_field, __( 'Both registration and last updated dates must be provided.' ) );
     875                        break;
     876                }
     877
     878                $month      = substr( $data[ $date_field ], 5, 2 );
     879                $day        = substr( $data[ $date_field ], 8, 2 );
     880                $year       = substr( $data[ $date_field ], 0, 4 );
     881                $valid_date = wp_checkdate( $month, $day, $year, $data[ $date_field ] );
     882                if ( ! $valid_date ) {
     883                        $validity->add( 'site_invalid_' . $date_field, __( 'Both registration and last updated dates must be valid dates.' ) );
     884                        break;
     885                }
     886        }
     887
     888        if ( ! empty( $validity->errors ) ) {
     889                return;
     890        }
     891
     892        // If a new site, or domain/path/network ID have changed, ensure uniqueness.
     893        if ( ! $old_site
     894                || $data['domain'] !== $old_site->domain
     895                || $data['path'] !== $old_site->path
     896                || $data['network_id'] !== $old_site->network_id
     897        ) {
     898                if ( domain_exists( $data['domain'], $data['path'], $data['network_id'] ) ) {
     899                        $validity->add( 'site_taken', __( 'Sorry, that site already exists!' ) );
     900                }
     901        }
     902}
     903
     904/**
    691905 * Retrieve option value for a given blog id based on name of option.
    692906 *
    693907 * If the option does not exist or does not have a value, then the return value
     
    11931407 * Update a blog details field.
    11941408 *
    11951409 * @since MU (3.0.0)
     1410 * @since 5.0.0 Use wp_update_site() internally.
    11961411 *
    11971412 * @global wpdb $wpdb WordPress database abstraction object.
    11981413 *
     
    12131428                return $value;
    12141429        }
    12151430
    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         );
     1431        $result = wp_update_site( $blog_id, array(
     1432                $pref => $value,
     1433        ) );
    12221434
    1223         if ( false === $result ) {
     1435        if ( is_wp_error( $result ) ) {
    12241436                return false;
    12251437        }
    12261438
    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 
    12731439        return $value;
    12741440}
    12751441
     
    15371703
    15381704        update_posts_count();
    15391705}
     1706
     1707/**
     1708 * Updates the count of sites for a network based on a changed site.
     1709 *
     1710 * @since 5.0.0
     1711 *
     1712 * @param WP_Site      $new_site The site object that has been inserted, updated or deleted.
     1713 * @param WP_Site|null $old_site Optional. If $new_site has been updated, this must be the previous
     1714 *                               state of that site. Default null.
     1715 */
     1716function wp_maybe_update_network_site_counts_on_update( $new_site, $old_site = null ) {
     1717        if ( null === $old_site ) {
     1718                wp_maybe_update_network_site_counts( $new_site->network_id );
     1719                return;
     1720        }
     1721
     1722        if ( $new_site->network_id != $old_site->network_id ) {
     1723                wp_maybe_update_network_site_counts( $new_site->network_id );
     1724                wp_maybe_update_network_site_counts( $old_site->network_id );
     1725        }
     1726}
     1727
     1728/**
     1729 * Triggers actions on site status updates.
     1730 *
     1731 * @since 5.0.0
     1732 *
     1733 * @param WP_Site      $new_site The site object after the update.
     1734 * @param WP_Site|null $old_site Optional. If $new_site has been updated, this must be the previous
     1735 *                               state of that site. Default null.
     1736 */
     1737function wp_maybe_transition_site_statuses_on_update( $new_site, $old_site = null ) {
     1738        $site_id = $new_site->id;
     1739
     1740        // Use the default values for a site if no previous state is given.
     1741        if ( ! $old_site ) {
     1742                $old_site = new WP_Site( new stdClass() );
     1743        }
     1744
     1745        if ( $new_site->spam != $old_site->spam ) {
     1746                if ( $new_site->spam == 1 ) {
     1747
     1748                        /**
     1749                         * Fires when the 'spam' status is added to a site.
     1750                         *
     1751                         * @since MU (3.0.0)
     1752                         *
     1753                         * @param int $site_id Site ID.
     1754                         */
     1755                        do_action( 'make_spam_blog', $site_id );
     1756                } else {
     1757
     1758                        /**
     1759                         * Fires when the 'spam' status is removed from a site.
     1760                         *
     1761                         * @since MU (3.0.0)
     1762                         *
     1763                         * @param int $site_id Site ID.
     1764                         */
     1765                        do_action( 'make_ham_blog', $site_id );
     1766                }
     1767        }
     1768
     1769        if ( $new_site->mature != $old_site->mature ) {
     1770                if ( $new_site->mature == 1 ) {
     1771
     1772                        /**
     1773                         * Fires when the 'mature' status is added to a site.
     1774                         *
     1775                         * @since 3.1.0
     1776                         *
     1777                         * @param int $site_id Site ID.
     1778                         */
     1779                        do_action( 'mature_blog', $site_id );
     1780                } else {
     1781
     1782                        /**
     1783                         * Fires when the 'mature' status is removed from a site.
     1784                         *
     1785                         * @since 3.1.0
     1786                         *
     1787                         * @param int $site_id Site ID.
     1788                         */
     1789                        do_action( 'unmature_blog', $site_id );
     1790                }
     1791        }
     1792
     1793        if ( $new_site->archived != $old_site->archived ) {
     1794                if ( $new_site->archived == 1 ) {
     1795
     1796                        /**
     1797                         * Fires when the 'archived' status is added to a site.
     1798                         *
     1799                         * @since MU (3.0.0)
     1800                         *
     1801                         * @param int $site_id Site ID.
     1802                         */
     1803                        do_action( 'archive_blog', $site_id );
     1804                } else {
     1805
     1806                        /**
     1807                         * Fires when the 'archived' status is removed from a site.
     1808                         *
     1809                         * @since MU (3.0.0)
     1810                         *
     1811                         * @param int $site_id Site ID.
     1812                         */
     1813                        do_action( 'unarchive_blog', $site_id );
     1814                }
     1815        }
     1816
     1817        if ( $new_site->deleted != $old_site->deleted ) {
     1818                if ( $new_site->deleted == 1 ) {
     1819
     1820                        /**
     1821                         * Fires when the 'deleted' status is added to a site.
     1822                         *
     1823                         * @since 3.5.0
     1824                         *
     1825                         * @param int $site_id Site ID.
     1826                         */
     1827                        do_action( 'make_delete_blog', $site_id );
     1828                } else {
     1829
     1830                        /**
     1831                         * Fires when the 'deleted' status is removed from a site.
     1832                         *
     1833                         * @since 3.5.0
     1834                         *
     1835                         * @param int $site_id Site ID.
     1836                         */
     1837                        do_action( 'make_undelete_blog', $site_id );
     1838                }
     1839        }
     1840
     1841        if ( $new_site->public != $old_site->public ) {
     1842
     1843                /**
     1844                 * Fires after the current blog's 'public' setting is updated.
     1845                 *
     1846                 * @since MU (3.0.0)
     1847                 *
     1848                 * @param int    $site_id Site ID.
     1849                 * @param string $value   The value of the site status.
     1850                 */
     1851                do_action( 'update_blog_public', $site_id, $new_site->public );
     1852        }
     1853}
     1854
     1855/**
     1856 * Cleans the necessary caches after specific site data has been updated.
     1857 *
     1858 * @since 5.0.0
     1859 *
     1860 * @param WP_Site $new_site The site object after the update.
     1861 * @param WP_Site $old_site The site obejct prior to the update.
     1862 */
     1863function wp_maybe_clean_new_site_cache_on_update( $new_site, $old_site ) {
     1864        if ( $old_site->domain !== $new_site->domain || $old_site->path !== $new_site->path ) {
     1865                clean_blog_cache( $new_site );
     1866        }
     1867}
     1868
     1869/**
     1870 * Updates the `blog_public` option for a given site ID.
     1871 *
     1872 * @since 5.0.0
     1873 *
     1874 * @param int    $site_id Site ID.
     1875 * @param string $public  The value of the site status.
     1876 */
     1877function wp_update_blog_public_option_on_site_update( $site_id, $public ) {
     1878        update_blog_option( $site_id, 'blog_public', $public );
     1879}
  • 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                                        ), array(
     1303                                                'domain'     => 'example.com',
     1304                                                'path'       => '/foo/',
     1305                                                'network_id' => 2,
     1306                                        ),
     1307                                ),
     1308                                array(
     1309                                        array(
     1310                                                'domain'  => 'example.com',
     1311                                                'path'    => '/bar/',
     1312                                                'site_id' => 2,
     1313                                                'public'  => 0,
     1314                                        ), array(
     1315                                                'domain'     => 'example.com',
     1316                                                'path'       => '/bar/',
     1317                                                'network_id' => 2,
     1318                                                'public'     => 0,
     1319                                        ),
     1320                                ),
     1321                                array(
     1322                                        array(
     1323                                                'domain'   => 'example.com',
     1324                                                'path'     => 'foobar',
     1325                                                'public'   => 0,
     1326                                                'archived' => 1,
     1327                                                'mature'   => 1,
     1328                                                'spam'     => 1,
     1329                                                'deleted'  => 1,
     1330                                                'lang_id'  => 1,
     1331                                        ), array(
     1332                                                'domain'   => 'example.com',
     1333                                                'path'     => '/foobar/',
     1334                                                'public'   => 0,
     1335                                                'archived' => 1,
     1336                                                'mature'   => 1,
     1337                                                'spam'     => 1,
     1338                                                'deleted'  => 1,
     1339                                                'lang_id'  => 1,
     1340                                        ),
     1341                                ),
     1342                        );
     1343                }
     1344
     1345                /**
     1346                 * @ticket 40364
     1347                 */
     1348                public function test_wp_insert_site_empty_domain() {
     1349                        $site_id = wp_insert_site( array( 'public' => 0 ) );
     1350
     1351                        $this->assertWPError( $site_id );
     1352                        $this->assertSame( 'site_empty_domain', $site_id->get_error_code() );
     1353                }
     1354
     1355                /**
     1356                 * @ticket 40364
     1357                 * @dataProvider data_wp_update_site
     1358                 */
     1359                public function test_wp_update_site( $site_data, $expected_data ) {
     1360                        $site_id = self::factory()->blog->create();
     1361
     1362                        $old_site = get_site( $site_id );
     1363
     1364                        $result = wp_update_site( $site_id, $site_data );
     1365
     1366                        $this->assertSame( $site_id, $result );
     1367
     1368                        $new_site = get_site( $site_id );
     1369                        foreach ( $new_site->to_array() as $key => $value ) {
     1370                                if ( isset( $expected_data[ $key ] ) ) {
     1371                                        $this->assertEquals( $expected_data[ $key ], $value );
     1372                                } elseif ( 'last_updated' === $key ) {
     1373                                        $this->assertTrue( $old_site->last_updated <= $value );
     1374                                } else {
     1375                                        $this->assertEquals( $old_site->$key, $value );
     1376                                }
     1377                        }
     1378                }
     1379
     1380                public function data_wp_update_site() {
     1381                        return array(
     1382                                array(
     1383                                        array(
     1384                                                'domain'     => 'example.com',
     1385                                                'network_id' => 2,
     1386                                        ),
     1387                                        array(
     1388                                                'domain'  => 'example.com',
     1389                                                'site_id' => 2,
     1390                                        ),
     1391                                ),
     1392                                array(
     1393                                        array(
     1394                                                'path' => 'foo',
     1395                                        ),
     1396                                        array(
     1397                                                'path' => '/foo/'
     1398                                        ),
     1399                                ),
     1400                                array(
     1401                                        array(
     1402                                                'public'   => 0,
     1403                                                'archived' => 1,
     1404                                                'mature'   => 1,
     1405                                                'spam'     => 1,
     1406                                                'deleted'  => 1,
     1407                                                'lang_id'  => 1,
     1408                                        ),
     1409                                        array(
     1410                                                'public'   => 0,
     1411                                                'archived' => 1,
     1412                                                'mature'   => 1,
     1413                                                'spam'     => 1,
     1414                                                'deleted'  => 1,
     1415                                                'lang_id'  => 1,
     1416                                        ),
     1417                                ),
     1418                        );
     1419                }
     1420
     1421                /**
     1422                 * @ticket 40364
     1423                 */
     1424                public function test_wp_update_site_empty_domain() {
     1425                        $site_id = self::factory()->blog->create();
     1426
     1427                        $result = wp_update_site( $site_id, array( 'domain' => '' ) );
     1428
     1429                        $this->assertWPError( $result );
     1430                        $this->assertSame( 'site_empty_domain', $result->get_error_code() );
     1431                }
     1432
     1433                /**
     1434                 * @ticket 40364
     1435                 */
     1436                public function test_wp_update_site_invalid_id() {
     1437                        $result = wp_update_site( 444444, array( 'domain' => 'example.com' ) );
     1438
     1439                        $this->assertWPError( $result );
     1440                        $this->assertSame( 'site_not_exist', $result->get_error_code() );
     1441                }
     1442
     1443                /**
     1444                 * @ticket 40364
     1445                 */
     1446                public function test_wp_update_site_cleans_cache() {
     1447                        $site_id = self::factory()->blog->create();
     1448                        $site1 = get_site( $site_id );
     1449
     1450                        $result = wp_update_site( $site_id, array( 'public' => 0 ) );
     1451                        $site2 = get_site( $site_id );
     1452
     1453                        $result = wp_update_site( $site_id, array( 'public' => 1 ) );
     1454                        $site3 = get_site( $site_id );
     1455
     1456                        $this->assertEquals( 1, $site1->public );
     1457                        $this->assertEquals( 0, $site2->public );
     1458                        $this->assertEquals( 1, $site3->public );
     1459                }
     1460
     1461                /**
     1462                 * @ticket 40364
     1463                 */
     1464                public function test_wp_delete_site() {
     1465                        $site_id = self::factory()->blog->create();
     1466
     1467                        $site = get_site( $site_id );
     1468
     1469                        $result = wp_delete_site( $site_id );
     1470
     1471                        $this->assertInstanceOf( 'WP_Site', $result );
     1472                        $this->assertEquals( $result->to_array(), $site->to_array() );
     1473                }
     1474
     1475                /**
     1476                 * @ticket 40364
     1477                 */
     1478                public function test_wp_delete_site_invalid_id() {
     1479                        $result = wp_delete_site( 444444 );
     1480
     1481                        $this->assertWPError( $result );
     1482                        $this->assertSame( 'site_not_exist', $result->get_error_code() );
     1483                }
     1484
     1485                /**
     1486                 * @ticket 40364
     1487                 * @dataProvider data_wp_normalize_site_data
     1488                 */
     1489                public function test_wp_normalize_site_data( $data, $expected ) {
     1490                        $result = wp_normalize_site_data( $data );
     1491
     1492                        $this->assertEqualSetsWithIndex( $expected, $result );
     1493                }
     1494
     1495                public function data_wp_normalize_site_data() {
     1496                        return array(
     1497                                array(
     1498                                        array(
     1499                                                'site_id' => 4,
     1500                                        ),
     1501                                        array(
     1502                                                'network_id' => 4,
     1503                                        ),
     1504                                ),
     1505                                array(
     1506                                        array(
     1507                                                'site_id'    => 4,
     1508                                                'network_id' => 5,
     1509                                        ),
     1510                                        array(
     1511                                                'network_id' => 5,
     1512                                        ),
     1513                                ),
     1514                                array(
     1515                                        array(
     1516                                                'network_id' => '4',
     1517                                        ),
     1518                                        array(
     1519                                                'network_id' => 4,
     1520                                        ),
     1521                                ),
     1522                                array(
     1523                                        array(
     1524                                                'domain' => 'invalid domain .com',
     1525                                                'path'   => 'foo',
     1526                                        ),
     1527                                        array(
     1528                                                'domain' => 'invaliddomain.com',
     1529                                                'path'   => '/foo/',
     1530                                        ),
     1531                                ),
     1532                                array(
     1533                                        array(
     1534                                                'domain' => '<yet>/another-invalid-domain.com',
     1535                                        ),
     1536                                        array(
     1537                                                'domain' => 'another-invalid-domain.com',
     1538                                        ),
     1539                                ),
     1540                                array(
     1541                                        array(
     1542                                                'path'   => '',
     1543                                        ),
     1544                                        array(
     1545                                                'path'   => '/',
     1546                                        ),
     1547                                ),
     1548                                array(
     1549                                        array(
     1550                                                'public'   => '0',
     1551                                                'archived' => '1',
     1552                                                'mature'   => '1',
     1553                                                'spam'     => true,
     1554                                                'deleted'  => true,
     1555                                        ),
     1556                                        array(
     1557                                                'public'   => 0,
     1558                                                'archived' => 1,
     1559                                                'mature'   => 1,
     1560                                                'spam'     => 1,
     1561                                                'deleted'  => 1,
     1562                                        ),
     1563                                ),
     1564                                array(
     1565                                        array(
     1566                                                'registered'   => '',
     1567                                                'last_updated' => '',
     1568                                        ),
     1569                                        array(),
     1570                                ),
     1571                                array(
     1572                                        array(
     1573                                                'registered'   => '0000-00-00 00:00:00',
     1574                                                'last_updated' => '0000-00-00 00:00:00',
     1575                                        ),
     1576                                        array(),
     1577                                ),
     1578                        );
     1579                }
     1580
     1581                /**
     1582                 * @ticket 40364
     1583                 * @dataProvider data_wp_validate_site_data
     1584                 */
     1585                public function test_wp_validate_site_data( $data, $expected_errors ) {
     1586                        $result = new WP_Error();
     1587                        wp_validate_site_data( $result, $data );
     1588
     1589                        if ( empty( $expected_errors ) ) {
     1590                                $this->assertEmpty( $result->errors );
     1591                        } else {
     1592                                $this->assertEqualSets( $expected_errors, array_keys( $result->errors ) );
     1593                        }
     1594                }
     1595
     1596                public function data_wp_validate_site_data() {
     1597                        $date = current_time( 'mysql', true );
     1598
     1599                        return array(
     1600                                array(
     1601                                        array(
     1602                                                'domain'       => 'example-site.com',
     1603                                                'path'         => '/',
     1604                                                'network_id'   => 1,
     1605                                                'registered'   => $date,
     1606                                                'last_updated' => $date,
     1607                                        ),
     1608                                        array(),
     1609                                ),
     1610                                array(
     1611                                        array(
     1612                                                'path'         => '/',
     1613                                                'network_id'   => 1,
     1614                                                'registered'   => $date,
     1615                                                'last_updated' => $date,
     1616                                        ),
     1617                                        array( 'site_empty_domain' ),
     1618                                ),
     1619                                array(
     1620                                        array(
     1621                                                'domain'       => 'example-site.com',
     1622                                                'network_id'   => 1,
     1623                                                'registered'   => $date,
     1624                                                'last_updated' => $date,
     1625                                        ),
     1626                                        array( 'site_empty_path' ),
     1627                                ),
     1628                                array(
     1629                                        array(
     1630                                                'domain'       => 'example-site.com',
     1631                                                'path'         => '/',
     1632                                                'registered'   => $date,
     1633                                                'last_updated' => $date,
     1634                                        ),
     1635                                        array( 'site_empty_network_id' ),
     1636                                ),
     1637                                array(
     1638                                        array(
     1639                                                'domain'       => get_site()->domain,
     1640                                                'path'         => get_site()->path,
     1641                                                'network_id'   => get_site()->network_id,
     1642                                                'registered'   => $date,
     1643                                                'last_updated' => $date,
     1644                                        ),
     1645                                        array( 'site_taken' ),
     1646                                ),
     1647                                array(
     1648                                        array(
     1649                                                'domain'       => 'valid-domain.com',
     1650                                                'path'         => '/valid-path/',
     1651                                                'network_id'   => 1,
     1652                                                'registered'   => '',
     1653                                                'last_updated' => $date,
     1654                                        ),
     1655                                        array( 'site_empty_registered' ),
     1656                                ),
     1657                                array(
     1658                                        array(
     1659                                                'domain'       => 'valid-domain.com',
     1660                                                'path'         => '/valid-path/',
     1661                                                'network_id'   => 1,
     1662                                                'registered'   => $date,
     1663                                                'last_updated' => '',
     1664                                        ),
     1665                                        array( 'site_empty_last_updated' ),
     1666                                ),
     1667                                array(
     1668                                        array(
     1669                                                'domain'       => 'valid-domain.com',
     1670                                                'path'         => '/valid-path/',
     1671                                                'network_id'   => 1,
     1672                                                'registered'   => '2000-13-32 25:25:61',
     1673                                                'last_updated' => $date,
     1674                                        ),
     1675                                        array( 'site_invalid_registered' ),
     1676                                ),
     1677                                array(
     1678                                        array(
     1679                                                'domain'       => 'valid-domain.com',
     1680                                                'path'         => '/valid-path/',
     1681                                                'network_id'   => 1,
     1682                                                'registered'   => $date,
     1683                                                'last_updated' => '2000-13-32 25:25:61',
     1684                                        ),
     1685                                        array( 'site_invalid_last_updated' ),
     1686                                ),
     1687                        );
     1688                }
     1689
     1690                /**
     1691                 * @ticket 40364
     1692                 */
     1693                public function test_site_dates_are_gmt() {
     1694                        $first_date = current_time( 'mysql', true );
     1695                        $site_id    = wp_insert_site( array(
     1696                                'domain'     => 'valid-domain.com',
     1697                                'path'       => '/valid-path/',
     1698                                'network_id' => 1,
     1699                        ) );
     1700                        $this->assertInternalType( 'integer', $site_id );
     1701
     1702                        $site = get_site( $site_id );
     1703                        $this->assertSame( $first_date, $site->registered );
     1704                        $this->assertSame( $first_date, $site->last_updated );
     1705
     1706                        $second_date = current_time( 'mysql', true );
     1707                        $site_id     = wp_update_site( $site_id, array() );
     1708                        $this->assertInternalType( 'integer', $site_id );
     1709
     1710                        $site = get_site( $site_id );
     1711                        $this->assertSame( $first_date, $site->registered );
     1712                        $this->assertSame( $second_date, $site->last_updated );
     1713                }
     1714
     1715                /**
     1716                 * @ticket 40364
     1717                 */
     1718                public function test_wp_delete_site_cleans_cache() {
     1719                        $site_id = self::factory()->blog->create();
     1720
     1721                        get_site( $site_id );
     1722
     1723                        wp_delete_site( $site_id );
     1724
     1725                        $this->assertNull( get_site( $site_id ) );
     1726                }
     1727
     1728                /**
     1729                 * @ticket 40364
     1730                 */
     1731                public function test_wp_update_site_cleans_old_cache_on_domain_change() {
     1732                        $old_domain = 'old.wordpress.org';
     1733                        $new_domain = 'new.wordpress.org';
     1734
     1735                        $site = self::factory()->blog->create_and_get( array(
     1736                                'domain' => $old_domain,
     1737                                'path'   => '/',
     1738                        ) );
     1739
     1740                        // Populate the caches.
     1741                        get_blog_details( array(
     1742                                'domain' => $old_domain,
     1743                                'path'   => '/',
     1744                        ) );
     1745                        get_blog_id_from_url( $old_domain, '/' );
     1746                        get_blog_details( array(
     1747                                'domain' => $new_domain,
     1748                                'path'   => '/',
     1749                        ) );
     1750                        get_blog_id_from_url( $new_domain, '/' );
     1751
     1752                        wp_update_site( $site->id, array(
     1753                                'domain' => $new_domain,
     1754                        ) );
     1755
     1756                        $domain_path_key_old = md5( $old_domain . '/' );
     1757                        $domain_path_key_new = md5( $new_domain . '/' );
     1758
     1759                        // Ensure all respective cache values are empty.
     1760                        $result = array(
     1761                                wp_cache_get( $domain_path_key_old, 'blog-lookup' ),
     1762                                wp_cache_get( $domain_path_key_old, 'blog-id-cache' ),
     1763                                wp_cache_get( 'current_blog_' . $old_domain, 'site-options' ),
     1764                                wp_cache_get( 'current_blog_' . $old_domain . '/', 'site-options' ),
     1765                                wp_cache_get( $domain_path_key_new, 'blog-lookup' ),
     1766                                wp_cache_get( $domain_path_key_new, 'blog-id-cache' ),
     1767                                wp_cache_get( 'current_blog_' . $new_domain, 'site-options' ),
     1768                                wp_cache_get( 'current_blog_' . $new_domain . '/', 'site-options' ),
     1769                        );
     1770
     1771                        $this->assertEmpty( array_filter( $result ) );
     1772                }
     1773
     1774                /**
     1775                 * @ticket 40364
     1776                 */
     1777                public function test_wp_update_site_cleans_old_cache_on_path_change() {
     1778                        $old_path = '/foo/';
     1779                        $new_path = '/bar/';
     1780
     1781                        $site = self::factory()->blog->create_and_get( array(
     1782                                'domain' => 'test.wordpress.org',
     1783                                'path'   => $old_path,
     1784                        ) );
     1785
     1786                        // Populate the caches.
     1787                        get_blog_details( array(
     1788                                'domain' => 'test.wordpress.org',
     1789                                'path'   => $old_path,
     1790                        ) );
     1791                        get_blog_id_from_url( 'test.wordpress.org', $old_path );
     1792                        get_blog_details( array(
     1793                                'domain' => 'test.wordpress.org',
     1794                                'path'   => $new_path,
     1795                        ) );
     1796                        get_blog_id_from_url( 'test.wordpress.org', $new_path );
     1797
     1798                        wp_update_site( $site->id, array(
     1799                                'path' => $new_path,
     1800                        ) );
     1801
     1802                        $domain_path_key_old = md5( 'test.wordpress.org' . $old_path );
     1803                        $domain_path_key_new = md5( 'test.wordpress.org' . $new_path );
     1804
     1805                        // Ensure all respective cache values are empty.
     1806                        $result = array(
     1807                                wp_cache_get( $domain_path_key_old, 'blog-lookup' ),
     1808                                wp_cache_get( $domain_path_key_old, 'blog-id-cache' ),
     1809                                wp_cache_get( 'current_blog_test.wordpress.org' . $old_path, 'site-options' ),
     1810                                wp_cache_get( $domain_path_key_new, 'blog-lookup' ),
     1811                                wp_cache_get( $domain_path_key_new, 'blog-id-cache' ),
     1812                                wp_cache_get( 'current_blog_test.wordpress.org' . $new_path, 'site-options' ),
     1813                        );
     1814
     1815                        $this->assertEmpty( array_filter( $result ) );
     1816                }
     1817
     1818                /**
     1819                 * @ticket 40364
     1820                 * @dataProvider data_site_status_hook_triggers
     1821                 */
     1822                public function test_site_status_hook_triggers( $insert_site_data, $expected_insert_hooks, $update_site_data, $expected_update_hooks ) {
     1823                        // First: Insert a site.
     1824                        $this->listen_to_site_status_hooks();
     1825
     1826                        $site_data = array_merge( array(
     1827                                'domain' => 'example-site.com',
     1828                                'path'   => '/',
     1829                        ), $insert_site_data );
     1830
     1831                        $site_id = wp_insert_site( $site_data );
     1832
     1833                        $insert_expected = array_fill_keys( $expected_insert_hooks, $site_id );
     1834                        $insert_result   = $this->get_listen_to_site_status_hooks_result();
     1835
     1836                        // Second: Update that site.
     1837                        $this->listen_to_site_status_hooks();
     1838
     1839                        wp_update_site( $site_id, $update_site_data );
     1840
     1841                        $update_expected = array_fill_keys( $expected_update_hooks, $site_id );
     1842                        $update_result   = $this->get_listen_to_site_status_hooks_result();
     1843
     1844                        // Check both insert and update results.
     1845                        $this->assertEqualSetsWithIndex( $insert_expected, $insert_result );
     1846                        $this->assertEqualSetsWithIndex( $update_expected, $update_result );
     1847                }
     1848
     1849                public function data_site_status_hook_triggers() {
     1850                        return array(
     1851                                array(
     1852                                        array(
     1853                                                'public'   => 1,
     1854                                                'archived' => 1,
     1855                                                'mature'   => 1,
     1856                                                'spam'     => 1,
     1857                                                'deleted'  => 1,
     1858                                        ),
     1859                                        array(
     1860                                                'archive_blog',
     1861                                                'mature_blog',
     1862                                                'make_spam_blog',
     1863                                                'make_delete_blog',
     1864                                        ),
     1865                                        array(
     1866                                                'public'   => 0,
     1867                                                'archived' => 0,
     1868                                                'mature'   => 0,
     1869                                                'spam'     => 0,
     1870                                                'deleted'  => 0,
     1871                                        ),
     1872                                        array(
     1873                                                'update_blog_public',
     1874                                                'unarchive_blog',
     1875                                                'unmature_blog',
     1876                                                'make_ham_blog',
     1877                                                'make_undelete_blog',
     1878                                        ),
     1879                                ),
     1880                                array(
     1881                                        array(
     1882                                                'public'   => 0,
     1883                                                'archived' => 0,
     1884                                                'mature'   => 0,
     1885                                                'spam'     => 0,
     1886                                                'deleted'  => 0,
     1887                                        ),
     1888                                        array(
     1889                                                'update_blog_public',
     1890                                        ),
     1891                                        array(
     1892                                                'public'   => 1,
     1893                                                'archived' => 1,
     1894                                                'mature'   => 1,
     1895                                                'spam'     => 1,
     1896                                                'deleted'  => 1,
     1897                                        ),
     1898                                        array(
     1899                                                'update_blog_public',
     1900                                                'archive_blog',
     1901                                                'mature_blog',
     1902                                                'make_spam_blog',
     1903                                                'make_delete_blog',
     1904                                        ),
     1905                                ),
     1906                                array(
     1907                                        array(
     1908                                                'public'   => 0,
     1909                                                'archived' => 0,
     1910                                                'mature'   => 1,
     1911                                                'spam'     => 1,
     1912                                                'deleted'  => 1,
     1913                                        ),
     1914                                        array(
     1915                                                'update_blog_public',
     1916                                                'mature_blog',
     1917                                                'make_spam_blog',
     1918                                                'make_delete_blog',
     1919                                        ),
     1920                                        array(
     1921                                                'public'   => 0,
     1922                                                'archived' => 1,
     1923                                                'mature'   => 1,
     1924                                                'spam'     => 1,
     1925                                                'deleted'  => 0,
     1926                                        ),
     1927                                        array(
     1928                                                'archive_blog',
     1929                                                'make_undelete_blog',
     1930                                        ),
     1931                                ),
     1932                        );
     1933                }
     1934
     1935                private function listen_to_site_status_hooks() {
     1936                        $this->site_status_hooks = array();
     1937
     1938                        $hooknames = array(
     1939                                'make_spam_blog',
     1940                                'make_ham_blog',
     1941                                'mature_blog',
     1942                                'unmature_blog',
     1943                                'archive_blog',
     1944                                'unarchive_blog',
     1945                                'make_delete_blog',
     1946                                'make_undelete_blog',
     1947                                'update_blog_public',
     1948                        );
     1949
     1950                        foreach ( $hooknames as $hookname ) {
     1951                                add_action( $hookname, array( $this, 'action_site_status_hook' ), 10, 1 );
     1952                        }
     1953                }
     1954
     1955                private function get_listen_to_site_status_hooks_result() {
     1956                        $hooknames = array(
     1957                                'make_spam_blog',
     1958                                'make_ham_blog',
     1959                                'mature_blog',
     1960                                'unmature_blog',
     1961                                'archive_blog',
     1962                                'unarchive_blog',
     1963                                'make_delete_blog',
     1964                                'make_undelete_blog',
     1965                                'update_blog_public',
     1966                        );
     1967
     1968                        foreach ( $hooknames as $hookname ) {
     1969                                remove_action( $hookname, array( $this, 'action_site_status_hook' ), 10 );
     1970                        }
     1971
     1972                        return $this->site_status_hooks;
     1973                }
     1974
     1975                public function action_site_status_hook( $site_id ) {
     1976                        $this->site_status_hooks[ current_action() ] = $site_id;
     1977                }
    12621978        }
    12631979
    12641980endif;