PageRenderTime 63ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/wp-admin/includes/upgrade.php

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

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