Make WordPress Core

Ticket #41333: 41333.2.diff

File 41333.2.diff, 26.4 KB (added by flixos90, 6 years ago)
  • src/wp-admin/includes/ms.php

     
    7373        }
    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();
    10378
     
    11994        }
    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 );
    143                         }
    144                 }
    145 
    14697                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', 'wp_delete_site' );
    147101
    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 );
     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 );
    180113                        }
    181                         $index++;
    182114                }
    183115
    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                 }
     116                update_blog_status( $blog_id, 'deleted', 1 );
    190117
    191                 clean_blog_cache( $blog );
     118                /** This action is documented in wp-includes/ms-blogs.php */
     119                do_action_deprecated( 'deleted_blog', array( $blog_id, false ), '5.0.0', 'wp_delete_site' );
    192120        }
    193121
    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 );
    203 
    204122        if ( $switch ) {
    205123                restore_current_blog();
    206124        }
  • src/wp-admin/includes/schema.php

     
    356356 * Create WordPress options and set the default values.
    357357 *
    358358 * @since 1.5.0
     359 * @since 5.0.0 The $options parameter has been added.
    359360 *
    360361 * @global wpdb $wpdb WordPress database abstraction object.
    361362 * @global int  $wp_db_version
    362363 * @global int  $wp_current_db_version
     364 *
     365 * @param array $options Optional. Custom option $key => $value pairs to use. Default empty array.
    363366 */
    364 function populate_options() {
     367function populate_options( array $options = array() ) {
    365368        global $wpdb, $wp_db_version, $wp_current_db_version;
    366369
    367370        $guessurl = wp_guess_url();
     
    406409                        $timezone_string = $offset_or_tz;
    407410        }
    408411
    409         $options = array(
     412        $options = wp_parse_args( $options, array(
    410413                'siteurl'                         => $guessurl,
    411414                'home'                            => $guessurl,
    412415                'blogname'                        => __( 'My Site' ),
     
    538541
    539542                // 4.9.8
    540543                'show_comments_cookies_opt_in'    => 0,
    541         );
     544        ) );
    542545
    543546        // 3.3
    544547        if ( ! is_multisite() ) {
  • src/wp-includes/ms-blogs.php

     
    442442                'lang_id'      => 0,
    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 ) ) {
    447453                return $data;
     
    464470         */
    465471        do_action( 'wp_insert_site', $new_site );
    466472
     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        }
     503
    467504        return (int) $new_site->id;
    468505}
    469506
     
    543580                return new WP_Error( 'site_not_exist', __( 'Site does not exist.' ) );
    544581        }
    545582
     583        /**
     584         * Fires before a site is deleted.
     585         *
     586         * @since MU (3.0.0)
     587         * @deprecated 5.0.0 Use wp_delete_site
     588         *
     589         * @param int  $site_id The site ID.
     590         * @param bool $drop    True if site's table should be dropped. Default is false.
     591         */
     592        do_action_deprecated( 'delete_blog', array( $old_site->id, true ), '5.0.0', 'wp_delete_site' );
     593
     594        /**
     595         * Fires when a site's uninitialization routine should be executed.
     596         *
     597         * @since 5.0.0
     598         *
     599         * @param WP_Site $old_site Deleted site object.
     600         */
     601        do_action( 'wp_uninitialize_site', $old_site );
     602
     603        if ( is_site_meta_supported() ) {
     604                $blog_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->blogmeta WHERE blog_id = %d ", $old_site->id ) );
     605                foreach ( $blog_meta_ids as $mid ) {
     606                        delete_metadata_by_mid( 'blog', $mid );
     607                }
     608        }
     609
    546610        if ( false === $wpdb->delete( $wpdb->blogs, array( 'blog_id' => $old_site->id ) ) ) {
    547611                return new WP_Error( 'db_delete_error', __( 'Could not delete site from the database.' ), $wpdb->last_error );
    548612        }
     
    558622         */
    559623        do_action( 'wp_delete_site', $old_site );
    560624
     625        /**
     626         * Fires after the site is deleted from the network.
     627         *
     628         * @since 4.8.0
     629         * @deprecated 5.0.0 Use wp_delete_site
     630         *
     631         * @param int  $site_id The site ID.
     632         * @param bool $drop    True if site's tables should be dropped. Default is false.
     633         */
     634        do_action_deprecated( 'deleted_blog', array( $old_site->id, true ), '5.0.0', 'wp_delete_site' );
     635
    561636        return $old_site;
    562637}
    563638
     
    913988}
    914989
    915990/**
     991 * Runs the initialization routine for a given site.
     992 *
     993 * This process includes creating the site's database tables and
     994 * populating them with defaults.
     995 *
     996 * @since 5.0.0
     997 *
     998 * @global wpdb     $wpdb     WordPress database abstraction object.
     999 * @global WP_Roles $wp_roles WordPress role management object.
     1000 *
     1001 * @param int|WP_Site $site_id Site ID or object.
     1002 * @param array       $args    {
     1003 *     Optional. Arguments to modify the initialization behavior.
     1004 *
     1005 *     @type int    $user_id Required. User ID for the site administrator.
     1006 *     @type string $title   Site title. Default is 'Site %d' where %d is the
     1007 *                           site ID.
     1008 *     @type array  $options Custom option $key => $value pairs to use. Default
     1009 *                           empty array.
     1010 * }
     1011 * @return bool|WP_Error True on success, or error object on failure.
     1012 */
     1013function wp_initialize_site( $site_id, array $args = array() ) {
     1014        global $wpdb, $wp_roles;
     1015
     1016        if ( empty( $site_id ) ) {
     1017                return new WP_Error( 'site_empty_id', __( 'Site ID must not be empty.' ) );
     1018        }
     1019
     1020        $site = get_site( $site_id );
     1021        if ( ! $site ) {
     1022                return new WP_Error( 'site_invalid_id', __( 'Site with the ID does not exist.' ) );
     1023        }
     1024
     1025        if ( empty( $args['user_id'] ) ) {
     1026                return new WP_Error( 'site_empty_user_id', __( 'A user ID must be specified for the site administrator.' ) );
     1027        }
     1028
     1029        if ( wp_is_site_initialized( $site ) ) {
     1030                return new WP_Error( 'site_already_initialized', __( 'The site appears to be already initialized.' ) );
     1031        }
     1032
     1033        $network = get_network( $site->network_id );
     1034        if ( ! $network ) {
     1035                $network = get_network();
     1036        }
     1037
     1038        $args = wp_parse_args( $args, array(
     1039                'user_id' => 0,
     1040                /* translators: %d: site ID */
     1041                'title'   => sprintf( __( 'Site %d' ), $site->id ),
     1042                'options' => array(),
     1043        ) );
     1044
     1045        if ( ! wp_installing() ) {
     1046                wp_installing( true );
     1047        }
     1048
     1049        $switch = false;
     1050        if ( get_current_blog_id() !== $site->id ) {
     1051                $switch = true;
     1052                switch_to_blog( $site->id );
     1053        }
     1054
     1055        require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
     1056
     1057        // Set up the database tables.
     1058        make_db_current_silent( 'blog' );
     1059
     1060        $home_scheme    = 'http';
     1061        $siteurl_scheme = 'http';
     1062        if ( ! is_subdomain_install() ) {
     1063                if ( 'https' === parse_url( get_home_url( $network->site_id ), PHP_URL_SCHEME ) ) {
     1064                        $home_scheme = 'https';
     1065                }
     1066                if ( 'https' === parse_url( get_network_option( $network->id, 'siteurl' ), PHP_URL_SCHEME ) ) {
     1067                        $siteurl_scheme = 'https';
     1068                }
     1069        }
     1070
     1071        // Populate the site's options.
     1072        populate_options( array(
     1073                'home'        => untrailingslashit( $home_scheme . '://' . $site->domain . $site->path ),
     1074                'siteurl'     => untrailingslashit( $siteurl_scheme . '://' . $site->domain . $site->path ),
     1075                'blogname'    => wp_unslash( $args['title'] ),
     1076                'admin_email' => '',
     1077                'upload_path' => get_network_option( $network->id, 'ms_files_rewriting' ) ? UPLOADBLOGSDIR . "/{$site->id}/files" : get_blog_option( $network->site_id, 'upload_path' ),
     1078                'blog_public' => (int) $site->public,
     1079                'WPLANG'      => get_network_option( $network->id, 'WPLANG' ),
     1080        ) );
     1081
     1082        // Populate the site's roles.
     1083        populate_roles();
     1084        $wp_roles = new WP_Roles();
     1085
     1086        // Remove all permissions that may exist for the site.
     1087        $table_prefix = $wpdb->get_blog_prefix();
     1088        delete_metadata( 'user', 0, $table_prefix . 'user_level', null, true ); // delete all
     1089        delete_metadata( 'user', 0, $table_prefix . 'capabilities', null, true ); // delete all
     1090
     1091        // Install default site content.
     1092        wp_install_defaults( $args['user_id'] );
     1093
     1094        // Set the site administrator.
     1095        add_user_to_blog( $site->id, $args['user_id'], 'administrator' );
     1096        if ( ! user_can( $args['user_id'], 'manage_network' ) && ! get_user_meta( $args['user_id'], 'primary_blog', true ) ) {
     1097                update_user_meta( $args['user_id'], 'primary_blog', $site->id );
     1098        }
     1099
     1100        // Override any specified options with custom values.
     1101        foreach ( $args['options'] as $key => $value ) {
     1102                update_option( $key, $value );
     1103        }
     1104
     1105        if ( $switch ) {
     1106                restore_current_blog();
     1107        }
     1108
     1109        return true;
     1110}
     1111
     1112/**
     1113 * Runs the uninitialization routine for a given site.
     1114 *
     1115 * This process includes dropping the site's database tables and deleting its uploads directory.
     1116 *
     1117 * @since 5.0.0
     1118 *
     1119 * @global wpdb $wpdb WordPress database abstraction object.
     1120 *
     1121 * @param int|WP_Site $site_id Site ID or object.
     1122 * @return bool|WP_Error True on success, or error object on failure.
     1123 */
     1124function wp_uninitialize_site( $site_id ) {
     1125        global $wpdb;
     1126
     1127        if ( empty( $site_id ) ) {
     1128                return new WP_Error( 'site_empty_id', __( 'Site ID must not be empty.' ) );
     1129        }
     1130
     1131        $site = get_site( $site_id );
     1132        if ( ! $site ) {
     1133                return new WP_Error( 'site_invalid_id', __( 'Site with the ID does not exist.' ) );
     1134        }
     1135
     1136        if ( ! wp_is_site_initialized( $site ) ) {
     1137                return new WP_Error( 'site_already_uninitialized', __( 'The site appears to be already uninitialized.' ) );
     1138        }
     1139
     1140        $users = get_users( array(
     1141                'blog_id' => $site->id,
     1142                'fields'  => 'ids',
     1143        ) );
     1144
     1145        // Remove users from the site.
     1146        if ( ! empty( $users ) ) {
     1147                foreach ( $users as $user_id ) {
     1148                        remove_user_from_blog( $user_id, $site->id );
     1149                }
     1150        }
     1151
     1152        $switch = false;
     1153        if ( get_current_blog_id() !== $site->id ) {
     1154                $switch = true;
     1155                switch_to_blog( $site->id );
     1156        }
     1157
     1158        $uploads = wp_get_upload_dir();
     1159
     1160        $tables = $wpdb->tables( 'blog' );
     1161
     1162        /**
     1163         * Filters the tables to drop when the site is deleted.
     1164         *
     1165         * @since MU (3.0.0)
     1166         *
     1167         * @param string[] $tables  Array of names of the site tables to be dropped.
     1168         * @param int      $site_id The ID of the site to drop tables for.
     1169         */
     1170        $drop_tables = apply_filters( 'wpmu_drop_tables', $tables, $site->id );
     1171
     1172        foreach ( (array) $drop_tables as $table ) {
     1173                $wpdb->query( "DROP TABLE IF EXISTS `$table`" ); // phpcs:ignore WordPress.WP.PreparedSQL.NotPrepared
     1174        }
     1175
     1176        /**
     1177         * Filters the upload base directory to delete when the site is deleted.
     1178         *
     1179         * @since MU (3.0.0)
     1180         *
     1181         * @param string $uploads['basedir'] Uploads path without subdirectory. @see wp_upload_dir()
     1182         * @param int    $site_id            The site ID.
     1183         */
     1184        $dir     = apply_filters( 'wpmu_delete_blog_upload_dir', $uploads['basedir'], $site->id );
     1185        $dir     = rtrim( $dir, DIRECTORY_SEPARATOR );
     1186        $top_dir = $dir;
     1187        $stack   = array( $dir );
     1188        $index   = 0;
     1189
     1190        while ( $index < count( $stack ) ) {
     1191                // Get indexed directory from stack
     1192                $dir = $stack[ $index ];
     1193
     1194                $dh = @opendir( $dir );
     1195                if ( $dh ) {
     1196                        $file = @readdir( $dh );
     1197                        while ( false !== $file ) {
     1198                                if ( '.' === $file || '..' === $file ) {
     1199                                        $file = @readdir( $dh );
     1200                                        continue;
     1201                                }
     1202
     1203                                if ( @is_dir( $dir . DIRECTORY_SEPARATOR . $file ) ) {
     1204                                        $stack[] = $dir . DIRECTORY_SEPARATOR . $file;
     1205                                } elseif ( @is_file( $dir . DIRECTORY_SEPARATOR . $file ) ) {
     1206                                        @unlink( $dir . DIRECTORY_SEPARATOR . $file );
     1207                                }
     1208
     1209                                $file = @readdir( $dh );
     1210                        }
     1211                        @closedir( $dh );
     1212                }
     1213                $index++;
     1214        }
     1215
     1216        $stack = array_reverse( $stack ); // Last added dirs are deepest
     1217        foreach ( (array) $stack as $dir ) {
     1218                if ( $dir != $top_dir ) {
     1219                        @rmdir( $dir );
     1220                }
     1221        }
     1222
     1223        if ( $switch ) {
     1224                restore_current_blog();
     1225        }
     1226
     1227        return true;
     1228}
     1229
     1230/**
     1231 * Checks whether a site is initialized.
     1232 *
     1233 * A site is considered initialized when its database tables are present.
     1234 *
     1235 * @since 5.0.0
     1236 *
     1237 * @global wpdb $wpdb WordPress database abstraction object.
     1238 *
     1239 * @param int|WP_Site $site_id Site ID or object.
     1240 * @return bool True if the site is initialized, false otherwise.
     1241 */
     1242function wp_is_site_initialized( $site_id ) {
     1243        global $wpdb;
     1244
     1245        if ( is_object( $site_id ) ) {
     1246                $site_id = $site_id->blog_id;
     1247        }
     1248        $site_id = (int) $site_id;
     1249
     1250        /**
     1251         * Filters the check for whether a site is initialized before the database is accessed.
     1252         *
     1253         * Returning a non-null value will effectively short-circuit the function, returning
     1254         * that value instead.
     1255         *
     1256         * @since 5.0.0
     1257         *
     1258         * @param bool|null $pre     The value to return, if not null.
     1259         * @param int       $site_id The site ID that is being checked.
     1260         */
     1261        $pre = apply_filters( 'pre_wp_is_site_initialized', null, $site_id );
     1262        if ( null !== $pre ) {
     1263                return (bool) $pre;
     1264        }
     1265
     1266        $switch = false;
     1267        if ( get_current_blog_id() !== $site_id ) {
     1268                $switch = true;
     1269                remove_action( 'switch_blog', 'wp_switch_roles_and_user', 1 );
     1270                switch_to_blog( $site_id );
     1271        }
     1272
     1273        $result   = false;
     1274        $suppress = $wpdb->suppress_errors();
     1275        if ( $wpdb->get_results( "DESCRIBE {$wpdb->posts}" ) ) {
     1276                $result = true;
     1277        }
     1278        $wpdb->suppress_errors( $suppress );
     1279
     1280        if ( $switch ) {
     1281                restore_current_blog();
     1282                add_action( 'switch_blog', 'wp_switch_roles_and_user', 1, 2 );
     1283        }
     1284
     1285        return $result;
     1286}
     1287
     1288/**
    9161289 * Retrieve option value for a given blog id based on name of option.
    9171290 *
    9181291 * If the option does not exist or does not have a value, then the return value
     
    18862259 * @param string $public  The value of the site status.
    18872260 */
    18882261function wp_update_blog_public_option_on_site_update( $site_id, $public ) {
     2262
     2263        // Bail if the site's database tables do not exist (yet).
     2264        if ( ! wp_is_site_initialized( $site_id ) ) {
     2265                return;
     2266        }
     2267
    18892268        update_blog_option( $site_id, 'blog_public', $public );
    18902269}
  • src/wp-includes/ms-default-filters.php

     
    4949add_action( 'wp_insert_site', 'wp_maybe_transition_site_statuses_on_update', 10, 1 );
    5050add_action( 'wp_update_site', 'wp_maybe_transition_site_statuses_on_update', 10, 2 );
    5151add_action( 'wp_update_site', 'wp_maybe_clean_new_site_cache_on_update', 10, 2 );
     52add_action( 'wp_initialize_site', 'wp_initialize_site', 10, 2 );
     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
    5456// Register Nonce
  • src/wp-includes/ms-deprecated.php

     
    580580
    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        require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
     674
     675        $suppress = $wpdb->suppress_errors();
     676
     677        wp_install_defaults( $user_id );
     678
     679        $wpdb->suppress_errors( $suppress );
     680}
  • src/wp-includes/ms-functions.php

     
    12691269function wpmu_create_blog( $domain, $path, $title, $user_id, $meta = array(), $network_id = 1 ) {
    12701270        $defaults = array(
    12711271                'public' => 0,
    1272                 'WPLANG' => get_network_option( $network_id, 'WPLANG' ),
    12731272        );
    12741273        $meta     = wp_parse_args( $meta, $defaults );
    12751274
     
    12991298                )
    13001299        );
    13011300
    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 );
     1301        $blog_id = wp_insert_site( array_merge( $site_data, array(
     1302                'title'   => $title,
     1303                'user_id' => $user_id,
     1304                'options' => array_diff_key( $meta, array_flip( $site_data_whitelist ) ),
     1305        ) ) );
    13071306
    13081307        if ( is_wp_error( $blog_id ) ) {
    13091308                return $blog_id;
    13101309        }
    13111310
    1312         switch_to_blog( $blog_id );
    1313         install_blog( $blog_id, $title );
    1314         wp_install_defaults( $user_id );
    1315 
    1316         add_user_to_blog( $blog_id, $user_id, 'administrator' );
    1317 
    1318         foreach ( $meta as $key => $value ) {
    1319                 update_option( $key, $value );
    1320         }
    1321 
    1322         update_option( 'blog_public', (int) $site_data['public'] );
    1323 
    1324         if ( ! is_super_admin( $user_id ) && ! get_user_meta( $user_id, 'primary_blog', true ) ) {
    1325                 update_user_meta( $user_id, 'primary_blog', $blog_id );
    1326         }
    1327 
    1328         restore_current_blog();
    1329 
    1330         $site = get_site( $blog_id );
    1331 
    1332         /**
    1333          * Fires immediately after a new site is created.
    1334          *
    1335          * @since MU (3.0.0)
    1336          *
    1337          * @param int    $blog_id    Site ID.
    1338          * @param int    $user_id    User ID.
    1339          * @param string $domain     Site domain.
    1340          * @param string $path       Site path.
    1341          * @param int    $network_id Network ID. Only relevant on multi-network installations.
    1342          * @param array  $meta       Meta data. Used to set initial site options.
    1343          */
    1344         do_action( 'wpmu_new_blog', $blog_id, $user_id, $site->domain, $site->path, $site->network_id, $meta );
    1345 
    13461311        wp_cache_set( 'last_changed', microtime(), 'sites' );
    13471312
    13481313        return $blog_id;
     
    14951460}
    14961461
    14971462/**
    1498  * Install an empty blog.
    1499  *
    1500  * Creates the new blog tables and options. If calling this function
    1501  * directly, be sure to use switch_to_blog() first, so that $wpdb
    1502  * points to the new blog.
    1503  *
    1504  * @since MU (3.0.0)
    1505  *
    1506  * @global wpdb     $wpdb
    1507  * @global WP_Roles $wp_roles
    1508  *
    1509  * @param int    $blog_id    The value returned by wp_insert_site().
    1510  * @param string $blog_title The title of the new site.
    1511  */
    1512 function install_blog( $blog_id, $blog_title = '' ) {
    1513         global $wpdb, $wp_roles;
    1514 
    1515         // Cast for security
    1516         $blog_id = (int) $blog_id;
    1517 
    1518         require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    1519 
    1520         $suppress = $wpdb->suppress_errors();
    1521         if ( $wpdb->get_results( "DESCRIBE {$wpdb->posts}" ) ) {
    1522                 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>' );
    1523         }
    1524         $wpdb->suppress_errors( $suppress );
    1525 
    1526         $url = get_blogaddress_by_id( $blog_id );
    1527 
    1528         // Set everything up
    1529         make_db_current_silent( 'blog' );
    1530         populate_options();
    1531         populate_roles();
    1532 
    1533         // populate_roles() clears previous role definitions so we start over.
    1534         $wp_roles = new WP_Roles();
    1535 
    1536         $siteurl = $home = untrailingslashit( $url );
    1537 
    1538         if ( ! is_subdomain_install() ) {
    1539 
    1540                 if ( 'https' === parse_url( get_site_option( 'siteurl' ), PHP_URL_SCHEME ) ) {
    1541                         $siteurl = set_url_scheme( $siteurl, 'https' );
    1542                 }
    1543                 if ( 'https' === parse_url( get_home_url( get_network()->site_id ), PHP_URL_SCHEME ) ) {
    1544                         $home = set_url_scheme( $home, 'https' );
    1545                 }
    1546         }
    1547 
    1548         update_option( 'siteurl', $siteurl );
    1549         update_option( 'home', $home );
    1550 
    1551         if ( get_site_option( 'ms_files_rewriting' ) ) {
    1552                 update_option( 'upload_path', UPLOADBLOGSDIR . "/$blog_id/files" );
    1553         } else {
    1554                 update_option( 'upload_path', get_blog_option( get_network()->site_id, 'upload_path' ) );
    1555         }
    1556 
    1557         update_option( 'blogname', wp_unslash( $blog_title ) );
    1558         update_option( 'admin_email', '' );
    1559 
    1560         // remove all perms
    1561         $table_prefix = $wpdb->get_blog_prefix();
    1562         delete_metadata( 'user', 0, $table_prefix . 'user_level', null, true ); // delete all
    1563         delete_metadata( 'user', 0, $table_prefix . 'capabilities', null, true ); // delete all
    1564 }
    1565 
    1566 /**
    1567  * Set blog defaults.
    1568  *
    1569  * This function creates a row in the wp_blogs table.
    1570  *
    1571  * @since MU (3.0.0)
    1572  * @deprecated MU
    1573  * @deprecated Use wp_install_defaults()
    1574  *
    1575  * @global wpdb $wpdb WordPress database abstraction object.
    1576  *
    1577  * @param int $blog_id Ignored in this function.
    1578  * @param int $user_id
    1579  */
    1580 function install_blog_defaults( $blog_id, $user_id ) {
    1581         global $wpdb;
    1582 
    1583         require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    1584 
    1585         $suppress = $wpdb->suppress_errors();
    1586 
    1587         wp_install_defaults( $user_id );
    1588 
    1589         $wpdb->suppress_errors( $suppress );
    1590 }
    1591 
    1592 /**
    15931463 * Notify a user that their blog activation has been successful.
    15941464 *
    15951465 * Filter {@see 'wpmu_welcome_notification'} to disable or bypass.