Make WordPress Core

Ticket #40364: 40364.10.diff

File 40364.10.diff, 47.7 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' );
     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' );
     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        return $data;
     820}
     821
     822/**
     823 * Validates data for a site prior to inserting or updating in the database.
     824 *
     825 * @since 5.0.0
     826 *
     827 * @param WP_Error     $validity Error object, passed by reference. Will contain validation errors if
     828 *                               any occurred.
     829 * @param array        $data     Associative array of complete site data. See {@see wp_insert_site()}
     830 *                               for the included data.
     831 * @param WP_Site|null $old_site The old site object if the data belongs to a site being updated,
     832 *                               or null if it is a new site being inserted.
     833 */
     834function wp_validate_site_data( $validity, $data, $old_site = null ) {
     835        // A domain must always be present.
     836        if ( empty( $data['domain'] ) ) {
     837                $validity->add( 'site_empty_domain', __( 'Site domain must not be empty.' ) );
     838        }
     839
     840        // A path must always be present.
     841        if ( empty( $data['path'] ) ) {
     842                $validity->add( 'site_empty_path', __( 'Site path must not be empty.' ) );
     843        }
     844
     845        // A network ID must always be present.
     846        if ( empty( $data['network_id'] ) ) {
     847                $validity->add( 'site_empty_network_id', __( 'Site network ID must be provided.' ) );
     848        }
     849
     850        if ( ! empty( $validity->errors ) ) {
     851                return;
     852        }
     853
     854        // If a new site, or domain/path/network ID have changed, ensure uniqueness.
     855        if ( ! $old_site
     856                || $data['domain'] !== $old_site->domain
     857                || $data['path'] !== $old_site->path
     858                || $data['network_id'] !== $old_site->network_id
     859        ) {
     860                if ( domain_exists( $data['domain'], $data['path'], $data['network_id'] ) ) {
     861                        $validity->add( 'site_taken', __( 'Sorry, that site already exists!' ) );
     862                }
     863        }
     864}
     865
     866/**
    691867 * Retrieve option value for a given blog id based on name of option.
    692868 *
    693869 * If the option does not exist or does not have a value, then the return value
     
    11931369 * Update a blog details field.
    11941370 *
    11951371 * @since MU (3.0.0)
     1372 * @since 5.0.0 Use wp_update_site() internally.
    11961373 *
    11971374 * @global wpdb $wpdb WordPress database abstraction object.
    11981375 *
     
    12131390                return $value;
    12141391        }
    12151392
    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         );
     1393        $result = wp_update_site( $blog_id, array(
     1394                $pref => $value,
     1395        ) );
    12221396
    1223         if ( false === $result ) {
     1397        if ( is_wp_error( $result ) ) {
    12241398                return false;
    12251399        }
    12261400
    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 
    12731401        return $value;
    12741402}
    12751403
     
    15371665
    15381666        update_posts_count();
    15391667}
     1668
     1669/**
     1670 * Updates the count of sites for a network based on a changed site.
     1671 *
     1672 * @since 5.0.0
     1673 *
     1674 * @param WP_Site      $new_site The site object that has been inserted, updated or deleted.
     1675 * @param WP_Site|null $old_site Optional. If $new_site has been updated, this must be the previous
     1676 *                               state of that site. Default null.
     1677 */
     1678function wp_maybe_update_network_site_counts_on_update( $new_site, $old_site = null ) {
     1679        if ( null === $old_site ) {
     1680                wp_maybe_update_network_site_counts( $new_site->network_id );
     1681                return;
     1682        }
     1683
     1684        if ( $new_site->network_id != $old_site->network_id ) {
     1685                wp_maybe_update_network_site_counts( $new_site->network_id );
     1686                wp_maybe_update_network_site_counts( $old_site->network_id );
     1687        }
     1688}
     1689
     1690/**
     1691 * Triggers actions on site status updates.
     1692 *
     1693 * @since 5.0.0
     1694 *
     1695 * @param WP_Site      $new_site The site object after the update.
     1696 * @param WP_Site|null $old_site Optional. If $new_site has been updated, this must be the previous
     1697 *                               state of that site. Default null.
     1698 */
     1699function wp_maybe_transition_site_statuses_on_update( $new_site, $old_site = null ) {
     1700        $site_id = $new_site->id;
     1701
     1702        // Use the default values for a site if no previous state is given.
     1703        if ( ! $old_site ) {
     1704                $old_site = new WP_Site( new stdClass() );
     1705        }
     1706
     1707        if ( $new_site->spam != $old_site->spam ) {
     1708                if ( $new_site->spam == 1 ) {
     1709
     1710                        /**
     1711                         * Fires when the 'spam' status is added to a site.
     1712                         *
     1713                         * @since MU (3.0.0)
     1714                         *
     1715                         * @param int $site_id Site ID.
     1716                         */
     1717                        do_action( 'make_spam_blog', $site_id );
     1718                } else {
     1719
     1720                        /**
     1721                         * Fires when the 'spam' status is removed from a site.
     1722                         *
     1723                         * @since MU (3.0.0)
     1724                         *
     1725                         * @param int $site_id Site ID.
     1726                         */
     1727                        do_action( 'make_ham_blog', $site_id );
     1728                }
     1729        }
     1730
     1731        if ( $new_site->mature != $old_site->mature ) {
     1732                if ( $new_site->mature == 1 ) {
     1733
     1734                        /**
     1735                         * Fires when the 'mature' status is added to a site.
     1736                         *
     1737                         * @since 3.1.0
     1738                         *
     1739                         * @param int $site_id Site ID.
     1740                         */
     1741                        do_action( 'mature_blog', $site_id );
     1742                } else {
     1743
     1744                        /**
     1745                         * Fires when the 'mature' status is removed from a site.
     1746                         *
     1747                         * @since 3.1.0
     1748                         *
     1749                         * @param int $site_id Site ID.
     1750                         */
     1751                        do_action( 'unmature_blog', $site_id );
     1752                }
     1753        }
     1754
     1755        if ( $new_site->archived != $old_site->archived ) {
     1756                if ( $new_site->archived == 1 ) {
     1757
     1758                        /**
     1759                         * Fires when the 'archived' status is added to a site.
     1760                         *
     1761                         * @since MU (3.0.0)
     1762                         *
     1763                         * @param int $site_id Site ID.
     1764                         */
     1765                        do_action( 'archive_blog', $site_id );
     1766                } else {
     1767
     1768                        /**
     1769                         * Fires when the 'archived' status is removed from a site.
     1770                         *
     1771                         * @since MU (3.0.0)
     1772                         *
     1773                         * @param int $site_id Site ID.
     1774                         */
     1775                        do_action( 'unarchive_blog', $site_id );
     1776                }
     1777        }
     1778
     1779        if ( $new_site->deleted != $old_site->deleted ) {
     1780                if ( $new_site->deleted == 1 ) {
     1781
     1782                        /**
     1783                         * Fires when the 'deleted' status is added to a site.
     1784                         *
     1785                         * @since 3.5.0
     1786                         *
     1787                         * @param int $site_id Site ID.
     1788                         */
     1789                        do_action( 'make_delete_blog', $site_id );
     1790                } else {
     1791
     1792                        /**
     1793                         * Fires when the 'deleted' status is removed from a site.
     1794                         *
     1795                         * @since 3.5.0
     1796                         *
     1797                         * @param int $site_id Site ID.
     1798                         */
     1799                        do_action( 'make_undelete_blog', $site_id );
     1800                }
     1801        }
     1802
     1803        if ( $new_site->public != $old_site->public ) {
     1804
     1805                /**
     1806                 * Fires after the current blog's 'public' setting is updated.
     1807                 *
     1808                 * @since MU (3.0.0)
     1809                 *
     1810                 * @param int    $site_id Site ID.
     1811                 * @param string $value   The value of the site status.
     1812                 */
     1813                do_action( 'update_blog_public', $site_id, $new_site->public );
     1814        }
     1815}
     1816
     1817/**
     1818 * Cleans the necessary caches after specific site data has been updated.
     1819 *
     1820 * @since 5.0.0
     1821 *
     1822 * @param WP_Site $new_site The site object after the update.
     1823 * @param WP_Site $old_site The site obejct prior to the update.
     1824 */
     1825function wp_maybe_clean_new_site_cache_on_update( $new_site, $old_site ) {
     1826        if ( $old_site->domain !== $new_site->domain || $old_site->path !== $new_site->path ) {
     1827                clean_blog_cache( $new_site );
     1828        }
     1829}
     1830
     1831/**
     1832 * Updates the `blog_public` option for a given site ID.
     1833 *
     1834 * @since 5.0.0
     1835 *
     1836 * @param int    $site_id Site ID.
     1837 * @param string $public  The value of the site status.
     1838 */
     1839function wp_update_blog_public_option_on_site_update( $site_id, $public ) {
     1840        update_blog_option( $site_id, 'blog_public', $public );
     1841}
  • 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_action( '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                        );
     1549                }
     1550
     1551                /**
     1552                 * @ticket 40364
     1553                 * @dataProvider data_wp_validate_site_data
     1554                 */
     1555                public function test_wp_validate_site_data( $data, $expected_errors ) {
     1556                        $result = new WP_Error();
     1557                        wp_validate_site_data( $result, $data );
     1558
     1559                        if ( empty( $expected_errors ) ) {
     1560                                $this->assertEmpty( $result->errors );
     1561                        } else {
     1562                                $this->assertEqualSets( $expected_errors, array_keys( $result->errors ) );
     1563                        }
     1564                }
     1565
     1566                public function data_wp_validate_site_data() {
     1567                        return array(
     1568                                array(
     1569                                        array(
     1570                                                'domain'     => 'example-site.com',
     1571                                                'path'       => '/',
     1572                                                'network_id' => 1,
     1573                                        ),
     1574                                        array(),
     1575                                ),
     1576                                array(
     1577                                        array(
     1578                                                'path'       => '/',
     1579                                                'network_id' => 1,
     1580                                        ),
     1581                                        array( 'site_empty_domain' ),
     1582                                ),
     1583                                array(
     1584                                        array(
     1585                                                'domain'     => 'example-site.com',
     1586                                                'network_id' => 1,
     1587                                        ),
     1588                                        array( 'site_empty_path' ),
     1589                                ),
     1590                                array(
     1591                                        array(
     1592                                                'domain'     => 'example-site.com',
     1593                                                'path'       => '/',
     1594                                        ),
     1595                                        array( 'site_empty_network_id' ),
     1596                                ),
     1597                                array(
     1598                                        array(
     1599                                                'domain'     => get_site()->domain,
     1600                                                'path'       => get_site()->path,
     1601                                                'network_id' => get_site()->network_id,
     1602                                        ),
     1603                                        array( 'site_taken' ),
     1604                                ),
     1605                        );
     1606                }
     1607
     1608                /**
     1609                 * @ticket 40364
     1610                 */
     1611                public function test_wp_delete_site_cleans_cache() {
     1612                        $site_id = self::factory()->blog->create();
     1613
     1614                        get_site( $site_id );
     1615
     1616                        wp_delete_site( $site_id );
     1617
     1618                        $this->assertNull( get_site( $site_id ) );
     1619                }
     1620
     1621                /**
     1622                 * @ticket 40364
     1623                 */
     1624                public function test_wp_update_site_cleans_old_cache_on_domain_change() {
     1625                        $old_domain = 'old.wordpress.org';
     1626                        $new_domain = 'new.wordpress.org';
     1627
     1628                        $site = self::factory()->blog->create_and_get( array(
     1629                                'domain' => $old_domain,
     1630                                'path'   => '/',
     1631                        ) );
     1632
     1633                        // Populate the caches.
     1634                        get_blog_details( array(
     1635                                'domain' => $old_domain,
     1636                                'path'   => '/',
     1637                        ) );
     1638                        get_blog_id_from_url( $old_domain, '/' );
     1639                        get_blog_details( array(
     1640                                'domain' => $new_domain,
     1641                                'path'   => '/',
     1642                        ) );
     1643                        get_blog_id_from_url( $new_domain, '/' );
     1644
     1645                        wp_update_site( $site->id, array(
     1646                                'domain' => $new_domain,
     1647                        ) );
     1648
     1649                        $domain_path_key_old = md5( $old_domain . '/' );
     1650                        $domain_path_key_new = md5( $new_domain . '/' );
     1651
     1652                        // Ensure all respective cache values are empty.
     1653                        $result = array(
     1654                                wp_cache_get( $domain_path_key_old, 'blog-lookup' ),
     1655                                wp_cache_get( $domain_path_key_old, 'blog-id-cache' ),
     1656                                wp_cache_get( 'current_blog_' . $old_domain, 'site-options' ),
     1657                                wp_cache_get( 'current_blog_' . $old_domain . '/', 'site-options' ),
     1658                                wp_cache_get( $domain_path_key_new, 'blog-lookup' ),
     1659                                wp_cache_get( $domain_path_key_new, 'blog-id-cache' ),
     1660                                wp_cache_get( 'current_blog_' . $new_domain, 'site-options' ),
     1661                                wp_cache_get( 'current_blog_' . $new_domain . '/', 'site-options' ),
     1662                        );
     1663
     1664                        $this->assertEmpty( array_filter( $result ) );
     1665                }
     1666
     1667                /**
     1668                 * @ticket 40364
     1669                 */
     1670                public function test_wp_update_site_cleans_old_cache_on_path_change() {
     1671                        $old_path = '/foo/';
     1672                        $new_path = '/bar/';
     1673
     1674                        $site = self::factory()->blog->create_and_get( array(
     1675                                'domain' => 'test.wordpress.org',
     1676                                'path'   => $old_path,
     1677                        ) );
     1678
     1679                        // Populate the caches.
     1680                        get_blog_details( array(
     1681                                'domain' => 'test.wordpress.org',
     1682                                'path'   => $old_path,
     1683                        ) );
     1684                        get_blog_id_from_url( 'test.wordpress.org', $old_path );
     1685                        get_blog_details( array(
     1686                                'domain' => 'test.wordpress.org',
     1687                                'path'   => $new_path,
     1688                        ) );
     1689                        get_blog_id_from_url( 'test.wordpress.org', $new_path );
     1690
     1691                        wp_update_site( $site->id, array(
     1692                                'path' => $new_path,
     1693                        ) );
     1694
     1695                        $domain_path_key_old = md5( 'test.wordpress.org' . $old_path );
     1696                        $domain_path_key_new = md5( 'test.wordpress.org' . $new_path );
     1697
     1698                        // Ensure all respective cache values are empty.
     1699                        $result = array(
     1700                                wp_cache_get( $domain_path_key_old, 'blog-lookup' ),
     1701                                wp_cache_get( $domain_path_key_old, 'blog-id-cache' ),
     1702                                wp_cache_get( 'current_blog_test.wordpress.org' . $old_path, 'site-options' ),
     1703                                wp_cache_get( $domain_path_key_new, 'blog-lookup' ),
     1704                                wp_cache_get( $domain_path_key_new, 'blog-id-cache' ),
     1705                                wp_cache_get( 'current_blog_test.wordpress.org' . $new_path, 'site-options' ),
     1706                        );
     1707
     1708                        $this->assertEmpty( array_filter( $result ) );
     1709                }
     1710
     1711                /**
     1712                 * @ticket 40364
     1713                 * @dataProvider data_site_status_hook_triggers
     1714                 */
     1715                public function test_site_status_hook_triggers( $insert_site_data, $expected_insert_hooks, $update_site_data, $expected_update_hooks ) {
     1716                        // First: Insert a site.
     1717                        $this->listen_to_site_status_hooks();
     1718
     1719                        $site_data = array_merge( array(
     1720                                'domain' => 'example-site.com',
     1721                                'path'   => '/',
     1722                        ), $insert_site_data );
     1723
     1724                        $site_id = wp_insert_site( $site_data );
     1725
     1726                        $insert_expected = array_fill_keys( $expected_insert_hooks, $site_id );
     1727                        $insert_result   = $this->get_listen_to_site_status_hooks_result();
     1728
     1729                        // Second: Update that site.
     1730                        $this->listen_to_site_status_hooks();
     1731
     1732                        wp_update_site( $site_id, $update_site_data );
     1733
     1734                        $update_expected = array_fill_keys( $expected_update_hooks, $site_id );
     1735                        $update_result   = $this->get_listen_to_site_status_hooks_result();
     1736
     1737                        // Check both insert and update results.
     1738                        $this->assertEqualSetsWithIndex( $insert_expected, $insert_result );
     1739                        $this->assertEqualSetsWithIndex( $update_expected, $update_result );
     1740                }
     1741
     1742                public function data_site_status_hook_triggers() {
     1743                        return array(
     1744                                array(
     1745                                        array(
     1746                                                'public'   => 1,
     1747                                                'archived' => 1,
     1748                                                'mature'   => 1,
     1749                                                'spam'     => 1,
     1750                                                'deleted'  => 1,
     1751                                        ),
     1752                                        array(
     1753                                                'archive_blog',
     1754                                                'mature_blog',
     1755                                                'make_spam_blog',
     1756                                                'make_delete_blog',
     1757                                        ),
     1758                                        array(
     1759                                                'public'   => 0,
     1760                                                'archived' => 0,
     1761                                                'mature'   => 0,
     1762                                                'spam'     => 0,
     1763                                                'deleted'  => 0,
     1764                                        ),
     1765                                        array(
     1766                                                'update_blog_public',
     1767                                                'unarchive_blog',
     1768                                                'unmature_blog',
     1769                                                'make_ham_blog',
     1770                                                'make_undelete_blog',
     1771                                        ),
     1772                                ),
     1773                                array(
     1774                                        array(
     1775                                                'public'   => 0,
     1776                                                'archived' => 0,
     1777                                                'mature'   => 0,
     1778                                                'spam'     => 0,
     1779                                                'deleted'  => 0,
     1780                                        ),
     1781                                        array(
     1782                                                'update_blog_public',
     1783                                        ),
     1784                                        array(
     1785                                                'public'   => 1,
     1786                                                'archived' => 1,
     1787                                                'mature'   => 1,
     1788                                                'spam'     => 1,
     1789                                                'deleted'  => 1,
     1790                                        ),
     1791                                        array(
     1792                                                'update_blog_public',
     1793                                                'archive_blog',
     1794                                                'mature_blog',
     1795                                                'make_spam_blog',
     1796                                                'make_delete_blog',
     1797                                        ),
     1798                                ),
     1799                                array(
     1800                                        array(
     1801                                                'public'   => 0,
     1802                                                'archived' => 0,
     1803                                                'mature'   => 1,
     1804                                                'spam'     => 1,
     1805                                                'deleted'  => 1,
     1806                                        ),
     1807                                        array(
     1808                                                'update_blog_public',
     1809                                                'mature_blog',
     1810                                                'make_spam_blog',
     1811                                                'make_delete_blog',
     1812                                        ),
     1813                                        array(
     1814                                                'public'   => 0,
     1815                                                'archived' => 1,
     1816                                                'mature'   => 1,
     1817                                                'spam'     => 1,
     1818                                                'deleted'  => 0,
     1819                                        ),
     1820                                        array(
     1821                                                'archive_blog',
     1822                                                'make_undelete_blog',
     1823                                        ),
     1824                                ),
     1825                        );
     1826                }
     1827
     1828                private function listen_to_site_status_hooks() {
     1829                        $this->site_status_hooks = array();
     1830
     1831                        $hooknames = array(
     1832                                'make_spam_blog',
     1833                                'make_ham_blog',
     1834                                'mature_blog',
     1835                                'unmature_blog',
     1836                                'archive_blog',
     1837                                'unarchive_blog',
     1838                                'make_delete_blog',
     1839                                'make_undelete_blog',
     1840                                'update_blog_public',
     1841                        );
     1842
     1843                        foreach ( $hooknames as $hookname ) {
     1844                                add_action( $hookname, array( $this, 'action_site_status_hook' ), 10, 1 );
     1845                        }
     1846                }
     1847
     1848                private function get_listen_to_site_status_hooks_result() {
     1849                        $hooknames = array(
     1850                                'make_spam_blog',
     1851                                'make_ham_blog',
     1852                                'mature_blog',
     1853                                'unmature_blog',
     1854                                'archive_blog',
     1855                                'unarchive_blog',
     1856                                'make_delete_blog',
     1857                                'make_undelete_blog',
     1858                                'update_blog_public',
     1859                        );
     1860
     1861                        foreach ( $hooknames as $hookname ) {
     1862                                remove_action( $hookname, array( $this, 'action_site_status_hook' ), 10 );
     1863                        }
     1864
     1865                        return $this->site_status_hooks;
     1866                }
     1867
     1868                public function action_site_status_hook( $site_id ) {
     1869                        $this->site_status_hooks[ current_action() ] = $site_id;
     1870                }
    12621871        }
    12631872
    12641873endif;