[ Index ]

WordPress Source Cross Reference

title

Body

[close]

/wp-admin/ -> upgrade-functions.php (source)

   1  <?php
   2  
   3  if ( file_exists(ABSPATH . 'wp-content/install.php') )
   4      require (ABSPATH . 'wp-content/install.php');
   5  require_once (ABSPATH . '/wp-admin/admin-functions.php');
   6  require_once (ABSPATH . '/wp-admin/admin-db.php');
   7  require_once (ABSPATH . '/wp-admin/upgrade-schema.php');
   8  require_once(ABSPATH . WPINC . '/registration.php');
   9  
  10  if ( !function_exists('wp_install') ) :
  11  function wp_install($blog_title, $user_name, $user_email, $public, $meta='') {
  12      global $wp_rewrite;
  13  
  14      wp_cache_flush();
  15      make_db_current_silent();
  16      populate_options();
  17      populate_roles();
  18  
  19      update_option('blogname', $blog_title);
  20      update_option('admin_email', $user_email);
  21      update_option('blog_public', $public);
  22      $schema = ( isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ) ? 'https://' : 'http://';
  23      $guessurl = preg_replace('|/wp-admin/.*|i', '', $schema . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
  24      update_option('siteurl', $guessurl);
  25  
  26      // If not a public blog, don't ping.
  27      if ( ! $public )
  28          update_option('default_pingback_flag', 0);
  29  
  30      // Create default user.  If the user already exists, the user tables are
  31      // being shared among blogs.  Just set the role in that case.
  32      $user_id = username_exists($user_name);
  33      if ( !$user_id ) {
  34          $random_password = substr(md5(uniqid(microtime())), 0, 6);
  35          $user_id = wp_create_user($user_name, $random_password, $user_email);
  36      } else {
  37          $random_password = __('User already exists.  Password inherited.');
  38      }
  39  
  40      $user = new WP_User($user_id);
  41      $user->set_role('administrator');
  42  
  43      wp_install_defaults($user_id);
  44  
  45      $wp_rewrite->flush_rules();
  46  
  47      wp_new_blog_notification($blog_title, $guessurl, $user_id, $random_password);
  48  
  49      wp_cache_flush();
  50  
  51      return array('url' => $guessurl, 'user_id' => $user_id, 'password' => $random_password);
  52  }
  53  endif;
  54  
  55  if ( !function_exists('wp_install_defaults') ) :
  56  function wp_install_defaults($user_id) {
  57      global $wpdb;
  58  
  59      // Default category
  60      $wpdb->query("INSERT INTO $wpdb->categories (cat_ID, cat_name, category_nicename, category_count, category_description) VALUES ('0', '".$wpdb->escape(__('Uncategorized'))."', '".sanitize_title(__('Uncategorized'))."', '1', '')");
  61  
  62      // Default link category
  63      $wpdb->query("INSERT INTO $wpdb->categories (cat_ID, cat_name, category_nicename, link_count, category_description) VALUES ('0', '".$wpdb->escape(__('Blogroll'))."', '".sanitize_title(__('Blogroll'))."', '7', '')");
  64  
  65      // Now drop in some default links
  66      $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://inphotos.org/', 'Donncha', 0, 'http://inphotos.org/feed/', '');");
  67      $wpdb->query( "INSERT INTO $wpdb->link2cat (`link_id`, `category_id`) VALUES (1, 2)" );
  68  
  69      $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://zengun.org/weblog/', 'Michel', 0, 'http://zengun.org/weblog/feed/', '');");
  70      $wpdb->query( "INSERT INTO $wpdb->link2cat (`link_id`, `category_id`) VALUES (2, 2)" );
  71  
  72      $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://boren.nu/', 'Ryan', 0, 'http://boren.nu/feed/', '');");
  73      $wpdb->query( "INSERT INTO $wpdb->link2cat (`link_id`, `category_id`) VALUES (3, 2)" );
  74  
  75      $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://photomatt.net/', 'Matt', 0, 'http://xml.photomatt.net/feed/', '');");
  76      $wpdb->query( "INSERT INTO $wpdb->link2cat (`link_id`, `category_id`) VALUES (4, 2)" );
  77  
  78      $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://zed1.com/journalized/', 'Mike', 0, 'http://zed1.com/journalized/feed/', '');");
  79      $wpdb->query( "INSERT INTO $wpdb->link2cat (`link_id`, `category_id`) VALUES (5, 2)" );
  80  
  81      $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://www.alexking.org/', 'Alex', 0, 'http://www.alexking.org/blog/wp-rss2.php', '');");
  82      $wpdb->query( "INSERT INTO $wpdb->link2cat (`link_id`, `category_id`) VALUES (6, 2)" );
  83  
  84      $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://dougal.gunters.org/', 'Dougal', 0, 'http://dougal.gunters.org/feed/', '');");
  85      $wpdb->query( "INSERT INTO $wpdb->link2cat (`link_id`, `category_id`) VALUES (7, 2)" );
  86  
  87      // First post
  88      $now = date('Y-m-d H:i:s');
  89      $now_gmt = gmdate('Y-m-d H:i:s');
  90      $wpdb->query("INSERT INTO $wpdb->posts (post_author, post_date, post_date_gmt, post_content, post_excerpt, post_title, post_category, post_name, post_modified, post_modified_gmt, comment_count, to_ping, pinged, post_content_filtered) VALUES ($user_id, '$now', '$now_gmt', '".$wpdb->escape(__('Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!'))."', '', '".$wpdb->escape(__('Hello world!'))."', '0', '".$wpdb->escape(__('hello-world'))."', '$now', '$now_gmt', '1', '', '', '')");
  91  
  92      $wpdb->query( "INSERT INTO $wpdb->post2cat (`rel_id`, `post_id`, `category_id`) VALUES (1, 1, 1)" );
  93  
  94      // Default comment
  95      $wpdb->query("INSERT INTO $wpdb->comments (comment_post_ID, comment_author, comment_author_email, comment_author_url, comment_date, comment_date_gmt, comment_content) VALUES ('1', '".$wpdb->escape(__('Mr WordPress'))."', '', 'http://wordpress.org/', '$now', '$now_gmt', '".$wpdb->escape(__('Hi, this is a comment.<br />To delete a comment, just log in, and view the posts\' comments, there you will have the option to edit or delete them.'))."')");
  96  
  97      // First Page
  98  
  99      $wpdb->query("INSERT INTO $wpdb->posts (post_author, post_date, post_date_gmt, post_content, post_excerpt, post_title, post_category, post_name, post_modified, post_modified_gmt, post_status, post_type, to_ping, pinged, post_content_filtered) VALUES ($user_id, '$now', '$now_gmt', '".$wpdb->escape(__('This is an example of a WordPress page, you could edit this to put information about yourself or your site so readers know where you are coming from. You can create as many pages like this one or sub-pages as you like and manage all of your content inside of WordPress.'))."', '', '".$wpdb->escape(__('About'))."', '0', '".$wpdb->escape(__('about'))."', '$now', '$now_gmt', 'publish', 'page', '', '', '')");
 100  }
 101  endif;
 102  
 103  if ( !function_exists('wp_new_blog_notification') ) :
 104  function wp_new_blog_notification($blog_title, $blog_url, $user_id, $password) {
 105      $user = new WP_User($user_id);
 106      $email = $user->user_email;
 107      $name = $user->user_login;
 108      $message_headers = 'From: ' . $blog_title . ' <wordpress@' . $_SERVER['SERVER_NAME'] . '>';
 109      $message = sprintf(__("Your new WordPress blog has been successfully set up at:
 110  
 111  %1\$s
 112  
 113  You can log in to the administrator account with the following information:
 114  
 115  Username: %2\$s
 116  Password: %3\$s
 117  
 118  We hope you enjoy your new weblog. Thanks!
 119  
 120  --The WordPress Team
 121  http://wordpress.org/
 122  "), $blog_url, $name, $password);
 123  
 124      @wp_mail($email, __('New WordPress Blog'), $message, $message_headers);    
 125  }
 126  endif;
 127  
 128  if ( !function_exists('wp_upgrade') ) :
 129  function wp_upgrade() {
 130      global $wp_current_db_version, $wp_db_version;
 131  
 132      $wp_current_db_version = __get_option('db_version');
 133  
 134      // We are up-to-date.  Nothing to do.
 135      if ( $wp_db_version == $wp_current_db_version )
 136          return;
 137  
 138      wp_cache_flush();
 139      make_db_current_silent();
 140      upgrade_all();
 141      wp_cache_flush();    
 142  }
 143  endif;
 144  
 145  // Functions to be called in install and upgrade scripts
 146  function upgrade_all() {
 147      global $wp_current_db_version, $wp_db_version, $wp_rewrite;
 148      $wp_current_db_version = __get_option('db_version');
 149  
 150      // We are up-to-date.  Nothing to do.
 151      if ( $wp_db_version == $wp_current_db_version )
 152          return;
 153  
 154      // If the version is not set in the DB, try to guess the version.
 155      if ( empty($wp_current_db_version) ) {
 156          $wp_current_db_version = 0;
 157  
 158          // If the template option exists, we have 1.5.
 159          $template = __get_option('template');
 160          if ( !empty($template) )
 161              $wp_current_db_version = 2541;
 162      }
 163  
 164      populate_options();
 165  
 166      if ( $wp_current_db_version < 2541 ) {
 167          upgrade_100();
 168          upgrade_101();
 169          upgrade_110();
 170          upgrade_130();
 171      }
 172  
 173      if ( $wp_current_db_version < 3308 )
 174          upgrade_160();
 175  
 176      if ( $wp_current_db_version < 3845 )
 177          upgrade_210();
 178  
 179      $wp_rewrite->flush_rules();
 180  
 181      update_option('db_version', $wp_db_version);
 182  }
 183  
 184  function upgrade_100() {
 185      global $wpdb;
 186  
 187      // Get the title and ID of every post, post_name to check if it already has a value
 188      $posts = $wpdb->get_results("SELECT ID, post_title, post_name FROM $wpdb->posts WHERE post_name = ''");
 189      if ($posts) {
 190          foreach($posts as $post) {
 191              if ('' == $post->post_name) { 
 192                  $newtitle = sanitize_title($post->post_title);
 193                  $wpdb->query("UPDATE $wpdb->posts SET post_name = '$newtitle' WHERE ID = '$post->ID'");
 194              }
 195          }
 196      }
 197  
 198      $categories = $wpdb->get_results("SELECT cat_ID, cat_name, category_nicename FROM $wpdb->categories");
 199      foreach ($categories as $category) {
 200          if ('' == $category->category_nicename) { 
 201              $newtitle = sanitize_title($category->cat_name);
 202              $wpdb->query("UPDATE $wpdb->categories SET category_nicename = '$newtitle' WHERE cat_ID = '$category->cat_ID'");
 203          }
 204      }
 205  
 206  
 207      $wpdb->query("UPDATE $wpdb->options SET option_value = REPLACE(option_value, 'wp-links/links-images/', 'wp-images/links/')
 208      WHERE option_name LIKE 'links_rating_image%'
 209      AND option_value LIKE 'wp-links/links-images/%'");
 210  
 211      $done_ids = $wpdb->get_results("SELECT DISTINCT post_id FROM $wpdb->post2cat");
 212      if ($done_ids) :
 213          foreach ($done_ids as $done_id) :
 214              $done_posts[] = $done_id->post_id;
 215          endforeach;
 216          $catwhere = ' AND ID NOT IN (' . implode(',', $done_posts) . ')';
 217      else:
 218          $catwhere = '';
 219      endif;
 220  
 221      $allposts = $wpdb->get_results("SELECT ID, post_category FROM $wpdb->posts WHERE post_category != '0' $catwhere");
 222      if ($allposts) :
 223          foreach ($allposts as $post) {
 224              // Check to see if it's already been imported
 225              $cat = $wpdb->get_row("SELECT * FROM $wpdb->post2cat WHERE post_id = $post->ID AND category_id = $post->post_category");
 226              if (!$cat && 0 != $post->post_category) { // If there's no result
 227                  $wpdb->query("
 228                      INSERT INTO $wpdb->post2cat
 229                      (post_id, category_id)
 230                      VALUES
 231                      ('$post->ID', '$post->post_category')
 232                      ");
 233              }
 234          }
 235      endif;
 236  }
 237  
 238  function upgrade_101() {
 239      global $wpdb;
 240  
 241      // Clean up indices, add a few
 242      add_clean_index($wpdb->posts, 'post_name');
 243      add_clean_index($wpdb->posts, 'post_status');
 244      add_clean_index($wpdb->categories, 'category_nicename');
 245      add_clean_index($wpdb->comments, 'comment_approved');
 246      add_clean_index($wpdb->comments, 'comment_post_ID');
 247      add_clean_index($wpdb->links , 'link_category');
 248      add_clean_index($wpdb->links , 'link_visible');
 249  }
 250  
 251  
 252  function upgrade_110() {
 253      global $wpdb;
 254  
 255      // Set user_nicename.
 256      $users = $wpdb->get_results("SELECT ID, user_nickname, user_nicename FROM $wpdb->users");
 257       foreach ($users as $user) {
 258           if ('' == $user->user_nicename) { 
 259               $newname = sanitize_title($user->user_nickname);
 260               $wpdb->query("UPDATE $wpdb->users SET user_nicename = '$newname' WHERE ID = '$user->ID'");
 261           }
 262       }
 263  
 264      $users = $wpdb->get_results("SELECT ID, user_pass from $wpdb->users");
 265      foreach ($users as $row) {
 266          if (!preg_match('/^[A-Fa-f0-9]{32}$/', $row->user_pass)) {
 267                 $wpdb->query('UPDATE '.$wpdb->users.' SET user_pass = MD5(\''.$row->user_pass.'\') WHERE ID = \''.$row->ID.'\'');
 268          }
 269      }
 270  
 271  
 272      // Get the GMT offset, we'll use that later on
 273      $all_options = get_alloptions_110();
 274  
 275      $time_difference = $all_options->time_difference;
 276  
 277      $server_time = time()+date('Z');
 278      $weblogger_time = $server_time + $time_difference*3600;
 279      $gmt_time = time();
 280  
 281      $diff_gmt_server = ($gmt_time - $server_time) / 3600;
 282      $diff_weblogger_server = ($weblogger_time - $server_time) / 3600;
 283      $diff_gmt_weblogger = $diff_gmt_server - $diff_weblogger_server;
 284      $gmt_offset = -$diff_gmt_weblogger;
 285  
 286      // Add a gmt_offset option, with value $gmt_offset
 287      add_option('gmt_offset', $gmt_offset);
 288  
 289      // Check if we already set the GMT fields (if we did, then
 290      // MAX(post_date_gmt) can't be '0000-00-00 00:00:00'
 291      // <michel_v> I just slapped myself silly for not thinking about it earlier
 292      $got_gmt_fields = ($wpdb->get_var("SELECT MAX(post_date_gmt) FROM $wpdb->posts") == '0000-00-00 00:00:00') ? false : true;
 293  
 294      if (!$got_gmt_fields) {
 295  
 296          // Add or substract time to all dates, to get GMT dates
 297          $add_hours = intval($diff_gmt_weblogger);
 298          $add_minutes = intval(60 * ($diff_gmt_weblogger - $add_hours));
 299          $wpdb->query("UPDATE $wpdb->posts SET post_date_gmt = DATE_ADD(post_date, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)");
 300          $wpdb->query("UPDATE $wpdb->posts SET post_modified = post_date");
 301          $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'");
 302          $wpdb->query("UPDATE $wpdb->comments SET comment_date_gmt = DATE_ADD(comment_date, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)");
 303          $wpdb->query("UPDATE $wpdb->users SET user_registered = DATE_ADD(user_registered, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)");
 304      }
 305  
 306  }
 307  
 308  function upgrade_130() {
 309      global $wpdb;
 310  
 311      // Remove extraneous backslashes.
 312      $posts = $wpdb->get_results("SELECT ID, post_title, post_content, post_excerpt, guid, post_date, post_name, post_status, post_author FROM $wpdb->posts");
 313      if ($posts) {
 314          foreach($posts as $post) {
 315              $post_content = addslashes(deslash($post->post_content));
 316              $post_title = addslashes(deslash($post->post_title));
 317              $post_excerpt = addslashes(deslash($post->post_excerpt));
 318              if ( empty($post->guid) )
 319                  $guid = get_permalink($post->ID);
 320              else
 321                  $guid = $post->guid;
 322  
 323              $wpdb->query("UPDATE $wpdb->posts SET post_title = '$post_title', post_content = '$post_content', post_excerpt = '$post_excerpt', guid = '$guid' WHERE ID = '$post->ID'");
 324          }
 325      }
 326  
 327      // Remove extraneous backslashes.
 328      $comments = $wpdb->get_results("SELECT comment_ID, comment_author, comment_content FROM $wpdb->comments");
 329      if ($comments) {
 330          foreach($comments as $comment) {
 331              $comment_content = addslashes(deslash($comment->comment_content));
 332              $comment_author = addslashes(deslash($comment->comment_author));
 333              $wpdb->query("UPDATE $wpdb->comments SET comment_content = '$comment_content', comment_author = '$comment_author' WHERE comment_ID = '$comment->comment_ID'");
 334          }
 335      }
 336  
 337      // Remove extraneous backslashes.
 338      $links = $wpdb->get_results("SELECT link_id, link_name, link_description FROM $wpdb->links");
 339      if ($links) {
 340          foreach($links as $link) {
 341              $link_name = addslashes(deslash($link->link_name));
 342              $link_description = addslashes(deslash($link->link_description));
 343              $wpdb->query("UPDATE $wpdb->links SET link_name = '$link_name', link_description = '$link_description' WHERE link_id = '$link->link_id'");
 344          }
 345      }
 346  
 347      // The "paged" option for what_to_show is no more.
 348      if ($wpdb->get_var("SELECT option_value FROM $wpdb->options WHERE option_name = 'what_to_show'") == 'paged') {
 349          $wpdb->query("UPDATE $wpdb->options SET option_value = 'posts' WHERE option_name = 'what_to_show'");
 350      }
 351  
 352          $active_plugins = __get_option('active_plugins');
 353  
 354          // If plugins are not stored in an array, they're stored in the old
 355          // newline separated format.  Convert to new format.
 356          if ( !is_array( $active_plugins ) ) {
 357              $active_plugins = explode("\n", trim($active_plugins));
 358              update_option('active_plugins', $active_plugins);
 359          }
 360  
 361      // Obsolete tables
 362      $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optionvalues');
 363      $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optiontypes');
 364      $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optiongroups');
 365      $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->