Make WordPress Core

Changeset 43654 for trunk


Ignore:
Timestamp:
09/24/2018 03:08:32 PM (7 years ago)
Author:
flixos90
Message:

Multisite: Introduce a site initialization and uninitialization API.

This changeset makes the new CRUD API for sites introduced in [43548] usable for real-world sites. A new function wp_initialize_site(), which takes care of creating a site's database tables and populating them with initial values, is hooked into the site insertion process that is initiated when calling wp_insert_site(). Similarly, a new function wp_uninitialize_site(), which takes care of dropping a site's database tables, is hooked into the site deletion process that is initiated when calling wp_delete_site().

A new function wp_is_site_initialized() completes the API, allowing to check whether a site is initialized. Since this function always makes a database request in its default behavior, it should be called with caution. Plugins that would like to use site initialization in special ways can leverage a pre_wp_is_site_initialized filter to alter that default behavior.

The separate handling of the site's row in the wp_blogs database table and the actual site setup allows for more flexibility in controlling whether or how a site's data is set up. For example, a unit test that only checks data from the site's database table row can unhook the site initialization process to improve performance. At the same time, developers consuming the new sites API only need to know about the CRUD functions, since the initialization and uninitialization processes happen internally.

With this changeset, the foundation for a sites REST API endpoint is fully available. The previously recommended functions wpmu_create_blog() and wpmu_delete_blog() now call the new respective function internally. Further follow-up work to this includes replacing calls to wpmu_create_blog() with wp_insert_site(), update_blog_details() with wp_update_site() and wpmu_delete_blog() with wp_delete_blog() throughout the codebase.

As a side-effect of this work, the wpmu_new_blog, delete_blog, and deleted_blog actions and the install_blog() function have been deprecated.

