PageRenderTime 62ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-admin/includes/upgrade.php

https://bitbucket.org/crypticrod/sr_wp_code
PHP | 2001 lines | 1266 code | 273 blank | 462 comment | 294 complexity | 167d87210e7c1b3910bda8f5e78b675e MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1, GPL-3.0, LGPL-2.0, AGPL-3.0
  1. <?php
  2. /**
  3. * WordPress Upgrade API
  4. *
  5. * Most of the functions are pluggable and can be overwritten
  6. *
  7. * @package WordPress
  8. * @subpackage Administration
  9. */
  10. /** Include user install customize script. */
  11. if ( file_exists(WP_CONTENT_DIR . '/install.php') )
  12. require (WP_CONTENT_DIR . '/install.php');
  13. /** WordPress Administration API */
  14. require_once(ABSPATH . 'wp-admin/includes/admin.php');
  15. /** WordPress Schema API */
  16. require_once(ABSPATH . 'wp-admin/includes/schema.php');
  17. if ( !function_exists('wp_install') ) :
  18. /**
  19. * Installs the blog
  20. *
  21. * {@internal Missing Long Description}}
  22. *
  23. * @since 2.1.0
  24. *
  25. * @param string $blog_title Blog title.
  26. * @param string $user_name User's username.
  27. * @param string $user_email User's email.
  28. * @param bool $public Whether blog is public.
  29. * @param null $deprecated Optional. Not used.
  30. * @param string $user_password Optional. User's chosen password. Will default to a random password.
  31. * @return array Array keys 'url', 'user_id', 'password', 'password_message'.
  32. */
  33. function wp_install( $blog_title, $user_name, $user_email, $public, $deprecated = '', $user_password = '' ) {
  34. global $wp_rewrite;
  35. if ( !empty( $deprecated ) )
  36. _deprecated_argument( __FUNCTION__, '2.6' );
  37. wp_check_mysql_version();
  38. wp_cache_flush();
  39. make_db_current_silent();
  40. populate_options();
  41. populate_roles();
  42. update_option('blogname', $blog_title);
  43. update_option('admin_email', $user_email);
  44. update_option('blog_public', $public);
  45. $guessurl = wp_guess_url();
  46. update_option('siteurl', $guessurl);
  47. // If not a public blog, don't ping.
  48. if ( ! $public )
  49. update_option('default_pingback_flag', 0);
  50. // Create default user. If the user already exists, the user tables are
  51. // being shared among blogs. Just set the role in that case.
  52. $user_id = username_exists($user_name);
  53. $user_password = trim($user_password);
  54. $email_password = false;
  55. if ( !$user_id && empty($user_password) ) {
  56. $user_password = wp_generate_password( 12, false );
  57. $message = __('<strong><em>Note that password</em></strong> carefully! It is a <em>random</em> password that was generated just for you.');
  58. $user_id = wp_create_user($user_name, $user_password, $user_email);
  59. update_user_option($user_id, 'default_password_nag', true, true);
  60. $email_password = true;
  61. } else if ( !$user_id ) {
  62. // Password has been provided
  63. $message = '<em>'.__('Your chosen password.').'</em>';
  64. $user_id = wp_create_user($user_name, $user_password, $user_email);
  65. } else {
  66. $message = __('User already exists. Password inherited.');
  67. }
  68. $user = new WP_User($user_id);
  69. $user->set_role('administrator');
  70. wp_install_defaults($user_id);
  71. $wp_rewrite->flush_rules();
  72. wp_new_blog_notification($blog_title, $guessurl, $user_id, ($email_password ? $user_password : __('The password you chose during the install.') ) );
  73. wp_cache_flush();
  74. return array('url' => $guessurl, 'user_id' => $user_id, 'password' => $user_password, 'password_message' => $message);
  75. }
  76. endif;
  77. if ( !function_exists('wp_install_defaults') ) :
  78. /**
  79. * {@internal Missing Short Description}}
  80. *
  81. * {@internal Missing Long Description}}
  82. *
  83. * @since 2.1.0
  84. *
  85. * @param int $user_id User ID.
  86. */
  87. function wp_install_defaults($user_id) {
  88. global $wpdb, $wp_rewrite, $current_site, $table_prefix;
  89. // Default category
  90. $cat_name = __('Uncategorized');
  91. /* translators: Default category slug */
  92. $cat_slug = sanitize_title(_x('Uncategorized', 'Default category slug'));
  93. if ( global_terms_enabled() ) {
  94. $cat_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM {$wpdb->sitecategories} WHERE category_nicename = %s", $cat_slug ) );
  95. if ( $cat_id == null ) {
  96. $wpdb->insert( $wpdb->sitecategories, array('cat_ID' => 0, 'cat_name' => $cat_name, 'category_nicename' => $cat_slug, 'last_updated' => current_time('mysql', true)) );
  97. $cat_id = $wpdb->insert_id;
  98. }
  99. update_option('default_category', $cat_id);
  100. } else {
  101. $cat_id = 1;
  102. }
  103. $wpdb->insert( $wpdb->terms, array('term_id' => $cat_id, 'name' => $cat_name, 'slug' => $cat_slug, 'term_group' => 0) );
  104. $wpdb->insert( $wpdb->term_taxonomy, array('term_id' => $cat_id, 'taxonomy' => 'category', 'description' => '', 'parent' => 0, 'count' => 1));
  105. $cat_tt_id = $wpdb->insert_id;
  106. // Default link category
  107. $cat_name = __('Blogroll');
  108. /* translators: Default link category slug */
  109. $cat_slug = sanitize_title(_x('Blogroll', 'Default link category slug'));
  110. if ( global_terms_enabled() ) {
  111. $blogroll_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM {$wpdb->sitecategories} WHERE category_nicename = %s", $cat_slug ) );
  112. if ( $blogroll_id == null ) {
  113. $wpdb->insert( $wpdb->sitecategories, array('cat_ID' => 0, 'cat_name' => $cat_name, 'category_nicename' => $cat_slug, 'last_updated' => current_time('mysql', true)) );
  114. $blogroll_id = $wpdb->insert_id;
  115. }
  116. update_option('default_link_category', $blogroll_id);
  117. } else {
  118. $blogroll_id = 2;
  119. }
  120. $wpdb->insert( $wpdb->terms, array('term_id' => $blogroll_id, 'name' => $cat_name, 'slug' => $cat_slug, 'term_group' => 0) );
  121. $wpdb->insert( $wpdb->term_taxonomy, array('term_id' => $blogroll_id, 'taxonomy' => 'link_category', 'description' => '', 'parent' => 0, 'count' => 7));
  122. $blogroll_tt_id = $wpdb->insert_id;
  123. // Now drop in some default links
  124. $default_links = array();
  125. $default_links[] = array( 'link_url' => 'http://codex.wordpress.org/',
  126. 'link_name' => 'Documentation',
  127. 'link_rss' => '',
  128. 'link_notes' => '');
  129. $default_links[] = array( 'link_url' => 'http://wordpress.org/news/',
  130. 'link_name' => 'WordPress Blog',
  131. 'link_rss' => 'http://wordpress.org/news/feed/',
  132. 'link_notes' => '');
  133. $default_links[] = array( 'link_url' => 'http://wordpress.org/extend/ideas/',
  134. 'link_name' => 'Suggest Ideas',
  135. 'link_rss' => '',
  136. 'link_notes' =>'');
  137. $default_links[] = array( 'link_url' => 'http://wordpress.org/support/',
  138. 'link_name' => 'Support Forum',
  139. 'link_rss' => '',
  140. 'link_notes' =>'');
  141. $default_links[] = array( 'link_url' => 'http://wordpress.org/extend/plugins/',
  142. 'link_name' => 'Plugins',
  143. 'link_rss' => '',
  144. 'link_notes' =>'');
  145. $default_links[] = array( 'link_url' => 'http://wordpress.org/extend/themes/',
  146. 'link_name' => 'Themes',
  147. 'link_rss' => '',
  148. 'link_notes' =>'');
  149. $default_links[] = array( 'link_url' => 'http://planet.wordpress.org/',
  150. 'link_name' => 'WordPress Planet',
  151. 'link_rss' => '',
  152. 'link_notes' =>'');
  153. foreach ( $default_links as $link ) {
  154. $wpdb->insert( $wpdb->links, $link);
  155. $wpdb->insert( $wpdb->term_relationships, array('term_taxonomy_id' => $blogroll_tt_id, 'object_id' => $wpdb->insert_id) );
  156. }
  157. // First post
  158. $now = date('Y-m-d H:i:s');
  159. $now_gmt = gmdate('Y-m-d H:i:s');
  160. $first_post_guid = get_option('home') . '/?p=1';
  161. if ( is_multisite() ) {
  162. $first_post = get_site_option( 'first_post' );
  163. if ( empty($first_post) )
  164. $first_post = stripslashes( __( 'Welcome to <a href="SITE_URL">SITE_NAME</a>. This is your first post. Edit or delete it, then start blogging!' ) );
  165. $first_post = str_replace( "SITE_URL", esc_url( network_home_url() ), $first_post );
  166. $first_post = str_replace( "SITE_NAME", $current_site->site_name, $first_post );
  167. } else {
  168. $first_post = __('Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!');
  169. }
  170. $wpdb->insert( $wpdb->posts, array(
  171. 'post_author' => $user_id,
  172. 'post_date' => $now,
  173. 'post_date_gmt' => $now_gmt,
  174. 'post_content' => $first_post,
  175. 'post_excerpt' => '',
  176. 'post_title' => __('Hello world!'),
  177. /* translators: Default post slug */
  178. 'post_name' => sanitize_title( _x('hello-world', 'Default post slug') ),
  179. 'post_modified' => $now,
  180. 'post_modified_gmt' => $now_gmt,
  181. 'guid' => $first_post_guid,
  182. 'comment_count' => 1,
  183. 'to_ping' => '',
  184. 'pinged' => '',
  185. 'post_content_filtered' => ''
  186. ));
  187. $wpdb->insert( $wpdb->term_relationships, array('term_taxonomy_id' => $cat_tt_id, 'object_id' => 1) );
  188. // Default comment
  189. $first_comment_author = __('Mr WordPress');
  190. $first_comment_url = 'http://wordpress.org/';
  191. $first_comment = __('Hi, this is a comment.<br />To delete a comment, just log in and view the post&#039;s comments. There you will have the option to edit or delete them.');
  192. if ( is_multisite() ) {
  193. $first_comment_author = get_site_option( 'first_comment_author', $first_comment_author );
  194. $first_comment_url = get_site_option( 'first_comment_url', network_home_url() );
  195. $first_comment = get_site_option( 'first_comment', $first_comment );
  196. }
  197. $wpdb->insert( $wpdb->comments, array(
  198. 'comment_post_ID' => 1,
  199. 'comment_author' => $first_comment_author,
  200. 'comment_author_email' => '',
  201. 'comment_author_url' => $first_comment_url,
  202. 'comment_date' => $now,
  203. 'comment_date_gmt' => $now_gmt,
  204. 'comment_content' => $first_comment
  205. ));
  206. // First Page
  207. $first_page = sprintf( __( "This is an example page. It's different from a blog post because it will stay in one place and will show up in your site navigation (in most themes). Most people start with an About page that introduces them to potential site visitors. It might say something like this:
  208. <blockquote>Hi there! I'm a bike messenger by day, aspiring actor by night, and this is my blog. I live in Los Angeles, have a great dog named Jack, and I like pi&#241;a coladas. (And gettin' caught in the rain.)</blockquote>
  209. ...or something like this:
  210. <blockquote>The XYZ Doohickey Company was founded in 1971, and has been providing quality doohickies to the public ever since. Located in Gotham City, XYZ employs over 2,000 people and does all kinds of awesome things for the Gotham community.</blockquote>
  211. As a new WordPress user, you should go to <a href=\"%s\">your dashboard</a> to delete this page and create new pages for your content. Have fun!" ), admin_url() );
  212. if ( is_multisite() )
  213. $first_page = get_site_option( 'first_page', $first_page );
  214. $first_post_guid = get_option('home') . '/?page_id=2';
  215. $wpdb->insert( $wpdb->posts, array(
  216. 'post_author' => $user_id,
  217. 'post_date' => $now,
  218. 'post_date_gmt' => $now_gmt,
  219. 'post_content' => $first_page,
  220. 'post_excerpt' => '',
  221. 'post_title' => __( 'Sample Page' ),
  222. /* translators: Default page slug */
  223. 'post_name' => __( 'sample-page' ),
  224. 'post_modified' => $now,
  225. 'post_modified_gmt' => $now_gmt,
  226. 'guid' => $first_post_guid,
  227. 'post_type' => 'page',
  228. 'to_ping' => '',
  229. 'pinged' => '',
  230. 'post_content_filtered' => ''
  231. ));
  232. $wpdb->insert( $wpdb->postmeta, array( 'post_id' => 2, 'meta_key' => '_wp_page_template', 'meta_value' => 'default' ) );
  233. // Set up default widgets for default theme.
  234. update_option( 'widget_search', array ( 2 => array ( 'title' => '' ), '_multiwidget' => 1 ) );
  235. update_option( 'widget_recent-posts', array ( 2 => array ( 'title' => '', 'number' => 5 ), '_multiwidget' => 1 ) );
  236. update_option( 'widget_recent-comments', array ( 2 => array ( 'title' => '', 'number' => 5 ), '_multiwidget' => 1 ) );
  237. update_option( 'widget_archives', array ( 2 => array ( 'title' => '', 'count' => 0, 'dropdown' => 0 ), '_multiwidget' => 1 ) );
  238. update_option( 'widget_categories', array ( 2 => array ( 'title' => '', 'count' => 0, 'hierarchical' => 0, 'dropdown' => 0 ), '_multiwidget' => 1 ) );
  239. update_option( 'widget_meta', array ( 2 => array ( 'title' => '' ), '_multiwidget' => 1 ) );
  240. update_option( 'sidebars_widgets', array ( 'wp_inactive_widgets' => array ( ), 'primary-widget-area' => array ( 0 => 'search-2', 1 => 'recent-posts-2', 2 => 'recent-comments-2', 3 => 'archives-2', 4 => 'categories-2', 5 => 'meta-2', ), 'secondary-widget-area' => array ( ), 'first-footer-widget-area' => array ( ), 'second-footer-widget-area' => array ( ), 'third-footer-widget-area' => array ( ), 'fourth-footer-widget-area' => array ( ), 'array_version' => 3 ) );
  241. if ( is_multisite() ) {
  242. // Flush rules to pick up the new page.
  243. $wp_rewrite->init();
  244. $wp_rewrite->flush_rules();
  245. $user = new WP_User($user_id);
  246. $wpdb->update( $wpdb->options, array('option_value' => $user->user_email), array('option_name' => 'admin_email') );
  247. // Remove all perms except for the login user.
  248. $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE user_id != %d AND meta_key = %s", $user_id, $table_prefix.'user_level') );
  249. $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE user_id != %d AND meta_key = %s", $user_id, $table_prefix.'capabilities') );
  250. // Delete any caps that snuck into the previously active blog. (Hardcoded to blog 1 for now.) TODO: Get previous_blog_id.
  251. if ( !is_super_admin( $user_id ) && $user_id != 1 )
  252. $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE user_id = %d AND meta_key = %s", $user_id, $wpdb->base_prefix.'1_capabilities') );
  253. }
  254. }
  255. endif;
  256. if ( !function_exists('wp_new_blog_notification') ) :
  257. /**
  258. * {@internal Missing Short Description}}
  259. *
  260. * {@internal Missing Long Description}}
  261. *
  262. * @since 2.1.0
  263. *
  264. * @param string $blog_title Blog title.
  265. * @param string $blog_url Blog url.
  266. * @param int $user_id User ID.
  267. * @param string $password User's Password.
  268. */
  269. function wp_new_blog_notification($blog_title, $blog_url, $user_id, $password) {
  270. $user = new WP_User($user_id);
  271. $email = $user->user_email;
  272. $name = $user->user_login;
  273. $message = sprintf(__("Your new WordPress site has been successfully set up at:
  274. %1\$s
  275. You can log in to the administrator account with the following information:
  276. Username: %2\$s
  277. Password: %3\$s
  278. We hope you enjoy your new site. Thanks!
  279. --The WordPress Team
  280. http://wordpress.org/
  281. "), $blog_url, $name, $password);
  282. @wp_mail($email, __('New WordPress Site'), $message);
  283. }
  284. endif;
  285. if ( !function_exists('wp_upgrade') ) :
  286. /**
  287. * Run WordPress Upgrade functions.
  288. *
  289. * {@internal Missing Long Description}}
  290. *
  291. * @since 2.1.0
  292. *
  293. * @return null
  294. */
  295. function wp_upgrade() {
  296. global $wp_current_db_version, $wp_db_version, $wpdb;
  297. $wp_current_db_version = __get_option('db_version');
  298. // We are up-to-date. Nothing to do.
  299. if ( $wp_db_version == $wp_current_db_version )
  300. return;
  301. if ( ! is_blog_installed() )
  302. return;
  303. wp_check_mysql_version();
  304. wp_cache_flush();
  305. pre_schema_upgrade();
  306. make_db_current_silent();
  307. upgrade_all();
  308. if ( is_multisite() && is_main_site() )
  309. upgrade_network();
  310. wp_cache_flush();
  311. if ( is_multisite() ) {
  312. if ( $wpdb->get_row( "SELECT blog_id FROM {$wpdb->blog_versions} WHERE blog_id = '{$wpdb->blogid}'" ) )
  313. $wpdb->query( "UPDATE {$wpdb->blog_versions} SET db_version = '{$wp_db_version}' WHERE blog_id = '{$wpdb->blogid}'" );
  314. else
  315. $wpdb->query( "INSERT INTO {$wpdb->blog_versions} ( `blog_id` , `db_version` , `last_updated` ) VALUES ( '{$wpdb->blogid}', '{$wp_db_version}', NOW());" );
  316. }
  317. }
  318. endif;
  319. /**
  320. * Functions to be called in install and upgrade scripts.
  321. *
  322. * {@internal Missing Long Description}}
  323. *
  324. * @since 1.0.1
  325. */
  326. function upgrade_all() {
  327. global $wp_current_db_version, $wp_db_version, $wp_rewrite;
  328. $wp_current_db_version = __get_option('db_version');
  329. // We are up-to-date. Nothing to do.
  330. if ( $wp_db_version == $wp_current_db_version )
  331. return;
  332. // If the version is not set in the DB, try to guess the version.
  333. if ( empty($wp_current_db_version) ) {
  334. $wp_current_db_version = 0;
  335. // If the template option exists, we have 1.5.
  336. $template = __get_option('template');
  337. if ( !empty($template) )
  338. $wp_current_db_version = 2541;
  339. }
  340. if ( $wp_current_db_version < 6039 )
  341. upgrade_230_options_table();
  342. populate_options();
  343. if ( $wp_current_db_version < 2541 ) {
  344. upgrade_100();
  345. upgrade_101();
  346. upgrade_110();
  347. upgrade_130();
  348. }
  349. if ( $wp_current_db_version < 3308 )
  350. upgrade_160();
  351. if ( $wp_current_db_version < 4772 )
  352. upgrade_210();
  353. if ( $wp_current_db_version < 4351 )
  354. upgrade_old_slugs();
  355. if ( $wp_current_db_version < 5539 )
  356. upgrade_230();
  357. if ( $wp_current_db_version < 6124 )
  358. upgrade_230_old_tables();
  359. if ( $wp_current_db_version < 7499 )
  360. upgrade_250();
  361. if ( $wp_current_db_version < 7935 )
  362. upgrade_252();
  363. if ( $wp_current_db_version < 8201 )
  364. upgrade_260();
  365. if ( $wp_current_db_version < 8989 )
  366. upgrade_270();
  367. if ( $wp_current_db_version < 10360 )
  368. upgrade_280();
  369. if ( $wp_current_db_version < 11958 )
  370. upgrade_290();
  371. if ( $wp_current_db_version < 15260 )
  372. upgrade_300();
  373. maybe_disable_automattic_widgets();
  374. update_option( 'db_version', $wp_db_version );
  375. update_option( 'db_upgraded', true );
  376. }
  377. /**
  378. * Execute changes made in WordPress 1.0.
  379. *
  380. * @since 1.0.0
  381. */
  382. function upgrade_100() {
  383. global $wpdb;
  384. // Get the title and ID of every post, post_name to check if it already has a value
  385. $posts = $wpdb->get_results("SELECT ID, post_title, post_name FROM $wpdb->posts WHERE post_name = ''");
  386. if ($posts) {
  387. foreach($posts as $post) {
  388. if ('' == $post->post_name) {
  389. $newtitle = sanitize_title($post->post_title);
  390. $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_name = %s WHERE ID = %d", $newtitle, $post->ID) );
  391. }
  392. }
  393. }
  394. $categories = $wpdb->get_results("SELECT cat_ID, cat_name, category_nicename FROM $wpdb->categories");
  395. foreach ($categories as $category) {
  396. if ('' == $category->category_nicename) {
  397. $newtitle = sanitize_title($category->cat_name);
  398. $wpdb>update( $wpdb->categories, array('category_nicename' => $newtitle), array('cat_ID' => $category->cat_ID) );
  399. }
  400. }
  401. $wpdb->query("UPDATE $wpdb->options SET option_value = REPLACE(option_value, 'wp-links/links-images/', 'wp-images/links/')
  402. WHERE option_name LIKE 'links_rating_image%'
  403. AND option_value LIKE 'wp-links/links-images/%'");
  404. $done_ids = $wpdb->get_results("SELECT DISTINCT post_id FROM $wpdb->post2cat");
  405. if ($done_ids) :
  406. foreach ($done_ids as $done_id) :
  407. $done_posts[] = $done_id->post_id;
  408. endforeach;
  409. $catwhere = ' AND ID NOT IN (' . implode(',', $done_posts) . ')';
  410. else:
  411. $catwhere = '';
  412. endif;
  413. $allposts = $wpdb->get_results("SELECT ID, post_category FROM $wpdb->posts WHERE post_category != '0' $catwhere");
  414. if ($allposts) :
  415. foreach ($allposts as $post) {
  416. // Check to see if it's already been imported
  417. $cat = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->post2cat WHERE post_id = %d AND category_id = %d", $post->ID, $post->post_category) );
  418. if (!$cat && 0 != $post->post_category) { // If there's no result
  419. $wpdb->insert( $wpdb->post2cat, array('post_id' => $post->ID, 'category_id' => $post->post_category) );
  420. }
  421. }
  422. endif;
  423. }
  424. /**
  425. * Execute changes made in WordPress 1.0.1.
  426. *
  427. * @since 1.0.1
  428. */
  429. function upgrade_101() {
  430. global $wpdb;
  431. // Clean up indices, add a few
  432. add_clean_index($wpdb->posts, 'post_name');
  433. add_clean_index($wpdb->posts, 'post_status');
  434. add_clean_index($wpdb->categories, 'category_nicename');
  435. add_clean_index($wpdb->comments, 'comment_approved');
  436. add_clean_index($wpdb->comments, 'comment_post_ID');
  437. add_clean_index($wpdb->links , 'link_category');
  438. add_clean_index($wpdb->links , 'link_visible');
  439. }
  440. /**
  441. * Execute changes made in WordPress 1.2.
  442. *
  443. * @since 1.2.0
  444. */
  445. function upgrade_110() {
  446. global $wpdb;
  447. // Set user_nicename.
  448. $users = $wpdb->get_results("SELECT ID, user_nickname, user_nicename FROM $wpdb->users");
  449. foreach ($users as $user) {
  450. if ('' == $user->user_nicename) {
  451. $newname = sanitize_title($user->user_nickname);
  452. $wpdb->update( $wpdb->users, array('user_nicename' => $newname), array('ID' => $user->ID) );
  453. }
  454. }
  455. $users = $wpdb->get_results("SELECT ID, user_pass from $wpdb->users");
  456. foreach ($users as $row) {
  457. if (!preg_match('/^[A-Fa-f0-9]{32}$/', $row->user_pass)) {
  458. $wpdb->update( $wpdb->users, array('user_pass' => md5($row->user_pass)), array('ID' => $row->ID) );
  459. }
  460. }
  461. // Get the GMT offset, we'll use that later on
  462. $all_options = get_alloptions_110();
  463. $time_difference = $all_options->time_difference;
  464. $server_time = time()+date('Z');
  465. $weblogger_time = $server_time + $time_difference*3600;
  466. $gmt_time = time();
  467. $diff_gmt_server = ($gmt_time - $server_time) / 3600;
  468. $diff_weblogger_server = ($weblogger_time - $server_time) / 3600;
  469. $diff_gmt_weblogger = $diff_gmt_server - $diff_weblogger_server;
  470. $gmt_offset = -$diff_gmt_weblogger;
  471. // Add a gmt_offset option, with value $gmt_offset
  472. add_option('gmt_offset', $gmt_offset);
  473. // Check if we already set the GMT fields (if we did, then
  474. // MAX(post_date_gmt) can't be '0000-00-00 00:00:00'
  475. // <michel_v> I just slapped myself silly for not thinking about it earlier
  476. $got_gmt_fields = ! ($wpdb->get_var("SELECT MAX(post_date_gmt) FROM $wpdb->posts") == '0000-00-00 00:00:00');
  477. if (!$got_gmt_fields) {
  478. // Add or substract time to all dates, to get GMT dates
  479. $add_hours = intval($diff_gmt_weblogger);
  480. $add_minutes = intval(60 * ($diff_gmt_weblogger - $add_hours));
  481. $wpdb->query("UPDATE $wpdb->posts SET post_date_gmt = DATE_ADD(post_date, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)");
  482. $wpdb->query("UPDATE $wpdb->posts SET post_modified = post_date");
  483. $wpdb->query("UPDATE $wpdb->posts SET post_modified_gmt = DATE_ADD(post_modified, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE) WHERE post_modified != '0000-00-00 00:00:00'");
  484. $wpdb->query("UPDATE $wpdb->comments SET comment_date_gmt = DATE_ADD(comment_date, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)");
  485. $wpdb->query("UPDATE $wpdb->users SET user_registered = DATE_ADD(user_registered, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)");
  486. }
  487. }
  488. /**
  489. * Execute changes made in WordPress 1.5.
  490. *
  491. * @since 1.5.0
  492. */
  493. function upgrade_130() {
  494. global $wpdb;
  495. // Remove extraneous backslashes.
  496. $posts = $wpdb->get_results("SELECT ID, post_title, post_content, post_excerpt, guid, post_date, post_name, post_status, post_author FROM $wpdb->posts");
  497. if ($posts) {
  498. foreach($posts as $post) {
  499. $post_content = addslashes(deslash($post->post_content));
  500. $post_title = addslashes(deslash($post->post_title));
  501. $post_excerpt = addslashes(deslash($post->post_excerpt));
  502. if ( empty($post->guid) )
  503. $guid = get_permalink($post->ID);
  504. else
  505. $guid = $post->guid;
  506. $wpdb->update( $wpdb->posts, compact('post_title', 'post_content', 'post_excerpt', 'guid'), array('ID' => $post->ID) );
  507. }
  508. }
  509. // Remove extraneous backslashes.
  510. $comments = $wpdb->get_results("SELECT comment_ID, comment_author, comment_content FROM $wpdb->comments");
  511. if ($comments) {
  512. foreach($comments as $comment) {
  513. $comment_content = deslash($comment->comment_content);
  514. $comment_author = deslash($comment->comment_author);
  515. $wpdb->update($wpdb->comments, compact('comment_content', 'comment_author'), array('comment_ID' => $comment->comment_ID) );
  516. }
  517. }
  518. // Remove extraneous backslashes.
  519. $links = $wpdb->get_results("SELECT link_id, link_name, link_description FROM $wpdb->links");
  520. if ($links) {
  521. foreach($links as $link) {
  522. $link_name = deslash($link->link_name);
  523. $link_description = deslash($link->link_description);
  524. $wpdb->update( $wpdb->links, compact('link_name', 'link_description'), array('link_id' => $link->link_id) );
  525. }
  526. }
  527. $active_plugins = __get_option('active_plugins');
  528. // If plugins are not stored in an array, they're stored in the old
  529. // newline separated format. Convert to new format.
  530. if ( !is_array( $active_plugins ) ) {
  531. $active_plugins = explode("\n", trim($active_plugins));
  532. update_option('active_plugins', $active_plugins);
  533. }
  534. // Obsolete tables
  535. $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optionvalues');
  536. $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optiontypes');
  537. $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optiongroups');
  538. $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optiongroup_options');
  539. // Update comments table to use comment_type
  540. $wpdb->query("UPDATE $wpdb->comments SET comment_type='trackback', comment_content = REPLACE(comment_content, '<trackback />', '') WHERE comment_content LIKE '<trackback />%'");
  541. $wpdb->query("UPDATE $wpdb->comments SET comment_type='pingback', comment_content = REPLACE(comment_content, '<pingback />', '') WHERE comment_content LIKE '<pingback />%'");
  542. // Some versions have multiple duplicate option_name rows with the same values
  543. $options = $wpdb->get_results("SELECT option_name, COUNT(option_name) AS dupes FROM `$wpdb->options` GROUP BY option_name");
  544. foreach ( $options as $option ) {
  545. if ( 1 != $option->dupes ) { // Could this be done in the query?
  546. $limit = $option->dupes - 1;
  547. $dupe_ids = $wpdb->get_col( $wpdb->prepare("SELECT option_id FROM $wpdb->options WHERE option_name = %s LIMIT %d", $option->option_name, $limit) );
  548. if ( $dupe_ids ) {
  549. $dupe_ids = join($dupe_ids, ',');
  550. $wpdb->query("DELETE FROM $wpdb->options WHERE option_id IN ($dupe_ids)");
  551. }
  552. }
  553. }
  554. make_site_theme();
  555. }
  556. /**
  557. * Execute changes made in WordPress 2.0.
  558. *
  559. * @since 2.0.0
  560. */
  561. function upgrade_160() {
  562. global $wpdb, $wp_current_db_version;
  563. populate_roles_160();
  564. $users = $wpdb->get_results("SELECT * FROM $wpdb->users");
  565. foreach ( $users as $user ) :
  566. if ( !empty( $user->user_firstname ) )
  567. update_user_meta( $user->ID, 'first_name', $wpdb->escape($user->user_firstname) );
  568. if ( !empty( $user->user_lastname ) )
  569. update_user_meta( $user->ID, 'last_name', $wpdb->escape($user->user_lastname) );
  570. if ( !empty( $user->user_nickname ) )
  571. update_user_meta( $user->ID, 'nickname', $wpdb->escape($user->user_nickname) );
  572. if ( !empty( $user->user_level ) )
  573. update_user_meta( $user->ID, $wpdb->prefix . 'user_level', $user->user_level );
  574. if ( !empty( $user->user_icq ) )
  575. update_user_meta( $user->ID, 'icq', $wpdb->escape($user->user_icq) );
  576. if ( !empty( $user->user_aim ) )
  577. update_user_meta( $user->ID, 'aim', $wpdb->escape($user->user_aim) );
  578. if ( !empty( $user->user_msn ) )
  579. update_user_meta( $user->ID, 'msn', $wpdb->escape($user->user_msn) );
  580. if ( !empty( $user->user_yim ) )
  581. update_user_meta( $user->ID, 'yim', $wpdb->escape($user->user_icq) );
  582. if ( !empty( $user->user_description ) )
  583. update_user_meta( $user->ID, 'description', $wpdb->escape($user->user_description) );
  584. if ( isset( $user->user_idmode ) ):
  585. $idmode = $user->user_idmode;
  586. if ($idmode == 'nickname') $id = $user->user_nickname;
  587. if ($idmode == 'login') $id = $user->user_login;
  588. if ($idmode == 'firstname') $id = $user->user_firstname;
  589. if ($idmode == 'lastname') $id = $user->user_lastname;
  590. if ($idmode == 'namefl') $id = $user->user_firstname.' '.$user->user_lastname;
  591. if ($idmode == 'namelf') $id = $user->user_lastname.' '.$user->user_firstname;
  592. if (!$idmode) $id = $user->user_nickname;
  593. $wpdb->update( $wpdb->users, array('display_name' => $id), array('ID' => $user->ID) );
  594. endif;
  595. // FIXME: RESET_CAPS is temporary code to reset roles and caps if flag is set.
  596. $caps = get_user_meta( $user->ID, $wpdb->prefix . 'capabilities');
  597. if ( empty($caps) || defined('RESET_CAPS') ) {
  598. $level = get_user_meta($user->ID, $wpdb->prefix . 'user_level', true);
  599. $role = translate_level_to_role($level);
  600. update_user_meta( $user->ID, $wpdb->prefix . 'capabilities', array($role => true) );
  601. }
  602. endforeach;
  603. $old_user_fields = array( 'user_firstname', 'user_lastname', 'user_icq', 'user_aim', 'user_msn', 'user_yim', 'user_idmode', 'user_ip', 'user_domain', 'user_browser', 'user_description', 'user_nickname', 'user_level' );
  604. $wpdb->hide_errors();
  605. foreach ( $old_user_fields as $old )
  606. $wpdb->query("ALTER TABLE $wpdb->users DROP $old");
  607. $wpdb->show_errors();
  608. // populate comment_count field of posts table
  609. $comments = $wpdb->get_results( "SELECT comment_post_ID, COUNT(*) as c FROM $wpdb->comments WHERE comment_approved = '1' GROUP BY comment_post_ID" );
  610. if ( is_array( $comments ) )
  611. foreach ($comments as $comment)
  612. $wpdb->update( $wpdb->posts, array('comment_count' => $comment->c), array('ID' => $comment->comment_post_ID) );
  613. // Some alpha versions used a post status of object instead of attachment and put
  614. // the mime type in post_type instead of post_mime_type.
  615. if ( $wp_current_db_version > 2541 && $wp_current_db_version <= 3091 ) {
  616. $objects = $wpdb->get_results("SELECT ID, post_type FROM $wpdb->posts WHERE post_status = 'object'");
  617. foreach ($objects as $object) {
  618. $wpdb->update( $wpdb->posts, array( 'post_status' => 'attachment',
  619. 'post_mime_type' => $object->post_type,
  620. 'post_type' => ''),
  621. array( 'ID' => $object->ID ) );
  622. $meta = get_post_meta($object->ID, 'imagedata', true);
  623. if ( ! empty($meta['file']) )
  624. update_attached_file( $object->ID, $meta['file'] );
  625. }
  626. }
  627. }
  628. /**
  629. * Execute changes made in WordPress 2.1.
  630. *
  631. * @since 2.1.0
  632. */
  633. function upgrade_210() {
  634. global $wpdb, $wp_current_db_version;
  635. if ( $wp_current_db_version < 3506 ) {
  636. // Update status and type.
  637. $posts = $wpdb->get_results("SELECT ID, post_status FROM $wpdb->posts");
  638. if ( ! empty($posts) ) foreach ($posts as $post) {
  639. $status = $post->post_status;
  640. $type = 'post';
  641. if ( 'static' == $status ) {
  642. $status = 'publish';
  643. $type = 'page';
  644. } else if ( 'attachment' == $status ) {
  645. $status = 'inherit';
  646. $type = 'attachment';
  647. }
  648. $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_status = %s, post_type = %s WHERE ID = %d", $status, $type, $post->ID) );
  649. }
  650. }
  651. if ( $wp_current_db_version < 3845 ) {
  652. populate_roles_210();
  653. }
  654. if ( $wp_current_db_version < 3531 ) {
  655. // Give future posts a post_status of future.
  656. $now = gmdate('Y-m-d H:i:59');
  657. $wpdb->query ("UPDATE $wpdb->posts SET post_status = 'future' WHERE post_status = 'publish' AND post_date_gmt > '$now'");
  658. $posts = $wpdb->get_results("SELECT ID, post_date FROM $wpdb->posts WHERE post_status ='future'");
  659. if ( !empty($posts) )
  660. foreach ( $posts as $post )
  661. wp_schedule_single_event(mysql2date('U', $post->post_date, false), 'publish_future_post', array($post->ID));
  662. }
  663. }
  664. /**
  665. * Execute changes made in WordPress 2.3.
  666. *
  667. * @since 2.3.0
  668. */
  669. function upgrade_230() {
  670. global $wp_current_db_version, $wpdb;
  671. if ( $wp_current_db_version < 5200 ) {
  672. populate_roles_230();
  673. }
  674. // Convert categories to terms.
  675. $tt_ids = array();
  676. $have_tags = false;
  677. $categories = $wpdb->get_results("SELECT * FROM $wpdb->categories ORDER BY cat_ID");
  678. foreach ($categories as $category) {
  679. $term_id = (int) $category->cat_ID;
  680. $name = $category->cat_name;
  681. $description = $category->category_description;
  682. $slug = $category->category_nicename;
  683. $parent = $category->category_parent;
  684. $term_group = 0;
  685. // Associate terms with the same slug in a term group and make slugs unique.
  686. if ( $exists = $wpdb->get_results( $wpdb->prepare("SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $slug) ) ) {
  687. $term_group = $exists[0]->term_group;
  688. $id = $exists[0]->term_id;
  689. $num = 2;
  690. do {
  691. $alt_slug = $slug . "-$num";
  692. $num++;
  693. $slug_check = $wpdb->get_var( $wpdb->prepare("SELECT slug FROM $wpdb->terms WHERE slug = %s", $alt_slug) );
  694. } while ( $slug_check );
  695. $slug = $alt_slug;
  696. if ( empty( $term_group ) ) {
  697. $term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms GROUP BY term_group") + 1;
  698. $wpdb->query( $wpdb->prepare("UPDATE $wpdb->terms SET term_group = %d WHERE term_id = %d", $term_group, $id) );
  699. }
  700. }
  701. $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->terms (term_id, name, slug, term_group) VALUES
  702. (%d, %s, %s, %d)", $term_id, $name, $slug, $term_group) );
  703. $count = 0;
  704. if ( !empty($category->category_count) ) {
  705. $count = (int) $category->category_count;
  706. $taxonomy = 'category';
  707. $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, description, parent, count) VALUES ( %d, %s, %s, %d, %d)", $term_id, $taxonomy, $description, $parent, $count) );
  708. $tt_ids[$term_id][$taxonomy] = (int) $wpdb->insert_id;
  709. }
  710. if ( !empty($category->link_count) ) {
  711. $count = (int) $category->link_count;
  712. $taxonomy = 'link_category';
  713. $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, description, parent, count) VALUES ( %d, %s, %s, %d, %d)", $term_id, $taxonomy, $description, $parent, $count) );
  714. $tt_ids[$term_id][$taxonomy] = (int) $wpdb->insert_id;
  715. }
  716. if ( !empty($category->tag_count) ) {
  717. $have_tags = true;
  718. $count = (int) $category->tag_count;
  719. $taxonomy = 'post_tag';
  720. $wpdb->insert( $wpdb->term_taxonomy, compact('term_id', 'taxonomy', 'description', 'parent', 'count') );
  721. $tt_ids[$term_id][$taxonomy] = (int) $wpdb->insert_id;
  722. }
  723. if ( empty($count) ) {
  724. $count = 0;
  725. $taxonomy = 'category';
  726. $wpdb->insert( $wpdb->term_taxonomy, compact('term_id', 'taxonomy', 'description', 'parent', 'count') );
  727. $tt_ids[$term_id][$taxonomy] = (int) $wpdb->insert_id;
  728. }
  729. }
  730. $select = 'post_id, category_id';
  731. if ( $have_tags )
  732. $select .= ', rel_type';
  733. $posts = $wpdb->get_results("SELECT $select FROM $wpdb->post2cat GROUP BY post_id, category_id");
  734. foreach ( $posts as $post ) {
  735. $post_id = (int) $post->post_id;
  736. $term_id = (int) $post->category_id;
  737. $taxonomy = 'category';
  738. if ( !empty($post->rel_type) && 'tag' == $post->rel_type)
  739. $taxonomy = 'tag';
  740. $tt_id = $tt_ids[$term_id][$taxonomy];
  741. if ( empty($tt_id) )
  742. continue;
  743. $wpdb->insert( $wpdb->term_relationships, array('object_id' => $post_id, 'term_taxonomy_id' => $tt_id) );
  744. }
  745. // < 3570 we used linkcategories. >= 3570 we used categories and link2cat.
  746. if ( $wp_current_db_version < 3570 ) {
  747. // Create link_category terms for link categories. Create a map of link cat IDs
  748. // to link_category terms.
  749. $link_cat_id_map = array();
  750. $default_link_cat = 0;
  751. $tt_ids = array();
  752. $link_cats = $wpdb->get_results("SELECT cat_id, cat_name FROM " . $wpdb->prefix . 'linkcategories');
  753. foreach ( $link_cats as $category) {
  754. $cat_id = (int) $category->cat_id;
  755. $term_id = 0;
  756. $name = $wpdb->escape($category->cat_name);
  757. $slug = sanitize_title($name);
  758. $term_group = 0;
  759. // Associate terms with the same slug in a term group and make slugs unique.
  760. if ( $exists = $wpdb->get_results( $wpdb->prepare("SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $slug) ) ) {
  761. $term_group = $exists[0]->term_group;
  762. $term_id = $exists[0]->term_id;
  763. }
  764. if ( empty($term_id) ) {
  765. $wpdb->insert( $wpdb->terms, compact('name', 'slug', 'term_group') );
  766. $term_id = (int) $wpdb->insert_id;
  767. }
  768. $link_cat_id_map[$cat_id] = $term_id;
  769. $default_link_cat = $term_id;
  770. $wpdb->insert( $wpdb->term_taxonomy, array('term_id' => $term_id, 'taxonomy' => 'link_category', 'description' => '', 'parent' => 0, 'count' => 0) );
  771. $tt_ids[$term_id] = (int) $wpdb->insert_id;
  772. }
  773. // Associate links to cats.
  774. $links = $wpdb->get_results("SELECT link_id, link_category FROM $wpdb->links");
  775. if ( !empty($links) ) foreach ( $links as $link ) {
  776. if ( 0 == $link->link_category )
  777. continue;
  778. if ( ! isset($link_cat_id_map[$link->link_category]) )
  779. continue;
  780. $term_id = $link_cat_id_map[$link->link_category];
  781. $tt_id = $tt_ids[$term_id];
  782. if ( empty($tt_id) )
  783. continue;
  784. $wpdb->insert( $wpdb->term_relationships, array('object_id' => $link->link_id, 'term_taxonomy_id' => $tt_id) );
  785. }
  786. // Set default to the last category we grabbed during the upgrade loop.
  787. update_option('default_link_category', $default_link_cat);
  788. } else {
  789. $links = $wpdb->get_results("SELECT link_id, category_id FROM $wpdb->link2cat GROUP BY link_id, category_id");
  790. foreach ( $links as $link ) {
  791. $link_id = (int) $link->link_id;
  792. $term_id = (int) $link->category_id;
  793. $taxonomy = 'link_category';
  794. $tt_id = $tt_ids[$term_id][$taxonomy];
  795. if ( empty($tt_id) )
  796. continue;
  797. $wpdb->insert( $wpdb->term_relationships, array('object_id' => $link_id, 'term_taxonomy_id' => $tt_id) );
  798. }
  799. }
  800. if ( $wp_current_db_version < 4772 ) {
  801. // Obsolete linkcategories table
  802. $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'linkcategories');
  803. }
  804. // Recalculate all counts
  805. $terms = $wpdb->get_results("SELECT term_taxonomy_id, taxonomy FROM $wpdb->term_taxonomy");
  806. foreach ( (array) $terms as $term ) {
  807. if ( ('post_tag' == $term->taxonomy) || ('category' == $term->taxonomy) )
  808. $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts WHERE $wpdb->posts.ID = $wpdb->term_relationships.object_id AND post_status = 'publish' AND post_type = 'post' AND term_taxonomy_id = %d", $term->term_taxonomy_id) );
  809. else
  810. $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $term->term_taxonomy_id) );
  811. $wpdb->update( $wpdb->term_taxonomy, array('count' => $count), array('term_taxonomy_id' => $term->term_taxonomy_id) );
  812. }
  813. }
  814. /**
  815. * Remove old options from the database.
  816. *
  817. * @since 2.3.0
  818. */
  819. function upgrade_230_options_table() {
  820. global $wpdb;
  821. $old_options_fields = array( 'option_can_override', 'option_type', 'option_width', 'option_height', 'option_description', 'option_admin_level' );
  822. $wpdb->hide_errors();
  823. foreach ( $old_options_fields as $old )
  824. $wpdb->query("ALTER TABLE $wpdb->options DROP $old");
  825. $wpdb->show_errors();
  826. }
  827. /**
  828. * Remove old categories, link2cat, and post2cat database tables.
  829. *
  830. * @since 2.3.0
  831. */
  832. function upgrade_230_old_tables() {
  833. global $wpdb;
  834. $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'categories');
  835. $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'link2cat');
  836. $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'post2cat');
  837. }
  838. /**
  839. * Upgrade old slugs made in version 2.2.
  840. *
  841. * @since 2.2.0
  842. */
  843. function upgrade_old_slugs() {
  844. // upgrade people who were using the Redirect Old Slugs plugin
  845. global $wpdb;
  846. $wpdb->query("UPDATE $wpdb->postmeta SET meta_key = '_wp_old_slug' WHERE meta_key = 'old_slug'");
  847. }
  848. /**
  849. * Execute changes made in WordPress 2.5.0.
  850. *
  851. * @since 2.5.0
  852. */
  853. function upgrade_250() {
  854. global $wp_current_db_version;
  855. if ( $wp_current_db_version < 6689 ) {
  856. populate_roles_250();
  857. }
  858. }
  859. /**
  860. * Execute changes made in WordPress 2.5.2.
  861. *
  862. * @since 2.5.2
  863. */
  864. function upgrade_252() {
  865. global $wpdb;
  866. $wpdb->query("UPDATE $wpdb->users SET user_activation_key = ''");
  867. }
  868. /**
  869. * Execute changes made in WordPress 2.6.
  870. *
  871. * @since 2.6.0
  872. */
  873. function upgrade_260() {
  874. global $wp_current_db_version;
  875. if ( $wp_current_db_version < 8000 )
  876. populate_roles_260();
  877. if ( $wp_current_db_version < 8201 ) {
  878. update_option('enable_app', 1);
  879. update_option('enable_xmlrpc', 1);
  880. }
  881. }
  882. /**
  883. * Execute changes made in WordPress 2.7.
  884. *
  885. * @since 2.7.0
  886. */
  887. function upgrade_270() {
  888. global $wpdb, $wp_current_db_version;
  889. if ( $wp_current_db_version < 8980 )
  890. populate_roles_270();
  891. // Update post_date for unpublished posts with empty timestamp
  892. if ( $wp_current_db_version < 8921 )
  893. $wpdb->query( "UPDATE $wpdb->posts SET post_date = post_modified WHERE post_date = '0000-00-00 00:00:00'" );
  894. }
  895. /**
  896. * Execute changes made in WordPress 2.8.
  897. *
  898. * @since 2.8.0
  899. */
  900. function upgrade_280() {
  901. global $wp_current_db_version, $wpdb;
  902. if ( $wp_current_db_version < 10360 )
  903. populate_roles_280();
  904. if ( is_multisite() ) {
  905. $start = 0;
  906. while( $rows = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options ORDER BY option_id LIMIT $start, 20" ) ) {
  907. foreach( $rows as $row ) {
  908. $value = $row->option_value;
  909. if ( !@unserialize( $value ) )
  910. $value = stripslashes( $value );
  911. if ( $value !== $row->option_value ) {
  912. update_option( $row->option_name, $value );
  913. }
  914. }
  915. $start += 20;
  916. }
  917. refresh_blog_details( $wpdb->blogid );
  918. }
  919. }
  920. /**
  921. * Execute changes made in WordPress 2.9.
  922. *
  923. * @since 2.9.0
  924. */
  925. function upgrade_290() {
  926. global $wp_current_db_version;
  927. if ( $wp_current_db_version < 11958 ) {
  928. // Previously, setting depth to 1 would redundantly disable threading, but now 2 is the minimum depth to avoid confusion
  929. if ( get_option( 'thread_comments_depth' ) == '1' ) {
  930. update_option( 'thread_comments_depth', 2 );
  931. update_option( 'thread_comments', 0 );
  932. }
  933. }
  934. }
  935. /**
  936. * Execute changes made in WordPress 3.0.
  937. *
  938. * @since 3.0.0
  939. */
  940. function upgrade_300() {
  941. global $wp_current_db_version, $wpdb;
  942. if ( $wp_current_db_version < 15093 )
  943. populate_roles_300();
  944. if ( $wp_current_db_version < 14139 && is_multisite() && is_main_site() && ! defined( 'MULTISITE' ) && get_site_option( 'siteurl' ) === false )
  945. add_site_option( 'siteurl', '' );
  946. // 3.0-alpha nav menu postmeta changes. can be removed before release. // r13802
  947. if ( $wp_current_db_version >= 13226 && $wp_current_db_version < 13974 )
  948. $wpdb->query( "DELETE FROM $wpdb->postmeta WHERE meta_key IN( 'menu_type', 'object_id', 'menu_new_window', 'menu_link', '_menu_item_append', 'menu_item_append', 'menu_item_type', 'menu_item_object_id', 'menu_item_target', 'menu_item_classes', 'menu_item_xfn', 'menu_item_url' )" );
  949. // 3.0-beta1 remove_user primitive->meta cap. can be removed before release. r13956
  950. if ( $wp_current_db_version >= 12751 && $wp_current_db_version < 13974 ) {
  951. $role =& get_role( 'administrator' );
  952. if ( ! empty( $role ) )
  953. $role->remove_cap( 'remove_user' );
  954. }
  955. // 3.0-beta1 nav menu postmeta changes. can be removed before release. r13974
  956. if ( $wp_current_db_version >= 13802 && $wp_current_db_version < 13974 )
  957. $wpdb->update( $wpdb->postmeta, array( 'meta_value' => '' ), array( 'meta_key' => '_menu_item_target', 'meta_value' => '_self' ) );
  958. // 3.0 screen options key name changes.
  959. if ( is_main_site() && !defined('DO_NOT_UPGRADE_GLOBAL_TABLES') ) {
  960. $prefix = like_escape($wpdb->base_prefix);
  961. $wpdb->query( "DELETE FROM $wpdb->usermeta WHERE meta_key LIKE '{$prefix}%meta-box-hidden%' OR meta_key LIKE '{$prefix}%closedpostboxes%' OR meta_key LIKE '{$prefix}%manage-%-columns-hidden%' OR meta_key LIKE '{$prefix}%meta-box-order%' OR meta_key LIKE '{$prefix}%metaboxorder%' OR meta_key LIKE '{$prefix}%screen_layout%'
  962. OR meta_key = 'manageedittagscolumnshidden' OR meta_key='managecategoriescolumnshidden' OR meta_key = 'manageedit-tagscolumnshidden' OR meta_key = 'manageeditcolumnshidden' OR meta_key = 'categories_per_page' OR meta_key = 'edit_tags_per_page'" );
  963. }
  964. }
  965. /**
  966. * Execute network level changes
  967. *
  968. * @since 3.0.0
  969. */
  970. function upgrade_network() {
  971. global $wp_current_db_version, $wpdb;
  972. // 2.8
  973. if ( $wp_current_db_version < 11549 ) {
  974. $wpmu_sitewide_plugins = get_site_option( 'wpmu_sitewide_plugins' );
  975. $active_sitewide_plugins = get_site_option( 'active_sitewide_plugins' );
  976. if ( $wpmu_sitewide_plugins ) {
  977. if ( !$active_sitewide_plugins )
  978. $sitewide_plugins = (array) $wpmu_sitewide_plugins;
  979. else
  980. $sitewide_plugins = array_merge( (array) $active_sitewide_plugins, (array) $wpmu_sitewide_plugins );
  981. update_site_option( 'active_sitewide_plugins', $sitewide_plugins );
  982. }
  983. delete_site_option( 'wpmu_sitewide_plugins' );
  984. delete_site_option( 'deactivated_sitewide_plugins' );
  985. $start = 0;
  986. while( $rows = $wpdb->get_results( "SELECT meta_key, meta_value FROM {$wpdb->sitemeta} ORDER BY meta_id LIMIT $start, 20" ) ) {
  987. foreach( $rows as $row ) {
  988. $value = $row->meta_value;
  989. if ( !@unserialize( $value ) )
  990. $value = stripslashes( $value );
  991. if ( $value !== $row->meta_value ) {
  992. update_site_option( $row->meta_key, $value );
  993. }
  994. }
  995. $start += 20;
  996. }
  997. }
  998. // 3.0
  999. if ( $wp_current_db_version < 13576 )
  1000. update_site_option( 'global_terms_enabled', '1' );
  1001. }
  1002. // The functions we use to actually do stuff
  1003. // General
  1004. /**
  1005. * {@internal Missing Short Description}}
  1006. *
  1007. * {@internal Missing Long Description}}
  1008. *
  1009. * @since 1.0.0
  1010. *
  1011. * @param string $table_name Database table name to create.
  1012. * @param string $create_ddl SQL statement to create table.
  1013. * @return bool If table already exists or was created by function.
  1014. */
  1015. function maybe_create_table($table_name, $create_ddl) {
  1016. global $wpdb;
  1017. if ( $wpdb->get_var("SHOW TABLES LIKE '$table_name'") == $table_name )
  1018. return true;
  1019. //didn't find it try to create it.
  1020. $q = $wpdb->query($create_ddl);
  1021. // we cannot directly tell that whether this succeeded!
  1022. if ( $wpdb->get_var("SHOW TABLES LIKE '$table_name'") == $table_name )
  1023. return true;
  1024. return false;
  1025. }
  1026. /**
  1027. * {@internal Missing Short Description}}
  1028. *
  1029. * {@internal Missing Long Description}}
  1030. *
  1031. * @since 1.0.1
  1032. *
  1033. * @param string $table Database table name.
  1034. * @param string $index Index name to drop.
  1035. * @return bool True, when finished.
  1036. */
  1037. function drop_index($table, $index) {
  1038. global $wpdb;
  1039. $wpdb->hide_errors();
  1040. $wpdb->query("ALTER TABLE `$table` DROP INDEX `$index`");
  1041. // Now we need to take out all the extra ones we may have created
  1042. for ($i = 0; $i < 25; $i++) {
  1043. $wpdb->query("ALTER TABLE `$table` DROP INDEX `{$index}_$i`");
  1044. }
  1045. $wpdb->show_errors();
  1046. return true;
  1047. }
  1048. /**
  1049. * {@internal Missing Short Description}}
  1050. *
  1051. * {@internal Missing Long Description}}
  1052. *
  1053. * @since 1.0.1
  1054. *
  1055. * @param string $table Database table name.
  1056. * @param string $index Database table index column.
  1057. * @return bool True, when done with execution.
  1058. */
  1059. function add_clean_index($table, $index) {
  1060. global $wpdb;
  1061. drop_index($table, $index);
  1062. $wpdb->query("ALTER TABLE `$table` ADD INDEX ( `$index` )");
  1063. return true;
  1064. }
  1065. /**
  1066. ** maybe_add_column()
  1067. ** Add column to db table if it doesn't exist.
  1068. ** Returns: true if already exists or on successful completion
  1069. ** false on error
  1070. */
  1071. function maybe_add_column($table_name, $column_name, $create_ddl) {
  1072. global $wpdb, $debug;
  1073. foreach ($wpdb->get_col("DESC $table_name", 0) as $column ) {
  1074. if ($debug) echo("checking $column == $column_name<br />");
  1075. if ($column == $column_name) {
  1076. return true;
  1077. }
  1078. }
  1079. //didn't find it try to create it.
  1080. $q = $wpdb->query($create_ddl);
  1081. // we cannot directly tell that whether this succeeded!
  1082. foreach ($wpdb->get_col("DESC $table_name", 0) as $column ) {
  1083. if ($column == $column_name) {
  1084. return true;
  1085. }
  1086. }
  1087. return false;
  1088. }
  1089. /**
  1090. * Retrieve all options as it was for 1.2.
  1091. *
  1092. * @since 1.2.0
  1093. *
  1094. * @return array List of options.
  1095. */
  1096. function get_alloptions_110() {
  1097. global $wpdb;
  1098. if ($options = $wpdb->get_results("SELECT option_name, option_value FROM $wpdb->options")) {
  1099. foreach ($options as $option) {
  1100. // "When trying to design a foolproof system,
  1101. // never underestimate the ingenuity of the fools :)" -- Dougal
  1102. if ('siteurl' == $option->option_name) $option->option_value = preg_replace('|/+$|', '', $option->option_value);
  1103. if ('home' == $option->option_name) $option->option_value = preg_replace('|/+$|', '', $option->option_value);
  1104. if ('category_base' == $option->option_name) $option->option_value = preg_replace('|/+$|', '', $option->option_value);
  1105. $all_options->{$option->option_name} = stripslashes($option->option_value);
  1106. }
  1107. }
  1108. return $all_options;
  1109. }
  1110. /**
  1111. * Version of get_option that is private to install/upgrade.
  1112. *
  1113. * @since 1.5.1
  1114. * @access private
  1115. *
  1116. * @param string $setting Option name.
  1117. * @return mixed
  1118. */
  1119. function __get_option($setting) {
  1120. global $wpdb;
  1121. if ( $setting == 'home' && defined( 'WP_HOME' ) ) {
  1122. return preg_replace( '|/+$|', '', WP_HOME );
  1123. }
  1124. if ( $setting == 'siteurl' && defined( 'WP_SITEURL' ) ) {
  1125. return preg_replace( '|/+$|', '', WP_SITEURL );
  1126. }
  1127. $option = $wpdb->get_var( $wpdb->prepare("SELECT option_value FROM $wpdb->options WHERE option_name = %s", $setting) );
  1128. if ( 'home' == $setting && '' == $option )
  1129. return __get_option('siteurl');
  1130. if ( 'siteurl' == $setting || 'home' == $setting || 'category_base' == $setting )
  1131. $option = preg_replace('|/+$|', '', $option);
  1132. @ $kellogs = unserialize($option);
  1133. if ($kellogs !== FALSE)
  1134. return $kellogs;
  1135. else
  1136. return $option;
  1137. }
  1138. /**
  1139. * {@internal Missing Short Description}}
  1140. *
  1141. * {@internal Missing Long Description}}
  1142. *
  1143. * @since 1.5.0
  1144. *
  1145. * @param string $content
  1146. * @return string
  1147. */
  1148. function deslash($content) {
  1149. // Note: \\\ inside a regex denotes a single backslash.
  1150. // Replace one or more backslashes followed by a single quote with
  1151. // a single quote.
  1152. $content = preg_replace("/\\\+'/", "'", $content);
  1153. // Replace one or more backslashes followed by a double quote with
  1154. // a double quote.
  1155. $content = preg_replace('/\\\+"/', '"', $content);
  1156. // Replace one or more backslashes with one backslash.
  1157. $content = preg_replace("/\\\+/", "\\", $content);
  1158. return $content;
  1159. }
  1160. /**
  1161. * {@internal Missing Short Description}}
  1162. *
  1163. * {@internal Missing Long Description}}
  1164. *
  1165. * @since 1.5.0
  1166. *
  1167. * @param unknown_type $queries
  1168. * @param unknown_type $execute
  1169. * @return unknown
  1170. */
  1171. function dbDelta($queries, $execute = true) {
  1172. global $wpdb;
  1173. // Separate individual queries into an array
  1174. if ( !is_array($queries) ) {
  1175. $queries = explode( ';', $queries );
  1176. if ('' == $queries[count($queries) - 1]) array_pop($queries);
  1177. }
  1178. $cqueries = array(); // Creation Queries
  1179. $iqueries = array(); // Insertion Queries
  1180. $for_update = array();
  1181. // Create a tablename index for an array ($cqueries) of queries
  1182. foreach($queries as $qry) {
  1183. if (preg_match("|CREATE TABLE ([^ ]*)|", $qry, $matches)) {
  1184. $cqueries[trim( strtolower($matches[1]), '`' )] = $qry;
  1185. $for_update[$matches[1]] = 'Created table '.$matches[1];
  1186. } else if (preg_match("|CREATE DATABASE ([^ ]*)|", $qry, $matches)) {
  1187. array_unshift($cqueries, $qry);
  1188. } else if (preg_match("|INSERT INTO ([^ ]*)|", $qry, $matches)) {
  1189. $iqueries[] = $qry;
  1190. } else if (preg_match("|UPDATE ([^ ]*)|", $qry, $matches)) {
  1191. $iqueries[] = $qry;
  1192. } else {
  1193. // Unrecognized query type
  1194. }
  1195. }
  1196. // Check to see which tables and fields exist
  1197. if ($tables = $wpdb->get_col('SHOW TABLES;')) {
  1198. // For every table in the database
  1199. foreach ($tables as $table) {
  1200. // Upgrade global tables only for the main site. Don't upgrade at all if DO_NOT_UPGRADE_GLOBAL_TABLES is defined.
  1201. if ( in_array($table, $wpdb->tables('global')) && ( !is_main_site() || defined('DO_NOT_UPGRADE_GLOBAL_TABLES') ) )
  1202. continue;
  1203. // If a table query exists for the database table...
  1204. if ( array_key_exists(strtolower($table), $cqueries) ) {
  1205. // Clear the field and index arrays
  1206. $cfields = $indices = array();
  1207. // Get all of the field names in the query from between the parens
  1208. preg_match("|\((.*)\)|ms", $cqueries[strtolower($table)], $match2);
  1209. $qryline = trim($match2[1]);
  1210. // Separate field lines into an array
  1211. $flds = explode("\n", $qryline);
  1212. //echo "<hr/><pre>\n".print_r(strtolower($table), true).":\n".print_r($cqueries, true)."</pre><hr/>";
  1213. // For every field line specified in the query
  1214. foreach ($flds as $fld) {
  1215. // Extract the field name
  1216. preg_match("|^([^ ]*)|", trim($fld), $fvals);
  1217. $fieldname = trim( $fvals[1], '`' );
  1218. // Verify the found field name
  1219. $validfield = true;
  1220. switch (strtolower($fieldname)) {
  1221. case '':
  1222. case 'primary':
  1223. case 'index':
  1224. case 'fulltext':
  1225. case 'unique':
  1226. case 'key':
  1227. $validfield = false;
  1228. $indices[] = trim(trim($fld), ", \n");
  1229. break;
  1230. }
  1231. $fld = trim($fld);
  1232. // If it's a valid field, add it to the field array
  1233. if ($validfield) {
  1234. $cfields[strtolower($fieldname)] = trim($fld, ", \n");
  1235. }
  1236. }
  1237. // Fetch the table column structure from the database
  1238. $tablefields = $wpdb->get_results("DESCRIBE {$table};");
  1239. // For every field in the table
  1240. foreach ($tablefields as $tablefield) {
  1241. // If the table field exists in the field array...
  1242. if (array_key_exists(strtolower($tablefield->Field), $cfields)) {
  1243. // Get the field type from the query
  1244. preg_match("|".$tablefield->Field." ([^ ]*( unsigned)?)|i", $cfields[strtolower($tablefield->Field)], $matches);
  1245. $fieldtype = $matches[1];
  1246. // Is actual field type different from the field type in query?
  1247. if ($tablefield->Type != $fieldtype) {
  1248. // Add a query to change the column type
  1249. $cqueries[] = "ALTER TABLE {$table} CHANGE COLUMN {$tablefield->Field} " . $cfields[strtolower($tablefield->Field)];
  1250. $for_update[$table.'.'.$tablefield->Field] = "Changed type of {$table}.{$tablefield->Field} from {$tablefield->Type} to {$fieldtype}";
  1251. }
  1252. // Get the default value from the array
  1253. //echo "{$cfields[strtolower($tablefield->Field)]}<br>";
  1254. if (preg_match("| DEFAULT '(.*)'|i", $cfields[strtolower($tablefield->Field)], $matches)) {
  1255. $default_value = $matches[1];
  1256. if ($tablefield->Default != $default_value) {
  1257. // Add a query to change the column's default value
  1258. $cqueries[] = "ALTER TABLE {$table} ALTER COLUMN {$tablefield->Field} SET DEFAULT '{$default_value}'";
  1259. $for_update[$table.'.'.$tablefield->Field] = "Changed default value of {$table}.{$tablefield->Field} from {$tablefield->Default} to {$default_value}";
  1260. }
  1261. }
  1262. // Remove the field from the array (so it's not added)
  1263. unset($cfields[strtolower($tablefield->Field)]);
  1264. } else {
  1265. // This field exists in the table, but not in the creation queries?
  1266. }
  1267. }
  1268. // For every remaining field specified for the table
  1269. foreach ($cfields as $fieldname => $fielddef) {
  1270. // Push a query line into $cqueries that adds the field to that table
  1271. $cqueries[] = "ALTER TABLE {$table} ADD COLUMN $fielddef";
  1272. $for_update[$table.'.'.$fieldname] = 'Added column '.$table.'.'.$fieldname;
  1273. }
  1274. // Index stuff goes here
  1275. // Fetch the table index structure from the database
  1276. $tableindices = $wpdb->get_results("SHOW INDEX FROM {$table};");
  1277. if ($tableindices) {
  1278. // Clear the index array
  1279. unset($index_ary);
  1280. // For every index in the table
  1281. foreach ($tableindices as $tableindex) {
  1282. // Add the index to the index data array
  1283. $keyname = $tableindex->Key_name;
  1284. $index_ary[$keyname]['columns'][] = array('fieldname' => $tableindex->Column_name, 'subpart' => $tableindex->Sub_part);
  1285. $index_ary[$keyname]['unique'] = ($tableindex->Non_unique == 0)?true:false;
  1286. }
  1287. // For each actual index in the index array
  1288. foreach ($index_ary as $index_name => $index_data) {
  1289. // Build a create string to compare to the query
  1290. $index_string = '';
  1291. if ($index_name == 'PRIMARY') {
  1292. $index_string .= 'PRIMARY ';
  1293. } else if($index_data['unique']) {
  1294. $index_string .= 'UNIQUE ';
  1295. }
  1296. $index_string .= 'KEY ';
  1297. if ($index_name != 'PRIMARY') {
  1298. $index_string .= $index_name;
  1299. }
  1300. $index_columns = '';
  1301. // For each column in the index
  1302. foreach ($index_data['columns'] as $column_data) {
  1303. if ($index_columns != '') $index_columns .= ',';
  1304. // Add the field to the column list string
  1305. $index_columns .= $column_data['fieldname'];
  1306. if ($column_data['subpart'] != '') {
  1307. $index_columns .= '('.$column_data['subpart'].')';
  1308. }
  1309. }
  1310. // Add the column list to the index create string
  1311. $index_string .= ' ('.$index_columns.')';
  1312. if (!(($aindex = array_search($index_string, $indices)) === false)) {
  1313. unset($indices[$aindex]);
  1314. //echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">{$table}:<br />Found index:".$index_string."</pre>\n";
  1315. }
  1316. //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";
  1317. }
  1318. }
  1319. // For every remaining index specified for the table
  1320. foreach ( (array) $indices as $index ) {
  1321. // Push a query line into $cqueries that adds the index to that table
  1322. $cqueries[] = "ALTER TABLE {$table} ADD $index";
  1323. $for_update[$table.'.'.$fieldname] = 'Added index '.$table.' '.$index;
  1324. }
  1325. // Remove the original table creation query from processing
  1326. unset($cqueries[strtolower($table)]);
  1327. unset($for_update[strtolower($table)]);
  1328. } else {
  1329. // This table exists in the database, but not in the creation queries?
  1330. }
  1331. }
  1332. }
  1333. $allqueries = array_merge($cqueries, $iqueries);
  1334. if ($execute) {
  1335. foreach ($allqueries as $query) {
  1336. //echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">".print_r($query, true)."</pre>\n";
  1337. $wpdb->query($query);
  1338. }
  1339. }
  1340. return $for_update;
  1341. }
  1342. /**
  1343. * {@internal Missing Short Description}}
  1344. *
  1345. * {@internal Missing Long Description}}
  1346. *
  1347. * @since 1.5.0
  1348. */
  1349. function make_db_current() {
  1350. global $wp_queries;
  1351. $alterations = dbDelta($wp_queries);
  1352. echo "<ol>\n";
  1353. foreach($alterations as $alteration) echo "<li>$alteration</li>\n";
  1354. echo "</ol>\n";
  1355. }
  1356. /**
  1357. * {@internal Missing Short Description}}
  1358. *
  1359. * {@internal Missing Long Description}}
  1360. *
  1361. * @since 1.5.0
  1362. */
  1363. function make_db_current_silent() {
  1364. global $wp_queries;
  1365. $alterations = dbDelta($wp_queries);
  1366. }
  1367. /**
  1368. * {@internal Missing Short Description}}
  1369. *
  1370. * {@internal Missing Long Description}}
  1371. *
  1372. * @since 1.5.0
  1373. *
  1374. * @param unknown_type $theme_name
  1375. * @param unknown_type $template
  1376. * @return unknown
  1377. */
  1378. function make_site_theme_from_oldschool($theme_name, $template) {
  1379. $home_path = get_home_path();
  1380. $site_dir = WP_CONTENT_DIR . "/themes/$template";
  1381. if (! file_exists("$home_path/index.php"))
  1382. return false;
  1383. // Copy files from the old locations to the site theme.
  1384. // TODO: This does not copy arbitarary include dependencies. Only the
  1385. // standard WP files are copied.
  1386. $files = array('index.php' => 'index.php', 'wp-layout.css' => 'style.css', 'wp-comments.php' => 'comments.php', 'wp-comments-popup.php' => 'comments-popup.php');
  1387. foreach ($files as $oldfile => $newfile) {
  1388. if ($oldfile == 'index.php')
  1389. $oldpath = $home_path;
  1390. else
  1391. $oldpath = ABSPATH;
  1392. if ($oldfile == 'index.php') { // Check to make sure it's not a new index
  1393. $index = implode('', file("$oldpath/$oldfile"));
  1394. if (strpos($index, 'WP_USE_THEMES') !== false) {
  1395. if (! @copy(WP_CONTENT_DIR . '/themes/' . WP_DEFAULT_THEME . '/index.php', "$site_dir/$newfile"))
  1396. return false;
  1397. continue; // Don't copy anything
  1398. }
  1399. }
  1400. if (! @copy("$oldpath/$oldfile", "$site_dir/$newfile"))
  1401. return false;
  1402. chmod("$site_dir/$newfile", 0777);
  1403. // Update the blog header include in each file.
  1404. $lines = explode("\n", implode('', file("$site_dir/$newfile")));
  1405. if ($lines) {
  1406. $f = fopen("$site_dir/$newfile", 'w');
  1407. foreach ($lines as $line) {
  1408. if (preg_match('/require.*wp-blog-header/', $line))
  1409. $line = '//' . $line;
  1410. // Update stylesheet references.
  1411. $line = str_replace("<?php echo __get_option('siteurl'); ?>/wp-layout.css", "<?php bloginfo('stylesheet_url'); ?>", $line);
  1412. // Update comments template inclusion.
  1413. $line = str_replace("<?php include(ABSPATH . 'wp-comments.php'); ?>", "<?php comments_template(); ?>", $line);
  1414. fwrite($f, "{$line}\n");
  1415. }
  1416. fclose($f);
  1417. }
  1418. }
  1419. // Add a theme header.
  1420. $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";
  1421. $stylelines = file_get_contents("$site_dir/style.css");
  1422. if ($stylelines) {
  1423. $f = fopen("$site_dir/style.css", 'w');
  1424. fwrite($f, $header);
  1425. fwrite($f, $stylelines);
  1426. fclose($f);
  1427. }
  1428. return true;
  1429. }
  1430. /**
  1431. * {@internal Missing Short Description}}
  1432. *
  1433. * {@internal Missing Long Description}}
  1434. *
  1435. * @since 1.5.0
  1436. *
  1437. * @param unknown_type $theme_name
  1438. * @param unknown_type $template
  1439. * @return unknown
  1440. */
  1441. function make_site_theme_from_default($theme_name, $template) {
  1442. $site_dir = WP_CONTENT_DIR . "/themes/$template";
  1443. $default_dir = WP_CONTENT_DIR . '/themes/' . WP_DEFAULT_THEME;
  1444. // Copy files from the default theme to the site theme.
  1445. //$files = array('index.php', 'comments.php', 'comments-popup.php', 'footer.php', 'header.php', 'sidebar.php', 'style.css');
  1446. $theme_dir = @ opendir($default_dir);
  1447. if ($theme_dir) {
  1448. while(($theme_file = readdir( $theme_dir )) !== false) {
  1449. if (is_dir("$default_dir/$theme_file"))
  1450. continue;
  1451. if (! @copy("$default_dir/$theme_file", "$site_dir/$theme_file"))
  1452. return;
  1453. chmod("$site_dir/$theme_file", 0777);
  1454. }
  1455. }
  1456. @closedir($theme_dir);
  1457. // Rewrite the theme header.
  1458. $stylelines = explode("\n", implode('', file("$site_dir/style.css")));
  1459. if ($stylelines) {
  1460. $f = fopen("$site_dir/style.css", 'w');
  1461. foreach ($stylelines as $line) {
  1462. if (strpos($line, 'Theme Name:') !== false) $line = 'Theme Name: ' . $theme_name;
  1463. elseif (strpos($line, 'Theme URI:') !== false) $line = 'Theme URI: ' . __get_option('url');
  1464. elseif (strpos($line, 'Description:') !== false) $line = 'Description: Your theme.';
  1465. elseif (strpos($line, 'Version:') !== false) $line = 'Version: 1';
  1466. elseif (strpos($line, 'Author:') !== false) $line = 'Author: You';
  1467. fwrite($f, $line . "\n");
  1468. }
  1469. fclose($f);
  1470. }
  1471. // Copy the images.
  1472. umask(0);
  1473. if (! mkdir("$site_dir/images", 0777)) {
  1474. return false;
  1475. }
  1476. $images_dir = @ opendir("$default_dir/images");
  1477. if ($images_dir) {
  1478. while(($image = readdir($images_dir)) !== false) {
  1479. if (is_dir("$default_dir/images/$image"))
  1480. continue;
  1481. if (! @copy("$default_dir/images/$image", "$site_dir/images/$image"))
  1482. return;
  1483. chmod("$site_dir/images/$image", 0777);
  1484. }
  1485. }
  1486. @closedir($images_dir);
  1487. }
  1488. // Create a site theme from the default theme.
  1489. /**
  1490. * {@internal Missing Short Description}}
  1491. *
  1492. * {@internal Missing Long Description}}
  1493. *
  1494. * @since 1.5.0
  1495. *
  1496. * @return unknown
  1497. */
  1498. function make_site_theme() {
  1499. // Name the theme after the blog.
  1500. $theme_name = __get_option('blogname');
  1501. $template = sanitize_title($theme_name);
  1502. $site_dir = WP_CONTENT_DIR . "/themes/$template";
  1503. // If the theme already exists, nothing to do.
  1504. if ( is_dir($site_dir)) {
  1505. return false;
  1506. }
  1507. // We must be able to write to the themes dir.
  1508. if (! is_writable(WP_CONTENT_DIR . "/themes")) {
  1509. return false;
  1510. }
  1511. umask(0);
  1512. if (! mkdir($site_dir, 0777)) {
  1513. return false;
  1514. }
  1515. if (file_exists(ABSPATH . 'wp-layout.css')) {
  1516. if (! make_site_theme_from_oldschool($theme_name, $template)) {
  1517. // TODO: rm -rf the site theme directory.
  1518. return false;
  1519. }
  1520. } else {
  1521. if (! make_site_theme_from_default($theme_name, $template))
  1522. // TODO: rm -rf the site theme directory.
  1523. return false;
  1524. }
  1525. // Make the new site theme active.
  1526. $current_template = __get_option('template');
  1527. if ($current_template == WP_DEFAULT_THEME) {
  1528. update_option('template', $template);
  1529. update_option('stylesheet', $template);
  1530. }
  1531. return $template;
  1532. }
  1533. /**
  1534. * Translate user level to user role name.
  1535. *
  1536. * @since 2.0.0
  1537. *
  1538. * @param int $level User level.
  1539. * @return string User role name.
  1540. */
  1541. function translate_level_to_role($level) {
  1542. switch ($level) {
  1543. case 10:
  1544. case 9:
  1545. case 8:
  1546. return 'administrator';
  1547. case 7:
  1548. case 6:
  1549. case 5:
  1550. return 'editor';
  1551. case 4:
  1552. case 3:
  1553. case 2:
  1554. return 'author';
  1555. case 1:
  1556. return 'contributor';
  1557. case 0:
  1558. return 'subscriber';
  1559. }
  1560. }
  1561. /**
  1562. * {@internal Missing Short Description}}
  1563. *
  1564. * {@internal Missing Long Description}}
  1565. *
  1566. * @since 2.1.0
  1567. */
  1568. function wp_check_mysql_version() {
  1569. global $wpdb;
  1570. $result = $wpdb->check_database_version();
  1571. if ( is_wp_error( $result ) )
  1572. die( $result->get_error_message() );
  1573. }
  1574. /**
  1575. * {@internal Missing Short Description}}
  1576. *
  1577. * {@internal Missing Long Description}}
  1578. *
  1579. * @since 2.2.0
  1580. */
  1581. function maybe_disable_automattic_widgets() {
  1582. $plugins = __get_option( 'active_plugins' );
  1583. foreach ( (array) $plugins as $plugin ) {
  1584. if ( basename( $plugin ) == 'widgets.php' ) {
  1585. array_splice( $plugins, array_search( $plugin, $plugins ), 1 );
  1586. update_option( 'active_plugins', $plugins );
  1587. break;
  1588. }
  1589. }
  1590. }
  1591. /**
  1592. * Runs before the schema is upgraded.
  1593. *
  1594. * @since 2.9.0
  1595. */
  1596. function pre_schema_upgrade() {
  1597. global $wp_current_db_version, $wp_db_version, $wpdb;
  1598. // Upgrade versions prior to 2.9
  1599. if ( $wp_current_db_version < 11557 ) {
  1600. // Delete duplicate options. Keep the option with the highest option_id.
  1601. $wpdb->query("DELETE o1 FROM $wpdb->options AS o1 JOIN $wpdb->options AS o2 USING (`option_name`) WHERE o2.option_id > o1.option_id");
  1602. // Drop the old primary key and add the new.
  1603. $wpdb->query("ALTER TABLE $wpdb->options DROP PRIMARY KEY, ADD PRIMARY KEY(option_id)");
  1604. // Drop the old option_name index. dbDelta() doesn't do the drop.
  1605. $wpdb->query("ALTER TABLE $wpdb->options DROP INDEX option_name");
  1606. }
  1607. }
  1608. /**
  1609. * Install Network.
  1610. *
  1611. * @since 3.0.0
  1612. *
  1613. */
  1614. if ( !function_exists( 'install_network' ) ) :
  1615. function install_network() {
  1616. global $wpdb, $charset_collate;
  1617. $ms_queries = "
  1618. CREATE TABLE $wpdb->users (
  1619. ID bigint(20) unsigned NOT NULL auto_increment,
  1620. user_login varchar(60) NOT NULL default '',
  1621. user_pass varchar(64) NOT NULL default '',
  1622. user_nicename varchar(50) NOT NULL default '',
  1623. user_email varchar(100) NOT NULL default '',
  1624. user_url varchar(100) NOT NULL default '',
  1625. user_registered datetime NOT NULL default '0000-00-00 00:00:00',
  1626. user_activation_key varchar(60) NOT NULL default '',
  1627. user_status int(11) NOT NULL default '0',
  1628. display_name varchar(250) NOT NULL default '',
  1629. spam tinyint(2) NOT NULL default '0',
  1630. deleted tinyint(2) NOT NULL default '0',
  1631. PRIMARY KEY (ID),
  1632. KEY user_login_key (user_login),
  1633. KEY user_nicename (user_nicename)
  1634. ) $charset_collate;
  1635. CREATE TABLE $wpdb->blogs (
  1636. blog_id bigint(20) NOT NULL auto_increment,
  1637. site_id bigint(20) NOT NULL default '0',
  1638. domain varchar(200) NOT NULL default '',
  1639. path varchar(100) NOT NULL default '',
  1640. registered datetime NOT NULL default '0000-00-00 00:00:00',
  1641. last_updated datetime NOT NULL default '0000-00-00 00:00:00',
  1642. public tinyint(2) NOT NULL default '1',
  1643. archived enum('0','1') NOT NULL default '0',
  1644. mature tinyint(2) NOT NULL default '0',
  1645. spam tinyint(2) NOT NULL default '0',
  1646. deleted tinyint(2) NOT NULL default '0',
  1647. lang_id int(11) NOT NULL default '0',
  1648. PRIMARY KEY (blog_id),
  1649. KEY domain (domain(50),path(5)),
  1650. KEY lang_id (lang_id)
  1651. ) $charset_collate;
  1652. CREATE TABLE $wpdb->blog_versions (
  1653. blog_id bigint(20) NOT NULL default '0',
  1654. db_version varchar(20) NOT NULL default '',
  1655. last_updated datetime NOT NULL default '0000-00-00 00:00:00',
  1656. PRIMARY KEY (blog_id),
  1657. KEY db_version (db_version)
  1658. ) $charset_collate;
  1659. CREATE TABLE $wpdb->registration_log (
  1660. ID bigint(20) NOT NULL auto_increment,
  1661. email varchar(255) NOT NULL default '',
  1662. IP varchar(30) NOT NULL default '',
  1663. blog_id bigint(20) NOT NULL default '0',
  1664. date_registered datetime NOT NULL default '0000-00-00 00:00:00',
  1665. PRIMARY KEY (ID),
  1666. KEY IP (IP)
  1667. ) $charset_collate;
  1668. CREATE TABLE $wpdb->site (
  1669. id bigint(20) NOT NULL auto_increment,
  1670. domain varchar(200) NOT NULL default '',
  1671. path varchar(100) NOT NULL default '',
  1672. PRIMARY KEY (id),
  1673. KEY domain (domain,path)
  1674. ) $charset_collate;
  1675. CREATE TABLE $wpdb->sitemeta (
  1676. meta_id bigint(20) NOT NULL auto_increment,
  1677. site_id bigint(20) NOT NULL default '0',
  1678. meta_key varchar(255) default NULL,
  1679. meta_value longtext,
  1680. PRIMARY KEY (meta_id),
  1681. KEY meta_key (meta_key),
  1682. KEY site_id (site_id)
  1683. ) $charset_collate;
  1684. CREATE TABLE $wpdb->signups (
  1685. domain varchar(200) NOT NULL default '',
  1686. path varchar(100) NOT NULL default '',
  1687. title longtext NOT NULL,
  1688. user_login varchar(60) NOT NULL default '',
  1689. user_email varchar(100) NOT NULL default '',
  1690. registered datetime NOT NULL default '0000-00-00 00:00:00',
  1691. activated datetime NOT NULL default '0000-00-00 00:00:00',
  1692. active tinyint(1) NOT NULL default '0',
  1693. activation_key varchar(50) NOT NULL default '',
  1694. meta longtext,
  1695. KEY activation_key (activation_key),
  1696. KEY domain (domain)
  1697. ) $charset_collate;
  1698. ";
  1699. // now create tables
  1700. dbDelta( $ms_queries );
  1701. }
  1702. endif;
  1703. /**
  1704. * Install global terms.
  1705. *
  1706. * @since 3.0.0
  1707. *
  1708. */
  1709. if ( !function_exists( 'install_global_terms' ) ) :
  1710. function install_global_terms() {
  1711. global $wpdb, $charset_collate;
  1712. $ms_queries = "
  1713. CREATE TABLE $wpdb->sitecategories (
  1714. cat_ID bigint(20) NOT NULL auto_increment,
  1715. cat_name varchar(55) NOT NULL default '',
  1716. category_nicename varchar(200) NOT NULL default '',
  1717. last_updated timestamp NOT NULL,
  1718. PRIMARY KEY (cat_ID),
  1719. KEY category_nicename (category_nicename),
  1720. KEY last_updated (last_updated)
  1721. ) $charset_collate;
  1722. ";
  1723. // now create tables
  1724. dbDelta( $ms_queries );
  1725. }
  1726. endif;
  1727. ?>