wp/wp-admin/includes/upgrade.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
     8  * @subpackage Administration
     8  * @subpackage Administration
     9  */
     9  */
    10 
    10 
    11 /** Include user installation customization script. */
    11 /** Include user installation customization script. */
    12 if ( file_exists( WP_CONTENT_DIR . '/install.php' ) ) {
    12 if ( file_exists( WP_CONTENT_DIR . '/install.php' ) ) {
    13 	require( WP_CONTENT_DIR . '/install.php' );
    13 	require WP_CONTENT_DIR . '/install.php';
    14 }
    14 }
    15 
    15 
    16 /** WordPress Administration API */
    16 /** WordPress Administration API */
    17 require_once( ABSPATH . 'wp-admin/includes/admin.php' );
    17 require_once ABSPATH . 'wp-admin/includes/admin.php';
    18 
    18 
    19 /** WordPress Schema API */
    19 /** WordPress Schema API */
    20 require_once( ABSPATH . 'wp-admin/includes/schema.php' );
    20 require_once ABSPATH . 'wp-admin/includes/schema.php';
    21 
    21 
    22 if ( ! function_exists( 'wp_install' ) ) :
    22 if ( ! function_exists( 'wp_install' ) ) :
    23 	/**
    23 	/**
    24 	 * Installs the site.
    24 	 * Installs the site.
    25 	 *
    25 	 *
    33 	 * @param string $user_email    User's email.
    33 	 * @param string $user_email    User's email.
    34 	 * @param bool   $public        Whether site is public.
    34 	 * @param bool   $public        Whether site is public.
    35 	 * @param string $deprecated    Optional. Not used.
    35 	 * @param string $deprecated    Optional. Not used.
    36 	 * @param string $user_password Optional. User's chosen password. Default empty (random password).
    36 	 * @param string $user_password Optional. User's chosen password. Default empty (random password).
    37 	 * @param string $language      Optional. Language chosen. Default empty.
    37 	 * @param string $language      Optional. Language chosen. Default empty.
    38 	 * @return array Array keys 'url', 'user_id', 'password', and 'password_message'.
    38 	 * @return array {
       
    39 	 *     Data for the newly installed site.
       
    40 	 *
       
    41 	 *     @type string $url              The URL of the site.
       
    42 	 *     @type int    $user_id          The ID of the site owner.
       
    43 	 *     @type string $password         The password of the site owner, if their user account didn't already exist.
       
    44 	 *     @type string $password_message The explanatory message regarding the password.
       
    45 	 * }
    39 	 */
    46 	 */
    40 	function wp_install( $blog_title, $user_name, $user_email, $public, $deprecated = '', $user_password = '', $language = '' ) {
    47 	function wp_install( $blog_title, $user_name, $user_email, $public, $deprecated = '', $user_password = '', $language = '' ) {
    41 		if ( ! empty( $deprecated ) ) {
    48 		if ( ! empty( $deprecated ) ) {
    42 			_deprecated_argument( __FUNCTION__, '2.6.0' );
    49 			_deprecated_argument( __FUNCTION__, '2.6.0' );
    43 		}
    50 		}
    73 		 * being shared among sites. Just set the role in that case.
    80 		 * being shared among sites. Just set the role in that case.
    74 		 */
    81 		 */
    75 		$user_id        = username_exists( $user_name );
    82 		$user_id        = username_exists( $user_name );
    76 		$user_password  = trim( $user_password );
    83 		$user_password  = trim( $user_password );
    77 		$email_password = false;
    84 		$email_password = false;
       
    85 		$user_created   = false;
       
    86 
    78 		if ( ! $user_id && empty( $user_password ) ) {
    87 		if ( ! $user_id && empty( $user_password ) ) {
    79 			$user_password = wp_generate_password( 12, false );
    88 			$user_password = wp_generate_password( 12, false );
    80 			$message       = __( '<strong><em>Note that password</em></strong> carefully! It is a <em>random</em> password that was generated just for you.' );
    89 			$message       = __( '<strong><em>Note that password</em></strong> carefully! It is a <em>random</em> password that was generated just for you.' );
    81 			$user_id       = wp_create_user( $user_name, $user_password, $user_email );
    90 			$user_id       = wp_create_user( $user_name, $user_password, $user_email );
    82 			update_user_option( $user_id, 'default_password_nag', true, true );
    91 			update_user_option( $user_id, 'default_password_nag', true, true );
    83 			$email_password = true;
    92 			$email_password = true;
       
    93 			$user_created   = true;
    84 		} elseif ( ! $user_id ) {
    94 		} elseif ( ! $user_id ) {
    85 			// Password has been provided
    95 			// Password has been provided.
    86 			$message = '<em>' . __( 'Your chosen password.' ) . '</em>';
    96 			$message      = '<em>' . __( 'Your chosen password.' ) . '</em>';
    87 			$user_id = wp_create_user( $user_name, $user_password, $user_email );
    97 			$user_id      = wp_create_user( $user_name, $user_password, $user_email );
       
    98 			$user_created = true;
    88 		} else {
    99 		} else {
    89 			$message = __( 'User already exists. Password inherited.' );
   100 			$message = __( 'User already exists. Password inherited.' );
    90 		}
   101 		}
    91 
   102 
    92 		$user = new WP_User( $user_id );
   103 		$user = new WP_User( $user_id );
    93 		$user->set_role( 'administrator' );
   104 		$user->set_role( 'administrator' );
       
   105 
       
   106 		if ( $user_created ) {
       
   107 			$user->user_url = $guessurl;
       
   108 			wp_update_user( $user );
       
   109 		}
    94 
   110 
    95 		wp_install_defaults( $user_id );
   111 		wp_install_defaults( $user_id );
    96 
   112 
    97 		wp_install_maybe_enable_pretty_permalinks();
   113 		wp_install_maybe_enable_pretty_permalinks();
    98 
   114 
   127 	 * Adds the default "Uncategorized" category, the first post (with comment),
   143 	 * Adds the default "Uncategorized" category, the first post (with comment),
   128 	 * first page, and default widgets for default theme for the current version.
   144 	 * first page, and default widgets for default theme for the current version.
   129 	 *
   145 	 *
   130 	 * @since 2.1.0
   146 	 * @since 2.1.0
   131 	 *
   147 	 *
   132 	 * @global wpdb       $wpdb
   148 	 * @global wpdb       $wpdb         WordPress database abstraction object.
   133 	 * @global WP_Rewrite $wp_rewrite
   149 	 * @global WP_Rewrite $wp_rewrite   WordPress rewrite component.
   134 	 * @global string     $table_prefix
   150 	 * @global string     $table_prefix
   135 	 *
   151 	 *
   136 	 * @param int $user_id User ID.
   152 	 * @param int $user_id User ID.
   137 	 */
   153 	 */
   138 	function wp_install_defaults( $user_id ) {
   154 	function wp_install_defaults( $user_id ) {
   139 		global $wpdb, $wp_rewrite, $table_prefix;
   155 		global $wpdb, $wp_rewrite, $table_prefix;
   140 
   156 
   141 		// Default category
   157 		// Default category.
   142 		$cat_name = __( 'Uncategorized' );
   158 		$cat_name = __( 'Uncategorized' );
   143 		/* translators: Default category slug */
   159 		/* translators: Default category slug. */
   144 		$cat_slug = sanitize_title( _x( 'Uncategorized', 'Default category slug' ) );
   160 		$cat_slug = sanitize_title( _x( 'Uncategorized', 'Default category slug' ) );
   145 
   161 
   146 		if ( global_terms_enabled() ) {
   162 		if ( global_terms_enabled() ) {
   147 			$cat_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM {$wpdb->sitecategories} WHERE category_nicename = %s", $cat_slug ) );
   163 			$cat_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM {$wpdb->sitecategories} WHERE category_nicename = %s", $cat_slug ) );
   148 			if ( $cat_id == null ) {
   164 			if ( null == $cat_id ) {
   149 				$wpdb->insert(
   165 				$wpdb->insert(
   150 					$wpdb->sitecategories,
   166 					$wpdb->sitecategories,
   151 					array(
   167 					array(
   152 						'cat_ID'            => 0,
   168 						'cat_ID'            => 0,
   153 						'cat_name'          => $cat_name,
   169 						'cat_name'          => $cat_name,
   181 				'count'       => 1,
   197 				'count'       => 1,
   182 			)
   198 			)
   183 		);
   199 		);
   184 		$cat_tt_id = $wpdb->insert_id;
   200 		$cat_tt_id = $wpdb->insert_id;
   185 
   201 
   186 		// First post
   202 		// First post.
   187 		$now             = current_time( 'mysql' );
   203 		$now             = current_time( 'mysql' );
   188 		$now_gmt         = current_time( 'mysql', 1 );
   204 		$now_gmt         = current_time( 'mysql', 1 );
   189 		$first_post_guid = get_option( 'home' ) . '/?p=1';
   205 		$first_post_guid = get_option( 'home' ) . '/?p=1';
   190 
   206 
   191 		if ( is_multisite() ) {
   207 		if ( is_multisite() ) {
   192 			$first_post = get_site_option( 'first_post' );
   208 			$first_post = get_site_option( 'first_post' );
   193 
   209 
   194 			if ( ! $first_post ) {
   210 			if ( ! $first_post ) {
   195 				$first_post = "<!-- wp:paragraph -->\n<p>" .
   211 				$first_post = "<!-- wp:paragraph -->\n<p>" .
   196 				/* translators: first post content, %s: site link */
   212 				/* translators: First post content. %s: Site link. */
   197 				__( 'Welcome to %s. This is your first post. Edit or delete it, then start writing!' ) .
   213 				__( 'Welcome to %s. This is your first post. Edit or delete it, then start writing!' ) .
   198 				"</p>\n<!-- /wp:paragraph -->";
   214 				"</p>\n<!-- /wp:paragraph -->";
   199 			}
   215 			}
   200 
   216 
   201 			$first_post = sprintf(
   217 			$first_post = sprintf(
   202 				$first_post,
   218 				$first_post,
   203 				sprintf( '<a href="%s">%s</a>', esc_url( network_home_url() ), get_network()->site_name )
   219 				sprintf( '<a href="%s">%s</a>', esc_url( network_home_url() ), get_network()->site_name )
   204 			);
   220 			);
   205 
   221 
   206 			// Back-compat for pre-4.4
   222 			// Back-compat for pre-4.4.
   207 			$first_post = str_replace( 'SITE_URL', esc_url( network_home_url() ), $first_post );
   223 			$first_post = str_replace( 'SITE_URL', esc_url( network_home_url() ), $first_post );
   208 			$first_post = str_replace( 'SITE_NAME', get_network()->site_name, $first_post );
   224 			$first_post = str_replace( 'SITE_NAME', get_network()->site_name, $first_post );
   209 		} else {
   225 		} else {
   210 			$first_post = "<!-- wp:paragraph -->\n<p>" .
   226 			$first_post = "<!-- wp:paragraph -->\n<p>" .
   211 			/* translators: first post content, %s: site link */
   227 			/* translators: First post content. %s: Site link. */
   212 			__( 'Welcome to WordPress. This is your first post. Edit or delete it, then start writing!' ) .
   228 			__( 'Welcome to WordPress. This is your first post. Edit or delete it, then start writing!' ) .
   213 			"</p>\n<!-- /wp:paragraph -->";
   229 			"</p>\n<!-- /wp:paragraph -->";
   214 		}
   230 		}
   215 
   231 
   216 		$wpdb->insert(
   232 		$wpdb->insert(
   220 				'post_date'             => $now,
   236 				'post_date'             => $now,
   221 				'post_date_gmt'         => $now_gmt,
   237 				'post_date_gmt'         => $now_gmt,
   222 				'post_content'          => $first_post,
   238 				'post_content'          => $first_post,
   223 				'post_excerpt'          => '',
   239 				'post_excerpt'          => '',
   224 				'post_title'            => __( 'Hello world!' ),
   240 				'post_title'            => __( 'Hello world!' ),
   225 				/* translators: Default post slug */
   241 				/* translators: Default post slug. */
   226 				'post_name'             => sanitize_title( _x( 'hello-world', 'Default post slug' ) ),
   242 				'post_name'             => sanitize_title( _x( 'hello-world', 'Default post slug' ) ),
   227 				'post_modified'         => $now,
   243 				'post_modified'         => $now,
   228 				'post_modified_gmt'     => $now_gmt,
   244 				'post_modified_gmt'     => $now_gmt,
   229 				'guid'                  => $first_post_guid,
   245 				'guid'                  => $first_post_guid,
   230 				'comment_count'         => 1,
   246 				'comment_count'         => 1,
   239 				'term_taxonomy_id' => $cat_tt_id,
   255 				'term_taxonomy_id' => $cat_tt_id,
   240 				'object_id'        => 1,
   256 				'object_id'        => 1,
   241 			)
   257 			)
   242 		);
   258 		);
   243 
   259 
   244 		// Default comment
   260 		// Default comment.
   245 		if ( is_multisite() ) {
   261 		if ( is_multisite() ) {
   246 			$first_comment_author = get_site_option( 'first_comment_author' );
   262 			$first_comment_author = get_site_option( 'first_comment_author' );
   247 			$first_comment_email  = get_site_option( 'first_comment_email' );
   263 			$first_comment_email  = get_site_option( 'first_comment_email' );
   248 			$first_comment_url    = get_site_option( 'first_comment_url', network_home_url() );
   264 			$first_comment_url    = get_site_option( 'first_comment_url', network_home_url() );
   249 			$first_comment        = get_site_option( 'first_comment' );
   265 			$first_comment        = get_site_option( 'first_comment' );
   265 				'comment_author_email' => $first_comment_email,
   281 				'comment_author_email' => $first_comment_email,
   266 				'comment_author_url'   => $first_comment_url,
   282 				'comment_author_url'   => $first_comment_url,
   267 				'comment_date'         => $now,
   283 				'comment_date'         => $now,
   268 				'comment_date_gmt'     => $now_gmt,
   284 				'comment_date_gmt'     => $now_gmt,
   269 				'comment_content'      => $first_comment,
   285 				'comment_content'      => $first_comment,
       
   286 				'comment_type'         => 'comment',
   270 			)
   287 			)
   271 		);
   288 		);
   272 
   289 
   273 		// First Page
   290 		// First page.
   274 		if ( is_multisite() ) {
   291 		if ( is_multisite() ) {
   275 			$first_page = get_site_option( 'first_page' );
   292 			$first_page = get_site_option( 'first_page' );
   276 		}
   293 		}
   277 
   294 
   278 		if ( empty( $first_page ) ) {
   295 		if ( empty( $first_page ) ) {
   279 			$first_page = "<!-- wp:paragraph -->\n<p>";
   296 			$first_page = "<!-- wp:paragraph -->\n<p>";
   280 			/* translators: first page content */
   297 			/* translators: First page content. */
   281 			$first_page .= __( "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:" );
   298 			$first_page .= __( "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:" );
   282 			$first_page .= "</p>\n<!-- /wp:paragraph -->\n\n";
   299 			$first_page .= "</p>\n<!-- /wp:paragraph -->\n\n";
   283 
   300 
   284 			$first_page .= "<!-- wp:quote -->\n<blockquote class=\"wp-block-quote\"><p>";
   301 			$first_page .= "<!-- wp:quote -->\n<blockquote class=\"wp-block-quote\"><p>";
   285 			/* translators: first page content */
   302 			/* translators: First page content. */
   286 			$first_page .= __( "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.)" );
   303 			$first_page .= __( "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.)" );
   287 			$first_page .= "</p></blockquote>\n<!-- /wp:quote -->\n\n";
   304 			$first_page .= "</p></blockquote>\n<!-- /wp:quote -->\n\n";
   288 
   305 
   289 			$first_page .= "<!-- wp:paragraph -->\n<p>";
   306 			$first_page .= "<!-- wp:paragraph -->\n<p>";
   290 			/* translators: first page content */
   307 			/* translators: First page content. */
   291 			$first_page .= __( '...or something like this:' );
   308 			$first_page .= __( '...or something like this:' );
   292 			$first_page .= "</p>\n<!-- /wp:paragraph -->\n\n";
   309 			$first_page .= "</p>\n<!-- /wp:paragraph -->\n\n";
   293 
   310 
   294 			$first_page .= "<!-- wp:quote -->\n<blockquote class=\"wp-block-quote\"><p>";
   311 			$first_page .= "<!-- wp:quote -->\n<blockquote class=\"wp-block-quote\"><p>";
   295 			/* translators: first page content */
   312 			/* translators: First page content. */
   296 			$first_page .= __( 'The XYZ Doohickey Company was founded in 1971, and has been providing quality doohickeys to the public ever since. Located in Gotham City, XYZ employs over 2,000 people and does all kinds of awesome things for the Gotham community.' );
   313 			$first_page .= __( 'The XYZ Doohickey Company was founded in 1971, and has been providing quality doohickeys to the public ever since. Located in Gotham City, XYZ employs over 2,000 people and does all kinds of awesome things for the Gotham community.' );
   297 			$first_page .= "</p></blockquote>\n<!-- /wp:quote -->\n\n";
   314 			$first_page .= "</p></blockquote>\n<!-- /wp:quote -->\n\n";
   298 
   315 
   299 			$first_page .= "<!-- wp:paragraph -->\n<p>";
   316 			$first_page .= "<!-- wp:paragraph -->\n<p>";
   300 			$first_page .= sprintf(
   317 			$first_page .= sprintf(
   301 				/* translators: first page content, %s: site admin URL */
   318 				/* translators: First page content. %s: Site admin URL. */
   302 				__( 'As a new WordPress user, you should go to <a href="%s">your dashboard</a> to delete this page and create new pages for your content. Have fun!' ),
   319 				__( 'As a new WordPress user, you should go to <a href="%s">your dashboard</a> to delete this page and create new pages for your content. Have fun!' ),
   303 				admin_url()
   320 				admin_url()
   304 			);
   321 			);
   305 			$first_page .= "</p>\n<!-- /wp:paragraph -->";
   322 			$first_page .= "</p>\n<!-- /wp:paragraph -->";
   306 		}
   323 		}
   314 				'post_date_gmt'         => $now_gmt,
   331 				'post_date_gmt'         => $now_gmt,
   315 				'post_content'          => $first_page,
   332 				'post_content'          => $first_page,
   316 				'post_excerpt'          => '',
   333 				'post_excerpt'          => '',
   317 				'comment_status'        => 'closed',
   334 				'comment_status'        => 'closed',
   318 				'post_title'            => __( 'Sample Page' ),
   335 				'post_title'            => __( 'Sample Page' ),
   319 				/* translators: Default page slug */
   336 				/* translators: Default page slug. */
   320 				'post_name'             => __( 'sample-page' ),
   337 				'post_name'             => __( 'sample-page' ),
   321 				'post_modified'         => $now,
   338 				'post_modified'         => $now,
   322 				'post_modified_gmt'     => $now_gmt,
   339 				'post_modified_gmt'     => $now_gmt,
   323 				'guid'                  => $first_post_guid,
   340 				'guid'                  => $first_post_guid,
   324 				'post_type'             => 'page',
   341 				'post_type'             => 'page',
   334 				'meta_key'   => '_wp_page_template',
   351 				'meta_key'   => '_wp_page_template',
   335 				'meta_value' => 'default',
   352 				'meta_value' => 'default',
   336 			)
   353 			)
   337 		);
   354 		);
   338 
   355 
   339 		// Privacy Policy page
   356 		// Privacy Policy page.
   340 		if ( is_multisite() ) {
   357 		if ( is_multisite() ) {
   341 			// Disable by default unless the suggested content is provided.
   358 			// Disable by default unless the suggested content is provided.
   342 			$privacy_policy_content = get_site_option( 'default_privacy_policy_content' );
   359 			$privacy_policy_content = get_site_option( 'default_privacy_policy_content' );
   343 		} else {
   360 		} else {
   344 			if ( ! class_exists( 'WP_Privacy_Policy_Content' ) ) {
   361 			if ( ! class_exists( 'WP_Privacy_Policy_Content' ) ) {
   345 				include_once( ABSPATH . 'wp-admin/includes/misc.php' );
   362 				include_once ABSPATH . 'wp-admin/includes/class-wp-privacy-policy-content.php';
   346 			}
   363 			}
   347 
   364 
   348 			$privacy_policy_content = WP_Privacy_Policy_Content::get_default_content();
   365 			$privacy_policy_content = WP_Privacy_Policy_Content::get_default_content();
   349 		}
   366 		}
   350 
   367 
   359 					'post_date_gmt'         => $now_gmt,
   376 					'post_date_gmt'         => $now_gmt,
   360 					'post_content'          => $privacy_policy_content,
   377 					'post_content'          => $privacy_policy_content,
   361 					'post_excerpt'          => '',
   378 					'post_excerpt'          => '',
   362 					'comment_status'        => 'closed',
   379 					'comment_status'        => 'closed',
   363 					'post_title'            => __( 'Privacy Policy' ),
   380 					'post_title'            => __( 'Privacy Policy' ),
   364 					/* translators: Privacy Policy page slug */
   381 					/* translators: Privacy Policy page slug. */
   365 					'post_name'             => __( 'privacy-policy' ),
   382 					'post_name'             => __( 'privacy-policy' ),
   366 					'post_modified'         => $now,
   383 					'post_modified'         => $now,
   367 					'post_modified_gmt'     => $now_gmt,
   384 					'post_modified_gmt'     => $now_gmt,
   368 					'guid'                  => $privacy_policy_guid,
   385 					'guid'                  => $privacy_policy_guid,
   369 					'post_type'             => 'page',
   386 					'post_type'             => 'page',
   448 				'wp_inactive_widgets' => array(),
   465 				'wp_inactive_widgets' => array(),
   449 				'sidebar-1'           => array(
   466 				'sidebar-1'           => array(
   450 					0 => 'search-2',
   467 					0 => 'search-2',
   451 					1 => 'recent-posts-2',
   468 					1 => 'recent-posts-2',
   452 					2 => 'recent-comments-2',
   469 					2 => 'recent-comments-2',
   453 					3 => 'archives-2',
   470 				),
   454 					4 => 'categories-2',
   471 				'sidebar-2'           => array(
   455 					5 => 'meta-2',
   472 					0 => 'archives-2',
       
   473 					1 => 'categories-2',
       
   474 					2 => 'meta-2',
   456 				),
   475 				),
   457 				'array_version'       => 3,
   476 				'array_version'       => 3,
   458 			)
   477 			)
   459 		);
   478 		);
   460 		if ( ! is_multisite() ) {
   479 		if ( ! is_multisite() ) {
   473 
   492 
   474 			// Remove all perms except for the login user.
   493 			// Remove all perms except for the login user.
   475 			$wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->usermeta WHERE user_id != %d AND meta_key = %s", $user_id, $table_prefix . 'user_level' ) );
   494 			$wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->usermeta WHERE user_id != %d AND meta_key = %s", $user_id, $table_prefix . 'user_level' ) );
   476 			$wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->usermeta WHERE user_id != %d AND meta_key = %s", $user_id, $table_prefix . 'capabilities' ) );
   495 			$wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->usermeta WHERE user_id != %d AND meta_key = %s", $user_id, $table_prefix . 'capabilities' ) );
   477 
   496 
   478 			// Delete any caps that snuck into the previously active blog. (Hardcoded to blog 1 for now.) TODO: Get previous_blog_id.
   497 			// Delete any caps that snuck into the previously active blog. (Hardcoded to blog 1 for now.)
   479 			if ( ! is_super_admin( $user_id ) && $user_id != 1 ) {
   498 			// TODO: Get previous_blog_id.
       
   499 			if ( ! is_super_admin( $user_id ) && 1 != $user_id ) {
   480 				$wpdb->delete(
   500 				$wpdb->delete(
   481 					$wpdb->usermeta,
   501 					$wpdb->usermeta,
   482 					array(
   502 					array(
   483 						'user_id'  => $user_id,
   503 						'user_id'  => $user_id,
   484 						'meta_key' => $wpdb->base_prefix . '1_capabilities',
   504 						'meta_key' => $wpdb->base_prefix . '1_capabilities',
   523 
   543 
   524 	foreach ( (array) $permalink_structures as $permalink_structure ) {
   544 	foreach ( (array) $permalink_structures as $permalink_structure ) {
   525 		$wp_rewrite->set_permalink_structure( $permalink_structure );
   545 		$wp_rewrite->set_permalink_structure( $permalink_structure );
   526 
   546 
   527 		/*
   547 		/*
   528 		  * Flush rules with the hard option to force refresh of the web-server's
   548 		 * Flush rules with the hard option to force refresh of the web-server's
   529 		  * rewrite config file (e.g. .htaccess or web.config).
   549 		 * rewrite config file (e.g. .htaccess or web.config).
   530 		  */
   550 		 */
   531 		$wp_rewrite->flush_rules( true );
   551 		$wp_rewrite->flush_rules( true );
   532 
   552 
   533 		$test_url = '';
   553 		$test_url = '';
   534 
   554 
   535 		// Test against a real WordPress Post
   555 		// Test against a real WordPress post.
   536 		$first_post = get_page_by_path( sanitize_title( _x( 'hello-world', 'Default post slug' ) ), OBJECT, 'post' );
   556 		$first_post = get_page_by_path( sanitize_title( _x( 'hello-world', 'Default post slug' ) ), OBJECT, 'post' );
   537 		if ( $first_post ) {
   557 		if ( $first_post ) {
   538 			$test_url = get_permalink( $first_post->ID );
   558 			$test_url = get_permalink( $first_post->ID );
   539 		}
   559 		}
   540 
   560 
   541 		/*
   561 		/*
   542 		  * Send a request to the site, and check whether
   562 		 * Send a request to the site, and check whether
   543 		  * the 'x-pingback' header is returned as expected.
   563 		 * the 'x-pingback' header is returned as expected.
   544 		  *
   564 		 *
   545 		  * Uses wp_remote_get() instead of wp_remote_head() because web servers
   565 		 * Uses wp_remote_get() instead of wp_remote_head() because web servers
   546 		  * can block head requests.
   566 		 * can block head requests.
   547 		  */
   567 		 */
   548 		$response          = wp_remote_get( $test_url, array( 'timeout' => 5 ) );
   568 		$response          = wp_remote_get( $test_url, array( 'timeout' => 5 ) );
   549 		$x_pingback_header = wp_remote_retrieve_header( $response, 'x-pingback' );
   569 		$x_pingback_header = wp_remote_retrieve_header( $response, 'x-pingback' );
   550 		$pretty_permalinks = $x_pingback_header && $x_pingback_header === get_bloginfo( 'pingback_url' );
   570 		$pretty_permalinks = $x_pingback_header && get_bloginfo( 'pingback_url' ) === $x_pingback_header;
   551 
   571 
   552 		if ( $pretty_permalinks ) {
   572 		if ( $pretty_permalinks ) {
   553 			return true;
   573 			return true;
   554 		}
   574 		}
   555 	}
   575 	}
   581 	function wp_new_blog_notification( $blog_title, $blog_url, $user_id, $password ) {
   601 	function wp_new_blog_notification( $blog_title, $blog_url, $user_id, $password ) {
   582 		$user      = new WP_User( $user_id );
   602 		$user      = new WP_User( $user_id );
   583 		$email     = $user->user_email;
   603 		$email     = $user->user_email;
   584 		$name      = $user->user_login;
   604 		$name      = $user->user_login;
   585 		$login_url = wp_login_url();
   605 		$login_url = wp_login_url();
   586 		/* translators: New site notification email. 1: New site URL, 2: User login, 3: User password or password reset link, 4: Login URL */
   606 
   587 		$message = sprintf(
   607 		$message = sprintf(
       
   608 			/* translators: New site notification email. 1: New site URL, 2: User login, 3: User password or password reset link, 4: Login URL. */
   588 			__(
   609 			__(
   589 				'Your new WordPress site has been successfully set up at:
   610 				'Your new WordPress site has been successfully set up at:
   590 
   611 
   591 %1$s
   612 %1$s
   592 
   613 
   606 			$name,
   627 			$name,
   607 			$password,
   628 			$password,
   608 			$login_url
   629 			$login_url
   609 		);
   630 		);
   610 
   631 
   611 		@wp_mail( $email, __( 'New WordPress Site' ), $message );
   632 		wp_mail( $email, __( 'New WordPress Site' ), $message );
   612 	}
   633 	}
   613 endif;
   634 endif;
   614 
   635 
   615 if ( ! function_exists( 'wp_upgrade' ) ) :
   636 if ( ! function_exists( 'wp_upgrade' ) ) :
   616 	/**
   637 	/**
   618 	 *
   639 	 *
   619 	 * Upgrades the database if needed during a site update.
   640 	 * Upgrades the database if needed during a site update.
   620 	 *
   641 	 *
   621 	 * @since 2.1.0
   642 	 * @since 2.1.0
   622 	 *
   643 	 *
   623 	 * @global int  $wp_current_db_version
   644 	 * @global int  $wp_current_db_version The old (current) database version.
   624 	 * @global int  $wp_db_version
   645 	 * @global int  $wp_db_version         The new database version.
   625 	 * @global wpdb $wpdb WordPress database abstraction object.
   646 	 * @global wpdb $wpdb                  WordPress database abstraction object.
   626 	 */
   647 	 */
   627 	function wp_upgrade() {
   648 	function wp_upgrade() {
   628 		global $wp_current_db_version, $wp_db_version, $wpdb;
   649 		global $wp_current_db_version, $wp_db_version, $wpdb;
   629 
   650 
   630 		$wp_current_db_version = __get_option( 'db_version' );
   651 		$wp_current_db_version = __get_option( 'db_version' );
   631 
   652 
   632 		// We are up-to-date. Nothing to do.
   653 		// We are up to date. Nothing to do.
   633 		if ( $wp_db_version == $wp_current_db_version ) {
   654 		if ( $wp_db_version == $wp_current_db_version ) {
   634 			return;
   655 			return;
   635 		}
   656 		}
   636 
   657 
   637 		if ( ! is_blog_installed() ) {
   658 		if ( ! is_blog_installed() ) {
   647 			upgrade_network();
   668 			upgrade_network();
   648 		}
   669 		}
   649 		wp_cache_flush();
   670 		wp_cache_flush();
   650 
   671 
   651 		if ( is_multisite() ) {
   672 		if ( is_multisite() ) {
   652 			$site_id = get_current_blog_id();
   673 			update_site_meta( get_current_blog_id(), 'db_version', $wp_db_version );
   653 
   674 			update_site_meta( get_current_blog_id(), 'db_last_updated', microtime() );
   654 			if ( $wpdb->get_row( $wpdb->prepare( "SELECT blog_id FROM {$wpdb->blog_versions} WHERE blog_id = %d", $site_id ) ) ) {
       
   655 				$wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->blog_versions} SET db_version = %d WHERE blog_id = %d", $wp_db_version, $site_id ) );
       
   656 			} else {
       
   657 				$wpdb->query( $wpdb->prepare( "INSERT INTO {$wpdb->blog_versions} ( `blog_id` , `db_version` , `last_updated` ) VALUES ( %d, %d, NOW() );", $site_id, $wp_db_version ) );
       
   658 			}
       
   659 		}
   675 		}
   660 
   676 
   661 		/**
   677 		/**
   662 		 * Fires after a site is fully upgraded.
   678 		 * Fires after a site is fully upgraded.
   663 		 *
   679 		 *
   677  * based on database version and WP version being updated-to.
   693  * based on database version and WP version being updated-to.
   678  *
   694  *
   679  * @ignore
   695  * @ignore
   680  * @since 1.0.1
   696  * @since 1.0.1
   681  *
   697  *
   682  * @global int $wp_current_db_version
   698  * @global int $wp_current_db_version The old (current) database version.
   683  * @global int $wp_db_version
   699  * @global int $wp_db_version         The new database version.
   684  */
   700  */
   685 function upgrade_all() {
   701 function upgrade_all() {
   686 	global $wp_current_db_version, $wp_db_version;
   702 	global $wp_current_db_version, $wp_db_version;
       
   703 
   687 	$wp_current_db_version = __get_option( 'db_version' );
   704 	$wp_current_db_version = __get_option( 'db_version' );
   688 
   705 
   689 	// We are up-to-date. Nothing to do.
   706 	// We are up to date. Nothing to do.
   690 	if ( $wp_db_version == $wp_current_db_version ) {
   707 	if ( $wp_db_version == $wp_current_db_version ) {
   691 		return;
   708 		return;
   692 	}
   709 	}
   693 
   710 
   694 	// If the version is not set in the DB, try to guess the version.
   711 	// If the version is not set in the DB, try to guess the version.
   813 
   830 
   814 	if ( $wp_current_db_version < 44719 ) {
   831 	if ( $wp_current_db_version < 44719 ) {
   815 		upgrade_510();
   832 		upgrade_510();
   816 	}
   833 	}
   817 
   834 
       
   835 	if ( $wp_current_db_version < 45744 ) {
       
   836 		upgrade_530();
       
   837 	}
       
   838 
       
   839 	if ( $wp_current_db_version < 48575 ) {
       
   840 		upgrade_550();
       
   841 	}
       
   842 
   818 	maybe_disable_link_manager();
   843 	maybe_disable_link_manager();
   819 
   844 
   820 	maybe_disable_automattic_widgets();
   845 	maybe_disable_automattic_widgets();
   821 
   846 
   822 	update_option( 'db_version', $wp_db_version );
   847 	update_option( 'db_version', $wp_db_version );
   832  * @global wpdb $wpdb WordPress database abstraction object.
   857  * @global wpdb $wpdb WordPress database abstraction object.
   833  */
   858  */
   834 function upgrade_100() {
   859 function upgrade_100() {
   835 	global $wpdb;
   860 	global $wpdb;
   836 
   861 
   837 	// Get the title and ID of every post, post_name to check if it already has a value
   862 	// Get the title and ID of every post, post_name to check if it already has a value.
   838 	$posts = $wpdb->get_results( "SELECT ID, post_title, post_name FROM $wpdb->posts WHERE post_name = ''" );
   863 	$posts = $wpdb->get_results( "SELECT ID, post_title, post_name FROM $wpdb->posts WHERE post_name = ''" );
   839 	if ( $posts ) {
   864 	if ( $posts ) {
   840 		foreach ( $posts as $post ) {
   865 		foreach ( $posts as $post ) {
   841 			if ( '' == $post->post_name ) {
   866 			if ( '' === $post->post_name ) {
   842 				$newtitle = sanitize_title( $post->post_title );
   867 				$newtitle = sanitize_title( $post->post_title );
   843 				$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_name = %s WHERE ID = %d", $newtitle, $post->ID ) );
   868 				$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_name = %s WHERE ID = %d", $newtitle, $post->ID ) );
   844 			}
   869 			}
   845 		}
   870 		}
   846 	}
   871 	}
   847 
   872 
   848 	$categories = $wpdb->get_results( "SELECT cat_ID, cat_name, category_nicename FROM $wpdb->categories" );
   873 	$categories = $wpdb->get_results( "SELECT cat_ID, cat_name, category_nicename FROM $wpdb->categories" );
   849 	foreach ( $categories as $category ) {
   874 	foreach ( $categories as $category ) {
   850 		if ( '' == $category->category_nicename ) {
   875 		if ( '' === $category->category_nicename ) {
   851 			$newtitle = sanitize_title( $category->cat_name );
   876 			$newtitle = sanitize_title( $category->cat_name );
   852 			$wpdb->update( $wpdb->categories, array( 'category_nicename' => $newtitle ), array( 'cat_ID' => $category->cat_ID ) );
   877 			$wpdb->update( $wpdb->categories, array( 'category_nicename' => $newtitle ), array( 'cat_ID' => $category->cat_ID ) );
   853 		}
   878 		}
   854 	}
   879 	}
   855 
   880 
   871 	endif;
   896 	endif;
   872 
   897 
   873 	$allposts = $wpdb->get_results( "SELECT ID, post_category FROM $wpdb->posts WHERE post_category != '0' $catwhere" );
   898 	$allposts = $wpdb->get_results( "SELECT ID, post_category FROM $wpdb->posts WHERE post_category != '0' $catwhere" );
   874 	if ( $allposts ) :
   899 	if ( $allposts ) :
   875 		foreach ( $allposts as $post ) {
   900 		foreach ( $allposts as $post ) {
   876 			// Check to see if it's already been imported
   901 			// Check to see if it's already been imported.
   877 			$cat = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->post2cat WHERE post_id = %d AND category_id = %d", $post->ID, $post->post_category ) );
   902 			$cat = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->post2cat WHERE post_id = %d AND category_id = %d", $post->ID, $post->post_category ) );
   878 			if ( ! $cat && 0 != $post->post_category ) { // If there's no result
   903 			if ( ! $cat && 0 != $post->post_category ) { // If there's no result.
   879 				$wpdb->insert(
   904 				$wpdb->insert(
   880 					$wpdb->post2cat,
   905 					$wpdb->post2cat,
   881 					array(
   906 					array(
   882 						'post_id'     => $post->ID,
   907 						'post_id'     => $post->ID,
   883 						'category_id' => $post->post_category,
   908 						'category_id' => $post->post_category,
   897  * @global wpdb $wpdb WordPress database abstraction object.
   922  * @global wpdb $wpdb WordPress database abstraction object.
   898  */
   923  */
   899 function upgrade_101() {
   924 function upgrade_101() {
   900 	global $wpdb;
   925 	global $wpdb;
   901 
   926 
   902 	// Clean up indices, add a few
   927 	// Clean up indices, add a few.
   903 	add_clean_index( $wpdb->posts, 'post_name' );
   928 	add_clean_index( $wpdb->posts, 'post_name' );
   904 	add_clean_index( $wpdb->posts, 'post_status' );
   929 	add_clean_index( $wpdb->posts, 'post_status' );
   905 	add_clean_index( $wpdb->categories, 'category_nicename' );
   930 	add_clean_index( $wpdb->categories, 'category_nicename' );
   906 	add_clean_index( $wpdb->comments, 'comment_approved' );
   931 	add_clean_index( $wpdb->comments, 'comment_approved' );
   907 	add_clean_index( $wpdb->comments, 'comment_post_ID' );
   932 	add_clean_index( $wpdb->comments, 'comment_post_ID' );
   921 	global $wpdb;
   946 	global $wpdb;
   922 
   947 
   923 	// Set user_nicename.
   948 	// Set user_nicename.
   924 	$users = $wpdb->get_results( "SELECT ID, user_nickname, user_nicename FROM $wpdb->users" );
   949 	$users = $wpdb->get_results( "SELECT ID, user_nickname, user_nicename FROM $wpdb->users" );
   925 	foreach ( $users as $user ) {
   950 	foreach ( $users as $user ) {
   926 		if ( '' == $user->user_nicename ) {
   951 		if ( '' === $user->user_nicename ) {
   927 			$newname = sanitize_title( $user->user_nickname );
   952 			$newname = sanitize_title( $user->user_nickname );
   928 			$wpdb->update( $wpdb->users, array( 'user_nicename' => $newname ), array( 'ID' => $user->ID ) );
   953 			$wpdb->update( $wpdb->users, array( 'user_nicename' => $newname ), array( 'ID' => $user->ID ) );
   929 		}
   954 		}
   930 	}
   955 	}
   931 
   956 
   934 		if ( ! preg_match( '/^[A-Fa-f0-9]{32}$/', $row->user_pass ) ) {
   959 		if ( ! preg_match( '/^[A-Fa-f0-9]{32}$/', $row->user_pass ) ) {
   935 			$wpdb->update( $wpdb->users, array( 'user_pass' => md5( $row->user_pass ) ), array( 'ID' => $row->ID ) );
   960 			$wpdb->update( $wpdb->users, array( 'user_pass' => md5( $row->user_pass ) ), array( 'ID' => $row->ID ) );
   936 		}
   961 		}
   937 	}
   962 	}
   938 
   963 
   939 	// Get the GMT offset, we'll use that later on
   964 	// Get the GMT offset, we'll use that later on.
   940 	$all_options = get_alloptions_110();
   965 	$all_options = get_alloptions_110();
   941 
   966 
   942 	$time_difference = $all_options->time_difference;
   967 	$time_difference = $all_options->time_difference;
   943 
   968 
   944 		$server_time = time() + date( 'Z' );
   969 		$server_time = time() + gmdate( 'Z' );
   945 	$weblogger_time  = $server_time + $time_difference * HOUR_IN_SECONDS;
   970 	$weblogger_time  = $server_time + $time_difference * HOUR_IN_SECONDS;
   946 	$gmt_time        = time();
   971 	$gmt_time        = time();
   947 
   972 
   948 	$diff_gmt_server       = ( $gmt_time - $server_time ) / HOUR_IN_SECONDS;
   973 	$diff_gmt_server       = ( $gmt_time - $server_time ) / HOUR_IN_SECONDS;
   949 	$diff_weblogger_server = ( $weblogger_time - $server_time ) / HOUR_IN_SECONDS;
   974 	$diff_weblogger_server = ( $weblogger_time - $server_time ) / HOUR_IN_SECONDS;
   950 	$diff_gmt_weblogger    = $diff_gmt_server - $diff_weblogger_server;
   975 	$diff_gmt_weblogger    = $diff_gmt_server - $diff_weblogger_server;
   951 	$gmt_offset            = -$diff_gmt_weblogger;
   976 	$gmt_offset            = -$diff_gmt_weblogger;
   952 
   977 
   953 	// Add a gmt_offset option, with value $gmt_offset
   978 	// Add a gmt_offset option, with value $gmt_offset.
   954 	add_option( 'gmt_offset', $gmt_offset );
   979 	add_option( 'gmt_offset', $gmt_offset );
   955 
   980 
   956 	// Check if we already set the GMT fields (if we did, then
   981 	/*
   957 	// MAX(post_date_gmt) can't be '0000-00-00 00:00:00'
   982 	 * Check if we already set the GMT fields. If we did, then
   958 	// <michel_v> I just slapped myself silly for not thinking about it earlier
   983 	 * MAX(post_date_gmt) can't be '0000-00-00 00:00:00'.
   959 	$got_gmt_fields = ! ( $wpdb->get_var( "SELECT MAX(post_date_gmt) FROM $wpdb->posts" ) == '0000-00-00 00:00:00' );
   984 	 * <michel_v> I just slapped myself silly for not thinking about it earlier.
       
   985 	 */
       
   986 	$got_gmt_fields = ( '0000-00-00 00:00:00' !== $wpdb->get_var( "SELECT MAX(post_date_gmt) FROM $wpdb->posts" ) );
   960 
   987 
   961 	if ( ! $got_gmt_fields ) {
   988 	if ( ! $got_gmt_fields ) {
   962 
   989 
   963 		// Add or subtract time to all dates, to get GMT dates
   990 		// Add or subtract time to all dates, to get GMT dates.
   964 		$add_hours   = intval( $diff_gmt_weblogger );
   991 		$add_hours   = intval( $diff_gmt_weblogger );
   965 		$add_minutes = intval( 60 * ( $diff_gmt_weblogger - $add_hours ) );
   992 		$add_minutes = intval( 60 * ( $diff_gmt_weblogger - $add_hours ) );
   966 		$wpdb->query( "UPDATE $wpdb->posts SET post_date_gmt = DATE_ADD(post_date, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)" );
   993 		$wpdb->query( "UPDATE $wpdb->posts SET post_date_gmt = DATE_ADD(post_date, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)" );
   967 		$wpdb->query( "UPDATE $wpdb->posts SET post_modified = post_date" );
   994 		$wpdb->query( "UPDATE $wpdb->posts SET post_modified = post_date" );
   968 		$wpdb->query( "UPDATE $wpdb->posts SET post_modified_gmt = DATE_ADD(post_modified, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE) WHERE post_modified != '0000-00-00 00:00:00'" );
   995 		$wpdb->query( "UPDATE $wpdb->posts SET post_modified_gmt = DATE_ADD(post_modified, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE) WHERE post_modified != '0000-00-00 00:00:00'" );
  1032 	if ( ! is_array( $active_plugins ) ) {
  1059 	if ( ! is_array( $active_plugins ) ) {
  1033 		$active_plugins = explode( "\n", trim( $active_plugins ) );
  1060 		$active_plugins = explode( "\n", trim( $active_plugins ) );
  1034 		update_option( 'active_plugins', $active_plugins );
  1061 		update_option( 'active_plugins', $active_plugins );
  1035 	}
  1062 	}
  1036 
  1063 
  1037 	// Obsolete tables
  1064 	// Obsolete tables.
  1038 	$wpdb->query( 'DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optionvalues' );
  1065 	$wpdb->query( 'DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optionvalues' );
  1039 	$wpdb->query( 'DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optiontypes' );
  1066 	$wpdb->query( 'DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optiontypes' );
  1040 	$wpdb->query( 'DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optiongroups' );
  1067 	$wpdb->query( 'DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optiongroups' );
  1041 	$wpdb->query( 'DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optiongroup_options' );
  1068 	$wpdb->query( 'DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optiongroup_options' );
  1042 
  1069 
  1043 	// Update comments table to use comment_type
  1070 	// Update comments table to use comment_type.
  1044 	$wpdb->query( "UPDATE $wpdb->comments SET comment_type='trackback', comment_content = REPLACE(comment_content, '<trackback />', '') WHERE comment_content LIKE '<trackback />%'" );
  1071 	$wpdb->query( "UPDATE $wpdb->comments SET comment_type='trackback', comment_content = REPLACE(comment_content, '<trackback />', '') WHERE comment_content LIKE '<trackback />%'" );
  1045 	$wpdb->query( "UPDATE $wpdb->comments SET comment_type='pingback', comment_content = REPLACE(comment_content, '<pingback />', '') WHERE comment_content LIKE '<pingback />%'" );
  1072 	$wpdb->query( "UPDATE $wpdb->comments SET comment_type='pingback', comment_content = REPLACE(comment_content, '<pingback />', '') WHERE comment_content LIKE '<pingback />%'" );
  1046 
  1073 
  1047 	// Some versions have multiple duplicate option_name rows with the same values
  1074 	// Some versions have multiple duplicate option_name rows with the same values.
  1048 	$options = $wpdb->get_results( "SELECT option_name, COUNT(option_name) AS dupes FROM `$wpdb->options` GROUP BY option_name" );
  1075 	$options = $wpdb->get_results( "SELECT option_name, COUNT(option_name) AS dupes FROM `$wpdb->options` GROUP BY option_name" );
  1049 	foreach ( $options as $option ) {
  1076 	foreach ( $options as $option ) {
  1050 		if ( 1 != $option->dupes ) { // Could this be done in the query?
  1077 		if ( 1 != $option->dupes ) { // Could this be done in the query?
  1051 			$limit    = $option->dupes - 1;
  1078 			$limit    = $option->dupes - 1;
  1052 			$dupe_ids = $wpdb->get_col( $wpdb->prepare( "SELECT option_id FROM $wpdb->options WHERE option_name = %s LIMIT %d", $option->option_name, $limit ) );
  1079 			$dupe_ids = $wpdb->get_col( $wpdb->prepare( "SELECT option_id FROM $wpdb->options WHERE option_name = %s LIMIT %d", $option->option_name, $limit ) );
  1053 			if ( $dupe_ids ) {
  1080 			if ( $dupe_ids ) {
  1054 				$dupe_ids = join( $dupe_ids, ',' );
  1081 				$dupe_ids = join( ',', $dupe_ids );
  1055 				$wpdb->query( "DELETE FROM $wpdb->options WHERE option_id IN ($dupe_ids)" );
  1082 				$wpdb->query( "DELETE FROM $wpdb->options WHERE option_id IN ($dupe_ids)" );
  1056 			}
  1083 			}
  1057 		}
  1084 		}
  1058 	}
  1085 	}
  1059 
  1086 
  1064  * Execute changes made in WordPress 2.0.
  1091  * Execute changes made in WordPress 2.0.
  1065  *
  1092  *
  1066  * @ignore
  1093  * @ignore
  1067  * @since 2.0.0
  1094  * @since 2.0.0
  1068  *
  1095  *
  1069  * @global wpdb $wpdb WordPress database abstraction object.
  1096  * @global wpdb $wpdb                  WordPress database abstraction object.
  1070  * @global int  $wp_current_db_version
  1097  * @global int  $wp_current_db_version The old (current) database version.
  1071  */
  1098  */
  1072 function upgrade_160() {
  1099 function upgrade_160() {
  1073 	global $wpdb, $wp_current_db_version;
  1100 	global $wpdb, $wp_current_db_version;
  1074 
  1101 
  1075 	populate_roles_160();
  1102 	populate_roles_160();
  1104 			update_user_meta( $user->ID, 'description', wp_slash( $user->user_description ) );
  1131 			update_user_meta( $user->ID, 'description', wp_slash( $user->user_description ) );
  1105 		}
  1132 		}
  1106 
  1133 
  1107 		if ( isset( $user->user_idmode ) ) :
  1134 		if ( isset( $user->user_idmode ) ) :
  1108 			$idmode = $user->user_idmode;
  1135 			$idmode = $user->user_idmode;
  1109 			if ( $idmode == 'nickname' ) {
  1136 			if ( 'nickname' === $idmode ) {
  1110 				$id = $user->user_nickname;
  1137 				$id = $user->user_nickname;
  1111 			}
  1138 			}
  1112 			if ( $idmode == 'login' ) {
  1139 			if ( 'login' === $idmode ) {
  1113 				$id = $user->user_login;
  1140 				$id = $user->user_login;
  1114 			}
  1141 			}
  1115 			if ( $idmode == 'firstname' ) {
  1142 			if ( 'firstname' === $idmode ) {
  1116 				$id = $user->user_firstname;
  1143 				$id = $user->user_firstname;
  1117 			}
  1144 			}
  1118 			if ( $idmode == 'lastname' ) {
  1145 			if ( 'lastname' === $idmode ) {
  1119 				$id = $user->user_lastname;
  1146 				$id = $user->user_lastname;
  1120 			}
  1147 			}
  1121 			if ( $idmode == 'namefl' ) {
  1148 			if ( 'namefl' === $idmode ) {
  1122 				$id = $user->user_firstname . ' ' . $user->user_lastname;
  1149 				$id = $user->user_firstname . ' ' . $user->user_lastname;
  1123 			}
  1150 			}
  1124 			if ( $idmode == 'namelf' ) {
  1151 			if ( 'namelf' === $idmode ) {
  1125 				$id = $user->user_lastname . ' ' . $user->user_firstname;
  1152 				$id = $user->user_lastname . ' ' . $user->user_firstname;
  1126 			}
  1153 			}
  1127 			if ( ! $idmode ) {
  1154 			if ( ! $idmode ) {
  1128 				$id = $user->user_nickname;
  1155 				$id = $user->user_nickname;
  1129 			}
  1156 			}
  1183  * Execute changes made in WordPress 2.1.
  1210  * Execute changes made in WordPress 2.1.
  1184  *
  1211  *
  1185  * @ignore
  1212  * @ignore
  1186  * @since 2.1.0
  1213  * @since 2.1.0
  1187  *
  1214  *
  1188  * @global wpdb $wpdb WordPress database abstraction object.
  1215  * @global int  $wp_current_db_version The old (current) database version.
  1189  * @global int  $wp_current_db_version
  1216  * @global wpdb $wpdb                  WordPress database abstraction object.
  1190  */
  1217  */
  1191 function upgrade_210() {
  1218 function upgrade_210() {
  1192 	global $wpdb, $wp_current_db_version;
  1219 	global $wp_current_db_version, $wpdb;
  1193 
  1220 
  1194 	if ( $wp_current_db_version < 3506 ) {
  1221 	if ( $wp_current_db_version < 3506 ) {
  1195 		// Update status and type.
  1222 		// Update status and type.
  1196 		$posts = $wpdb->get_results( "SELECT ID, post_status FROM $wpdb->posts" );
  1223 		$posts = $wpdb->get_results( "SELECT ID, post_status FROM $wpdb->posts" );
  1197 
  1224 
  1198 		if ( ! empty( $posts ) ) {
  1225 		if ( ! empty( $posts ) ) {
  1199 			foreach ( $posts as $post ) {
  1226 			foreach ( $posts as $post ) {
  1200 				$status = $post->post_status;
  1227 				$status = $post->post_status;
  1201 				$type   = 'post';
  1228 				$type   = 'post';
  1202 
  1229 
  1203 				if ( 'static' == $status ) {
  1230 				if ( 'static' === $status ) {
  1204 					$status = 'publish';
  1231 					$status = 'publish';
  1205 					$type   = 'page';
  1232 					$type   = 'page';
  1206 				} elseif ( 'attachment' == $status ) {
  1233 				} elseif ( 'attachment' === $status ) {
  1207 					$status = 'inherit';
  1234 					$status = 'inherit';
  1208 					$type   = 'attachment';
  1235 					$type   = 'attachment';
  1209 				}
  1236 				}
  1210 
  1237 
  1211 				$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_status = %s, post_type = %s WHERE ID = %d", $status, $type, $post->ID ) );
  1238 				$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_status = %s, post_type = %s WHERE ID = %d", $status, $type, $post->ID ) );
  1235  * Execute changes made in WordPress 2.3.
  1262  * Execute changes made in WordPress 2.3.
  1236  *
  1263  *
  1237  * @ignore
  1264  * @ignore
  1238  * @since 2.3.0
  1265  * @since 2.3.0
  1239  *
  1266  *
  1240  * @global wpdb $wpdb WordPress database abstraction object.
  1267  * @global int  $wp_current_db_version The old (current) database version.
  1241  * @global int  $wp_current_db_version
  1268  * @global wpdb $wpdb                  WordPress database abstraction object.
  1242  */
  1269  */
  1243 function upgrade_230() {
  1270 function upgrade_230() {
  1244 	global $wp_current_db_version, $wpdb;
  1271 	global $wp_current_db_version, $wpdb;
  1245 
  1272 
  1246 	if ( $wp_current_db_version < 5200 ) {
  1273 	if ( $wp_current_db_version < 5200 ) {
  1258 		$slug        = $category->category_nicename;
  1285 		$slug        = $category->category_nicename;
  1259 		$parent      = $category->category_parent;
  1286 		$parent      = $category->category_parent;
  1260 		$term_group  = 0;
  1287 		$term_group  = 0;
  1261 
  1288 
  1262 		// Associate terms with the same slug in a term group and make slugs unique.
  1289 		// Associate terms with the same slug in a term group and make slugs unique.
  1263 		if ( $exists = $wpdb->get_results( $wpdb->prepare( "SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $slug ) ) ) {
  1290 		$exists = $wpdb->get_results( $wpdb->prepare( "SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $slug ) );
       
  1291 		if ( $exists ) {
  1264 			$term_group = $exists[0]->term_group;
  1292 			$term_group = $exists[0]->term_group;
  1265 			$id         = $exists[0]->term_id;
  1293 			$id         = $exists[0]->term_id;
  1266 			$num        = 2;
  1294 			$num        = 2;
  1267 			do {
  1295 			do {
  1268 				$alt_slug = $slug . "-$num";
  1296 				$alt_slug = $slug . "-$num";
  1328 	$posts = $wpdb->get_results( "SELECT $select FROM $wpdb->post2cat GROUP BY post_id, category_id" );
  1356 	$posts = $wpdb->get_results( "SELECT $select FROM $wpdb->post2cat GROUP BY post_id, category_id" );
  1329 	foreach ( $posts as $post ) {
  1357 	foreach ( $posts as $post ) {
  1330 		$post_id  = (int) $post->post_id;
  1358 		$post_id  = (int) $post->post_id;
  1331 		$term_id  = (int) $post->category_id;
  1359 		$term_id  = (int) $post->category_id;
  1332 		$taxonomy = 'category';
  1360 		$taxonomy = 'category';
  1333 		if ( ! empty( $post->rel_type ) && 'tag' == $post->rel_type ) {
  1361 		if ( ! empty( $post->rel_type ) && 'tag' === $post->rel_type ) {
  1334 			$taxonomy = 'tag';
  1362 			$taxonomy = 'tag';
  1335 		}
  1363 		}
  1336 		$tt_id = $tt_ids[ $term_id ][ $taxonomy ];
  1364 		$tt_id = $tt_ids[ $term_id ][ $taxonomy ];
  1337 		if ( empty( $tt_id ) ) {
  1365 		if ( empty( $tt_id ) ) {
  1338 			continue;
  1366 			continue;
  1349 
  1377 
  1350 	// < 3570 we used linkcategories. >= 3570 we used categories and link2cat.
  1378 	// < 3570 we used linkcategories. >= 3570 we used categories and link2cat.
  1351 	if ( $wp_current_db_version < 3570 ) {
  1379 	if ( $wp_current_db_version < 3570 ) {
  1352 		/*
  1380 		/*
  1353 		 * Create link_category terms for link categories. Create a map of link
  1381 		 * Create link_category terms for link categories. Create a map of link
  1354 		 * cat IDs to link_category terms.
  1382 		 * category IDs to link_category terms.
  1355 		 */
  1383 		 */
  1356 		$link_cat_id_map  = array();
  1384 		$link_cat_id_map  = array();
  1357 		$default_link_cat = 0;
  1385 		$default_link_cat = 0;
  1358 		$tt_ids           = array();
  1386 		$tt_ids           = array();
  1359 		$link_cats        = $wpdb->get_results( 'SELECT cat_id, cat_name FROM ' . $wpdb->prefix . 'linkcategories' );
  1387 		$link_cats        = $wpdb->get_results( 'SELECT cat_id, cat_name FROM ' . $wpdb->prefix . 'linkcategories' );
  1363 			$name       = wp_slash( $category->cat_name );
  1391 			$name       = wp_slash( $category->cat_name );
  1364 			$slug       = sanitize_title( $name );
  1392 			$slug       = sanitize_title( $name );
  1365 			$term_group = 0;
  1393 			$term_group = 0;
  1366 
  1394 
  1367 			// Associate terms with the same slug in a term group and make slugs unique.
  1395 			// Associate terms with the same slug in a term group and make slugs unique.
  1368 			if ( $exists = $wpdb->get_results( $wpdb->prepare( "SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $slug ) ) ) {
  1396 			$exists = $wpdb->get_results( $wpdb->prepare( "SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $slug ) );
       
  1397 			if ( $exists ) {
  1369 				$term_group = $exists[0]->term_group;
  1398 				$term_group = $exists[0]->term_group;
  1370 				$term_id    = $exists[0]->term_id;
  1399 				$term_id    = $exists[0]->term_id;
  1371 			}
  1400 			}
  1372 
  1401 
  1373 			if ( empty( $term_id ) ) {
  1402 			if ( empty( $term_id ) ) {
  1389 				)
  1418 				)
  1390 			);
  1419 			);
  1391 			$tt_ids[ $term_id ] = (int) $wpdb->insert_id;
  1420 			$tt_ids[ $term_id ] = (int) $wpdb->insert_id;
  1392 		}
  1421 		}
  1393 
  1422 
  1394 		// Associate links to cats.
  1423 		// Associate links to categories.
  1395 		$links = $wpdb->get_results( "SELECT link_id, link_category FROM $wpdb->links" );
  1424 		$links = $wpdb->get_results( "SELECT link_id, link_category FROM $wpdb->links" );
  1396 		if ( ! empty( $links ) ) {
  1425 		if ( ! empty( $links ) ) {
  1397 			foreach ( $links as $link ) {
  1426 			foreach ( $links as $link ) {
  1398 				if ( 0 == $link->link_category ) {
  1427 				if ( 0 == $link->link_category ) {
  1399 					continue;
  1428 					continue;
  1438 			);
  1467 			);
  1439 		}
  1468 		}
  1440 	}
  1469 	}
  1441 
  1470 
  1442 	if ( $wp_current_db_version < 4772 ) {
  1471 	if ( $wp_current_db_version < 4772 ) {
  1443 		// Obsolete linkcategories table
  1472 		// Obsolete linkcategories table.
  1444 		$wpdb->query( 'DROP TABLE IF EXISTS ' . $wpdb->prefix . 'linkcategories' );
  1473 		$wpdb->query( 'DROP TABLE IF EXISTS ' . $wpdb->prefix . 'linkcategories' );
  1445 	}
  1474 	}
  1446 
  1475 
  1447 	// Recalculate all counts
  1476 	// Recalculate all counts.
  1448 	$terms = $wpdb->get_results( "SELECT term_taxonomy_id, taxonomy FROM $wpdb->term_taxonomy" );
  1477 	$terms = $wpdb->get_results( "SELECT term_taxonomy_id, taxonomy FROM $wpdb->term_taxonomy" );
  1449 	foreach ( (array) $terms as $term ) {
  1478 	foreach ( (array) $terms as $term ) {
  1450 		if ( ( 'post_tag' == $term->taxonomy ) || ( 'category' == $term->taxonomy ) ) {
  1479 		if ( 'post_tag' === $term->taxonomy || 'category' === $term->taxonomy ) {
  1451 			$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts WHERE $wpdb->posts.ID = $wpdb->term_relationships.object_id AND post_status = 'publish' AND post_type = 'post' AND term_taxonomy_id = %d", $term->term_taxonomy_id ) );
  1480 			$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts WHERE $wpdb->posts.ID = $wpdb->term_relationships.object_id AND post_status = 'publish' AND post_type = 'post' AND term_taxonomy_id = %d", $term->term_taxonomy_id ) );
  1452 		} else {
  1481 		} else {
  1453 			$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $term->term_taxonomy_id ) );
  1482 			$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $term->term_taxonomy_id ) );
  1454 		}
  1483 		}
  1455 		$wpdb->update( $wpdb->term_taxonomy, array( 'count' => $count ), array( 'term_taxonomy_id' => $term->term_taxonomy_id ) );
  1484 		$wpdb->update( $wpdb->term_taxonomy, array( 'count' => $count ), array( 'term_taxonomy_id' => $term->term_taxonomy_id ) );
  1507  * Execute changes made in WordPress 2.5.0.
  1536  * Execute changes made in WordPress 2.5.0.
  1508  *
  1537  *
  1509  * @ignore
  1538  * @ignore
  1510  * @since 2.5.0
  1539  * @since 2.5.0
  1511  *
  1540  *
  1512  * @global int $wp_current_db_version
  1541  * @global int $wp_current_db_version The old (current) database version.
  1513  */
  1542  */
  1514 function upgrade_250() {
  1543 function upgrade_250() {
  1515 	global $wp_current_db_version;
  1544 	global $wp_current_db_version;
  1516 
  1545 
  1517 	if ( $wp_current_db_version < 6689 ) {
  1546 	if ( $wp_current_db_version < 6689 ) {
  1538  * Execute changes made in WordPress 2.6.
  1567  * Execute changes made in WordPress 2.6.
  1539  *
  1568  *
  1540  * @ignore
  1569  * @ignore
  1541  * @since 2.6.0
  1570  * @since 2.6.0
  1542  *
  1571  *
  1543  * @global int $wp_current_db_version
  1572  * @global int $wp_current_db_version The old (current) database version.
  1544  */
  1573  */
  1545 function upgrade_260() {
  1574 function upgrade_260() {
  1546 	global $wp_current_db_version;
  1575 	global $wp_current_db_version;
  1547 
  1576 
  1548 	if ( $wp_current_db_version < 8000 ) {
  1577 	if ( $wp_current_db_version < 8000 ) {
  1554  * Execute changes made in WordPress 2.7.
  1583  * Execute changes made in WordPress 2.7.
  1555  *
  1584  *
  1556  * @ignore
  1585  * @ignore
  1557  * @since 2.7.0
  1586  * @since 2.7.0
  1558  *
  1587  *
  1559  * @global wpdb $wpdb WordPress database abstraction object.
  1588  * @global int  $wp_current_db_version The old (current) database version.
  1560  * @global int  $wp_current_db_version
  1589  * @global wpdb $wpdb                  WordPress database abstraction object.
  1561  */
  1590  */
  1562 function upgrade_270() {
  1591 function upgrade_270() {
  1563 	global $wpdb, $wp_current_db_version;
  1592 	global $wp_current_db_version, $wpdb;
  1564 
  1593 
  1565 	if ( $wp_current_db_version < 8980 ) {
  1594 	if ( $wp_current_db_version < 8980 ) {
  1566 		populate_roles_270();
  1595 		populate_roles_270();
  1567 	}
  1596 	}
  1568 
  1597 
  1569 	// Update post_date for unpublished posts with empty timestamp
  1598 	// Update post_date for unpublished posts with empty timestamp.
  1570 	if ( $wp_current_db_version < 8921 ) {
  1599 	if ( $wp_current_db_version < 8921 ) {
  1571 		$wpdb->query( "UPDATE $wpdb->posts SET post_date = post_modified WHERE post_date = '0000-00-00 00:00:00'" );
  1600 		$wpdb->query( "UPDATE $wpdb->posts SET post_date = post_modified WHERE post_date = '0000-00-00 00:00:00'" );
  1572 	}
  1601 	}
  1573 }
  1602 }
  1574 
  1603 
  1576  * Execute changes made in WordPress 2.8.
  1605  * Execute changes made in WordPress 2.8.
  1577  *
  1606  *
  1578  * @ignore
  1607  * @ignore
  1579  * @since 2.8.0
  1608  * @since 2.8.0
  1580  *
  1609  *
  1581  * @global int  $wp_current_db_version
  1610  * @global int  $wp_current_db_version The old (current) database version.
  1582  * @global wpdb $wpdb WordPress database abstraction object.
  1611  * @global wpdb $wpdb                  WordPress database abstraction object.
  1583  */
  1612  */
  1584 function upgrade_280() {
  1613 function upgrade_280() {
  1585 	global $wp_current_db_version, $wpdb;
  1614 	global $wp_current_db_version, $wpdb;
  1586 
  1615 
  1587 	if ( $wp_current_db_version < 10360 ) {
  1616 	if ( $wp_current_db_version < 10360 ) {
  1609  * Execute changes made in WordPress 2.9.
  1638  * Execute changes made in WordPress 2.9.
  1610  *
  1639  *
  1611  * @ignore
  1640  * @ignore
  1612  * @since 2.9.0
  1641  * @since 2.9.0
  1613  *
  1642  *
  1614  * @global int $wp_current_db_version
  1643  * @global int $wp_current_db_version The old (current) database version.
  1615  */
  1644  */
  1616 function upgrade_290() {
  1645 function upgrade_290() {
  1617 	global $wp_current_db_version;
  1646 	global $wp_current_db_version;
  1618 
  1647 
  1619 	if ( $wp_current_db_version < 11958 ) {
  1648 	if ( $wp_current_db_version < 11958 ) {
  1620 		// Previously, setting depth to 1 would redundantly disable threading, but now 2 is the minimum depth to avoid confusion
  1649 		// Previously, setting depth to 1 would redundantly disable threading,
       
  1650 		// but now 2 is the minimum depth to avoid confusion.
  1621 		if ( get_option( 'thread_comments_depth' ) == '1' ) {
  1651 		if ( get_option( 'thread_comments_depth' ) == '1' ) {
  1622 			update_option( 'thread_comments_depth', 2 );
  1652 			update_option( 'thread_comments_depth', 2 );
  1623 			update_option( 'thread_comments', 0 );
  1653 			update_option( 'thread_comments', 0 );
  1624 		}
  1654 		}
  1625 	}
  1655 	}
  1629  * Execute changes made in WordPress 3.0.
  1659  * Execute changes made in WordPress 3.0.
  1630  *
  1660  *
  1631  * @ignore
  1661  * @ignore
  1632  * @since 3.0.0
  1662  * @since 3.0.0
  1633  *
  1663  *
  1634  * @global int  $wp_current_db_version
  1664  * @global int  $wp_current_db_version The old (current) database version.
  1635  * @global wpdb $wpdb WordPress database abstraction object.
  1665  * @global wpdb $wpdb                  WordPress database abstraction object.
  1636  */
  1666  */
  1637 function upgrade_300() {
  1667 function upgrade_300() {
  1638 	global $wp_current_db_version, $wpdb;
  1668 	global $wp_current_db_version, $wpdb;
  1639 
  1669 
  1640 	if ( $wp_current_db_version < 15093 ) {
  1670 	if ( $wp_current_db_version < 15093 ) {
  1680  * Execute changes made in WordPress 3.3.
  1710  * Execute changes made in WordPress 3.3.
  1681  *
  1711  *
  1682  * @ignore
  1712  * @ignore
  1683  * @since 3.3.0
  1713  * @since 3.3.0
  1684  *
  1714  *
  1685  * @global int   $wp_current_db_version
  1715  * @global int   $wp_current_db_version The old (current) database version.
  1686  * @global wpdb  $wpdb
  1716  * @global wpdb  $wpdb                  WordPress database abstraction object.
  1687  * @global array $wp_registered_widgets
  1717  * @global array $wp_registered_widgets
  1688  * @global array $sidebars_widgets
  1718  * @global array $sidebars_widgets
  1689  */
  1719  */
  1690 function upgrade_330() {
  1720 function upgrade_330() {
  1691 	global $wp_current_db_version, $wpdb, $wp_registered_widgets, $sidebars_widgets;
  1721 	global $wp_current_db_version, $wpdb, $wp_registered_widgets, $sidebars_widgets;
  1747 			}
  1777 			}
  1748 			$_sidebars_widgets['array_version'] = 2;
  1778 			$_sidebars_widgets['array_version'] = 2;
  1749 			$sidebars_widgets                   = $_sidebars_widgets;
  1779 			$sidebars_widgets                   = $_sidebars_widgets;
  1750 			unset( $_sidebars_widgets );
  1780 			unset( $_sidebars_widgets );
  1751 
  1781 
  1752 			// intentional fall-through to upgrade to the next version.
  1782 			// Intentional fall-through to upgrade to the next version.
  1753 		case 2:
  1783 		case 2:
  1754 			$sidebars_widgets                  = retrieve_widgets();
  1784 			$sidebars_widgets                  = retrieve_widgets();
  1755 			$sidebars_widgets['array_version'] = 3;
  1785 			$sidebars_widgets['array_version'] = 3;
  1756 			update_option( 'sidebars_widgets', $sidebars_widgets );
  1786 			update_option( 'sidebars_widgets', $sidebars_widgets );
  1757 	}
  1787 	}
  1761  * Execute changes made in WordPress 3.4.
  1791  * Execute changes made in WordPress 3.4.
  1762  *
  1792  *
  1763  * @ignore
  1793  * @ignore
  1764  * @since 3.4.0
  1794  * @since 3.4.0
  1765  *
  1795  *
  1766  * @global int   $wp_current_db_version
  1796  * @global int  $wp_current_db_version The old (current) database version.
  1767  * @global wpdb  $wpdb
  1797  * @global wpdb $wpdb                  WordPress database abstraction object.
  1768  */
  1798  */
  1769 function upgrade_340() {
  1799 function upgrade_340() {
  1770 	global $wp_current_db_version, $wpdb;
  1800 	global $wp_current_db_version, $wpdb;
  1771 
  1801 
  1772 	if ( $wp_current_db_version < 19798 ) {
  1802 	if ( $wp_current_db_version < 19798 ) {
  1784 	if ( $wp_current_db_version < 20022 && wp_should_upgrade_global_tables() ) {
  1814 	if ( $wp_current_db_version < 20022 && wp_should_upgrade_global_tables() ) {
  1785 		$wpdb->query( "DELETE FROM $wpdb->usermeta WHERE meta_key = 'themes_last_view'" );
  1815 		$wpdb->query( "DELETE FROM $wpdb->usermeta WHERE meta_key = 'themes_last_view'" );
  1786 	}
  1816 	}
  1787 
  1817 
  1788 	if ( $wp_current_db_version < 20080 ) {
  1818 	if ( $wp_current_db_version < 20080 ) {
  1789 		if ( 'yes' == $wpdb->get_var( "SELECT autoload FROM $wpdb->options WHERE option_name = 'uninstall_plugins'" ) ) {
  1819 		if ( 'yes' === $wpdb->get_var( "SELECT autoload FROM $wpdb->options WHERE option_name = 'uninstall_plugins'" ) ) {
  1790 			$uninstall_plugins = get_option( 'uninstall_plugins' );
  1820 			$uninstall_plugins = get_option( 'uninstall_plugins' );
  1791 			delete_option( 'uninstall_plugins' );
  1821 			delete_option( 'uninstall_plugins' );
  1792 			add_option( 'uninstall_plugins', $uninstall_plugins, null, 'no' );
  1822 			add_option( 'uninstall_plugins', $uninstall_plugins, null, 'no' );
  1793 		}
  1823 		}
  1794 	}
  1824 	}
  1798  * Execute changes made in WordPress 3.5.
  1828  * Execute changes made in WordPress 3.5.
  1799  *
  1829  *
  1800  * @ignore
  1830  * @ignore
  1801  * @since 3.5.0
  1831  * @since 3.5.0
  1802  *
  1832  *
  1803  * @global int   $wp_current_db_version
  1833  * @global int  $wp_current_db_version The old (current) database version.
  1804  * @global wpdb  $wpdb
  1834  * @global wpdb $wpdb                  WordPress database abstraction object.
  1805  */
  1835  */
  1806 function upgrade_350() {
  1836 function upgrade_350() {
  1807 	global $wp_current_db_version, $wpdb;
  1837 	global $wp_current_db_version, $wpdb;
  1808 
  1838 
  1809 	if ( $wp_current_db_version < 22006 && $wpdb->get_var( "SELECT link_id FROM $wpdb->links LIMIT 1" ) ) {
  1839 	if ( $wp_current_db_version < 22006 && $wpdb->get_var( "SELECT link_id FROM $wpdb->links LIMIT 1" ) ) {
  1810 		update_option( 'link_manager_enabled', 1 ); // Previously set to 0 by populate_options()
  1840 		update_option( 'link_manager_enabled', 1 ); // Previously set to 0 by populate_options().
  1811 	}
  1841 	}
  1812 
  1842 
  1813 	if ( $wp_current_db_version < 21811 && wp_should_upgrade_global_tables() ) {
  1843 	if ( $wp_current_db_version < 21811 && wp_should_upgrade_global_tables() ) {
  1814 		$meta_keys = array();
  1844 		$meta_keys = array();
  1815 		foreach ( array_merge( get_post_types(), get_taxonomies() ) as $name ) {
  1845 		foreach ( array_merge( get_post_types(), get_taxonomies() ) as $name ) {
  1821 			$meta_keys = implode( "', '", $meta_keys );
  1851 			$meta_keys = implode( "', '", $meta_keys );
  1822 			$wpdb->query( "DELETE FROM $wpdb->usermeta WHERE meta_key IN ('$meta_keys')" );
  1852 			$wpdb->query( "DELETE FROM $wpdb->usermeta WHERE meta_key IN ('$meta_keys')" );
  1823 		}
  1853 		}
  1824 	}
  1854 	}
  1825 
  1855 
  1826 	if ( $wp_current_db_version < 22422 && $term = get_term_by( 'slug', 'post-format-standard', 'post_format' ) ) {
  1856 	if ( $wp_current_db_version < 22422 ) {
  1827 		wp_delete_term( $term->term_id, 'post_format' );
  1857 		$term = get_term_by( 'slug', 'post-format-standard', 'post_format' );
       
  1858 		if ( $term ) {
       
  1859 			wp_delete_term( $term->term_id, 'post_format' );
       
  1860 		}
  1828 	}
  1861 	}
  1829 }
  1862 }
  1830 
  1863 
  1831 /**
  1864 /**
  1832  * Execute changes made in WordPress 3.7.
  1865  * Execute changes made in WordPress 3.7.
  1833  *
  1866  *
  1834  * @ignore
  1867  * @ignore
  1835  * @since 3.7.0
  1868  * @since 3.7.0
  1836  *
  1869  *
  1837  * @global int $wp_current_db_version
  1870  * @global int $wp_current_db_version The old (current) database version.
  1838  */
  1871  */
  1839 function upgrade_370() {
  1872 function upgrade_370() {
  1840 	global $wp_current_db_version;
  1873 	global $wp_current_db_version;
       
  1874 
  1841 	if ( $wp_current_db_version < 25824 ) {
  1875 	if ( $wp_current_db_version < 25824 ) {
  1842 		wp_clear_scheduled_hook( 'wp_auto_updates_maybe_update' );
  1876 		wp_clear_scheduled_hook( 'wp_auto_updates_maybe_update' );
  1843 	}
  1877 	}
  1844 }
  1878 }
  1845 
  1879 
  1848  *
  1882  *
  1849  * @ignore
  1883  * @ignore
  1850  * @since 3.7.2
  1884  * @since 3.7.2
  1851  * @since 3.8.0
  1885  * @since 3.8.0
  1852  *
  1886  *
  1853  * @global int $wp_current_db_version
  1887  * @global int $wp_current_db_version The old (current) database version.
  1854  */
  1888  */
  1855 function upgrade_372() {
  1889 function upgrade_372() {
  1856 	global $wp_current_db_version;
  1890 	global $wp_current_db_version;
       
  1891 
  1857 	if ( $wp_current_db_version < 26148 ) {
  1892 	if ( $wp_current_db_version < 26148 ) {
  1858 		wp_clear_scheduled_hook( 'wp_maybe_auto_update' );
  1893 		wp_clear_scheduled_hook( 'wp_maybe_auto_update' );
  1859 	}
  1894 	}
  1860 }
  1895 }
  1861 
  1896 
  1863  * Execute changes made in WordPress 3.8.0.
  1898  * Execute changes made in WordPress 3.8.0.
  1864  *
  1899  *
  1865  * @ignore
  1900  * @ignore
  1866  * @since 3.8.0
  1901  * @since 3.8.0
  1867  *
  1902  *
  1868  * @global int $wp_current_db_version
  1903  * @global int $wp_current_db_version The old (current) database version.
  1869  */
  1904  */
  1870 function upgrade_380() {
  1905 function upgrade_380() {
  1871 	global $wp_current_db_version;
  1906 	global $wp_current_db_version;
       
  1907 
  1872 	if ( $wp_current_db_version < 26691 ) {
  1908 	if ( $wp_current_db_version < 26691 ) {
  1873 		deactivate_plugins( array( 'mp6/mp6.php' ), true );
  1909 		deactivate_plugins( array( 'mp6/mp6.php' ), true );
  1874 	}
  1910 	}
  1875 }
  1911 }
  1876 
  1912 
  1878  * Execute changes made in WordPress 4.0.0.
  1914  * Execute changes made in WordPress 4.0.0.
  1879  *
  1915  *
  1880  * @ignore
  1916  * @ignore
  1881  * @since 4.0.0
  1917  * @since 4.0.0
  1882  *
  1918  *
  1883  * @global int $wp_current_db_version
  1919  * @global int $wp_current_db_version The old (current) database version.
  1884  */
  1920  */
  1885 function upgrade_400() {
  1921 function upgrade_400() {
  1886 	global $wp_current_db_version;
  1922 	global $wp_current_db_version;
       
  1923 
  1887 	if ( $wp_current_db_version < 29630 ) {
  1924 	if ( $wp_current_db_version < 29630 ) {
  1888 		if ( ! is_multisite() && false === get_option( 'WPLANG' ) ) {
  1925 		if ( ! is_multisite() && false === get_option( 'WPLANG' ) ) {
  1889 			if ( defined( 'WPLANG' ) && ( '' !== WPLANG ) && in_array( WPLANG, get_available_languages() ) ) {
  1926 			if ( defined( 'WPLANG' ) && ( '' !== WPLANG ) && in_array( WPLANG, get_available_languages(), true ) ) {
  1890 				update_option( 'WPLANG', WPLANG );
  1927 				update_option( 'WPLANG', WPLANG );
  1891 			} else {
  1928 			} else {
  1892 				update_option( 'WPLANG', '' );
  1929 				update_option( 'WPLANG', '' );
  1893 			}
  1930 			}
  1894 		}
  1931 		}
  1898 /**
  1935 /**
  1899  * Execute changes made in WordPress 4.2.0.
  1936  * Execute changes made in WordPress 4.2.0.
  1900  *
  1937  *
  1901  * @ignore
  1938  * @ignore
  1902  * @since 4.2.0
  1939  * @since 4.2.0
  1903  *
       
  1904  * @global int   $wp_current_db_version
       
  1905  * @global wpdb  $wpdb
       
  1906  */
  1940  */
  1907 function upgrade_420() {}
  1941 function upgrade_420() {}
  1908 
  1942 
  1909 /**
  1943 /**
  1910  * Executes changes made in WordPress 4.3.0.
  1944  * Executes changes made in WordPress 4.3.0.
  1911  *
  1945  *
  1912  * @ignore
  1946  * @ignore
  1913  * @since 4.3.0
  1947  * @since 4.3.0
  1914  *
  1948  *
  1915  * @global int  $wp_current_db_version Current version.
  1949  * @global int  $wp_current_db_version The old (current) database version.
  1916  * @global wpdb $wpdb                  WordPress database abstraction object.
  1950  * @global wpdb $wpdb                  WordPress database abstraction object.
  1917  */
  1951  */
  1918 function upgrade_430() {
  1952 function upgrade_430() {
  1919 	global $wp_current_db_version, $wpdb;
  1953 	global $wp_current_db_version, $wpdb;
  1920 
  1954 
  1949  * Executes comments changes made in WordPress 4.3.0.
  1983  * Executes comments changes made in WordPress 4.3.0.
  1950  *
  1984  *
  1951  * @ignore
  1985  * @ignore
  1952  * @since 4.3.0
  1986  * @since 4.3.0
  1953  *
  1987  *
  1954  * @global int  $wp_current_db_version Current version.
  1988  * @global wpdb $wpdb WordPress database abstraction object.
  1955  * @global wpdb $wpdb                  WordPress database abstraction object.
       
  1956  */
  1989  */
  1957 function upgrade_430_fix_comments() {
  1990 function upgrade_430_fix_comments() {
  1958 	global $wp_current_db_version, $wpdb;
  1991 	global $wpdb;
  1959 
  1992 
  1960 	$content_length = $wpdb->get_col_length( $wpdb->comments, 'comment_content' );
  1993 	$content_length = $wpdb->get_col_length( $wpdb->comments, 'comment_content' );
  1961 
  1994 
  1962 	if ( is_wp_error( $content_length ) ) {
  1995 	if ( is_wp_error( $content_length ) ) {
  1963 		return;
  1996 		return;
  2000  *
  2033  *
  2001  * @ignore
  2034  * @ignore
  2002  * @since 4.3.1
  2035  * @since 4.3.1
  2003  */
  2036  */
  2004 function upgrade_431() {
  2037 function upgrade_431() {
  2005 	// Fix incorrect cron entries for term splitting
  2038 	// Fix incorrect cron entries for term splitting.
  2006 	$cron_array = _get_cron_array();
  2039 	$cron_array = _get_cron_array();
  2007 	if ( isset( $cron_array['wp_batch_split_terms'] ) ) {
  2040 	if ( isset( $cron_array['wp_batch_split_terms'] ) ) {
  2008 		unset( $cron_array['wp_batch_split_terms'] );
  2041 		unset( $cron_array['wp_batch_split_terms'] );
  2009 		_set_cron_array( $cron_array );
  2042 		_set_cron_array( $cron_array );
  2010 	}
  2043 	}
  2014  * Executes changes made in WordPress 4.4.0.
  2047  * Executes changes made in WordPress 4.4.0.
  2015  *
  2048  *
  2016  * @ignore
  2049  * @ignore
  2017  * @since 4.4.0
  2050  * @since 4.4.0
  2018  *
  2051  *
  2019  * @global int  $wp_current_db_version Current version.
  2052  * @global int  $wp_current_db_version The old (current) database version.
  2020  * @global wpdb $wpdb                  WordPress database abstraction object.
  2053  * @global wpdb $wpdb                  WordPress database abstraction object.
  2021  */
  2054  */
  2022 function upgrade_440() {
  2055 function upgrade_440() {
  2023 	global $wp_current_db_version, $wpdb;
  2056 	global $wp_current_db_version, $wpdb;
  2024 
  2057 
  2039  * Executes changes made in WordPress 4.5.0.
  2072  * Executes changes made in WordPress 4.5.0.
  2040  *
  2073  *
  2041  * @ignore
  2074  * @ignore
  2042  * @since 4.5.0
  2075  * @since 4.5.0
  2043  *
  2076  *
  2044  * @global int  $wp_current_db_version Current database version.
  2077  * @global int  $wp_current_db_version The old (current) database version.
  2045  * @global wpdb $wpdb                  WordPress database abstraction object.
  2078  * @global wpdb $wpdb                  WordPress database abstraction object.
  2046  */
  2079  */
  2047 function upgrade_450() {
  2080 function upgrade_450() {
  2048 	global $wp_current_db_version, $wpdb;
  2081 	global $wp_current_db_version, $wpdb;
  2049 
  2082 
  2064  * Executes changes made in WordPress 4.6.0.
  2097  * Executes changes made in WordPress 4.6.0.
  2065  *
  2098  *
  2066  * @ignore
  2099  * @ignore
  2067  * @since 4.6.0
  2100  * @since 4.6.0
  2068  *
  2101  *
  2069  * @global int $wp_current_db_version Current database version.
  2102  * @global int $wp_current_db_version The old (current) database version.
  2070  */
  2103  */
  2071 function upgrade_460() {
  2104 function upgrade_460() {
  2072 	global $wp_current_db_version;
  2105 	global $wp_current_db_version;
  2073 
  2106 
  2074 	// Remove unused post meta.
  2107 	// Remove unused post meta.
  2111 function upgrade_510() {
  2144 function upgrade_510() {
  2112 	delete_site_option( 'upgrade_500_was_gutenberg_active' );
  2145 	delete_site_option( 'upgrade_500_was_gutenberg_active' );
  2113 }
  2146 }
  2114 
  2147 
  2115 /**
  2148 /**
       
  2149  * Executes changes made in WordPress 5.3.0.
       
  2150  *
       
  2151  * @ignore
       
  2152  * @since 5.3.0
       
  2153  */
       
  2154 function upgrade_530() {
       
  2155 	/*
       
  2156 	 * The `admin_email_lifespan` option may have been set by an admin that just logged in,
       
  2157 	 * saw the verification screen, clicked on a button there, and is now upgrading the db,
       
  2158 	 * or by populate_options() that is called earlier in upgrade_all().
       
  2159 	 * In the second case `admin_email_lifespan` should be reset so the verification screen
       
  2160 	 * is shown next time an admin logs in.
       
  2161 	 */
       
  2162 	if ( function_exists( 'current_user_can' ) && ! current_user_can( 'manage_options' ) ) {
       
  2163 		update_option( 'admin_email_lifespan', 0 );
       
  2164 	}
       
  2165 }
       
  2166 
       
  2167 /**
       
  2168  * Executes changes made in WordPress 5.5.0.
       
  2169  *
       
  2170  * @ignore
       
  2171  * @since 5.5.0
       
  2172  */
       
  2173 function upgrade_550() {
       
  2174 	global $wp_current_db_version;
       
  2175 
       
  2176 	if ( $wp_current_db_version < 48121 ) {
       
  2177 		$comment_previously_approved = get_option( 'comment_whitelist', '' );
       
  2178 		update_option( 'comment_previously_approved', $comment_previously_approved );
       
  2179 		delete_option( 'comment_whitelist' );
       
  2180 	}
       
  2181 
       
  2182 	if ( $wp_current_db_version < 48575 ) {
       
  2183 		// Use more clear and inclusive language.
       
  2184 		$disallowed_list = get_option( 'blacklist_keys' );
       
  2185 
       
  2186 		/*
       
  2187 		 * This option key was briefly renamed `blocklist_keys`.
       
  2188 		 * Account for sites that have this key present when the original key does not exist.
       
  2189 		 */
       
  2190 		if ( false === $disallowed_list ) {
       
  2191 			$disallowed_list = get_option( 'blocklist_keys' );
       
  2192 		}
       
  2193 
       
  2194 		update_option( 'disallowed_keys', $disallowed_list );
       
  2195 		delete_option( 'blacklist_keys' );
       
  2196 		delete_option( 'blocklist_keys' );
       
  2197 	}
       
  2198 
       
  2199 	if ( $wp_current_db_version < 48748 ) {
       
  2200 		update_option( 'finished_updating_comment_type', 0 );
       
  2201 		wp_schedule_single_event( time() + ( 1 * MINUTE_IN_SECONDS ), 'wp_update_comment_type_batch' );
       
  2202 	}
       
  2203 }
       
  2204 
       
  2205 /**
  2116  * Executes network-level upgrade routines.
  2206  * Executes network-level upgrade routines.
  2117  *
  2207  *
  2118  * @since 3.0.0
  2208  * @since 3.0.0
  2119  *
  2209  *
  2120  * @global int   $wp_current_db_version
  2210  * @global int  $wp_current_db_version The old (current) database version.
  2121  * @global wpdb  $wpdb
  2211  * @global wpdb $wpdb                  WordPress database abstraction object.
  2122  */
  2212  */
  2123 function upgrade_network() {
  2213 function upgrade_network() {
  2124 	global $wp_current_db_version, $wpdb;
  2214 	global $wp_current_db_version, $wpdb;
  2125 
  2215 
  2126 	// Always clear expired transients
  2216 	// Always clear expired transients.
  2127 	delete_expired_transients( true );
  2217 	delete_expired_transients( true );
  2128 
  2218 
  2129 	// 2.8.
  2219 	// 2.8.0
  2130 	if ( $wp_current_db_version < 11549 ) {
  2220 	if ( $wp_current_db_version < 11549 ) {
  2131 		$wpmu_sitewide_plugins   = get_site_option( 'wpmu_sitewide_plugins' );
  2221 		$wpmu_sitewide_plugins   = get_site_option( 'wpmu_sitewide_plugins' );
  2132 		$active_sitewide_plugins = get_site_option( 'active_sitewide_plugins' );
  2222 		$active_sitewide_plugins = get_site_option( 'active_sitewide_plugins' );
  2133 		if ( $wpmu_sitewide_plugins ) {
  2223 		if ( $wpmu_sitewide_plugins ) {
  2134 			if ( ! $active_sitewide_plugins ) {
  2224 			if ( ! $active_sitewide_plugins ) {
  2155 			}
  2245 			}
  2156 			$start += 20;
  2246 			$start += 20;
  2157 		}
  2247 		}
  2158 	}
  2248 	}
  2159 
  2249 
  2160 	// 3.0
  2250 	// 3.0.0
  2161 	if ( $wp_current_db_version < 13576 ) {
  2251 	if ( $wp_current_db_version < 13576 ) {
  2162 		update_site_option( 'global_terms_enabled', '1' );
  2252 		update_site_option( 'global_terms_enabled', '1' );
  2163 	}
  2253 	}
  2164 
  2254 
  2165 	// 3.3
  2255 	// 3.3.0
  2166 	if ( $wp_current_db_version < 19390 ) {
  2256 	if ( $wp_current_db_version < 19390 ) {
  2167 		update_site_option( 'initial_db_version', $wp_current_db_version );
  2257 		update_site_option( 'initial_db_version', $wp_current_db_version );
  2168 	}
  2258 	}
  2169 
  2259 
  2170 	if ( $wp_current_db_version < 19470 ) {
  2260 	if ( $wp_current_db_version < 19470 ) {
  2171 		if ( false === get_site_option( 'active_sitewide_plugins' ) ) {
  2261 		if ( false === get_site_option( 'active_sitewide_plugins' ) ) {
  2172 			update_site_option( 'active_sitewide_plugins', array() );
  2262 			update_site_option( 'active_sitewide_plugins', array() );
  2173 		}
  2263 		}
  2174 	}
  2264 	}
  2175 
  2265 
  2176 	// 3.4
  2266 	// 3.4.0
  2177 	if ( $wp_current_db_version < 20148 ) {
  2267 	if ( $wp_current_db_version < 20148 ) {
  2178 		// 'allowedthemes' keys things by stylesheet. 'allowed_themes' keyed things by name.
  2268 		// 'allowedthemes' keys things by stylesheet. 'allowed_themes' keyed things by name.
  2179 		$allowedthemes  = get_site_option( 'allowedthemes' );
  2269 		$allowedthemes  = get_site_option( 'allowedthemes' );
  2180 		$allowed_themes = get_site_option( 'allowed_themes' );
  2270 		$allowed_themes = get_site_option( 'allowed_themes' );
  2181 		if ( false === $allowedthemes && is_array( $allowed_themes ) && $allowed_themes ) {
  2271 		if ( false === $allowedthemes && is_array( $allowed_themes ) && $allowed_themes ) {
  2189 			update_site_option( 'allowedthemes', $converted );
  2279 			update_site_option( 'allowedthemes', $converted );
  2190 			delete_site_option( 'allowed_themes' );
  2280 			delete_site_option( 'allowed_themes' );
  2191 		}
  2281 		}
  2192 	}
  2282 	}
  2193 
  2283 
  2194 	// 3.5
  2284 	// 3.5.0
  2195 	if ( $wp_current_db_version < 21823 ) {
  2285 	if ( $wp_current_db_version < 21823 ) {
  2196 		update_site_option( 'ms_files_rewriting', '1' );
  2286 		update_site_option( 'ms_files_rewriting', '1' );
  2197 	}
  2287 	}
  2198 
  2288 
  2199 	// 3.5.2
  2289 	// 3.5.2
  2204 			$illegal_names = explode( ' ', $illegal_name );
  2294 			$illegal_names = explode( ' ', $illegal_name );
  2205 			update_site_option( 'illegal_names', $illegal_names );
  2295 			update_site_option( 'illegal_names', $illegal_names );
  2206 		}
  2296 		}
  2207 	}
  2297 	}
  2208 
  2298 
  2209 	// 4.2
  2299 	// 4.2.0
  2210 	if ( $wp_current_db_version < 31351 && $wpdb->charset === 'utf8mb4' ) {
  2300 	if ( $wp_current_db_version < 31351 && 'utf8mb4' === $wpdb->charset ) {
  2211 		if ( wp_should_upgrade_global_tables() ) {
  2301 		if ( wp_should_upgrade_global_tables() ) {
  2212 			$wpdb->query( "ALTER TABLE $wpdb->usermeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" );
  2302 			$wpdb->query( "ALTER TABLE $wpdb->usermeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" );
  2213 			$wpdb->query( "ALTER TABLE $wpdb->site DROP INDEX domain, ADD INDEX domain(domain(140),path(51))" );
  2303 			$wpdb->query( "ALTER TABLE $wpdb->site DROP INDEX domain, ADD INDEX domain(domain(140),path(51))" );
  2214 			$wpdb->query( "ALTER TABLE $wpdb->sitemeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" );
  2304 			$wpdb->query( "ALTER TABLE $wpdb->sitemeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" );
  2215 			$wpdb->query( "ALTER TABLE $wpdb->signups DROP INDEX domain_path, ADD INDEX domain_path(domain(140),path(51))" );
  2305 			$wpdb->query( "ALTER TABLE $wpdb->signups DROP INDEX domain_path, ADD INDEX domain_path(domain(140),path(51))" );
  2225 				maybe_convert_table_to_utf8mb4( $table );
  2315 				maybe_convert_table_to_utf8mb4( $table );
  2226 			}
  2316 			}
  2227 		}
  2317 		}
  2228 	}
  2318 	}
  2229 
  2319 
  2230 	// 4.3
  2320 	// 4.3.0
  2231 	if ( $wp_current_db_version < 33055 && 'utf8mb4' === $wpdb->charset ) {
  2321 	if ( $wp_current_db_version < 33055 && 'utf8mb4' === $wpdb->charset ) {
  2232 		if ( wp_should_upgrade_global_tables() ) {
  2322 		if ( wp_should_upgrade_global_tables() ) {
  2233 			$upgrade = false;
  2323 			$upgrade = false;
  2234 			$indexes = $wpdb->get_results( "SHOW INDEXES FROM $wpdb->signups" );
  2324 			$indexes = $wpdb->get_results( "SHOW INDEXES FROM $wpdb->signups" );
  2235 			foreach ( $indexes as $index ) {
  2325 			foreach ( $indexes as $index ) {
  2236 				if ( 'domain_path' == $index->Key_name && 'domain' == $index->Column_name && 140 != $index->Sub_part ) {
  2326 				if ( 'domain_path' === $index->Key_name && 'domain' === $index->Column_name && 140 != $index->Sub_part ) {
  2237 					$upgrade = true;
  2327 					$upgrade = true;
  2238 					break;
  2328 					break;
  2239 				}
  2329 				}
  2240 			}
  2330 			}
  2241 
  2331 
  2254 				maybe_convert_table_to_utf8mb4( $table );
  2344 				maybe_convert_table_to_utf8mb4( $table );
  2255 			}
  2345 			}
  2256 		}
  2346 		}
  2257 	}
  2347 	}
  2258 
  2348 
  2259 	// 5.1
  2349 	// 5.1.0
  2260 	if ( $wp_current_db_version < 44467 ) {
  2350 	if ( $wp_current_db_version < 44467 ) {
  2261 		$network_id = get_main_network_id();
  2351 		$network_id = get_main_network_id();
  2262 		delete_network_option( $network_id, 'site_meta_supported' );
  2352 		delete_network_option( $network_id, 'site_meta_supported' );
  2263 		is_site_meta_supported();
  2353 		is_site_meta_supported();
  2264 	}
  2354 	}
  2265 }
  2355 }
  2266 
  2356 
  2267 //
  2357 //
  2268 // General functions we use to actually do stuff
  2358 // General functions we use to actually do stuff.
  2269 //
  2359 //
  2270 
  2360 
  2271 /**
  2361 /**
  2272  * Creates a table in the database if it doesn't already exist.
  2362  * Creates a table in the database, if it doesn't already exist.
  2273  *
  2363  *
  2274  * This method checks for an existing database and creates a new one if it's not
  2364  * This method checks for an existing database and creates a new one if it's not
  2275  * already present. It doesn't rely on MySQL's "IF NOT EXISTS" statement, but chooses
  2365  * already present. It doesn't rely on MySQL's "IF NOT EXISTS" statement, but chooses
  2276  * to query all tables first and then run the SQL statement creating the table.
  2366  * to query all tables first and then run the SQL statement creating the table.
  2277  *
  2367  *
  2278  * @since 1.0.0
  2368  * @since 1.0.0
  2279  *
  2369  *
  2280  * @global wpdb  $wpdb
  2370  * @global wpdb $wpdb WordPress database abstraction object.
  2281  *
  2371  *
  2282  * @param string $table_name Database table name to create.
  2372  * @param string $table_name Database table name.
  2283  * @param string $create_ddl SQL statement to create table.
  2373  * @param string $create_ddl SQL statement to create table.
  2284  * @return bool If table already exists or was created by function.
  2374  * @return bool True on success or if the table already exists. False on failure.
  2285  */
  2375  */
  2286 function maybe_create_table( $table_name, $create_ddl ) {
  2376 function maybe_create_table( $table_name, $create_ddl ) {
  2287 	global $wpdb;
  2377 	global $wpdb;
  2288 
  2378 
  2289 	$query = $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $table_name ) );
  2379 	$query = $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $table_name ) );
  2290 
  2380 
  2291 	if ( $wpdb->get_var( $query ) == $table_name ) {
  2381 	if ( $wpdb->get_var( $query ) === $table_name ) {
  2292 		return true;
  2382 		return true;
  2293 	}
  2383 	}
  2294 
  2384 
  2295 	// Didn't find it try to create it..
  2385 	// Didn't find it, so try to create it.
  2296 	$wpdb->query( $create_ddl );
  2386 	$wpdb->query( $create_ddl );
  2297 
  2387 
  2298 	// We cannot directly tell that whether this succeeded!
  2388 	// We cannot directly tell that whether this succeeded!
  2299 	if ( $wpdb->get_var( $query ) == $table_name ) {
  2389 	if ( $wpdb->get_var( $query ) === $table_name ) {
  2300 		return true;
  2390 		return true;
  2301 	}
  2391 	}
       
  2392 
  2302 	return false;
  2393 	return false;
  2303 }
  2394 }
  2304 
  2395 
  2305 /**
  2396 /**
  2306  * Drops a specified index from a table.
  2397  * Drops a specified index from a table.
  2307  *
  2398  *
  2308  * @since 1.0.1
  2399  * @since 1.0.1
  2309  *
  2400  *
  2310  * @global wpdb  $wpdb
  2401  * @global wpdb $wpdb WordPress database abstraction object.
  2311  *
  2402  *
  2312  * @param string $table Database table name.
  2403  * @param string $table Database table name.
  2313  * @param string $index Index name to drop.
  2404  * @param string $index Index name to drop.
  2314  * @return true True, when finished.
  2405  * @return true True, when finished.
  2315  */
  2406  */
  2316 function drop_index( $table, $index ) {
  2407 function drop_index( $table, $index ) {
  2317 	global $wpdb;
  2408 	global $wpdb;
       
  2409 
  2318 	$wpdb->hide_errors();
  2410 	$wpdb->hide_errors();
       
  2411 
  2319 	$wpdb->query( "ALTER TABLE `$table` DROP INDEX `$index`" );
  2412 	$wpdb->query( "ALTER TABLE `$table` DROP INDEX `$index`" );
  2320 	// Now we need to take out all the extra ones we may have created
  2413 
       
  2414 	// Now we need to take out all the extra ones we may have created.
  2321 	for ( $i = 0; $i < 25; $i++ ) {
  2415 	for ( $i = 0; $i < 25; $i++ ) {
  2322 		$wpdb->query( "ALTER TABLE `$table` DROP INDEX `{$index}_$i`" );
  2416 		$wpdb->query( "ALTER TABLE `$table` DROP INDEX `{$index}_$i`" );
  2323 	}
  2417 	}
       
  2418 
  2324 	$wpdb->show_errors();
  2419 	$wpdb->show_errors();
       
  2420 
  2325 	return true;
  2421 	return true;
  2326 }
  2422 }
  2327 
  2423 
  2328 /**
  2424 /**
  2329  * Adds an index to a specified table.
  2425  * Adds an index to a specified table.
  2330  *
  2426  *
  2331  * @since 1.0.1
  2427  * @since 1.0.1
  2332  *
  2428  *
  2333  * @global wpdb  $wpdb
  2429  * @global wpdb $wpdb WordPress database abstraction object.
  2334  *
  2430  *
  2335  * @param string $table Database table name.
  2431  * @param string $table Database table name.
  2336  * @param string $index Database table index column.
  2432  * @param string $index Database table index column.
  2337  * @return true True, when done with execution.
  2433  * @return true True, when done with execution.
  2338  */
  2434  */
  2339 function add_clean_index( $table, $index ) {
  2435 function add_clean_index( $table, $index ) {
  2340 	global $wpdb;
  2436 	global $wpdb;
       
  2437 
  2341 	drop_index( $table, $index );
  2438 	drop_index( $table, $index );
  2342 	$wpdb->query( "ALTER TABLE `$table` ADD INDEX ( `$index` )" );
  2439 	$wpdb->query( "ALTER TABLE `$table` ADD INDEX ( `$index` )" );
       
  2440 
  2343 	return true;
  2441 	return true;
  2344 }
  2442 }
  2345 
  2443 
  2346 /**
  2444 /**
  2347  * Adds column to a database table if it doesn't already exist.
  2445  * Adds column to a database table, if it doesn't already exist.
  2348  *
  2446  *
  2349  * @since 1.3.0
  2447  * @since 1.3.0
  2350  *
  2448  *
  2351  * @global wpdb  $wpdb
  2449  * @global wpdb $wpdb WordPress database abstraction object.
  2352  *
  2450  *
  2353  * @param string $table_name  The table name to modify.
  2451  * @param string $table_name  Database table name.
  2354  * @param string $column_name The column name to add to the table.
  2452  * @param string $column_name Table column name.
  2355  * @param string $create_ddl  The SQL statement used to add the column.
  2453  * @param string $create_ddl  SQL statement to add column.
  2356  * @return bool True if already exists or on successful completion, false on error.
  2454  * @return bool True on success or if the column already exists. False on failure.
  2357  */
  2455  */
  2358 function maybe_add_column( $table_name, $column_name, $create_ddl ) {
  2456 function maybe_add_column( $table_name, $column_name, $create_ddl ) {
  2359 	global $wpdb;
  2457 	global $wpdb;
       
  2458 
  2360 	foreach ( $wpdb->get_col( "DESC $table_name", 0 ) as $column ) {
  2459 	foreach ( $wpdb->get_col( "DESC $table_name", 0 ) as $column ) {
  2361 		if ( $column == $column_name ) {
  2460 		if ( $column === $column_name ) {
  2362 			return true;
  2461 			return true;
  2363 		}
  2462 		}
  2364 	}
  2463 	}
  2365 
  2464 
  2366 	// Didn't find it try to create it.
  2465 	// Didn't find it, so try to create it.
  2367 	$wpdb->query( $create_ddl );
  2466 	$wpdb->query( $create_ddl );
  2368 
  2467 
  2369 	// We cannot directly tell that whether this succeeded!
  2468 	// We cannot directly tell that whether this succeeded!
  2370 	foreach ( $wpdb->get_col( "DESC $table_name", 0 ) as $column ) {
  2469 	foreach ( $wpdb->get_col( "DESC $table_name", 0 ) as $column ) {
  2371 		if ( $column == $column_name ) {
  2470 		if ( $column === $column_name ) {
  2372 			return true;
  2471 			return true;
  2373 		}
  2472 		}
  2374 	}
  2473 	}
       
  2474 
  2375 	return false;
  2475 	return false;
  2376 }
  2476 }
  2377 
  2477 
  2378 /**
  2478 /**
  2379  * If a table only contains utf8 or utf8mb4 columns, convert it to utf8mb4.
  2479  * If a table only contains utf8 or utf8mb4 columns, convert it to utf8mb4.
  2380  *
  2480  *
  2381  * @since 4.2.0
  2481  * @since 4.2.0
  2382  *
  2482  *
  2383  * @global wpdb  $wpdb
  2483  * @global wpdb $wpdb WordPress database abstraction object.
  2384  *
  2484  *
  2385  * @param string $table The table to convert.
  2485  * @param string $table The table to convert.
  2386  * @return bool true if the table was converted, false if it wasn't.
  2486  * @return bool True if the table was converted, false if it wasn't.
  2387  */
  2487  */
  2388 function maybe_convert_table_to_utf8mb4( $table ) {
  2488 function maybe_convert_table_to_utf8mb4( $table ) {
  2389 	global $wpdb;
  2489 	global $wpdb;
  2390 
  2490 
  2391 	$results = $wpdb->get_results( "SHOW FULL COLUMNS FROM `$table`" );
  2491 	$results = $wpdb->get_results( "SHOW FULL COLUMNS FROM `$table`" );
  2421 /**
  2521 /**
  2422  * Retrieve all options as it was for 1.2.
  2522  * Retrieve all options as it was for 1.2.
  2423  *
  2523  *
  2424  * @since 1.2.0
  2524  * @since 1.2.0
  2425  *
  2525  *
  2426  * @global wpdb  $wpdb
  2526  * @global wpdb $wpdb WordPress database abstraction object.
  2427  *
  2527  *
  2428  * @return stdClass List of options.
  2528  * @return stdClass List of options.
  2429  */
  2529  */
  2430 function get_alloptions_110() {
  2530 function get_alloptions_110() {
  2431 	global $wpdb;
  2531 	global $wpdb;
  2432 	$all_options = new stdClass;
  2532 	$all_options = new stdClass;
  2433 	if ( $options = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options" ) ) {
  2533 	$options     = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options" );
       
  2534 	if ( $options ) {
  2434 		foreach ( $options as $option ) {
  2535 		foreach ( $options as $option ) {
  2435 			if ( 'siteurl' == $option->option_name || 'home' == $option->option_name || 'category_base' == $option->option_name ) {
  2536 			if ( 'siteurl' === $option->option_name || 'home' === $option->option_name || 'category_base' === $option->option_name ) {
  2436 				$option->option_value = untrailingslashit( $option->option_value );
  2537 				$option->option_value = untrailingslashit( $option->option_value );
  2437 			}
  2538 			}
  2438 			$all_options->{$option->option_name} = stripslashes( $option->option_value );
  2539 			$all_options->{$option->option_name} = stripslashes( $option->option_value );
  2439 		}
  2540 		}
  2440 	}
  2541 	}
  2446  *
  2547  *
  2447  * @ignore
  2548  * @ignore
  2448  * @since 1.5.1
  2549  * @since 1.5.1
  2449  * @access private
  2550  * @access private
  2450  *
  2551  *
  2451  * @global wpdb  $wpdb
  2552  * @global wpdb $wpdb WordPress database abstraction object.
  2452  *
  2553  *
  2453  * @param string $setting Option name.
  2554  * @param string $setting Option name.
  2454  * @return mixed
  2555  * @return mixed
  2455  */
  2556  */
  2456 function __get_option( $setting ) {
  2557 function __get_option( $setting ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionDoubleUnderscore,PHPCompatibility.FunctionNameRestrictions.ReservedFunctionNames.FunctionDoubleUnderscore
  2457 	global $wpdb;
  2558 	global $wpdb;
  2458 
  2559 
  2459 	if ( $setting == 'home' && defined( 'WP_HOME' ) ) {
  2560 	if ( 'home' === $setting && defined( 'WP_HOME' ) ) {
  2460 		return untrailingslashit( WP_HOME );
  2561 		return untrailingslashit( WP_HOME );
  2461 	}
  2562 	}
  2462 
  2563 
  2463 	if ( $setting == 'siteurl' && defined( 'WP_SITEURL' ) ) {
  2564 	if ( 'siteurl' === $setting && defined( 'WP_SITEURL' ) ) {
  2464 		return untrailingslashit( WP_SITEURL );
  2565 		return untrailingslashit( WP_SITEURL );
  2465 	}
  2566 	}
  2466 
  2567 
  2467 	$option = $wpdb->get_var( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s", $setting ) );
  2568 	$option = $wpdb->get_var( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s", $setting ) );
  2468 
  2569 
  2469 	if ( 'home' == $setting && '' == $option ) {
  2570 	if ( 'home' === $setting && ! $option ) {
  2470 		return __get_option( 'siteurl' );
  2571 		return __get_option( 'siteurl' );
  2471 	}
  2572 	}
  2472 
  2573 
  2473 	if ( 'siteurl' == $setting || 'home' == $setting || 'category_base' == $setting || 'tag_base' == $setting ) {
  2574 	if ( in_array( $setting, array( 'siteurl', 'home', 'category_base', 'tag_base' ), true ) ) {
  2474 		$option = untrailingslashit( $option );
  2575 		$option = untrailingslashit( $option );
  2475 	}
  2576 	}
  2476 
  2577 
  2477 	return maybe_unserialize( $option );
  2578 	return maybe_unserialize( $option );
  2478 }
  2579 }
  2511  *
  2612  *
  2512  * Useful for creating new tables and updating existing tables to a new structure.
  2613  * Useful for creating new tables and updating existing tables to a new structure.
  2513  *
  2614  *
  2514  * @since 1.5.0
  2615  * @since 1.5.0
  2515  *
  2616  *
  2516  * @global wpdb  $wpdb
  2617  * @global wpdb $wpdb WordPress database abstraction object.
  2517  *
  2618  *
  2518  * @param string[]|string $queries Optional. The query to run. Can be multiple queries
  2619  * @param string[]|string $queries Optional. The query to run. Can be multiple queries
  2519  *                                 in an array, or a string of queries separated by
  2620  *                                 in an array, or a string of queries separated by
  2520  *                                 semicolons. Default empty string.
  2621  *                                 semicolons. Default empty string.
  2521  * @param bool            $execute Optional. Whether or not to execute the query right away.
  2622  * @param bool            $execute Optional. Whether or not to execute the query right away.
  2522  *                                 Default true.
  2623  *                                 Default true.
  2523  * @return array Strings containing the results of the various update queries.
  2624  * @return array Strings containing the results of the various update queries.
  2524  */
  2625  */
  2525 function dbDelta( $queries = '', $execute = true ) {
  2626 function dbDelta( $queries = '', $execute = true ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
  2526 	global $wpdb;
  2627 	global $wpdb;
  2527 
  2628 
  2528 	if ( in_array( $queries, array( '', 'all', 'blog', 'global', 'ms_global' ), true ) ) {
  2629 	if ( in_array( $queries, array( '', 'all', 'blog', 'global', 'ms_global' ), true ) ) {
  2529 		$queries = wp_get_db_schema( $queries );
  2630 		$queries = wp_get_db_schema( $queries );
  2530 	}
  2631 	}
  2531 
  2632 
  2532 	// Separate individual queries into an array
  2633 	// Separate individual queries into an array.
  2533 	if ( ! is_array( $queries ) ) {
  2634 	if ( ! is_array( $queries ) ) {
  2534 		$queries = explode( ';', $queries );
  2635 		$queries = explode( ';', $queries );
  2535 		$queries = array_filter( $queries );
  2636 		$queries = array_filter( $queries );
  2536 	}
  2637 	}
  2537 
  2638 
  2542 	 *
  2643 	 *
  2543 	 * @param string[] $queries An array of dbDelta SQL queries.
  2644 	 * @param string[] $queries An array of dbDelta SQL queries.
  2544 	 */
  2645 	 */
  2545 	$queries = apply_filters( 'dbdelta_queries', $queries );
  2646 	$queries = apply_filters( 'dbdelta_queries', $queries );
  2546 
  2647 
  2547 	$cqueries   = array(); // Creation Queries
  2648 	$cqueries   = array(); // Creation queries.
  2548 	$iqueries   = array(); // Insertion Queries
  2649 	$iqueries   = array(); // Insertion queries.
  2549 	$for_update = array();
  2650 	$for_update = array();
  2550 
  2651 
  2551 	// Create a tablename index for an array ($cqueries) of queries
  2652 	// Create a tablename index for an array ($cqueries) of queries.
  2552 	foreach ( $queries as $qry ) {
  2653 	foreach ( $queries as $qry ) {
  2553 		if ( preg_match( '|CREATE TABLE ([^ ]*)|', $qry, $matches ) ) {
  2654 		if ( preg_match( '|CREATE TABLE ([^ ]*)|', $qry, $matches ) ) {
  2554 			$cqueries[ trim( $matches[1], '`' ) ] = $qry;
  2655 			$cqueries[ trim( $matches[1], '`' ) ] = $qry;
  2555 			$for_update[ $matches[1] ]            = 'Created table ' . $matches[1];
  2656 			$for_update[ $matches[1] ]            = 'Created table ' . $matches[1];
  2556 		} elseif ( preg_match( '|CREATE DATABASE ([^ ]*)|', $qry, $matches ) ) {
  2657 		} elseif ( preg_match( '|CREATE DATABASE ([^ ]*)|', $qry, $matches ) ) {
  2558 		} elseif ( preg_match( '|INSERT INTO ([^ ]*)|', $qry, $matches ) ) {
  2659 		} elseif ( preg_match( '|INSERT INTO ([^ ]*)|', $qry, $matches ) ) {
  2559 			$iqueries[] = $qry;
  2660 			$iqueries[] = $qry;
  2560 		} elseif ( preg_match( '|UPDATE ([^ ]*)|', $qry, $matches ) ) {
  2661 		} elseif ( preg_match( '|UPDATE ([^ ]*)|', $qry, $matches ) ) {
  2561 			$iqueries[] = $qry;
  2662 			$iqueries[] = $qry;
  2562 		} else {
  2663 		} else {
  2563 			// Unrecognized query type
  2664 			// Unrecognized query type.
  2564 		}
  2665 		}
  2565 	}
  2666 	}
  2566 
  2667 
  2567 	/**
  2668 	/**
  2568 	 * Filters the dbDelta SQL queries for creating tables and/or databases.
  2669 	 * Filters the dbDelta SQL queries for creating tables and/or databases.
  2590 	$blob_fields = array( 'tinyblob', 'blob', 'mediumblob', 'longblob' );
  2691 	$blob_fields = array( 'tinyblob', 'blob', 'mediumblob', 'longblob' );
  2591 
  2692 
  2592 	$global_tables = $wpdb->tables( 'global' );
  2693 	$global_tables = $wpdb->tables( 'global' );
  2593 	foreach ( $cqueries as $table => $qry ) {
  2694 	foreach ( $cqueries as $table => $qry ) {
  2594 		// Upgrade global tables only for the main site. Don't upgrade at all if conditions are not optimal.
  2695 		// Upgrade global tables only for the main site. Don't upgrade at all if conditions are not optimal.
  2595 		if ( in_array( $table, $global_tables ) && ! wp_should_upgrade_global_tables() ) {
  2696 		if ( in_array( $table, $global_tables, true ) && ! wp_should_upgrade_global_tables() ) {
  2596 			unset( $cqueries[ $table ], $for_update[ $table ] );
  2697 			unset( $cqueries[ $table ], $for_update[ $table ] );
  2597 			continue;
  2698 			continue;
  2598 		}
  2699 		}
  2599 
  2700 
  2600 		// Fetch the table column structure from the database
  2701 		// Fetch the table column structure from the database.
  2601 		$suppress    = $wpdb->suppress_errors();
  2702 		$suppress    = $wpdb->suppress_errors();
  2602 		$tablefields = $wpdb->get_results( "DESCRIBE {$table};" );
  2703 		$tablefields = $wpdb->get_results( "DESCRIBE {$table};" );
  2603 		$wpdb->suppress_errors( $suppress );
  2704 		$wpdb->suppress_errors( $suppress );
  2604 
  2705 
  2605 		if ( ! $tablefields ) {
  2706 		if ( ! $tablefields ) {
  2606 			continue;
  2707 			continue;
  2607 		}
  2708 		}
  2608 
  2709 
  2609 		// Clear the field and index arrays.
  2710 		// Clear the field and index arrays.
  2610 		$cfields = $indices = $indices_without_subparts = array();
  2711 		$cfields                  = array();
       
  2712 		$indices                  = array();
       
  2713 		$indices_without_subparts = array();
  2611 
  2714 
  2612 		// Get all of the field names in the query from between the parentheses.
  2715 		// Get all of the field names in the query from between the parentheses.
  2613 		preg_match( '|\((.*)\)|ms', $qry, $match2 );
  2716 		preg_match( '|\((.*)\)|ms', $qry, $match2 );
  2614 		$qryline = trim( $match2[1] );
  2717 		$qryline = trim( $match2[1] );
  2615 
  2718 
  2680 
  2783 
  2681 					// Escape the index name with backticks. An index for a primary key has no name.
  2784 					// Escape the index name with backticks. An index for a primary key has no name.
  2682 					$index_name = ( 'PRIMARY KEY' === $index_type ) ? '' : '`' . strtolower( $index_matches['index_name'] ) . '`';
  2785 					$index_name = ( 'PRIMARY KEY' === $index_type ) ? '' : '`' . strtolower( $index_matches['index_name'] ) . '`';
  2683 
  2786 
  2684 					// Parse the columns. Multiple columns are separated by a comma.
  2787 					// Parse the columns. Multiple columns are separated by a comma.
  2685 					$index_columns = $index_columns_without_subparts = array_map( 'trim', explode( ',', $index_matches['index_columns'] ) );
  2788 					$index_columns                  = array_map( 'trim', explode( ',', $index_matches['index_columns'] ) );
       
  2789 					$index_columns_without_subparts = $index_columns;
  2686 
  2790 
  2687 					// Normalize columns.
  2791 					// Normalize columns.
  2688 					foreach ( $index_columns as $id => &$index_column ) {
  2792 					foreach ( $index_columns as $id => &$index_column ) {
  2689 						// Extract column name and number of indexed characters (sub_part).
  2793 						// Extract column name and number of indexed characters (sub_part).
  2690 						preg_match(
  2794 						preg_match(
  2740 		// For every field in the table.
  2844 		// For every field in the table.
  2741 		foreach ( $tablefields as $tablefield ) {
  2845 		foreach ( $tablefields as $tablefield ) {
  2742 			$tablefield_field_lowercased = strtolower( $tablefield->Field );
  2846 			$tablefield_field_lowercased = strtolower( $tablefield->Field );
  2743 			$tablefield_type_lowercased  = strtolower( $tablefield->Type );
  2847 			$tablefield_type_lowercased  = strtolower( $tablefield->Type );
  2744 
  2848 
  2745 			// If the table field exists in the field array ...
  2849 			// If the table field exists in the field array...
  2746 			if ( array_key_exists( $tablefield_field_lowercased, $cfields ) ) {
  2850 			if ( array_key_exists( $tablefield_field_lowercased, $cfields ) ) {
  2747 
  2851 
  2748 				// Get the field type from the query.
  2852 				// Get the field type from the query.
  2749 				preg_match( '|`?' . $tablefield->Field . '`? ([^ ]*( unsigned)?)|i', $cfields[ $tablefield_field_lowercased ], $matches );
  2853 				preg_match( '|`?' . $tablefield->Field . '`? ([^ ]*( unsigned)?)|i', $cfields[ $tablefield_field_lowercased ], $matches );
  2750 				$fieldtype            = $matches[1];
  2854 				$fieldtype            = $matches[1];
  2751 				$fieldtype_lowercased = strtolower( $fieldtype );
  2855 				$fieldtype_lowercased = strtolower( $fieldtype );
  2752 
  2856 
  2753 				// Is actual field type different from the field type in query?
  2857 				// Is actual field type different from the field type in query?
  2754 				if ( $tablefield->Type != $fieldtype ) {
  2858 				if ( $tablefield->Type != $fieldtype ) {
  2755 					$do_change = true;
  2859 					$do_change = true;
  2756 					if ( in_array( $fieldtype_lowercased, $text_fields ) && in_array( $tablefield_type_lowercased, $text_fields ) ) {
  2860 					if ( in_array( $fieldtype_lowercased, $text_fields, true ) && in_array( $tablefield_type_lowercased, $text_fields, true ) ) {
  2757 						if ( array_search( $fieldtype_lowercased, $text_fields ) < array_search( $tablefield_type_lowercased, $text_fields ) ) {
  2861 						if ( array_search( $fieldtype_lowercased, $text_fields, true ) < array_search( $tablefield_type_lowercased, $text_fields, true ) ) {
  2758 							$do_change = false;
  2862 							$do_change = false;
  2759 						}
  2863 						}
  2760 					}
  2864 					}
  2761 
  2865 
  2762 					if ( in_array( $fieldtype_lowercased, $blob_fields ) && in_array( $tablefield_type_lowercased, $blob_fields ) ) {
  2866 					if ( in_array( $fieldtype_lowercased, $blob_fields, true ) && in_array( $tablefield_type_lowercased, $blob_fields, true ) ) {
  2763 						if ( array_search( $fieldtype_lowercased, $blob_fields ) < array_search( $tablefield_type_lowercased, $blob_fields ) ) {
  2867 						if ( array_search( $fieldtype_lowercased, $blob_fields, true ) < array_search( $tablefield_type_lowercased, $blob_fields, true ) ) {
  2764 							$do_change = false;
  2868 							$do_change = false;
  2765 						}
  2869 						}
  2766 					}
  2870 					}
  2767 
  2871 
  2768 					if ( $do_change ) {
  2872 					if ( $do_change ) {
  2769 						// Add a query to change the column type.
  2873 						// Add a query to change the column type.
  2770 						$cqueries[]                                      = "ALTER TABLE {$table} CHANGE COLUMN `{$tablefield->Field}` " . $cfields[ $tablefield_field_lowercased ];
  2874 						$cqueries[] = "ALTER TABLE {$table} CHANGE COLUMN `{$tablefield->Field}` " . $cfields[ $tablefield_field_lowercased ];
       
  2875 
  2771 						$for_update[ $table . '.' . $tablefield->Field ] = "Changed type of {$table}.{$tablefield->Field} from {$tablefield->Type} to {$fieldtype}";
  2876 						$for_update[ $table . '.' . $tablefield->Field ] = "Changed type of {$table}.{$tablefield->Field} from {$tablefield->Type} to {$fieldtype}";
  2772 					}
  2877 					}
  2773 				}
  2878 				}
  2774 
  2879 
  2775 				// Get the default value from the array.
  2880 				// Get the default value from the array.
  2776 				if ( preg_match( "| DEFAULT '(.*?)'|i", $cfields[ $tablefield_field_lowercased ], $matches ) ) {
  2881 				if ( preg_match( "| DEFAULT '(.*?)'|i", $cfields[ $tablefield_field_lowercased ], $matches ) ) {
  2777 					$default_value = $matches[1];
  2882 					$default_value = $matches[1];
  2778 					if ( $tablefield->Default != $default_value ) {
  2883 					if ( $tablefield->Default != $default_value ) {
  2779 						// Add a query to change the column's default value
  2884 						// Add a query to change the column's default value
  2780 						$cqueries[]                                      = "ALTER TABLE {$table} ALTER COLUMN `{$tablefield->Field}` SET DEFAULT '{$default_value}'";
  2885 						$cqueries[] = "ALTER TABLE {$table} ALTER COLUMN `{$tablefield->Field}` SET DEFAULT '{$default_value}'";
       
  2886 
  2781 						$for_update[ $table . '.' . $tablefield->Field ] = "Changed default value of {$table}.{$tablefield->Field} from {$tablefield->Default} to {$default_value}";
  2887 						$for_update[ $table . '.' . $tablefield->Field ] = "Changed default value of {$table}.{$tablefield->Field} from {$tablefield->Default} to {$default_value}";
  2782 					}
  2888 					}
  2783 				}
  2889 				}
  2784 
  2890 
  2785 				// Remove the field from the array (so it's not added).
  2891 				// Remove the field from the array (so it's not added).
  2790 		}
  2896 		}
  2791 
  2897 
  2792 		// For every remaining field specified for the table.
  2898 		// For every remaining field specified for the table.
  2793 		foreach ( $cfields as $fieldname => $fielddef ) {
  2899 		foreach ( $cfields as $fieldname => $fielddef ) {
  2794 			// Push a query line into $cqueries that adds the field to that table.
  2900 			// Push a query line into $cqueries that adds the field to that table.
  2795 			$cqueries[]                              = "ALTER TABLE {$table} ADD COLUMN $fielddef";
  2901 			$cqueries[] = "ALTER TABLE {$table} ADD COLUMN $fielddef";
       
  2902 
  2796 			$for_update[ $table . '.' . $fieldname ] = 'Added column ' . $table . '.' . $fieldname;
  2903 			$for_update[ $table . '.' . $fieldname ] = 'Added column ' . $table . '.' . $fieldname;
  2797 		}
  2904 		}
  2798 
  2905 
  2799 		// Index stuff goes here. Fetch the table index structure from the database.
  2906 		// Index stuff goes here. Fetch the table index structure from the database.
  2800 		$tableindices = $wpdb->get_results( "SHOW INDEX FROM {$table};" );
  2907 		$tableindices = $wpdb->get_results( "SHOW INDEX FROM {$table};" );
  2803 			// Clear the index array.
  2910 			// Clear the index array.
  2804 			$index_ary = array();
  2911 			$index_ary = array();
  2805 
  2912 
  2806 			// For every index in the table.
  2913 			// For every index in the table.
  2807 			foreach ( $tableindices as $tableindex ) {
  2914 			foreach ( $tableindices as $tableindex ) {
       
  2915 				$keyname = strtolower( $tableindex->Key_name );
  2808 
  2916 
  2809 				// Add the index to the index data array.
  2917 				// Add the index to the index data array.
  2810 				$keyname                             = strtolower( $tableindex->Key_name );
       
  2811 				$index_ary[ $keyname ]['columns'][]  = array(
  2918 				$index_ary[ $keyname ]['columns'][]  = array(
  2812 					'fieldname' => $tableindex->Column_name,
  2919 					'fieldname' => $tableindex->Column_name,
  2813 					'subpart'   => $tableindex->Sub_part,
  2920 					'subpart'   => $tableindex->Sub_part,
  2814 				);
  2921 				);
  2815 				$index_ary[ $keyname ]['unique']     = ( $tableindex->Non_unique == 0 ) ? true : false;
  2922 				$index_ary[ $keyname ]['unique']     = ( 0 == $tableindex->Non_unique ) ? true : false;
  2816 				$index_ary[ $keyname ]['index_type'] = $tableindex->Index_type;
  2923 				$index_ary[ $keyname ]['index_type'] = $tableindex->Index_type;
  2817 			}
  2924 			}
  2818 
  2925 
  2819 			// For each actual index in the index array.
  2926 			// For each actual index in the index array.
  2820 			foreach ( $index_ary as $index_name => $index_data ) {
  2927 			foreach ( $index_ary as $index_name => $index_data ) {
  2821 
  2928 
  2822 				// Build a create string to compare to the query.
  2929 				// Build a create string to compare to the query.
  2823 				$index_string = '';
  2930 				$index_string = '';
  2824 				if ( $index_name == 'primary' ) {
  2931 				if ( 'primary' === $index_name ) {
  2825 					$index_string .= 'PRIMARY ';
  2932 					$index_string .= 'PRIMARY ';
  2826 				} elseif ( $index_data['unique'] ) {
  2933 				} elseif ( $index_data['unique'] ) {
  2827 					$index_string .= 'UNIQUE ';
  2934 					$index_string .= 'UNIQUE ';
  2828 				}
  2935 				}
  2829 				if ( 'FULLTEXT' === strtoupper( $index_data['index_type'] ) ) {
  2936 				if ( 'FULLTEXT' === strtoupper( $index_data['index_type'] ) ) {
  2838 				}
  2945 				}
  2839 				$index_columns = '';
  2946 				$index_columns = '';
  2840 
  2947 
  2841 				// For each column in the index.
  2948 				// For each column in the index.
  2842 				foreach ( $index_data['columns'] as $column_data ) {
  2949 				foreach ( $index_data['columns'] as $column_data ) {
  2843 					if ( $index_columns != '' ) {
  2950 					if ( '' !== $index_columns ) {
  2844 						$index_columns .= ',';
  2951 						$index_columns .= ',';
  2845 					}
  2952 					}
  2846 
  2953 
  2847 					// Add the field to the column list string.
  2954 					// Add the field to the column list string.
  2848 					$index_columns .= '`' . $column_data['fieldname'] . '`';
  2955 					$index_columns .= '`' . $column_data['fieldname'] . '`';
  2850 
  2957 
  2851 				// Add the column list to the index create string.
  2958 				// Add the column list to the index create string.
  2852 				$index_string .= " ($index_columns)";
  2959 				$index_string .= " ($index_columns)";
  2853 
  2960 
  2854 				// Check if the index definition exists, ignoring subparts.
  2961 				// Check if the index definition exists, ignoring subparts.
  2855 				if ( ! ( ( $aindex = array_search( $index_string, $indices_without_subparts ) ) === false ) ) {
  2962 				$aindex = array_search( $index_string, $indices_without_subparts, true );
       
  2963 				if ( false !== $aindex ) {
  2856 					// If the index already exists (even with different subparts), we don't need to create it.
  2964 					// If the index already exists (even with different subparts), we don't need to create it.
  2857 					unset( $indices_without_subparts[ $aindex ] );
  2965 					unset( $indices_without_subparts[ $aindex ] );
  2858 					unset( $indices[ $aindex ] );
  2966 					unset( $indices[ $aindex ] );
  2859 				}
  2967 				}
  2860 			}
  2968 			}
  2861 		}
  2969 		}
  2862 
  2970 
  2863 		// For every remaining index specified for the table.
  2971 		// For every remaining index specified for the table.
  2864 		foreach ( (array) $indices as $index ) {
  2972 		foreach ( (array) $indices as $index ) {
  2865 			// Push a query line into $cqueries that adds the index to that table.
  2973 			// Push a query line into $cqueries that adds the index to that table.
  2866 			$cqueries[]   = "ALTER TABLE {$table} ADD $index";
  2974 			$cqueries[] = "ALTER TABLE {$table} ADD $index";
       
  2975 
  2867 			$for_update[] = 'Added index ' . $table . ' ' . $index;
  2976 			$for_update[] = 'Added index ' . $table . ' ' . $index;
  2868 		}
  2977 		}
  2869 
  2978 
  2870 		// Remove the original table creation query from processing.
  2979 		// Remove the original table creation query from processing.
  2871 		unset( $cqueries[ $table ], $for_update[ $table ] );
  2980 		unset( $cqueries[ $table ], $for_update[ $table ] );
  2947 		'wp-comments.php'       => 'comments.php',
  3056 		'wp-comments.php'       => 'comments.php',
  2948 		'wp-comments-popup.php' => 'comments-popup.php',
  3057 		'wp-comments-popup.php' => 'comments-popup.php',
  2949 	);
  3058 	);
  2950 
  3059 
  2951 	foreach ( $files as $oldfile => $newfile ) {
  3060 	foreach ( $files as $oldfile => $newfile ) {
  2952 		if ( $oldfile == 'index.php' ) {
  3061 		if ( 'index.php' === $oldfile ) {
  2953 			$oldpath = $home_path;
  3062 			$oldpath = $home_path;
  2954 		} else {
  3063 		} else {
  2955 			$oldpath = ABSPATH;
  3064 			$oldpath = ABSPATH;
  2956 		}
  3065 		}
  2957 
  3066 
  2958 		// Check to make sure it's not a new index.
  3067 		// Check to make sure it's not a new index.
  2959 		if ( $oldfile == 'index.php' ) {
  3068 		if ( 'index.php' === $oldfile ) {
  2960 			$index = implode( '', file( "$oldpath/$oldfile" ) );
  3069 			$index = implode( '', file( "$oldpath/$oldfile" ) );
  2961 			if ( strpos( $index, 'WP_USE_THEMES' ) !== false ) {
  3070 			if ( strpos( $index, 'WP_USE_THEMES' ) !== false ) {
  2962 				if ( ! @copy( WP_CONTENT_DIR . '/themes/' . WP_DEFAULT_THEME . '/index.php', "$site_dir/$newfile" ) ) {
  3071 				if ( ! copy( WP_CONTENT_DIR . '/themes/' . WP_DEFAULT_THEME . '/index.php', "$site_dir/$newfile" ) ) {
  2963 					return false;
  3072 					return false;
  2964 				}
  3073 				}
  2965 
  3074 
  2966 				// Don't copy anything.
  3075 				// Don't copy anything.
  2967 				continue;
  3076 				continue;
  2968 			}
  3077 			}
  2969 		}
  3078 		}
  2970 
  3079 
  2971 		if ( ! @copy( "$oldpath/$oldfile", "$site_dir/$newfile" ) ) {
  3080 		if ( ! copy( "$oldpath/$oldfile", "$site_dir/$newfile" ) ) {
  2972 			return false;
  3081 			return false;
  2973 		}
  3082 		}
  2974 
  3083 
  2975 		chmod( "$site_dir/$newfile", 0777 );
  3084 		chmod( "$site_dir/$newfile", 0777 );
  2976 
  3085 
  3018  *
  3127  *
  3019  * @since 1.5.0
  3128  * @since 1.5.0
  3020  *
  3129  *
  3021  * @param string $theme_name The name of the theme.
  3130  * @param string $theme_name The name of the theme.
  3022  * @param string $template   The directory name of the theme.
  3131  * @param string $template   The directory name of the theme.
  3023  * @return false|void
  3132  * @return void|false
  3024  */
  3133  */
  3025 function make_site_theme_from_default( $theme_name, $template ) {
  3134 function make_site_theme_from_default( $theme_name, $template ) {
  3026 	$site_dir    = WP_CONTENT_DIR . "/themes/$template";
  3135 	$site_dir    = WP_CONTENT_DIR . "/themes/$template";
  3027 	$default_dir = WP_CONTENT_DIR . '/themes/' . WP_DEFAULT_THEME;
  3136 	$default_dir = WP_CONTENT_DIR . '/themes/' . WP_DEFAULT_THEME;
  3028 
  3137 
  3029 	// Copy files from the default theme to the site theme.
  3138 	// Copy files from the default theme to the site theme.
  3030 	//$files = array('index.php', 'comments.php', 'comments-popup.php', 'footer.php', 'header.php', 'sidebar.php', 'style.css');
  3139 	// $files = array( 'index.php', 'comments.php', 'comments-popup.php', 'footer.php', 'header.php', 'sidebar.php', 'style.css' );
  3031 
  3140 
  3032 	$theme_dir = @ opendir( $default_dir );
  3141 	$theme_dir = @opendir( $default_dir );
  3033 	if ( $theme_dir ) {
  3142 	if ( $theme_dir ) {
  3034 		while ( ( $theme_file = readdir( $theme_dir ) ) !== false ) {
  3143 		while ( ( $theme_file = readdir( $theme_dir ) ) !== false ) {
  3035 			if ( is_dir( "$default_dir/$theme_file" ) ) {
  3144 			if ( is_dir( "$default_dir/$theme_file" ) ) {
  3036 				continue;
  3145 				continue;
  3037 			}
  3146 			}
  3038 			if ( ! @copy( "$default_dir/$theme_file", "$site_dir/$theme_file" ) ) {
  3147 			if ( ! copy( "$default_dir/$theme_file", "$site_dir/$theme_file" ) ) {
  3039 				return;
  3148 				return;
  3040 			}
  3149 			}
  3041 			chmod( "$site_dir/$theme_file", 0777 );
  3150 			chmod( "$site_dir/$theme_file", 0777 );
  3042 		}
  3151 		}
  3043 	}
  3152 
  3044 	@closedir( $theme_dir );
  3153 		closedir( $theme_dir );
       
  3154 	}
  3045 
  3155 
  3046 	// Rewrite the theme header.
  3156 	// Rewrite the theme header.
  3047 	$stylelines = explode( "\n", implode( '', file( "$site_dir/style.css" ) ) );
  3157 	$stylelines = explode( "\n", implode( '', file( "$site_dir/style.css" ) ) );
  3048 	if ( $stylelines ) {
  3158 	if ( $stylelines ) {
  3049 		$f = fopen( "$site_dir/style.css", 'w' );
  3159 		$f = fopen( "$site_dir/style.css", 'w' );
  3069 	umask( 0 );
  3179 	umask( 0 );
  3070 	if ( ! mkdir( "$site_dir/images", 0777 ) ) {
  3180 	if ( ! mkdir( "$site_dir/images", 0777 ) ) {
  3071 		return false;
  3181 		return false;
  3072 	}
  3182 	}
  3073 
  3183 
  3074 	$images_dir = @ opendir( "$default_dir/images" );
  3184 	$images_dir = @opendir( "$default_dir/images" );
  3075 	if ( $images_dir ) {
  3185 	if ( $images_dir ) {
  3076 		while ( ( $image = readdir( $images_dir ) ) !== false ) {
  3186 		while ( ( $image = readdir( $images_dir ) ) !== false ) {
  3077 			if ( is_dir( "$default_dir/images/$image" ) ) {
  3187 			if ( is_dir( "$default_dir/images/$image" ) ) {
  3078 				continue;
  3188 				continue;
  3079 			}
  3189 			}
  3080 			if ( ! @copy( "$default_dir/images/$image", "$site_dir/images/$image" ) ) {
  3190 			if ( ! copy( "$default_dir/images/$image", "$site_dir/images/$image" ) ) {
  3081 				return;
  3191 				return;
  3082 			}
  3192 			}
  3083 			chmod( "$site_dir/images/$image", 0777 );
  3193 			chmod( "$site_dir/images/$image", 0777 );
  3084 		}
  3194 		}
  3085 	}
  3195 
  3086 	@closedir( $images_dir );
  3196 		closedir( $images_dir );
       
  3197 	}
  3087 }
  3198 }
  3088 
  3199 
  3089 /**
  3200 /**
  3090  * Creates a site theme.
  3201  * Creates a site theme.
  3091  *
  3202  *
  3092  * {@internal Missing Long Description}}
  3203  * {@internal Missing Long Description}}
  3093  *
  3204  *
  3094  * @since 1.5.0
  3205  * @since 1.5.0
  3095  *
  3206  *
  3096  * @return false|string
  3207  * @return string|false
  3097  */
  3208  */
  3098 function make_site_theme() {
  3209 function make_site_theme() {
  3099 	// Name the theme after the blog.
  3210 	// Name the theme after the blog.
  3100 	$theme_name = __get_option( 'blogname' );
  3211 	$theme_name = __get_option( 'blogname' );
  3101 	$template   = sanitize_title( $theme_name );
  3212 	$template   = sanitize_title( $theme_name );
  3128 		}
  3239 		}
  3129 	}
  3240 	}
  3130 
  3241 
  3131 	// Make the new site theme active.
  3242 	// Make the new site theme active.
  3132 	$current_template = __get_option( 'template' );
  3243 	$current_template = __get_option( 'template' );
  3133 	if ( $current_template == WP_DEFAULT_THEME ) {
  3244 	if ( WP_DEFAULT_THEME == $current_template ) {
  3134 		update_option( 'template', $template );
  3245 		update_option( 'template', $template );
  3135 		update_option( 'stylesheet', $template );
  3246 		update_option( 'stylesheet', $template );
  3136 	}
  3247 	}
  3137 	return $template;
  3248 	return $template;
  3138 }
  3249 }
  3160 		case 2:
  3271 		case 2:
  3161 			return 'author';
  3272 			return 'author';
  3162 		case 1:
  3273 		case 1:
  3163 			return 'contributor';
  3274 			return 'contributor';
  3164 		case 0:
  3275 		case 0:
       
  3276 		default:
  3165 			return 'subscriber';
  3277 			return 'subscriber';
  3166 	}
  3278 	}
  3167 }
  3279 }
  3168 
  3280 
  3169 /**
  3281 /**
  3170  * Checks the version of the installed MySQL binary.
  3282  * Checks the version of the installed MySQL binary.
  3171  *
  3283  *
  3172  * @since 2.1.0
  3284  * @since 2.1.0
  3173  *
  3285  *
  3174  * @global wpdb  $wpdb
  3286  * @global wpdb $wpdb WordPress database abstraction object.
  3175  */
  3287  */
  3176 function wp_check_mysql_version() {
  3288 function wp_check_mysql_version() {
  3177 	global $wpdb;
  3289 	global $wpdb;
  3178 	$result = $wpdb->check_database_version();
  3290 	$result = $wpdb->check_database_version();
  3179 	if ( is_wp_error( $result ) ) {
  3291 	if ( is_wp_error( $result ) ) {
  3188  */
  3300  */
  3189 function maybe_disable_automattic_widgets() {
  3301 function maybe_disable_automattic_widgets() {
  3190 	$plugins = __get_option( 'active_plugins' );
  3302 	$plugins = __get_option( 'active_plugins' );
  3191 
  3303 
  3192 	foreach ( (array) $plugins as $plugin ) {
  3304 	foreach ( (array) $plugins as $plugin ) {
  3193 		if ( basename( $plugin ) == 'widgets.php' ) {
  3305 		if ( 'widgets.php' === basename( $plugin ) ) {
  3194 			array_splice( $plugins, array_search( $plugin, $plugins ), 1 );
  3306 			array_splice( $plugins, array_search( $plugin, $plugins, true ), 1 );
  3195 			update_option( 'active_plugins', $plugins );
  3307 			update_option( 'active_plugins', $plugins );
  3196 			break;
  3308 			break;
  3197 		}
  3309 		}
  3198 	}
  3310 	}
  3199 }
  3311 }
  3201 /**
  3313 /**
  3202  * Disables the Link Manager on upgrade if, at the time of upgrade, no links exist in the DB.
  3314  * Disables the Link Manager on upgrade if, at the time of upgrade, no links exist in the DB.
  3203  *
  3315  *
  3204  * @since 3.5.0
  3316  * @since 3.5.0
  3205  *
  3317  *
  3206  * @global int  $wp_current_db_version
  3318  * @global int  $wp_current_db_version The old (current) database version.
  3207  * @global wpdb $wpdb WordPress database abstraction object.
  3319  * @global wpdb $wpdb                  WordPress database abstraction object.
  3208  */
  3320  */
  3209 function maybe_disable_link_manager() {
  3321 function maybe_disable_link_manager() {
  3210 	global $wp_current_db_version, $wpdb;
  3322 	global $wp_current_db_version, $wpdb;
  3211 
  3323 
  3212 	if ( $wp_current_db_version >= 22006 && get_option( 'link_manager_enabled' ) && ! $wpdb->get_var( "SELECT link_id FROM $wpdb->links LIMIT 1" ) ) {
  3324 	if ( $wp_current_db_version >= 22006 && get_option( 'link_manager_enabled' ) && ! $wpdb->get_var( "SELECT link_id FROM $wpdb->links LIMIT 1" ) ) {
  3217 /**
  3329 /**
  3218  * Runs before the schema is upgraded.
  3330  * Runs before the schema is upgraded.
  3219  *
  3331  *
  3220  * @since 2.9.0
  3332  * @since 2.9.0
  3221  *
  3333  *
  3222  * @global int  $wp_current_db_version
  3334  * @global int  $wp_current_db_version The old (current) database version.
  3223  * @global wpdb $wpdb WordPress database abstraction object.
  3335  * @global wpdb $wpdb                  WordPress database abstraction object.
  3224  */
  3336  */
  3225 function pre_schema_upgrade() {
  3337 function pre_schema_upgrade() {
  3226 	global $wp_current_db_version, $wpdb;
  3338 	global $wp_current_db_version, $wpdb;
  3227 
  3339 
  3228 	// Upgrade versions prior to 2.9
  3340 	// Upgrade versions prior to 2.9.
  3229 	if ( $wp_current_db_version < 11557 ) {
  3341 	if ( $wp_current_db_version < 11557 ) {
  3230 		// Delete duplicate options. Keep the option with the highest option_id.
  3342 		// Delete duplicate options. Keep the option with the highest option_id.
  3231 		$wpdb->query( "DELETE o1 FROM $wpdb->options AS o1 JOIN $wpdb->options AS o2 USING (`option_name`) WHERE o2.option_id > o1.option_id" );
  3343 		$wpdb->query( "DELETE o1 FROM $wpdb->options AS o1 JOIN $wpdb->options AS o2 USING (`option_name`) WHERE o2.option_id > o1.option_id" );
  3232 
  3344 
  3233 		// Drop the old primary key and add the new.
  3345 		// Drop the old primary key and add the new.
  3238 	}
  3350 	}
  3239 
  3351 
  3240 	// Multisite schema upgrades.
  3352 	// Multisite schema upgrades.
  3241 	if ( $wp_current_db_version < 25448 && is_multisite() && wp_should_upgrade_global_tables() ) {
  3353 	if ( $wp_current_db_version < 25448 && is_multisite() && wp_should_upgrade_global_tables() ) {
  3242 
  3354 
  3243 		// Upgrade versions prior to 3.7
  3355 		// Upgrade versions prior to 3.7.
  3244 		if ( $wp_current_db_version < 25179 ) {
  3356 		if ( $wp_current_db_version < 25179 ) {
  3245 			// New primary key for signups.
  3357 			// New primary key for signups.
  3246 			$wpdb->query( "ALTER TABLE $wpdb->signups ADD signup_id BIGINT(20) NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST" );
  3358 			$wpdb->query( "ALTER TABLE $wpdb->signups ADD signup_id BIGINT(20) NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST" );
  3247 			$wpdb->query( "ALTER TABLE $wpdb->signups DROP INDEX domain" );
  3359 			$wpdb->query( "ALTER TABLE $wpdb->signups DROP INDEX domain" );
  3248 		}
  3360 		}
  3280 	/**
  3392 	/**
  3281 	 * Install global terms.
  3393 	 * Install global terms.
  3282 	 *
  3394 	 *
  3283 	 * @since 3.0.0
  3395 	 * @since 3.0.0
  3284 	 *
  3396 	 *
  3285 	 * @global wpdb   $wpdb
  3397 	 * @global wpdb   $wpdb            WordPress database abstraction object.
  3286 	 * @global string $charset_collate
  3398 	 * @global string $charset_collate
  3287 	 */
  3399 	 */
  3288 	function install_global_terms() {
  3400 	function install_global_terms() {
  3289 		global $wpdb, $charset_collate;
  3401 		global $wpdb, $charset_collate;
  3290 		$ms_queries = "
  3402 		$ms_queries = "
  3296   PRIMARY KEY  (cat_ID),
  3408   PRIMARY KEY  (cat_ID),
  3297   KEY category_nicename (category_nicename),
  3409   KEY category_nicename (category_nicename),
  3298   KEY last_updated (last_updated)
  3410   KEY last_updated (last_updated)
  3299 ) $charset_collate;
  3411 ) $charset_collate;
  3300 ";
  3412 ";
  3301 		// now create tables
  3413 		// Now create tables.
  3302 		dbDelta( $ms_queries );
  3414 		dbDelta( $ms_queries );
  3303 	}
  3415 	}
  3304 endif;
  3416 endif;
  3305 
  3417 
  3306 /**
  3418 /**
  3321  *
  3433  *
  3322  * @return bool Whether to run the upgrade routines on global tables.
  3434  * @return bool Whether to run the upgrade routines on global tables.
  3323  */
  3435  */
  3324 function wp_should_upgrade_global_tables() {
  3436 function wp_should_upgrade_global_tables() {
  3325 
  3437 
  3326 	// Return false early if explicitly not upgrading
  3438 	// Return false early if explicitly not upgrading.
  3327 	if ( defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) ) {
  3439 	if ( defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) ) {
  3328 		return false;
  3440 		return false;
  3329 	}
  3441 	}
  3330 
  3442 
  3331 	// Assume global tables should be upgraded
  3443 	// Assume global tables should be upgraded.
  3332 	$should_upgrade = true;
  3444 	$should_upgrade = true;
  3333 
  3445 
  3334 	// Set to false if not on main network (does not matter if not multi-network)
  3446 	// Set to false if not on main network (does not matter if not multi-network).
  3335 	if ( ! is_main_network() ) {
  3447 	if ( ! is_main_network() ) {
  3336 		$should_upgrade = false;
  3448 		$should_upgrade = false;
  3337 	}
  3449 	}
  3338 
  3450 
  3339 	// Set to false if not on main site of current network (does not matter if not multi-site)
  3451 	// Set to false if not on main site of current network (does not matter if not multi-site).
  3340 	if ( ! is_main_site() ) {
  3452 	if ( ! is_main_site() ) {
  3341 		$should_upgrade = false;
  3453 		$should_upgrade = false;
  3342 	}
  3454 	}
  3343 
  3455 
  3344 	/**
  3456 	/**