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

/wp-admin/includes/upgrade.php

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