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

/wp-admin/includes/upgrade.php

https://bitbucket.org/abnopanda/wordpress
PHP | 2000 lines | 1247 code | 288 blank | 465 comment | 326 complexity | 8a966654f05a872eb733ba7a17b2fcea MD5 | raw 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 < 21501 && $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. }
  1081. // The functions we use to actually do stuff
  1082. // General
  1083. /**
  1084. * {@internal Missing Short Description}}
  1085. *
  1086. * {@internal Missing Long Description}}
  1087. *
  1088. * @since 1.0.0
  1089. *
  1090. * @param string $table_name Database table name to create.
  1091. * @param string $create_ddl SQL statement to create table.
  1092. * @return bool If table already exists or was created by function.
  1093. */
  1094. function maybe_create_table($table_name, $create_ddl) {
  1095. global $wpdb;
  1096. if ( $wpdb->get_var("SHOW TABLES LIKE '$table_name'") == $table_name )
  1097. return true;
  1098. //didn't find it try to create it.
  1099. $q = $wpdb->query($create_ddl);
  1100. // we cannot directly tell that whether this succeeded!
  1101. if ( $wpdb->get_var("SHOW TABLES LIKE '$table_name'") == $table_name )
  1102. return true;
  1103. return false;
  1104. }
  1105. /**
  1106. * {@internal Missing Short Description}}
  1107. *
  1108. * {@internal Missing Long Description}}
  1109. *
  1110. * @since 1.0.1
  1111. *
  1112. * @param string $table Database table name.
  1113. * @param string $index Index name to drop.
  1114. * @return bool True, when finished.
  1115. */
  1116. function drop_index($table, $index) {
  1117. global $wpdb;
  1118. $wpdb->hide_errors();
  1119. $wpdb->query("ALTER TABLE `$table` DROP INDEX `$index`");
  1120. // Now we need to take out all the extra ones we may have created
  1121. for ($i = 0; $i < 25; $i++) {
  1122. $wpdb->query("ALTER TABLE `$table` DROP INDEX `{$index}_$i`");
  1123. }
  1124. $wpdb->show_errors();
  1125. return true;
  1126. }
  1127. /**
  1128. * {@internal Missing Short Description}}
  1129. *
  1130. * {@internal Missing Long Description}}
  1131. *
  1132. * @since 1.0.1
  1133. *
  1134. * @param string $table Database table name.
  1135. * @param string $index Database table index column.
  1136. * @return bool True, when done with execution.
  1137. */
  1138. function add_clean_index($table, $index) {
  1139. global $wpdb;
  1140. drop_index($table, $index);
  1141. $wpdb->query("ALTER TABLE `$table` ADD INDEX ( `$index` )");
  1142. return true;
  1143. }
  1144. /**
  1145. ** maybe_add_column()
  1146. ** Add column to db table if it doesn't exist.
  1147. ** Returns: true if already exists or on successful completion
  1148. ** false on error
  1149. */
  1150. function maybe_add_column($table_name, $column_name, $create_ddl) {
  1151. global $wpdb;
  1152. foreach ($wpdb->get_col("DESC $table_name", 0) as $column ) {
  1153. if ($column == $column_name) {
  1154. return true;
  1155. }
  1156. }
  1157. //didn't find it try to create it.
  1158. $q = $wpdb->query($create_ddl);
  1159. // we cannot directly tell that whether this succeeded!
  1160. foreach ($wpdb->get_col("DESC $table_name", 0) as $column ) {
  1161. if ($column == $column_name) {
  1162. return true;
  1163. }
  1164. }
  1165. return false;
  1166. }
  1167. /**
  1168. * Retrieve all options as it was for 1.2.
  1169. *
  1170. * @since 1.2.0
  1171. *
  1172. * @return array List of options.
  1173. */
  1174. function get_alloptions_110() {
  1175. global $wpdb;
  1176. $all_options = new stdClass;
  1177. if ( $options = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options" ) ) {
  1178. foreach ( $options as $option ) {
  1179. if ( 'siteurl' == $option->option_name || 'home' == $option->option_name || 'category_base' == $option->option_name )
  1180. $option->option_value = untrailingslashit( $option->option_value );
  1181. $all_options->{$option->option_name} = stripslashes( $option->option_value );
  1182. }
  1183. }
  1184. return $all_options;
  1185. }
  1186. /**
  1187. * Version of get_option that is private to install/upgrade.
  1188. *
  1189. * @since 1.5.1
  1190. * @access private
  1191. *
  1192. * @param string $setting Option name.
  1193. * @return mixed
  1194. */
  1195. function __get_option($setting) {
  1196. global $wpdb;
  1197. if ( $setting == 'home' && defined( 'WP_HOME' ) )
  1198. return untrailingslashit( WP_HOME );
  1199. if ( $setting == 'siteurl' && defined( 'WP_SITEURL' ) )
  1200. return untrailingslashit( WP_SITEURL );
  1201. $option = $wpdb->get_var( $wpdb->prepare("SELECT option_value FROM $wpdb->options WHERE option_name = %s", $setting ) );
  1202. if ( 'home' == $setting && '' == $option )
  1203. return __get_option( 'siteurl' );
  1204. if ( 'siteurl' == $setting || 'home' == $setting || 'category_base' == $setting || 'tag_base' == $setting )
  1205. $option = untrailingslashit( $option );
  1206. @ $kellogs = unserialize( $option );
  1207. if ( $kellogs !== false )
  1208. return $kellogs;
  1209. else
  1210. return $option;
  1211. }
  1212. /**
  1213. * {@internal Missing Short Description}}
  1214. *
  1215. * {@internal Missing Long Description}}
  1216. *
  1217. * @since 1.5.0
  1218. *
  1219. * @param string $content
  1220. * @return string
  1221. */
  1222. function deslash($content) {
  1223. // Note: \\\ inside a regex denotes a single backslash.
  1224. // Replace one or more backslashes followed by a single quote with
  1225. // a single quote.
  1226. $content = preg_replace("/\\\+'/", "'", $content);
  1227. // Replace one or more backslashes followed by a double quote with
  1228. // a double quote.
  1229. $content = preg_replace('/\\\+"/', '"', $content);
  1230. // Replace one or more backslashes with one backslash.
  1231. $content = preg_replace("/\\\+/", "\\", $content);
  1232. return $content;
  1233. }
  1234. /**
  1235. * {@internal Missing Short Description}}
  1236. *
  1237. * {@internal Missing Long Description}}
  1238. *
  1239. * @since 1.5.0
  1240. *
  1241. * @param unknown_type $queries
  1242. * @param unknown_type $execute
  1243. * @return unknown
  1244. */
  1245. function dbDelta( $queries = '', $execute = true ) {
  1246. global $wpdb;
  1247. if ( in_array( $queries, array( '', 'all', 'blog', 'global', 'ms_global' ), true ) )
  1248. $queries = wp_get_db_schema( $queries );
  1249. // Separate individual queries into an array
  1250. if ( !is_array($queries) ) {
  1251. $queries = explode( ';', $queries );
  1252. $queries = array_filter( $queries );
  1253. }
  1254. $queries = apply_filters( 'dbdelta_queries', $queries );
  1255. $cqueries = array(); // Creation Queries
  1256. $iqueries = array(); // Insertion Queries
  1257. $for_update = array();
  1258. // Create a tablename index for an array ($cqueries) of queries
  1259. foreach($queries as $qry) {
  1260. if (preg_match("|CREATE TABLE ([^ ]*)|", $qry, $matches)) {
  1261. $cqueries[ trim( $matches[1], '`' ) ] = $qry;
  1262. $for_update[$matches[1]] = 'Created table '.$matches[1];
  1263. } else if (preg_match("|CREATE DATABASE ([^ ]*)|", $qry, $matches)) {
  1264. array_unshift($cqueries, $qry);
  1265. } else if (preg_match("|INSERT INTO ([^ ]*)|", $qry, $matches)) {
  1266. $iqueries[] = $qry;
  1267. } else if (preg_match("|UPDATE ([^ ]*)|", $qry, $matches)) {
  1268. $iqueries[] = $qry;
  1269. } else {
  1270. // Unrecognized query type
  1271. }
  1272. }
  1273. $cqueries = apply_filters( 'dbdelta_create_queries', $cqueries );
  1274. $iqueries = apply_filters( 'dbdelta_insert_queries', $iqueries );
  1275. $global_tables = $wpdb->tables( 'global' );
  1276. foreach ( $cqueries as $table => $qry ) {
  1277. // Upgrade global tables only for the main site. Don't upgrade at all if DO_NOT_UPGRADE_GLOBAL_TABLES is defined.
  1278. if ( in_array( $table, $global_tables ) && ( !is_main_site() || defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) ) )
  1279. continue;
  1280. // Fetch the table column structure from the database
  1281. $wpdb->suppress_errors();
  1282. $tablefields = $wpdb->get_results("DESCRIBE {$table};");
  1283. $wpdb->suppress_errors( false );
  1284. if ( ! $tablefields )
  1285. continue;
  1286. // Clear the field and index arrays
  1287. $cfields = $indices = array();
  1288. // Get all of the field names in the query from between the parens
  1289. preg_match("|\((.*)\)|ms", $qry, $match2);
  1290. $qryline = trim($match2[1]);
  1291. // Separate field lines into an array
  1292. $flds = explode("\n", $qryline);
  1293. //echo "<hr/><pre>\n".print_r(strtolower($table), true).":\n".print_r($cqueries, true)."</pre><hr/>";
  1294. // For every field line specified in the query
  1295. foreach ($flds as $fld) {
  1296. // Extract the field name
  1297. preg_match("|^([^ ]*)|", trim($fld), $fvals);
  1298. $fieldname = trim( $fvals[1], '`' );
  1299. // Verify the found field name
  1300. $validfield = true;
  1301. switch (strtolower($fieldname)) {
  1302. case '':
  1303. case 'primary':
  1304. case 'index':
  1305. case 'fulltext':
  1306. case 'unique':
  1307. case 'key':
  1308. $validfield = false;
  1309. $indices[] = trim(trim($fld), ", \n");
  1310. break;
  1311. }
  1312. $fld = trim($fld);
  1313. // If it's a valid field, add it to the field array
  1314. if ($validfield) {
  1315. $cfields[strtolower($fieldname)] = trim($fld, ", \n");
  1316. }
  1317. }
  1318. // For every field in the table
  1319. foreach ($tablefields as $tablefield) {
  1320. // If the table field exists in the field array...
  1321. if (array_key_exists(strtolower($tablefield->Field), $cfields)) {
  1322. // Get the field type from the query
  1323. preg_match("|".$tablefield->Field." ([^ ]*( unsigned)?)|i", $cfields[strtolower($tablefield->Field)], $matches);
  1324. $fieldtype = $matches[1];
  1325. // Is actual field type different from the field type in query?
  1326. if ($tablefield->Type != $fieldtype) {
  1327. // Add a query to change the column type
  1328. $cqueries[] = "ALTER TABLE {$table} CHANGE COLUMN {$tablefield->Field} " . $cfields[strtolower($tablefield->Field)];
  1329. $for_update[$table.'.'.$tablefield->Field] = "Changed type of {$table}.{$tablefield->Field} from {$tablefield->Type} to {$fieldtype}";
  1330. }
  1331. // Get the default value from the array
  1332. //echo "{$cfields[strtolower($tablefield->Field)]}<br>";
  1333. if (preg_match("| DEFAULT '(.*)'|i", $cfields[strtolower($tablefield->Field)], $matches)) {
  1334. $default_value = $matches[1];
  1335. if ($tablefield->Default != $default_value) {
  1336. // Add a query to change the column's default value
  1337. $cqueries[] = "ALTER TABLE {$table} ALTER COLUMN {$tablefield->Field} SET DEFAULT '{$default_value}'";
  1338. $for_update[$table.'.'.$tablefield->Field] = "Changed default value of {$table}.{$tablefield->Field} from {$tablefield->Default} to {$default_value}";
  1339. }
  1340. }
  1341. // Remove the field from the array (so it's not added)
  1342. unset($cfields[strtolower($tablefield->Field)]);
  1343. } else {
  1344. // This field exists in the table, but not in the creation queries?
  1345. }
  1346. }
  1347. // For every remaining field specified for the table
  1348. foreach ($cfields as $fieldname => $fielddef) {
  1349. // Push a query line into $cqueries that adds the field to that table
  1350. $cqueries[] = "ALTER TABLE {$table} ADD COLUMN $fielddef";
  1351. $for_update[$table.'.'.$fieldname] = 'Added column '.$table.'.'.$fieldname;
  1352. }
  1353. // Index stuff goes here
  1354. // Fetch the table index structure from the database
  1355. $tableindices = $wpdb->get_results("SHOW INDEX FROM {$table};");
  1356. if ($tableindices) {
  1357. // Clear the index array
  1358. unset($index_ary);
  1359. // For every index in the table
  1360. foreach ($tableindices as $tableindex) {
  1361. // Add the index to the index data array
  1362. $keyname = $tableindex->Key_name;
  1363. $index_ary[$keyname]['columns'][] = array('fieldname' => $tableindex->Column_name, 'subpart' => $tableindex->Sub_part);
  1364. $index_ary[$keyname]['unique'] = ($tableindex->Non_unique == 0)?true:false;
  1365. }
  1366. // For each actual index in the index array
  1367. foreach ($index_ary as $index_name => $index_data) {
  1368. // Build a create string to compare to the query
  1369. $index_string = '';
  1370. if ($index_name == 'PRIMARY') {
  1371. $index_string .= 'PRIMARY ';
  1372. } else if($index_data['unique']) {
  1373. $index_string .= 'UNIQUE ';
  1374. }
  1375. $index_string .= 'KEY ';
  1376. if ($index_name != 'PRIMARY') {
  1377. $index_string .= $index_name;
  1378. }
  1379. $index_columns = '';
  1380. // For each column in the index
  1381. foreach ($index_data['columns'] as $column_data) {
  1382. if ($index_columns != '') $index_columns .= ',';
  1383. // Add the field to the column list string
  1384. $index_columns .= $column_data['fieldname'];
  1385. if ($column_data['subpart'] != '') {
  1386. $index_columns .= '('.$column_data['subpart'].')';
  1387. }
  1388. }
  1389. // Add the column list to the index create string
  1390. $index_string .= ' ('.$index_columns.')';
  1391. if (!(($aindex = array_search($index_string, $indices)) === false)) {
  1392. unset($indices[$aindex]);
  1393. //echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">{$table}:<br />Found index:".$index_string."</pre>\n";
  1394. }
  1395. //else echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">{$table}:<br /><b>Did not find index:</b>".$index_string."<br />".print_r($indices, true)."</pre>\n";
  1396. }
  1397. }
  1398. // For every remaining index specified for the table
  1399. foreach ( (array) $indices as $index ) {
  1400. // Push a query line into $cqueries that adds the index to that table
  1401. $cqueries[] = "ALTER TABLE {$table} ADD $index";
  1402. $for_update[$table.'.'.$fieldname] = 'Added index '.$table.' '.$index;
  1403. }
  1404. // Remove the original table creation query from processing
  1405. unset( $cqueries[ $table ], $for_update[ $table ] );
  1406. }
  1407. $allqueries = array_merge($cqueries, $iqueries);
  1408. if ($execute) {
  1409. foreach ($allqueries as $query) {
  1410. //echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">".print_r($query, true)."</pre>\n";
  1411. $wpdb->query($query);
  1412. }
  1413. }
  1414. return $for_update;
  1415. }
  1416. /**
  1417. * {@internal Missing Short Description}}
  1418. *
  1419. * {@internal Missing Long Description}}
  1420. *
  1421. * @since 1.5.0
  1422. */
  1423. function make_db_current( $tables = 'all' ) {
  1424. $alterations = dbDelta( $tables );
  1425. echo "<ol>\n";
  1426. foreach($alterations as $alteration) echo "<li>$alteration</li>\n";
  1427. echo "</ol>\n";
  1428. }
  1429. /**
  1430. * {@internal Missing Short Description}}
  1431. *
  1432. * {@internal Missing Long Description}}
  1433. *
  1434. * @since 1.5.0
  1435. */
  1436. function make_db_current_silent( $tables = 'all' ) {
  1437. $alterations = dbDelta( $tables );
  1438. }
  1439. /**
  1440. * {@internal Missing Short Description}}
  1441. *
  1442. * {@internal Missing Long Description}}
  1443. *
  1444. * @since 1.5.0
  1445. *
  1446. * @param unknown_type $theme_name
  1447. * @param unknown_type $template
  1448. * @return unknown
  1449. */
  1450. function make_site_theme_from_oldschool($theme_name, $template) {
  1451. $home_path = get_home_path();
  1452. $site_dir = WP_CONTENT_DIR . "/themes/$template";
  1453. if (! file_exists("$home_path/index.php"))
  1454. return false;
  1455. // Copy files from the old locations to the site theme.
  1456. // TODO: This does not copy arbitrary include dependencies. Only the
  1457. // standard WP files are copied.
  1458. $files = array('index.php' => 'index.php', 'wp-layout.css' => 'style.css', 'wp-comments.php' => 'comments.php', 'wp-comments-popup.php' => 'comments-popup.php');
  1459. foreach ($files as $oldfile => $newfile) {
  1460. if ($oldfile == 'index.php')
  1461. $oldpath = $home_path;
  1462. else
  1463. $oldpath = ABSPATH;
  1464. if ($oldfile == 'index.php') { // Check to make sure it's not a new index
  1465. $index = implode('', file("$oldpath/$oldfile"));
  1466. if (strpos($index, 'WP_USE_THEMES') !== false) {
  1467. if (! @copy(WP_CONTENT_DIR . '/themes/' . WP_DEFAULT_THEME . '/index.php', "$site_dir/$newfile"))
  1468. return false;
  1469. continue; // Don't copy anything
  1470. }
  1471. }
  1472. if (! @copy("$oldpath/$oldfile", "$site_dir/$newfile"))
  1473. return false;
  1474. chmod("$site_dir/$newfile", 0777);
  1475. // Update the blog header include in each file.
  1476. $lines = explode("\n", implode('', file("$site_dir/$newfile")));
  1477. if ($lines) {
  1478. $f = fopen("$site_dir/$newfile", 'w');
  1479. foreach ($lines as $line) {
  1480. if (preg_match('/require.*wp-blog-header/', $line))
  1481. $line = '//' . $line;
  1482. // Update stylesheet references.
  1483. $line = str_replace("<?php echo __get_option('siteurl'); ?>/wp-layout.css", "<?php bloginfo('stylesheet_url'); ?>", $line);
  1484. // Update comments template inclusion.
  1485. $line = str_replace("<?php include(ABSPATH . 'wp-comments.php'); ?>", "<?php comments_template(); ?>", $line);
  1486. fwrite($f, "{$line}\n");
  1487. }
  1488. fclose($f);
  1489. }
  1490. }
  1491. // Add a theme header.
  1492. $header = "/*\nTheme Name: $theme_name\nTheme URI: " . __get_option('siteurl') . "\nDescription: A theme automatically created by the update.\nVersion: 1.0\nAuthor: Moi\n*/\n";
  1493. $stylelines = file_get_contents("$site_dir/style.css");
  1494. if ($stylelines) {
  1495. $f = fopen("$site_dir/style.css", 'w');
  1496. fwrite($f, $header);
  1497. fwrite($f, $stylelines);
  1498. fclose($f);
  1499. }
  1500. return true;
  1501. }
  1502. /**
  1503. * {@internal Missing Short Description}}
  1504. *
  1505. * {@internal Missing Long Description}}
  1506. *
  1507. * @since 1.5.0
  1508. *
  1509. * @param unknown_type $theme_name
  1510. * @param unknown_type $template
  1511. * @return unknown
  1512. */
  1513. function make_site_theme_from_default($theme_name, $template) {
  1514. $site_dir = WP_CONTENT_DIR . "/themes/$template";
  1515. $default_dir = WP_CONTENT_DIR . '/themes/' . WP_DEFAULT_THEME;
  1516. // Copy files from the default theme to the site theme.
  1517. //$files = array('index.php', 'comments.php', 'comments-popup.php', 'footer.php', 'header.php', 'sidebar.php', 'style.css');
  1518. $theme_dir = @ opendir($default_dir);
  1519. if ($theme_dir) {
  1520. while(($theme_file = readdir( $theme_dir )) !== false) {
  1521. if (is_dir("$default_dir/$theme_file"))
  1522. continue;
  1523. if (! @copy("$default_dir/$theme_file", "$site_dir/$theme_file"))
  1524. return;
  1525. chmod("$site_dir/$theme_file", 0777);
  1526. }
  1527. }
  1528. @closedir($theme_dir);
  1529. // Rewrite the theme header.
  1530. $stylelines = explode("\n", implode('', file("$site_dir/style.css")));
  1531. if ($stylelines) {
  1532. $f = fopen("$site_dir/style.css", 'w');
  1533. foreach ($stylelines as $line) {
  1534. if (strpos($line, 'Theme Name:') !== false) $line = 'Theme Name: ' . $theme_name;
  1535. elseif (strpos($line, 'Theme URI:') !== false) $line = 'Theme URI: ' . __get_option('url');
  1536. elseif (strpos($line, 'Description:') !== false) $line = 'Description: Your theme.';
  1537. elseif (strpos($line, 'Version:') !== false) $line = 'Version: 1';
  1538. elseif (strpos($line, 'Author:') !== false) $line = 'Author: You';
  1539. fwrite($f, $line . "\n");
  1540. }
  1541. fclose($f);
  1542. }
  1543. // Copy the images.
  1544. umask(0);
  1545. if (! mkdir("$site_dir/images", 0777)) {
  1546. return false;
  1547. }
  1548. $images_dir = @ opendir("$default_dir/images");
  1549. if ($images_dir) {
  1550. while(($image = readdir($images_dir)) !== false) {
  1551. if (is_dir("$default_dir/images/$image"))
  1552. continue;
  1553. if (! @copy("$default_dir/images/$image", "$site_dir/images/$image"))
  1554. return;
  1555. chmod("$site_dir/images/$image", 0777);
  1556. }
  1557. }
  1558. @closedir($images_dir);
  1559. }
  1560. // Create a site theme from the default theme.
  1561. /**
  1562. * {@internal Missing Short Description}}
  1563. *
  1564. * {@internal Missing Long Description}}
  1565. *
  1566. * @since 1.5.0
  1567. *
  1568. * @return unknown
  1569. */
  1570. function make_site_theme() {
  1571. // Name the theme after the blog.
  1572. $theme_name = __get_option('blogname');
  1573. $template = sanitize_title($theme_name);
  1574. $site_dir = WP_CONTENT_DIR . "/themes/$template";
  1575. // If the theme already exists, nothing to do.
  1576. if ( is_dir($site_dir)) {
  1577. return false;
  1578. }
  1579. // We must be able to write to the themes dir.
  1580. if (! is_writable(WP_CONTENT_DIR . "/themes")) {
  1581. return false;
  1582. }
  1583. umask(0);
  1584. if (! mkdir($site_dir, 0777)) {
  1585. return false;
  1586. }
  1587. if (file_exists(ABSPATH . 'wp-layout.css')) {
  1588. if (! make_site_theme_from_oldschool($theme_name, $template)) {
  1589. // TODO: rm -rf the site theme directory.
  1590. return false;
  1591. }
  1592. } else {
  1593. if (! make_site_theme_from_default($theme_name, $template))
  1594. // TODO: rm -rf the site theme directory.
  1595. return false;
  1596. }
  1597. // Make the new site theme active.
  1598. $current_template = __get_option('template');
  1599. if ($current_template == WP_DEFAULT_THEME) {
  1600. update_option('template', $template);
  1601. update_option('stylesheet', $template);
  1602. }
  1603. return $template;
  1604. }
  1605. /**
  1606. * Translate user level to user role name.
  1607. *
  1608. * @since 2.0.0
  1609. *
  1610. * @param int $level User level.
  1611. * @return string User role name.
  1612. */
  1613. function translate_level_to_role($level) {
  1614. switch ($level) {
  1615. case 10:
  1616. case 9:
  1617. case 8:
  1618. return 'administrator';
  1619. case 7:
  1620. case 6:
  1621. case 5:
  1622. return 'editor';
  1623. case 4:
  1624. case 3:
  1625. case 2:
  1626. return 'author';
  1627. case 1:
  1628. return 'contributor';
  1629. case 0:
  1630. return 'subscriber';
  1631. }
  1632. }
  1633. /**
  1634. * {@internal Missing Short Description}}
  1635. *
  1636. * {@internal Missing Long Description}}
  1637. *
  1638. * @since 2.1.0
  1639. */
  1640. function wp_check_mysql_version() {
  1641. global $wpdb;
  1642. $result = $wpdb->check_database_version();
  1643. if ( is_wp_error( $result ) )
  1644. die( $result->get_error_message() );
  1645. }
  1646. /**
  1647. * Disables the Automattic widgets plugin, which was merged into core.
  1648. *
  1649. * @since 2.2.0
  1650. */
  1651. function maybe_disable_automattic_widgets() {
  1652. $plugins = __get_option( 'active_plugins' );
  1653. foreach ( (array) $plugins as $plugin ) {
  1654. if ( basename( $plugin ) == 'widgets.php' ) {
  1655. array_splice( $plugins, array_search( $plugin, $plugins ), 1 );
  1656. update_option( 'active_plugins', $plugins );
  1657. break;
  1658. }
  1659. }
  1660. }
  1661. /**
  1662. * Disables the Link Manager on upgrade, if at the time of upgrade, no links exist in the DB.
  1663. *
  1664. * @since 3.5.0
  1665. */
  1666. function maybe_disable_link_manager() {
  1667. global $wp_current_db_version, $wpdb;
  1668. if ( $wp_current_db_version >= 21501 && get_option( 'link_manager_enabled' ) && ! $wpdb->get_var( "SELECT link_id FROM $wpdb->links LIMIT 1" ) )
  1669. update_option( 'link_manager_enabled', 0 );
  1670. }
  1671. /**
  1672. * Runs before the schema is upgraded.
  1673. *
  1674. * @since 2.9.0
  1675. */
  1676. function pre_schema_upgrade() {
  1677. global $wp_current_db_version, $wp_db_version, $wpdb;
  1678. // Upgrade versions prior to 2.9
  1679. if ( $wp_current_db_version < 11557 ) {
  1680. // Delete duplicate options. Keep the option with the highest option_id.
  1681. $wpdb->query("DELETE o1 FROM $wpdb->options AS o1 JOIN $wpdb->options AS o2 USING (`option_name`) WHERE o2.option_id > o1.option_id");
  1682. // Drop the old primary key and add the new.
  1683. $wpdb->query("ALTER TABLE $wpdb->options DROP PRIMARY KEY, ADD PRIMARY KEY(option_id)");
  1684. // Drop the old option_name index. dbDelta() doesn't do the drop.
  1685. $wpdb->query("ALTER TABLE $wpdb->options DROP INDEX option_name");
  1686. }
  1687. }
  1688. /**
  1689. * Install global terms.
  1690. *
  1691. * @since 3.0.0
  1692. *
  1693. */
  1694. if ( !function_exists( 'install_global_terms' ) ) :
  1695. function install_global_terms() {
  1696. global $wpdb, $charset_collate;
  1697. $ms_queries = "
  1698. CREATE TABLE $wpdb->sitecategories (
  1699. cat_ID bigint(20) NOT NULL auto_increment,
  1700. cat_name varchar(55) NOT NULL default '',
  1701. category_nicename varchar(200) NOT NULL default '',
  1702. last_updated timestamp NOT NULL,
  1703. PRIMARY KEY (cat_ID),
  1704. KEY category_nicename (category_nicename),
  1705. KEY last_updated (last_updated)
  1706. ) $charset_collate;
  1707. ";
  1708. // now create tables
  1709. dbDelta( $ms_queries );
  1710. }
  1711. endif;