PageRenderTime 53ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/includes/upgrade.php

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

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