3 * WordPress Upgrade API
5 * Most of the functions are pluggable and can be overwritten.
8 * @subpackage Administration
11 /** Include user install customize script. */
12 if ( file_exists(WP_CONTENT_DIR . '/install.php') )
13 require (WP_CONTENT_DIR . '/install.php');
15 /** WordPress Administration API */
16 require_once(ABSPATH . 'wp-admin/includes/admin.php');
18 /** WordPress Schema API */
19 require_once(ABSPATH . 'wp-admin/includes/schema.php');
21 if ( !function_exists('wp_install') ) :
25 * Runs the required functions to set up and populate the database,
26 * including primary admin user and initial options.
30 * @param string $blog_title Blog title.
31 * @param string $user_name User's username.
32 * @param string $user_email User's email.
33 * @param bool $public Whether blog is public.
34 * @param string $deprecated Optional. Not used.
35 * @param string $user_password Optional. User's chosen password. Default empty (random password).
36 * @param string $language Optional. Language chosen. Default empty.
37 * @return array Array keys 'url', 'user_id', 'password', and 'password_message'.
39 function wp_install( $blog_title, $user_name, $user_email, $public, $deprecated = '', $user_password = '', $language = '' ) {
40 if ( !empty( $deprecated ) )
41 _deprecated_argument( __FUNCTION__, '2.6' );
43 wp_check_mysql_version();
45 make_db_current_silent();
49 update_option('blogname', $blog_title);
50 update_option('admin_email', $user_email);
51 update_option('blog_public', $public);
54 update_option( 'WPLANG', $language );
57 $guessurl = wp_guess_url();
59 update_option('siteurl', $guessurl);
61 // If not a public blog, don't ping.
63 update_option('default_pingback_flag', 0);
66 * Create default user. If the user already exists, the user tables are
67 * being shared among blogs. Just set the role in that case.
69 $user_id = username_exists($user_name);
70 $user_password = trim($user_password);
71 $email_password = false;
72 if ( !$user_id && empty($user_password) ) {
73 $user_password = wp_generate_password( 12, false );
74 $message = __('<strong><em>Note that password</em></strong> carefully! It is a <em>random</em> password that was generated just for you.');
75 $user_id = wp_create_user($user_name, $user_password, $user_email);
76 update_user_option($user_id, 'default_password_nag', true, true);
77 $email_password = true;
78 } elseif ( ! $user_id ) {
79 // Password has been provided
80 $message = '<em>'.__('Your chosen password.').'</em>';
81 $user_id = wp_create_user($user_name, $user_password, $user_email);
83 $message = __('User already exists. Password inherited.');
86 $user = new WP_User($user_id);
87 $user->set_role('administrator');
89 wp_install_defaults($user_id);
91 wp_install_maybe_enable_pretty_permalinks();
93 flush_rewrite_rules();
95 wp_new_blog_notification($blog_title, $guessurl, $user_id, ($email_password ? $user_password : __('The password you chose during the install.') ) );
100 * Fires after a site is fully installed.
104 * @param WP_User $user The site owner.
106 do_action( 'wp_install', $user );
108 return array('url' => $guessurl, 'user_id' => $user_id, 'password' => $user_password, 'password_message' => $message);
112 if ( !function_exists('wp_install_defaults') ) :
114 * Creates the initial content for a newly-installed site.
116 * Adds the default "Uncategorized" category, the first post (with comment),
117 * first page, and default widgets for default theme for the current version.
122 * @global WP_Rewrite $wp_rewrite
123 * @global string $table_prefix
125 * @param int $user_id User ID.
127 function wp_install_defaults( $user_id ) {
128 global $wpdb, $wp_rewrite, $table_prefix;
131 $cat_name = __('Uncategorized');
132 /* translators: Default category slug */
133 $cat_slug = sanitize_title(_x('Uncategorized', 'Default category slug'));
135 if ( global_terms_enabled() ) {
136 $cat_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM {$wpdb->sitecategories} WHERE category_nicename = %s", $cat_slug ) );
137 if ( $cat_id == null ) {
138 $wpdb->insert( $wpdb->sitecategories, array('cat_ID' => 0, 'cat_name' => $cat_name, 'category_nicename' => $cat_slug, 'last_updated' => current_time('mysql', true)) );
139 $cat_id = $wpdb->insert_id;
141 update_option('default_category', $cat_id);
146 $wpdb->insert( $wpdb->terms, array('term_id' => $cat_id, 'name' => $cat_name, 'slug' => $cat_slug, 'term_group' => 0) );
147 $wpdb->insert( $wpdb->term_taxonomy, array('term_id' => $cat_id, 'taxonomy' => 'category', 'description' => '', 'parent' => 0, 'count' => 1));
148 $cat_tt_id = $wpdb->insert_id;
151 $now = current_time( 'mysql' );
152 $now_gmt = current_time( 'mysql', 1 );
153 $first_post_guid = get_option( 'home' ) . '/?p=1';
155 if ( is_multisite() ) {
156 $first_post = get_site_option( 'first_post' );
158 if ( ! $first_post ) {
159 /* translators: %s: site link */
160 $first_post = __( 'Welcome to %s. This is your first post. Edit or delete it, then start blogging!' );
163 $first_post = sprintf( $first_post,
164 sprintf( '<a href="%s">%s</a>', esc_url( network_home_url() ), get_current_site()->site_name )
167 // Back-compat for pre-4.4
168 $first_post = str_replace( 'SITE_URL', esc_url( network_home_url() ), $first_post );
169 $first_post = str_replace( 'SITE_NAME', get_current_site()->site_name, $first_post );
171 $first_post = __( 'Welcome to WordPress. This is your first post. Edit or delete it, then start writing!' );
174 $wpdb->insert( $wpdb->posts, array(
175 'post_author' => $user_id,
177 'post_date_gmt' => $now_gmt,
178 'post_content' => $first_post,
179 'post_excerpt' => '',
180 'post_title' => __('Hello world!'),
181 /* translators: Default post slug */
182 'post_name' => sanitize_title( _x('hello-world', 'Default post slug') ),
183 'post_modified' => $now,
184 'post_modified_gmt' => $now_gmt,
185 'guid' => $first_post_guid,
186 'comment_count' => 1,
189 'post_content_filtered' => ''
191 $wpdb->insert( $wpdb->term_relationships, array('term_taxonomy_id' => $cat_tt_id, 'object_id' => 1) );
194 $first_comment_author = __('Mr WordPress');
195 $first_comment_url = 'https://wordpress.org/';
196 $first_comment = __('Hi, this is a comment.
197 To delete a comment, just log in and view the post's comments. There you will have the option to edit or delete them.');
198 if ( is_multisite() ) {
199 $first_comment_author = get_site_option( 'first_comment_author', $first_comment_author );
200 $first_comment_url = get_site_option( 'first_comment_url', network_home_url() );
201 $first_comment = get_site_option( 'first_comment', $first_comment );
203 $wpdb->insert( $wpdb->comments, array(
204 'comment_post_ID' => 1,
205 'comment_author' => $first_comment_author,
206 'comment_author_email' => '',
207 'comment_author_url' => $first_comment_url,
208 'comment_date' => $now,
209 'comment_date_gmt' => $now_gmt,
210 'comment_content' => $first_comment
214 $first_page = sprintf( __( "This is an example page. It's different from a blog post because it will stay in one place and will show up in your site navigation (in most themes). Most people start with an About page that introduces them to potential site visitors. It might say something like this:
216 <blockquote>Hi there! I'm a bike messenger by day, aspiring actor by night, and this is my website. I live in Los Angeles, have a great dog named Jack, and I like piña coladas. (And gettin' caught in the rain.)</blockquote>
218 ...or something like this:
220 <blockquote>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.</blockquote>
222 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!" ), admin_url() );
223 if ( is_multisite() )
224 $first_page = get_site_option( 'first_page', $first_page );
225 $first_post_guid = get_option('home') . '/?page_id=2';
226 $wpdb->insert( $wpdb->posts, array(
227 'post_author' => $user_id,
229 'post_date_gmt' => $now_gmt,
230 'post_content' => $first_page,
231 'post_excerpt' => '',
232 'comment_status' => 'closed',
233 'post_title' => __( 'Sample Page' ),
234 /* translators: Default page slug */
235 'post_name' => __( 'sample-page' ),
236 'post_modified' => $now,
237 'post_modified_gmt' => $now_gmt,
238 'guid' => $first_post_guid,
239 'post_type' => 'page',
242 'post_content_filtered' => ''
244 $wpdb->insert( $wpdb->postmeta, array( 'post_id' => 2, 'meta_key' => '_wp_page_template', 'meta_value' => 'default' ) );
246 // Set up default widgets for default theme.
247 update_option( 'widget_search', array ( 2 => array ( 'title' => '' ), '_multiwidget' => 1 ) );
248 update_option( 'widget_recent-posts', array ( 2 => array ( 'title' => '', 'number' => 5 ), '_multiwidget' => 1 ) );
249 update_option( 'widget_recent-comments', array ( 2 => array ( 'title' => '', 'number' => 5 ), '_multiwidget' => 1 ) );
250 update_option( 'widget_archives', array ( 2 => array ( 'title' => '', 'count' => 0, 'dropdown' => 0 ), '_multiwidget' => 1 ) );
251 update_option( 'widget_categories', array ( 2 => array ( 'title' => '', 'count' => 0, 'hierarchical' => 0, 'dropdown' => 0 ), '_multiwidget' => 1 ) );
252 update_option( 'widget_meta', array ( 2 => array ( 'title' => '' ), '_multiwidget' => 1 ) );
253 update_option( 'sidebars_widgets', array ( 'wp_inactive_widgets' => array (), 'sidebar-1' => array ( 0 => 'search-2', 1 => 'recent-posts-2', 2 => 'recent-comments-2', 3 => 'archives-2', 4 => 'categories-2', 5 => 'meta-2', ), 'array_version' => 3 ) );
255 if ( ! is_multisite() )
256 update_user_meta( $user_id, 'show_welcome_panel', 1 );
257 elseif ( ! is_super_admin( $user_id ) && ! metadata_exists( 'user', $user_id, 'show_welcome_panel' ) )
258 update_user_meta( $user_id, 'show_welcome_panel', 2 );
260 if ( is_multisite() ) {
261 // Flush rules to pick up the new page.
263 $wp_rewrite->flush_rules();
265 $user = new WP_User($user_id);
266 $wpdb->update( $wpdb->options, array('option_value' => $user->user_email), array('option_name' => 'admin_email') );
268 // Remove all perms except for the login user.
269 $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE user_id != %d AND meta_key = %s", $user_id, $table_prefix.'user_level') );
270 $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE user_id != %d AND meta_key = %s", $user_id, $table_prefix.'capabilities') );
272 // Delete any caps that snuck into the previously active blog. (Hardcoded to blog 1 for now.) TODO: Get previous_blog_id.
273 if ( !is_super_admin( $user_id ) && $user_id != 1 )
274 $wpdb->delete( $wpdb->usermeta, array( 'user_id' => $user_id , 'meta_key' => $wpdb->base_prefix.'1_capabilities' ) );
280 * Maybe enable pretty permalinks on install.
282 * If after enabling pretty permalinks don't work, fallback to query-string permalinks.
286 * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
288 * @return bool Whether pretty permalinks are enabled. False otherwise.
290 function wp_install_maybe_enable_pretty_permalinks() {
293 // Bail if a permalink structure is already enabled.
294 if ( get_option( 'permalink_structure' ) ) {
299 * The Permalink structures to attempt.
301 * The first is designed for mod_rewrite or nginx rewriting.
303 * The second is PATHINFO-based permalinks for web server configurations
304 * without a true rewrite module enabled.
306 $permalink_structures = array(
307 '/%year%/%monthnum%/%day%/%postname%/',
308 '/index.php/%year%/%monthnum%/%day%/%postname%/'
311 foreach ( (array) $permalink_structures as $permalink_structure ) {
312 $wp_rewrite->set_permalink_structure( $permalink_structure );
315 * Flush rules with the hard option to force refresh of the web-server's
316 * rewrite config file (e.g. .htaccess or web.config).
318 $wp_rewrite->flush_rules( true );
320 // Test against a real WordPress Post, or if none were created, a random 404 page.
321 $test_url = get_permalink( 1 );
324 $test_url = home_url( '/wordpress-check-for-rewrites/' );
328 * Send a request to the site, and check whether
329 * the 'x-pingback' header is returned as expected.
331 * Uses wp_remote_get() instead of wp_remote_head() because web servers
332 * can block head requests.
334 $response = wp_remote_get( $test_url, array( 'timeout' => 5 ) );
335 $x_pingback_header = wp_remote_retrieve_header( $response, 'x-pingback' );
336 $pretty_permalinks = $x_pingback_header && $x_pingback_header === get_bloginfo( 'pingback_url' );
338 if ( $pretty_permalinks ) {
344 * If it makes it this far, pretty permalinks failed.
345 * Fallback to query-string permalinks.
347 $wp_rewrite->set_permalink_structure( '' );
348 $wp_rewrite->flush_rules( true );
353 if ( !function_exists('wp_new_blog_notification') ) :
355 * Notifies the site admin that the setup is complete.
357 * Sends an email with wp_mail to the new administrator that the site setup is complete,
358 * and provides them with a record of their login credentials.
362 * @param string $blog_title Blog title.
363 * @param string $blog_url Blog url.
364 * @param int $user_id User ID.
365 * @param string $password User's Password.
367 function wp_new_blog_notification($blog_title, $blog_url, $user_id, $password) {
368 $user = new WP_User( $user_id );
369 $email = $user->user_email;
370 $name = $user->user_login;
371 $login_url = wp_login_url();
372 $message = sprintf( __( "Your new WordPress site has been successfully set up at:
376 You can log in to the administrator account with the following information:
382 We hope you enjoy your new site. Thanks!
385 https://wordpress.org/
386 "), $blog_url, $name, $password, $login_url );
388 @wp_mail($email, __('New WordPress Site'), $message);
392 if ( !function_exists('wp_upgrade') ) :
394 * Runs WordPress Upgrade functions.
396 * Upgrades the database if needed during a site update.
400 * @global int $wp_current_db_version
401 * @global int $wp_db_version
402 * @global wpdb $wpdb WordPress database abstraction object.
404 function wp_upgrade() {
405 global $wp_current_db_version, $wp_db_version, $wpdb;
407 $wp_current_db_version = __get_option('db_version');
409 // We are up-to-date. Nothing to do.
410 if ( $wp_db_version == $wp_current_db_version )
413 if ( ! is_blog_installed() )
416 wp_check_mysql_version();
418 pre_schema_upgrade();
419 make_db_current_silent();
421 if ( is_multisite() && is_main_site() )
425 if ( is_multisite() ) {
426 if ( $wpdb->get_row( "SELECT blog_id FROM {$wpdb->blog_versions} WHERE blog_id = '{$wpdb->blogid}'" ) )
427 $wpdb->query( "UPDATE {$wpdb->blog_versions} SET db_version = '{$wp_db_version}' WHERE blog_id = '{$wpdb->blogid}'" );
429 $wpdb->query( "INSERT INTO {$wpdb->blog_versions} ( `blog_id` , `db_version` , `last_updated` ) VALUES ( '{$wpdb->blogid}', '{$wp_db_version}', NOW());" );
433 * Fires after a site is fully upgraded.
437 * @param int $wp_db_version The new $wp_db_version.
438 * @param int $wp_current_db_version The old (current) $wp_db_version.
440 do_action( 'wp_upgrade', $wp_db_version, $wp_current_db_version );
445 * Functions to be called in install and upgrade scripts.
447 * Contains conditional checks to determine which upgrade scripts to run,
448 * based on database version and WP version being updated-to.
452 * @global int $wp_current_db_version
453 * @global int $wp_db_version
455 function upgrade_all() {
456 global $wp_current_db_version, $wp_db_version;
457 $wp_current_db_version = __get_option('db_version');
459 // We are up-to-date. Nothing to do.
460 if ( $wp_db_version == $wp_current_db_version )
463 // If the version is not set in the DB, try to guess the version.
464 if ( empty($wp_current_db_version) ) {
465 $wp_current_db_version = 0;
467 // If the template option exists, we have 1.5.
468 $template = __get_option('template');
469 if ( !empty($template) )
470 $wp_current_db_version = 2541;
473 if ( $wp_current_db_version < 6039 )
474 upgrade_230_options_table();
478 if ( $wp_current_db_version < 2541 ) {
485 if ( $wp_current_db_version < 3308 )
488 if ( $wp_current_db_version < 4772 )
491 if ( $wp_current_db_version < 4351 )
494 if ( $wp_current_db_version < 5539 )
497 if ( $wp_current_db_version < 6124 )
498 upgrade_230_old_tables();
500 if ( $wp_current_db_version < 7499 )
503 if ( $wp_current_db_version < 7935 )
506 if ( $wp_current_db_version < 8201 )
509 if ( $wp_current_db_version < 8989 )
512 if ( $wp_current_db_version < 10360 )
515 if ( $wp_current_db_version < 11958 )
518 if ( $wp_current_db_version < 15260 )
521 if ( $wp_current_db_version < 19389 )
524 if ( $wp_current_db_version < 20080 )
527 if ( $wp_current_db_version < 22422 )
530 if ( $wp_current_db_version < 25824 )
533 if ( $wp_current_db_version < 26148 )
536 if ( $wp_current_db_version < 26691 )
539 if ( $wp_current_db_version < 29630 )
542 if ( $wp_current_db_version < 33055 )
545 if ( $wp_current_db_version < 33056 )
548 if ( $wp_current_db_version < 35700 )
551 maybe_disable_link_manager();
553 maybe_disable_automattic_widgets();
555 update_option( 'db_version', $wp_db_version );
556 update_option( 'db_upgraded', true );
560 * Execute changes made in WordPress 1.0.
564 * @global wpdb $wpdb WordPress database abstraction object.
566 function upgrade_100() {
569 // Get the title and ID of every post, post_name to check if it already has a value
570 $posts = $wpdb->get_results("SELECT ID, post_title, post_name FROM $wpdb->posts WHERE post_name = ''");
572 foreach ($posts as $post) {
573 if ('' == $post->post_name) {
574 $newtitle = sanitize_title($post->post_title);
575 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_name = %s WHERE ID = %d", $newtitle, $post->ID) );
580 $categories = $wpdb->get_results("SELECT cat_ID, cat_name, category_nicename FROM $wpdb->categories");
581 foreach ($categories as $category) {
582 if ('' == $category->category_nicename) {
583 $newtitle = sanitize_title($category->cat_name);
584 $wpdb->update( $wpdb->categories, array('category_nicename' => $newtitle), array('cat_ID' => $category->cat_ID) );
588 $sql = "UPDATE $wpdb->options
589 SET option_value = REPLACE(option_value, 'wp-links/links-images/', 'wp-images/links/')
590 WHERE option_name LIKE %s
591 AND option_value LIKE %s";
592 $wpdb->query( $wpdb->prepare( $sql, $wpdb->esc_like( 'links_rating_image' ) . '%', $wpdb->esc_like( 'wp-links/links-images/' ) . '%' ) );
594 $done_ids = $wpdb->get_results("SELECT DISTINCT post_id FROM $wpdb->post2cat");
596 $done_posts = array();
597 foreach ($done_ids as $done_id) :
598 $done_posts[] = $done_id->post_id;
600 $catwhere = ' AND ID NOT IN (' . implode(',', $done_posts) . ')';
605 $allposts = $wpdb->get_results("SELECT ID, post_category FROM $wpdb->posts WHERE post_category != '0' $catwhere");
607 foreach ($allposts as $post) {
608 // Check to see if it's already been imported
609 $cat = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->post2cat WHERE post_id = %d AND category_id = %d", $post->ID, $post->post_category) );
610 if (!$cat && 0 != $post->post_category) { // If there's no result
611 $wpdb->insert( $wpdb->post2cat, array('post_id' => $post->ID, 'category_id' => $post->post_category) );
618 * Execute changes made in WordPress 1.0.1.
622 * @global wpdb $wpdb WordPress database abstraction object.
624 function upgrade_101() {
627 // Clean up indices, add a few
628 add_clean_index($wpdb->posts, 'post_name');
629 add_clean_index($wpdb->posts, 'post_status');
630 add_clean_index($wpdb->categories, 'category_nicename');
631 add_clean_index($wpdb->comments, 'comment_approved');
632 add_clean_index($wpdb->comments, 'comment_post_ID');
633 add_clean_index($wpdb->links , 'link_category');
634 add_clean_index($wpdb->links , 'link_visible');
638 * Execute changes made in WordPress 1.2.
642 * @global wpdb $wpdb WordPress database abstraction object.
644 function upgrade_110() {
647 // Set user_nicename.
648 $users = $wpdb->get_results("SELECT ID, user_nickname, user_nicename FROM $wpdb->users");
649 foreach ($users as $user) {
650 if ('' == $user->user_nicename) {
651 $newname = sanitize_title($user->user_nickname);
652 $wpdb->update( $wpdb->users, array('user_nicename' => $newname), array('ID' => $user->ID) );
656 $users = $wpdb->get_results("SELECT ID, user_pass from $wpdb->users");
657 foreach ($users as $row) {
658 if (!preg_match('/^[A-Fa-f0-9]{32}$/', $row->user_pass)) {
659 $wpdb->update( $wpdb->users, array('user_pass' => md5($row->user_pass)), array('ID' => $row->ID) );
663 // Get the GMT offset, we'll use that later on
664 $all_options = get_alloptions_110();
666 $time_difference = $all_options->time_difference;
668 $server_time = time()+date('Z');
669 $weblogger_time = $server_time + $time_difference * HOUR_IN_SECONDS;
672 $diff_gmt_server = ($gmt_time - $server_time) / HOUR_IN_SECONDS;
673 $diff_weblogger_server = ($weblogger_time - $server_time) / HOUR_IN_SECONDS;
674 $diff_gmt_weblogger = $diff_gmt_server - $diff_weblogger_server;
675 $gmt_offset = -$diff_gmt_weblogger;
677 // Add a gmt_offset option, with value $gmt_offset
678 add_option('gmt_offset', $gmt_offset);
680 // Check if we already set the GMT fields (if we did, then
681 // MAX(post_date_gmt) can't be '0000-00-00 00:00:00'
682 // <michel_v> I just slapped myself silly for not thinking about it earlier
683 $got_gmt_fields = ! ($wpdb->get_var("SELECT MAX(post_date_gmt) FROM $wpdb->posts") == '0000-00-00 00:00:00');
685 if (!$got_gmt_fields) {
687 // Add or subtract time to all dates, to get GMT dates
688 $add_hours = intval($diff_gmt_weblogger);
689 $add_minutes = intval(60 * ($diff_gmt_weblogger - $add_hours));
690 $wpdb->query("UPDATE $wpdb->posts SET post_date_gmt = DATE_ADD(post_date, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)");
691 $wpdb->query("UPDATE $wpdb->posts SET post_modified = post_date");
692 $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'");
693 $wpdb->query("UPDATE $wpdb->comments SET comment_date_gmt = DATE_ADD(comment_date, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)");
694 $wpdb->query("UPDATE $wpdb->users SET user_registered = DATE_ADD(user_registered, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)");
700 * Execute changes made in WordPress 1.5.
704 * @global wpdb $wpdb WordPress database abstraction object.
706 function upgrade_130() {
709 // Remove extraneous backslashes.
710 $posts = $wpdb->get_results("SELECT ID, post_title, post_content, post_excerpt, guid, post_date, post_name, post_status, post_author FROM $wpdb->posts");
712 foreach ($posts as $post) {
713 $post_content = addslashes(deslash($post->post_content));
714 $post_title = addslashes(deslash($post->post_title));
715 $post_excerpt = addslashes(deslash($post->post_excerpt));
716 if ( empty($post->guid) )
717 $guid = get_permalink($post->ID);
721 $wpdb->update( $wpdb->posts, compact('post_title', 'post_content', 'post_excerpt', 'guid'), array('ID' => $post->ID) );
726 // Remove extraneous backslashes.
727 $comments = $wpdb->get_results("SELECT comment_ID, comment_author, comment_content FROM $wpdb->comments");
729 foreach ($comments as $comment) {
730 $comment_content = deslash($comment->comment_content);
731 $comment_author = deslash($comment->comment_author);
733 $wpdb->update($wpdb->comments, compact('comment_content', 'comment_author'), array('comment_ID' => $comment->comment_ID) );
737 // Remove extraneous backslashes.
738 $links = $wpdb->get_results("SELECT link_id, link_name, link_description FROM $wpdb->links");
740 foreach ($links as $link) {
741 $link_name = deslash($link->link_name);
742 $link_description = deslash($link->link_description);
744 $wpdb->update( $wpdb->links, compact('link_name', 'link_description'), array('link_id' => $link->link_id) );
748 $active_plugins = __get_option('active_plugins');
751 * If plugins are not stored in an array, they're stored in the old
752 * newline separated format. Convert to new format.
754 if ( !is_array( $active_plugins ) ) {
755 $active_plugins = explode("\n", trim($active_plugins));
756 update_option('active_plugins', $active_plugins);
760 $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optionvalues');
761 $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optiontypes');
762 $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optiongroups');
763 $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optiongroup_options');
765 // Update comments table to use comment_type
766 $wpdb->query("UPDATE $wpdb->comments SET comment_type='trackback', comment_content = REPLACE(comment_content, '<trackback />', '') WHERE comment_content LIKE '<trackback />%'");
767 $wpdb->query("UPDATE $wpdb->comments SET comment_type='pingback', comment_content = REPLACE(comment_content, '<pingback />', '') WHERE comment_content LIKE '<pingback />%'");
769 // Some versions have multiple duplicate option_name rows with the same values
770 $options = $wpdb->get_results("SELECT option_name, COUNT(option_name) AS dupes FROM `$wpdb->options` GROUP BY option_name");
771 foreach ( $options as $option ) {
772 if ( 1 != $option->dupes ) { // Could this be done in the query?
773 $limit = $option->dupes - 1;
774 $dupe_ids = $wpdb->get_col( $wpdb->prepare("SELECT option_id FROM $wpdb->options WHERE option_name = %s LIMIT %d", $option->option_name, $limit) );
776 $dupe_ids = join($dupe_ids, ',');
777 $wpdb->query("DELETE FROM $wpdb->options WHERE option_id IN ($dupe_ids)");
786 * Execute changes made in WordPress 2.0.
790 * @global wpdb $wpdb WordPress database abstraction object.
791 * @global int $wp_current_db_version
793 function upgrade_160() {
794 global $wpdb, $wp_current_db_version;
796 populate_roles_160();
798 $users = $wpdb->get_results("SELECT * FROM $wpdb->users");
799 foreach ( $users as $user ) :
800 if ( !empty( $user->user_firstname ) )
801 update_user_meta( $user->ID, 'first_name', wp_slash($user->user_firstname) );
802 if ( !empty( $user->user_lastname ) )
803 update_user_meta( $user->ID, 'last_name', wp_slash($user->user_lastname) );
804 if ( !empty( $user->user_nickname ) )
805 update_user_meta( $user->ID, 'nickname', wp_slash($user->user_nickname) );
806 if ( !empty( $user->user_level ) )
807 update_user_meta( $user->ID, $wpdb->prefix . 'user_level', $user->user_level );
808 if ( !empty( $user->user_icq ) )
809 update_user_meta( $user->ID, 'icq', wp_slash($user->user_icq) );
810 if ( !empty( $user->user_aim ) )
811 update_user_meta( $user->ID, 'aim', wp_slash($user->user_aim) );
812 if ( !empty( $user->user_msn ) )
813 update_user_meta( $user->ID, 'msn', wp_slash($user->user_msn) );
814 if ( !empty( $user->user_yim ) )
815 update_user_meta( $user->ID, 'yim', wp_slash($user->user_icq) );
816 if ( !empty( $user->user_description ) )
817 update_user_meta( $user->ID, 'description', wp_slash($user->user_description) );
819 if ( isset( $user->user_idmode ) ):
820 $idmode = $user->user_idmode;
821 if ($idmode == 'nickname') $id = $user->user_nickname;
822 if ($idmode == 'login') $id = $user->user_login;
823 if ($idmode == 'firstname') $id = $user->user_firstname;
824 if ($idmode == 'lastname') $id = $user->user_lastname;
825 if ($idmode == 'namefl') $id = $user->user_firstname.' '.$user->user_lastname;
826 if ($idmode == 'namelf') $id = $user->user_lastname.' '.$user->user_firstname;
827 if (!$idmode) $id = $user->user_nickname;
828 $wpdb->update( $wpdb->users, array('display_name' => $id), array('ID' => $user->ID) );
831 // FIXME: RESET_CAPS is temporary code to reset roles and caps if flag is set.
832 $caps = get_user_meta( $user->ID, $wpdb->prefix . 'capabilities');
833 if ( empty($caps) || defined('RESET_CAPS') ) {
834 $level = get_user_meta($user->ID, $wpdb->prefix . 'user_level', true);
835 $role = translate_level_to_role($level);
836 update_user_meta( $user->ID, $wpdb->prefix . 'capabilities', array($role => true) );
840 $old_user_fields = array( 'user_firstname', 'user_lastname', 'user_icq', 'user_aim', 'user_msn', 'user_yim', 'user_idmode', 'user_ip', 'user_domain', 'user_browser', 'user_description', 'user_nickname', 'user_level' );
841 $wpdb->hide_errors();
842 foreach ( $old_user_fields as $old )
843 $wpdb->query("ALTER TABLE $wpdb->users DROP $old");
844 $wpdb->show_errors();
846 // Populate comment_count field of posts table.
847 $comments = $wpdb->get_results( "SELECT comment_post_ID, COUNT(*) as c FROM $wpdb->comments WHERE comment_approved = '1' GROUP BY comment_post_ID" );
848 if ( is_array( $comments ) )
849 foreach ($comments as $comment)
850 $wpdb->update( $wpdb->posts, array('comment_count' => $comment->c), array('ID' => $comment->comment_post_ID) );
853 * Some alpha versions used a post status of object instead of attachment
854 * and put the mime type in post_type instead of post_mime_type.
856 if ( $wp_current_db_version > 2541 && $wp_current_db_version <= 3091 ) {
857 $objects = $wpdb->get_results("SELECT ID, post_type FROM $wpdb->posts WHERE post_status = 'object'");
858 foreach ($objects as $object) {
859 $wpdb->update( $wpdb->posts, array( 'post_status' => 'attachment',
860 'post_mime_type' => $object->post_type,
862 array( 'ID' => $object->ID ) );
864 $meta = get_post_meta($object->ID, 'imagedata', true);
865 if ( ! empty($meta['file']) )
866 update_attached_file( $object->ID, $meta['file'] );
872 * Execute changes made in WordPress 2.1.
876 * @global wpdb $wpdb WordPress database abstraction object.
877 * @global int $wp_current_db_version
879 function upgrade_210() {
880 global $wpdb, $wp_current_db_version;
882 if ( $wp_current_db_version < 3506 ) {
883 // Update status and type.
884 $posts = $wpdb->get_results("SELECT ID, post_status FROM $wpdb->posts");
886 if ( ! empty($posts) ) foreach ($posts as $post) {
887 $status = $post->post_status;
890 if ( 'static' == $status ) {
893 } elseif ( 'attachment' == $status ) {
895 $type = 'attachment';
898 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_status = %s, post_type = %s WHERE ID = %d", $status, $type, $post->ID) );
902 if ( $wp_current_db_version < 3845 ) {
903 populate_roles_210();
906 if ( $wp_current_db_version < 3531 ) {
907 // Give future posts a post_status of future.
908 $now = gmdate('Y-m-d H:i:59');
909 $wpdb->query ("UPDATE $wpdb->posts SET post_status = 'future' WHERE post_status = 'publish' AND post_date_gmt > '$now'");
911 $posts = $wpdb->get_results("SELECT ID, post_date FROM $wpdb->posts WHERE post_status ='future'");
912 if ( !empty($posts) )
913 foreach ( $posts as $post )
914 wp_schedule_single_event(mysql2date('U', $post->post_date, false), 'publish_future_post', array($post->ID));
919 * Execute changes made in WordPress 2.3.
923 * @global wpdb $wpdb WordPress database abstraction object.
924 * @global int $wp_current_db_version
926 function upgrade_230() {
927 global $wp_current_db_version, $wpdb;
929 if ( $wp_current_db_version < 5200 ) {
930 populate_roles_230();
933 // Convert categories to terms.
936 $categories = $wpdb->get_results("SELECT * FROM $wpdb->categories ORDER BY cat_ID");
937 foreach ($categories as $category) {
938 $term_id = (int) $category->cat_ID;
939 $name = $category->cat_name;
940 $description = $category->category_description;
941 $slug = $category->category_nicename;
942 $parent = $category->category_parent;
945 // Associate terms with the same slug in a term group and make slugs unique.
946 if ( $exists = $wpdb->get_results( $wpdb->prepare("SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $slug) ) ) {
947 $term_group = $exists[0]->term_group;
948 $id = $exists[0]->term_id;
951 $alt_slug = $slug . "-$num";
953 $slug_check = $wpdb->get_var( $wpdb->prepare("SELECT slug FROM $wpdb->terms WHERE slug = %s", $alt_slug) );
954 } while ( $slug_check );
958 if ( empty( $term_group ) ) {
959 $term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms GROUP BY term_group") + 1;
960 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->terms SET term_group = %d WHERE term_id = %d", $term_group, $id) );
964 $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->terms (term_id, name, slug, term_group) VALUES
965 (%d, %s, %s, %d)", $term_id, $name, $slug, $term_group) );
968 if ( !empty($category->category_count) ) {
969 $count = (int) $category->category_count;
970 $taxonomy = 'category';
971 $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, description, parent, count) VALUES ( %d, %s, %s, %d, %d)", $term_id, $taxonomy, $description, $parent, $count) );
972 $tt_ids[$term_id][$taxonomy] = (int) $wpdb->insert_id;
975 if ( !empty($category->link_count) ) {
976 $count = (int) $category->link_count;
977 $taxonomy = 'link_category';
978 $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, description, parent, count) VALUES ( %d, %s, %s, %d, %d)", $term_id, $taxonomy, $description, $parent, $count) );
979 $tt_ids[$term_id][$taxonomy] = (int) $wpdb->insert_id;
982 if ( !empty($category->tag_count) ) {
984 $count = (int) $category->tag_count;
985 $taxonomy = 'post_tag';
986 $wpdb->insert( $wpdb->term_taxonomy, compact('term_id', 'taxonomy', 'description', 'parent', 'count') );
987 $tt_ids[$term_id][$taxonomy] = (int) $wpdb->insert_id;
990 if ( empty($count) ) {
992 $taxonomy = 'category';
993 $wpdb->insert( $wpdb->term_taxonomy, compact('term_id', 'taxonomy', 'description', 'parent', 'count') );
994 $tt_ids[$term_id][$taxonomy] = (int) $wpdb->insert_id;
998 $select = 'post_id, category_id';
1000 $select .= ', rel_type';
1002 $posts = $wpdb->get_results("SELECT $select FROM $wpdb->post2cat GROUP BY post_id, category_id");
1003 foreach ( $posts as $post ) {
1004 $post_id = (int) $post->post_id;
1005 $term_id = (int) $post->category_id;
1006 $taxonomy = 'category';
1007 if ( !empty($post->rel_type) && 'tag' == $post->rel_type)
1009 $tt_id = $tt_ids[$term_id][$taxonomy];
1010 if ( empty($tt_id) )
1013 $wpdb->insert( $wpdb->term_relationships, array('object_id' => $post_id, 'term_taxonomy_id' => $tt_id) );
1016 // < 3570 we used linkcategories. >= 3570 we used categories and link2cat.
1017 if ( $wp_current_db_version < 3570 ) {
1019 * Create link_category terms for link categories. Create a map of link
1020 * cat IDs to link_category terms.
1022 $link_cat_id_map = array();
1023 $default_link_cat = 0;
1025 $link_cats = $wpdb->get_results("SELECT cat_id, cat_name FROM " . $wpdb->prefix . 'linkcategories');
1026 foreach ( $link_cats as $category) {
1027 $cat_id = (int) $category->cat_id;
1029 $name = wp_slash($category->cat_name);
1030 $slug = sanitize_title($name);
1033 // Associate terms with the same slug in a term group and make slugs unique.
1034 if ( $exists = $wpdb->get_results( $wpdb->prepare("SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $slug) ) ) {
1035 $term_group = $exists[0]->term_group;
1036 $term_id = $exists[0]->term_id;
1039 if ( empty($term_id) ) {
1040 $wpdb->insert( $wpdb->terms, compact('name', 'slug', 'term_group') );
1041 $term_id = (int) $wpdb->insert_id;
1044 $link_cat_id_map[$cat_id] = $term_id;
1045 $default_link_cat = $term_id;
1047 $wpdb->insert( $wpdb->term_taxonomy, array('term_id' => $term_id, 'taxonomy' => 'link_category', 'description' => '', 'parent' => 0, 'count' => 0) );
1048 $tt_ids[$term_id] = (int) $wpdb->insert_id;
1051 // Associate links to cats.
1052 $links = $wpdb->get_results("SELECT link_id, link_category FROM $wpdb->links");
1053 if ( !empty($links) ) foreach ( $links as $link ) {
1054 if ( 0 == $link->link_category )
1056 if ( ! isset($link_cat_id_map[$link->link_category]) )
1058 $term_id = $link_cat_id_map[$link->link_category];
1059 $tt_id = $tt_ids[$term_id];
1060 if ( empty($tt_id) )
1063 $wpdb->insert( $wpdb->term_relationships, array('object_id' => $link->link_id, 'term_taxonomy_id' => $tt_id) );
1066 // Set default to the last category we grabbed during the upgrade loop.
1067 update_option('default_link_category', $default_link_cat);
1069 $links = $wpdb->get_results("SELECT link_id, category_id FROM $wpdb->link2cat GROUP BY link_id, category_id");
1070 foreach ( $links as $link ) {
1071 $link_id = (int) $link->link_id;
1072 $term_id = (int) $link->category_id;
1073 $taxonomy = 'link_category';
1074 $tt_id = $tt_ids[$term_id][$taxonomy];
1075 if ( empty($tt_id) )
1077 $wpdb->insert( $wpdb->term_relationships, array('object_id' => $link_id, 'term_taxonomy_id' => $tt_id) );
1081 if ( $wp_current_db_version < 4772 ) {
1082 // Obsolete linkcategories table
1083 $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'linkcategories');
1086 // Recalculate all counts
1087 $terms = $wpdb->get_results("SELECT term_taxonomy_id, taxonomy FROM $wpdb->term_taxonomy");
1088 foreach ( (array) $terms as $term ) {
1089 if ( ('post_tag' == $term->taxonomy) || ('category' == $term->taxonomy) )
1090 $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) );
1092 $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $term->term_taxonomy_id) );
1093 $wpdb->update( $wpdb->term_taxonomy, array('count' => $count), array('term_taxonomy_id' => $term->term_taxonomy_id) );
1098 * Remove old options from the database.
1102 * @global wpdb $wpdb WordPress database abstraction object.
1104 function upgrade_230_options_table() {
1106 $old_options_fields = array( 'option_can_override', 'option_type', 'option_width', 'option_height', 'option_description', 'option_admin_level' );
1107 $wpdb->hide_errors();
1108 foreach ( $old_options_fields as $old )
1109 $wpdb->query("ALTER TABLE $wpdb->options DROP $old");
1110 $wpdb->show_errors();
1114 * Remove old categories, link2cat, and post2cat database tables.
1118 * @global wpdb $wpdb WordPress database abstraction object.
1120 function upgrade_230_old_tables() {
1122 $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'categories');
1123 $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'link2cat');
1124 $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'post2cat');
1128 * Upgrade old slugs made in version 2.2.
1132 * @global wpdb $wpdb WordPress database abstraction object.
1134 function upgrade_old_slugs() {
1135 // Upgrade people who were using the Redirect Old Slugs plugin.
1137 $wpdb->query("UPDATE $wpdb->postmeta SET meta_key = '_wp_old_slug' WHERE meta_key = 'old_slug'");
1141 * Execute changes made in WordPress 2.5.0.
1145 * @global int $wp_current_db_version
1147 function upgrade_250() {
1148 global $wp_current_db_version;
1150 if ( $wp_current_db_version < 6689 ) {
1151 populate_roles_250();
1157 * Execute changes made in WordPress 2.5.2.
1161 * @global wpdb $wpdb WordPress database abstraction object.
1163 function upgrade_252() {
1166 $wpdb->query("UPDATE $wpdb->users SET user_activation_key = ''");
1170 * Execute changes made in WordPress 2.6.
1174 * @global int $wp_current_db_version
1176 function upgrade_260() {
1177 global $wp_current_db_version;
1179 if ( $wp_current_db_version < 8000 )
1180 populate_roles_260();
1184 * Execute changes made in WordPress 2.7.
1188 * @global wpdb $wpdb WordPress database abstraction object.
1189 * @global int $wp_current_db_version
1191 function upgrade_270() {
1192 global $wpdb, $wp_current_db_version;
1194 if ( $wp_current_db_version < 8980 )
1195 populate_roles_270();
1197 // Update post_date for unpublished posts with empty timestamp
1198 if ( $wp_current_db_version < 8921 )
1199 $wpdb->query( "UPDATE $wpdb->posts SET post_date = post_modified WHERE post_date = '0000-00-00 00:00:00'" );
1203 * Execute changes made in WordPress 2.8.
1207 * @global int $wp_current_db_version
1208 * @global wpdb $wpdb WordPress database abstraction object.
1210 function upgrade_280() {
1211 global $wp_current_db_version, $wpdb;
1213 if ( $wp_current_db_version < 10360 )
1214 populate_roles_280();
1215 if ( is_multisite() ) {
1217 while( $rows = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options ORDER BY option_id LIMIT $start, 20" ) ) {
1218 foreach ( $rows as $row ) {
1219 $value = $row->option_value;
1220 if ( !@unserialize( $value ) )
1221 $value = stripslashes( $value );
1222 if ( $value !== $row->option_value ) {
1223 update_option( $row->option_name, $value );
1228 refresh_blog_details( $wpdb->blogid );
1233 * Execute changes made in WordPress 2.9.
1237 * @global int $wp_current_db_version
1239 function upgrade_290() {
1240 global $wp_current_db_version;
1242 if ( $wp_current_db_version < 11958 ) {
1243 // Previously, setting depth to 1 would redundantly disable threading, but now 2 is the minimum depth to avoid confusion
1244 if ( get_option( 'thread_comments_depth' ) == '1' ) {
1245 update_option( 'thread_comments_depth', 2 );
1246 update_option( 'thread_comments', 0 );
1252 * Execute changes made in WordPress 3.0.
1256 * @global int $wp_current_db_version
1257 * @global wpdb $wpdb WordPress database abstraction object.
1259 function upgrade_300() {
1260 global $wp_current_db_version, $wpdb;
1262 if ( $wp_current_db_version < 15093 )
1263 populate_roles_300();
1265 if ( $wp_current_db_version < 14139 && is_multisite() && is_main_site() && ! defined( 'MULTISITE' ) && get_site_option( 'siteurl' ) === false )
1266 add_site_option( 'siteurl', '' );
1268 // 3.0 screen options key name changes.
1269 if ( wp_should_upgrade_global_tables() ) {
1270 $sql = "DELETE FROM $wpdb->usermeta
1271 WHERE meta_key LIKE %s
1277 OR meta_key = 'manageedittagscolumnshidden'
1278 OR meta_key = 'managecategoriescolumnshidden'
1279 OR meta_key = 'manageedit-tagscolumnshidden'
1280 OR meta_key = 'manageeditcolumnshidden'
1281 OR meta_key = 'categories_per_page'
1282 OR meta_key = 'edit_tags_per_page'";
1283 $prefix = $wpdb->esc_like( $wpdb->base_prefix );
1284 $wpdb->query( $wpdb->prepare( $sql,
1285 $prefix . '%' . $wpdb->esc_like( 'meta-box-hidden' ) . '%',
1286 $prefix . '%' . $wpdb->esc_like( 'closedpostboxes' ) . '%',
1287 $prefix . '%' . $wpdb->esc_like( 'manage-' ) . '%' . $wpdb->esc_like( '-columns-hidden' ) . '%',
1288 $prefix . '%' . $wpdb->esc_like( 'meta-box-order' ) . '%',
1289 $prefix . '%' . $wpdb->esc_like( 'metaboxorder' ) . '%',
1290 $prefix . '%' . $wpdb->esc_like( 'screen_layout' ) . '%'
1297 * Execute changes made in WordPress 3.3.
1301 * @global int $wp_current_db_version
1302 * @global wpdb $wpdb
1303 * @global array $wp_registered_widgets
1304 * @global array $sidebars_widgets
1306 function upgrade_330() {
1307 global $wp_current_db_version, $wpdb, $wp_registered_widgets, $sidebars_widgets;
1309 if ( $wp_current_db_version < 19061 && wp_should_upgrade_global_tables() ) {
1310 $wpdb->query( "DELETE FROM $wpdb->usermeta WHERE meta_key IN ('show_admin_bar_admin', 'plugins_last_view')" );
1313 if ( $wp_current_db_version >= 11548 )
1316 $sidebars_widgets = get_option( 'sidebars_widgets', array() );
1317 $_sidebars_widgets = array();
1319 if ( isset($sidebars_widgets['wp_inactive_widgets']) || empty($sidebars_widgets) )
1320 $sidebars_widgets['array_version'] = 3;
1321 elseif ( !isset($sidebars_widgets['array_version']) )
1322 $sidebars_widgets['array_version'] = 1;
1324 switch ( $sidebars_widgets['array_version'] ) {
1326 foreach ( (array) $sidebars_widgets as $index => $sidebar )
1327 if ( is_array($sidebar) )
1328 foreach ( (array) $sidebar as $i => $name ) {
1329 $id = strtolower($name);
1330 if ( isset($wp_registered_widgets[$id]) ) {
1331 $_sidebars_widgets[$index][$i] = $id;
1334 $id = sanitize_title($name);
1335 if ( isset($wp_registered_widgets[$id]) ) {
1336 $_sidebars_widgets[$index][$i] = $id;
1342 foreach ( $wp_registered_widgets as $widget_id => $widget ) {
1343 if ( strtolower($widget['name']) == strtolower($name) ) {
1344 $_sidebars_widgets[$index][$i] = $widget['id'];
1347 } elseif ( sanitize_title($widget['name']) == sanitize_title($name) ) {
1348 $_sidebars_widgets[$index][$i] = $widget['id'];
1357 unset($_sidebars_widgets[$index][$i]);
1359 $_sidebars_widgets['array_version'] = 2;
1360 $sidebars_widgets = $_sidebars_widgets;
1361 unset($_sidebars_widgets);
1364 $sidebars_widgets = retrieve_widgets();
1365 $sidebars_widgets['array_version'] = 3;
1366 update_option( 'sidebars_widgets', $sidebars_widgets );
1371 * Execute changes made in WordPress 3.4.
1375 * @global int $wp_current_db_version
1376 * @global wpdb $wpdb
1378 function upgrade_340() {
1379 global $wp_current_db_version, $wpdb;
1381 if ( $wp_current_db_version < 19798 ) {
1382 $wpdb->hide_errors();
1383 $wpdb->query( "ALTER TABLE $wpdb->options DROP COLUMN blog_id" );
1384 $wpdb->show_errors();
1387 if ( $wp_current_db_version < 19799 ) {
1388 $wpdb->hide_errors();
1389 $wpdb->query("ALTER TABLE $wpdb->comments DROP INDEX comment_approved");
1390 $wpdb->show_errors();
1393 if ( $wp_current_db_version < 20022 && wp_should_upgrade_global_tables() ) {
1394 $wpdb->query( "DELETE FROM $wpdb->usermeta WHERE meta_key = 'themes_last_view'" );
1397 if ( $wp_current_db_version < 20080 ) {
1398 if ( 'yes' == $wpdb->get_var( "SELECT autoload FROM $wpdb->options WHERE option_name = 'uninstall_plugins'" ) ) {
1399 $uninstall_plugins = get_option( 'uninstall_plugins' );
1400 delete_option( 'uninstall_plugins' );
1401 add_option( 'uninstall_plugins', $uninstall_plugins, null, 'no' );
1407 * Execute changes made in WordPress 3.5.
1411 * @global int $wp_current_db_version
1412 * @global wpdb $wpdb
1414 function upgrade_350() {
1415 global $wp_current_db_version, $wpdb;
1417 if ( $wp_current_db_version < 22006 && $wpdb->get_var( "SELECT link_id FROM $wpdb->links LIMIT 1" ) )
1418 update_option( 'link_manager_enabled', 1 ); // Previously set to 0 by populate_options()
1420 if ( $wp_current_db_version < 21811 && wp_should_upgrade_global_tables() ) {
1421 $meta_keys = array();
1422 foreach ( array_merge( get_post_types(), get_taxonomies() ) as $name ) {
1423 if ( false !== strpos( $name, '-' ) )
1424 $meta_keys[] = 'edit_' . str_replace( '-', '_', $name ) . '_per_page';
1427 $meta_keys = implode( "', '", $meta_keys );
1428 $wpdb->query( "DELETE FROM $wpdb->usermeta WHERE meta_key IN ('$meta_keys')" );
1432 if ( $wp_current_db_version < 22422 && $term = get_term_by( 'slug', 'post-format-standard', 'post_format' ) )
1433 wp_delete_term( $term->term_id, 'post_format' );
1437 * Execute changes made in WordPress 3.7.
1441 * @global int $wp_current_db_version
1443 function upgrade_370() {
1444 global $wp_current_db_version;
1445 if ( $wp_current_db_version < 25824 )
1446 wp_clear_scheduled_hook( 'wp_auto_updates_maybe_update' );
1450 * Execute changes made in WordPress 3.7.2.
1455 * @global int $wp_current_db_version
1457 function upgrade_372() {
1458 global $wp_current_db_version;
1459 if ( $wp_current_db_version < 26148 )
1460 wp_clear_scheduled_hook( 'wp_maybe_auto_update' );
1464 * Execute changes made in WordPress 3.8.0.
1468 * @global int $wp_current_db_version
1470 function upgrade_380() {
1471 global $wp_current_db_version;
1472 if ( $wp_current_db_version < 26691 ) {
1473 deactivate_plugins( array( 'mp6/mp6.php' ), true );
1478 * Execute changes made in WordPress 4.0.0.
1482 * @global int $wp_current_db_version
1484 function upgrade_400() {
1485 global $wp_current_db_version;
1486 if ( $wp_current_db_version < 29630 ) {
1487 if ( ! is_multisite() && false === get_option( 'WPLANG' ) ) {
1488 if ( defined( 'WPLANG' ) && ( '' !== WPLANG ) && in_array( WPLANG, get_available_languages() ) ) {
1489 update_option( 'WPLANG', WPLANG );
1491 update_option( 'WPLANG', '' );
1498 * Execute changes made in WordPress 4.2.0.
1502 * @global int $wp_current_db_version
1503 * @global wpdb $wpdb
1505 function upgrade_420() {}
1508 * Executes changes made in WordPress 4.3.0.
1512 * @global int $wp_current_db_version Current version.
1513 * @global wpdb $wpdb WordPress database abstraction object.
1515 function upgrade_430() {
1516 global $wp_current_db_version, $wpdb;
1518 if ( $wp_current_db_version < 32364 ) {
1519 upgrade_430_fix_comments();
1522 // Shared terms are split in a separate process.
1523 if ( $wp_current_db_version < 32814 ) {
1524 update_option( 'finished_splitting_shared_terms', 0 );
1525 wp_schedule_single_event( time() + ( 1 * MINUTE_IN_SECONDS ), 'wp_split_shared_term_batch' );
1528 if ( $wp_current_db_version < 33055 && 'utf8mb4' === $wpdb->charset ) {
1529 if ( is_multisite() ) {
1530 $tables = $wpdb->tables( 'blog' );
1532 $tables = $wpdb->tables( 'all' );
1533 if ( ! wp_should_upgrade_global_tables() ) {
1534 $global_tables = $wpdb->tables( 'global' );
1535 $tables = array_diff_assoc( $tables, $global_tables );
1539 foreach ( $tables as $table ) {
1540 maybe_convert_table_to_utf8mb4( $table );
1546 * Executes comments changes made in WordPress 4.3.0.
1550 * @global int $wp_current_db_version Current version.
1551 * @global wpdb $wpdb WordPress database abstraction object.
1553 function upgrade_430_fix_comments() {
1554 global $wp_current_db_version, $wpdb;
1556 $content_length = $wpdb->get_col_length( $wpdb->comments, 'comment_content' );
1558 if ( is_wp_error( $content_length ) ) {
1562 if ( false === $content_length ) {
1563 $content_length = array(
1567 } elseif ( ! is_array( $content_length ) ) {
1568 $length = (int) $content_length > 0 ? (int) $content_length : 65535;
1569 $content_length = array(
1575 if ( 'byte' !== $content_length['type'] || 0 === $content_length['length'] ) {
1576 // Sites with malformed DB schemas are on their own.
1580 $allowed_length = intval( $content_length['length'] ) - 10;
1582 $comments = $wpdb->get_results(
1583 "SELECT `comment_ID` FROM `{$wpdb->comments}`
1584 WHERE `comment_date_gmt` > '2015-04-26'
1585 AND LENGTH( `comment_content` ) >= {$allowed_length}
1586 AND ( `comment_content` LIKE '%<%' OR `comment_content` LIKE '%>%' )"
1589 foreach ( $comments as $comment ) {
1590 wp_delete_comment( $comment->comment_ID, true );
1595 * Executes changes made in WordPress 4.3.1.
1599 function upgrade_431() {
1600 // Fix incorrect cron entries for term splitting
1601 $cron_array = _get_cron_array();
1602 if ( isset( $cron_array['wp_batch_split_terms'] ) ) {
1603 unset( $cron_array['wp_batch_split_terms'] );
1604 _set_cron_array( $cron_array );
1609 * Executes changes made in WordPress 4.4.0.
1613 * @global int $wp_current_db_version Current version.
1614 * @global wpdb $wpdb WordPress database abstraction object.
1616 function upgrade_440() {
1617 global $wp_current_db_version, $wpdb;
1619 if ( $wp_current_db_version < 34030 ) {
1620 $wpdb->query( "ALTER TABLE {$wpdb->options} MODIFY option_name VARCHAR(191)" );
1623 // Remove the unused 'add_users' role.
1624 $roles = wp_roles();
1625 foreach ( $roles->role_objects as $role ) {
1626 if ( $role->has_cap( 'add_users' ) ) {
1627 $role->remove_cap( 'add_users' );
1633 * Executes network-level upgrade routines.
1637 * @global int $wp_current_db_version
1638 * @global wpdb $wpdb
1640 function upgrade_network() {
1641 global $wp_current_db_version, $wpdb;
1644 if ( is_main_network() ) {
1646 * Deletes all expired transients. The multi-table delete syntax is used
1647 * to delete the transient record from table a, and the corresponding
1648 * transient_timeout record from table b.
1651 $sql = "DELETE a, b FROM $wpdb->sitemeta a, $wpdb->sitemeta b
1652 WHERE a.meta_key LIKE %s
1653 AND a.meta_key NOT LIKE %s
1654 AND b.meta_key = CONCAT( '_site_transient_timeout_', SUBSTRING( a.meta_key, 17 ) )
1655 AND b.meta_value < %d";
1656 $wpdb->query( $wpdb->prepare( $sql, $wpdb->esc_like( '_site_transient_' ) . '%', $wpdb->esc_like ( '_site_transient_timeout_' ) . '%', $time ) );
1660 if ( $wp_current_db_version < 11549 ) {
1661 $wpmu_sitewide_plugins = get_site_option( 'wpmu_sitewide_plugins' );
1662 $active_sitewide_plugins = get_site_option( 'active_sitewide_plugins' );
1663 if ( $wpmu_sitewide_plugins ) {
1664 if ( !$active_sitewide_plugins )
1665 $sitewide_plugins = (array) $wpmu_sitewide_plugins;
1667 $sitewide_plugins = array_merge( (array) $active_sitewide_plugins, (array) $wpmu_sitewide_plugins );
1669 update_site_option( 'active_sitewide_plugins', $sitewide_plugins );
1671 delete_site_option( 'wpmu_sitewide_plugins' );
1672 delete_site_option( 'deactivated_sitewide_plugins' );
1675 while( $rows = $wpdb->get_results( "SELECT meta_key, meta_value FROM {$wpdb->sitemeta} ORDER BY meta_id LIMIT $start, 20" ) ) {
1676 foreach ( $rows as $row ) {
1677 $value = $row->meta_value;
1678 if ( !@unserialize( $value ) )
1679 $value = stripslashes( $value );
1680 if ( $value !== $row->meta_value ) {
1681 update_site_option( $row->meta_key, $value );
1689 if ( $wp_current_db_version < 13576 )
1690 update_site_option( 'global_terms_enabled', '1' );
1693 if ( $wp_current_db_version < 19390 )
1694 update_site_option( 'initial_db_version', $wp_current_db_version );
1696 if ( $wp_current_db_version < 19470 ) {
1697 if ( false === get_site_option( 'active_sitewide_plugins' ) )
1698 update_site_option( 'active_sitewide_plugins', array() );
1702 if ( $wp_current_db_version < 20148 ) {
1703 // 'allowedthemes' keys things by stylesheet. 'allowed_themes' keyed things by name.
1704 $allowedthemes = get_site_option( 'allowedthemes' );
1705 $allowed_themes = get_site_option( 'allowed_themes' );
1706 if ( false === $allowedthemes && is_array( $allowed_themes ) && $allowed_themes ) {
1707 $converted = array();
1708 $themes = wp_get_themes();
1709 foreach ( $themes as $stylesheet => $theme_data ) {
1710 if ( isset( $allowed_themes[ $theme_data->get('Name') ] ) )
1711 $converted[ $stylesheet ] = true;
1713 update_site_option( 'allowedthemes', $converted );
1714 delete_site_option( 'allowed_themes' );
1719 if ( $wp_current_db_version < 21823 )
1720 update_site_option( 'ms_files_rewriting', '1' );
1723 if ( $wp_current_db_version < 24448 ) {
1724 $illegal_names = get_site_option( 'illegal_names' );
1725 if ( is_array( $illegal_names ) && count( $illegal_names ) === 1 ) {
1726 $illegal_name = reset( $illegal_names );
1727 $illegal_names = explode( ' ', $illegal_name );
1728 update_site_option( 'illegal_names', $illegal_names );
1733 if ( $wp_current_db_version < 31351 && $wpdb->charset === 'utf8mb4' ) {
1734 if ( wp_should_upgrade_global_tables() ) {
1735 $wpdb->query( "ALTER TABLE $wpdb->usermeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" );
1736 $wpdb->query( "ALTER TABLE $wpdb->site DROP INDEX domain, ADD INDEX domain(domain(140),path(51))" );
1737 $wpdb->query( "ALTER TABLE $wpdb->sitemeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" );
1738 $wpdb->query( "ALTER TABLE $wpdb->signups DROP INDEX domain_path, ADD INDEX domain_path(domain(140),path(51))" );
1740 $tables = $wpdb->tables( 'global' );
1742 // sitecategories may not exist.
1743 if ( ! $wpdb->get_var( "SHOW TABLES LIKE '{$tables['sitecategories']}'" ) ) {
1744 unset( $tables['sitecategories'] );
1747 foreach ( $tables as $table ) {
1748 maybe_convert_table_to_utf8mb4( $table );
1754 if ( $wp_current_db_version < 33055 && 'utf8mb4' === $wpdb->charset ) {
1755 if ( wp_should_upgrade_global_tables() ) {
1757 $indexes = $wpdb->get_results( "SHOW INDEXES FROM $wpdb->signups" );
1758 foreach ( $indexes as $index ) {
1759 if ( 'domain_path' == $index->Key_name && 'domain' == $index->Column_name && 140 != $index->Sub_part ) {
1766 $wpdb->query( "ALTER TABLE $wpdb->signups DROP INDEX domain_path, ADD INDEX domain_path(domain(140),path(51))" );
1769 $tables = $wpdb->tables( 'global' );
1771 // sitecategories may not exist.
1772 if ( ! $wpdb->get_var( "SHOW TABLES LIKE '{$tables['sitecategories']}'" ) ) {
1773 unset( $tables['sitecategories'] );
1776 foreach ( $tables as $table ) {
1777 maybe_convert_table_to_utf8mb4( $table );
1784 // General functions we use to actually do stuff
1788 * Creates a table in the database if it doesn't already exist.
1790 * This method checks for an existing database and creates a new one if it's not
1791 * already present. It doesn't rely on MySQL's "IF NOT EXISTS" statement, but chooses
1792 * to query all tables first and then run the SQL statement creating the table.
1796 * @global wpdb $wpdb
1798 * @param string $table_name Database table name to create.
1799 * @param string $create_ddl SQL statement to create table.
1800 * @return bool If table already exists or was created by function.
1802 function maybe_create_table($table_name, $create_ddl) {
1805 $query = $wpdb->prepare( "SHOW TABLES LIKE %s", $wpdb->esc_like( $table_name ) );
1807 if ( $wpdb->get_var( $query ) == $table_name ) {
1811 // Didn't find it try to create it..
1812 $wpdb->query($create_ddl);
1814 // We cannot directly tell that whether this succeeded!
1815 if ( $wpdb->get_var( $query ) == $table_name ) {
1822 * Drops a specified index from a table.
1826 * @global wpdb $wpdb
1828 * @param string $table Database table name.
1829 * @param string $index Index name to drop.
1830 * @return true True, when finished.
1832 function drop_index($table, $index) {
1834 $wpdb->hide_errors();
1835 $wpdb->query("ALTER TABLE `$table` DROP INDEX `$index`");
1836 // Now we need to take out all the extra ones we may have created
1837 for ($i = 0; $i < 25; $i++) {
1838 $wpdb->query("ALTER TABLE `$table` DROP INDEX `{$index}_$i`");
1840 $wpdb->show_errors();
1845 * Adds an index to a specified table.
1849 * @global wpdb $wpdb
1851 * @param string $table Database table name.
1852 * @param string $index Database table index column.
1853 * @return true True, when done with execution.
1855 function add_clean_index($table, $index) {
1857 drop_index($table, $index);
1858 $wpdb->query("ALTER TABLE `$table` ADD INDEX ( `$index` )");
1863 * Adds column to a database table if it doesn't already exist.
1867 * @global wpdb $wpdb
1869 * @param string $table_name The table name to modify.
1870 * @param string $column_name The column name to add to the table.
1871 * @param string $create_ddl The SQL statement used to add the column.
1872 * @return bool True if already exists or on successful completion, false on error.
1874 function maybe_add_column($table_name, $column_name, $create_ddl) {
1876 foreach ($wpdb->get_col("DESC $table_name", 0) as $column ) {
1877 if ($column == $column_name) {
1882 // Didn't find it try to create it.
1883 $wpdb->query($create_ddl);
1885 // We cannot directly tell that whether this succeeded!
1886 foreach ($wpdb->get_col("DESC $table_name", 0) as $column ) {
1887 if ($column == $column_name) {
1895 * If a table only contains utf8 or utf8mb4 columns, convert it to utf8mb4.
1899 * @global wpdb $wpdb
1901 * @param string $table The table to convert.
1902 * @return bool true if the table was converted, false if it wasn't.
1904 function maybe_convert_table_to_utf8mb4( $table ) {
1907 $results = $wpdb->get_results( "SHOW FULL COLUMNS FROM `$table`" );
1912 foreach ( $results as $column ) {
1913 if ( $column->Collation ) {
1914 list( $charset ) = explode( '_', $column->Collation );
1915 $charset = strtolower( $charset );
1916 if ( 'utf8' !== $charset && 'utf8mb4' !== $charset ) {
1917 // Don't upgrade tables that have non-utf8 columns.
1923 $table_details = $wpdb->get_row( "SHOW TABLE STATUS LIKE '$table'" );
1924 if ( ! $table_details ) {
1928 list( $table_charset ) = explode( '_', $table_details->Collation );
1929 $table_charset = strtolower( $table_charset );
1930 if ( 'utf8mb4' === $table_charset ) {
1934 return $wpdb->query( "ALTER TABLE $table CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci" );
1938 * Retrieve all options as it was for 1.2.
1942 * @global wpdb $wpdb
1944 * @return stdClass List of options.
1946 function get_alloptions_110() {
1948 $all_options = new stdClass;
1949 if ( $options = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options" ) ) {
1950 foreach ( $options as $option ) {
1951 if ( 'siteurl' == $option->option_name || 'home' == $option->option_name || 'category_base' == $option->option_name )
1952 $option->option_value = untrailingslashit( $option->option_value );
1953 $all_options->{$option->option_name} = stripslashes( $option->option_value );
1956 return $all_options;
1960 * Utility version of get_option that is private to install/upgrade.
1966 * @global wpdb $wpdb
1968 * @param string $setting Option name.
1971 function __get_option($setting) {
1974 if ( $setting == 'home' && defined( 'WP_HOME' ) )
1975 return untrailingslashit( WP_HOME );
1977 if ( $setting == 'siteurl' && defined( 'WP_SITEURL' ) )
1978 return untrailingslashit( WP_SITEURL );
1980 $option = $wpdb->get_var( $wpdb->prepare("SELECT option_value FROM $wpdb->options WHERE option_name = %s", $setting ) );
1982 if ( 'home' == $setting && '' == $option )
1983 return __get_option( 'siteurl' );
1985 if ( 'siteurl' == $setting || 'home' == $setting || 'category_base' == $setting || 'tag_base' == $setting )
1986 $option = untrailingslashit( $option );
1988 return maybe_unserialize( $option );
1992 * Filters for content to remove unnecessary slashes.
1996 * @param string $content The content to modify.
1997 * @return string The de-slashed content.
1999 function deslash($content) {
2000 // Note: \\\ inside a regex denotes a single backslash.
2003 * Replace one or more backslashes followed by a single quote with
2006 $content = preg_replace("/\\\+'/", "'", $content);
2009 * Replace one or more backslashes followed by a double quote with
2012 $content = preg_replace('/\\\+"/', '"', $content);
2014 // Replace one or more backslashes with one backslash.
2015 $content = preg_replace("/\\\+/", "\\", $content);
2021 * Modifies the database based on specified SQL statements.
2023 * Useful for creating new tables and updating existing tables to a new structure.
2027 * @global wpdb $wpdb
2029 * @param string|array $queries Optional. The query to run. Can be multiple queries
2030 * in an array, or a string of queries separated by
2031 * semicolons. Default empty.
2032 * @param bool $execute Optional. Whether or not to execute the query right away.
2034 * @return array Strings containing the results of the various update queries.
2036 function dbDelta( $queries = '', $execute = true ) {
2039 if ( in_array( $queries, array( '', 'all', 'blog', 'global', 'ms_global' ), true ) )
2040 $queries = wp_get_db_schema( $queries );
2042 // Separate individual queries into an array
2043 if ( !is_array($queries) ) {
2044 $queries = explode( ';', $queries );
2045 $queries = array_filter( $queries );
2049 * Filter the dbDelta SQL queries.
2053 * @param array $queries An array of dbDelta SQL queries.
2055 $queries = apply_filters( 'dbdelta_queries', $queries );
2057 $cqueries = array(); // Creation Queries
2058 $iqueries = array(); // Insertion Queries
2059 $for_update = array();
2061 // Create a tablename index for an array ($cqueries) of queries
2062 foreach ($queries as $qry) {
2063 if ( preg_match( "|CREATE TABLE ([^ ]*)|", $qry, $matches ) ) {
2064 $cqueries[ trim( $matches[1], '`' ) ] = $qry;
2065 $for_update[$matches[1]] = 'Created table '.$matches[1];
2066 } elseif ( preg_match( "|CREATE DATABASE ([^ ]*)|", $qry, $matches ) ) {
2067 array_unshift( $cqueries, $qry );
2068 } elseif ( preg_match( "|INSERT INTO ([^ ]*)|", $qry, $matches ) ) {
2070 } elseif ( preg_match( "|UPDATE ([^ ]*)|", $qry, $matches ) ) {
2073 // Unrecognized query type
2078 * Filter the dbDelta SQL queries for creating tables and/or databases.
2080 * Queries filterable via this hook contain "CREATE TABLE" or "CREATE DATABASE".
2084 * @param array $cqueries An array of dbDelta create SQL queries.
2086 $cqueries = apply_filters( 'dbdelta_create_queries', $cqueries );
2089 * Filter the dbDelta SQL queries for inserting or updating.
2091 * Queries filterable via this hook contain "INSERT INTO" or "UPDATE".
2095 * @param array $iqueries An array of dbDelta insert or update SQL queries.
2097 $iqueries = apply_filters( 'dbdelta_insert_queries', $iqueries );
2099 $global_tables = $wpdb->tables( 'global' );
2100 foreach ( $cqueries as $table => $qry ) {
2101 // Upgrade global tables only for the main site. Don't upgrade at all if conditions are not optimal.
2102 if ( in_array( $table, $global_tables ) && ! wp_should_upgrade_global_tables() ) {
2103 unset( $cqueries[ $table ], $for_update[ $table ] );
2107 // Fetch the table column structure from the database
2108 $suppress = $wpdb->suppress_errors();
2109 $tablefields = $wpdb->get_results("DESCRIBE {$table};");
2110 $wpdb->suppress_errors( $suppress );
2112 if ( ! $tablefields )
2115 // Clear the field and index arrays.
2116 $cfields = $indices = array();
2118 // Get all of the field names in the query from between the parentheses.
2119 preg_match("|\((.*)\)|ms", $qry, $match2);
2120 $qryline = trim($match2[1]);
2122 // Separate field lines into an array.
2123 $flds = explode("\n", $qryline);
2125 // todo: Remove this?
2126 //echo "<hr/><pre>\n".print_r(strtolower($table), true).":\n".print_r($cqueries, true)."</pre><hr/>";
2128 // For every field line specified in the query.
2129 foreach ($flds as $fld) {
2131 // Extract the field name.
2132 preg_match("|^([^ ]*)|", trim($fld), $fvals);
2133 $fieldname = trim( $fvals[1], '`' );
2135 // Verify the found field name.
2137 switch (strtolower($fieldname)) {
2144 $validfield = false;
2145 $indices[] = trim(trim($fld), ", \n");
2150 // If it's a valid field, add it to the field array.
2152 $cfields[strtolower($fieldname)] = trim($fld, ", \n");
2156 // For every field in the table.
2157 foreach ($tablefields as $tablefield) {
2159 // If the table field exists in the field array ...
2160 if (array_key_exists(strtolower($tablefield->Field), $cfields)) {
2162 // Get the field type from the query.
2163 preg_match("|".$tablefield->Field." ([^ ]*( unsigned)?)|i", $cfields[strtolower($tablefield->Field)], $matches);
2164 $fieldtype = $matches[1];
2166 // Is actual field type different from the field type in query?
2167 if ($tablefield->Type != $fieldtype) {
2168 // Add a query to change the column type
2169 $cqueries[] = "ALTER TABLE {$table} CHANGE COLUMN {$tablefield->Field} " . $cfields[strtolower($tablefield->Field)];
2170 $for_update[$table.'.'.$tablefield->Field] = "Changed type of {$table}.{$tablefield->Field} from {$tablefield->Type} to {$fieldtype}";
2173 // Get the default value from the array
2174 // todo: Remove this?
2175 //echo "{$cfields[strtolower($tablefield->Field)]}<br>";
2176 if (preg_match("| DEFAULT '(.*?)'|i", $cfields[strtolower($tablefield->Field)], $matches)) {
2177 $default_value = $matches[1];
2178 if ($tablefield->Default != $default_value) {
2179 // Add a query to change the column's default value
2180 $cqueries[] = "ALTER TABLE {$table} ALTER COLUMN {$tablefield->Field} SET DEFAULT '{$default_value}'";
2181 $for_update[$table.'.'.$tablefield->Field] = "Changed default value of {$table}.{$tablefield->Field} from {$tablefield->Default} to {$default_value}";
2185 // Remove the field from the array (so it's not added).
2186 unset($cfields[strtolower($tablefield->Field)]);
2188 // This field exists in the table, but not in the creation queries?
2192 // For every remaining field specified for the table.
2193 foreach ($cfields as $fieldname => $fielddef) {
2194 // Push a query line into $cqueries that adds the field to that table.
2195 $cqueries[] = "ALTER TABLE {$table} ADD COLUMN $fielddef";
2196 $for_update[$table.'.'.$fieldname] = 'Added column '.$table.'.'.$fieldname;
2199 // Index stuff goes here. Fetch the table index structure from the database.
2200 $tableindices = $wpdb->get_results("SHOW INDEX FROM {$table};");
2202 if ($tableindices) {
2203 // Clear the index array.
2204 $index_ary = array();
2206 // For every index in the table.
2207 foreach ($tableindices as $tableindex) {
2209 // Add the index to the index data array.
2210 $keyname = $tableindex->Key_name;
2211 $index_ary[$keyname]['columns'][] = array('fieldname' => $tableindex->Column_name, 'subpart' => $tableindex->Sub_part);
2212 $index_ary[$keyname]['unique'] = ($tableindex->Non_unique == 0)?true:false;
2213 $index_ary[$keyname]['index_type'] = $tableindex->Index_type;
2216 // For each actual index in the index array.
2217 foreach ($index_ary as $index_name => $index_data) {
2219 // Build a create string to compare to the query.
2221 if ($index_name == 'PRIMARY') {
2222 $index_string .= 'PRIMARY ';
2223 } elseif ( $index_data['unique'] ) {
2224 $index_string .= 'UNIQUE ';
2226 if ( 'FULLTEXT' === strtoupper( $index_data['index_type'] ) ) {
2227 $index_string .= 'FULLTEXT ';
2229 $index_string .= 'KEY ';
2230 if ($index_name != 'PRIMARY') {
2231 $index_string .= $index_name;
2233 $index_columns = '';
2235 // For each column in the index.
2236 foreach ($index_data['columns'] as $column_data) {
2237 if ($index_columns != '') $index_columns .= ',';
2239 // Add the field to the column list string.
2240 $index_columns .= $column_data['fieldname'];
2241 if ($column_data['subpart'] != '') {
2242 $index_columns .= '('.$column_data['subpart'].')';
2246 // The alternative index string doesn't care about subparts
2247 $alt_index_columns = preg_replace( '/\([^)]*\)/', '', $index_columns );
2249 // Add the column list to the index create string.
2250 $index_strings = array(
2251 "$index_string ($index_columns)",
2252 "$index_string ($alt_index_columns)",
2255 foreach ( $index_strings as $index_string ) {
2256 if ( ! ( ( $aindex = array_search( $index_string, $indices ) ) === false ) ) {
2257 unset( $indices[ $aindex ] );
2259 // todo: Remove this?
2260 //echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">{$table}:<br />Found index:".$index_string."</pre>\n";
2263 // todo: Remove this?
2264 //else echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">{$table}:<br /><b>Did not find index:</b>".$index_string."<br />".print_r($indices, true)."</pre>\n";
2268 // For every remaining index specified for the table.
2269 foreach ( (array) $indices as $index ) {
2270 // Push a query line into $cqueries that adds the index to that table.
2271 $cqueries[] = "ALTER TABLE {$table} ADD $index";
2272 $for_update[] = 'Added index ' . $table . ' ' . $index;
2275 // Remove the original table creation query from processing.
2276 unset( $cqueries[ $table ], $for_update[ $table ] );
2279 $allqueries = array_merge($cqueries, $iqueries);
2281 foreach ($allqueries as $query) {
2282 // todo: Remove this?
2283 //echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">".print_r($query, true)."</pre>\n";
2284 $wpdb->query($query);
2292 * Updates the database tables to a new schema.
2294 * By default, updates all the tables to use the latest defined schema, but can also
2295 * be used to update a specific set of tables in wp_get_db_schema().
2301 * @param string $tables Optional. Which set of tables to update. Default is 'all'.
2303 function make_db_current( $tables = 'all' ) {
2304 $alterations = dbDelta( $tables );
2306 foreach ($alterations as $alteration) echo "<li>$alteration</li>\n";
2311 * Updates the database tables to a new schema, but without displaying results.
2313 * By default, updates all the tables to use the latest defined schema, but can
2314 * also be used to update a specific set of tables in wp_get_db_schema().
2318 * @see make_db_current()
2320 * @param string $tables Optional. Which set of tables to update. Default is 'all'.
2322 function make_db_current_silent( $tables = 'all' ) {
2327 * Creates a site theme from an existing theme.
2329 * {@internal Missing Long Description}}
2333 * @param string $theme_name The name of the theme.
2334 * @param string $template The directory name of the theme.
2337 function make_site_theme_from_oldschool($theme_name, $template) {
2338 $home_path = get_home_path();
2339 $site_dir = WP_CONTENT_DIR . "/themes/$template";
2341 if (! file_exists("$home_path/index.php"))
2345 * Copy files from the old locations to the site theme.
2346 * TODO: This does not copy arbitrary include dependencies. Only the standard WP files are copied.
2348 $files = array('index.php' => 'index.php', 'wp-layout.css' => 'style.css', 'wp-comments.php' => 'comments.php', 'wp-comments-popup.php' => 'comments-popup.php');
2350 foreach ($files as $oldfile => $newfile) {
2351 if ($oldfile == 'index.php')
2352 $oldpath = $home_path;
2356 // Check to make sure it's not a new index.
2357 if ($oldfile == 'index.php') {
2358 $index = implode('', file("$oldpath/$oldfile"));
2359 if (strpos($index, 'WP_USE_THEMES') !== false) {
2360 if (! @copy(WP_CONTENT_DIR . '/themes/' . WP_DEFAULT_THEME . '/index.php', "$site_dir/$newfile"))
2363 // Don't copy anything.
2368 if (! @copy("$oldpath/$oldfile", "$site_dir/$newfile"))
2371 chmod("$site_dir/$newfile", 0777);
2373 // Update the blog header include in each file.
2374 $lines = explode("\n", implode('', file("$site_dir/$newfile")));
2376 $f = fopen("$site_dir/$newfile", 'w');
2378 foreach ($lines as $line) {
2379 if (preg_match('/require.*wp-blog-header/', $line))
2380 $line = '//' . $line;
2382 // Update stylesheet references.
2383 $line = str_replace("<?php echo __get_option('siteurl'); ?>/wp-layout.css", "<?php bloginfo('stylesheet_url'); ?>", $line);
2385 // Update comments template inclusion.
2386 $line = str_replace("<?php include(ABSPATH . 'wp-comments.php'); ?>", "<?php comments_template(); ?>", $line);
2388 fwrite($f, "{$line}\n");
2394 // Add a theme header.
2395 $header = "/*\nTheme Name: $theme_name\nTheme URI: " . __get_option('siteurl') . "\nDescription: A theme automatically created by the update.\nVersion: 1.0\nAuthor: Moi\n*/\n";
2397 $stylelines = file_get_contents("$site_dir/style.css");
2399 $f = fopen("$site_dir/style.css", 'w');
2401 fwrite($f, $header);
2402 fwrite($f, $stylelines);
2410 * Creates a site theme from the default theme.
2412 * {@internal Missing Long Description}}
2416 * @param string $theme_name The name of the theme.
2417 * @param string $template The directory name of the theme.
2418 * @return false|void
2420 function make_site_theme_from_default($theme_name, $template) {
2421 $site_dir = WP_CONTENT_DIR . "/themes/$template";
2422 $default_dir = WP_CONTENT_DIR . '/themes/' . WP_DEFAULT_THEME;
2424 // Copy files from the default theme to the site theme.
2425 //$files = array('index.php', 'comments.php', 'comments-popup.php', 'footer.php', 'header.php', 'sidebar.php', 'style.css');
2427 $theme_dir = @ opendir($default_dir);
2429 while(($theme_file = readdir( $theme_dir )) !== false) {
2430 if (is_dir("$default_dir/$theme_file"))
2432 if (! @copy("$default_dir/$theme_file", "$site_dir/$theme_file"))
2434 chmod("$site_dir/$theme_file", 0777);
2437 @closedir($theme_dir);
2439 // Rewrite the theme header.
2440 $stylelines = explode("\n", implode('', file("$site_dir/style.css")));
2442 $f = fopen("$site_dir/style.css", 'w');
2444 foreach ($stylelines as $line) {
2445 if (strpos($line, 'Theme Name:') !== false) $line = 'Theme Name: ' . $theme_name;
2446 elseif (strpos($line, 'Theme URI:') !== false) $line = 'Theme URI: ' . __get_option('url');
2447 elseif (strpos($line, 'Description:') !== false) $line = 'Description: Your theme.';
2448 elseif (strpos($line, 'Version:') !== false) $line = 'Version: 1';
2449 elseif (strpos($line, 'Author:') !== false) $line = 'Author: You';
2450 fwrite($f, $line . "\n");
2457 if (! mkdir("$site_dir/images", 0777)) {
2461 $images_dir = @ opendir("$default_dir/images");
2463 while(($image = readdir($images_dir)) !== false) {
2464 if (is_dir("$default_dir/images/$image"))
2466 if (! @copy("$default_dir/images/$image", "$site_dir/images/$image"))
2468 chmod("$site_dir/images/$image", 0777);
2471 @closedir($images_dir);
2475 * Creates a site theme.
2477 * {@internal Missing Long Description}}
2481 * @return false|string
2483 function make_site_theme() {
2484 // Name the theme after the blog.
2485 $theme_name = __get_option('blogname');
2486 $template = sanitize_title($theme_name);
2487 $site_dir = WP_CONTENT_DIR . "/themes/$template";
2489 // If the theme already exists, nothing to do.
2490 if ( is_dir($site_dir)) {
2494 // We must be able to write to the themes dir.
2495 if (! is_writable(WP_CONTENT_DIR . "/themes")) {
2500 if (! mkdir($site_dir, 0777)) {
2504 if (file_exists(ABSPATH . 'wp-layout.css')) {
2505 if (! make_site_theme_from_oldschool($theme_name, $template)) {
2506 // TODO: rm -rf the site theme directory.
2510 if (! make_site_theme_from_default($theme_name, $template))
2511 // TODO: rm -rf the site theme directory.
2515 // Make the new site theme active.
2516 $current_template = __get_option('template');
2517 if ($current_template == WP_DEFAULT_THEME) {
2518 update_option('template', $template);
2519 update_option('stylesheet', $template);
2525 * Translate user level to user role name.
2529 * @param int $level User level.
2530 * @return string User role name.
2532 function translate_level_to_role($level) {
2537 return 'administrator';
2547 return 'contributor';
2549 return 'subscriber';
2554 * Checks the version of the installed MySQL binary.
2558 * @global wpdb $wpdb
2560 function wp_check_mysql_version() {
2562 $result = $wpdb->check_database_version();
2563 if ( is_wp_error( $result ) )
2564 die( $result->get_error_message() );
2568 * Disables the Automattic widgets plugin, which was merged into core.
2572 function maybe_disable_automattic_widgets() {
2573 $plugins = __get_option( 'active_plugins' );
2575 foreach ( (array) $plugins as $plugin ) {
2576 if ( basename( $plugin ) == 'widgets.php' ) {
2577 array_splice( $plugins, array_search( $plugin, $plugins ), 1 );
2578 update_option( 'active_plugins', $plugins );
2585 * Disables the Link Manager on upgrade if, at the time of upgrade, no links exist in the DB.
2589 * @global int $wp_current_db_version
2590 * @global wpdb $wpdb WordPress database abstraction object.
2592 function maybe_disable_link_manager() {
2593 global $wp_current_db_version, $wpdb;
2595 if ( $wp_current_db_version >= 22006 && get_option( 'link_manager_enabled' ) && ! $wpdb->get_var( "SELECT link_id FROM $wpdb->links LIMIT 1" ) )
2596 update_option( 'link_manager_enabled', 0 );
2600 * Runs before the schema is upgraded.
2604 * @global int $wp_current_db_version
2605 * @global wpdb $wpdb WordPress database abstraction object.
2607 function pre_schema_upgrade() {
2608 global $wp_current_db_version, $wpdb;
2610 // Upgrade versions prior to 2.9
2611 if ( $wp_current_db_version < 11557 ) {
2612 // Delete duplicate options. Keep the option with the highest option_id.
2613 $wpdb->query("DELETE o1 FROM $wpdb->options AS o1 JOIN $wpdb->options AS o2 USING (`option_name`) WHERE o2.option_id > o1.option_id");
2615 // Drop the old primary key and add the new.
2616 $wpdb->query("ALTER TABLE $wpdb->options DROP PRIMARY KEY, ADD PRIMARY KEY(option_id)");
2618 // Drop the old option_name index. dbDelta() doesn't do the drop.
2619 $wpdb->query("ALTER TABLE $wpdb->options DROP INDEX option_name");
2622 // Multisite schema upgrades.
2623 if ( $wp_current_db_version < 25448 && is_multisite() && wp_should_upgrade_global_tables() ) {
2625 // Upgrade verions prior to 3.7
2626 if ( $wp_current_db_version < 25179 ) {
2627 // New primary key for signups.
2628 $wpdb->query( "ALTER TABLE $wpdb->signups ADD signup_id BIGINT(20) NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST" );
2629 $wpdb->query( "ALTER TABLE $wpdb->signups DROP INDEX domain" );
2632 if ( $wp_current_db_version < 25448 ) {
2633 // Convert archived from enum to tinyint.
2634 $wpdb->query( "ALTER TABLE $wpdb->blogs CHANGE COLUMN archived archived varchar(1) NOT NULL default '0'" );
2635 $wpdb->query( "ALTER TABLE $wpdb->blogs CHANGE COLUMN archived archived tinyint(2) NOT NULL default 0" );
2639 // Upgrade versions prior to 4.2.
2640 if ( $wp_current_db_version < 31351 ) {
2641 if ( ! is_multisite() && wp_should_upgrade_global_tables() ) {
2642 $wpdb->query( "ALTER TABLE $wpdb->usermeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" );
2644 $wpdb->query( "ALTER TABLE $wpdb->terms DROP INDEX slug, ADD INDEX slug(slug(191))" );
2645 $wpdb->query( "ALTER TABLE $wpdb->terms DROP INDEX name, ADD INDEX name(name(191))" );
2646 $wpdb->query( "ALTER TABLE $wpdb->commentmeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" );
2647 $wpdb->query( "ALTER TABLE $wpdb->postmeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" );
2648 $wpdb->query( "ALTER TABLE $wpdb->posts DROP INDEX post_name, ADD INDEX post_name(post_name(191))" );
2651 // Upgrade versions prior to 4.4.
2652 if ( $wp_current_db_version < 34978 ) {
2653 // If compatible termmeta table is found, use it, but enforce a proper index and update collation.
2654 if ( $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->termmeta}'" ) && $wpdb->get_results( "SHOW INDEX FROM {$wpdb->termmeta} WHERE Column_name = 'meta_key'" ) ) {
2655 $wpdb->query( "ALTER TABLE $wpdb->termmeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" );
2656 maybe_convert_table_to_utf8mb4( $wpdb->termmeta );
2662 * Install global terms.
2666 * @global wpdb $wpdb
2667 * @global string $charset_collate
2669 if ( !function_exists( 'install_global_terms' ) ) :
2670 function install_global_terms() {
2671 global $wpdb, $charset_collate;
2673 CREATE TABLE $wpdb->sitecategories (
2674 cat_ID bigint(20) NOT NULL auto_increment,
2675 cat_name varchar(55) NOT NULL default '',
2676 category_nicename varchar(200) NOT NULL default '',
2677 last_updated timestamp NOT NULL,
2678 PRIMARY KEY (cat_ID),
2679 KEY category_nicename (category_nicename),
2680 KEY last_updated (last_updated)
2683 // now create tables
2684 dbDelta( $ms_queries );
2689 * Determine if global tables should be upgraded.
2691 * This function performs a series of checks to ensure the environment allows
2692 * for the safe upgrading of global WordPress database tables. It is necessary
2693 * because global tables will commonly grow to millions of rows on large
2694 * installations, and the ability to control their upgrade routines can be
2695 * critical to the operation of large networks.
2697 * In a future iteration, this function may use `wp_is_large_network()` to more-
2698 * intelligently prevent global table upgrades. Until then, we make sure
2699 * WordPress is on the main site of the main network, to avoid running queries
2700 * more than once in multi-site or multi-network environments.
2704 * @return bool Whether to run the upgrade routines on global tables.
2706 function wp_should_upgrade_global_tables() {
2708 // Return false early if explicitly not upgrading
2709 if ( defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) ) {
2713 // Assume global tables should be upgraded
2714 $should_upgrade = true;
2716 // Set to false if not on main network (does not matter if not multi-network)
2717 if ( ! is_main_network() ) {
2718 $should_upgrade = false;
2721 // Set to false if not on main site of current network (does not matter if not multi-site)
2722 if ( ! is_main_site() ) {
2723 $should_upgrade = false;
2727 * Filter if upgrade routines should be run on global tables.
2729 * @param bool $should_upgrade Whether to run the upgrade routines on global tables.
2731 return apply_filters( 'wp_should_upgrade_global_tables', $should_upgrade );