wp/wp-admin/includes/schema.php
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
equal deleted inserted replaced
4:346c88efed21 5:5e2f62d02dcd
    41 	if ( $blog_id && $blog_id != $wpdb->blogid )
    41 	if ( $blog_id && $blog_id != $wpdb->blogid )
    42 		$old_blog_id = $wpdb->set_blog_id( $blog_id );
    42 		$old_blog_id = $wpdb->set_blog_id( $blog_id );
    43 
    43 
    44 	// Engage multisite if in the middle of turning it on from network.php.
    44 	// Engage multisite if in the middle of turning it on from network.php.
    45 	$is_multisite = is_multisite() || ( defined( 'WP_INSTALLING_NETWORK' ) && WP_INSTALLING_NETWORK );
    45 	$is_multisite = is_multisite() || ( defined( 'WP_INSTALLING_NETWORK' ) && WP_INSTALLING_NETWORK );
       
    46 
       
    47 	/*
       
    48 	 * Indexes have a maximum size of 767 bytes. Historically, we haven't need to be concerned about that.
       
    49 	 * As of 4.2, however, we moved to utf8mb4, which uses 4 bytes per character. This means that an index which
       
    50 	 * used to have room for floor(767/3) = 255 characters, now only has room for floor(767/4) = 191 characters.
       
    51 	 */
       
    52 	$max_index_length = 191;
    46 
    53 
    47 	// Blog specific tables.
    54 	// Blog specific tables.
    48 	$blog_tables = "CREATE TABLE $wpdb->terms (
    55 	$blog_tables = "CREATE TABLE $wpdb->terms (
    49  term_id bigint(20) unsigned NOT NULL auto_increment,
    56  term_id bigint(20) unsigned NOT NULL auto_increment,
    50  name varchar(200) NOT NULL default '',
    57  name varchar(200) NOT NULL default '',
    51  slug varchar(200) NOT NULL default '',
    58  slug varchar(200) NOT NULL default '',
    52  term_group bigint(10) NOT NULL default 0,
    59  term_group bigint(10) NOT NULL default 0,
    53  PRIMARY KEY  (term_id),
    60  PRIMARY KEY  (term_id),
    54  UNIQUE KEY slug (slug),
    61  KEY slug (slug($max_index_length)),
    55  KEY name (name)
    62  KEY name (name($max_index_length))
    56 ) $charset_collate;
    63 ) $charset_collate;
    57 CREATE TABLE $wpdb->term_taxonomy (
    64 CREATE TABLE $wpdb->term_taxonomy (
    58  term_taxonomy_id bigint(20) unsigned NOT NULL auto_increment,
    65  term_taxonomy_id bigint(20) unsigned NOT NULL auto_increment,
    59  term_id bigint(20) unsigned NOT NULL default 0,
    66  term_id bigint(20) unsigned NOT NULL default 0,
    60  taxonomy varchar(32) NOT NULL default '',
    67  taxonomy varchar(32) NOT NULL default '',
    77   comment_id bigint(20) unsigned NOT NULL default '0',
    84   comment_id bigint(20) unsigned NOT NULL default '0',
    78   meta_key varchar(255) default NULL,
    85   meta_key varchar(255) default NULL,
    79   meta_value longtext,
    86   meta_value longtext,
    80   PRIMARY KEY  (meta_id),
    87   PRIMARY KEY  (meta_id),
    81   KEY comment_id (comment_id),
    88   KEY comment_id (comment_id),
    82   KEY meta_key (meta_key)
    89   KEY meta_key (meta_key($max_index_length))
    83 ) $charset_collate;
    90 ) $charset_collate;
    84 CREATE TABLE $wpdb->comments (
    91 CREATE TABLE $wpdb->comments (
    85   comment_ID bigint(20) unsigned NOT NULL auto_increment,
    92   comment_ID bigint(20) unsigned NOT NULL auto_increment,
    86   comment_post_ID bigint(20) unsigned NOT NULL default '0',
    93   comment_post_ID bigint(20) unsigned NOT NULL default '0',
    87   comment_author tinytext NOT NULL,
    94   comment_author tinytext NOT NULL,
    99   user_id bigint(20) unsigned NOT NULL default '0',
   106   user_id bigint(20) unsigned NOT NULL default '0',
   100   PRIMARY KEY  (comment_ID),
   107   PRIMARY KEY  (comment_ID),
   101   KEY comment_post_ID (comment_post_ID),
   108   KEY comment_post_ID (comment_post_ID),
   102   KEY comment_approved_date_gmt (comment_approved,comment_date_gmt),
   109   KEY comment_approved_date_gmt (comment_approved,comment_date_gmt),
   103   KEY comment_date_gmt (comment_date_gmt),
   110   KEY comment_date_gmt (comment_date_gmt),
   104   KEY comment_parent (comment_parent)
   111   KEY comment_parent (comment_parent),
       
   112   KEY comment_author_email (comment_author_email(10))
   105 ) $charset_collate;
   113 ) $charset_collate;
   106 CREATE TABLE $wpdb->links (
   114 CREATE TABLE $wpdb->links (
   107   link_id bigint(20) unsigned NOT NULL auto_increment,
   115   link_id bigint(20) unsigned NOT NULL auto_increment,
   108   link_url varchar(255) NOT NULL default '',
   116   link_url varchar(255) NOT NULL default '',
   109   link_name varchar(255) NOT NULL default '',
   117   link_name varchar(255) NOT NULL default '',
   133   post_id bigint(20) unsigned NOT NULL default '0',
   141   post_id bigint(20) unsigned NOT NULL default '0',
   134   meta_key varchar(255) default NULL,
   142   meta_key varchar(255) default NULL,
   135   meta_value longtext,
   143   meta_value longtext,
   136   PRIMARY KEY  (meta_id),
   144   PRIMARY KEY  (meta_id),
   137   KEY post_id (post_id),
   145   KEY post_id (post_id),
   138   KEY meta_key (meta_key)
   146   KEY meta_key (meta_key($max_index_length))
   139 ) $charset_collate;
   147 ) $charset_collate;
   140 CREATE TABLE $wpdb->posts (
   148 CREATE TABLE $wpdb->posts (
   141   ID bigint(20) unsigned NOT NULL auto_increment,
   149   ID bigint(20) unsigned NOT NULL auto_increment,
   142   post_author bigint(20) unsigned NOT NULL default '0',
   150   post_author bigint(20) unsigned NOT NULL default '0',
   143   post_date datetime NOT NULL default '0000-00-00 00:00:00',
   151   post_date datetime NOT NULL default '0000-00-00 00:00:00',
   160   menu_order int(11) NOT NULL default '0',
   168   menu_order int(11) NOT NULL default '0',
   161   post_type varchar(20) NOT NULL default 'post',
   169   post_type varchar(20) NOT NULL default 'post',
   162   post_mime_type varchar(100) NOT NULL default '',
   170   post_mime_type varchar(100) NOT NULL default '',
   163   comment_count bigint(20) NOT NULL default '0',
   171   comment_count bigint(20) NOT NULL default '0',
   164   PRIMARY KEY  (ID),
   172   PRIMARY KEY  (ID),
   165   KEY post_name (post_name),
   173   KEY post_name (post_name($max_index_length)),
   166   KEY type_status_date (post_type,post_status,post_date,ID),
   174   KEY type_status_date (post_type,post_status,post_date,ID),
   167   KEY post_parent (post_parent),
   175   KEY post_parent (post_parent),
   168   KEY post_author (post_author)
   176   KEY post_author (post_author)
   169 ) $charset_collate;\n";
   177 ) $charset_collate;\n";
   170 
   178 
   202   PRIMARY KEY  (ID),
   210   PRIMARY KEY  (ID),
   203   KEY user_login_key (user_login),
   211   KEY user_login_key (user_login),
   204   KEY user_nicename (user_nicename)
   212   KEY user_nicename (user_nicename)
   205 ) $charset_collate;\n";
   213 ) $charset_collate;\n";
   206 
   214 
   207 	// usermeta
   215 	// Usermeta.
   208 	$usermeta_table = "CREATE TABLE $wpdb->usermeta (
   216 	$usermeta_table = "CREATE TABLE $wpdb->usermeta (
   209   umeta_id bigint(20) unsigned NOT NULL auto_increment,
   217   umeta_id bigint(20) unsigned NOT NULL auto_increment,
   210   user_id bigint(20) unsigned NOT NULL default '0',
   218   user_id bigint(20) unsigned NOT NULL default '0',
   211   meta_key varchar(255) default NULL,
   219   meta_key varchar(255) default NULL,
   212   meta_value longtext,
   220   meta_value longtext,
   213   PRIMARY KEY  (umeta_id),
   221   PRIMARY KEY  (umeta_id),
   214   KEY user_id (user_id),
   222   KEY user_id (user_id),
   215   KEY meta_key (meta_key)
   223   KEY meta_key (meta_key($max_index_length))
   216 ) $charset_collate;\n";
   224 ) $charset_collate;\n";
   217 
   225 
   218 	// Global tables
   226 	// Global tables
   219 	if ( $is_multisite )
   227 	if ( $is_multisite )
   220 		$global_tables = $users_multi_table . $usermeta_table;
   228 		$global_tables = $users_multi_table . $usermeta_table;
   258 CREATE TABLE $wpdb->site (
   266 CREATE TABLE $wpdb->site (
   259   id bigint(20) NOT NULL auto_increment,
   267   id bigint(20) NOT NULL auto_increment,
   260   domain varchar(200) NOT NULL default '',
   268   domain varchar(200) NOT NULL default '',
   261   path varchar(100) NOT NULL default '',
   269   path varchar(100) NOT NULL default '',
   262   PRIMARY KEY  (id),
   270   PRIMARY KEY  (id),
   263   KEY domain (domain,path)
   271   KEY domain (domain(140),path(51))
   264 ) $charset_collate;
   272 ) $charset_collate;
   265 CREATE TABLE $wpdb->sitemeta (
   273 CREATE TABLE $wpdb->sitemeta (
   266   meta_id bigint(20) NOT NULL auto_increment,
   274   meta_id bigint(20) NOT NULL auto_increment,
   267   site_id bigint(20) NOT NULL default '0',
   275   site_id bigint(20) NOT NULL default '0',
   268   meta_key varchar(255) default NULL,
   276   meta_key varchar(255) default NULL,
   269   meta_value longtext,
   277   meta_value longtext,
   270   PRIMARY KEY  (meta_id),
   278   PRIMARY KEY  (meta_id),
   271   KEY meta_key (meta_key),
   279   KEY meta_key (meta_key($max_index_length)),
   272   KEY site_id (site_id)
   280   KEY site_id (site_id)
   273 ) $charset_collate;
   281 ) $charset_collate;
   274 CREATE TABLE $wpdb->signups (
   282 CREATE TABLE $wpdb->signups (
   275   signup_id bigint(20) NOT NULL auto_increment,
   283   signup_id bigint(20) NOT NULL auto_increment,
   276   domain varchar(200) NOT NULL default '',
   284   domain varchar(200) NOT NULL default '',
   285   meta longtext,
   293   meta longtext,
   286   PRIMARY KEY  (signup_id),
   294   PRIMARY KEY  (signup_id),
   287   KEY activation_key (activation_key),
   295   KEY activation_key (activation_key),
   288   KEY user_email (user_email),
   296   KEY user_email (user_email),
   289   KEY user_login_email (user_login,user_email),
   297   KEY user_login_email (user_login,user_email),
   290   KEY domain_path (domain,path)
   298   KEY domain_path (domain(140),path(51))
   291 ) $charset_collate;";
   299 ) $charset_collate;";
   292 
   300 
   293 	switch ( $scope ) {
   301 	switch ( $scope ) {
   294 		case 'blog' :
   302 		case 'blog' :
   295 			$queries = $blog_tables;
   303 			$queries = $blog_tables;
   300 				$queries .= $ms_global_tables;
   308 				$queries .= $ms_global_tables;
   301 			break;
   309 			break;
   302 		case 'ms_global' :
   310 		case 'ms_global' :
   303 			$queries = $ms_global_tables;
   311 			$queries = $ms_global_tables;
   304 			break;
   312 			break;
       
   313 		case 'all' :
   305 		default:
   314 		default:
   306 		case 'all' :
       
   307 			$queries = $global_tables . $blog_tables;
   315 			$queries = $global_tables . $blog_tables;
   308 			if ( $is_multisite )
   316 			if ( $is_multisite )
   309 				$queries .= $ms_global_tables;
   317 				$queries .= $ms_global_tables;
   310 			break;
   318 			break;
   311 	}
   319 	}
   321 
   329 
   322 /**
   330 /**
   323  * Create WordPress options and set the default values.
   331  * Create WordPress options and set the default values.
   324  *
   332  *
   325  * @since 1.5.0
   333  * @since 1.5.0
   326  * @uses $wpdb
   334  *
       
   335  * @global wpdb $wpdb WordPress database abstraction object.
   327  * @uses $wp_db_version
   336  * @uses $wp_db_version
   328  */
   337  */
   329 function populate_options() {
   338 function populate_options() {
   330 	global $wpdb, $wp_db_version, $current_site, $wp_current_db_version;
   339 	global $wpdb, $wp_db_version, $wp_current_db_version;
   331 
   340 
   332 	$guessurl = wp_guess_url();
   341 	$guessurl = wp_guess_url();
   333 
   342 	/**
   334 	do_action('populate_options');
   343 	 * Fires before creating WordPress options and populating their default values.
       
   344 	 *
       
   345 	 * @since 2.6.0
       
   346 	 */
       
   347 	do_action( 'populate_options' );
   335 
   348 
   336 	if ( ini_get('safe_mode') ) {
   349 	if ( ini_get('safe_mode') ) {
   337 		// Safe mode can break mkdir() so use a flat structure by default.
   350 		// Safe mode can break mkdir() so use a flat structure by default.
   338 		$uploads_use_yearmonth_folders = 0;
   351 		$uploads_use_yearmonth_folders = 0;
   339 	} else {
   352 	} else {
   358 	elseif ( $offset_or_tz && in_array( $offset_or_tz, timezone_identifiers_list() ) )
   371 	elseif ( $offset_or_tz && in_array( $offset_or_tz, timezone_identifiers_list() ) )
   359 			$timezone_string = $offset_or_tz;
   372 			$timezone_string = $offset_or_tz;
   360 
   373 
   361 	$options = array(
   374 	$options = array(
   362 	'siteurl' => $guessurl,
   375 	'siteurl' => $guessurl,
       
   376 	'home' => $guessurl,
   363 	'blogname' => __('My Site'),
   377 	'blogname' => __('My Site'),
   364 	/* translators: blog tagline */
   378 	/* translators: blog tagline */
   365 	'blogdescription' => __('Just another WordPress site'),
   379 	'blogdescription' => __('Just another WordPress site'),
   366 	'users_can_register' => 0,
   380 	'users_can_register' => 0,
   367 	'admin_email' => 'you@example.com',
   381 	'admin_email' => 'you@example.com',
   386 	'date_format' => __('F j, Y'),
   400 	'date_format' => __('F j, Y'),
   387 	/* translators: default time format, see http://php.net/date */
   401 	/* translators: default time format, see http://php.net/date */
   388 	'time_format' => __('g:i a'),
   402 	'time_format' => __('g:i a'),
   389 	/* translators: links last updated date format, see http://php.net/date */
   403 	/* translators: links last updated date format, see http://php.net/date */
   390 	'links_updated_date_format' => __('F j, Y g:i a'),
   404 	'links_updated_date_format' => __('F j, Y g:i a'),
   391 	'links_recently_updated_prepend' => '<em>',
       
   392 	'links_recently_updated_append' => '</em>',
       
   393 	'links_recently_updated_time' => 120,
       
   394 	'comment_moderation' => 0,
   405 	'comment_moderation' => 0,
   395 	'moderation_notify' => 1,
   406 	'moderation_notify' => 1,
   396 	'permalink_structure' => '',
   407 	'permalink_structure' => '',
   397 	'gzipcompression' => 0,
   408 	'gzipcompression' => 0,
   398 	'hack_file' => 0,
   409 	'hack_file' => 0,
   399 	'blog_charset' => 'UTF-8',
   410 	'blog_charset' => 'UTF-8',
   400 	'moderation_keys' => '',
   411 	'moderation_keys' => '',
   401 	'active_plugins' => array(),
   412 	'active_plugins' => array(),
   402 	'home' => $guessurl,
       
   403 	'category_base' => '',
   413 	'category_base' => '',
   404 	'ping_sites' => 'http://rpc.pingomatic.com/',
   414 	'ping_sites' => 'http://rpc.pingomatic.com/',
   405 	'advanced_edit' => 0,
   415 	'advanced_edit' => 0,
   406 	'comment_max_links' => 2,
   416 	'comment_max_links' => 2,
   407 	'gmt_offset' => $gmt_offset,
   417 	'gmt_offset' => $gmt_offset,
   489 	}
   499 	}
   490 
   500 
   491 	// 3.0 multisite
   501 	// 3.0 multisite
   492 	if ( is_multisite() ) {
   502 	if ( is_multisite() ) {
   493 		/* translators: blog tagline */
   503 		/* translators: blog tagline */
   494 		$options[ 'blogdescription' ] = sprintf(__('Just another %s site'), $current_site->site_name );
   504 		$options[ 'blogdescription' ] = sprintf(__('Just another %s site'), get_current_site()->site_name );
   495 		$options[ 'permalink_structure' ] = '/%year%/%monthnum%/%day%/%postname%/';
   505 		$options[ 'permalink_structure' ] = '/%year%/%monthnum%/%day%/%postname%/';
   496 	}
   506 	}
   497 
   507 
   498 	// Set autoload to no for these options
   508 	// Set autoload to no for these options
   499 	$fat_options = array( 'moderation_keys', 'recently_edited', 'blacklist_keys', 'uninstall_plugins' );
   509 	$fat_options = array( 'moderation_keys', 'recently_edited', 'blacklist_keys', 'uninstall_plugins' );
   518 	}
   528 	}
   519 
   529 
   520 	if ( !empty($insert) )
   530 	if ( !empty($insert) )
   521 		$wpdb->query("INSERT INTO $wpdb->options (option_name, option_value, autoload) VALUES " . $insert);
   531 		$wpdb->query("INSERT INTO $wpdb->options (option_name, option_value, autoload) VALUES " . $insert);
   522 
   532 
   523 	// in case it is set, but blank, update "home"
   533 	// In case it is set, but blank, update "home".
   524 	if ( !__get_option('home') ) update_option('home', $guessurl);
   534 	if ( !__get_option('home') ) update_option('home', $guessurl);
   525 
   535 
   526 	// Delete unused options
   536 	// Delete unused options.
   527 	$unusedoptions = array(
   537 	$unusedoptions = array(
   528 		'blodotgsping_url', 'bodyterminator', 'emailtestonly', 'phoneemail_separator', 'smilies_directory',
   538 		'blodotgsping_url', 'bodyterminator', 'emailtestonly', 'phoneemail_separator', 'smilies_directory',
   529 		'subjectprefix', 'use_bbcode', 'use_blodotgsping', 'use_phoneemail', 'use_quicktags', 'use_weblogsping',
   539 		'subjectprefix', 'use_bbcode', 'use_blodotgsping', 'use_phoneemail', 'use_quicktags', 'use_weblogsping',
   530 		'weblogs_cache_file', 'use_preview', 'use_htmltrans', 'smilies_directory', 'fileupload_allowedusers',
   540 		'weblogs_cache_file', 'use_preview', 'use_htmltrans', 'smilies_directory', 'fileupload_allowedusers',
   531 		'use_phoneemail', 'default_post_status', 'default_post_category', 'archive_mode', 'time_difference',
   541 		'use_phoneemail', 'default_post_status', 'default_post_category', 'archive_mode', 'time_difference',
   532 		'links_minadminlevel', 'links_use_adminlevels', 'links_rating_type', 'links_rating_char',
   542 		'links_minadminlevel', 'links_use_adminlevels', 'links_rating_type', 'links_rating_char',
   533 		'links_rating_ignore_zero', 'links_rating_single_image', 'links_rating_image0', 'links_rating_image1',
   543 		'links_rating_ignore_zero', 'links_rating_single_image', 'links_rating_image0', 'links_rating_image1',
   534 		'links_rating_image2', 'links_rating_image3', 'links_rating_image4', 'links_rating_image5',
   544 		'links_rating_image2', 'links_rating_image3', 'links_rating_image4', 'links_rating_image5',
   535 		'links_rating_image6', 'links_rating_image7', 'links_rating_image8', 'links_rating_image9',
   545 		'links_rating_image6', 'links_rating_image7', 'links_rating_image8', 'links_rating_image9',
       
   546 		'links_recently_updated_time', 'links_recently_updated_prepend', 'links_recently_updated_append',
   536 		'weblogs_cacheminutes', 'comment_allowed_tags', 'search_engine_friendly_urls', 'default_geourl_lat',
   547 		'weblogs_cacheminutes', 'comment_allowed_tags', 'search_engine_friendly_urls', 'default_geourl_lat',
   537 		'default_geourl_lon', 'use_default_geourl', 'weblogs_xml_url', 'new_users_can_blog', '_wpnonce',
   548 		'default_geourl_lon', 'use_default_geourl', 'weblogs_xml_url', 'new_users_can_blog', '_wpnonce',
   538 		'_wp_http_referer', 'Update', 'action', 'rich_editing', 'autosave_interval', 'deactivated_plugins',
   549 		'_wp_http_referer', 'Update', 'action', 'rich_editing', 'autosave_interval', 'deactivated_plugins',
   539 		'can_compress_scripts', 'page_uris', 'update_core', 'update_plugins', 'update_themes', 'doing_cron',
   550 		'can_compress_scripts', 'page_uris', 'update_core', 'update_plugins', 'update_themes', 'doing_cron',
   540 		'random_seed', 'rss_excerpt_length', 'secret', 'use_linksupdate', 'default_comment_status_page',
   551 		'random_seed', 'rss_excerpt_length', 'secret', 'use_linksupdate', 'default_comment_status_page',
   542 		'embed_autourls', 'default_post_edit_rows',
   553 		'embed_autourls', 'default_post_edit_rows',
   543 	);
   554 	);
   544 	foreach ( $unusedoptions as $option )
   555 	foreach ( $unusedoptions as $option )
   545 		delete_option($option);
   556 		delete_option($option);
   546 
   557 
   547 	// delete obsolete magpie stuff
   558 	// Delete obsolete magpie stuff.
   548 	$wpdb->query("DELETE FROM $wpdb->options WHERE option_name REGEXP '^rss_[0-9a-f]{32}(_ts)?$'");
   559 	$wpdb->query("DELETE FROM $wpdb->options WHERE option_name REGEXP '^rss_[0-9a-f]{32}(_ts)?$'");
   549 
   560 
   550 	// Deletes all expired transients.
   561 	/*
   551 	// The multi-table delete syntax is used to delete the transient record from table a,
   562 	 * Deletes all expired transients. The multi-table delete syntax is used
   552 	// and the corresponding transient_timeout record from table b.
   563 	 * to delete the transient record from table a, and the corresponding
       
   564 	 * transient_timeout record from table b.
       
   565 	 */
   553 	$time = time();
   566 	$time = time();
   554 	$wpdb->query("DELETE a, b FROM $wpdb->options a, $wpdb->options b WHERE
   567 	$sql = "DELETE a, b FROM $wpdb->options a, $wpdb->options b
   555 	        a.option_name LIKE '\_transient\_%' AND
   568 		WHERE a.option_name LIKE %s
   556 	        a.option_name NOT LIKE '\_transient\_timeout\_%' AND
   569 		AND a.option_name NOT LIKE %s
   557 	        b.option_name = CONCAT( '_transient_timeout_', SUBSTRING( a.option_name, 12 ) )
   570 		AND b.option_name = CONCAT( '_transient_timeout_', SUBSTRING( a.option_name, 12 ) )
   558 	        AND b.option_value < $time");
   571 		AND b.option_value < %d";
       
   572 	$wpdb->query( $wpdb->prepare( $sql, $wpdb->esc_like( '_transient_' ) . '%', $wpdb->esc_like( '_transient_timeout_' ) . '%', $time ) );
   559 
   573 
   560 	if ( is_main_site() && is_main_network() ) {
   574 	if ( is_main_site() && is_main_network() ) {
   561 		$wpdb->query("DELETE a, b FROM $wpdb->options a, $wpdb->options b WHERE
   575 		$sql = "DELETE a, b FROM $wpdb->options a, $wpdb->options b
   562 			a.option_name LIKE '\_site\_transient\_%' AND
   576 			WHERE a.option_name LIKE %s
   563 			a.option_name NOT LIKE '\_site\_transient\_timeout\_%' AND
   577 			AND a.option_name NOT LIKE %s
   564 			b.option_name = CONCAT( '_site_transient_timeout_', SUBSTRING( a.option_name, 17 ) )
   578 			AND b.option_name = CONCAT( '_site_transient_timeout_', SUBSTRING( a.option_name, 17 ) )
   565 			AND b.option_value < $time");
   579 			AND b.option_value < %d";
   566     }
   580 		$wpdb->query( $wpdb->prepare( $sql, $wpdb->esc_like( '_site_transient_' ) . '%', $wpdb->esc_like( '_site_transient_timeout_' ) . '%', $time ) );
       
   581 	}
   567 }
   582 }
   568 
   583 
   569 /**
   584 /**
   570  * Execute WordPress role creation for the various WordPress versions.
   585  * Execute WordPress role creation for the various WordPress versions.
   571  *
   586  *
   812 	if ( !empty( $role ) ) {
   827 	if ( !empty( $role ) ) {
   813 		$role->add_cap( 'update_core' );
   828 		$role->add_cap( 'update_core' );
   814 		$role->add_cap( 'list_users' );
   829 		$role->add_cap( 'list_users' );
   815 		$role->add_cap( 'remove_users' );
   830 		$role->add_cap( 'remove_users' );
   816 
   831 
   817 		// Never used, will be removed. create_users or
   832 		/*
   818 		// promote_users is the capability you're looking for.
   833 		 * Never used, will be removed. create_users or promote_users
       
   834 		 * is the capability you're looking for.
       
   835 		 */
   819 		$role->add_cap( 'add_users' );
   836 		$role->add_cap( 'add_users' );
   820 
   837 
   821 		$role->add_cap( 'promote_users' );
   838 		$role->add_cap( 'promote_users' );
   822 		$role->add_cap( 'edit_theme_options' );
   839 		$role->add_cap( 'edit_theme_options' );
   823 		$role->add_cap( 'delete_themes' );
   840 		$role->add_cap( 'delete_themes' );
   839 	dbDelta( wp_get_db_schema( 'global' ) );
   856 	dbDelta( wp_get_db_schema( 'global' ) );
   840 }
   857 }
   841 endif;
   858 endif;
   842 
   859 
   843 /**
   860 /**
   844  * populate network settings
   861  * Populate network settings.
   845  *
   862  *
   846  * @since 3.0.0
   863  * @since 3.0.0
   847  *
   864  *
   848  * @param int $network_id id of network to populate
   865  * @param int $network_id ID of network to populate.
   849  * @return bool|WP_Error True on success, or WP_Error on warning (with the install otherwise successful,
   866  * @return bool|WP_Error True on success, or WP_Error on warning (with the install otherwise successful,
   850  * 	so the error code must be checked) or failure.
   867  *                       so the error code must be checked) or failure.
   851  */
   868  */
   852 function populate_network( $network_id = 1, $domain = '', $email = '', $site_name = '', $path = '/', $subdomain_install = false ) {
   869 function populate_network( $network_id = 1, $domain = '', $email = '', $site_name = '', $path = '/', $subdomain_install = false ) {
   853 	global $wpdb, $current_site, $wp_db_version, $wp_rewrite;
   870 	global $wpdb, $current_site, $wp_db_version, $wp_rewrite;
   854 
   871 
   855 	$errors = new WP_Error();
   872 	$errors = new WP_Error();
   856 	if ( '' == $domain )
   873 	if ( '' == $domain )
   857 		$errors->add( 'empty_domain', __( 'You must provide a domain name.' ) );
   874 		$errors->add( 'empty_domain', __( 'You must provide a domain name.' ) );
   858 	if ( '' == $site_name )
   875 	if ( '' == $site_name )
   859 		$errors->add( 'empty_sitename', __( 'You must provide a name for your network of sites.' ) );
   876 		$errors->add( 'empty_sitename', __( 'You must provide a name for your network of sites.' ) );
   860 
   877 
   861 	// check for network collision
   878 	// Check for network collision.
   862 	if ( $network_id == $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $wpdb->site WHERE id = %d", $network_id ) ) )
   879 	if ( $network_id == $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $wpdb->site WHERE id = %d", $network_id ) ) )
   863 		$errors->add( 'siteid_exists', __( 'The network already exists.' ) );
   880 		$errors->add( 'siteid_exists', __( 'The network already exists.' ) );
   864 
   881 
   865 	$site_user = get_user_by( 'email', $email );
   882 	$site_user = get_user_by( 'email', $email );
   866 	if ( ! is_email( $email ) )
   883 	if ( ! is_email( $email ) )
   867 		$errors->add( 'invalid_email', __( 'You must provide a valid e-mail address.' ) );
   884 		$errors->add( 'invalid_email', __( 'You must provide a valid e-mail address.' ) );
   868 
   885 
   869 	if ( $errors->get_error_code() )
   886 	if ( $errors->get_error_code() )
   870 		return $errors;
   887 		return $errors;
   871 
   888 
   872 	// set up site tables
   889 	// Set up site tables.
   873 	$template = get_option( 'template' );
   890 	$template = get_option( 'template' );
   874 	$stylesheet = get_option( 'stylesheet' );
   891 	$stylesheet = get_option( 'stylesheet' );
   875 	$allowed_themes = array( $stylesheet => true );
   892 	$allowed_themes = array( $stylesheet => true );
   876 	if ( $template != $stylesheet )
   893 	if ( $template != $stylesheet )
   877 		$allowed_themes[ $template ] = true;
   894 		$allowed_themes[ $template ] = true;
   882 		$wpdb->insert( $wpdb->site, array( 'domain' => $domain, 'path' => $path ) );
   899 		$wpdb->insert( $wpdb->site, array( 'domain' => $domain, 'path' => $path ) );
   883 		$network_id = $wpdb->insert_id;
   900 		$network_id = $wpdb->insert_id;
   884 	} else {
   901 	} else {
   885 		$wpdb->insert( $wpdb->site, array( 'domain' => $domain, 'path' => $path, 'id' => $network_id ) );
   902 		$wpdb->insert( $wpdb->site, array( 'domain' => $domain, 'path' => $path, 'id' => $network_id ) );
   886 	}
   903 	}
       
   904 
       
   905 	wp_cache_delete( 'networks_have_paths', 'site-options' );
   887 
   906 
   888 	if ( !is_multisite() ) {
   907 	if ( !is_multisite() ) {
   889 		$site_admins = array( $site_user->user_login );
   908 		$site_admins = array( $site_user->user_login );
   890 		$users = get_users( array( 'fields' => array( 'ID', 'user_login' ) ) );
   909 		$users = get_users( array( 'fields' => array( 'ID', 'user_login' ) ) );
   891 		if ( $users ) {
   910 		if ( $users ) {
   896 		}
   915 		}
   897 	} else {
   916 	} else {
   898 		$site_admins = get_site_option( 'site_admins' );
   917 		$site_admins = get_site_option( 'site_admins' );
   899 	}
   918 	}
   900 
   919 
   901 	$welcome_email = __( 'Dear User,
   920 	$welcome_email = __( 'Howdy USERNAME,
   902 
   921 
   903 Your new SITE_NAME site has been successfully set up at:
   922 Your new SITE_NAME site has been successfully set up at:
   904 BLOG_URL
   923 BLOG_URL
   905 
   924 
   906 You can log in to the administrator account with the following information:
   925 You can log in to the administrator account with the following information:
       
   926 
   907 Username: USERNAME
   927 Username: USERNAME
   908 Password: PASSWORD
   928 Password: PASSWORD
   909 Log in here: BLOG_URLwp-login.php
   929 Log in here: BLOG_URLwp-login.php
   910 
   930 
   911 We hope you enjoy your new site. Thanks!
   931 We hope you enjoy your new site. Thanks!
   912 
   932 
   913 --The Team @ SITE_NAME' );
   933 --The Team @ SITE_NAME' );
       
   934 
       
   935 	$misc_exts = array(
       
   936 		// Images.
       
   937 		'jpg', 'jpeg', 'png', 'gif',
       
   938 		// Video.
       
   939 		'mov', 'avi', 'mpg', '3gp', '3g2',
       
   940 		// "audio".
       
   941 		'midi', 'mid',
       
   942 		// Miscellaneous.
       
   943 		'pdf', 'doc', 'ppt', 'odt', 'pptx', 'docx', 'pps', 'ppsx', 'xls', 'xlsx', 'key',
       
   944 	);
       
   945 	$audio_exts = wp_get_audio_extensions();
       
   946 	$video_exts = wp_get_video_extensions();
       
   947 	$upload_filetypes = array_unique( array_merge( $misc_exts, $audio_exts, $video_exts ) );
   914 
   948 
   915 	$sitemeta = array(
   949 	$sitemeta = array(
   916 		'site_name' => $site_name,
   950 		'site_name' => $site_name,
   917 		'admin_email' => $site_user->user_email,
   951 		'admin_email' => $site_user->user_email,
   918 		'admin_user_id' => $site_user->ID,
   952 		'admin_user_id' => $site_user->ID,
   919 		'registration' => 'none',
   953 		'registration' => 'none',
   920 		'upload_filetypes' => 'jpg jpeg png gif mp3 mov avi wmv midi mid pdf',
   954 		'upload_filetypes' => implode( ' ', $upload_filetypes ),
   921 		'blog_upload_space' => 100,
   955 		'blog_upload_space' => 100,
   922 		'fileupload_maxk' => 1500,
   956 		'fileupload_maxk' => 1500,
   923 		'site_admins' => $site_admins,
   957 		'site_admins' => $site_admins,
   924 		'allowedthemes' => $allowed_themes,
   958 		'allowedthemes' => $allowed_themes,
   925 		'illegal_names' => array( 'www', 'web', 'root', 'admin', 'main', 'invite', 'administrator', 'files' ),
   959 		'illegal_names' => array( 'www', 'web', 'root', 'admin', 'main', 'invite', 'administrator', 'files' ),
   943 	/**
   977 	/**
   944 	 * Filter meta for a network on creation.
   978 	 * Filter meta for a network on creation.
   945 	 *
   979 	 *
   946 	 * @since 3.7.0
   980 	 * @since 3.7.0
   947 	 *
   981 	 *
   948 	 * @param array $sitemeta Associative of meta keys and values to be inserted.
   982 	 * @param array $sitemeta   Associative array of network meta keys and values to be inserted.
   949 	 * @param int $network_id Network ID being created.
   983 	 * @param int   $network_id ID of network to populate.
   950 	 */
   984 	 */
   951 	$sitemeta = apply_filters( 'populate_network_meta', $sitemeta, $network_id );
   985 	$sitemeta = apply_filters( 'populate_network_meta', $sitemeta, $network_id );
   952 
   986 
   953 	$insert = '';
   987 	$insert = '';
   954 	foreach ( $sitemeta as $meta_key => $meta_value ) {
   988 	foreach ( $sitemeta as $meta_key => $meta_value ) {
   958 			$insert .= ', ';
   992 			$insert .= ', ';
   959 		$insert .= $wpdb->prepare( "( %d, %s, %s)", $network_id, $meta_key, $meta_value );
   993 		$insert .= $wpdb->prepare( "( %d, %s, %s)", $network_id, $meta_key, $meta_value );
   960 	}
   994 	}
   961 	$wpdb->query( "INSERT INTO $wpdb->sitemeta ( site_id, meta_key, meta_value ) VALUES " . $insert );
   995 	$wpdb->query( "INSERT INTO $wpdb->sitemeta ( site_id, meta_key, meta_value ) VALUES " . $insert );
   962 
   996 
   963 	// When upgrading from single to multisite, assume the current site will become the main site of the network.
   997 	/*
   964 	// When using populate_network() to create another network in an existing multisite environment,
   998 	 * When upgrading from single to multisite, assume the current site will
   965 	// skip these steps since the main site of the new network has not yet been created.
   999 	 * become the main site of the network. When using populate_network()
       
  1000 	 * to create another network in an existing multisite environment, skip
       
  1001 	 * these steps since the main site of the new network has not yet been
       
  1002 	 * created.
       
  1003 	 */
   966 	if ( ! is_multisite() ) {
  1004 	if ( ! is_multisite() ) {
   967 		$current_site = new stdClass;
  1005 		$current_site = new stdClass;
   968 		$current_site->domain = $domain;
  1006 		$current_site->domain = $domain;
   969 		$current_site->path = $path;
  1007 		$current_site->path = $path;
   970 		$current_site->site_name = ucfirst( $domain );
  1008 		$current_site->site_name = ucfirst( $domain );