]> scripts.mit.edu Git - autoinstalls/wordpress.git/blobdiff - wp-admin/includes/upgrade.php
Wordpress 4.5.3
[autoinstalls/wordpress.git] / wp-admin / includes / upgrade.php
index 9803f88b42dc26540cb8d439191dc283d08c73a5..3ddbc8b17d76012ccc62382e74025d71776d5da6 100644 (file)
@@ -27,10 +27,10 @@ if ( !function_exists('wp_install') ) :
  *
  * @since 2.1.0
  *
  *
  * @since 2.1.0
  *
- * @param string $blog_title    Blog title.
+ * @param string $blog_title    Site title.
  * @param string $user_name     User's username.
  * @param string $user_email    User's email.
  * @param string $user_name     User's username.
  * @param string $user_email    User's email.
- * @param bool   $public        Whether blog is public.
+ * @param bool   $public        Whether site is public.
  * @param string $deprecated    Optional. Not used.
  * @param string $user_password Optional. User's chosen password. Default empty (random password).
  * @param string $language      Optional. Language chosen. Default empty.
  * @param string $deprecated    Optional. Not used.
  * @param string $user_password Optional. User's chosen password. Default empty (random password).
  * @param string $language      Optional. Language chosen. Default empty.
@@ -64,7 +64,7 @@ function wp_install( $blog_title, $user_name, $user_email, $public, $deprecated
 
        /*
         * Create default user. If the user already exists, the user tables are
 
        /*
         * Create default user. If the user already exists, the user tables are
-        * being shared among blogs. Just set the role in that case.
+        * being shared among sites. Just set the role in that case.
         */
        $user_id = username_exists($user_name);
        $user_password = trim($user_password);
         */
        $user_id = username_exists($user_name);
        $user_password = trim($user_password);
@@ -118,6 +118,10 @@ if ( !function_exists('wp_install_defaults') ) :
  *
  * @since 2.1.0
  *
  *
  * @since 2.1.0
  *
+ * @global wpdb       $wpdb
+ * @global WP_Rewrite $wp_rewrite
+ * @global string     $table_prefix
+ *
  * @param int $user_id User ID.
  */
 function wp_install_defaults( $user_id ) {
  * @param int $user_id User ID.
  */
 function wp_install_defaults( $user_id ) {
@@ -151,32 +155,39 @@ function wp_install_defaults( $user_id ) {
        if ( is_multisite() ) {
                $first_post = get_site_option( 'first_post' );
 
        if ( is_multisite() ) {
                $first_post = get_site_option( 'first_post' );
 
-               if ( empty($first_post) )
-                       $first_post = __( 'Welcome to <a href="SITE_URL">SITE_NAME</a>. This is your first post. Edit or delete it, then start blogging!' );
+               if ( ! $first_post ) {
+                       /* translators: %s: site link */
+                       $first_post = __( 'Welcome to %s. This is your first post. Edit or delete it, then start blogging!' );
+               }
+
+               $first_post = sprintf( $first_post,
+                       sprintf( '<a href="%s">%s</a>', esc_url( network_home_url() ), get_current_site()->site_name )
+               );
 
 
-               $first_post = str_replace( "SITE_URL", esc_url( network_home_url() ), $first_post );
-               $first_post = str_replace( "SITE_NAME", get_current_site()->site_name, $first_post );
+               // Back-compat for pre-4.4
+               $first_post = str_replace( 'SITE_URL', esc_url( network_home_url() ), $first_post );
+               $first_post = str_replace( 'SITE_NAME', get_current_site()->site_name, $first_post );
        } else {
        } else {
-               $first_post = __('Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!');
+               $first_post = __( 'Welcome to WordPress. This is your first post. Edit or delete it, then start writing!' );
        }
 
        $wpdb->insert( $wpdb->posts, array(
        }
 
        $wpdb->insert( $wpdb->posts, array(
-                                                               'post_author' => $user_id,
-                                                               'post_date' => $now,
-                                                               'post_date_gmt' => $now_gmt,
-                                                               'post_content' => $first_post,
-                                                               'post_excerpt' => '',
-                                                               'post_title' => __('Hello world!'),
-                                                               /* translators: Default post slug */
-                                                               'post_name' => sanitize_title( _x('hello-world', 'Default post slug') ),
-                                                               'post_modified' => $now,
-                                                               'post_modified_gmt' => $now_gmt,
-                                                               'guid' => $first_post_guid,
-                                                               'comment_count' => 1,
-                                                               'to_ping' => '',
-                                                               'pinged' => '',
-                                                               'post_content_filtered' => ''
-                                                               ));
+               'post_author' => $user_id,
+               'post_date' => $now,
+               'post_date_gmt' => $now_gmt,
+               'post_content' => $first_post,
+               'post_excerpt' => '',
+               'post_title' => __('Hello world!'),
+               /* translators: Default post slug */
+               'post_name' => sanitize_title( _x('hello-world', 'Default post slug') ),
+               'post_modified' => $now,
+               'post_modified_gmt' => $now_gmt,
+               'guid' => $first_post_guid,
+               'comment_count' => 1,
+               'to_ping' => '',
+               'pinged' => '',
+               'post_content_filtered' => ''
+       ));
        $wpdb->insert( $wpdb->term_relationships, array('term_taxonomy_id' => $cat_tt_id, 'object_id' => 1) );
 
        // Default comment
        $wpdb->insert( $wpdb->term_relationships, array('term_taxonomy_id' => $cat_tt_id, 'object_id' => 1) );
 
        // Default comment
@@ -190,19 +201,19 @@ To delete a comment, just log in and view the post&#039;s comments. There you wi
                $first_comment = get_site_option( 'first_comment', $first_comment );
        }
        $wpdb->insert( $wpdb->comments, array(
                $first_comment = get_site_option( 'first_comment', $first_comment );
        }
        $wpdb->insert( $wpdb->comments, array(
-                                                               'comment_post_ID' => 1,
-                                                               'comment_author' => $first_comment_author,
-                                                               'comment_author_email' => '',
-                                                               'comment_author_url' => $first_comment_url,
-                                                               'comment_date' => $now,
-                                                               'comment_date_gmt' => $now_gmt,
-                                                               'comment_content' => $first_comment
-                                                               ));
+               'comment_post_ID' => 1,
+               'comment_author' => $first_comment_author,
+               'comment_author_email' => '',
+               'comment_author_url' => $first_comment_url,
+               'comment_date' => $now,
+               'comment_date_gmt' => $now_gmt,
+               'comment_content' => $first_comment
+       ));
 
        // First Page
        $first_page = sprintf( __( "This is an example page. It's different from a blog post because it will stay in one place and will show up in your site navigation (in most themes). Most people start with an About page that introduces them to potential site visitors. It might say something like this:
 
 
        // First Page
        $first_page = sprintf( __( "This is an example page. It's different from a blog post because it will stay in one place and will show up in your site navigation (in most themes). Most people start with an About page that introduces them to potential site visitors. It might say something like this:
 
-<blockquote>Hi there! I'm a bike messenger by day, aspiring actor by night, and this is my blog. I live in Los Angeles, have a great dog named Jack, and I like pi&#241;a coladas. (And gettin' caught in the rain.)</blockquote>
+<blockquote>Hi there! I'm a bike messenger by day, aspiring actor by night, and this is my website. I live in Los Angeles, have a great dog named Jack, and I like pi&#241;a coladas. (And gettin' caught in the rain.)</blockquote>
 
 ...or something like this:
 
 
 ...or something like this:
 
@@ -213,22 +224,23 @@ As a new WordPress user, you should go to <a href=\"%s\">your dashboard</a> to d
                $first_page = get_site_option( 'first_page', $first_page );
        $first_post_guid = get_option('home') . '/?page_id=2';
        $wpdb->insert( $wpdb->posts, array(
                $first_page = get_site_option( 'first_page', $first_page );
        $first_post_guid = get_option('home') . '/?page_id=2';
        $wpdb->insert( $wpdb->posts, array(
-                                                               'post_author' => $user_id,
-                                                               'post_date' => $now,
-                                                               'post_date_gmt' => $now_gmt,
-                                                               'post_content' => $first_page,
-                                                               'post_excerpt' => '',
-                                                               'post_title' => __( 'Sample Page' ),
-                                                               /* translators: Default page slug */
-                                                               'post_name' => __( 'sample-page' ),
-                                                               'post_modified' => $now,
-                                                               'post_modified_gmt' => $now_gmt,
-                                                               'guid' => $first_post_guid,
-                                                               'post_type' => 'page',
-                                                               'to_ping' => '',
-                                                               'pinged' => '',
-                                                               'post_content_filtered' => ''
-                                                               ));
+               'post_author' => $user_id,
+               'post_date' => $now,
+               'post_date_gmt' => $now_gmt,
+               'post_content' => $first_page,
+               'post_excerpt' => '',
+               'comment_status' => 'closed',
+               'post_title' => __( 'Sample Page' ),
+               /* translators: Default page slug */
+               'post_name' => __( 'sample-page' ),
+               'post_modified' => $now,
+               'post_modified_gmt' => $now_gmt,
+               'guid' => $first_post_guid,
+               'post_type' => 'page',
+               'to_ping' => '',
+               'pinged' => '',
+               'post_content_filtered' => ''
+       ));
        $wpdb->insert( $wpdb->postmeta, array( 'post_id' => 2, 'meta_key' => '_wp_page_template', 'meta_value' => 'default' ) );
 
        // Set up default widgets for default theme.
        $wpdb->insert( $wpdb->postmeta, array( 'post_id' => 2, 'meta_key' => '_wp_page_template', 'meta_value' => 'default' ) );
 
        // Set up default widgets for default theme.
@@ -347,8 +359,8 @@ if ( !function_exists('wp_new_blog_notification') ) :
  *
  * @since 2.1.0
  *
  *
  * @since 2.1.0
  *
- * @param string $blog_title Blog title.
- * @param string $blog_url   Blog url.
+ * @param string $blog_title Site title.
+ * @param string $blog_url   Site url.
  * @param int    $user_id    User ID.
  * @param string $password   User's Password.
  */
  * @param int    $user_id    User ID.
  * @param string $password   User's Password.
  */
@@ -385,7 +397,9 @@ if ( !function_exists('wp_upgrade') ) :
  *
  * @since 2.1.0
  *
  *
  * @since 2.1.0
  *
- * @return null If no update is necessary or site isn't completely installed, null.
+ * @global int  $wp_current_db_version
+ * @global int  $wp_db_version
+ * @global wpdb $wpdb WordPress database abstraction object.
  */
 function wp_upgrade() {
        global $wp_current_db_version, $wp_db_version, $wpdb;
  */
 function wp_upgrade() {
        global $wp_current_db_version, $wp_db_version, $wpdb;
@@ -433,9 +447,11 @@ endif;
  * Contains conditional checks to determine which upgrade scripts to run,
  * based on database version and WP version being updated-to.
  *
  * Contains conditional checks to determine which upgrade scripts to run,
  * based on database version and WP version being updated-to.
  *
+ * @ignore
  * @since 1.0.1
  *
  * @since 1.0.1
  *
- * @return null If no update is necessary, null.
+ * @global int $wp_current_db_version
+ * @global int $wp_db_version
  */
 function upgrade_all() {
        global $wp_current_db_version, $wp_db_version;
  */
 function upgrade_all() {
        global $wp_current_db_version, $wp_db_version;
@@ -524,11 +540,17 @@ function upgrade_all() {
        if ( $wp_current_db_version < 29630 )
                upgrade_400();
 
        if ( $wp_current_db_version < 29630 )
                upgrade_400();
 
-       if ( $wp_current_db_version < 31351 )
-               upgrade_420();
+       if ( $wp_current_db_version < 33055 )
+               upgrade_430();
+
+       if ( $wp_current_db_version < 33056 )
+               upgrade_431();
 
 
-       if ( $wp_current_db_version < 31533 )
-               upgrade_421();
+       if ( $wp_current_db_version < 35700 )
+               upgrade_440();
+
+       if ( $wp_current_db_version < 36686 )
+               upgrade_450();
 
        maybe_disable_link_manager();
 
 
        maybe_disable_link_manager();
 
@@ -541,7 +563,10 @@ function upgrade_all() {
 /**
  * Execute changes made in WordPress 1.0.
  *
 /**
  * Execute changes made in WordPress 1.0.
  *
+ * @ignore
  * @since 1.0.0
  * @since 1.0.0
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
  */
 function upgrade_100() {
        global $wpdb;
  */
 function upgrade_100() {
        global $wpdb;
@@ -549,7 +574,7 @@ function upgrade_100() {
        // Get the title and ID of every post, post_name to check if it already has a value
        $posts = $wpdb->get_results("SELECT ID, post_title, post_name FROM $wpdb->posts WHERE post_name = ''");
        if ($posts) {
        // Get the title and ID of every post, post_name to check if it already has a value
        $posts = $wpdb->get_results("SELECT ID, post_title, post_name FROM $wpdb->posts WHERE post_name = ''");
        if ($posts) {
-               foreach($posts as $post) {
+               foreach ($posts as $post) {
                        if ('' == $post->post_name) {
                                $newtitle = sanitize_title($post->post_title);
                                $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_name = %s WHERE ID = %d", $newtitle, $post->ID) );
                        if ('' == $post->post_name) {
                                $newtitle = sanitize_title($post->post_title);
                                $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_name = %s WHERE ID = %d", $newtitle, $post->ID) );
@@ -597,7 +622,10 @@ function upgrade_100() {
 /**
  * Execute changes made in WordPress 1.0.1.
  *
 /**
  * Execute changes made in WordPress 1.0.1.
  *
+ * @ignore
  * @since 1.0.1
  * @since 1.0.1
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
  */
 function upgrade_101() {
        global $wpdb;
  */
 function upgrade_101() {
        global $wpdb;
@@ -615,7 +643,10 @@ function upgrade_101() {
 /**
  * Execute changes made in WordPress 1.2.
  *
 /**
  * Execute changes made in WordPress 1.2.
  *
+ * @ignore
  * @since 1.2.0
  * @since 1.2.0
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
  */
 function upgrade_110() {
        global $wpdb;
  */
 function upgrade_110() {
        global $wpdb;
@@ -675,7 +706,10 @@ function upgrade_110() {
 /**
  * Execute changes made in WordPress 1.5.
  *
 /**
  * Execute changes made in WordPress 1.5.
  *
+ * @ignore
  * @since 1.5.0
  * @since 1.5.0
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
  */
 function upgrade_130() {
        global $wpdb;
  */
 function upgrade_130() {
        global $wpdb;
@@ -683,7 +717,7 @@ function upgrade_130() {
        // Remove extraneous backslashes.
        $posts = $wpdb->get_results("SELECT ID, post_title, post_content, post_excerpt, guid, post_date, post_name, post_status, post_author FROM $wpdb->posts");
        if ($posts) {
        // Remove extraneous backslashes.
        $posts = $wpdb->get_results("SELECT ID, post_title, post_content, post_excerpt, guid, post_date, post_name, post_status, post_author FROM $wpdb->posts");
        if ($posts) {
-               foreach($posts as $post) {
+               foreach ($posts as $post) {
                        $post_content = addslashes(deslash($post->post_content));
                        $post_title = addslashes(deslash($post->post_title));
                        $post_excerpt = addslashes(deslash($post->post_excerpt));
                        $post_content = addslashes(deslash($post->post_content));
                        $post_title = addslashes(deslash($post->post_title));
                        $post_excerpt = addslashes(deslash($post->post_excerpt));
@@ -700,7 +734,7 @@ function upgrade_130() {
        // Remove extraneous backslashes.
        $comments = $wpdb->get_results("SELECT comment_ID, comment_author, comment_content FROM $wpdb->comments");
        if ($comments) {
        // Remove extraneous backslashes.
        $comments = $wpdb->get_results("SELECT comment_ID, comment_author, comment_content FROM $wpdb->comments");
        if ($comments) {
-               foreach($comments as $comment) {
+               foreach ($comments as $comment) {
                        $comment_content = deslash($comment->comment_content);
                        $comment_author = deslash($comment->comment_author);
 
                        $comment_content = deslash($comment->comment_content);
                        $comment_author = deslash($comment->comment_author);
 
@@ -711,7 +745,7 @@ function upgrade_130() {
        // Remove extraneous backslashes.
        $links = $wpdb->get_results("SELECT link_id, link_name, link_description FROM $wpdb->links");
        if ($links) {
        // Remove extraneous backslashes.
        $links = $wpdb->get_results("SELECT link_id, link_name, link_description FROM $wpdb->links");
        if ($links) {
-               foreach($links as $link) {
+               foreach ($links as $link) {
                        $link_name = deslash($link->link_name);
                        $link_description = deslash($link->link_description);
 
                        $link_name = deslash($link->link_name);
                        $link_description = deslash($link->link_description);
 
@@ -759,7 +793,11 @@ function upgrade_130() {
 /**
  * Execute changes made in WordPress 2.0.
  *
 /**
  * Execute changes made in WordPress 2.0.
  *
+ * @ignore
  * @since 2.0.0
  * @since 2.0.0
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
+ * @global int  $wp_current_db_version
  */
 function upgrade_160() {
        global $wpdb, $wp_current_db_version;
  */
 function upgrade_160() {
        global $wpdb, $wp_current_db_version;
@@ -842,7 +880,11 @@ function upgrade_160() {
 /**
  * Execute changes made in WordPress 2.1.
  *
 /**
  * Execute changes made in WordPress 2.1.
  *
+ * @ignore
  * @since 2.1.0
  * @since 2.1.0
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
+ * @global int  $wp_current_db_version
  */
 function upgrade_210() {
        global $wpdb, $wp_current_db_version;
  */
 function upgrade_210() {
        global $wpdb, $wp_current_db_version;
@@ -886,7 +928,11 @@ function upgrade_210() {
 /**
  * Execute changes made in WordPress 2.3.
  *
 /**
  * Execute changes made in WordPress 2.3.
  *
+ * @ignore
  * @since 2.3.0
  * @since 2.3.0
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
+ * @global int  $wp_current_db_version
  */
 function upgrade_230() {
        global $wp_current_db_version, $wpdb;
  */
 function upgrade_230() {
        global $wp_current_db_version, $wpdb;
@@ -1062,7 +1108,10 @@ function upgrade_230() {
 /**
  * Remove old options from the database.
  *
 /**
  * Remove old options from the database.
  *
+ * @ignore
  * @since 2.3.0
  * @since 2.3.0
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
  */
 function upgrade_230_options_table() {
        global $wpdb;
  */
 function upgrade_230_options_table() {
        global $wpdb;
@@ -1076,7 +1125,10 @@ function upgrade_230_options_table() {
 /**
  * Remove old categories, link2cat, and post2cat database tables.
  *
 /**
  * Remove old categories, link2cat, and post2cat database tables.
  *
+ * @ignore
  * @since 2.3.0
  * @since 2.3.0
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
  */
 function upgrade_230_old_tables() {
        global $wpdb;
  */
 function upgrade_230_old_tables() {
        global $wpdb;
@@ -1088,7 +1140,10 @@ function upgrade_230_old_tables() {
 /**
  * Upgrade old slugs made in version 2.2.
  *
 /**
  * Upgrade old slugs made in version 2.2.
  *
+ * @ignore
  * @since 2.2.0
  * @since 2.2.0
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
  */
 function upgrade_old_slugs() {
        // Upgrade people who were using the Redirect Old Slugs plugin.
  */
 function upgrade_old_slugs() {
        // Upgrade people who were using the Redirect Old Slugs plugin.
@@ -1099,7 +1154,10 @@ function upgrade_old_slugs() {
 /**
  * Execute changes made in WordPress 2.5.0.
  *
 /**
  * Execute changes made in WordPress 2.5.0.
  *
+ * @ignore
  * @since 2.5.0
  * @since 2.5.0
+ *
+ * @global int $wp_current_db_version
  */
 function upgrade_250() {
        global $wp_current_db_version;
  */
 function upgrade_250() {
        global $wp_current_db_version;
@@ -1113,7 +1171,10 @@ function upgrade_250() {
 /**
  * Execute changes made in WordPress 2.5.2.
  *
 /**
  * Execute changes made in WordPress 2.5.2.
  *
+ * @ignore
  * @since 2.5.2
  * @since 2.5.2
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
  */
 function upgrade_252() {
        global $wpdb;
  */
 function upgrade_252() {
        global $wpdb;
@@ -1124,7 +1185,10 @@ function upgrade_252() {
 /**
  * Execute changes made in WordPress 2.6.
  *
 /**
  * Execute changes made in WordPress 2.6.
  *
+ * @ignore
  * @since 2.6.0
  * @since 2.6.0
+ *
+ * @global int $wp_current_db_version
  */
 function upgrade_260() {
        global $wp_current_db_version;
  */
 function upgrade_260() {
        global $wp_current_db_version;
@@ -1136,7 +1200,11 @@ function upgrade_260() {
 /**
  * Execute changes made in WordPress 2.7.
  *
 /**
  * Execute changes made in WordPress 2.7.
  *
+ * @ignore
  * @since 2.7.0
  * @since 2.7.0
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
+ * @global int  $wp_current_db_version
  */
 function upgrade_270() {
        global $wpdb, $wp_current_db_version;
  */
 function upgrade_270() {
        global $wpdb, $wp_current_db_version;
@@ -1152,7 +1220,11 @@ function upgrade_270() {
 /**
  * Execute changes made in WordPress 2.8.
  *
 /**
  * Execute changes made in WordPress 2.8.
  *
+ * @ignore
  * @since 2.8.0
  * @since 2.8.0
+ *
+ * @global int  $wp_current_db_version
+ * @global wpdb $wpdb WordPress database abstraction object.
  */
 function upgrade_280() {
        global $wp_current_db_version, $wpdb;
  */
 function upgrade_280() {
        global $wp_current_db_version, $wpdb;
@@ -1162,7 +1234,7 @@ function upgrade_280() {
        if ( is_multisite() ) {
                $start = 0;
                while( $rows = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options ORDER BY option_id LIMIT $start, 20" ) ) {
        if ( is_multisite() ) {
                $start = 0;
                while( $rows = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options ORDER BY option_id LIMIT $start, 20" ) ) {
-                       foreach( $rows as $row ) {
+                       foreach ( $rows as $row ) {
                                $value = $row->option_value;
                                if ( !@unserialize( $value ) )
                                        $value = stripslashes( $value );
                                $value = $row->option_value;
                                if ( !@unserialize( $value ) )
                                        $value = stripslashes( $value );
@@ -1179,7 +1251,10 @@ function upgrade_280() {
 /**
  * Execute changes made in WordPress 2.9.
  *
 /**
  * Execute changes made in WordPress 2.9.
  *
+ * @ignore
  * @since 2.9.0
  * @since 2.9.0
+ *
+ * @global int $wp_current_db_version
  */
 function upgrade_290() {
        global $wp_current_db_version;
  */
 function upgrade_290() {
        global $wp_current_db_version;
@@ -1196,7 +1271,11 @@ function upgrade_290() {
 /**
  * Execute changes made in WordPress 3.0.
  *
 /**
  * Execute changes made in WordPress 3.0.
  *
+ * @ignore
  * @since 3.0.0
  * @since 3.0.0
+ *
+ * @global int  $wp_current_db_version
+ * @global wpdb $wpdb WordPress database abstraction object.
  */
 function upgrade_300() {
        global $wp_current_db_version, $wpdb;
  */
 function upgrade_300() {
        global $wp_current_db_version, $wpdb;
@@ -1208,7 +1287,7 @@ function upgrade_300() {
                add_site_option( 'siteurl', '' );
 
        // 3.0 screen options key name changes.
                add_site_option( 'siteurl', '' );
 
        // 3.0 screen options key name changes.
-       if ( is_main_site() && !defined('DO_NOT_UPGRADE_GLOBAL_TABLES') ) {
+       if ( wp_should_upgrade_global_tables() ) {
                $sql = "DELETE FROM $wpdb->usermeta
                        WHERE meta_key LIKE %s
                        OR meta_key LIKE %s
                $sql = "DELETE FROM $wpdb->usermeta
                        WHERE meta_key LIKE %s
                        OR meta_key LIKE %s
@@ -1238,12 +1317,18 @@ function upgrade_300() {
 /**
  * Execute changes made in WordPress 3.3.
  *
 /**
  * Execute changes made in WordPress 3.3.
  *
+ * @ignore
  * @since 3.3.0
  * @since 3.3.0
+ *
+ * @global int   $wp_current_db_version
+ * @global wpdb  $wpdb
+ * @global array $wp_registered_widgets
+ * @global array $sidebars_widgets
  */
 function upgrade_330() {
        global $wp_current_db_version, $wpdb, $wp_registered_widgets, $sidebars_widgets;
 
  */
 function upgrade_330() {
        global $wp_current_db_version, $wpdb, $wp_registered_widgets, $sidebars_widgets;
 
-       if ( $wp_current_db_version < 19061 && is_main_site() && ! defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) ) {
+       if ( $wp_current_db_version < 19061 && wp_should_upgrade_global_tables() ) {
                $wpdb->query( "DELETE FROM $wpdb->usermeta WHERE meta_key IN ('show_admin_bar_admin', 'plugins_last_view')" );
        }
 
                $wpdb->query( "DELETE FROM $wpdb->usermeta WHERE meta_key IN ('show_admin_bar_admin', 'plugins_last_view')" );
        }
 
@@ -1307,7 +1392,11 @@ function upgrade_330() {
 /**
  * Execute changes made in WordPress 3.4.
  *
 /**
  * Execute changes made in WordPress 3.4.
  *
+ * @ignore
  * @since 3.4.0
  * @since 3.4.0
+ *
+ * @global int   $wp_current_db_version
+ * @global wpdb  $wpdb
  */
 function upgrade_340() {
        global $wp_current_db_version, $wpdb;
  */
 function upgrade_340() {
        global $wp_current_db_version, $wpdb;
@@ -1324,7 +1413,7 @@ function upgrade_340() {
                $wpdb->show_errors();
        }
 
                $wpdb->show_errors();
        }
 
-       if ( $wp_current_db_version < 20022 && is_main_site() && ! defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) ) {
+       if ( $wp_current_db_version < 20022 && wp_should_upgrade_global_tables() ) {
                $wpdb->query( "DELETE FROM $wpdb->usermeta WHERE meta_key = 'themes_last_view'" );
        }
 
                $wpdb->query( "DELETE FROM $wpdb->usermeta WHERE meta_key = 'themes_last_view'" );
        }
 
@@ -1340,7 +1429,11 @@ function upgrade_340() {
 /**
  * Execute changes made in WordPress 3.5.
  *
 /**
  * Execute changes made in WordPress 3.5.
  *
+ * @ignore
  * @since 3.5.0
  * @since 3.5.0
+ *
+ * @global int   $wp_current_db_version
+ * @global wpdb  $wpdb
  */
 function upgrade_350() {
        global $wp_current_db_version, $wpdb;
  */
 function upgrade_350() {
        global $wp_current_db_version, $wpdb;
@@ -1348,7 +1441,7 @@ function upgrade_350() {
        if ( $wp_current_db_version < 22006 && $wpdb->get_var( "SELECT link_id FROM $wpdb->links LIMIT 1" ) )
                update_option( 'link_manager_enabled', 1 ); // Previously set to 0 by populate_options()
 
        if ( $wp_current_db_version < 22006 && $wpdb->get_var( "SELECT link_id FROM $wpdb->links LIMIT 1" ) )
                update_option( 'link_manager_enabled', 1 ); // Previously set to 0 by populate_options()
 
-       if ( $wp_current_db_version < 21811 && is_main_site() && ! defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) ) {
+       if ( $wp_current_db_version < 21811 && wp_should_upgrade_global_tables() ) {
                $meta_keys = array();
                foreach ( array_merge( get_post_types(), get_taxonomies() ) as $name ) {
                        if ( false !== strpos( $name, '-' ) )
                $meta_keys = array();
                foreach ( array_merge( get_post_types(), get_taxonomies() ) as $name ) {
                        if ( false !== strpos( $name, '-' ) )
@@ -1367,7 +1460,10 @@ function upgrade_350() {
 /**
  * Execute changes made in WordPress 3.7.
  *
 /**
  * Execute changes made in WordPress 3.7.
  *
+ * @ignore
  * @since 3.7.0
  * @since 3.7.0
+ *
+ * @global int $wp_current_db_version
  */
 function upgrade_370() {
        global $wp_current_db_version;
  */
 function upgrade_370() {
        global $wp_current_db_version;
@@ -1378,8 +1474,11 @@ function upgrade_370() {
 /**
  * Execute changes made in WordPress 3.7.2.
  *
 /**
  * Execute changes made in WordPress 3.7.2.
  *
+ * @ignore
  * @since 3.7.2
  * @since 3.8.0
  * @since 3.7.2
  * @since 3.8.0
+ *
+ * @global int $wp_current_db_version
  */
 function upgrade_372() {
        global $wp_current_db_version;
  */
 function upgrade_372() {
        global $wp_current_db_version;
@@ -1390,7 +1489,10 @@ function upgrade_372() {
 /**
  * Execute changes made in WordPress 3.8.0.
  *
 /**
  * Execute changes made in WordPress 3.8.0.
  *
+ * @ignore
  * @since 3.8.0
  * @since 3.8.0
+ *
+ * @global int $wp_current_db_version
  */
 function upgrade_380() {
        global $wp_current_db_version;
  */
 function upgrade_380() {
        global $wp_current_db_version;
@@ -1402,7 +1504,10 @@ function upgrade_380() {
 /**
  * Execute changes made in WordPress 4.0.0.
  *
 /**
  * Execute changes made in WordPress 4.0.0.
  *
+ * @ignore
  * @since 4.0.0
  * @since 4.0.0
+ *
+ * @global int $wp_current_db_version
  */
 function upgrade_400() {
        global $wp_current_db_version;
  */
 function upgrade_400() {
        global $wp_current_db_version;
@@ -1420,16 +1525,45 @@ function upgrade_400() {
 /**
  * Execute changes made in WordPress 4.2.0.
  *
 /**
  * Execute changes made in WordPress 4.2.0.
  *
+ * @ignore
  * @since 4.2.0
  * @since 4.2.0
+ *
+ * @global int   $wp_current_db_version
+ * @global wpdb  $wpdb
  */
  */
-function upgrade_420() {
+function upgrade_420() {}
+
+/**
+ * Executes changes made in WordPress 4.3.0.
+ *
+ * @ignore
+ * @since 4.3.0
+ *
+ * @global int  $wp_current_db_version Current version.
+ * @global wpdb $wpdb                  WordPress database abstraction object.
+ */
+function upgrade_430() {
        global $wp_current_db_version, $wpdb;
 
        global $wp_current_db_version, $wpdb;
 
-       if ( $wp_current_db_version < 31351 && $wpdb->charset === 'utf8mb4' ) {
+       if ( $wp_current_db_version < 32364 ) {
+               upgrade_430_fix_comments();
+       }
+
+       // Shared terms are split in a separate process.
+       if ( $wp_current_db_version < 32814 ) {
+               update_option( 'finished_splitting_shared_terms', 0 );
+               wp_schedule_single_event( time() + ( 1 * MINUTE_IN_SECONDS ), 'wp_split_shared_term_batch' );
+       }
+
+       if ( $wp_current_db_version < 33055 && 'utf8mb4' === $wpdb->charset ) {
                if ( is_multisite() ) {
                        $tables = $wpdb->tables( 'blog' );
                } else {
                        $tables = $wpdb->tables( 'all' );
                if ( is_multisite() ) {
                        $tables = $wpdb->tables( 'blog' );
                } else {
                        $tables = $wpdb->tables( 'all' );
+                       if ( ! wp_should_upgrade_global_tables() ) {
+                               $global_tables = $wpdb->tables( 'global' );
+                               $tables = array_diff_assoc( $tables, $global_tables );
+                       }
                }
 
                foreach ( $tables as $table ) {
                }
 
                foreach ( $tables as $table ) {
@@ -1439,36 +1573,127 @@ function upgrade_420() {
 }
 
 /**
 }
 
 /**
- * Execute changes made in WordPress 4.2.1.
+ * Executes comments changes made in WordPress 4.3.0.
+ *
+ * @ignore
+ * @since 4.3.0
  *
  *
- * @since 4.2.1
+ * @global int  $wp_current_db_version Current version.
+ * @global wpdb $wpdb                  WordPress database abstraction object.
  */
  */
-function upgrade_421() {
+function upgrade_430_fix_comments() {
        global $wp_current_db_version, $wpdb;
 
        global $wp_current_db_version, $wpdb;
 
-       if ( $wp_current_db_version < 31533 ) {
-               $content_length = $wpdb->get_col_length( $wpdb->comments, 'comment_content' );
-               if ( ! $content_length ) {
-                       $content_length = 65535;
-               }
+       $content_length = $wpdb->get_col_length( $wpdb->comments, 'comment_content' );
+
+       if ( is_wp_error( $content_length ) ) {
+               return;
+       }
 
 
-               $comments = $wpdb->get_results(
-                       "SELECT comment_ID FROM $wpdb->comments
-                       WHERE comment_date_gmt > '2015-04-26'
-                       AND CHAR_LENGTH( comment_content ) >= $content_length
-                       AND ( comment_content LIKE '%<%' OR comment_content LIKE '%>%' )"
+       if ( false === $content_length ) {
+               $content_length = array(
+                       'type'   => 'byte',
+                       'length' => 65535,
+               );
+       } elseif ( ! is_array( $content_length ) ) {
+               $length = (int) $content_length > 0 ? (int) $content_length : 65535;
+               $content_length = array(
+                       'type'   => 'byte',
+                       'length' => $length
                );
                );
+       }
+
+       if ( 'byte' !== $content_length['type'] || 0 === $content_length['length'] ) {
+               // Sites with malformed DB schemas are on their own.
+               return;
+       }
+
+       $allowed_length = intval( $content_length['length'] ) - 10;
+
+       $comments = $wpdb->get_results(
+               "SELECT `comment_ID` FROM `{$wpdb->comments}`
+                       WHERE `comment_date_gmt` > '2015-04-26'
+                       AND LENGTH( `comment_content` ) >= {$allowed_length}
+                       AND ( `comment_content` LIKE '%<%' OR `comment_content` LIKE '%>%' )"
+       );
+
+       foreach ( $comments as $comment ) {
+               wp_delete_comment( $comment->comment_ID, true );
+       }
+}
+
+/**
+ * Executes changes made in WordPress 4.3.1.
+ *
+ * @ignore
+ * @since 4.3.1
+ */
+function upgrade_431() {
+       // Fix incorrect cron entries for term splitting
+       $cron_array = _get_cron_array();
+       if ( isset( $cron_array['wp_batch_split_terms'] ) ) {
+               unset( $cron_array['wp_batch_split_terms'] );
+               _set_cron_array( $cron_array );
+       }
+}
 
 
-               foreach ( $comments as $comment ) {
-                       wp_delete_comment( $comment->comment_ID, true );
+/**
+ * Executes changes made in WordPress 4.4.0.
+ *
+ * @ignore
+ * @since 4.4.0
+ *
+ * @global int  $wp_current_db_version Current version.
+ * @global wpdb $wpdb                  WordPress database abstraction object.
+ */
+function upgrade_440() {
+       global $wp_current_db_version, $wpdb;
+
+       if ( $wp_current_db_version < 34030 ) {
+               $wpdb->query( "ALTER TABLE {$wpdb->options} MODIFY option_name VARCHAR(191)" );
+       }
+
+       // Remove the unused 'add_users' role.
+       $roles = wp_roles();
+       foreach ( $roles->role_objects as $role ) {
+               if ( $role->has_cap( 'add_users' ) ) {
+                       $role->remove_cap( 'add_users' );
                }
        }
 }
 
                }
        }
 }
 
+/**
+ * Executes changes made in WordPress 4.5.0.
+ *
+ * @ignore
+ * @since 4.5.0
+ *
+ * @global int  $wp_current_db_version Current database version.
+ * @global wpdb $wpdb                  WordPress database abstraction object.
+ */
+function upgrade_450() {
+       global $wp_current_db_version, $wpdb;
+
+       if ( $wp_current_db_version < 36180 ) {
+               wp_clear_scheduled_hook( 'wp_maybe_auto_update' );
+       }
+
+       // Remove unused email confirmation options, moved to usermeta.
+       if ( $wp_current_db_version < 36679 && is_multisite() ) {
+               $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name REGEXP '^[0-9]+_new_email$'" );
+       }
+
+       // Remove unused user setting for wpLink.
+       delete_user_setting( 'wplink' );
+}
+
 /**
  * Executes network-level upgrade routines.
  *
  * @since 3.0.0
 /**
  * Executes network-level upgrade routines.
  *
  * @since 3.0.0
+ *
+ * @global int   $wp_current_db_version
+ * @global wpdb  $wpdb
  */
 function upgrade_network() {
        global $wp_current_db_version, $wpdb;
  */
 function upgrade_network() {
        global $wp_current_db_version, $wpdb;
@@ -1506,7 +1731,7 @@ function upgrade_network() {
 
                $start = 0;
                while( $rows = $wpdb->get_results( "SELECT meta_key, meta_value FROM {$wpdb->sitemeta} ORDER BY meta_id LIMIT $start, 20" ) ) {
 
                $start = 0;
                while( $rows = $wpdb->get_results( "SELECT meta_key, meta_value FROM {$wpdb->sitemeta} ORDER BY meta_id LIMIT $start, 20" ) ) {
-                       foreach( $rows as $row ) {
+                       foreach ( $rows as $row ) {
                                $value = $row->meta_value;
                                if ( !@unserialize( $value ) )
                                        $value = stripslashes( $value );
                                $value = $row->meta_value;
                                if ( !@unserialize( $value ) )
                                        $value = stripslashes( $value );
@@ -1564,14 +1789,48 @@ function upgrade_network() {
 
        // 4.2
        if ( $wp_current_db_version < 31351 && $wpdb->charset === 'utf8mb4' ) {
 
        // 4.2
        if ( $wp_current_db_version < 31351 && $wpdb->charset === 'utf8mb4' ) {
-               if ( ! ( defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) && DO_NOT_UPGRADE_GLOBAL_TABLES ) ) {
+               if ( wp_should_upgrade_global_tables() ) {
                        $wpdb->query( "ALTER TABLE $wpdb->usermeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" );
                        $wpdb->query( "ALTER TABLE $wpdb->site DROP INDEX domain, ADD INDEX domain(domain(140),path(51))" );
                        $wpdb->query( "ALTER TABLE $wpdb->sitemeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" );
                        $wpdb->query( "ALTER TABLE $wpdb->usermeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" );
                        $wpdb->query( "ALTER TABLE $wpdb->site DROP INDEX domain, ADD INDEX domain(domain(140),path(51))" );
                        $wpdb->query( "ALTER TABLE $wpdb->sitemeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" );
-                       $wpdb->query( "ALTER TABLE $wpdb->signups DROP INDEX domain, ADD INDEX domain(domain(140),path(51))" );
+                       $wpdb->query( "ALTER TABLE $wpdb->signups DROP INDEX domain_path, ADD INDEX domain_path(domain(140),path(51))" );
 
                        $tables = $wpdb->tables( 'global' );
 
 
                        $tables = $wpdb->tables( 'global' );
 
+                       // sitecategories may not exist.
+                       if ( ! $wpdb->get_var( "SHOW TABLES LIKE '{$tables['sitecategories']}'" ) ) {
+                               unset( $tables['sitecategories'] );
+                       }
+
+                       foreach ( $tables as $table ) {
+                               maybe_convert_table_to_utf8mb4( $table );
+                       }
+               }
+       }
+
+       // 4.3
+       if ( $wp_current_db_version < 33055 && 'utf8mb4' === $wpdb->charset ) {
+               if ( wp_should_upgrade_global_tables() ) {
+                       $upgrade = false;
+                       $indexes = $wpdb->get_results( "SHOW INDEXES FROM $wpdb->signups" );
+                       foreach ( $indexes as $index ) {
+                               if ( 'domain_path' == $index->Key_name && 'domain' == $index->Column_name && 140 != $index->Sub_part ) {
+                                       $upgrade = true;
+                                       break;
+                               }
+                       }
+
+                       if ( $upgrade ) {
+                               $wpdb->query( "ALTER TABLE $wpdb->signups DROP INDEX domain_path, ADD INDEX domain_path(domain(140),path(51))" );
+                       }
+
+                       $tables = $wpdb->tables( 'global' );
+
+                       // sitecategories may not exist.
+                       if ( ! $wpdb->get_var( "SHOW TABLES LIKE '{$tables['sitecategories']}'" ) ) {
+                               unset( $tables['sitecategories'] );
+                       }
+
                        foreach ( $tables as $table ) {
                                maybe_convert_table_to_utf8mb4( $table );
                        }
                        foreach ( $tables as $table ) {
                                maybe_convert_table_to_utf8mb4( $table );
                        }
@@ -1592,6 +1851,8 @@ function upgrade_network() {
  *
  * @since 1.0.0
  *
  *
  * @since 1.0.0
  *
+ * @global wpdb  $wpdb
+ *
  * @param string $table_name Database table name to create.
  * @param string $create_ddl SQL statement to create table.
  * @return bool If table already exists or was created by function.
  * @param string $table_name Database table name to create.
  * @param string $create_ddl SQL statement to create table.
  * @return bool If table already exists or was created by function.
@@ -1620,9 +1881,11 @@ function maybe_create_table($table_name, $create_ddl) {
  *
  * @since 1.0.1
  *
  *
  * @since 1.0.1
  *
+ * @global wpdb  $wpdb
+ *
  * @param string $table Database table name.
  * @param string $index Index name to drop.
  * @param string $table Database table name.
  * @param string $index Index name to drop.
- * @return bool True, when finished.
+ * @return true True, when finished.
  */
 function drop_index($table, $index) {
        global $wpdb;
  */
 function drop_index($table, $index) {
        global $wpdb;
@@ -1641,9 +1904,11 @@ function drop_index($table, $index) {
  *
  * @since 1.0.1
  *
  *
  * @since 1.0.1
  *
+ * @global wpdb  $wpdb
+ *
  * @param string $table Database table name.
  * @param string $index Database table index column.
  * @param string $table Database table name.
  * @param string $index Database table index column.
- * @return bool True, when done with execution.
+ * @return true True, when done with execution.
  */
 function add_clean_index($table, $index) {
        global $wpdb;
  */
 function add_clean_index($table, $index) {
        global $wpdb;
@@ -1657,10 +1922,12 @@ function add_clean_index($table, $index) {
  *
  * @since 1.3.0
  *
  *
  * @since 1.3.0
  *
+ * @global wpdb  $wpdb
+ *
  * @param string $table_name  The table name to modify.
  * @param string $column_name The column name to add to the table.
  * @param string $create_ddl  The SQL statement used to add the column.
  * @param string $table_name  The table name to modify.
  * @param string $column_name The column name to add to the table.
  * @param string $create_ddl  The SQL statement used to add the column.
- * @return True if already exists or on successful completion, false on error.
+ * @return bool True if already exists or on successful completion, false on error.
  */
 function maybe_add_column($table_name, $column_name, $create_ddl) {
        global $wpdb;
  */
 function maybe_add_column($table_name, $column_name, $create_ddl) {
        global $wpdb;
@@ -1687,6 +1954,8 @@ function maybe_add_column($table_name, $column_name, $create_ddl) {
  *
  * @since 4.2.0
  *
  *
  * @since 4.2.0
  *
+ * @global wpdb  $wpdb
+ *
  * @param string $table The table to convert.
  * @return bool true if the table was converted, false if it wasn't.
  */
  * @param string $table The table to convert.
  * @return bool true if the table was converted, false if it wasn't.
  */
@@ -1709,6 +1978,17 @@ function maybe_convert_table_to_utf8mb4( $table ) {
                }
        }
 
                }
        }
 
+       $table_details = $wpdb->get_row( "SHOW TABLE STATUS LIKE '$table'" );
+       if ( ! $table_details ) {
+               return false;
+       }
+
+       list( $table_charset ) = explode( '_', $table_details->Collation );
+       $table_charset = strtolower( $table_charset );
+       if ( 'utf8mb4' === $table_charset ) {
+               return true;
+       }
+
        return $wpdb->query( "ALTER TABLE $table CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci" );
 }
 
        return $wpdb->query( "ALTER TABLE $table CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci" );
 }
 
@@ -1717,6 +1997,8 @@ function maybe_convert_table_to_utf8mb4( $table ) {
  *
  * @since 1.2.0
  *
  *
  * @since 1.2.0
  *
+ * @global wpdb  $wpdb
+ *
  * @return stdClass List of options.
  */
 function get_alloptions_110() {
  * @return stdClass List of options.
  */
 function get_alloptions_110() {
@@ -1739,6 +2021,8 @@ function get_alloptions_110() {
  * @since 1.5.1
  * @access private
  *
  * @since 1.5.1
  * @access private
  *
+ * @global wpdb  $wpdb
+ *
  * @param string $setting Option name.
  * @return mixed
  */
  * @param string $setting Option name.
  * @return mixed
  */
@@ -1798,6 +2082,8 @@ function deslash($content) {
  *
  * @since 1.5.0
  *
  *
  * @since 1.5.0
  *
+ * @global wpdb  $wpdb
+ *
  * @param string|array $queries Optional. The query to run. Can be multiple queries
  *                              in an array, or a string of queries separated by
  *                              semicolons. Default empty.
  * @param string|array $queries Optional. The query to run. Can be multiple queries
  *                              in an array, or a string of queries separated by
  *                              semicolons. Default empty.
@@ -1831,7 +2117,7 @@ function dbDelta( $queries = '', $execute = true ) {
        $for_update = array();
 
        // Create a tablename index for an array ($cqueries) of queries
        $for_update = array();
 
        // Create a tablename index for an array ($cqueries) of queries
-       foreach($queries as $qry) {
+       foreach ($queries as $qry) {
                if ( preg_match( "|CREATE TABLE ([^ ]*)|", $qry, $matches ) ) {
                        $cqueries[ trim( $matches[1], '`' ) ] = $qry;
                        $for_update[$matches[1]] = 'Created table '.$matches[1];
                if ( preg_match( "|CREATE TABLE ([^ ]*)|", $qry, $matches ) ) {
                        $cqueries[ trim( $matches[1], '`' ) ] = $qry;
                        $for_update[$matches[1]] = 'Created table '.$matches[1];
@@ -1868,10 +2154,13 @@ function dbDelta( $queries = '', $execute = true ) {
         */
        $iqueries = apply_filters( 'dbdelta_insert_queries', $iqueries );
 
         */
        $iqueries = apply_filters( 'dbdelta_insert_queries', $iqueries );
 
+       $text_fields = array( 'tinytext', 'text', 'mediumtext', 'longtext' );
+       $blob_fields = array( 'tinyblob', 'blob', 'mediumblob', 'longblob' );
+
        $global_tables = $wpdb->tables( 'global' );
        foreach ( $cqueries as $table => $qry ) {
        $global_tables = $wpdb->tables( 'global' );
        foreach ( $cqueries as $table => $qry ) {
-               // Upgrade global tables only for the main site. Don't upgrade at all if DO_NOT_UPGRADE_GLOBAL_TABLES is defined.
-               if ( in_array( $table, $global_tables ) && ( !is_main_site() || defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) ) ) {
+               // Upgrade global tables only for the main site. Don't upgrade at all if conditions are not optimal.
+               if ( in_array( $table, $global_tables ) && ! wp_should_upgrade_global_tables() ) {
                        unset( $cqueries[ $table ], $for_update[ $table ] );
                        continue;
                }
                        unset( $cqueries[ $table ], $for_update[ $table ] );
                        continue;
                }
@@ -1937,9 +2226,24 @@ function dbDelta( $queries = '', $execute = true ) {
 
                                // Is actual field type different from the field type in query?
                                if ($tablefield->Type != $fieldtype) {
 
                                // Is actual field type different from the field type in query?
                                if ($tablefield->Type != $fieldtype) {
+                                       $do_change = true;
+                                       if ( in_array( strtolower( $fieldtype ), $text_fields ) && in_array( strtolower( $tablefield->Type ), $text_fields ) ) {
+                                               if ( array_search( strtolower( $fieldtype ), $text_fields ) < array_search( strtolower( $tablefield->Type ), $text_fields ) ) {
+                                                       $do_change = false;
+                                               }
+                                       }
+
+                                       if ( in_array( strtolower( $fieldtype ), $blob_fields ) && in_array( strtolower( $tablefield->Type ), $blob_fields ) ) {
+                                               if ( array_search( strtolower( $fieldtype ), $blob_fields ) < array_search( strtolower( $tablefield->Type ), $blob_fields ) ) {
+                                                       $do_change = false;
+                                               }
+                                       }
+
+                                       if ( $do_change ) {
                                        // Add a query to change the column type
                                        // Add a query to change the column type
-                                       $cqueries[] = "ALTER TABLE {$table} CHANGE COLUMN {$tablefield->Field} " . $cfields[strtolower($tablefield->Field)];
-                                       $for_update[$table.'.'.$tablefield->Field] = "Changed type of {$table}.{$tablefield->Field} from {$tablefield->Type} to {$fieldtype}";
+                                               $cqueries[] = "ALTER TABLE {$table} CHANGE COLUMN {$tablefield->Field} " . $cfields[strtolower($tablefield->Field)];
+                                               $for_update[$table.'.'.$tablefield->Field] = "Changed type of {$table}.{$tablefield->Field} from {$tablefield->Type} to {$fieldtype}";
+                                       }
                                }
 
                                // Get the default value from the array
                                }
 
                                // Get the default value from the array
@@ -1982,6 +2286,7 @@ function dbDelta( $queries = '', $execute = true ) {
                                $keyname = $tableindex->Key_name;
                                $index_ary[$keyname]['columns'][] = array('fieldname' => $tableindex->Column_name, 'subpart' => $tableindex->Sub_part);
                                $index_ary[$keyname]['unique'] = ($tableindex->Non_unique == 0)?true:false;
                                $keyname = $tableindex->Key_name;
                                $index_ary[$keyname]['columns'][] = array('fieldname' => $tableindex->Column_name, 'subpart' => $tableindex->Sub_part);
                                $index_ary[$keyname]['unique'] = ($tableindex->Non_unique == 0)?true:false;
+                               $index_ary[$keyname]['index_type'] = $tableindex->Index_type;
                        }
 
                        // For each actual index in the index array.
                        }
 
                        // For each actual index in the index array.
@@ -1994,6 +2299,9 @@ function dbDelta( $queries = '', $execute = true ) {
                                } elseif ( $index_data['unique'] ) {
                                        $index_string .= 'UNIQUE ';
                                }
                                } elseif ( $index_data['unique'] ) {
                                        $index_string .= 'UNIQUE ';
                                }
+                               if ( 'FULLTEXT' === strtoupper( $index_data['index_type'] ) ) {
+                                       $index_string .= 'FULLTEXT ';
+                               }
                                $index_string .= 'KEY ';
                                if ($index_name != 'PRIMARY') {
                                        $index_string .= $index_name;
                                $index_string .= 'KEY ';
                                if ($index_name != 'PRIMARY') {
                                        $index_string .= $index_name;
@@ -2020,7 +2328,7 @@ function dbDelta( $queries = '', $execute = true ) {
                                        "$index_string ($alt_index_columns)",
                                );
 
                                        "$index_string ($alt_index_columns)",
                                );
 
-                               foreach( $index_strings as $index_string ) {
+                               foreach ( $index_strings as $index_string ) {
                                        if ( ! ( ( $aindex = array_search( $index_string, $indices ) ) === false ) ) {
                                                unset( $indices[ $aindex ] );
                                                break;
                                        if ( ! ( ( $aindex = array_search( $index_string, $indices ) ) === false ) ) {
                                                unset( $indices[ $aindex ] );
                                                break;
@@ -2071,7 +2379,7 @@ function dbDelta( $queries = '', $execute = true ) {
 function make_db_current( $tables = 'all' ) {
        $alterations = dbDelta( $tables );
        echo "<ol>\n";
 function make_db_current( $tables = 'all' ) {
        $alterations = dbDelta( $tables );
        echo "<ol>\n";
-       foreach($alterations as $alteration) echo "<li>$alteration</li>\n";
+       foreach ($alterations as $alteration) echo "<li>$alteration</li>\n";
        echo "</ol>\n";
 }
 
        echo "</ol>\n";
 }
 
@@ -2130,7 +2438,7 @@ function make_site_theme_from_oldschool($theme_name, $template) {
 
                                // Don't copy anything.
                                continue;
 
                                // Don't copy anything.
                                continue;
-                               }
+                       }
                }
 
                if (! @copy("$oldpath/$oldfile", "$site_dir/$newfile"))
                }
 
                if (! @copy("$oldpath/$oldfile", "$site_dir/$newfile"))
@@ -2183,7 +2491,7 @@ function make_site_theme_from_oldschool($theme_name, $template) {
  *
  * @param string $theme_name The name of the theme.
  * @param string $template   The directory name of the theme.
  *
  * @param string $theme_name The name of the theme.
  * @param string $template   The directory name of the theme.
- * @return null|false
+ * @return false|void
  */
 function make_site_theme_from_default($theme_name, $template) {
        $site_dir = WP_CONTENT_DIR . "/themes/$template";
  */
 function make_site_theme_from_default($theme_name, $template) {
        $site_dir = WP_CONTENT_DIR . "/themes/$template";
@@ -2322,6 +2630,8 @@ function translate_level_to_role($level) {
  * Checks the version of the installed MySQL binary.
  *
  * @since 2.1.0
  * Checks the version of the installed MySQL binary.
  *
  * @since 2.1.0
+ *
+ * @global wpdb  $wpdb
  */
 function wp_check_mysql_version() {
        global $wpdb;
  */
 function wp_check_mysql_version() {
        global $wpdb;
@@ -2351,6 +2661,9 @@ function maybe_disable_automattic_widgets() {
  * Disables the Link Manager on upgrade if, at the time of upgrade, no links exist in the DB.
  *
  * @since 3.5.0
  * Disables the Link Manager on upgrade if, at the time of upgrade, no links exist in the DB.
  *
  * @since 3.5.0
+ *
+ * @global int  $wp_current_db_version
+ * @global wpdb $wpdb WordPress database abstraction object.
  */
 function maybe_disable_link_manager() {
        global $wp_current_db_version, $wpdb;
  */
 function maybe_disable_link_manager() {
        global $wp_current_db_version, $wpdb;
@@ -2363,6 +2676,9 @@ function maybe_disable_link_manager() {
  * Runs before the schema is upgraded.
  *
  * @since 2.9.0
  * Runs before the schema is upgraded.
  *
  * @since 2.9.0
+ *
+ * @global int  $wp_current_db_version
+ * @global wpdb $wpdb WordPress database abstraction object.
  */
 function pre_schema_upgrade() {
        global $wp_current_db_version, $wpdb;
  */
 function pre_schema_upgrade() {
        global $wp_current_db_version, $wpdb;
@@ -2380,7 +2696,7 @@ function pre_schema_upgrade() {
        }
 
        // Multisite schema upgrades.
        }
 
        // Multisite schema upgrades.
-       if ( $wp_current_db_version < 25448 && is_multisite() && ! defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) && is_main_network() ) {
+       if ( $wp_current_db_version < 25448 && is_multisite() && wp_should_upgrade_global_tables() ) {
 
                // Upgrade verions prior to 3.7
                if ( $wp_current_db_version < 25179 ) {
 
                // Upgrade verions prior to 3.7
                if ( $wp_current_db_version < 25179 ) {
@@ -2396,14 +2712,9 @@ function pre_schema_upgrade() {
                }
        }
 
                }
        }
 
-       if ( $wp_current_db_version < 30133 ) {
-               // dbDelta() can recreate but can't drop the index.
-               $wpdb->query( "ALTER TABLE $wpdb->terms DROP INDEX slug" );
-       }
-
        // Upgrade versions prior to 4.2.
        if ( $wp_current_db_version < 31351 ) {
        // Upgrade versions prior to 4.2.
        if ( $wp_current_db_version < 31351 ) {
-               if ( ! is_multisite() ) {
+               if ( ! is_multisite() && wp_should_upgrade_global_tables() ) {
                        $wpdb->query( "ALTER TABLE $wpdb->usermeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" );
                }
                $wpdb->query( "ALTER TABLE $wpdb->terms DROP INDEX slug, ADD INDEX slug(slug(191))" );
                        $wpdb->query( "ALTER TABLE $wpdb->usermeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" );
                }
                $wpdb->query( "ALTER TABLE $wpdb->terms DROP INDEX slug, ADD INDEX slug(slug(191))" );
@@ -2412,6 +2723,15 @@ function pre_schema_upgrade() {
                $wpdb->query( "ALTER TABLE $wpdb->postmeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" );
                $wpdb->query( "ALTER TABLE $wpdb->posts DROP INDEX post_name, ADD INDEX post_name(post_name(191))" );
        }
                $wpdb->query( "ALTER TABLE $wpdb->postmeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" );
                $wpdb->query( "ALTER TABLE $wpdb->posts DROP INDEX post_name, ADD INDEX post_name(post_name(191))" );
        }
+
+       // Upgrade versions prior to 4.4.
+       if ( $wp_current_db_version < 34978 ) {
+               // If compatible termmeta table is found, use it, but enforce a proper index and update collation.
+               if ( $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->termmeta}'" ) && $wpdb->get_results( "SHOW INDEX FROM {$wpdb->termmeta} WHERE Column_name = 'meta_key'" ) ) {
+                       $wpdb->query( "ALTER TABLE $wpdb->termmeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" );
+                       maybe_convert_table_to_utf8mb4( $wpdb->termmeta );
+               }
+       }
 }
 
 /**
 }
 
 /**
@@ -2419,6 +2739,8 @@ function pre_schema_upgrade() {
  *
  * @since 3.0.0
  *
  *
  * @since 3.0.0
  *
+ * @global wpdb   $wpdb
+ * @global string $charset_collate
  */
 if ( !function_exists( 'install_global_terms' ) ) :
 function install_global_terms() {
  */
 if ( !function_exists( 'install_global_terms' ) ) :
 function install_global_terms() {
@@ -2438,3 +2760,49 @@ CREATE TABLE $wpdb->sitecategories (
        dbDelta( $ms_queries );
 }
 endif;
        dbDelta( $ms_queries );
 }
 endif;
+
+/**
+ * Determine if global tables should be upgraded.
+ *
+ * This function performs a series of checks to ensure the environment allows
+ * for the safe upgrading of global WordPress database tables. It is necessary
+ * because global tables will commonly grow to millions of rows on large
+ * installations, and the ability to control their upgrade routines can be
+ * critical to the operation of large networks.
+ *
+ * In a future iteration, this function may use `wp_is_large_network()` to more-
+ * intelligently prevent global table upgrades. Until then, we make sure
+ * WordPress is on the main site of the main network, to avoid running queries
+ * more than once in multi-site or multi-network environments.
+ *
+ * @since 4.3.0
+ *
+ * @return bool Whether to run the upgrade routines on global tables.
+ */
+function wp_should_upgrade_global_tables() {
+
+       // Return false early if explicitly not upgrading
+       if ( defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) ) {
+               return false;
+       }
+
+       // Assume global tables should be upgraded
+       $should_upgrade = true;
+
+       // Set to false if not on main network (does not matter if not multi-network)
+       if ( ! is_main_network() ) {
+               $should_upgrade = false;
+       }
+
+       // Set to false if not on main site of current network (does not matter if not multi-site)
+       if ( ! is_main_site() ) {
+               $should_upgrade = false;
+       }
+
+       /**
+        * Filter if upgrade routines should be run on global tables.
+        *
+        * @param bool $should_upgrade Whether to run the upgrade routines on global tables.
+        */
+       return apply_filters( 'wp_should_upgrade_global_tables', $should_upgrade );
+}