Fixes #41333. See #40364.

Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/ms.php

    r43571 r43654  
    7474
    7575    $blog = get_site( $blog_id );
    76     /**
    77      * Fires before a site is deleted.
    78      *
    79      * @since MU (3.0.0)
    80      *
    81      * @param int  $blog_id The site ID.
    82      * @param bool $drop    True if site's table should be dropped. Default is false.
    83      */
    84     do_action( 'delete_blog', $blog_id, $drop );
    85 
    86     $users = get_users(
    87         array(
    88             'blog_id' => $blog_id,
    89             'fields'  => 'ids',
    90         )
    91     );
    92 
    93     // Remove users from this blog.
    94     if ( ! empty( $users ) ) {
    95         foreach ( $users as $user_id ) {
    96             remove_user_from_blog( $user_id, $blog_id );
    97         }
    98     }
    99 
    100     update_blog_status( $blog_id, 'deleted', 1 );
    10176
    10277    $current_network = get_network();
     
    12095
    12196    if ( $drop ) {
    122         $uploads = wp_get_upload_dir();
    123 
    124         $tables = $wpdb->tables( 'blog' );
    125         /**
    126          * Filters the tables to drop when the site is deleted.
    127          *
    128          * @since MU (3.0.0)
    129          *
    130          * @param string[] $tables  Array of names of the site tables to be dropped.
    131          * @param int      $blog_id The ID of the site to drop tables for.
    132          */
    133         $drop_tables = apply_filters( 'wpmu_drop_tables', $tables, $blog_id );
    134 
    135         foreach ( (array) $drop_tables as $table ) {
    136             $wpdb->query( "DROP TABLE IF EXISTS `$table`" );
    137         }
    138 
    139         if ( is_site_meta_supported() ) {
    140             $blog_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->blogmeta WHERE blog_id = %d ", $blog_id ) );
    141             foreach ( $blog_meta_ids as $mid ) {
    142                 delete_metadata_by_mid( 'blog', $mid );
     97        wp_delete_site( $blog_id );
     98    } else {
     99        /** This action is documented in wp-includes/ms-blogs.php */
     100        do_action_deprecated( 'delete_blog', array( $blog_id, false ), '5.0.0' );
     101
     102        $users = get_users(
     103            array(
     104                'blog_id' => $blog_id,
     105                'fields'  => 'ids',
     106            )
     107        );
     108
     109        // Remove users from this blog.
     110        if ( ! empty( $users ) ) {
     111            foreach ( $users as $user_id ) {
     112                remove_user_from_blog( $user_id, $blog_id );
    143113            }
    144114        }
    145115
    146         wp_delete_site( $blog_id );
    147 
    148         /**
    149          * Filters the upload base directory to delete when the site is deleted.
    150          *
    151          * @since MU (3.0.0)
    152          *
    153          * @param string $uploads['basedir'] Uploads path without subdirectory. @see wp_upload_dir()
    154          * @param int    $blog_id            The site ID.
    155          */
    156         $dir     = apply_filters( 'wpmu_delete_blog_upload_dir', $uploads['basedir'], $blog_id );
    157         $dir     = rtrim( $dir, DIRECTORY_SEPARATOR );
    158         $top_dir = $dir;
    159         $stack   = array( $dir );
    160         $index   = 0;
    161 
    162         while ( $index < count( $stack ) ) {
    163             // Get indexed directory from stack
    164             $dir = $stack[ $index ];
    165 
    166             $dh = @opendir( $dir );
    167             if ( $dh ) {
    168                 while ( ( $file = @readdir( $dh ) ) !== false ) {
    169                     if ( $file == '.' || $file == '..' ) {
    170                         continue;
    171                     }
    172 
    173                     if ( @is_dir( $dir . DIRECTORY_SEPARATOR . $file ) ) {
    174                         $stack[] = $dir . DIRECTORY_SEPARATOR . $file;
    175                     } elseif ( @is_file( $dir . DIRECTORY_SEPARATOR . $file ) ) {
    176                         @unlink( $dir . DIRECTORY_SEPARATOR . $file );
    177                     }
    178                 }
    179                 @closedir( $dh );
    180             }
    181             $index++;
    182         }
    183 
    184         $stack = array_reverse( $stack ); // Last added dirs are deepest
    185         foreach ( (array) $stack as $dir ) {
    186             if ( $dir != $top_dir ) {
    187                 @rmdir( $dir );
    188             }
    189         }
    190 
    191         clean_blog_cache( $blog );
    192     }
    193 
    194     /**
    195      * Fires after the site is deleted from the network.
    196      *
    197      * @since 4.8.0
    198      *
    199      * @param int  $blog_id The site ID.
    200      * @param bool $drop    True if site's tables should be dropped. Default is false.
    201      */
    202     do_action( 'deleted_blog', $blog_id, $drop );
     116        update_blog_status( $blog_id, 'deleted', 1 );
     117
     118        /** This action is documented in wp-includes/ms-blogs.php */
     119        do_action_deprecated( 'deleted_blog', array( $blog_id, false ), '5.0.0' );
     120    }
    203121
    204122    if ( $switch ) {
  • trunk/src/wp-includes/ms-blogs.php

    r43571 r43654  
    443443    );
    444444
     445    // Extract the passed arguments that may be relevant for site initialization.
     446    $args = array_diff_key( $data, $defaults );
     447    if ( isset( $args['site_id'] ) ) {
     448        unset( $args['site_id'] );
     449    }
     450
    445451    $data = wp_prepare_site_data( $data, $defaults );
    446452    if ( is_wp_error( $data ) ) {
     
    464470     */
    465471    do_action( 'wp_insert_site', $new_site );
     472
     473    /**
     474     * Fires when a site's initialization routine should be executed.
     475     *
     476     * @since 5.0.0
     477     *
     478     * @param WP_Site $new_site New site object.
     479     * @param array   $args     Arguments for the initialization.
     480     */
     481    do_action( 'wp_initialize_site', $new_site, $args );
     482
     483    // Only compute extra hook parameters if the deprecated hook is actually in use.
     484    if ( has_action( 'wpmu_new_blog' ) ) {
     485        $user_id = ! empty( $args['user_id'] ) ? $args['user_id'] : 0;
     486        $meta    = ! empty( $args['options'] ) ? $args['options'] : array();
     487
     488        /**
     489         * Fires immediately after a new site is created.
     490         *
     491         * @since MU (3.0.0)
     492         * @deprecated 5.0.0 Use wp_insert_site
     493         *
     494         * @param int    $site_id    Site ID.
     495         * @param int    $user_id    User ID.
     496         * @param string $domain     Site domain.
     497         * @param string $path       Site path.
     498         * @param int    $network_id Network ID. Only relevant on multi-network installations.
     499         * @param array  $meta       Meta data. Used to set initial site options.
     500         */
     501        do_action_deprecated( 'wpmu_new_blog', array( $new_site->id, $user_id, $new_site->domain, $new_site->path, $new_site->network_id, $meta ), '5.0.0', 'wp_insert_site' );
     502    }
    466503
    467504    return (int) $new_site->id;
     
    544581    }
    545582
     583    $errors = new WP_Error();
     584
     585    /**
     586     * Fires before a site should be deleted from the database.
     587     *
     588     * Plugins should amend the `$errors` object via its `WP_Error::add()` method. If any errors
     589     * are present, the site will not be deleted.
     590     *
     591     * @since 5.0.0
     592     *
     593     * @param WP_Error $errors   Error object to add validation errors to.
     594     * @param WP_Site  $old_site The site object to be deleted.
     595     */
     596    do_action( 'wp_validate_site_deletion', $errors, $old_site );
     597
     598    if ( ! empty( $errors->errors ) ) {
     599        return $errors;
     600    }
     601
     602    /**
     603     * Fires before a site is deleted.
     604     *
     605     * @since MU (3.0.0)
     606     * @deprecated 5.0.0
     607     *
     608     * @param int  $site_id The site ID.
     609     * @param bool $drop    True if site's table should be dropped. Default is false.
     610     */
     611    do_action_deprecated( 'delete_blog', array( $old_site->id, true ), '5.0.0' );
     612
     613    /**
     614     * Fires when a site's uninitialization routine should be executed.
     615     *
     616     * @since 5.0.0
     617     *
     618     * @param WP_Site $old_site Deleted site object.
     619     */
     620    do_action( 'wp_uninitialize_site', $old_site );
     621
     622    if ( is_site_meta_supported() ) {
     623        $blog_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->blogmeta WHERE blog_id = %d ", $old_site->id ) );
     624        foreach ( $blog_meta_ids as $mid ) {
     625            delete_metadata_by_mid( 'blog', $mid );
     626        }
     627    }
     628
    546629    if ( false === $wpdb->delete( $wpdb->blogs, array( 'blog_id' => $old_site->id ) ) ) {
    547630        return new WP_Error( 'db_delete_error', __( 'Could not delete site from the database.' ), $wpdb->last_error );
     
    558641     */
    559642    do_action( 'wp_delete_site', $old_site );
     643
     644    /**
     645     * Fires after the site is deleted from the network.
     646     *
     647     * @since 4.8.0
     648     * @deprecated 5.0.0
     649     *
     650     * @param int  $site_id The site ID.
     651     * @param bool $drop    True if site's tables should be dropped. Default is false.
     652     */
     653    do_action_deprecated( 'deleted_blog', array( $old_site->id, true ), '5.0.0' );
    560654
    561655    return $old_site;
     
    620714    $non_cached_ids = _get_non_cached_ids( $ids, 'sites' );
    621715    if ( ! empty( $non_cached_ids ) ) {
    622         $fresh_sites = $wpdb->get_results( sprintf( "SELECT * FROM $wpdb->blogs WHERE blog_id IN (%s)", join( ',', array_map( 'intval', $non_cached_ids ) ) ) );
     716        $fresh_sites = $wpdb->get_results( sprintf( "SELECT * FROM $wpdb->blogs WHERE blog_id IN (%s)", join( ',', array_map( 'intval', $non_cached_ids ) ) ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
    623717
    624718        update_site_cache( $fresh_sites, $update_meta_cache );
     
    9141008
    9151009/**
     1010 * Runs the initialization routine for a given site.
     1011 *
     1012 * This process includes creating the site's database tables and
     1013 * populating them with defaults.
     1014 *
     1015 * @since 5.0.0
     1016 *
     1017 * @global wpdb     $wpdb     WordPress database abstraction object.
     1018 * @global WP_Roles $wp_roles WordPress role management object.
     1019 *
     1020 * @param int|WP_Site $site_id Site ID or object.
     1021 * @param array       $args    {
     1022 *     Optional. Arguments to modify the initialization behavior.
     1023 *
     1024 *     @type int    $user_id Required. User ID for the site administrator.
     1025 *     @type string $title   Site title. Default is 'Site %d' where %d is the
     1026 *                           site ID.
     1027 *     @type array  $options Custom option $key => $value pairs to use. Default
     1028 *                           empty array.
     1029 *     @type array  $meta    Custom site metadata $key => $value pairs to use.
     1030 *                           Default empty array.
     1031 * }
     1032 * @return bool|WP_Error True on success, or error object on failure.
     1033 */
     1034function wp_initialize_site( $site_id, array $args = array() ) {
     1035    global $wpdb, $wp_roles;
     1036
     1037    if ( empty( $site_id ) ) {
     1038        return new WP_Error( 'site_empty_id', __( 'Site ID must not be empty.' ) );
     1039    }
     1040
     1041    $site = get_site( $site_id );
     1042    if ( ! $site ) {
     1043        return new WP_Error( 'site_invalid_id', __( 'Site with the ID does not exist.' ) );
     1044    }
     1045
     1046    if ( wp_is_site_initialized( $site ) ) {
     1047        return new WP_Error( 'site_already_initialized', __( 'The site appears to be already initialized.' ) );
     1048    }
     1049
     1050    $network = get_network( $site->network_id );
     1051    if ( ! $network ) {
     1052        $network = get_network();
     1053    }
     1054
     1055    $args = wp_parse_args(
     1056        $args,
     1057        array(
     1058            'user_id' => 0,
     1059            /* translators: %d: site ID */
     1060            'title'   => sprintf( __( 'Site %d' ), $site->id ),
     1061            'options' => array(),
     1062            'meta'    => array(),
     1063        )
     1064    );
     1065
     1066    /**
     1067     * Filters the arguments for initializing a site.
     1068     *
     1069     * @since 5.0.0
     1070     *
     1071     * @param array      $args    Arguments to modify the initialization behavior.
     1072     * @param WP_Site    $site    Site that is being initialized.
     1073     * @param WP_Network $network Network that the site belongs to.
     1074     */
     1075    $args = apply_filters( 'wp_initialize_site_args', $args, $site, $network );
     1076
     1077    $orig_installing = wp_installing();
     1078    if ( ! $orig_installing ) {
     1079        wp_installing( true );
     1080    }
     1081
     1082    $switch = false;
     1083    if ( get_current_blog_id() !== $site->id ) {
     1084        $switch = true;
     1085        switch_to_blog( $site->id );
     1086    }
     1087
     1088    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
     1089
     1090    // Set up the database tables.
     1091    make_db_current_silent( 'blog' );
     1092
     1093    $home_scheme    = 'http';
     1094    $siteurl_scheme = 'http';
     1095    if ( ! is_subdomain_install() ) {
     1096        if ( 'https' === parse_url( get_home_url( $network->site_id ), PHP_URL_SCHEME ) ) {
     1097            $home_scheme = 'https';
     1098        }
     1099        if ( 'https' === parse_url( get_network_option( $network->id, 'siteurl' ), PHP_URL_SCHEME ) ) {
     1100            $siteurl_scheme = 'https';
     1101        }
     1102    }
     1103
     1104    // Populate the site's options.
     1105    populate_options(
     1106        array_merge(
     1107            array(
     1108                'home'        => untrailingslashit( $home_scheme . '://' . $site->domain . $site->path ),
     1109                'siteurl'     => untrailingslashit( $siteurl_scheme . '://' . $site->domain . $site->path ),
     1110                'blogname'    => wp_unslash( $args['title'] ),
     1111                'admin_email' => '',
     1112                'upload_path' => get_network_option( $network->id, 'ms_files_rewriting' ) ? UPLOADBLOGSDIR . "/{$site->id}/files" : get_blog_option( $network->site_id, 'upload_path' ),
     1113                'blog_public' => (int) $site->public,
     1114                'WPLANG'      => get_network_option( $network->id, 'WPLANG' ),
     1115            ),
     1116            $args['options']
     1117        )
     1118    );
     1119
     1120    // Populate the site's roles.
     1121    populate_roles();
     1122    $wp_roles = new WP_Roles();
     1123
     1124    // Populate metadata for the site.
     1125    populate_site_meta( $site->id, $args['meta'] );
     1126
     1127    // Remove all permissions that may exist for the site.
     1128    $table_prefix = $wpdb->get_blog_prefix();
     1129    delete_metadata( 'user', 0, $table_prefix . 'user_level', null, true ); // delete all
     1130    delete_metadata( 'user', 0, $table_prefix . 'capabilities', null, true ); // delete all
     1131
     1132    // Install default site content.
     1133    wp_install_defaults( $args['user_id'] );
     1134
     1135    // Set the site administrator.
     1136    add_user_to_blog( $site->id, $args['user_id'], 'administrator' );
     1137    if ( ! user_can( $args['user_id'], 'manage_network' ) && ! get_user_meta( $args['user_id'], 'primary_blog', true ) ) {
     1138        update_user_meta( $args['user_id'], 'primary_blog', $site->id );
     1139    }
     1140
     1141    if ( $switch ) {
     1142        restore_current_blog();
     1143    }
     1144
     1145    wp_installing( $orig_installing );
     1146
     1147    return true;
     1148}
     1149
     1150/**
     1151 * Runs the uninitialization routine for a given site.
     1152 *
     1153 * This process includes dropping the site's database tables and deleting its uploads directory.
     1154 *
     1155 * @since 5.0.0
     1156 *
     1157 * @global wpdb $wpdb WordPress database abstraction object.
     1158 *
     1159 * @param int|WP_Site $site_id Site ID or object.
     1160 * @return bool|WP_Error True on success, or error object on failure.
     1161 */
     1162function wp_uninitialize_site( $site_id ) {
     1163    global $wpdb;
     1164
     1165    if ( empty( $site_id ) ) {
     1166        return new WP_Error( 'site_empty_id', __( 'Site ID must not be empty.' ) );
     1167    }
     1168
     1169    $site = get_site( $site_id );
     1170    if ( ! $site ) {
     1171        return new WP_Error( 'site_invalid_id', __( 'Site with the ID does not exist.' ) );
     1172    }
     1173
     1174    if ( ! wp_is_site_initialized( $site ) ) {
     1175        return new WP_Error( 'site_already_uninitialized', __( 'The site appears to be already uninitialized.' ) );
     1176    }
     1177
     1178    $users = get_users( array(
     1179        'blog_id' => $site->id,
     1180        'fields'  => 'ids',
     1181    ) );
     1182
     1183    // Remove users from the site.
     1184    if ( ! empty( $users ) ) {
     1185        foreach ( $users as $user_id ) {
     1186            remove_user_from_blog( $user_id, $site->id );
     1187        }
     1188    }
     1189
     1190    $switch = false;
     1191    if ( get_current_blog_id() !== $site->id ) {
     1192        $switch = true;
     1193        switch_to_blog( $site->id );
     1194    }
     1195
     1196    $uploads = wp_get_upload_dir();
     1197
     1198    $tables = $wpdb->tables( 'blog' );
     1199
     1200    /**
     1201     * Filters the tables to drop when the site is deleted.
     1202     *
     1203     * @since MU (3.0.0)
     1204     *
     1205     * @param string[] $tables  Array of names of the site tables to be dropped.
     1206     * @param int      $site_id The ID of the site to drop tables for.
     1207     */
     1208    $drop_tables = apply_filters( 'wpmu_drop_tables', $tables, $site->id );
     1209
     1210    foreach ( (array) $drop_tables as $table ) {
     1211        $wpdb->query( "DROP TABLE IF EXISTS `$table`" ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
     1212    }
     1213
     1214    /**
     1215     * Filters the upload base directory to delete when the site is deleted.
     1216     *
     1217     * @since MU (3.0.0)
     1218     *
     1219     * @param string $uploads['basedir'] Uploads path without subdirectory. @see wp_upload_dir()
     1220     * @param int    $site_id            The site ID.
     1221     */
     1222    $dir     = apply_filters( 'wpmu_delete_blog_upload_dir', $uploads['basedir'], $site->id );
     1223    $dir     = rtrim( $dir, DIRECTORY_SEPARATOR );
     1224    $top_dir = $dir;
     1225    $stack   = array( $dir );
     1226    $index   = 0;
     1227
     1228    while ( $index < count( $stack ) ) {
     1229        // Get indexed directory from stack
     1230        $dir = $stack[ $index ];
     1231
     1232        // phpcs:disable Generic.PHP.NoSilencedErrors.Discouraged
     1233        $dh = @opendir( $dir );
     1234        if ( $dh ) {
     1235            $file = @readdir( $dh );
     1236            while ( false !== $file ) {
     1237                if ( '.' === $file || '..' === $file ) {
     1238                    $file = @readdir( $dh );
     1239                    continue;
     1240                }
     1241
     1242                if ( @is_dir( $dir . DIRECTORY_SEPARATOR . $file ) ) {
     1243                    $stack[] = $dir . DIRECTORY_SEPARATOR . $file;
     1244                } elseif ( @is_file( $dir . DIRECTORY_SEPARATOR . $file ) ) {
     1245                    @unlink( $dir . DIRECTORY_SEPARATOR . $file );
     1246                }
     1247
     1248                $file = @readdir( $dh );
     1249            }
     1250            @closedir( $dh );
     1251        }
     1252        $index++;
     1253    }
     1254
     1255    $stack = array_reverse( $stack ); // Last added dirs are deepest
     1256    foreach ( (array) $stack as $dir ) {
     1257        if ( $dir != $top_dir ) {
     1258            @rmdir( $dir );
     1259        }
     1260    }
     1261
     1262    // phpcs:enable Generic.PHP.NoSilencedErrors.Discouraged
     1263    if ( $switch ) {
     1264        restore_current_blog();
     1265    }
     1266
     1267    return true;
     1268}
     1269
     1270/**
     1271 * Checks whether a site is initialized.
     1272 *
     1273 * A site is considered initialized when its database tables are present.
     1274 *
     1275 * @since 5.0.0
     1276 *
     1277 * @global wpdb $wpdb WordPress database abstraction object.
     1278 *
     1279 * @param int|WP_Site $site_id Site ID or object.
     1280 * @return bool True if the site is initialized, false otherwise.
     1281 */
     1282function wp_is_site_initialized( $site_id ) {
     1283    global $wpdb;
     1284
     1285    if ( is_object( $site_id ) ) {
     1286        $site_id = $site_id->blog_id;
     1287    }
     1288    $site_id = (int) $site_id;
     1289
     1290    /**
     1291     * Filters the check for whether a site is initialized before the database is accessed.
     1292     *
     1293     * Returning a non-null value will effectively short-circuit the function, returning
     1294     * that value instead.
     1295     *
     1296     * @since 5.0.0
     1297     *
     1298     * @param bool|null $pre     The value to return, if not null.
     1299     * @param int       $site_id The site ID that is being checked.
     1300     */
     1301    $pre = apply_filters( 'pre_wp_is_site_initialized', null, $site_id );
     1302    if ( null !== $pre ) {
     1303        return (bool) $pre;
     1304    }
     1305
     1306    $switch = false;
     1307    if ( get_current_blog_id() !== $site_id ) {
     1308        $switch = true;
     1309        remove_action( 'switch_blog', 'wp_switch_roles_and_user', 1 );
     1310        switch_to_blog( $site_id );
     1311    }
     1312
     1313    $suppress = $wpdb->suppress_errors();
     1314    $result   = (bool) $wpdb->get_results( "DESCRIBE {$wpdb->posts}" );
     1315    $wpdb->suppress_errors( $suppress );
     1316
     1317    if ( $switch ) {
     1318        restore_current_blog();
     1319        add_action( 'switch_blog', 'wp_switch_roles_and_user', 1, 2 );
     1320    }
     1321
     1322    return $result;
     1323}
     1324
     1325/**
    9161326 * Retrieve option value for a given blog id based on name of option.
    9171327 *
     
    16222032    $non_cached_ids = _get_non_cached_ids( $network_ids, 'networks' );
    16232033    if ( ! empty( $non_cached_ids ) ) {
    1624         $fresh_networks = $wpdb->get_results( sprintf( "SELECT $wpdb->site.* FROM $wpdb->site WHERE id IN (%s)", join( ',', array_map( 'intval', $non_cached_ids ) ) ) );
     2034        $fresh_networks = $wpdb->get_results( sprintf( "SELECT $wpdb->site.* FROM $wpdb->site WHERE id IN (%s)", join( ',', array_map( 'intval', $non_cached_ids ) ) ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
    16252035
    16262036        update_network_cache( $fresh_networks );
     
    17582168
    17592169    if ( $new_site->spam != $old_site->spam ) {
    1760         if ( $new_site->spam == 1 ) {
     2170        if ( 1 == $new_site->spam ) {
    17612171
    17622172            /**
     
    17822192
    17832193    if ( $new_site->mature != $old_site->mature ) {
    1784         if ( $new_site->mature == 1 ) {
     2194        if ( 1 == $new_site->mature ) {
    17852195
    17862196            /**
     
    18062216
    18072217    if ( $new_site->archived != $old_site->archived ) {
    1808         if ( $new_site->archived == 1 ) {
     2218        if ( 1 == $new_site->archived ) {
    18092219
    18102220            /**
     
    18302240
    18312241    if ( $new_site->deleted != $old_site->deleted ) {
    1832         if ( $new_site->deleted == 1 ) {
     2242        if ( 1 == $new_site->deleted ) {
    18332243
    18342244            /**
     
    18902300 */
    18912301function wp_update_blog_public_option_on_site_update( $site_id, $public ) {
     2302
     2303    // Bail if the site's database tables do not exist (yet).
     2304    if ( ! wp_is_site_initialized( $site_id ) ) {
     2305        return;
     2306    }
     2307
    18922308    update_blog_option( $site_id, 'blog_public', $public );
    18932309}
  • trunk/src/wp-includes/ms-default-filters.php

    r43548 r43654  
    3838// Blogs
    3939add_filter( 'wpmu_validate_blog_signup', 'signup_nonce_check' );
    40 add_action( 'wpmu_new_blog', 'wpmu_log_new_registrations', 10, 2 );
    41 add_action( 'wpmu_new_blog', 'newblog_notify_siteadmin', 10, 2 );
    4240add_action( 'wpmu_activate_blog', 'wpmu_welcome_notification', 10, 5 );
    4341add_action( 'after_signup_site', 'wpmu_signup_blog_notification', 10, 7 );
     
    5048add_action( 'wp_update_site', 'wp_maybe_transition_site_statuses_on_update', 10, 2 );
    5149add_action( 'wp_update_site', 'wp_maybe_clean_new_site_cache_on_update', 10, 2 );
     50add_action( 'wp_initialize_site', 'wp_initialize_site', 10, 2 );
     51add_action( 'wp_initialize_site', 'wpmu_log_new_registrations', 100, 2 );
     52add_action( 'wp_initialize_site', 'newblog_notify_siteadmin', 100, 1 );
     53add_action( 'wp_uninitialize_site', 'wp_uninitialize_site', 10, 1 );
    5254add_action( 'update_blog_public', 'wp_update_blog_public_option_on_site_update', 1, 2 );
    5355
  • trunk/src/wp-includes/ms-deprecated.php

    r43548 r43654  
    581581    return $site_id;
    582582}
     583
     584/**
     585 * Install an empty blog.
     586 *
     587 * Creates the new blog tables and options. If calling this function
     588 * directly, be sure to use switch_to_blog() first, so that $wpdb
     589 * points to the new blog.
     590 *
     591 * @since MU (3.0.0)
     592 * @deprecated 5.0.0
     593 *
     594 * @global wpdb     $wpdb
     595 * @global WP_Roles $wp_roles
     596 *
     597 * @param int    $blog_id    The value returned by wp_insert_site().
     598 * @param string $blog_title The title of the new site.
     599 */
     600function install_blog( $blog_id, $blog_title = '' ) {
     601    global $wpdb, $wp_roles;
     602
     603    _deprecated_function( __FUNCTION__, '5.0.0' );
     604
     605    // Cast for security
     606    $blog_id = (int) $blog_id;
     607
     608    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
     609
     610    $suppress = $wpdb->suppress_errors();
     611    if ( $wpdb->get_results( "DESCRIBE {$wpdb->posts}" ) ) {
     612        die( '<h1>' . __( 'Already Installed' ) . '</h1><p>' . __( 'You appear to have already installed WordPress. To reinstall please clear your old database tables first.' ) . '</p></body></html>' );
     613    }
     614    $wpdb->suppress_errors( $suppress );
     615
     616    $url = get_blogaddress_by_id( $blog_id );
     617
     618    // Set everything up
     619    make_db_current_silent( 'blog' );
     620    populate_options();
     621    populate_roles();
     622
     623    // populate_roles() clears previous role definitions so we start over.
     624    $wp_roles = new WP_Roles();
     625
     626    $siteurl = $home = untrailingslashit( $url );
     627
     628    if ( ! is_subdomain_install() ) {
     629
     630        if ( 'https' === parse_url( get_site_option( 'siteurl' ), PHP_URL_SCHEME ) ) {
     631            $siteurl = set_url_scheme( $siteurl, 'https' );
     632        }
     633        if ( 'https' === parse_url( get_home_url( get_network()->site_id ), PHP_URL_SCHEME ) ) {
     634            $home = set_url_scheme( $home, 'https' );
     635        }
     636    }
     637
     638    update_option( 'siteurl', $siteurl );
     639    update_option( 'home', $home );
     640
     641    if ( get_site_option( 'ms_files_rewriting' ) ) {
     642        update_option( 'upload_path', UPLOADBLOGSDIR . "/$blog_id/files" );
     643    } else {
     644        update_option( 'upload_path', get_blog_option( get_network()->site_id, 'upload_path' ) );
     645    }
     646
     647    update_option( 'blogname', wp_unslash( $blog_title ) );
     648    update_option( 'admin_email', '' );
     649
     650    // remove all perms
     651    $table_prefix = $wpdb->get_blog_prefix();
     652    delete_metadata( 'user', 0, $table_prefix . 'user_level', null, true ); // delete all
     653    delete_metadata( 'user', 0, $table_prefix . 'capabilities', null, true ); // delete all
     654}
     655
     656/**
     657 * Set blog defaults.
     658 *
     659 * This function creates a row in the wp_blogs table.
     660 *
     661 * @since MU (3.0.0)
     662 * @deprecated MU
     663 * @deprecated Use wp_install_defaults()
     664 *
     665 * @global wpdb $wpdb WordPress database abstraction object.
     666 *
     667 * @param int $blog_id Ignored in this function.
     668 * @param int $user_id
     669 */
     670function install_blog_defaults( $blog_id, $user_id ) {
     671    global $wpdb;
     672
     673    _deprecated_function( __FUNCTION__, 'MU' );
     674
     675    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
     676
     677    $suppress = $wpdb->suppress_errors();
     678
     679    wp_install_defaults( $user_id );
     680
     681    $wpdb->suppress_errors( $suppress );
     682}
  • trunk/src/wp-includes/ms-functions.php

    r43571 r43654  
    12861286 * @param string $title      The new site's title.
    12871287 * @param int    $user_id    The user ID of the new site's admin.
    1288  * @param array  $meta       Optional. Array of key=>value pairs used to set initial site options.
     1288 * @param array  $options    Optional. Array of key=>value pairs used to set initial site options.
    12891289 *                           If valid status keys are included ('public', 'archived', 'mature',
    12901290 *                           'spam', 'deleted', or 'lang_id') the given site status(es) will be
     
    12941294 * @return int|WP_Error Returns WP_Error object on failure, the new site ID on success.
    12951295 */
    1296 function wpmu_create_blog( $domain, $path, $title, $user_id, $meta = array(), $network_id = 1 ) {
     1296function wpmu_create_blog( $domain, $path, $title, $user_id, $options = array(), $network_id = 1 ) {
    12971297    $defaults = array(
    12981298        'public' => 0,
    1299         'WPLANG' => get_network_option( $network_id, 'WPLANG' ),
    13001299    );
    1301     $meta     = wp_parse_args( $meta, $defaults );
     1300    $options  = wp_parse_args( $options, $defaults );
    13021301
    13031302    $title   = strip_tags( $title );
     
    13211320            'network_id' => $network_id,
    13221321        ),
    1323         array_intersect_key(
    1324             $meta,
    1325             array_flip( $site_data_whitelist )
    1326         )
     1322        array_intersect_key( $options, array_flip( $site_data_whitelist ) )
    13271323    );
    13281324
    1329     $meta = array_diff_key( $meta, array_flip( $site_data_whitelist ) );
    1330 
    1331     remove_action( 'update_blog_public', 'wp_update_blog_public_option_on_site_update', 1 );
    1332     $blog_id = wp_insert_site( $site_data );
    1333     add_action( 'update_blog_public', 'wp_update_blog_public_option_on_site_update', 1, 2 );
     1325    // Data to pass to wp_initialize_site().
     1326    $site_initialization_data = array(
     1327        'title'   => $title,
     1328        'user_id' => $user_id,
     1329        'options' => array_diff_key( $options, array_flip( $site_data_whitelist ) ),
     1330    );
     1331
     1332    $blog_id = wp_insert_site( array_merge( $site_data, $site_initialization_data ) );
    13341333
    13351334    if ( is_wp_error( $blog_id ) ) {
     
    13371336    }
    13381337
    1339     switch_to_blog( $blog_id );
    1340     install_blog( $blog_id, $title );
    1341     wp_install_defaults( $user_id );
    1342 
    1343     add_user_to_blog( $blog_id, $user_id, 'administrator' );
    1344 
    1345     foreach ( $meta as $key => $value ) {
    1346         update_option( $key, $value );
    1347     }
    1348 
    1349     update_option( 'blog_public', (int) $site_data['public'] );
    1350 
    1351     if ( ! is_super_admin( $user_id ) && ! get_user_meta( $user_id, 'primary_blog', true ) ) {
    1352         update_user_meta( $user_id, 'primary_blog', $blog_id );
    1353     }
    1354 
    1355     restore_current_blog();
    1356 
    1357     $site = get_site( $blog_id );
    1358 
    1359     /**
    1360      * Fires immediately after a new site is created.
    1361      *
    1362      * @since MU (3.0.0)
    1363      *
    1364      * @param int    $blog_id    Site ID.
    1365      * @param int    $user_id    User ID.
    1366      * @param string $domain     Site domain.
    1367      * @param string $path       Site path.
    1368      * @param int    $network_id Network ID. Only relevant on multi-network installations.
    1369      * @param array  $meta       Meta data. Used to set initial site options.
    1370      */
    1371     do_action( 'wpmu_new_blog', $blog_id, $user_id, $site->domain, $site->path, $site->network_id, $meta );
    1372 
    13731338    wp_cache_set( 'last_changed', microtime(), 'sites' );
    13741339
     
    13831348 *
    13841349 * @since MU (3.0.0)
    1385  *
    1386  * @param int    $blog_id    The new site's ID.
    1387  * @param string $deprecated Not used.
     1350 * @since 5.0.0 $blog_id now supports input from the {@see 'wp_initialize_site'} action.
     1351 *
     1352 * @param WP_Site|int $blog_id    The new site's object or ID.
     1353 * @param string      $deprecated Not used.
    13881354 * @return bool
    13891355 */
    13901356function newblog_notify_siteadmin( $blog_id, $deprecated = '' ) {
     1357    if ( is_object( $blog_id ) ) {
     1358        $blog_id = $blog_id->blog_id;
     1359    }
     1360
    13911361    if ( get_site_option( 'registrationnotification' ) != 'yes' ) {
    13921362        return false;
     
    15271497     */
    15281498    return apply_filters( 'domain_exists', $result, $domain, $path, $network_id );
    1529 }
    1530 
    1531 /**
    1532  * Install an empty blog.
    1533  *
    1534  * Creates the new blog tables and options. If calling this function
    1535  * directly, be sure to use switch_to_blog() first, so that $wpdb
    1536  * points to the new blog.
    1537  *
    1538  * @since MU (3.0.0)
    1539  *
    1540  * @global wpdb     $wpdb
    1541  * @global WP_Roles $wp_roles
    1542  *
    1543  * @param int    $blog_id    The value returned by wp_insert_site().
    1544  * @param string $blog_title The title of the new site.
    1545  */
    1546 function install_blog( $blog_id, $blog_title = '' ) {
    1547     global $wpdb, $wp_roles;
    1548 
    1549     // Cast for security
    1550     $blog_id = (int) $blog_id;
    1551 
    1552     require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    1553 
    1554     $suppress = $wpdb->suppress_errors();
    1555     if ( $wpdb->get_results( "DESCRIBE {$wpdb->posts}" ) ) {
    1556         die( '<h1>' . __( 'Already Installed' ) . '</h1><p>' . __( 'You appear to have already installed WordPress. To reinstall please clear your old database tables first.' ) . '</p></body></html>' );
    1557     }
    1558     $wpdb->suppress_errors( $suppress );
    1559 
    1560     $url = get_blogaddress_by_id( $blog_id );
    1561 
    1562     // Set everything up
    1563     make_db_current_silent( 'blog' );
    1564     populate_options();
    1565     populate_roles();
    1566 
    1567     // populate_roles() clears previous role definitions so we start over.
    1568     $wp_roles = new WP_Roles();
    1569 
    1570     $siteurl = $home = untrailingslashit( $url );
    1571 
    1572     if ( ! is_subdomain_install() ) {
    1573 
    1574         if ( 'https' === parse_url( get_site_option( 'siteurl' ), PHP_URL_SCHEME ) ) {
    1575             $siteurl = set_url_scheme( $siteurl, 'https' );
    1576         }
    1577         if ( 'https' === parse_url( get_home_url( get_network()->site_id ), PHP_URL_SCHEME ) ) {
    1578             $home = set_url_scheme( $home, 'https' );
    1579         }
    1580     }
    1581 
    1582     update_option( 'siteurl', $siteurl );
    1583     update_option( 'home', $home );
    1584 
    1585     if ( get_site_option( 'ms_files_rewriting' ) ) {
    1586         update_option( 'upload_path', UPLOADBLOGSDIR . "/$blog_id/files" );
    1587     } else {
    1588         update_option( 'upload_path', get_blog_option( get_network()->site_id, 'upload_path' ) );
    1589     }
    1590 
    1591     update_option( 'blogname', wp_unslash( $blog_title ) );
    1592     update_option( 'admin_email', '' );
    1593 
    1594     // remove all perms
    1595     $table_prefix = $wpdb->get_blog_prefix();
    1596     delete_metadata( 'user', 0, $table_prefix . 'user_level', null, true ); // delete all
    1597     delete_metadata( 'user', 0, $table_prefix . 'capabilities', null, true ); // delete all
    1598 }
    1599 
    1600 /**
    1601  * Set blog defaults.
    1602  *
    1603  * This function creates a row in the wp_blogs table.
    1604  *
    1605  * @since MU (3.0.0)
    1606  * @deprecated MU
    1607  * @deprecated Use wp_install_defaults()
    1608  *
    1609  * @global wpdb $wpdb WordPress database abstraction object.
    1610  *
    1611  * @param int $blog_id Ignored in this function.
    1612  * @param int $user_id
    1613  */
    1614 function install_blog_defaults( $blog_id, $user_id ) {
    1615     global $wpdb;
    1616 
    1617     require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    1618 
    1619     $suppress = $wpdb->suppress_errors();
    1620 
    1621     wp_install_defaults( $user_id );
    1622 
    1623     $wpdb->suppress_errors( $suppress );
    16241499}
    16251500
     
    20251900 *
    20261901 * @since MU (3.0.0)
     1902 * @since 5.0.0 Parameters now support input from the {@see 'wp_initialize_site'} action.
    20271903 *
    20281904 * @global wpdb $wpdb WordPress database abstraction object.
    20291905 *
    2030  * @param int $blog_id
    2031  * @param int $user_id
     1906 * @param WP_Site|int $blog_id The new site's object or ID.
     1907 * @param int|array   $user_id User ID, or array of arguments including 'user_id'.
    20321908 */
    20331909function wpmu_log_new_registrations( $blog_id, $user_id ) {
    20341910    global $wpdb;
     1911
     1912    if ( is_object( $blog_id ) ) {
     1913        $blog_id = $blog_id->blog_id;
     1914    }
     1915
     1916    if ( is_array( $user_id ) ) {
     1917        $user_id = ! empty( $user_id['user_id'] ) ? $user_id['user_id'] : 0;
     1918    }
     1919
    20351920    $user = get_userdata( (int) $user_id );
    20361921    if ( $user ) {
  • trunk/tests/phpunit/tests/multisite/site.php

    r43571 r43654  
    1010     */
    1111    class Tests_Multisite_Site extends WP_UnitTestCase {
    12         protected $suppress          = false;
    13         protected $site_status_hooks = array();
     12        protected $suppress                = false;
     13        protected $site_status_hooks       = array();
     14        protected $wp_initialize_site_args = array();
    1415        protected static $network_ids;
    1516        protected static $site_ids;
     17        protected static $uninitialized_site_id;
    1618
    1719        function setUp() {
     
    5759            }
    5860            unset( $id );
     61
     62            remove_action( 'wp_initialize_site', 'wp_initialize_site', 10 );
     63            self::$uninitialized_site_id = wp_insert_site( array(
     64                'domain'  => 'uninitialized.org',
     65                'path'    => '/',
     66                'site_id' => self::$network_ids['make.wordpress.org/'],
     67            ) );
     68            add_action( 'wp_initialize_site', 'wp_initialize_site', 10, 2 );
    5969        }
    6070
    6171        public static function wpTearDownAfterClass() {
    6272            global $wpdb;
     73
     74            remove_action( 'wp_uninitialize_site', 'wp_uninitialize_site', 10 );
     75            wp_delete_site( self::$uninitialized_site_id );
     76            add_action( 'wp_uninitialize_site', 'wp_uninitialize_site', 10, 1 );
    6377
    6478            foreach ( self::$site_ids as $id ) {
     
    12671281         */
    12681282        public function test_wp_insert_site( $site_data, $expected_data ) {
     1283            remove_action( 'wp_initialize_site', 'wp_initialize_site', 10 );
    12691284            $site_id = wp_insert_site( $site_data );
    12701285
     
    13611376         */
    13621377        public function test_wp_insert_site_empty_domain() {
     1378            remove_action( 'wp_initialize_site', 'wp_initialize_site', 10 );
    13631379            $site_id = wp_insert_site( array( 'public' => 0 ) );
    13641380
     
    14981514
    14991515        /**
     1516         * @ticket 41333
     1517         */
     1518        public function test_wp_delete_site_validate_site_deletion_action() {
     1519            add_action( 'wp_validate_site_deletion', array( $this, 'action_wp_validate_site_deletion_prevent_deletion' ) );
     1520            $result = wp_delete_site( self::$site_ids['make.wordpress.org/'] );
     1521            $this->assertWPError( $result );
     1522            $this->assertSame( 'action_does_not_like_deletion', $result->get_error_code() );
     1523        }
     1524
     1525        public function action_wp_validate_site_deletion_prevent_deletion( $errors ) {
     1526            $errors->add( 'action_does_not_like_deletion', 'You cannot delete this site because the action does not like it.' );
     1527        }
     1528
     1529        /**
    15001530         * @ticket 40364
    15011531         * @dataProvider data_wp_normalize_site_data
     
    17101740        public function test_site_dates_are_gmt() {
    17111741            $first_date = current_time( 'mysql', true );
    1712             $site_id    = wp_insert_site(
     1742
     1743            remove_action( 'wp_initialize_site', 'wp_initialize_site', 10 );
     1744            $site_id = wp_insert_site(
    17131745                array(
    17141746                    'domain'     => 'valid-domain.com',
     
    20162048            $this->site_status_hooks[ current_action() ] = $site_id;
    20172049        }
     2050
     2051        /**
     2052         * @ticket 41333
     2053         * @dataProvider data_wp_initialize_site
     2054         */
     2055        public function test_wp_initialize_site( $args, $expected_options, $expected_meta ) {
     2056            $result = wp_initialize_site( self::$uninitialized_site_id, $args );
     2057
     2058            switch_to_blog( self::$uninitialized_site_id );
     2059
     2060            $options = array();
     2061            foreach ( $expected_options as $option => $value ) {
     2062                $options[ $option ] = get_option( $option );
     2063            }
     2064
     2065            $meta = array();
     2066            foreach ( $expected_meta as $meta_key => $value ) {
     2067                $meta[ $meta_key ] = get_site_meta( self::$uninitialized_site_id, $meta_key, true );
     2068            }
     2069
     2070            restore_current_blog();
     2071
     2072            $initialized = wp_is_site_initialized( self::$uninitialized_site_id );
     2073
     2074            wp_uninitialize_site( self::$uninitialized_site_id );
     2075
     2076            $this->assertTrue( $result );
     2077            $this->assertTrue( $initialized );
     2078            $this->assertEquals( $expected_options, $options );
     2079            $this->assertEquals( $expected_meta, $meta );
     2080        }
     2081
     2082        public function data_wp_initialize_site() {
     2083            return array(
     2084                array(
     2085                    array(),
     2086                    array(
     2087                        'home'        => 'http://uninitialized.org',
     2088                        'siteurl'     => 'http://uninitialized.org',
     2089                        'admin_email' => '',
     2090                        'blog_public' => '1',
     2091                    ),
     2092                    array(),
     2093                ),
     2094                array(
     2095                    array(
     2096                        'options' => array(
     2097                            'home'    => 'https://uninitialized.org',
     2098                            'siteurl' => 'https://uninitialized.org',
     2099                            'key'     => 'value',
     2100                        ),
     2101                        'meta'    => array(
     2102                            'key1' => 'value1',
     2103                            'key2' => 'value2',
     2104                        ),
     2105                    ),
     2106                    array(
     2107                        'home'    => 'https://uninitialized.org',
     2108                        'siteurl' => 'https://uninitialized.org',
     2109                        'key'     => 'value',
     2110                    ),
     2111                    array(
     2112                        'key1' => 'value1',
     2113                        'key2' => 'value2',
     2114                        'key3' => '',
     2115                    ),
     2116                ),
     2117                array(
     2118                    array(
     2119                        'title'   => 'My New Site',
     2120                        'options' => array(
     2121                            'blogdescription' => 'Just My New Site',
     2122                        ),
     2123                    ),
     2124                    array(
     2125                        'blogname'        => 'My New Site',
     2126                        'blogdescription' => 'Just My New Site',
     2127                    ),
     2128                    array(),
     2129                ),
     2130            );
     2131        }
     2132
     2133        /**
     2134         * @ticket 41333
     2135         */
     2136        public function test_wp_initialize_site_user_roles() {
     2137            global $wpdb;
     2138
     2139            $result = wp_initialize_site( self::$uninitialized_site_id, array() );
     2140
     2141            switch_to_blog( self::$uninitialized_site_id );
     2142            $table_prefix = $wpdb->get_blog_prefix( self::$uninitialized_site_id );
     2143            $roles        = get_option( $table_prefix . 'user_roles' );
     2144            restore_current_blog();
     2145
     2146            wp_uninitialize_site( self::$uninitialized_site_id );
     2147
     2148            $this->assertTrue( $result );
     2149            $this->assertEqualSets(
     2150                array(
     2151                    'administrator',
     2152                    'editor',
     2153                    'author',
     2154                    'contributor',
     2155                    'subscriber',
     2156                ),
     2157                array_keys( $roles )
     2158            );
     2159        }
     2160
     2161        /**
     2162         * @ticket 41333
     2163         */
     2164        public function test_wp_initialize_site_user_is_admin() {
     2165            $result = wp_initialize_site( self::$uninitialized_site_id, array( 'user_id' => 1 ) );
     2166
     2167            switch_to_blog( self::$uninitialized_site_id );
     2168            $user_is_admin = user_can( 1, 'manage_options' );
     2169            $admin_email   = get_option( 'admin_email' );
     2170            restore_current_blog();
     2171
     2172            wp_uninitialize_site( self::$uninitialized_site_id );
     2173
     2174            $this->assertTrue( $result );
     2175            $this->assertTrue( $user_is_admin );
     2176            $this->assertEquals( get_userdata( 1 )->user_email, $admin_email );
     2177        }
     2178
     2179        /**
     2180         * @ticket 41333
     2181         */
     2182        public function test_wp_initialize_site_args_filter() {
     2183            add_filter( 'wp_initialize_site_args', array( $this, 'filter_wp_initialize_site_args' ), 10, 3 );
     2184            $result = wp_initialize_site( self::$uninitialized_site_id, array( 'title' => 'My Site' ) );
     2185
     2186            switch_to_blog( self::$uninitialized_site_id );
     2187            $site_title = get_option( 'blogname' );
     2188            restore_current_blog();
     2189
     2190            wp_uninitialize_site( self::$uninitialized_site_id );
     2191
     2192            $this->assertSame(
     2193                sprintf( 'My Site %1$d in Network %2$d', self::$uninitialized_site_id, get_site( self::$uninitialized_site_id )->network_id ),
     2194                $site_title
     2195            );
     2196        }
     2197
     2198        public function filter_wp_initialize_site_args( $args, $site, $network ) {
     2199            $args['title'] = sprintf( 'My Site %1$d in Network %2$d', $site->id, $network->id );
     2200
     2201            return $args;
     2202        }
     2203
     2204        /**
     2205         * @ticket 41333
     2206         */
     2207        public function test_wp_initialize_site_empty_id() {
     2208            $result = wp_initialize_site( 0 );
     2209            $this->assertWPError( $result );
     2210            $this->assertSame( 'site_empty_id', $result->get_error_code() );
     2211        }
     2212
     2213        /**
     2214         * @ticket 41333
     2215         */
     2216        public function test_wp_initialize_site_invalid_id() {
     2217            $result = wp_initialize_site( 123 );
     2218            $this->assertWPError( $result );
     2219            $this->assertSame( 'site_invalid_id', $result->get_error_code() );
     2220        }
     2221
     2222        /**
     2223         * @ticket 41333
     2224         */
     2225        public function test_wp_initialize_site_already_initialized() {
     2226            $result = wp_initialize_site( get_current_blog_id() );
     2227            $this->assertWPError( $result );
     2228            $this->assertSame( 'site_already_initialized', $result->get_error_code() );
     2229        }
     2230
     2231        /**
     2232         * @ticket 41333
     2233         */
     2234        public function test_wp_uninitialize_site() {
     2235            $site_id = self::factory()->blog->create();
     2236
     2237            $result = wp_uninitialize_site( $site_id );
     2238            $this->assertTrue( $result );
     2239            $this->assertFalse( wp_is_site_initialized( $site_id ) );
     2240        }
     2241
     2242        /**
     2243         * @ticket 41333
     2244         */
     2245        public function test_wp_uninitialize_site_empty_id() {
     2246            $result = wp_uninitialize_site( 0 );
     2247            $this->assertWPError( $result );
     2248            $this->assertSame( 'site_empty_id', $result->get_error_code() );
     2249        }
     2250
     2251        /**
     2252         * @ticket 41333
     2253         */
     2254        public function test_wp_uninitialize_site_invalid_id() {
     2255            $result = wp_uninitialize_site( 123 );
     2256            $this->assertWPError( $result );
     2257            $this->assertSame( 'site_invalid_id', $result->get_error_code() );
     2258        }
     2259
     2260        /**
     2261         * @ticket 41333
     2262         */
     2263        public function test_wp_uninitialize_site_already_uninitialized() {
     2264            $result = wp_uninitialize_site( self::$uninitialized_site_id );
     2265            $this->assertWPError( $result );
     2266            $this->assertSame( 'site_already_uninitialized', $result->get_error_code() );
     2267        }
     2268
     2269        /**
     2270         * @ticket 41333
     2271         */
     2272        public function test_wp_is_site_initialized() {
     2273            $this->assertTrue( wp_is_site_initialized( get_current_blog_id() ) );
     2274            $this->assertFalse( wp_is_site_initialized( self::$uninitialized_site_id ) );
     2275        }
     2276
     2277        /**
     2278         * @ticket 41333
     2279         */
     2280        public function test_wp_is_site_initialized_prefilter() {
     2281            add_filter( 'pre_wp_is_site_initialized', '__return_false' );
     2282            $this->assertFalse( wp_is_site_initialized( get_current_blog_id() ) );
     2283
     2284            add_filter( 'pre_wp_is_site_initialized', '__return_true' );
     2285            $this->assertTrue( wp_is_site_initialized( self::$uninitialized_site_id ) );
     2286        }
     2287
     2288        /**
     2289         * @ticket 41333
     2290         */
     2291        public function test_wp_insert_site_forwards_args_to_wp_initialize_site() {
     2292            $args = array(
     2293                'user_id' => 1,
     2294                'title'   => 'My Site',
     2295                'options' => array( 'option1' => 'value1' ),
     2296                'meta'    => array( 'meta1' => 'value1' ),
     2297            );
     2298
     2299            add_filter( 'wp_initialize_site_args', array( $this, 'filter_wp_initialize_site_args_catch_args' ) );
     2300            $site_id = wp_insert_site(
     2301                array_merge(
     2302                    array(
     2303                        'domain' => 'testsite.org',
     2304                        'path'   => '/',
     2305                    ),
     2306                    $args
     2307                )
     2308            );
     2309            $passed_args = $this->wp_initialize_site_args;
     2310
     2311            $this->wp_initialize_site_args = null;
     2312
     2313            $this->assertEqualSetsWithIndex( $args, $passed_args );
     2314        }
     2315
     2316        public function filter_wp_initialize_site_args_catch_args( $args ) {
     2317            $this->wp_initialize_site_args = $args;
     2318
     2319            return $args;
     2320        }
    20182321    }
    20192322
Note: See TracChangeset for help on using the changeset viewer.