Make WordPress Core

Ticket #41333: 41333.3.diff

File 41333.3.diff, 31.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' );
    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' );
    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-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        $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 );
    548631        }
     
    558641         */
    559642        do_action( 'wp_delete_site', $old_site );
    560643
     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' );
     654
    561655        return $old_site;
    562656}
    563657
     
    619713
    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 );
    625719        }
     
    9131007}
    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        $result   = false;
     1314        $suppress = $wpdb->suppress_errors();
     1315        if ( $wpdb->get_results( "DESCRIBE {$wpdb->posts}" ) ) {
     1316                $result = true;
     1317        }
     1318        $wpdb->suppress_errors( $suppress );
     1319
     1320        if ( $switch ) {
     1321                restore_current_blog();
     1322                add_action( 'switch_blog', 'wp_switch_roles_and_user', 1, 2 );
     1323        }
     1324
     1325        return $result;
     1326}
     1327
     1328/**
    9161329 * Retrieve option value for a given blog id based on name of option.
    9171330 *
    9181331 * If the option does not exist or does not have a value, then the return value
     
    16212034
    16222035        $non_cached_ids = _get_non_cached_ids( $network_ids, 'networks' );
    16232036        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 ) ) ) );
     2037                $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
    16252038
    16262039                update_network_cache( $fresh_networks );
    16272040        }
     
    17572170        }
    17582171
    17592172        if ( $new_site->spam != $old_site->spam ) {
    1760                 if ( $new_site->spam == 1 ) {
     2173                if ( 1 == $new_site->spam ) {
    17612174
    17622175                        /**
    17632176                         * Fires when the 'spam' status is added to a site.
     
    17812194        }
    17822195
    17832196        if ( $new_site->mature != $old_site->mature ) {
    1784                 if ( $new_site->mature == 1 ) {
     2197                if ( 1 == $new_site->mature ) {
    17852198
    17862199                        /**
    17872200                         * Fires when the 'mature' status is added to a site.
     
    18052218        }
    18062219
    18072220        if ( $new_site->archived != $old_site->archived ) {
    1808                 if ( $new_site->archived == 1 ) {
     2221                if ( 1 == $new_site->archived ) {
    18092222
    18102223                        /**
    18112224                         * Fires when the 'archived' status is added to a site.
     
    18292242        }
    18302243
    18312244        if ( $new_site->deleted != $old_site->deleted ) {
    1832                 if ( $new_site->deleted == 1 ) {
     2245                if ( 1 == $new_site->deleted ) {
    18332246
    18342247                        /**
    18352248                         * Fires when the 'deleted' status is added to a site.
     
    18892302 * @param string $public  The value of the site status.
    18902303 */
    18912304function wp_update_blog_public_option_on_site_update( $site_id, $public ) {
     2305
     2306        // Bail if the site's database tables do not exist (yet).
     2307        if ( ! wp_is_site_initialized( $site_id ) ) {
     2308                return;
     2309        }
     2310
    18922311        update_blog_option( $site_id, 'blog_public', $public );
    18932312}
  • src/wp-includes/ms-default-filters.php

     
    3737
    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 );
    4442add_filter( 'wp_normalize_site_data', 'wp_normalize_site_data', 10, 1 );
     
    4947add_action( 'wp_insert_site', 'wp_maybe_transition_site_statuses_on_update', 10, 1 );
    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
    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

     
    12851285 * @param string $path       The new site's path.
    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
    12911291 *                           updated. Otherwise, keys and values will be used to set options for
     
    12931293 * @param int    $network_id Optional. Network ID. Only relevant on multi-network installations.
    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 );
    13041303        $user_id = (int) $user_id;
     
    13211320                        'network_id' => $network_id,
    13221321                ),
    13231322                array_intersect_key(
    1324                         $meta,
     1323                        $options,
    13251324                        array_flip( $site_data_whitelist )
    13261325                )
    13271326        );
    13281327
    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 );
     1328        $blog_id = wp_insert_site(
     1329                array_merge(
     1330                        $site_data,
     1331                        array(
     1332                                'title'   => $title,
     1333                                'user_id' => $user_id,
     1334                                'options' => array_diff_key( $options, array_flip( $site_data_whitelist ) ),
     1335                        )
     1336                )
     1337        );
    13341338
    13351339        if ( is_wp_error( $blog_id ) ) {
    13361340                return $blog_id;
    13371341        }
    13381342
    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 
    13731343        wp_cache_set( 'last_changed', microtime(), 'sites' );
    13741344
    13751345        return $blog_id;
     
    13821352 * the notification email.
    13831353 *
    13841354 * @since MU (3.0.0)
     1355 * @since 5.0.0 $blog_id now supports input from the {@see 'wp_initialize_site'} action.
    13851356 *
    1386  * @param int    $blog_id    The new site's ID.
    1387  * @param string $deprecated Not used.
     1357 * @param WP_Site|int $blog_id    The new site's object or ID.
     1358 * @param string      $deprecated Not used.
    13881359 * @return bool
    13891360 */
    13901361function newblog_notify_siteadmin( $blog_id, $deprecated = '' ) {
     1362        if ( is_object( $blog_id ) ) {
     1363                $blog_id = $blog_id->blog_id;
     1364        }
     1365
    13911366        if ( get_site_option( 'registrationnotification' ) != 'yes' ) {
    13921367                return false;
    13931368        }
     
    15291504}
    15301505
    15311506/**
    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 );
    1624 }
    1625 
    1626 /**
    16271507 * Notify a user that their blog activation has been successful.
    16281508 *
    16291509 * Filter {@see 'wpmu_welcome_notification'} to disable or bypass.
     
    20241904 * Logs the user email, IP, and registration date of a new site.
    20251905 *
    20261906 * @since MU (3.0.0)
     1907 * @since 5.0.0 Parameters now support input from the {@see 'wp_initialize_site'} action.
    20271908 *
    20281909 * @global wpdb $wpdb WordPress database abstraction object.
    20291910 *
    2030  * @param int $blog_id
    2031  * @param int $user_id
     1911 * @param WP_Site|int $blog_id The new site's object or ID.
     1912 * @param int|array   $user_id User ID, or array of arguments including 'user_id'.
    20321913 */
    20331914function wpmu_log_new_registrations( $blog_id, $user_id ) {
    20341915        global $wpdb;
     1916
     1917        if ( is_object( $blog_id ) ) {
     1918                $blog_id = $blog_id->blog_id;
     1919        }
     1920
     1921        if ( is_array( $user_id ) ) {
     1922                $user_id = ! empty( $user_id['user_id'] ) ? $user_id['user_id'] : 0;
     1923        }
     1924
    20351925        $user = get_userdata( (int) $user_id );
    20361926        if ( $user ) {
    20371927                $wpdb->insert(