PageRenderTime 48ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-admin/includes/upgrade.php

https://bitbucket.org/crypticrod/sr_wp_code
PHP | 2001 lines | 1266 code | 273 blank | 462 comment | 294 complexity | 167d87210e7c1b3910bda8f5e78b675e MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1, GPL-3.0, LGPL-2.0, AGPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * WordPress Upgrade API
  4. *
  5. * Most of the functions are pluggable and can be overwritten
  6. *
  7. * @package WordPress
  8. * @subpackage Administration
  9. */
  10. /** Include user install customize script. */
  11. if ( file_exists(WP_CONTENT_DIR . '/install.php') )
  12. require (WP_CONTENT_DIR . '/install.php');
  13. /** WordPress Administration API */
  14. require_once(ABSPATH . 'wp-admin/includes/admin.php');
  15. /** WordPress Schema API */
  16. require_once(ABSPATH . 'wp-admin/includes/schema.php');
  17. if ( !function_exists('wp_install') ) :
  18. /**
  19. * Installs the blog
  20. *
  21. * {@internal Missing Long Description}}
  22. *
  23. * @since 2.1.0
  24. *
  25. * @param string $blog_title Blog title.
  26. * @param string $user_name User's username.
  27. * @param string $user_email User's email.
  28. * @param bool $public Whether blog is public.
  29. * @param null $deprecated Optional. Not used.
  30. * @param string $user_password Optional. User's chosen password. Will default to a random password.
  31. * @return array Array keys 'url', 'user_id', 'password', 'password_message'.
  32. */
  33. function wp_install( $blog_title, $user_name, $user_email, $public, $deprecated = '', $user_password = '' ) {
  34. global $wp_rewrite;
  35. if ( !empty( $deprecated ) )
  36. _deprecated_argument( __FUNCTION__, '2.6' );
  37. wp_check_mysql_version();
  38. wp_cache_flush();
  39. make_db_current_silent();
  40. populate_options();
  41. populate_roles();
  42. update_option('blogname', $blog_title);
  43. update_option('admin_email', $user_email);
  44. update_option('blog_public', $public);
  45. $guessurl = wp_guess_url();
  46. update_option('siteurl', $guessurl);
  47. // If not a public blog, don't ping.
  48. if ( ! $public )
  49. update_option('default_pingback_flag', 0);
  50. // Create default user. If the user already exists, the user tables are
  51. // being shared among blogs. Just set the role in that case.
  52. $user_id = username_exists($user_name);
  53. $user_password = trim($user_password);
  54. $email_password = false;
  55. if ( !$user_id && empty($user_password) ) {
  56. $user_password = wp_generate_password( 12, false );
  57. $message = __('<strong><em>Note that password</em></strong> carefully! It is a <em>random</em> password that was generated just for you.');
  58. $user_id = wp_create_user($user_name, $user_password, $user_email);
  59. update_user_option($user_id, 'default_password_nag', true, true);
  60. $email_password = true;
  61. } else if ( !$user_id ) {
  62. // Password has been provided
  63. $message = '<em>'.__('Your chosen password.').'</em>';
  64. $user_id = wp_create_user($user_name, $user_password, $user_email);
  65. } else {
  66. $message = __('User already exists. Password inherited.');
  67. }
  68. $user = new WP_User($user_id);
  69. $user->set_role('administrator');
  70. wp_install_defaults($user_id);
  71. $wp_rewrite->flush_rules();
  72. wp_new_blog_notification($blog_title, $guessurl, $user_id, ($email_password ? $user_password : __('The password you chose during the install.') ) );
  73. wp_cache_flush();
  74. return array('url' => $guessurl, 'user_id' => $user_id, 'password' => $user_password, 'password_message' => $message);
  75. }
  76. endif;
  77. if ( !function_exists('wp_install_defaults') ) :
  78. /**
  79. * {@internal Missing Short Description}}
  80. *
  81. * {@internal Missing Long Description}}
  82. *
  83. * @since 2.1.0
  84. *
  85. * @param int $user_id User ID.
  86. */
  87. function wp_install_defaults($user_id) {
  88. global $wpdb, $wp_rewrite, $current_site, $table_prefix;
  89. // Default category
  90. $cat_name = __('Uncategorized');
  91. /* translators: Default category slug */
  92. $cat_slug = sanitize_title(_x('Uncategorized', 'Default category slug'));
  93. if ( global_terms_enabled() ) {
  94. $cat_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM {$wpdb->sitecategories} WHERE category_nicename = %s", $cat_slug ) );
  95. if ( $cat_id == null ) {
  96. $wpdb->insert( $wpdb->sitecategories, array('cat_ID' => 0, 'cat_name' => $cat_name, 'category_nicename' => $cat_slug, 'last_updated' => current_time('mysql', true)) );
  97. $cat_id = $wpdb->insert_id;
  98. }
  99. update_option('default_category', $cat_id);
  100. } else {
  101. $cat_id = 1;
  102. }
  103. $wpdb->insert( $wpdb->terms, array('term_id' => $cat_id, 'name' => $cat_name, 'slug' => $cat_slug, 'term_group' => 0) );
  104. $wpdb->insert( $wpdb->term_taxonomy, array('term_id' => $cat_id, 'taxonomy' => 'category', 'description' => '', 'parent' => 0, 'count' => 1));
  105. $cat_tt_id = $wpdb->insert_id;
  106. // Default link category
  107. $cat_name = __('Blogroll');
  108. /* translators: Default link category slug */
  109. $cat_slug = sanitize_title(_x('Blogroll', 'Default link category slug'));
  110. if ( global_terms_enabled() ) {
  111. $blogroll_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM {$wpdb->sitecategories} WHERE category_nicename = %s", $cat_slug ) );
  112. if ( $blogroll_id == null ) {
  113. $wpdb->insert( $wpdb->sitecategories, array('cat_ID' => 0, 'cat_name' => $cat_name, 'category_nicename' => $cat_slug, 'last_updated' => current_time('mysql', true)) );
  114. $blogroll_id = $wpdb->insert_id;
  115. }
  116. update_option('default_link_category', $blogroll_id);
  117. } else {
  118. $blogroll_id = 2;
  119. }
  120. $wpdb->insert( $wpdb->terms, array('term_id' => $blogroll_id, 'name' => $cat_name, 'slug' => $cat_slug, 'term_group' => 0) );
  121. $wpdb->insert( $wpdb->term_taxonomy, array('term_id' => $blogroll_id, 'taxonomy' => 'link_category', 'description' => '', 'parent' => 0, 'count' => 7));
  122. $blogroll_tt_id = $wpdb->insert_id;
  123. // Now drop in some default links
  124. $default_links = array();
  125. $default_links[] = array( 'link_url' => 'http://codex.wordpress.org/',
  126. 'link_name' => 'Documentation',
  127. 'link_rss' => '',
  128. 'link_notes' => '');
  129. $default_links[] = array( 'link_url' => 'http://wordpress.org/news/',
  130. 'link_name' => 'WordPress Blog',
  131. 'link_rss' => 'http://wordpress.org/news/feed/',
  132. 'link_notes' => '');
  133. $default_links[] = array( 'link_url' => 'http://wordpress.org/extend/ideas/',
  134. 'link_name' => 'Suggest Ideas',
  135. 'link_rss' => '',
  136. 'link_notes' =>'');
  137. $default_links[] = array( 'link_url' => 'http://wordpress.org/support/',
  138. 'link_name' => 'Support Forum',
  139. 'link_rss' => '',
  140. 'link_notes' =>'');
  141. $default_links[] = array( 'link_url' => 'http://wordpress.org/extend/plugins/',
  142. 'link_name' => 'Plugins',
  143. 'link_rss' => '',
  144. 'link_notes' =>'');
  145. $default_links[] = array( 'link_url' => 'http://wordpress.org/extend/themes/',
  146. 'link_name' => 'Themes',
  147. 'link_rss' => '',
  148. 'link_notes' =>'');
  149. $default_links[] = array( 'link_url' => 'http://planet.wordpress.org/',
  150. 'link_name' => 'WordPress Planet',
  151. 'link_rss' => '',
  152. 'link_notes' =>'');
  153. foreach ( $default_links as $link ) {
  154. $wpdb->insert( $wpdb->links, $link);
  155. $wpdb->insert( $wpdb->term_relationships, array('term_taxonomy_id' => $blogroll_tt_id, 'object_id' => $wpdb->insert_id) );
  156. }
  157. // First post
  158. $now = date('Y-m-d H:i:s');
  159. $now_gmt = gmdate('Y-m-d H:i:s');
  160. $first_post_guid = get_option('home') . '/?p=1';
  161. if ( is_multisite() ) {
  162. $first_post = get_site_option( 'first_post' );
  163. if ( empty($first_post) )
  164. $first_post = stripslashes( __( 'Welcome to <a href="SITE_URL">SITE_NAME</a>. This is your first post. Edit or delete it, then start blogging!' ) );
  165. $first_post = str_replace( "SITE_URL", esc_url( network_home_url() ), $first_post );
  166. $first_post = str_replace( "SITE_NAME", $current_site->site_name, $first_post );
  167. } else {
  168. $first_post = __('Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!');
  169. }
  170. $wpdb->insert( $wpdb->posts, array(
  171. 'post_author' => $user_id,
  172. 'post_date' => $now,
  173. 'post_date_gmt' => $now_gmt,
  174. 'post_content' => $first_post,
  175. 'post_excerpt' => '',
  176. 'post_title' => __('Hello world!'),
  177. /* translators: Default post slug */
  178. 'post_name' => sanitize_title( _x('hello-world', 'Default post slug') ),
  179. 'post_modified' => $now,
  180. 'post_modified_gmt' => $now_gmt,
  181. 'guid' => $first_post_guid,
  182. 'comment_count' => 1,
  183. 'to_ping' => '',
  184. 'pinged' => '',
  185. 'post_content_filtered' => ''
  186. ));
  187. $wpdb->insert( $wpdb->term_relationships, array('term_taxonomy_id' => $cat_tt_id, 'object_id' => 1) );
  188. // Default comment
  189. $first_comment_author = __('Mr WordPress');
  190. $first_comment_url = 'http://wordpress.org/';
  191. $first_comment = __('Hi, this is a comment.<br />To delete a comment, just log in and view the post&#039;s comments. There you will have the option to edit or delete them.');
  192. if ( is_multisite() ) {
  193. $first_comment_author = get_site_option( 'first_comment_author', $first_comment_author );
  194. $first_comment_url = get_site_option( 'first_comment_url', network_home_url() );
  195. $first_comment = get_site_option( 'first_comment', $first_comment );
  196. }
  197. $wpdb->insert( $wpdb->comments, array(
  198. 'comment_post_ID' => 1,
  199. 'comment_author' => $first_comment_author,
  200. 'comment_author_email' => '',
  201. 'comment_author_url' => $first_comment_url,
  202. 'comment_date' => $now,
  203. 'comment_date_gmt' => $now_gmt,
  204. 'comment_content' => $first_comment
  205. ));
  206. // First Page
  207. $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:
  208. <blockquote>Hi there! I'm a bike messenger by day, aspiring actor by night, and this is my blog. I live in Los Angeles, have a great dog named Jack, and I like pi&#241;a coladas. (And gettin' caught in the rain.)</blockquote>
  209. ...or something like this:
  210. <blockquote>The XYZ Doohickey Company was founded in 1971, and has been providing quality doohickies 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>
  211. 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() );
  212. if ( is_multisite() )
  213. $first_page = get_site_option( 'first_page', $first_page );
  214. $first_post_guid = get_option('home') . '/?page_id=2';
  215. $wpdb->insert( $wpdb->posts, array(
  216. 'post_author' => $user_id,
  217. 'post_date' => $now,
  218. 'post_date_gmt' => $now_gmt,
  219. 'post_content' => $first_page,
  220. 'post_excerpt' => '',
  221. 'post_title' => __( 'Sample Page' ),
  222. /* translators: Default page slug */
  223. 'post_name' => __( 'sample-page' ),
  224. 'post_modified' => $now,
  225. 'post_modified_gmt' => $now_gmt,
  226. 'guid' => $first_post_guid,
  227. 'post_type' => 'page',
  228. 'to_ping' => '',
  229. 'pinged' => '',
  230. 'post_content_filtered' => ''
  231. ));
  232. $wpdb->insert( $wpdb->postmeta, array( 'post_id' => 2, 'meta_key' => '_wp_page_template', 'meta_value' => 'default' ) );
  233. // Set up default widgets for default theme.
  234. update_option( 'widget_search', array ( 2 => array ( 'title' => '' ), '_multiwidget' => 1 ) );
  235. update_option( 'widget_recent-posts', array ( 2 => array ( 'title' => '', 'number' => 5 ), '_multiwidget' => 1 ) );
  236. update_option( 'widget_recent-comments', array ( 2 => array ( 'title' => '', 'number' => 5 ), '_multiwidget' => 1 ) );
  237. update_option( 'widget_archives', array ( 2 => array ( 'title' => '', 'count' => 0, 'dropdown' => 0 ), '_multiwidget' => 1 ) );
  238. update_option( 'widget_categories', array ( 2 => array ( 'title' => '', 'count' => 0, 'hierarchical' => 0, 'dropdown' => 0 ), '_multiwidget' => 1 ) );
  239. update_option( 'widget_meta', array ( 2 => array ( 'title' => '' ), '_multiwidget' => 1 ) );
  240. update_option( 'sidebars_widgets', array ( 'wp_inactive_widgets' => array ( ), 'primary-widget-area' => array ( 0 => 'search-2', 1 => 'recent-posts-2', 2 => 'recent-comments-2', 3 => 'archives-2', 4 => 'categories-2', 5 => 'meta-2', ), 'secondary-widget-area' => array ( ), 'first-footer-widget-area' => array ( ), 'second-footer-widget-area' => array ( ), 'third-footer-widget-area' => array ( ), 'fourth-footer-widget-area' => array ( ), 'array_version' => 3 ) );
  241. if ( is_multisite() ) {
  242. // Flush rules to pick up the new page.
  243. $wp_rewrite->init();
  244. $wp_rewrite->flush_rules();
  245. $user = new WP_User($user_id);
  246. $wpdb->update( $wpdb->options, array('option_value' => $user->user_email), array('option_name' => 'admin_email') );
  247. // Remove all perms except for the login user.
  248. $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE user_id != %d AND meta_key = %s", $user_id, $table_prefix.'user_level') );
  249. $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE user_id != %d AND meta_key = %s", $user_id, $table_prefix.'capabilities') );
  250. // Delete any caps that snuck into the previously active blog. (Hardcoded to blog 1 for now.) TODO: Get previous_blog_id.
  251. if ( !is_super_admin( $user_id ) && $user_id != 1 )
  252. $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE user_id = %d AND meta_key = %s", $user_id, $wpdb->base_prefix.'1_capabilities') );
  253. }
  254. }
  255. endif;
  256. if ( !function_exists('wp_new_blog_notification') ) :
  257. /**
  258. * {@internal Missing Short Description}}
  259. *
  260. * {@internal Missing Long Description}}
  261. *
  262. * @since 2.1.0
  263. *
  264. * @param string $blog_title Blog title.
  265. * @param string $blog_url Blog url.
  266. * @param int $user_id User ID.
  267. * @param string $password User's Password.
  268. */
  269. function wp_new_blog_notification($blog_title, $blog_url, $user_id, $password) {
  270. $user = new WP_User($user_id);
  271. $email = $user->user_email;
  272. $name = $user->user_login;
  273. $message = sprintf(__("Your new WordPress site has been successfully set up at:
  274. %1\$s
  275. You can log in to the administrator account with the following information:
  276. Username: %2\$s
  277. Password: %3\$s
  278. We hope you enjoy your new site. Thanks!
  279. --The WordPress Team
  280. http://wordpress.org/
  281. "), $blog_url, $name, $password);
  282. @wp_mail($email, __('New WordPress Site'), $message);
  283. }
  284. endif;
  285. if ( !function_exists('wp_upgrade') ) :
  286. /**
  287. * Run WordPress Upgrade functions.
  288. *
  289. * {@internal Missing Long Description}}
  290. *
  291. * @since 2.1.0
  292. *
  293. * @return null
  294. */
  295. function wp_upgrade() {
  296. global $wp_current_db_version, $wp_db_version, $wpdb;
  297. $wp_current_db_version = __get_option('db_version');
  298. // We are up-to-date. Nothing to do.
  299. if ( $wp_db_version == $wp_current_db_version )
  300. return;
  301. if ( ! is_blog_installed() )
  302. return;
  303. wp_check_mysql_version();
  304. wp_cache_flush();
  305. pre_schema_upgrade();
  306. make_db_current_silent();
  307. upgrade_all();
  308. if ( is_multisite() && is_main_site() )
  309. upgrade_network();
  310. wp_cache_flush();
  311. if ( is_multisite() ) {
  312. if ( $wpdb->get_row( "SELECT blog_id FROM {$wpdb->blog_versions} WHERE blog_id = '{$wpdb->blogid}'" ) )
  313. $wpdb->query( "UPDATE {$wpdb->blog_versions} SET db_version = '{$wp_db_version}' WHERE blog_id = '{$wpdb->blogid}'" );
  314. else
  315. $wpdb->query( "INSERT INTO {$wpdb->blog_versions} ( `blog_id` , `db_version` , `last_updated` ) VALUES ( '{$wpdb->blogid}', '{$wp_db_version}', NOW());" );
  316. }
  317. }
  318. endif;
  319. /**
  320. * Functions to be called in install and upgrade scripts.
  321. *
  322. * {@internal Missing Long Description}}
  323. *
  324. * @since 1.0.1
  325. */
  326. function upgrade_all() {
  327. global $wp_current_db_version, $wp_db_version, $wp_rewrite;
  328. $wp_current_db_version = __get_option('db_version');
  329. // We are up-to-date. Nothing to do.
  330. if ( $wp_db_version == $wp_current_db_version )
  331. return;
  332. // If the version is not set in the DB, try to guess the version.
  333. if ( empty($wp_current_db_version) ) {
  334. $wp_current_db_version = 0;
  335. // If the template option exists, we have 1.5.
  336. $template = __get_option('template');
  337. if ( !empty($template) )
  338. $wp_current_db_version = 2541;
  339. }
  340. if ( $wp_current_db_version < 6039 )
  341. upgrade_230_options_table();
  342. populate_options();
  343. if ( $wp_current_db_version < 2541 ) {
  344. upgrade_100();
  345. upgrade_101();
  346. upgrade_110();
  347. upgrade_130();
  348. }
  349. if ( $wp_current_db_version < 3308 )
  350. upgrade_160();
  351. if ( $wp_current_db_version < 4772 )
  352. upgrade_210();
  353. if ( $wp_current_db_version < 4351 )
  354. upgrade_old_slugs();
  355. if ( $wp_current_db_version < 5539 )
  356. upgrade_230();
  357. if ( $wp_current_db_version < 6124 )
  358. upgrade_230_old_tables();
  359. if ( $wp_current_db_version < 7499 )
  360. upgrade_250();
  361. if ( $wp_current_db_version < 7935 )
  362. upgrade_252();
  363. if ( $wp_current_db_version < 8201 )
  364. upgrade_260();
  365. if ( $wp_current_db_version < 8989 )
  366. upgrade_270();
  367. if ( $wp_current_db_version < 10360 )
  368. upgrade_280();
  369. if ( $wp_current_db_version < 11958 )
  370. upgrade_290();
  371. if ( $wp_current_db_version < 15260 )
  372. upgrade_300();
  373. maybe_disable_automattic_widgets();
  374. update_option( 'db_version', $wp_db_version );
  375. update_option( 'db_upgraded', true );
  376. }
  377. /**
  378. * Execute changes made in WordPress 1.0.
  379. *
  380. * @since 1.0.0
  381. */
  382. function upgrade_100() {
  383. global $wpdb;
  384. // Get the title and ID of every post, post_name to check if it already has a value
  385. $posts = $wpdb->get_results("SELECT ID, post_title, post_name FROM $wpdb->posts WHERE post_name = ''");
  386. if ($posts) {
  387. foreach($posts as $post) {
  388. if ('' == $post->post_name) {
  389. $newtitle = sanitize_title($post->post_title);
  390. $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_name = %s WHERE ID = %d", $newtitle, $post->ID) );
  391. }
  392. }
  393. }
  394. $categories = $wpdb->get_results("SELECT cat_ID, cat_name, category_nicename FROM $wpdb->categories");
  395. foreach ($categories as $category) {
  396. if ('' == $category->category_nicename) {
  397. $newtitle = sanitize_title($category->cat_name);
  398. $wpdb>update( $wpdb->categories, array('category_nicename' => $newtitle), array('cat_ID' => $category->cat_ID) );
  399. }
  400. }
  401. $wpdb->query("UPDATE $wpdb->options SET option_value = REPLACE(option_value, 'wp-links/links-images/', 'wp-images/links/')
  402. WHERE option_name LIKE 'links_rating_image%'
  403. AND option_value LIKE 'wp-links/links-images/%'");
  404. $done_ids = $wpdb->get_results("SELECT DISTINCT post_id FROM $wpdb->post2cat");
  405. if ($done_ids) :
  406. foreach ($done_ids as $done_id) :
  407. $done_posts[] = $done_id->post_id;
  408. endforeach;
  409. $catwhere = ' AND ID NOT IN (' . implode(',', $done_posts) . ')';
  410. else:
  411. $catwhere = '';
  412. endif;
  413. $allposts = $wpdb->get_results("SELECT ID, post_category FROM $wpdb->posts WHERE post_category != '0' $catwhere");
  414. if ($allposts) :
  415. foreach ($allposts as $post) {
  416. // Check to see if it's already been imported
  417. $cat = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->post2cat WHERE post_id = %d AND category_id = %d", $post->ID, $post->post_category) );
  418. if (!$cat && 0 != $post->post_category) { // If there's no result
  419. $wpdb->insert( $wpdb->post2cat, array('post_id' => $post->ID, 'category_id' => $post->post_category) );
  420. }
  421. }
  422. endif;
  423. }
  424. /**
  425. * Execute changes made in WordPress 1.0.1.
  426. *
  427. * @since 1.0.1
  428. */
  429. function upgrade_101() {
  430. global $wpdb;
  431. // Clean up indices, add a few
  432. add_clean_index($wpdb->posts, 'post_name');
  433. add_clean_index($wpdb->posts, 'post_status');
  434. add_clean_index($wpdb->categories, 'category_nicename');
  435. add_clean_index($wpdb->comments, 'comment_approved');
  436. add_clean_index($wpdb->comments, 'comment_post_ID');
  437. add_clean_index($wpdb->links , 'link_category');
  438. add_clean_index($wpdb->links , 'link_visible');
  439. }
  440. /**
  441. * Execute changes made in WordPress 1.2.
  442. *
  443. * @since 1.2.0
  444. */
  445. function upgrade_110() {
  446. global $wpdb;
  447. // Set user_nicename.
  448. $users = $wpdb->get_results("SELECT ID, user_nickname, user_nicename FROM $wpdb->users");
  449. foreach ($users as $user) {
  450. if ('' == $user->user_nicename) {
  451. $newname = sanitize_title($user->user_nickname);
  452. $wpdb->update( $wpdb->users, array('user_nicename' => $newname), array('ID' => $user->ID) );
  453. }
  454. }
  455. $users = $wpdb->get_results("SELECT ID, user_pass from $wpdb->users");
  456. foreach ($users as $row) {
  457. if (!preg_match('/^[A-Fa-f0-9]{32}$/', $row->user_pass)) {
  458. $wpdb->update( $wpdb->users, array('user_pass' => md5($row->user_pass)), array('ID' => $row->ID) );
  459. }
  460. }
  461. // Get the GMT offset, we'll use that later on
  462. $all_options = get_alloptions_110();
  463. $time_difference = $all_options->time_difference;
  464. $server_time = time()+date('Z');
  465. $weblogger_time = $server_time + $time_difference*3600;
  466. $gmt_time = time();
  467. $diff_gmt_server = ($gmt_time - $server_time) / 3600;
  468. $diff_weblogger_server = ($weblogger_time - $server_time) / 3600;
  469. $diff_gmt_weblogger = $diff_gmt_server - $diff_weblogger_server;
  470. $gmt_offset = -$diff_gmt_weblogger;
  471. // Add a gmt_offset option, with value $gmt_offset
  472. add_option('gmt_offset', $gmt_offset);
  473. // Check if we already set the GMT fields (if we did, then
  474. // MAX(post_date_gmt) can't be '0000-00-00 00:00:00'
  475. // <michel_v> I just slapped myself silly for not thinking about it earlier
  476. $got_gmt_fields = ! ($wpdb->get_var("SELECT MAX(post_date_gmt) FROM $wpdb->posts") == '0000-00-00 00:00:00');
  477. if (!$got_gmt_fields) {
  478. // Add or substract time to all dates, to get GMT dates
  479. $add_hours = intval($diff_gmt_weblogger);
  480. $add_minutes = intval(60 * ($diff_gmt_weblogger - $add_hours));
  481. $wpdb->query("UPDATE $wpdb->posts SET post_date_gmt = DATE_ADD(post_date, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)");
  482. $wpdb->query("UPDATE $wpdb->posts SET post_modified = post_date");
  483. $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'");
  484. $wpdb->query("UPDATE $wpdb->comments SET comment_date_gmt = DATE_ADD(comment_date, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)");
  485. $wpdb->query("UPDATE $wpdb->users SET user_registered = DATE_ADD(user_registered, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)");
  486. }
  487. }
  488. /**
  489. * Execute changes made in WordPress 1.5.
  490. *
  491. * @since 1.5.0
  492. */
  493. function upgrade_130() {
  494. global $wpdb;
  495. // Remove extraneous backslashes.
  496. $posts = $wpdb->get_results("SELECT ID, post_title, post_content, post_excerpt, guid, post_date, post_name, post_status, post_author FROM $wpdb->posts");
  497. if ($posts) {
  498. foreach($posts as $post) {
  499. $post_content = addslashes(deslash($post->post_content));
  500. $post_title = addslashes(deslash($post->post_title));
  501. $post_excerpt = addslashes(deslash($post->post_excerpt));
  502. if ( empty($post->guid) )
  503. $guid = get_permalink($post->ID);
  504. else
  505. $guid = $post->guid;
  506. $wpdb->update( $wpdb->posts, compact('post_title', 'post_content', 'post_excerpt', 'guid'), array('ID' => $post->ID) );
  507. }
  508. }
  509. // Remove extraneous backslashes.
  510. $comments = $wpdb->get_results("SELECT comment_ID, comment_author, comment_content FROM $wpdb->comments");
  511. if ($comments) {
  512. foreach($comments as $comment) {
  513. $comment_content = deslash($comment->comment_content);
  514. $comment_author = deslash($comment->comment_author);
  515. $wpdb->update($wpdb->comments, compact('comment_content', 'comment_author'), array('comment_ID' => $comment->comment_ID) );
  516. }
  517. }
  518. // Remove extraneous backslashes.
  519. $links = $wpdb->get_results("SELECT link_id, link_name, link_description FROM $wpdb->links");
  520. if ($links) {
  521. foreach($links as $link) {
  522. $link_name = deslash($link->link_name);
  523. $link_description = deslash($link->link_description);
  524. $wpdb->update( $wpdb->links, compact('link_name', 'link_description'), array('link_id' => $link->link_id) );
  525. }
  526. }
  527. $active_plugins = __get_option('active_plugins');
  528. // If plugins are not stored in an array, they're stored in the old
  529. // newline separated format. Convert to new format.
  530. if ( !is_array( $active_plugins ) ) {
  531. $active_plugins = explode("\n", trim($active_plugins));
  532. update_option('active_plugins', $active_plugins);
  533. }
  534. // Obsolete tables
  535. $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optionvalues');
  536. $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optiontypes');
  537. $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optiongroups');
  538. $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optiongroup_options');
  539. // Update comments table to use comment_type
  540. $wpdb->query("UPDATE $wpdb->comments SET comment_type='trackback', comment_content = REPLACE(comment_content, '<trackback />', '') WHERE comment_content LIKE '<trackback />%'");
  541. $wpdb->query("UPDATE $wpdb->comments SET comment_type='pingback', comment_content = REPLACE(comment_content, '<pingback />', '') WHERE comment_content LIKE '<pingback />%'");
  542. // Some versions have multiple duplicate option_name rows with the same values
  543. $options = $wpdb->get_results("SELECT option_name, COUNT(option_name) AS dupes FROM `$wpdb->options` GROUP BY option_name");
  544. foreach ( $options as $option ) {
  545. if ( 1 != $option->dupes ) { // Could this be done in the query?
  546. $limit = $option->dupes - 1;
  547. $dupe_ids = $wpdb->get_col( $wpdb->prepare("SELECT option_id FROM $wpdb->options WHERE option_name = %s LIMIT %d", $option->option_name, $limit) );
  548. if ( $dupe_ids ) {
  549. $dupe_ids = join($dupe_ids, ',');
  550. $wpdb->query("DELETE FROM $wpdb->options WHERE option_id IN ($dupe_ids)");
  551. }
  552. }
  553. }
  554. make_site_theme();
  555. }
  556. /**
  557. * Execute changes made in WordPress 2.0.
  558. *
  559. * @since 2.0.0
  560. */
  561. function upgrade_160() {
  562. global $wpdb, $wp_current_db_version;
  563. populate_roles_160();
  564. $users = $wpdb->get_results("SELECT * FROM $wpdb->users");
  565. foreach ( $users as $user ) :
  566. if ( !empty( $user->user_firstname ) )
  567. update_user_meta( $user->ID, 'first_name', $wpdb->escape($user->user_firstname) );
  568. if ( !empty( $user->user_lastname ) )
  569. update_user_meta( $user->ID, 'last_name', $wpdb->escape($user->user_lastname) );
  570. if ( !empty( $user->user_nickname ) )
  571. update_user_meta( $user->ID, 'nickname', $wpdb->escape($user->user_nickname) );
  572. if ( !empty( $user->user_level ) )
  573. update_user_meta( $user->ID, $wpdb->prefix . 'user_level', $user->user_level );
  574. if ( !empty( $user->user_icq ) )
  575. update_user_meta( $user->ID, 'icq', $wpdb->escape($user->user_icq) );
  576. if ( !empty( $user->user_aim ) )
  577. update_user_meta( $user->ID, 'aim', $wpdb->escape($user->user_aim) );
  578. if ( !empty( $user->user_msn ) )
  579. update_user_meta( $user->ID, 'msn', $wpdb->escape($user->user_msn) );
  580. if ( !empty( $user->user_yim ) )
  581. update_user_meta( $user->ID, 'yim', $wpdb->escape($user->user_icq) );
  582. if ( !empty( $user->user_description ) )
  583. update_user_meta( $user->ID, 'description', $wpdb->escape($user->user_description) );
  584. if ( isset( $user->user_idmode ) ):
  585. $idmode = $user->user_idmode;
  586. if ($idmode == 'nickname') $id = $user->user_nickname;
  587. if ($idmode == 'login') $id = $user->user_login;
  588. if ($idmode == 'firstname') $id = $user->user_firstname;
  589. if ($idmode == 'lastname') $id = $user->user_lastname;
  590. if ($idmode == 'namefl') $id = $user->user_firstname.' '.$user->user_lastname;
  591. if ($idmode == 'namelf') $id = $user->user_lastname.' '.$user->user_firstname;
  592. if (!$idmode) $id = $user->user_nickname;
  593. $wpdb->update( $wpdb->users, array('display_name' => $id), array('ID' => $user->ID) );
  594. endif;
  595. // FIXME: RESET_CAPS is temporary code to reset roles and caps if flag is set.
  596. $caps = get_user_meta( $user->ID, $wpdb->prefix . 'capabilities');
  597. if ( empty($caps) || defined('RESET_CAPS') ) {
  598. $level = get_user_meta($user->ID, $wpdb->prefix . 'user_level', true);
  599. $role = translate_level_to_role($level);
  600. update_user_meta( $user->ID, $wpdb->prefix . 'capabilities', array($role => true) );
  601. }
  602. endforeach;
  603. $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' );
  604. $wpdb->hide_errors();
  605. foreach ( $old_user_fields as $old )
  606. $wpdb->query("ALTER TABLE $wpdb->users DROP $old");
  607. $wpdb->show_errors();
  608. // populate comment_count field of posts table
  609. $comments = $wpdb->get_results( "SELECT comment_post_ID, COUNT(*) as c FROM $wpdb->comments WHERE comment_approved = '1' GROUP BY comment_post_ID" );
  610. if ( is_array( $comments ) )
  611. foreach ($comments as $comment)
  612. $wpdb->update( $wpdb->posts, array('comment_count' => $comment->c), array('ID' => $comment->comment_post_ID) );
  613. // Some alpha versions used a post status of object instead of attachment and put
  614. // the mime type in post_type instead of post_mime_type.
  615. if ( $wp_current_db_version > 2541 && $wp_current_db_version <= 3091 ) {
  616. $objects = $wpdb->get_results("SELECT ID, post_type FROM $wpdb->posts WHERE post_status = 'object'");
  617. foreach ($objects as $object) {
  618. $wpdb->update( $wpdb->posts, array( 'post_status' => 'attachment',
  619. 'post_mime_type' => $object->post_type,
  620. 'post_type' => ''),
  621. array( 'ID' => $object->ID ) );
  622. $meta = get_post_meta($object->ID, 'imagedata', true);
  623. if ( ! empty($meta['file']) )
  624. update_attached_file( $object->ID, $meta['file'] );
  625. }
  626. }
  627. }
  628. /**
  629. * Execute changes made in WordPress 2.1.
  630. *
  631. * @since 2.1.0
  632. */
  633. function upgrade_210() {
  634. global $wpdb, $wp_current_db_version;
  635. if ( $wp_current_db_version < 3506 ) {
  636. // Update status and type.
  637. $posts = $wpdb->get_results("SELECT ID, post_status FROM $wpdb->posts");
  638. if ( ! empty($posts) ) foreach ($posts as $post) {
  639. $status = $post->post_status;
  640. $type = 'post';
  641. if ( 'static' == $status ) {
  642. $status = 'publish';
  643. $type = 'page';
  644. } else if ( 'attachment' == $status ) {
  645. $status = 'inherit';
  646. $type = 'attachment';
  647. }
  648. $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_status = %s, post_type = %s WHERE ID = %d", $status, $type, $post->ID) );
  649. }
  650. }
  651. if ( $wp_current_db_version < 3845 ) {
  652. populate_roles_210();
  653. }
  654. if ( $wp_current_db_version < 3531 ) {
  655. // Give future posts a post_status of future.
  656. $now = gmdate('Y-m-d H:i:59');
  657. $wpdb->query ("UPDATE $wpdb->posts SET post_status = 'future' WHERE post_status = 'publish' AND post_date_gmt > '$now'");
  658. $posts = $wpdb->get_results("SELECT ID, post_date FROM $wpdb->posts WHERE post_status ='future'");
  659. if ( !empty($posts) )
  660. foreach ( $posts as $post )
  661. wp_schedule_single_event(mysql2date('U', $post->post_date, false), 'publish_future_post', array($post->ID));
  662. }
  663. }
  664. /**
  665. * Execute changes made in WordPress 2.3.
  666. *
  667. * @since 2.3.0
  668. */
  669. function upgrade_230() {
  670. global $wp_current_db_version, $wpdb;
  671. if ( $wp_current_db_version < 5200 ) {
  672. populate_roles_230();
  673. }
  674. // Convert categories to terms.
  675. $tt_ids = array();
  676. $have_tags = false;
  677. $categories = $wpdb->get_results("SELECT * FROM $wpdb->categories ORDER BY cat_ID");
  678. foreach ($categories as $category) {
  679. $term_id = (int) $category->cat_ID;
  680. $name = $category->cat_name;
  681. $description = $category->category_description;
  682. $slug = $category->category_nicename;
  683. $parent = $category->category_parent;
  684. $term_group = 0;
  685. // Associate terms with the same slug in a term group and make slugs unique.
  686. if ( $exists = $wpdb->get_results( $wpdb->prepare("SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $slug) ) ) {
  687. $term_group = $exists[0]->term_group;
  688. $id = $exists[0]->term_id;
  689. $num = 2;
  690. do {
  691. $alt_slug = $slug . "-$num";
  692. $num++;
  693. $slug_check = $wpdb->get_var( $wpdb->prepare("SELECT slug FROM $wpdb->terms WHERE slug = %s", $alt_slug) );
  694. } while ( $slug_check );
  695. $slug = $alt_slug;
  696. if ( empty( $term_group ) ) {
  697. $term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms GROUP BY term_group") + 1;
  698. $wpdb->query( $wpdb->prepare("UPDATE $wpdb->terms SET term_group = %d WHERE term_id = %d", $term_group, $id) );
  699. }
  700. }
  701. $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->terms (term_id, name, slug, term_group) VALUES
  702. (%d, %s, %s, %d)", $term_id, $name, $slug, $term_group) );
  703. $count = 0;
  704. if ( !empty($category->category_count) ) {
  705. $count = (int) $category->category_count;
  706. $taxonomy = 'category';
  707. $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) );
  708. $tt_ids[$term_id][$taxonomy] = (int) $wpdb->insert_id;
  709. }
  710. if ( !empty($category->link_count) ) {
  711. $count = (int) $category->link_count;
  712. $taxonomy = 'link_category';
  713. $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) );
  714. $tt_ids[$term_id][$taxonomy] = (int) $wpdb->insert_id;
  715. }
  716. if ( !empty($category->tag_count) ) {
  717. $have_tags = true;
  718. $count = (int) $category->tag_count;
  719. $taxonomy = 'post_tag';
  720. $wpdb->insert( $wpdb->term_taxonomy, compact('term_id', 'taxonomy', 'description', 'parent', 'count') );
  721. $tt_ids[$term_id][$taxonomy] = (int) $wpdb->insert_id;
  722. }
  723. if ( empty($count) ) {
  724. $count = 0;
  725. $taxonomy = 'category';
  726. $wpdb->insert( $wpdb->term_taxonomy, compact('term_id', 'taxonomy', 'description', 'parent', 'count') );
  727. $tt_ids[$term_id][$taxonomy] = (int) $wpdb->insert_id;
  728. }
  729. }
  730. $select = 'post_id, category_id';
  731. if ( $have_tags )
  732. $select .= ', rel_type';
  733. $posts = $wpdb->get_results("SELECT $select FROM $wpdb->post2cat GROUP BY post_id, category_id");
  734. foreach ( $posts as $post ) {
  735. $post_id = (int) $post->post_id;
  736. $term_id = (int) $post->category_id;
  737. $taxonomy = 'category';
  738. if ( !empty($post->rel_type) && 'tag' == $post->rel_type)
  739. $taxonomy = 'tag';
  740. $tt_id = $tt_ids[$term_id][$taxonomy];
  741. if ( empty($tt_id) )
  742. continue;
  743. $wpdb->insert( $wpdb->term_relationships, array('object_id' => $post_id, 'term_taxonomy_id' => $tt_id) );
  744. }
  745. // < 3570 we used linkcategories. >= 3570 we used categories and link2cat.
  746. if ( $wp_current_db_version < 3570 ) {
  747. // Create link_category terms for link categories. Create a map of link cat IDs
  748. // to link_category terms.
  749. $link_cat_id_map = array();
  750. $default_link_cat = 0;
  751. $tt_ids = array();
  752. $link_cats = $wpdb->get_results("SELECT cat_id, cat_name FROM " . $wpdb->prefix . 'linkcategories');
  753. foreach ( $link_cats as $category) {
  754. $cat_id = (int) $category->cat_id;
  755. $term_id = 0;
  756. $name = $wpdb->escape($category->cat_name);
  757. $slug = sanitize_title($name);
  758. $term_group = 0;
  759. // Associate terms with the same slug in a term group and make slugs unique.
  760. if ( $exists = $wpdb->get_results( $wpdb->prepare("SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $slug) ) ) {
  761. $term_group = $exists[0]->term_group;
  762. $term_id = $exists[0]->term_id;
  763. }
  764. if ( empty($term_id) ) {
  765. $wpdb->insert( $wpdb->terms, compact('name', 'slug', 'term_group') );
  766. $term_id = (int) $wpdb->insert_id;
  767. }
  768. $link_cat_id_map[$cat_id] = $term_id;
  769. $default_link_cat = $term_id;
  770. $wpdb->insert( $wpdb->term_taxonomy, array('term_id' => $term_id, 'taxonomy' => 'link_category', 'description' => '', 'parent' => 0, 'count' => 0) );
  771. $tt_ids[$term_id] = (int) $wpdb->insert_id;
  772. }
  773. // Associate links to cats.
  774. $links = $wpdb->get_results("SELECT link_id, link_category FROM $wpdb->links");
  775. if ( !empty($links) ) foreach ( $links as $link ) {
  776. if ( 0 == $link->link_category )
  777. continue;
  778. if ( ! isset($link_cat_id_map[$link->link_category]) )
  779. continue;
  780. $term_id = $link_cat_id_map[$link->link_category];
  781. $tt_id = $tt_ids[$term_id];
  782. if ( empty($tt_id) )
  783. continue;
  784. $wpdb->insert( $wpdb->term_relationships, array('object_id' => $link->link_id, 'term_taxonomy_id' => $tt_id) );
  785. }
  786. // Set default to the last category we grabbed during the upgrade loop.
  787. update_option('default_link_category', $default_link_cat);
  788. } else {
  789. $links = $wpdb->get_results("SELECT link_id, category_id FROM $wpdb->link2cat GROUP BY link_id, category_id");
  790. foreach ( $links as $link ) {
  791. $link_id = (int) $link->link_id;
  792. $term_id = (int) $link->category_id;
  793. $taxonomy = 'link_category';
  794. $tt_id = $tt_ids[$term_id][$taxonomy];
  795. if ( empty($tt_id) )
  796. continue;
  797. $wpdb->insert( $wpdb->term_relationships, array('object_id' => $link_id, 'term_taxonomy_id' => $tt_id) );
  798. }
  799. }
  800. if ( $wp_current_db_version < 4772 ) {
  801. // Obsolete linkcategories table
  802. $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'linkcategories');
  803. }
  804. // Recalculate all counts
  805. $terms = $wpdb->get_results("SELECT term_taxonomy_id, taxonomy FROM $wpdb->term_taxonomy");
  806. foreach ( (array) $terms as $term ) {
  807. if ( ('post_tag' == $term->taxonomy) || ('category' == $term->taxonomy) )
  808. $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) );
  809. else
  810. $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $term->term_taxonomy_id) );
  811. $wpdb->update( $wpdb->term_taxonomy, array('count' => $count), array('term_taxonomy_id' => $term->term_taxonomy_id) );
  812. }
  813. }
  814. /**
  815. * Remove old options from the database.
  816. *
  817. * @since 2.3.0
  818. */
  819. function upgrade_230_options_table() {
  820. global $wpdb;
  821. $old_options_fields = array( 'option_can_override', 'option_type', 'option_width', 'option_height', 'option_description', 'option_admin_level' );
  822. $wpdb->hide_errors();
  823. foreach ( $old_options_fields as $old )
  824. $wpdb->query("ALTER TABLE $wpdb->options DROP $old");
  825. $wpdb->show_errors();
  826. }
  827. /**
  828. * Remove old categories, link2cat, and post2cat database tables.
  829. *
  830. * @since 2.3.0
  831. */
  832. function upgrade_230_old_tables() {
  833. global $wpdb;
  834. $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'categories');
  835. $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'link2cat');
  836. $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'post2cat');
  837. }
  838. /**
  839. * Upgrade old slugs made in version 2.2.
  840. *
  841. * @since 2.2.0
  842. */
  843. function upgrade_old_slugs() {
  844. // upgrade people who were using the Redirect Old Slugs plugin
  845. global $wpdb;
  846. $wpdb->query("UPDATE $wpdb->postmeta SET meta_key = '_wp_old_slug' WHERE meta_key = 'old_slug'");
  847. }
  848. /**
  849. * Execute changes made in WordPress 2.5.0.
  850. *
  851. * @since 2.5.0
  852. */
  853. function upgrade_250() {
  854. global $wp_current_db_version;
  855. if ( $wp_current_db_version < 6689 ) {
  856. populate_roles_250();
  857. }
  858. }
  859. /**
  860. * Execute changes made in WordPress 2.5.2.
  861. *
  862. * @since 2.5.2
  863. */
  864. function upgrade_252() {
  865. global $wpdb;
  866. $wpdb->query("UPDATE $wpdb->users SET user_activation_key = ''");
  867. }
  868. /**
  869. * Execute changes made in WordPress 2.6.
  870. *
  871. * @since 2.6.0
  872. */
  873. function upgrade_260() {
  874. global $wp_current_db_version;
  875. if ( $wp_current_db_version < 8000 )
  876. populate_roles_260();
  877. if ( $wp_current_db_version < 8201 ) {
  878. update_option('enable_app', 1);
  879. update_option('enable_xmlrpc', 1);
  880. }
  881. }
  882. /**
  883. * Execute changes made in WordPress 2.7.
  884. *
  885. * @since 2.7.0
  886. */
  887. function upgrade_270() {
  888. global $wpdb, $wp_current_db_version;
  889. if ( $wp_current_db_version < 8980 )
  890. populate_roles_270();
  891. // Update post_date for unpublished posts with empty timestamp
  892. if ( $wp_current_db_version < 8921 )
  893. $wpdb->query( "UPDATE $wpdb->posts SET post_date = post_modified WHERE post_date = '0000-00-00 00:00:00'" );
  894. }
  895. /**
  896. * Execute changes made in WordPress 2.8.
  897. *
  898. * @since 2.8.0
  899. */
  900. function upgrade_280() {
  901. global $wp_current_db_version, $wpdb;
  902. if ( $wp_current_db_version < 10360 )
  903. populate_roles_280();
  904. if ( is_multisite() ) {
  905. $start = 0;
  906. while( $rows = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options ORDER BY option_id LIMIT $start, 20" ) ) {
  907. foreach( $rows as $row ) {
  908. $value = $row->option_value;
  909. if ( !@unserialize( $value ) )
  910. $value = stripslashes( $value );
  911. if ( $value !== $row->option_value ) {
  912. update_option( $row->option_name, $value );
  913. }
  914. }
  915. $start += 20;
  916. }
  917. refresh_blog_details( $wpdb->blogid );
  918. }
  919. }
  920. /**
  921. * Execute changes made in WordPress 2.9.
  922. *
  923. * @since 2.9.0
  924. */
  925. function upgrade_290() {
  926. global $wp_current_db_version;
  927. if ( $wp_current_db_version < 11958 ) {
  928. // Previously, setting depth to 1 would redundantly disable threading, but now 2 is the minimum depth to avoid confusion
  929. if ( get_option( 'thread_comments_depth' ) == '1' ) {
  930. update_option( 'thread_comments_depth', 2 );
  931. update_option( 'thread_comments', 0 );
  932. }
  933. }
  934. }
  935. /**
  936. * Execute changes made in WordPress 3.0.
  937. *
  938. * @since 3.0.0
  939. */
  940. function upgrade_300() {
  941. global $wp_current_db_version, $wpdb;
  942. if ( $wp_current_db_version < 15093 )
  943. populate_roles_300();
  944. if ( $wp_current_db_version < 14139 && is_multisite() && is_main_site() && ! defined( 'MULTISITE' ) && get_site_option( 'siteurl' ) === false )
  945. add_site_option( 'siteurl', '' );
  946. // 3.0-alpha nav menu postmeta changes. can be removed before release. // r13802
  947. if ( $wp_current_db_version >= 13226 && $wp_current_db_version < 13974 )
  948. $wpdb->query( "DELETE FROM $wpdb->postmeta WHERE meta_key IN( 'menu_type', 'object_id', 'menu_new_window', 'menu_link', '_menu_item_append', 'menu_item_append', 'menu_item_type', 'menu_item_object_id', 'menu_item_target', 'menu_item_classes', 'menu_item_xfn', 'menu_item_url' )" );
  949. // 3.0-beta1 remove_user primitive->meta cap. can be removed before release. r13956
  950. if ( $wp_current_db_version >= 12751 && $wp_current_db_version < 13974 ) {
  951. $role =& get_role( 'administrator' );
  952. if ( ! empty( $role ) )
  953. $role->remove_cap( 'remove_user' );
  954. }
  955. // 3.0-beta1 nav menu postmeta changes. can be removed before release. r13974
  956. if ( $wp_current_db_version >= 13802 && $wp_current_db_version < 13974 )
  957. $wpdb->update( $wpdb->postmeta, array( 'meta_value' => '' ), array( 'meta_key' => '_menu_item_target', 'meta_value' => '_self' ) );
  958. // 3.0 screen options key name changes.
  959. if ( is_main_site() && !defined('DO_NOT_UPGRADE_GLOBAL_TABLES') ) {
  960. $prefix = like_escape($wpdb->base_prefix);
  961. $wpdb->query( "DELETE FROM $wpdb->usermeta WHERE meta_key LIKE '{$prefix}%meta-box-hidden%' OR meta_key LIKE '{$prefix}%closedpostboxes%' OR meta_key LIKE '{$prefix}%manage-%-columns-hidden%' OR meta_key LIKE '{$prefix}%meta-box-order%' OR meta_key LIKE '{$prefix}%metaboxorder%' OR meta_key LIKE '{$prefix}%screen_layout%'
  962. OR meta_key = 'manageedittagscolumnshidden' OR meta_key='managecategoriescolumnshidden' OR meta_key = 'manageedit-tagscolumnshidden' OR meta_key = 'manageeditcolumnshidden' OR meta_key = 'categories_per_page' OR meta_key = 'edit_tags_per_page'" );
  963. }
  964. }
  965. /**
  966. * Execute network level changes
  967. *
  968. * @since 3.0.0
  969. */
  970. function upgrade_network() {
  971. global $wp_current_db_version, $wpdb;
  972. // 2.8
  973. if ( $wp_current_db_version < 11549 ) {
  974. $wpmu_sitewide_plugins = get_site_option( 'wpmu_sitewide_plugins' );
  975. $active_sitewide_plugins = get_site_option( 'active_sitewide_plugins' );
  976. if ( $wpmu_sitewide_plugins ) {
  977. if ( !$active_sitewide_plugins )
  978. $sitewide_plugins = (array) $wpmu_sitewide_plugins;
  979. else
  980. $sitewide_plugins = array_merge( (array) $active_sitewide_plugins, (array) $wpmu_sitewide_plugins );
  981. update_site_option( 'active_sitewide_plugins', $sitewide_plugins );
  982. }
  983. delete_site_option( 'wpmu_sitewide_plugins' );
  984. delete_site_option( 'deactivated_sitewide_plugins' );
  985. $start = 0;
  986. while( $rows = $wpdb->get_results( "SELECT meta_key, meta_value FROM {$wpdb->sitemeta} ORDER BY meta_id LIMIT $start, 20" ) ) {
  987. foreach( $rows as $row ) {
  988. $value = $row->meta_value;
  989. if ( !@unserialize( $value ) )
  990. $value = stripslashes( $value );
  991. if ( $value !== $row->meta_value ) {
  992. update_site_option( $row->meta_key, $value );
  993. }
  994. }
  995. $start += 20;
  996. }
  997. }
  998. // 3.0
  999. if ( $wp_current_db_version < 13576 )
  1000. update_site_option( 'global_terms_enabled', '1' );
  1001. }
  1002. // The functions we use to actually do stuff
  1003. // General
  1004. /**
  1005. * {@internal Missing Short Description}}
  1006. *
  1007. * {@internal Missing Long Description}}
  1008. *
  1009. * @since 1.0.0
  1010. *
  1011. * @param string $table_name Database table name to create.
  1012. * @param string $create_ddl SQL statement to create table.
  1013. * @return bool If table already exists or was created by function.
  1014. */
  1015. function maybe_create_table($table_name, $create_ddl) {
  1016. global $wpdb;
  1017. if ( $wpdb->get_var("SHOW TABLES LIKE '$table_name'") == $table_name )
  1018. return true;
  1019. //didn't find it try to create it.
  1020. $q = $wpdb->query($create_ddl);
  1021. // we cannot directly tell that whether this succeeded!
  1022. if ( $wpdb->get_var("SHOW TABLES LIKE '$table_name'") == $table_name )
  1023. return true;
  1024. return false;
  1025. }
  1026. /**
  1027. * {@internal Missing Short Description}}
  1028. *
  1029. * {@internal Missing Long Description}}
  1030. *
  1031. * @since 1.0.1
  1032. *
  1033. * @param string $table Database table name.
  1034. * @param string $index Index name to drop.
  1035. * @return bool True, when finished.
  1036. */
  1037. function drop_index($table, $index) {
  1038. global $wpdb;
  1039. $wpdb->hide_errors();
  1040. $wpdb->query("ALTER TABLE `$table` DROP INDEX `$index`");
  1041. // Now we need to take out all the extra ones we may have created
  1042. for ($i = 0; $i < 25; $i++) {
  1043. $wpdb->query("ALTER TABLE `$table` DROP INDEX `{$index}_$i`");
  1044. }
  1045. $wpdb->show_errors();
  1046. return true;
  1047. }
  1048. /**
  1049. * {@internal Missing Short Description}}
  1050. *
  1051. * {@internal Missing Long Description}}
  1052. *
  1053. * @since 1.0.1
  1054. *
  1055. * @param string $table Database table name.
  1056. * @param string $index Database table index column.
  1057. * @return bool True, when done with execution.
  1058. */
  1059. function add_clean_index($table, $index) {
  1060. global $wpdb;
  1061. drop_index($table, $index);
  1062. $wpdb->query("ALTER TABLE `$table` ADD INDEX ( `$index` )");
  1063. return true;
  1064. }
  1065. /**
  1066. ** maybe_add_column()
  1067. ** Add column to db table if it doesn't exist.
  1068. ** Returns: true if already exists or on successful completion
  1069. ** false on error
  1070. */
  1071. function maybe_add_column($table_name, $column_name, $create_ddl) {
  1072. global $wpdb, $debug;
  1073. foreach ($wpdb->get_col("DESC $table_name", 0) as $column ) {
  1074. if ($debug) echo("checking $column == $column_name<br />");
  1075. if ($column == $column_name) {
  1076. return true;
  1077. }
  1078. }
  1079. //didn't find it try to create it.
  1080. $q = $wpdb->query($create_ddl);
  1081. // we cannot directly tell that whether this succeeded!
  1082. foreach ($wpdb->get_col("DESC $table_name", 0) as $column ) {
  1083. if ($column == $column_name) {
  1084. return true;
  1085. }
  1086. }
  1087. return false;
  1088. }
  1089. /**
  1090. * Retrieve all options as it was for 1.2.
  1091. *
  1092. * @since 1.2.0
  1093. *
  1094. * @return array List of options.
  1095. */
  1096. function get_alloptions_110() {
  1097. global $wpdb;
  1098. if ($options = $wpdb->get_results("SELECT option_name, option_value FROM $wpdb->options")) {
  1099. foreach ($options as $option) {
  1100. // "When trying to design a foolproof system,
  1101. // never underestimate the ingenuity of the fools :)" -- Dougal
  1102. if ('siteurl' == $option->option_name) $option->option_value = preg_replace('|/+$|', '', $option->option_value);
  1103. if ('home' == $option->option_name) $option->option_value = preg_replace('|/+$|', '', $option->option_value);
  1104. if ('category_base' == $option->option_name) $option->option_value = preg_replace('|/+$|', '', $option->option_value);
  1105. $all_options->{$option->option_name} = stripslashes($option->option_value);
  1106. }
  1107. }
  1108. return $all_options;
  1109. }
  1110. /**
  1111. * Version of get_option that is private to install/upgrade.
  1112. *
  1113. * @since 1.5.1
  1114. * @access private
  1115. *
  1116. * @param string $setting Option name.
  1117. * @return mixed
  1118. */
  1119. function __get_option($setting) {
  1120. global $wpdb;
  1121. if ( $setting == 'home' && defined( 'WP_HOME' ) ) {
  1122. return preg_replace( '|/+$|', '', WP_HOME );
  1123. }
  1124. if ( $setting == 'siteurl' && defined( 'WP_SITEURL' ) ) {
  1125. return preg_replace( '|/+$|', '', WP_SITEURL );
  1126. }
  1127. $option = $wpdb->get_var( $wpdb->prepare("SELECT option_value FROM $wpdb->options WHERE option_name = %s", $setting) );
  1128. if ( 'home' == $setting && '' == $option )
  1129. return __get_option('siteurl');
  1130. if ( 'siteurl' == $setting || 'home' == $setting || 'category_base' == $setting )
  1131. $option = preg_replace('|/+$|', '', $option);
  1132. @ $kellogs = unserialize($option);
  1133. if ($kellogs !== FALSE)
  1134. return $kellogs;
  1135. else
  1136. return $option;
  1137. }
  1138. /**
  1139. * {@internal Missing Short Description}}
  1140. *
  1141. * {@internal Missing Long Description}}
  1142. *
  1143. * @since 1.5.0
  1144. *
  1145. * @param string $content
  1146. * @return string
  1147. */
  1148. function deslash($content) {
  1149. // Note: \\\ inside a regex denotes a single backslash.
  1150. // Replace one or more backslashes followed by a single quote with
  1151. // a single quote.
  1152. $content = preg_replace("/\\\+'/", "'", $content);
  1153. // Replace one or more backslashes followed by a double quote with
  1154. // a double quote.
  1155. $content = preg_replace('/\\\+"/', '"', $content);
  1156. // Replace one or more backslashes with one backslash.
  1157. $content = preg_replace("/\\\+/", "\\", $content);
  1158. return $content;
  1159. }
  1160. /**
  1161. * {@internal Missing Short Description}}
  1162. *
  1163. * {@internal Missing Long Description}}
  1164. *
  1165. * @since 1.5.0
  1166. *
  1167. * @param unknown_type $queries
  1168. * @param unknown_type $execute
  1169. * @return unknown
  1170. */
  1171. function dbDelta($queries, $execute = true) {
  1172. global $wpdb;
  1173. // Separate individual queries into an array
  1174. if ( !is_array($queries) ) {
  1175. $queries = explode( ';', $queries );
  1176. if ('' == $queries[count($queries) - 1]) array_pop($queries);
  1177. }
  1178. $cqueries = array(); // Creation Queries
  1179. $iqueries = array(); // Insertion Queries
  1180. $for_update = array();
  1181. // Create a tablename index for an array ($cqueries) of queries
  1182. foreach($queries as $qry) {
  1183. if (preg_match("|CREATE TABLE ([^ ]*)|", $qry, $matches)) {
  1184. $cqueries[trim( strtolower($matches[1]), '`' )] = $qry;
  1185. $for_update[$matches[1]] = 'Created table '.$matches[1];
  1186. } else if (preg_match("|CREATE DATABASE ([^ ]*)|", $qry, $matches)) {
  1187. array_unshift($cqueries, $qry);
  1188. } else if (preg_match("|INSERT INTO ([^ ]*)|", $qry, $matches)) {
  1189. $iqueries[] = $qry;
  1190. } else if (preg_match("|UPDATE ([^ ]*)|", $qry, $matches)) {
  1191. $iqueries[] = $qry;
  1192. } else {
  1193. // Unrecognized query type
  1194. }
  1195. }
  1196. // Check to see which tables and fields exist
  1197. if ($tables = $wpdb->get_col('SHOW TABLES;')) {
  1198. // For every table in the database
  1199. foreach ($tables as $table) {
  1200. // Upgrade global tables only for the main site. Don't upgrade at all if DO_NOT_UPGRADE_GLOBAL_TABLES is defined.
  1201. if ( in_array($table, $wpdb->tables('global')) && ( !is_main_site() || defined('DO_NOT_UPGRADE_GLOBAL_TABLES') ) )
  1202. continue;
  1203. // If a table query exists for the database table...
  1204. if ( array_key_exists(

Large files files are truncated, but you can click here to view the full file