/wp-admin/includes/upgrade.php

  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.  
  11. /** Include user install customize script. */ 
  12. if ( file_exists(WP_CONTENT_DIR . '/install.php') ) 
  13. require (WP_CONTENT_DIR . '/install.php'); 
  14.  
  15. /** WordPress Administration API */ 
  16. require_once(ABSPATH . 'wp-admin/includes/admin.php'); 
  17.  
  18. /** WordPress Schema API */ 
  19. require_once(ABSPATH . 'wp-admin/includes/schema.php'); 
  20.  
  21. if ( !function_exists('wp_install') ) : 
  22. /** 
  23. * Installs the site. 
  24. * 
  25. * Runs the required functions to set up and populate the database,  
  26. * including primary admin user and initial options. 
  27. * 
  28. * @since 2.1.0 
  29. * 
  30. * @param string $blog_title Site title. 
  31. * @param string $user_name User's username. 
  32. * @param string $user_email User's email. 
  33. * @param bool $public Whether site is public. 
  34. * @param string $deprecated Optional. Not used. 
  35. * @param string $user_password Optional. User's chosen password. Default empty (random password). 
  36. * @param string $language Optional. Language chosen. Default empty. 
  37. * @return array Array keys 'url', 'user_id', 'password', and 'password_message'. 
  38. */ 
  39. function wp_install( $blog_title, $user_name, $user_email, $public, $deprecated = '', $user_password = '', $language = '' ) { 
  40. if ( !empty( $deprecated ) ) 
  41. _deprecated_argument( __FUNCTION__, '2.6.0' ); 
  42.  
  43. wp_check_mysql_version(); 
  44. wp_cache_flush(); 
  45. make_db_current_silent(); 
  46. populate_options(); 
  47. populate_roles(); 
  48.  
  49. update_option('blogname', $blog_title); 
  50. update_option('admin_email', $user_email); 
  51. update_option('blog_public', $public); 
  52.  
  53. // Freshness of site - in the future, this could get more specific about actions taken, perhaps. 
  54. update_option( 'fresh_site', 1 ); 
  55.  
  56. if ( $language ) { 
  57. update_option( 'WPLANG', $language ); 
  58.  
  59. $guessurl = wp_guess_url(); 
  60.  
  61. update_option('siteurl', $guessurl); 
  62.  
  63. // If not a public blog, don't ping. 
  64. if ( ! $public ) 
  65. update_option('default_pingback_flag', 0); 
  66.  
  67. /** 
  68. * Create default user. If the user already exists, the user tables are 
  69. * being shared among sites. Just set the role in that case. 
  70. */ 
  71. $user_id = username_exists($user_name); 
  72. $user_password = trim($user_password); 
  73. $email_password = false; 
  74. if ( !$user_id && empty($user_password) ) { 
  75. $user_password = wp_generate_password( 12, false ); 
  76. $message = __('<strong><em>Note that password</em></strong> carefully! It is a <em>random</em> password that was generated just for you.'); 
  77. $user_id = wp_create_user($user_name, $user_password, $user_email); 
  78. update_user_option($user_id, 'default_password_nag', true, true); 
  79. $email_password = true; 
  80. } elseif ( ! $user_id ) { 
  81. // Password has been provided 
  82. $message = '<em>'.__('Your chosen password.').'</em>'; 
  83. $user_id = wp_create_user($user_name, $user_password, $user_email); 
  84. } else { 
  85. $message = __('User already exists. Password inherited.'); 
  86.  
  87. $user = new WP_User($user_id); 
  88. $user->set_role('administrator'); 
  89.  
  90. wp_install_defaults($user_id); 
  91.  
  92. wp_install_maybe_enable_pretty_permalinks(); 
  93.  
  94. flush_rewrite_rules(); 
  95.  
  96. wp_new_blog_notification($blog_title, $guessurl, $user_id, ($email_password ? $user_password : __('The password you chose during the install.') ) ); 
  97.  
  98. wp_cache_flush(); 
  99.  
  100. /** 
  101. * Fires after a site is fully installed. 
  102. * 
  103. * @since 3.9.0 
  104. * 
  105. * @param WP_User $user The site owner. 
  106. */ 
  107. do_action( 'wp_install', $user ); 
  108.  
  109. return array('url' => $guessurl, 'user_id' => $user_id, 'password' => $user_password, 'password_message' => $message); 
  110. endif; 
  111.  
  112. if ( !function_exists('wp_install_defaults') ) : 
  113. /** 
  114. * Creates the initial content for a newly-installed site. 
  115. * 
  116. * Adds the default "Uncategorized" category, the first post (with comment),  
  117. * first page, and default widgets for default theme for the current version. 
  118. * 
  119. * @since 2.1.0 
  120. * 
  121. * @global wpdb $wpdb 
  122. * @global WP_Rewrite $wp_rewrite 
  123. * @global string $table_prefix 
  124. * 
  125. * @param int $user_id User ID. 
  126. */ 
  127. function wp_install_defaults( $user_id ) { 
  128. global $wpdb, $wp_rewrite, $table_prefix; 
  129.  
  130. // Default category 
  131. $cat_name = __('Uncategorized'); 
  132. /** translators: Default category slug */ 
  133. $cat_slug = sanitize_title(_x('Uncategorized', 'Default category slug')); 
  134.  
  135. if ( global_terms_enabled() ) { 
  136. $cat_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM {$wpdb->sitecategories} WHERE category_nicename = %s", $cat_slug ) ); 
  137. if ( $cat_id == null ) { 
  138. $wpdb->insert( $wpdb->sitecategories, array('cat_ID' => 0, 'cat_name' => $cat_name, 'category_nicename' => $cat_slug, 'last_updated' => current_time('mysql', true)) ); 
  139. $cat_id = $wpdb->insert_id; 
  140. update_option('default_category', $cat_id); 
  141. } else { 
  142. $cat_id = 1; 
  143.  
  144. $wpdb->insert( $wpdb->terms, array('term_id' => $cat_id, 'name' => $cat_name, 'slug' => $cat_slug, 'term_group' => 0) ); 
  145. $wpdb->insert( $wpdb->term_taxonomy, array('term_id' => $cat_id, 'taxonomy' => 'category', 'description' => '', 'parent' => 0, 'count' => 1)); 
  146. $cat_tt_id = $wpdb->insert_id; 
  147.  
  148. // First post 
  149. $now = current_time( 'mysql' ); 
  150. $now_gmt = current_time( 'mysql', 1 ); 
  151. $first_post_guid = get_option( 'home' ) . '/?p=1'; 
  152.  
  153. if ( is_multisite() ) { 
  154. $first_post = get_site_option( 'first_post' ); 
  155.  
  156. if ( ! $first_post ) { 
  157. /** translators: %s: site link */ 
  158. $first_post = __( 'Welcome to %s. This is your first post. Edit or delete it, then start blogging!' ); 
  159.  
  160. $first_post = sprintf( $first_post,  
  161. sprintf( '<a href="%s">%s</a>', esc_url( network_home_url() ), get_network()->site_name ) 
  162. ); 
  163.  
  164. // Back-compat for pre-4.4 
  165. $first_post = str_replace( 'SITE_URL', esc_url( network_home_url() ), $first_post ); 
  166. $first_post = str_replace( 'SITE_NAME', get_network()->site_name, $first_post ); 
  167. } else { 
  168. $first_post = __( 'Welcome to WordPress. This is your first post. Edit or delete it, then start writing!' ); 
  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.  
  189. // Default comment 
  190. $first_comment_author = __( 'A WordPress Commenter' ); 
  191. $first_comment_email = 'wapuu@wordpress.example'; 
  192. $first_comment_url = 'https://wordpress.org/'; 
  193. $first_comment = __( 'Hi, this is a comment. 
  194. To get started with moderating, editing, and deleting comments, please visit the Comments screen in the dashboard. 
  195. Commenter avatars come from <a href="https://gravatar.com">Gravatar</a>.' ); 
  196. if ( is_multisite() ) { 
  197. $first_comment_author = get_site_option( 'first_comment_author', $first_comment_author ); 
  198. $first_comment_email = get_site_option( 'first_comment_email', $first_comment_email ); 
  199. $first_comment_url = get_site_option( 'first_comment_url', network_home_url() ); 
  200. $first_comment = get_site_option( 'first_comment', $first_comment ); 
  201. $wpdb->insert( $wpdb->comments, array( 
  202. 'comment_post_ID' => 1,  
  203. 'comment_author' => $first_comment_author,  
  204. 'comment_author_email' => $first_comment_email,  
  205. 'comment_author_url' => $first_comment_url,  
  206. 'comment_date' => $now,  
  207. 'comment_date_gmt' => $now_gmt,  
  208. 'comment_content' => $first_comment 
  209. )); 
  210.  
  211. // First Page 
  212. $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: 
  213.  
  214. <blockquote>Hi there! I'm a bike messenger by day, aspiring actor by night, and this is my website. I live in Los Angeles, have a great dog named Jack, and I like piña coladas. (And gettin' caught in the rain.)</blockquote> 
  215.  
  216. ...or something like this: 
  217.  
  218. <blockquote>The XYZ Doohickey Company was founded in 1971, and has been providing quality doohickeys to the public ever since. Located in Gotham City, XYZ employs over 2, 000 people and does all kinds of awesome things for the Gotham community.</blockquote> 
  219.  
  220. 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() ); 
  221. if ( is_multisite() ) 
  222. $first_page = get_site_option( 'first_page', $first_page ); 
  223. $first_post_guid = get_option('home') . '/?page_id=2'; 
  224. $wpdb->insert( $wpdb->posts, array( 
  225. 'post_author' => $user_id,  
  226. 'post_date' => $now,  
  227. 'post_date_gmt' => $now_gmt,  
  228. 'post_content' => $first_page,  
  229. 'post_excerpt' => '',  
  230. 'comment_status' => 'closed',  
  231. 'post_title' => __( 'Sample Page' ),  
  232. /** translators: Default page slug */ 
  233. 'post_name' => __( 'sample-page' ),  
  234. 'post_modified' => $now,  
  235. 'post_modified_gmt' => $now_gmt,  
  236. 'guid' => $first_post_guid,  
  237. 'post_type' => 'page',  
  238. 'to_ping' => '',  
  239. 'pinged' => '',  
  240. 'post_content_filtered' => '' 
  241. )); 
  242. $wpdb->insert( $wpdb->postmeta, array( 'post_id' => 2, 'meta_key' => '_wp_page_template', 'meta_value' => 'default' ) ); 
  243.  
  244. // Set up default widgets for default theme. 
  245. update_option( 'widget_search', array ( 2 => array ( 'title' => '' ), '_multiwidget' => 1 ) ); 
  246. update_option( 'widget_recent-posts', array ( 2 => array ( 'title' => '', 'number' => 5 ), '_multiwidget' => 1 ) ); 
  247. update_option( 'widget_recent-comments', array ( 2 => array ( 'title' => '', 'number' => 5 ), '_multiwidget' => 1 ) ); 
  248. update_option( 'widget_archives', array ( 2 => array ( 'title' => '', 'count' => 0, 'dropdown' => 0 ), '_multiwidget' => 1 ) ); 
  249. update_option( 'widget_categories', array ( 2 => array ( 'title' => '', 'count' => 0, 'hierarchical' => 0, 'dropdown' => 0 ), '_multiwidget' => 1 ) ); 
  250. update_option( 'widget_meta', array ( 2 => array ( 'title' => '' ), '_multiwidget' => 1 ) ); 
  251. update_option( 'sidebars_widgets', array( 'wp_inactive_widgets' => array(), 'sidebar-1' => array( 0 => 'search-2', 1 => 'recent-posts-2', 2 => 'recent-comments-2', 3 => 'archives-2', 4 => 'categories-2', 5 => 'meta-2' ), 'sidebar-2' => array(), 'sidebar-3' => array(), 'array_version' => 3 ) ); 
  252. if ( ! is_multisite() ) 
  253. update_user_meta( $user_id, 'show_welcome_panel', 1 ); 
  254. elseif ( ! is_super_admin( $user_id ) && ! metadata_exists( 'user', $user_id, 'show_welcome_panel' ) ) 
  255. update_user_meta( $user_id, 'show_welcome_panel', 2 ); 
  256.  
  257. if ( is_multisite() ) { 
  258. // Flush rules to pick up the new page. 
  259. $wp_rewrite->init(); 
  260. $wp_rewrite->flush_rules(); 
  261.  
  262. $user = new WP_User($user_id); 
  263. $wpdb->update( $wpdb->options, array('option_value' => $user->user_email), array('option_name' => 'admin_email') ); 
  264.  
  265. // Remove all perms except for the login user. 
  266. $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE user_id != %d AND meta_key = %s", $user_id, $table_prefix.'user_level') ); 
  267. $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE user_id != %d AND meta_key = %s", $user_id, $table_prefix.'capabilities') ); 
  268.  
  269. // Delete any caps that snuck into the previously active blog. (Hardcoded to blog 1 for now.) TODO: Get previous_blog_id. 
  270. if ( !is_super_admin( $user_id ) && $user_id != 1 ) 
  271. $wpdb->delete( $wpdb->usermeta, array( 'user_id' => $user_id , 'meta_key' => $wpdb->base_prefix.'1_capabilities' ) ); 
  272. endif; 
  273.  
  274. /** 
  275. * Maybe enable pretty permalinks on install. 
  276. * 
  277. * If after enabling pretty permalinks don't work, fallback to query-string permalinks. 
  278. * 
  279. * @since 4.2.0 
  280. * 
  281. * @global WP_Rewrite $wp_rewrite WordPress rewrite component. 
  282. * 
  283. * @return bool Whether pretty permalinks are enabled. False otherwise. 
  284. */ 
  285. function wp_install_maybe_enable_pretty_permalinks() { 
  286. global $wp_rewrite; 
  287.  
  288. // Bail if a permalink structure is already enabled. 
  289. if ( get_option( 'permalink_structure' ) ) { 
  290. return true; 
  291.  
  292. /** 
  293. * The Permalink structures to attempt. 
  294. * 
  295. * The first is designed for mod_rewrite or nginx rewriting. 
  296. * 
  297. * The second is PATHINFO-based permalinks for web server configurations 
  298. * without a true rewrite module enabled. 
  299. */ 
  300. $permalink_structures = array( 
  301. '/%year%/%monthnum%/%day%/%postname%/',  
  302. '/index.php/%year%/%monthnum%/%day%/%postname%/' 
  303. ); 
  304.  
  305. foreach ( (array) $permalink_structures as $permalink_structure ) { 
  306. $wp_rewrite->set_permalink_structure( $permalink_structure ); 
  307.  
  308. /** 
  309. * Flush rules with the hard option to force refresh of the web-server's 
  310. * rewrite config file (e.g. .htaccess or web.config). 
  311. */ 
  312. $wp_rewrite->flush_rules( true ); 
  313.  
  314. $test_url = ''; 
  315.  
  316. // Test against a real WordPress Post 
  317. $first_post = get_page_by_path( sanitize_title( _x( 'hello-world', 'Default post slug' ) ), OBJECT, 'post' ); 
  318. if ( $first_post ) { 
  319. $test_url = get_permalink( $first_post->ID ); 
  320.  
  321. /** 
  322. * Send a request to the site, and check whether 
  323. * the 'x-pingback' header is returned as expected. 
  324. * 
  325. * Uses wp_remote_get() instead of wp_remote_head() because web servers 
  326. * can block head requests. 
  327. */ 
  328. $response = wp_remote_get( $test_url, array( 'timeout' => 5 ) ); 
  329. $x_pingback_header = wp_remote_retrieve_header( $response, 'x-pingback' ); 
  330. $pretty_permalinks = $x_pingback_header && $x_pingback_header === get_bloginfo( 'pingback_url' ); 
  331.  
  332. if ( $pretty_permalinks ) { 
  333. return true; 
  334.  
  335. /** 
  336. * If it makes it this far, pretty permalinks failed. 
  337. * Fallback to query-string permalinks. 
  338. */ 
  339. $wp_rewrite->set_permalink_structure( '' ); 
  340. $wp_rewrite->flush_rules( true ); 
  341.  
  342. return false; 
  343.  
  344. if ( !function_exists('wp_new_blog_notification') ) : 
  345. /** 
  346. * Notifies the site admin that the setup is complete. 
  347. * 
  348. * Sends an email with wp_mail to the new administrator that the site setup is complete,  
  349. * and provides them with a record of their login credentials. 
  350. * 
  351. * @since 2.1.0 
  352. * 
  353. * @param string $blog_title Site title. 
  354. * @param string $blog_url Site url. 
  355. * @param int $user_id User ID. 
  356. * @param string $password User's Password. 
  357. */ 
  358. function wp_new_blog_notification($blog_title, $blog_url, $user_id, $password) { 
  359. $user = new WP_User( $user_id ); 
  360. $email = $user->user_email; 
  361. $name = $user->user_login; 
  362. $login_url = wp_login_url(); 
  363. /** translators: New site notification email. 1: New site URL, 2: User login, 3: User password or password reset link, 4: Login URL */ 
  364. $message = sprintf( __( "Your new WordPress site has been successfully set up at: 
  365.  
  366. %1\$s 
  367.  
  368. You can log in to the administrator account with the following information: 
  369.  
  370. Username: %2\$s 
  371. Password: %3\$s 
  372. Log in here: %4\$s 
  373.  
  374. We hope you enjoy your new site. Thanks! 
  375.  
  376. --The WordPress Team 
  377. https://wordpress.org/ 
  378. "), $blog_url, $name, $password, $login_url ); 
  379.  
  380. @wp_mail($email, __('New WordPress Site'), $message); 
  381. endif; 
  382.  
  383. if ( !function_exists('wp_upgrade') ) : 
  384. /** 
  385. * Runs WordPress Upgrade functions. 
  386. * 
  387. * Upgrades the database if needed during a site update. 
  388. * 
  389. * @since 2.1.0 
  390. * 
  391. * @global int $wp_current_db_version 
  392. * @global int $wp_db_version 
  393. * @global wpdb $wpdb WordPress database abstraction object. 
  394. */ 
  395. function wp_upgrade() { 
  396. global $wp_current_db_version, $wp_db_version, $wpdb; 
  397.  
  398. $wp_current_db_version = __get_option('db_version'); 
  399.  
  400. // We are up-to-date. Nothing to do. 
  401. if ( $wp_db_version == $wp_current_db_version ) 
  402. return; 
  403.  
  404. if ( ! is_blog_installed() ) 
  405. return; 
  406.  
  407. wp_check_mysql_version(); 
  408. wp_cache_flush(); 
  409. pre_schema_upgrade(); 
  410. make_db_current_silent(); 
  411. upgrade_all(); 
  412. if ( is_multisite() && is_main_site() ) 
  413. upgrade_network(); 
  414. wp_cache_flush(); 
  415.  
  416. if ( is_multisite() ) { 
  417. if ( $wpdb->get_row( "SELECT blog_id FROM {$wpdb->blog_versions} WHERE blog_id = '{$wpdb->blogid}'" ) ) 
  418. $wpdb->query( "UPDATE {$wpdb->blog_versions} SET db_version = '{$wp_db_version}' WHERE blog_id = '{$wpdb->blogid}'" ); 
  419. else 
  420. $wpdb->query( "INSERT INTO {$wpdb->blog_versions} ( `blog_id` , `db_version` , `last_updated` ) VALUES ( '{$wpdb->blogid}', '{$wp_db_version}', NOW());" ); 
  421.  
  422. /** 
  423. * Fires after a site is fully upgraded. 
  424. * 
  425. * @since 3.9.0 
  426. * 
  427. * @param int $wp_db_version The new $wp_db_version. 
  428. * @param int $wp_current_db_version The old (current) $wp_db_version. 
  429. */ 
  430. do_action( 'wp_upgrade', $wp_db_version, $wp_current_db_version ); 
  431. endif; 
  432.  
  433. /** 
  434. * Functions to be called in install and upgrade scripts. 
  435. * 
  436. * Contains conditional checks to determine which upgrade scripts to run,  
  437. * based on database version and WP version being updated-to. 
  438. * 
  439. * @ignore 
  440. * @since 1.0.1 
  441. * 
  442. * @global int $wp_current_db_version 
  443. * @global int $wp_db_version 
  444. */ 
  445. function upgrade_all() { 
  446. global $wp_current_db_version, $wp_db_version; 
  447. $wp_current_db_version = __get_option('db_version'); 
  448.  
  449. // We are up-to-date. Nothing to do. 
  450. if ( $wp_db_version == $wp_current_db_version ) 
  451. return; 
  452.  
  453. // If the version is not set in the DB, try to guess the version. 
  454. if ( empty($wp_current_db_version) ) { 
  455. $wp_current_db_version = 0; 
  456.  
  457. // If the template option exists, we have 1.5. 
  458. $template = __get_option('template'); 
  459. if ( !empty($template) ) 
  460. $wp_current_db_version = 2541; 
  461.  
  462. if ( $wp_current_db_version < 6039 ) 
  463. upgrade_230_options_table(); 
  464.  
  465. populate_options(); 
  466.  
  467. if ( $wp_current_db_version < 2541 ) { 
  468. upgrade_100(); 
  469. upgrade_101(); 
  470. upgrade_110(); 
  471. upgrade_130(); 
  472.  
  473. if ( $wp_current_db_version < 3308 ) 
  474. upgrade_160(); 
  475.  
  476. if ( $wp_current_db_version < 4772 ) 
  477. upgrade_210(); 
  478.  
  479. if ( $wp_current_db_version < 4351 ) 
  480. upgrade_old_slugs(); 
  481.  
  482. if ( $wp_current_db_version < 5539 ) 
  483. upgrade_230(); 
  484.  
  485. if ( $wp_current_db_version < 6124 ) 
  486. upgrade_230_old_tables(); 
  487.  
  488. if ( $wp_current_db_version < 7499 ) 
  489. upgrade_250(); 
  490.  
  491. if ( $wp_current_db_version < 7935 ) 
  492. upgrade_252(); 
  493.  
  494. if ( $wp_current_db_version < 8201 ) 
  495. upgrade_260(); 
  496.  
  497. if ( $wp_current_db_version < 8989 ) 
  498. upgrade_270(); 
  499.  
  500. if ( $wp_current_db_version < 10360 ) 
  501. upgrade_280(); 
  502.  
  503. if ( $wp_current_db_version < 11958 ) 
  504. upgrade_290(); 
  505.  
  506. if ( $wp_current_db_version < 15260 ) 
  507. upgrade_300(); 
  508.  
  509. if ( $wp_current_db_version < 19389 ) 
  510. upgrade_330(); 
  511.  
  512. if ( $wp_current_db_version < 20080 ) 
  513. upgrade_340(); 
  514.  
  515. if ( $wp_current_db_version < 22422 ) 
  516. upgrade_350(); 
  517.  
  518. if ( $wp_current_db_version < 25824 ) 
  519. upgrade_370(); 
  520.  
  521. if ( $wp_current_db_version < 26148 ) 
  522. upgrade_372(); 
  523.  
  524. if ( $wp_current_db_version < 26691 ) 
  525. upgrade_380(); 
  526.  
  527. if ( $wp_current_db_version < 29630 ) 
  528. upgrade_400(); 
  529.  
  530. if ( $wp_current_db_version < 33055 ) 
  531. upgrade_430(); 
  532.  
  533. if ( $wp_current_db_version < 33056 ) 
  534. upgrade_431(); 
  535.  
  536. if ( $wp_current_db_version < 35700 ) 
  537. upgrade_440(); 
  538.  
  539. if ( $wp_current_db_version < 36686 ) 
  540. upgrade_450(); 
  541.  
  542. if ( $wp_current_db_version < 37965 ) 
  543. upgrade_460(); 
  544.  
  545. maybe_disable_link_manager(); 
  546.  
  547. maybe_disable_automattic_widgets(); 
  548.  
  549. update_option( 'db_version', $wp_db_version ); 
  550. update_option( 'db_upgraded', true ); 
  551.  
  552. /** 
  553. * Execute changes made in WordPress 1.0. 
  554. * 
  555. * @ignore 
  556. * @since 1.0.0 
  557. * 
  558. * @global wpdb $wpdb WordPress database abstraction object. 
  559. */ 
  560. function upgrade_100() { 
  561. global $wpdb; 
  562.  
  563. // Get the title and ID of every post, post_name to check if it already has a value 
  564. $posts = $wpdb->get_results("SELECT ID, post_title, post_name FROM $wpdb->posts WHERE post_name = ''"); 
  565. if ($posts) { 
  566. foreach ($posts as $post) { 
  567. if ('' == $post->post_name) { 
  568. $newtitle = sanitize_title($post->post_title); 
  569. $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_name = %s WHERE ID = %d", $newtitle, $post->ID) ); 
  570.  
  571. $categories = $wpdb->get_results("SELECT cat_ID, cat_name, category_nicename FROM $wpdb->categories"); 
  572. foreach ($categories as $category) { 
  573. if ('' == $category->category_nicename) { 
  574. $newtitle = sanitize_title($category->cat_name); 
  575. $wpdb->update( $wpdb->categories, array('category_nicename' => $newtitle), array('cat_ID' => $category->cat_ID) ); 
  576.  
  577. $sql = "UPDATE $wpdb->options 
  578. SET option_value = REPLACE(option_value, 'wp-links/links-images/', 'wp-images/links/') 
  579. WHERE option_name LIKE %s 
  580. AND option_value LIKE %s"; 
  581. $wpdb->query( $wpdb->prepare( $sql, $wpdb->esc_like( 'links_rating_image' ) . '%', $wpdb->esc_like( 'wp-links/links-images/' ) . '%' ) ); 
  582.  
  583. $done_ids = $wpdb->get_results("SELECT DISTINCT post_id FROM $wpdb->post2cat"); 
  584. if ($done_ids) : 
  585. $done_posts = array(); 
  586. foreach ($done_ids as $done_id) : 
  587. $done_posts[] = $done_id->post_id; 
  588. endforeach; 
  589. $catwhere = ' AND ID NOT IN (' . implode(', ', $done_posts) . ')'; 
  590. else: 
  591. $catwhere = ''; 
  592. endif; 
  593.  
  594. $allposts = $wpdb->get_results("SELECT ID, post_category FROM $wpdb->posts WHERE post_category != '0' $catwhere"); 
  595. if ($allposts) : 
  596. foreach ($allposts as $post) { 
  597. // Check to see if it's already been imported 
  598. $cat = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->post2cat WHERE post_id = %d AND category_id = %d", $post->ID, $post->post_category) ); 
  599. if (!$cat && 0 != $post->post_category) { // If there's no result 
  600. $wpdb->insert( $wpdb->post2cat, array('post_id' => $post->ID, 'category_id' => $post->post_category) ); 
  601. endif; 
  602.  
  603. /** 
  604. * Execute changes made in WordPress 1.0.1. 
  605. * 
  606. * @ignore 
  607. * @since 1.0.1 
  608. * 
  609. * @global wpdb $wpdb WordPress database abstraction object. 
  610. */ 
  611. function upgrade_101() { 
  612. global $wpdb; 
  613.  
  614. // Clean up indices, add a few 
  615. add_clean_index($wpdb->posts, 'post_name'); 
  616. add_clean_index($wpdb->posts, 'post_status'); 
  617. add_clean_index($wpdb->categories, 'category_nicename'); 
  618. add_clean_index($wpdb->comments, 'comment_approved'); 
  619. add_clean_index($wpdb->comments, 'comment_post_ID'); 
  620. add_clean_index($wpdb->links , 'link_category'); 
  621. add_clean_index($wpdb->links , 'link_visible'); 
  622.  
  623. /** 
  624. * Execute changes made in WordPress 1.2. 
  625. * 
  626. * @ignore 
  627. * @since 1.2.0 
  628. * 
  629. * @global wpdb $wpdb WordPress database abstraction object. 
  630. */ 
  631. function upgrade_110() { 
  632. global $wpdb; 
  633.  
  634. // Set user_nicename. 
  635. $users = $wpdb->get_results("SELECT ID, user_nickname, user_nicename FROM $wpdb->users"); 
  636. foreach ($users as $user) { 
  637. if ('' == $user->user_nicename) { 
  638. $newname = sanitize_title($user->user_nickname); 
  639. $wpdb->update( $wpdb->users, array('user_nicename' => $newname), array('ID' => $user->ID) ); 
  640.  
  641. $users = $wpdb->get_results("SELECT ID, user_pass from $wpdb->users"); 
  642. foreach ($users as $row) { 
  643. if (!preg_match('/^[A-Fa-f0-9]{32}$/', $row->user_pass)) { 
  644. $wpdb->update( $wpdb->users, array('user_pass' => md5($row->user_pass)), array('ID' => $row->ID) ); 
  645.  
  646. // Get the GMT offset, we'll use that later on 
  647. $all_options = get_alloptions_110(); 
  648.  
  649. $time_difference = $all_options->time_difference; 
  650.  
  651. $server_time = time()+date('Z'); 
  652. $weblogger_time = $server_time + $time_difference * HOUR_IN_SECONDS; 
  653. $gmt_time = time(); 
  654.  
  655. $diff_gmt_server = ($gmt_time - $server_time) / HOUR_IN_SECONDS; 
  656. $diff_weblogger_server = ($weblogger_time - $server_time) / HOUR_IN_SECONDS; 
  657. $diff_gmt_weblogger = $diff_gmt_server - $diff_weblogger_server; 
  658. $gmt_offset = -$diff_gmt_weblogger; 
  659.  
  660. // Add a gmt_offset option, with value $gmt_offset 
  661. add_option('gmt_offset', $gmt_offset); 
  662.  
  663. // Check if we already set the GMT fields (if we did, then 
  664. // MAX(post_date_gmt) can't be '0000-00-00 00:00:00' 
  665. // <michel_v> I just slapped myself silly for not thinking about it earlier 
  666. $got_gmt_fields = ! ($wpdb->get_var("SELECT MAX(post_date_gmt) FROM $wpdb->posts") == '0000-00-00 00:00:00'); 
  667.  
  668. if (!$got_gmt_fields) { 
  669.  
  670. // Add or subtract time to all dates, to get GMT dates 
  671. $add_hours = intval($diff_gmt_weblogger); 
  672. $add_minutes = intval(60 * ($diff_gmt_weblogger - $add_hours)); 
  673. $wpdb->query("UPDATE $wpdb->posts SET post_date_gmt = DATE_ADD(post_date, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)"); 
  674. $wpdb->query("UPDATE $wpdb->posts SET post_modified = post_date"); 
  675. $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'"); 
  676. $wpdb->query("UPDATE $wpdb->comments SET comment_date_gmt = DATE_ADD(comment_date, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)"); 
  677. $wpdb->query("UPDATE $wpdb->users SET user_registered = DATE_ADD(user_registered, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)"); 
  678.  
  679.  
  680. /** 
  681. * Execute changes made in WordPress 1.5. 
  682. * 
  683. * @ignore 
  684. * @since 1.5.0 
  685. * 
  686. * @global wpdb $wpdb WordPress database abstraction object. 
  687. */ 
  688. function upgrade_130() { 
  689. global $wpdb; 
  690.  
  691. // Remove extraneous backslashes. 
  692. $posts = $wpdb->get_results("SELECT ID, post_title, post_content, post_excerpt, guid, post_date, post_name, post_status, post_author FROM $wpdb->posts"); 
  693. if ($posts) { 
  694. foreach ($posts as $post) { 
  695. $post_content = addslashes(deslash($post->post_content)); 
  696. $post_title = addslashes(deslash($post->post_title)); 
  697. $post_excerpt = addslashes(deslash($post->post_excerpt)); 
  698. if ( empty($post->guid) ) 
  699. $guid = get_permalink($post->ID); 
  700. else 
  701. $guid = $post->guid; 
  702.  
  703. $wpdb->update( $wpdb->posts, compact('post_title', 'post_content', 'post_excerpt', 'guid'), array('ID' => $post->ID) ); 
  704.  
  705.  
  706. // Remove extraneous backslashes. 
  707. $comments = $wpdb->get_results("SELECT comment_ID, comment_author, comment_content FROM $wpdb->comments"); 
  708. if ($comments) { 
  709. foreach ($comments as $comment) { 
  710. $comment_content = deslash($comment->comment_content); 
  711. $comment_author = deslash($comment->comment_author); 
  712.  
  713. $wpdb->update($wpdb->comments, compact('comment_content', 'comment_author'), array('comment_ID' => $comment->comment_ID) ); 
  714.  
  715. // Remove extraneous backslashes. 
  716. $links = $wpdb->get_results("SELECT link_id, link_name, link_description FROM $wpdb->links"); 
  717. if ($links) { 
  718. foreach ($links as $link) { 
  719. $link_name = deslash($link->link_name); 
  720. $link_description = deslash($link->link_description); 
  721.  
  722. $wpdb->update( $wpdb->links, compact('link_name', 'link_description'), array('link_id' => $link->link_id) ); 
  723.  
  724. $active_plugins = __get_option('active_plugins'); 
  725.  
  726. /** 
  727. * If plugins are not stored in an array, they're stored in the old 
  728. * newline separated format. Convert to new format. 
  729. */ 
  730. if ( !is_array( $active_plugins ) ) { 
  731. $active_plugins = explode("\n", trim($active_plugins)); 
  732. update_option('active_plugins', $active_plugins); 
  733.  
  734. // Obsolete tables 
  735. $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optionvalues'); 
  736. $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optiontypes'); 
  737. $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optiongroups'); 
  738. $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optiongroup_options'); 
  739.  
  740. // Update comments table to use comment_type 
  741. $wpdb->query("UPDATE $wpdb->comments SET comment_type='trackback', comment_content = REPLACE(comment_content, '<trackback />', '') WHERE comment_content LIKE '<trackback />%'"); 
  742. $wpdb->query("UPDATE $wpdb->comments SET comment_type='pingback', comment_content = REPLACE(comment_content, '<pingback />', '') WHERE comment_content LIKE '<pingback />%'"); 
  743.  
  744. // Some versions have multiple duplicate option_name rows with the same values 
  745. $options = $wpdb->get_results("SELECT option_name, COUNT(option_name) AS dupes FROM `$wpdb->options` GROUP BY option_name"); 
  746. foreach ( $options as $option ) { 
  747. if ( 1 != $option->dupes ) { // Could this be done in the query? 
  748. $limit = $option->dupes - 1; 
  749. $dupe_ids = $wpdb->get_col( $wpdb->prepare("SELECT option_id FROM $wpdb->options WHERE option_name = %s LIMIT %d", $option->option_name, $limit) ); 
  750. if ( $dupe_ids ) { 
  751. $dupe_ids = join($dupe_ids, ', '); 
  752. $wpdb->query("DELETE FROM $wpdb->options WHERE option_id IN ($dupe_ids)"); 
  753.  
  754. make_site_theme(); 
  755.  
  756. /** 
  757. * Execute changes made in WordPress 2.0. 
  758. * 
  759. * @ignore 
  760. * @since 2.0.0 
  761. * 
  762. * @global wpdb $wpdb WordPress database abstraction object. 
  763. * @global int $wp_current_db_version 
  764. */ 
  765. function upgrade_160() { 
  766. global $wpdb, $wp_current_db_version; 
  767.  
  768. populate_roles_160(); 
  769.  
  770. $users = $wpdb->get_results("SELECT * FROM $wpdb->users"); 
  771. foreach ( $users as $user ) : 
  772. if ( !empty( $user->user_firstname ) ) 
  773. update_user_meta( $user->ID, 'first_name', wp_slash($user->user_firstname) ); 
  774. if ( !empty( $user->user_lastname ) ) 
  775. update_user_meta( $user->ID, 'last_name', wp_slash($user->user_lastname) ); 
  776. if ( !empty( $user->user_nickname ) ) 
  777. update_user_meta( $user->ID, 'nickname', wp_slash($user->user_nickname) ); 
  778. if ( !empty( $user->user_level ) ) 
  779. update_user_meta( $user->ID, $wpdb->prefix . 'user_level', $user->user_level ); 
  780. if ( !empty( $user->user_icq ) ) 
  781. update_user_meta( $user->ID, 'icq', wp_slash($user->user_icq) ); 
  782. if ( !empty( $user->user_aim ) ) 
  783. update_user_meta( $user->ID, 'aim', wp_slash($user->user_aim) ); 
  784. if ( !empty( $user->user_msn ) ) 
  785. update_user_meta( $user->ID, 'msn', wp_slash($user->user_msn) ); 
  786. if ( !empty( $user->user_yim ) ) 
  787. update_user_meta( $user->ID, 'yim', wp_slash($user->user_icq) ); 
  788. if ( !empty( $user->user_description ) ) 
  789. update_user_meta( $user->ID, 'description', wp_slash($user->user_description) ); 
  790.  
  791. if ( isset( $user->user_idmode ) ): 
  792. $idmode = $user->user_idmode; 
  793. if ($idmode == 'nickname') $id = $user->user_nickname; 
  794. if ($idmode == 'login') $id = $user->user_login; 
  795. if ($idmode == 'firstname') $id = $user->user_firstname; 
  796. if ($idmode == 'lastname') $id = $user->user_lastname; 
  797. if ($idmode == 'namefl') $id = $user->user_firstname.' '.$user->user_lastname; 
  798. if ($idmode == 'namelf') $id = $user->user_lastname.' '.$user->user_firstname; 
  799. if (!$idmode) $id = $user->user_nickname; 
  800. $wpdb->update( $wpdb->users, array('display_name' => $id), array('ID' => $user->ID) ); 
  801. endif; 
  802.  
  803. // FIXME: RESET_CAPS is temporary code to reset roles and caps if flag is set. 
  804. $caps = get_user_meta( $user->ID, $wpdb->prefix . 'capabilities'); 
  805. if ( empty($caps) || defined('RESET_CAPS') ) { 
  806. $level = get_user_meta($user->ID, $wpdb->prefix . 'user_level', true); 
  807. $role = translate_level_to_role($level); 
  808. update_user_meta( $user->ID, $wpdb->prefix . 'capabilities', array($role => true) ); 
  809.  
  810. endforeach; 
  811. $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' ); 
  812. $wpdb->hide_errors(); 
  813. foreach ( $old_user_fields as $old ) 
  814. $wpdb->query("ALTER TABLE $wpdb->users DROP $old"); 
  815. $wpdb->show_errors(); 
  816.  
  817. // Populate comment_count field of posts table. 
  818. $comments = $wpdb->get_results( "SELECT comment_post_ID, COUNT(*) as c FROM $wpdb->comments WHERE comment_approved = '1' GROUP BY comment_post_ID" ); 
  819. if ( is_array( $comments ) ) 
  820. foreach ($comments as $comment) 
  821. $wpdb->update( $wpdb->posts, array('comment_count' => $comment->c), array('ID' => $comment->comment_post_ID) ); 
  822.  
  823. /** 
  824. * Some alpha versions used a post status of object instead of attachment 
  825. * and put the mime type in post_type instead of post_mime_type. 
  826. */ 
  827. if ( $wp_current_db_version > 2541 && $wp_current_db_version <= 3091 ) { 
  828. $objects = $wpdb->get_results("SELECT ID, post_type FROM $wpdb->posts WHERE post_status = 'object'"); 
  829. foreach ($objects as $object) { 
  830. $wpdb->update( $wpdb->posts, array( 'post_status' => 'attachment',  
  831. 'post_mime_type' => $object->post_type,  
  832. 'post_type' => ''),  
  833. array( 'ID' => $object->ID ) ); 
  834.  
  835. $meta = get_post_meta($object->ID, 'imagedata', true); 
  836. if ( ! empty($meta['file']) ) 
  837. update_attached_file( $object->ID, $meta['file'] ); 
  838.  
  839. /** 
  840. * Execute changes made in WordPress 2.1. 
  841. * 
  842. * @ignore 
  843. * @since 2.1.0 
  844. * 
  845. * @global wpdb $wpdb WordPress database abstraction object. 
  846. * @global int $wp_current_db_version 
  847. */ 
  848. function upgrade_210() { 
  849. global $wpdb, $wp_current_db_version; 
  850.  
  851. if ( $wp_current_db_version < 3506 ) { 
  852. // Update status and type. 
  853. $posts = $wpdb->get_results("SELECT ID, post_status FROM $wpdb->posts"); 
  854.  
  855. if ( ! empty($posts) ) foreach ($posts as $post) { 
  856. $status = $post->post_status; 
  857. $type = 'post'; 
  858.  
  859. if ( 'static' == $status ) { 
  860. $status = 'publish'; 
  861. $type = 'page'; 
  862. } elseif ( 'attachment' == $status ) { 
  863. $status = 'inherit'; 
  864. $type = 'attachment'; 
  865.  
  866. $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_status = %s, post_type = %s WHERE ID = %d", $status, $type, $post->ID) ); 
  867.  
  868. if ( $wp_current_db_version < 3845 ) { 
  869. populate_roles_210(); 
  870.  
  871. if ( $wp_current_db_version < 3531 ) { 
  872. // Give future posts a post_status of future. 
  873. $now = gmdate('Y-m-d H:i:59'); 
  874. $wpdb->query ("UPDATE $wpdb->posts SET post_status = 'future' WHERE post_status = 'publish' AND post_date_gmt > '$now'"); 
  875.  
  876. $posts = $wpdb->get_results("SELECT ID, post_date FROM $wpdb->posts WHERE post_status ='future'"); 
  877. if ( !empty($posts) ) 
  878. foreach ( $posts as $post ) 
  879. wp_schedule_single_event(mysql2date('U', $post->post_date, false), 'publish_future_post', array($post->ID)); 
  880.  
  881. /** 
  882. * Execute changes made in WordPress 2.3. 
  883. * 
  884. * @ignore 
  885. * @since 2.3.0 
  886. * 
  887. * @global wpdb $wpdb WordPress database abstraction object. 
  888. * @global int $wp_current_db_version 
  889. */ 
  890. function upgrade_230() { 
  891. global $wp_current_db_version, $wpdb; 
  892.  
  893. if ( $wp_current_db_version < 5200 ) { 
  894. populate_roles_230(); 
  895.  
  896. // Convert categories to terms. 
  897. $tt_ids = array(); 
  898. $have_tags = false; 
  899. $categories = $wpdb->get_results("SELECT * FROM $wpdb->categories ORDER BY cat_ID"); 
  900. foreach ($categories as $category) { 
  901. $term_id = (int) $category->cat_ID; 
  902. $name = $category->cat_name; 
  903. $description = $category->category_description; 
  904. $slug = $category->category_nicename; 
  905. $parent = $category->category_parent; 
  906. $term_group = 0; 
  907.  
  908. // Associate terms with the same slug in a term group and make slugs unique. 
  909. if ( $exists = $wpdb->get_results( $wpdb->prepare("SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $slug) ) ) { 
  910. $term_group = $exists[0]->term_group; 
  911. $id = $exists[0]->term_id; 
  912. $num = 2; 
  913. do { 
  914. $alt_slug = $slug . "-$num"; 
  915. $num++; 
  916. $slug_check = $wpdb->get_var( $wpdb->prepare("SELECT slug FROM $wpdb->terms WHERE slug = %s", $alt_slug) ); 
  917. } while ( $slug_check ); 
  918.  
  919. $slug = $alt_slug; 
  920.  
  921. if ( empty( $term_group ) ) { 
  922. $term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms GROUP BY term_group") + 1; 
  923. $wpdb->query( $wpdb->prepare("UPDATE $wpdb->terms SET term_group = %d WHERE term_id = %d", $term_group, $id) ); 
  924.  
  925. $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->terms (term_id, name, slug, term_group) VALUES 
  926. (%d, %s, %s, %d)", $term_id, $name, $slug, $term_group) ); 
  927.  
  928. $count = 0; 
  929. if ( !empty($category->category_count) ) { 
  930. $count = (int) $category->category_count; 
  931. $taxonomy = 'category'; 
  932. $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) ); 
  933. $tt_ids[$term_id][$taxonomy] = (int) $wpdb->insert_id; 
  934.  
  935. if ( !empty($category->link_count) ) { 
  936. $count = (int) $category->link_count; 
  937. $taxonomy = 'link_category'; 
  938. $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) ); 
  939. $tt_ids[$term_id][$taxonomy] = (int) $wpdb->insert_id; 
  940.  
  941. if ( !empty($category->tag_count) ) { 
  942. $have_tags = true; 
  943. $count = (int) $category->tag_count; 
  944. $taxonomy = 'post_tag'; 
  945. $wpdb->insert( $wpdb->term_taxonomy, compact('term_id', 'taxonomy', 'description', 'parent', 'count') ); 
  946. $tt_ids[$term_id][$taxonomy] = (int) $wpdb->insert_id; 
  947.  
  948. if ( empty($count) ) { 
  949. $count = 0; 
  950. $taxonomy = 'category'; 
  951. $wpdb->insert( $wpdb->term_taxonomy, compact('term_id', 'taxonomy', 'description', 'parent', 'count') ); 
  952. $tt_ids[$term_id][$taxonomy] = (int) $wpdb->insert_id; 
  953.  
  954. $select = 'post_id, category_id'; 
  955. if ( $have_tags ) 
  956. $select .= ', rel_type'; 
  957.  
  958. $posts = $wpdb->get_results("SELECT $select FROM $wpdb->post2cat GROUP BY post_id, category_id"); 
  959. foreach ( $posts as $post ) { 
  960. $post_id = (int) $post->post_id; 
  961. $term_id = (int) $post->category_id; 
  962. $taxonomy = 'category'; 
  963. if ( !empty($post->rel_type) && 'tag' == $post->rel_type) 
  964. $taxonomy = 'tag'; 
  965. $tt_id = $tt_ids[$term_id][$taxonomy]; 
  966. if ( empty($tt_id) ) 
  967. continue; 
  968.  
  969. $wpdb->insert( $wpdb->term_relationships, array('object_id' => $post_id, 'term_taxonomy_id' => $tt_id) ); 
  970.  
  971. // < 3570 we used linkcategories. >= 3570 we used categories and link2cat. 
  972. if ( $wp_current_db_version < 3570 ) { 
  973. /** 
  974. * Create link_category terms for link categories. Create a map of link 
  975. * cat IDs to link_category terms. 
  976. */ 
  977. $link_cat_id_map = array(); 
  978. $default_link_cat = 0; 
  979. $tt_ids = array(); 
  980. $link_cats = $wpdb->get_results("SELECT cat_id, cat_name FROM " . $wpdb->prefix . 'linkcategories'); 
  981. foreach ( $link_cats as $category) { 
  982. $cat_id = (int) $category->cat_id; 
  983. $term_id = 0; 
  984. $name = wp_slash($category->cat_name); 
  985. $slug = sanitize_title($name); 
  986. $term_group = 0; 
  987.  
  988. // Associate terms with the same slug in a term group and make slugs unique. 
  989. if ( $exists = $wpdb->get_results( $wpdb->prepare("SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $slug) ) ) { 
  990. $term_group = $exists[0]->term_group; 
  991. $term_id = $exists[0]->term_id; 
  992.  
  993. if ( empty($term_id) ) { 
  994. $wpdb->insert( $wpdb->terms, compact('name', 'slug', 'term_group') ); 
  995. $term_id = (int) $wpdb->insert_id; 
  996.  
  997. $link_cat_id_map[$cat_id] = $term_id; 
  998. $default_link_cat = $term_id; 
  999.  
  1000. $wpdb->insert( $wpdb->term_taxonomy, array('term_id' => $term_id, 'taxonomy' => 'link_category', 'description' => '', 'parent' => 0, 'count' => 0) ); 
  1001. $tt_ids[$term_id] = (int) $wpdb->insert_id; 
  1002.  
  1003. // Associate links to cats. 
  1004. $links = $wpdb->get_results("SELECT link_id, link_category FROM $wpdb->links"); 
  1005. if ( !empty($links) ) foreach ( $links as $link ) { 
  1006. if ( 0 == $link->link_category ) 
  1007. continue; 
  1008. if ( ! isset($link_cat_id_map[$link->link_category]) ) 
  1009. continue; 
  1010. $term_id = $link_cat_id_map[$link->link_category]; 
  1011. $tt_id = $tt_ids[$term_id]; 
  1012. if ( empty($tt_id) ) 
  1013. continue; 
  1014.  
  1015. $wpdb->insert( $wpdb->term_relationships, array('object_id' => $link->link_id, 'term_taxonomy_id' => $tt_id) ); 
  1016.  
  1017. // Set default to the last category we grabbed during the upgrade loop. 
  1018. update_option('default_link_category', $default_link_cat); 
  1019. } else { 
  1020. $links = $wpdb->get_results("SELECT link_id, category_id FROM $wpdb->link2cat GROUP BY link_id, category_id"); 
  1021. foreach ( $links as $link ) { 
  1022. $link_id = (int) $link->link_id; 
  1023. $term_id = (int) $link->category_id; 
  1024. $taxonomy = 'link_category'; 
  1025. $tt_id = $tt_ids[$term_id][$taxonomy]; 
  1026. if ( empty($tt_id) ) 
  1027. continue; 
  1028. $wpdb->insert( $wpdb->term_relationships, array('object_id' => $link_id, 'term_taxonomy_id' => $tt_id) ); 
  1029.  
  1030. if ( $wp_current_db_version < 4772 ) { 
  1031. // Obsolete linkcategories table 
  1032. $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'linkcategories'); 
  1033.  
  1034. // Recalculate all counts 
  1035. $terms = $wpdb->get_results("SELECT term_taxonomy_id, taxonomy FROM $wpdb->term_taxonomy"); 
  1036. foreach ( (array) $terms as $term ) { 
  1037. if ( ('post_tag' == $term->taxonomy) || ('category' == $term->taxonomy) ) 
  1038. $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) ); 
  1039. else 
  1040. $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $term->term_taxonomy_id) ); 
  1041. $wpdb->update( $wpdb->term_taxonomy, array('count' => $count), array('term_taxonomy_id' => $term->term_taxonomy_id) ); 
  1042.  
  1043. /** 
  1044. * Remove old options from the database. 
  1045. * 
  1046. * @ignore 
  1047. * @since 2.3.0 
  1048. * 
  1049. * @global wpdb $wpdb WordPress database abstraction object. 
  1050. */ 
  1051. function upgrade_230_options_table() { 
  1052. global $wpdb; 
  1053. $old_options_fields = array( 'option_can_override', 'option_type', 'option_width', 'option_height', 'option_description', 'option_admin_level' ); 
  1054. $wpdb->hide_errors(); 
  1055. foreach ( $old_options_fields as $old ) 
  1056. $wpdb->query("ALTER TABLE $wpdb->options DROP $old"); 
  1057. $wpdb->show_errors(); 
  1058.  
  1059. /** 
  1060. * Remove old categories, link2cat, and post2cat database tables. 
  1061. * 
  1062. * @ignore 
  1063. * @since 2.3.0 
  1064. * 
  1065. * @global wpdb $wpdb WordPress database abstraction object. 
  1066. */ 
  1067. function upgrade_230_old_tables() { 
  1068. global $wpdb; 
  1069. $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'categories'); 
  1070. $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'link2cat'); 
  1071. $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'post2cat'); 
  1072.  
  1073. /** 
  1074. * Upgrade old slugs made in version 2.2. 
  1075. * 
  1076. * @ignore 
  1077. * @since 2.2.0 
  1078. * 
  1079. * @global wpdb $wpdb WordPress database abstraction object. 
  1080. */ 
  1081. function upgrade_old_slugs() { 
  1082. // Upgrade people who were using the Redirect Old Slugs plugin. 
  1083. global $wpdb; 
  1084. $wpdb->query("UPDATE $wpdb->postmeta SET meta_key = '_wp_old_slug' WHERE meta_key = 'old_slug'"); 
  1085.  
  1086. /** 
  1087. * Execute changes made in WordPress 2.5.0. 
  1088. * 
  1089. * @ignore 
  1090. * @since 2.5.0 
  1091. * 
  1092. * @global int $wp_current_db_version 
  1093. */ 
  1094. function upgrade_250() { 
  1095. global $wp_current_db_version; 
  1096.  
  1097. if ( $wp_current_db_version < 6689 ) { 
  1098. populate_roles_250(); 
  1099.  
  1100.  
  1101. /** 
  1102. * Execute changes made in WordPress 2.5.2. 
  1103. * 
  1104. * @ignore 
  1105. * @since 2.5.2 
  1106. * 
  1107. * @global wpdb $wpdb WordPress database abstraction object. 
  1108. */ 
  1109. function upgrade_252() { 
  1110. global $wpdb; 
  1111.  
  1112. $wpdb->query("UPDATE $wpdb->users SET user_activation_key = ''"); 
  1113.  
  1114. /** 
  1115. * Execute changes made in WordPress 2.6. 
  1116. * 
  1117. * @ignore 
  1118. * @since 2.6.0 
  1119. * 
  1120. * @global int $wp_current_db_version 
  1121. */ 
  1122. function upgrade_260() { 
  1123. global $wp_current_db_version; 
  1124.  
  1125. if ( $wp_current_db_version < 8000 ) 
  1126. populate_roles_260(); 
  1127.  
  1128. /** 
  1129. * Execute changes made in WordPress 2.7. 
  1130. * 
  1131. * @ignore 
  1132. * @since 2.7.0 
  1133. * 
  1134. * @global wpdb $wpdb WordPress database abstraction object. 
  1135. * @global int $wp_current_db_version 
  1136. */ 
  1137. function upgrade_270() { 
  1138. global $wpdb, $wp_current_db_version; 
  1139.  
  1140. if ( $wp_current_db_version < 8980 ) 
  1141. populate_roles_270(); 
  1142.  
  1143. // Update post_date for unpublished posts with empty timestamp 
  1144. if ( $wp_current_db_version < 8921 ) 
  1145. $wpdb->query( "UPDATE $wpdb->posts SET post_date = post_modified WHERE post_date = '0000-00-00 00:00:00'" ); 
  1146.  
  1147. /** 
  1148. * Execute changes made in WordPress 2.8. 
  1149. * 
  1150. * @ignore 
  1151. * @since 2.8.0 
  1152. * 
  1153. * @global int $wp_current_db_version 
  1154. * @global wpdb $wpdb WordPress database abstraction object. 
  1155. */ 
  1156. function upgrade_280() { 
  1157. global $wp_current_db_version, $wpdb; 
  1158.  
  1159. if ( $wp_current_db_version < 10360 ) 
  1160. populate_roles_280(); 
  1161. if ( is_multisite() ) { 
  1162. $start = 0; 
  1163. while( $rows = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options ORDER BY option_id LIMIT $start, 20" ) ) { 
  1164. foreach ( $rows as $row ) { 
  1165. $value = $row->option_value; 
  1166. if ( !@unserialize( $value ) ) 
  1167. $value = stripslashes( $value ); 
  1168. if ( $value !== $row->option_value ) { 
  1169. update_option( $row->option_name, $value ); 
  1170. $start += 20; 
  1171. refresh_blog_details( $wpdb->blogid ); 
  1172.  
  1173. /** 
  1174. * Execute changes made in WordPress 2.9. 
  1175. * 
  1176. * @ignore 
  1177. * @since 2.9.0 
  1178. * 
  1179. * @global int $wp_current_db_version 
  1180. */ 
  1181. function upgrade_290() { 
  1182. global $wp_current_db_version; 
  1183.  
  1184. if ( $wp_current_db_version < 11958 ) { 
  1185. // Previously, setting depth to 1 would redundantly disable threading, but now 2 is the minimum depth to avoid confusion 
  1186. if ( get_option( 'thread_comments_depth' ) == '1' ) { 
  1187. update_option( 'thread_comments_depth', 2 ); 
  1188. update_option( 'thread_comments', 0 ); 
  1189.  
  1190. /** 
  1191. * Execute changes made in WordPress 3.0. 
  1192. * 
  1193. * @ignore 
  1194. * @since 3.0.0 
  1195. * 
  1196. * @global int $wp_current_db_version 
  1197. * @global wpdb $wpdb WordPress database abstraction object. 
  1198. */ 
  1199. function upgrade_300() { 
  1200. global $wp_current_db_version, $wpdb; 
  1201.  
  1202. if ( $wp_current_db_version < 15093 ) 
  1203. populate_roles_300(); 
  1204.  
  1205. if ( $wp_current_db_version < 14139 && is_multisite() && is_main_site() && ! defined( 'MULTISITE' ) && get_site_option( 'siteurl' ) === false ) 
  1206. add_site_option( 'siteurl', '' ); 
  1207.  
  1208. // 3.0 screen options key name changes. 
  1209. if ( wp_should_upgrade_global_tables() ) { 
  1210. $sql = "DELETE FROM $wpdb->usermeta 
  1211. WHERE meta_key LIKE %s 
  1212. OR meta_key LIKE %s 
  1213. OR meta_key LIKE %s 
  1214. OR meta_key LIKE %s 
  1215. OR meta_key LIKE %s 
  1216. OR meta_key LIKE %s 
  1217. OR meta_key = 'manageedittagscolumnshidden' 
  1218. OR meta_key = 'managecategoriescolumnshidden' 
  1219. OR meta_key = 'manageedit-tagscolumnshidden' 
  1220. OR meta_key = 'manageeditcolumnshidden' 
  1221. OR meta_key = 'categories_per_page' 
  1222. OR meta_key = 'edit_tags_per_page'"; 
  1223. $prefix = $wpdb->esc_like( $wpdb->base_prefix ); 
  1224. $wpdb->query( $wpdb->prepare( $sql,  
  1225. $prefix . '%' . $wpdb->esc_like( 'meta-box-hidden' ) . '%',  
  1226. $prefix . '%' . $wpdb->esc_like( 'closedpostboxes' ) . '%',  
  1227. $prefix . '%' . $wpdb->esc_like( 'manage-' ) . '%' . $wpdb->esc_like( '-columns-hidden' ) . '%',  
  1228. $prefix . '%' . $wpdb->esc_like( 'meta-box-order' ) . '%',  
  1229. $prefix . '%' . $wpdb->esc_like( 'metaboxorder' ) . '%',  
  1230. $prefix . '%' . $wpdb->esc_like( 'screen_layout' ) . '%' 
  1231. ) ); 
  1232.  
  1233.  
  1234. /** 
  1235. * Execute changes made in WordPress 3.3. 
  1236. * 
  1237. * @ignore 
  1238. * @since 3.3.0 
  1239. * 
  1240. * @global int $wp_current_db_version 
  1241. * @global wpdb $wpdb 
  1242. * @global array $wp_registered_widgets 
  1243. * @global array $sidebars_widgets 
  1244. */ 
  1245. function upgrade_330() { 
  1246. global $wp_current_db_version, $wpdb, $wp_registered_widgets, $sidebars_widgets; 
  1247.  
  1248. if ( $wp_current_db_version < 19061 && wp_should_upgrade_global_tables() ) { 
  1249. $wpdb->query( "DELETE FROM $wpdb->usermeta WHERE meta_key IN ('show_admin_bar_admin', 'plugins_last_view')" ); 
  1250.  
  1251. if ( $wp_current_db_version >= 11548 ) 
  1252. return; 
  1253.  
  1254. $sidebars_widgets = get_option( 'sidebars_widgets', array() ); 
  1255. $_sidebars_widgets = array(); 
  1256.  
  1257. if ( isset($sidebars_widgets['wp_inactive_widgets']) || empty($sidebars_widgets) ) 
  1258. $sidebars_widgets['array_version'] = 3; 
  1259. elseif ( !isset($sidebars_widgets['array_version']) ) 
  1260. $sidebars_widgets['array_version'] = 1; 
  1261.  
  1262. switch ( $sidebars_widgets['array_version'] ) { 
  1263. case 1 : 
  1264. foreach ( (array) $sidebars_widgets as $index => $sidebar ) 
  1265. if ( is_array($sidebar) ) 
  1266. foreach ( (array) $sidebar as $i => $name ) { 
  1267. $id = strtolower($name); 
  1268. if ( isset($wp_registered_widgets[$id]) ) { 
  1269. $_sidebars_widgets[$index][$i] = $id; 
  1270. continue; 
  1271. $id = sanitize_title($name); 
  1272. if ( isset($wp_registered_widgets[$id]) ) { 
  1273. $_sidebars_widgets[$index][$i] = $id; 
  1274. continue; 
  1275.  
  1276. $found = false; 
  1277.  
  1278. foreach ( $wp_registered_widgets as $widget_id => $widget ) { 
  1279. if ( strtolower($widget['name']) == strtolower($name) ) { 
  1280. $_sidebars_widgets[$index][$i] = $widget['id']; 
  1281. $found = true; 
  1282. break; 
  1283. } elseif ( sanitize_title($widget['name']) == sanitize_title($name) ) { 
  1284. $_sidebars_widgets[$index][$i] = $widget['id']; 
  1285. $found = true; 
  1286. break; 
  1287.  
  1288. if ( $found ) 
  1289. continue; 
  1290.  
  1291. unset($_sidebars_widgets[$index][$i]); 
  1292. $_sidebars_widgets['array_version'] = 2; 
  1293. $sidebars_widgets = $_sidebars_widgets; 
  1294. unset($_sidebars_widgets); 
  1295.  
  1296. case 2 : 
  1297. $sidebars_widgets = retrieve_widgets(); 
  1298. $sidebars_widgets['array_version'] = 3; 
  1299. update_option( 'sidebars_widgets', $sidebars_widgets ); 
  1300.  
  1301. /** 
  1302. * Execute changes made in WordPress 3.4. 
  1303. * 
  1304. * @ignore 
  1305. * @since 3.4.0 
  1306. * 
  1307. * @global int $wp_current_db_version 
  1308. * @global wpdb $wpdb 
  1309. */ 
  1310. function upgrade_340() { 
  1311. global $wp_current_db_version, $wpdb; 
  1312.  
  1313. if ( $wp_current_db_version < 19798 ) { 
  1314. $wpdb->hide_errors(); 
  1315. $wpdb->query( "ALTER TABLE $wpdb->options DROP COLUMN blog_id" ); 
  1316. $wpdb->show_errors(); 
  1317.  
  1318. if ( $wp_current_db_version < 19799 ) { 
  1319. $wpdb->hide_errors(); 
  1320. $wpdb->query("ALTER TABLE $wpdb->comments DROP INDEX comment_approved"); 
  1321. $wpdb->show_errors(); 
  1322.  
  1323. if ( $wp_current_db_version < 20022 && wp_should_upgrade_global_tables() ) { 
  1324. $wpdb->query( "DELETE FROM $wpdb->usermeta WHERE meta_key = 'themes_last_view'" ); 
  1325.  
  1326. if ( $wp_current_db_version < 20080 ) { 
  1327. if ( 'yes' == $wpdb->get_var( "SELECT autoload FROM $wpdb->options WHERE option_name = 'uninstall_plugins'" ) ) { 
  1328. $uninstall_plugins = get_option( 'uninstall_plugins' ); 
  1329. delete_option( 'uninstall_plugins' ); 
  1330. add_option( 'uninstall_plugins', $uninstall_plugins, null, 'no' ); 
  1331.  
  1332. /** 
  1333. * Execute changes made in WordPress 3.5. 
  1334. * 
  1335. * @ignore 
  1336. * @since 3.5.0 
  1337. * 
  1338. * @global int $wp_current_db_version 
  1339. * @global wpdb $wpdb 
  1340. */ 
  1341. function upgrade_350() { 
  1342. global $wp_current_db_version, $wpdb; 
  1343.  
  1344. if ( $wp_current_db_version < 22006 && $wpdb->get_var( "SELECT link_id FROM $wpdb->links LIMIT 1" ) ) 
  1345. update_option( 'link_manager_enabled', 1 ); // Previously set to 0 by populate_options() 
  1346.  
  1347. if ( $wp_current_db_version < 21811 && wp_should_upgrade_global_tables() ) { 
  1348. $meta_keys = array(); 
  1349. foreach ( array_merge( get_post_types(), get_taxonomies() ) as $name ) { 
  1350. if ( false !== strpos( $name, '-' ) ) 
  1351. $meta_keys[] = 'edit_' . str_replace( '-', '_', $name ) . '_per_page'; 
  1352. if ( $meta_keys ) { 
  1353. $meta_keys = implode( "', '", $meta_keys ); 
  1354. $wpdb->query( "DELETE FROM $wpdb->usermeta WHERE meta_key IN ('$meta_keys')" ); 
  1355.  
  1356. if ( $wp_current_db_version < 22422 && $term = get_term_by( 'slug', 'post-format-standard', 'post_format' ) ) 
  1357. wp_delete_term( $term->term_id, 'post_format' ); 
  1358.  
  1359. /** 
  1360. * Execute changes made in WordPress 3.7. 
  1361. * 
  1362. * @ignore 
  1363. * @since 3.7.0 
  1364. * 
  1365. * @global int $wp_current_db_version 
  1366. */ 
  1367. function upgrade_370() { 
  1368. global $wp_current_db_version; 
  1369. if ( $wp_current_db_version < 25824 ) 
  1370. wp_clear_scheduled_hook( 'wp_auto_updates_maybe_update' ); 
  1371.  
  1372. /** 
  1373. * Execute changes made in WordPress 3.7.2. 
  1374. * 
  1375. * @ignore 
  1376. * @since 3.7.2 
  1377. * @since 3.8.0 
  1378. * 
  1379. * @global int $wp_current_db_version 
  1380. */ 
  1381. function upgrade_372() { 
  1382. global $wp_current_db_version; 
  1383. if ( $wp_current_db_version < 26148 ) 
  1384. wp_clear_scheduled_hook( 'wp_maybe_auto_update' ); 
  1385.  
  1386. /** 
  1387. * Execute changes made in WordPress 3.8.0. 
  1388. * 
  1389. * @ignore 
  1390. * @since 3.8.0 
  1391. * 
  1392. * @global int $wp_current_db_version 
  1393. */ 
  1394. function upgrade_380() { 
  1395. global $wp_current_db_version; 
  1396. if ( $wp_current_db_version < 26691 ) { 
  1397. deactivate_plugins( array( 'mp6/mp6.php' ), true ); 
  1398.  
  1399. /** 
  1400. * Execute changes made in WordPress 4.0.0. 
  1401. * 
  1402. * @ignore 
  1403. * @since 4.0.0 
  1404. * 
  1405. * @global int $wp_current_db_version 
  1406. */ 
  1407. function upgrade_400() { 
  1408. global $wp_current_db_version; 
  1409. if ( $wp_current_db_version < 29630 ) { 
  1410. if ( ! is_multisite() && false === get_option( 'WPLANG' ) ) { 
  1411. if ( defined( 'WPLANG' ) && ( '' !== WPLANG ) && in_array( WPLANG, get_available_languages() ) ) { 
  1412. update_option( 'WPLANG', WPLANG ); 
  1413. } else { 
  1414. update_option( 'WPLANG', '' ); 
  1415.  
  1416. /** 
  1417. * Execute changes made in WordPress 4.2.0. 
  1418. * 
  1419. * @ignore 
  1420. * @since 4.2.0 
  1421. * 
  1422. * @global int $wp_current_db_version 
  1423. * @global wpdb $wpdb 
  1424. */ 
  1425. function upgrade_420() {} 
  1426.  
  1427. /** 
  1428. * Executes changes made in WordPress 4.3.0. 
  1429. * 
  1430. * @ignore 
  1431. * @since 4.3.0 
  1432. * 
  1433. * @global int $wp_current_db_version Current version. 
  1434. * @global wpdb $wpdb WordPress database abstraction object. 
  1435. */ 
  1436. function upgrade_430() { 
  1437. global $wp_current_db_version, $wpdb; 
  1438.  
  1439. if ( $wp_current_db_version < 32364 ) { 
  1440. upgrade_430_fix_comments(); 
  1441.  
  1442. // Shared terms are split in a separate process. 
  1443. if ( $wp_current_db_version < 32814 ) { 
  1444. update_option( 'finished_splitting_shared_terms', 0 ); 
  1445. wp_schedule_single_event( time() + ( 1 * MINUTE_IN_SECONDS ), 'wp_split_shared_term_batch' ); 
  1446.  
  1447. if ( $wp_current_db_version < 33055 && 'utf8mb4' === $wpdb->charset ) { 
  1448. if ( is_multisite() ) { 
  1449. $tables = $wpdb->tables( 'blog' ); 
  1450. } else { 
  1451. $tables = $wpdb->tables( 'all' ); 
  1452. if ( ! wp_should_upgrade_global_tables() ) { 
  1453. $global_tables = $wpdb->tables( 'global' ); 
  1454. $tables = array_diff_assoc( $tables, $global_tables ); 
  1455.  
  1456. foreach ( $tables as $table ) { 
  1457. maybe_convert_table_to_utf8mb4( $table ); 
  1458.  
  1459. /** 
  1460. * Executes comments changes made in WordPress 4.3.0. 
  1461. * 
  1462. * @ignore 
  1463. * @since 4.3.0 
  1464. * 
  1465. * @global int $wp_current_db_version Current version. 
  1466. * @global wpdb $wpdb WordPress database abstraction object. 
  1467. */ 
  1468. function upgrade_430_fix_comments() { 
  1469. global $wp_current_db_version, $wpdb; 
  1470.  
  1471. $content_length = $wpdb->get_col_length( $wpdb->comments, 'comment_content' ); 
  1472.  
  1473. if ( is_wp_error( $content_length ) ) { 
  1474. return; 
  1475.  
  1476. if ( false === $content_length ) { 
  1477. $content_length = array( 
  1478. 'type' => 'byte',  
  1479. 'length' => 65535,  
  1480. ); 
  1481. } elseif ( ! is_array( $content_length ) ) { 
  1482. $length = (int) $content_length > 0 ? (int) $content_length : 65535; 
  1483. $content_length = array( 
  1484. 'type' => 'byte',  
  1485. 'length' => $length 
  1486. ); 
  1487.  
  1488. if ( 'byte' !== $content_length['type'] || 0 === $content_length['length'] ) { 
  1489. // Sites with malformed DB schemas are on their own. 
  1490. return; 
  1491.  
  1492. $allowed_length = intval( $content_length['length'] ) - 10; 
  1493.  
  1494. $comments = $wpdb->get_results( 
  1495. "SELECT `comment_ID` FROM `{$wpdb->comments}` 
  1496. WHERE `comment_date_gmt` > '2015-04-26' 
  1497. AND LENGTH( `comment_content` ) >= {$allowed_length} 
  1498. AND ( `comment_content` LIKE '%<%' OR `comment_content` LIKE '%>%' )" 
  1499. ); 
  1500.  
  1501. foreach ( $comments as $comment ) { 
  1502. wp_delete_comment( $comment->comment_ID, true ); 
  1503.  
  1504. /** 
  1505. * Executes changes made in WordPress 4.3.1. 
  1506. * 
  1507. * @ignore 
  1508. * @since 4.3.1 
  1509. */ 
  1510. function upgrade_431() { 
  1511. // Fix incorrect cron entries for term splitting 
  1512. $cron_array = _get_cron_array(); 
  1513. if ( isset( $cron_array['wp_batch_split_terms'] ) ) { 
  1514. unset( $cron_array['wp_batch_split_terms'] ); 
  1515. _set_cron_array( $cron_array ); 
  1516.  
  1517. /** 
  1518. * Executes changes made in WordPress 4.4.0. 
  1519. * 
  1520. * @ignore 
  1521. * @since 4.4.0 
  1522. * 
  1523. * @global int $wp_current_db_version Current version. 
  1524. * @global wpdb $wpdb WordPress database abstraction object. 
  1525. */ 
  1526. function upgrade_440() { 
  1527. global $wp_current_db_version, $wpdb; 
  1528.  
  1529. if ( $wp_current_db_version < 34030 ) { 
  1530. $wpdb->query( "ALTER TABLE {$wpdb->options} MODIFY option_name VARCHAR(191)" ); 
  1531.  
  1532. // Remove the unused 'add_users' role. 
  1533. $roles = wp_roles(); 
  1534. foreach ( $roles->role_objects as $role ) { 
  1535. if ( $role->has_cap( 'add_users' ) ) { 
  1536. $role->remove_cap( 'add_users' ); 
  1537.  
  1538. /** 
  1539. * Executes changes made in WordPress 4.5.0. 
  1540. * 
  1541. * @ignore 
  1542. * @since 4.5.0 
  1543. * 
  1544. * @global int $wp_current_db_version Current database version. 
  1545. * @global wpdb $wpdb WordPress database abstraction object. 
  1546. */ 
  1547. function upgrade_450() { 
  1548. global $wp_current_db_version, $wpdb; 
  1549.  
  1550. if ( $wp_current_db_version < 36180 ) { 
  1551. wp_clear_scheduled_hook( 'wp_maybe_auto_update' ); 
  1552.  
  1553. // Remove unused email confirmation options, moved to usermeta. 
  1554. if ( $wp_current_db_version < 36679 && is_multisite() ) { 
  1555. $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name REGEXP '^[0-9]+_new_email$'" ); 
  1556.  
  1557. // Remove unused user setting for wpLink. 
  1558. delete_user_setting( 'wplink' ); 
  1559.  
  1560. /** 
  1561. * Executes changes made in WordPress 4.6.0. 
  1562. * 
  1563. * @ignore 
  1564. * @since 4.6.0 
  1565. * 
  1566. * @global int $wp_current_db_version Current database version. 
  1567. */ 
  1568. function upgrade_460() { 
  1569. global $wp_current_db_version; 
  1570.  
  1571. // Remove unused post meta. 
  1572. if ( $wp_current_db_version < 37854 ) { 
  1573. delete_post_meta_by_key( '_post_restored_from' ); 
  1574.  
  1575. // Remove plugins with callback as an array object/method as the uninstall hook, see #13786. 
  1576. if ( $wp_current_db_version < 37965 ) { 
  1577. $uninstall_plugins = get_option( 'uninstall_plugins', array() ); 
  1578.  
  1579. if ( ! empty( $uninstall_plugins ) ) { 
  1580. foreach ( $uninstall_plugins as $basename => $callback ) { 
  1581. if ( is_array( $callback ) && is_object( $callback[0] ) ) { 
  1582. unset( $uninstall_plugins[ $basename ] ); 
  1583.  
  1584. update_option( 'uninstall_plugins', $uninstall_plugins ); 
  1585.  
  1586. /** 
  1587. * Executes network-level upgrade routines. 
  1588. * 
  1589. * @since 3.0.0 
  1590. * 
  1591. * @global int $wp_current_db_version 
  1592. * @global wpdb $wpdb 
  1593. */ 
  1594. function upgrade_network() { 
  1595. global $wp_current_db_version, $wpdb; 
  1596.  
  1597. // Always. 
  1598. if ( is_main_network() ) { 
  1599. /** 
  1600. * Deletes all expired transients. The multi-table delete syntax is used 
  1601. * to delete the transient record from table a, and the corresponding 
  1602. * transient_timeout record from table b. 
  1603. */ 
  1604. $time = time(); 
  1605. $sql = "DELETE a, b FROM $wpdb->sitemeta a, $wpdb->sitemeta b 
  1606. WHERE a.meta_key LIKE %s 
  1607. AND a.meta_key NOT LIKE %s 
  1608. AND b.meta_key = CONCAT( '_site_transient_timeout_', SUBSTRING( a.meta_key, 17 ) ) 
  1609. AND b.meta_value < %d"; 
  1610. $wpdb->query( $wpdb->prepare( $sql, $wpdb->esc_like( '_site_transient_' ) . '%', $wpdb->esc_like ( '_site_transient_timeout_' ) . '%', $time ) ); 
  1611.  
  1612. // 2.8. 
  1613. if ( $wp_current_db_version < 11549 ) { 
  1614. $wpmu_sitewide_plugins = get_site_option( 'wpmu_sitewide_plugins' ); 
  1615. $active_sitewide_plugins = get_site_option( 'active_sitewide_plugins' ); 
  1616. if ( $wpmu_sitewide_plugins ) { 
  1617. if ( !$active_sitewide_plugins ) 
  1618. $sitewide_plugins = (array) $wpmu_sitewide_plugins; 
  1619. else 
  1620. $sitewide_plugins = array_merge( (array) $active_sitewide_plugins, (array) $wpmu_sitewide_plugins ); 
  1621.  
  1622. update_site_option( 'active_sitewide_plugins', $sitewide_plugins ); 
  1623. delete_site_option( 'wpmu_sitewide_plugins' ); 
  1624. delete_site_option( 'deactivated_sitewide_plugins' ); 
  1625.  
  1626. $start = 0; 
  1627. while( $rows = $wpdb->get_results( "SELECT meta_key, meta_value FROM {$wpdb->sitemeta} ORDER BY meta_id LIMIT $start, 20" ) ) { 
  1628. foreach ( $rows as $row ) { 
  1629. $value = $row->meta_value; 
  1630. if ( !@unserialize( $value ) ) 
  1631. $value = stripslashes( $value ); 
  1632. if ( $value !== $row->meta_value ) { 
  1633. update_site_option( $row->meta_key, $value ); 
  1634. $start += 20; 
  1635.  
  1636. // 3.0 
  1637. if ( $wp_current_db_version < 13576 ) 
  1638. update_site_option( 'global_terms_enabled', '1' ); 
  1639.  
  1640. // 3.3 
  1641. if ( $wp_current_db_version < 19390 ) 
  1642. update_site_option( 'initial_db_version', $wp_current_db_version ); 
  1643.  
  1644. if ( $wp_current_db_version < 19470 ) { 
  1645. if ( false === get_site_option( 'active_sitewide_plugins' ) ) 
  1646. update_site_option( 'active_sitewide_plugins', array() ); 
  1647.  
  1648. // 3.4 
  1649. if ( $wp_current_db_version < 20148 ) { 
  1650. // 'allowedthemes' keys things by stylesheet. 'allowed_themes' keyed things by name. 
  1651. $allowedthemes = get_site_option( 'allowedthemes' ); 
  1652. $allowed_themes = get_site_option( 'allowed_themes' ); 
  1653. if ( false === $allowedthemes && is_array( $allowed_themes ) && $allowed_themes ) { 
  1654. $converted = array(); 
  1655. $themes = wp_get_themes(); 
  1656. foreach ( $themes as $stylesheet => $theme_data ) { 
  1657. if ( isset( $allowed_themes[ $theme_data->get('Name') ] ) ) 
  1658. $converted[ $stylesheet ] = true; 
  1659. update_site_option( 'allowedthemes', $converted ); 
  1660. delete_site_option( 'allowed_themes' ); 
  1661.  
  1662. // 3.5 
  1663. if ( $wp_current_db_version < 21823 ) 
  1664. update_site_option( 'ms_files_rewriting', '1' ); 
  1665.  
  1666. // 3.5.2 
  1667. if ( $wp_current_db_version < 24448 ) { 
  1668. $illegal_names = get_site_option( 'illegal_names' ); 
  1669. if ( is_array( $illegal_names ) && count( $illegal_names ) === 1 ) { 
  1670. $illegal_name = reset( $illegal_names ); 
  1671. $illegal_names = explode( ' ', $illegal_name ); 
  1672. update_site_option( 'illegal_names', $illegal_names ); 
  1673.  
  1674. // 4.2 
  1675. if ( $wp_current_db_version < 31351 && $wpdb->charset === 'utf8mb4' ) { 
  1676. if ( wp_should_upgrade_global_tables() ) { 
  1677. $wpdb->query( "ALTER TABLE $wpdb->usermeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" ); 
  1678. $wpdb->query( "ALTER TABLE $wpdb->site DROP INDEX domain, ADD INDEX domain(domain(140), path(51))" ); 
  1679. $wpdb->query( "ALTER TABLE $wpdb->sitemeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" ); 
  1680. $wpdb->query( "ALTER TABLE $wpdb->signups DROP INDEX domain_path, ADD INDEX domain_path(domain(140), path(51))" ); 
  1681.  
  1682. $tables = $wpdb->tables( 'global' ); 
  1683.  
  1684. // sitecategories may not exist. 
  1685. if ( ! $wpdb->get_var( "SHOW TABLES LIKE '{$tables['sitecategories']}'" ) ) { 
  1686. unset( $tables['sitecategories'] ); 
  1687.  
  1688. foreach ( $tables as $table ) { 
  1689. maybe_convert_table_to_utf8mb4( $table ); 
  1690.  
  1691. // 4.3 
  1692. if ( $wp_current_db_version < 33055 && 'utf8mb4' === $wpdb->charset ) { 
  1693. if ( wp_should_upgrade_global_tables() ) { 
  1694. $upgrade = false; 
  1695. $indexes = $wpdb->get_results( "SHOW INDEXES FROM $wpdb->signups" ); 
  1696. foreach ( $indexes as $index ) { 
  1697. if ( 'domain_path' == $index->Key_name && 'domain' == $index->Column_name && 140 != $index->Sub_part ) { 
  1698. $upgrade = true; 
  1699. break; 
  1700.  
  1701. if ( $upgrade ) { 
  1702. $wpdb->query( "ALTER TABLE $wpdb->signups DROP INDEX domain_path, ADD INDEX domain_path(domain(140), path(51))" ); 
  1703.  
  1704. $tables = $wpdb->tables( 'global' ); 
  1705.  
  1706. // sitecategories may not exist. 
  1707. if ( ! $wpdb->get_var( "SHOW TABLES LIKE '{$tables['sitecategories']}'" ) ) { 
  1708. unset( $tables['sitecategories'] ); 
  1709.  
  1710. foreach ( $tables as $table ) { 
  1711. maybe_convert_table_to_utf8mb4( $table ); 
  1712.  
  1713. // 
  1714. // General functions we use to actually do stuff 
  1715. // 
  1716.   
  1717. /** 
  1718. * Creates a table in the database if it doesn't already exist. 
  1719. * 
  1720. * This method checks for an existing database and creates a new one if it's not 
  1721. * already present. It doesn't rely on MySQL's "IF NOT EXISTS" statement, but chooses 
  1722. * to query all tables first and then run the SQL statement creating the table. 
  1723. * 
  1724. * @since 1.0.0 
  1725. * 
  1726. * @global wpdb $wpdb 
  1727. * 
  1728. * @param string $table_name Database table name to create. 
  1729. * @param string $create_ddl SQL statement to create table. 
  1730. * @return bool If table already exists or was created by function. 
  1731. */ 
  1732. function maybe_create_table($table_name, $create_ddl) { 
  1733. global $wpdb; 
  1734.  
  1735. $query = $wpdb->prepare( "SHOW TABLES LIKE %s", $wpdb->esc_like( $table_name ) ); 
  1736.  
  1737. if ( $wpdb->get_var( $query ) == $table_name ) { 
  1738. return true; 
  1739.  
  1740. // Didn't find it try to create it.. 
  1741. $wpdb->query($create_ddl); 
  1742.  
  1743. // We cannot directly tell that whether this succeeded! 
  1744. if ( $wpdb->get_var( $query ) == $table_name ) { 
  1745. return true; 
  1746. return false; 
  1747.  
  1748. /** 
  1749. * Drops a specified index from a table. 
  1750. * 
  1751. * @since 1.0.1 
  1752. * 
  1753. * @global wpdb $wpdb 
  1754. * 
  1755. * @param string $table Database table name. 
  1756. * @param string $index Index name to drop. 
  1757. * @return true True, when finished. 
  1758. */ 
  1759. function drop_index($table, $index) { 
  1760. global $wpdb; 
  1761. $wpdb->hide_errors(); 
  1762. $wpdb->query("ALTER TABLE `$table` DROP INDEX `$index`"); 
  1763. // Now we need to take out all the extra ones we may have created 
  1764. for ($i = 0; $i < 25; $i++) { 
  1765. $wpdb->query("ALTER TABLE `$table` DROP INDEX `{$index}_$i`"); 
  1766. $wpdb->show_errors(); 
  1767. return true; 
  1768.  
  1769. /** 
  1770. * Adds an index to a specified table. 
  1771. * 
  1772. * @since 1.0.1 
  1773. * 
  1774. * @global wpdb $wpdb 
  1775. * 
  1776. * @param string $table Database table name. 
  1777. * @param string $index Database table index column. 
  1778. * @return true True, when done with execution. 
  1779. */ 
  1780. function add_clean_index($table, $index) { 
  1781. global $wpdb; 
  1782. drop_index($table, $index); 
  1783. $wpdb->query("ALTER TABLE `$table` ADD INDEX ( `$index` )"); 
  1784. return true; 
  1785.  
  1786. /** 
  1787. * Adds column to a database table if it doesn't already exist. 
  1788. * 
  1789. * @since 1.3.0 
  1790. * 
  1791. * @global wpdb $wpdb 
  1792. * 
  1793. * @param string $table_name The table name to modify. 
  1794. * @param string $column_name The column name to add to the table. 
  1795. * @param string $create_ddl The SQL statement used to add the column. 
  1796. * @return bool True if already exists or on successful completion, false on error. 
  1797. */ 
  1798. function maybe_add_column($table_name, $column_name, $create_ddl) { 
  1799. global $wpdb; 
  1800. foreach ($wpdb->get_col("DESC $table_name", 0) as $column ) { 
  1801. if ($column == $column_name) { 
  1802. return true; 
  1803.  
  1804. // Didn't find it try to create it. 
  1805. $wpdb->query($create_ddl); 
  1806.  
  1807. // We cannot directly tell that whether this succeeded! 
  1808. foreach ($wpdb->get_col("DESC $table_name", 0) as $column ) { 
  1809. if ($column == $column_name) { 
  1810. return true; 
  1811. return false; 
  1812.  
  1813. /** 
  1814. * If a table only contains utf8 or utf8mb4 columns, convert it to utf8mb4. 
  1815. * 
  1816. * @since 4.2.0 
  1817. * 
  1818. * @global wpdb $wpdb 
  1819. * 
  1820. * @param string $table The table to convert. 
  1821. * @return bool true if the table was converted, false if it wasn't. 
  1822. */ 
  1823. function maybe_convert_table_to_utf8mb4( $table ) { 
  1824. global $wpdb; 
  1825.  
  1826. $results = $wpdb->get_results( "SHOW FULL COLUMNS FROM `$table`" ); 
  1827. if ( ! $results ) { 
  1828. return false; 
  1829.  
  1830. foreach ( $results as $column ) { 
  1831. if ( $column->Collation ) { 
  1832. list( $charset ) = explode( '_', $column->Collation ); 
  1833. $charset = strtolower( $charset ); 
  1834. if ( 'utf8' !== $charset && 'utf8mb4' !== $charset ) { 
  1835. // Don't upgrade tables that have non-utf8 columns. 
  1836. return false; 
  1837.  
  1838. $table_details = $wpdb->get_row( "SHOW TABLE STATUS LIKE '$table'" ); 
  1839. if ( ! $table_details ) { 
  1840. return false; 
  1841.  
  1842. list( $table_charset ) = explode( '_', $table_details->Collation ); 
  1843. $table_charset = strtolower( $table_charset ); 
  1844. if ( 'utf8mb4' === $table_charset ) { 
  1845. return true; 
  1846.  
  1847. return $wpdb->query( "ALTER TABLE $table CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci" ); 
  1848.  
  1849. /** 
  1850. * Retrieve all options as it was for 1.2. 
  1851. * 
  1852. * @since 1.2.0 
  1853. * 
  1854. * @global wpdb $wpdb 
  1855. * 
  1856. * @return stdClass List of options. 
  1857. */ 
  1858. function get_alloptions_110() { 
  1859. global $wpdb; 
  1860. $all_options = new stdClass; 
  1861. if ( $options = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options" ) ) { 
  1862. foreach ( $options as $option ) { 
  1863. if ( 'siteurl' == $option->option_name || 'home' == $option->option_name || 'category_base' == $option->option_name ) 
  1864. $option->option_value = untrailingslashit( $option->option_value ); 
  1865. $all_options->{$option->option_name} = stripslashes( $option->option_value ); 
  1866. return $all_options; 
  1867.  
  1868. /** 
  1869. * Utility version of get_option that is private to install/upgrade. 
  1870. * 
  1871. * @ignore 
  1872. * @since 1.5.1 
  1873. * @access private 
  1874. * 
  1875. * @global wpdb $wpdb 
  1876. * 
  1877. * @param string $setting Option name. 
  1878. * @return mixed 
  1879. */ 
  1880. function __get_option($setting) { 
  1881. global $wpdb; 
  1882.  
  1883. if ( $setting == 'home' && defined( 'WP_HOME' ) ) 
  1884. return untrailingslashit( WP_HOME ); 
  1885.  
  1886. if ( $setting == 'siteurl' && defined( 'WP_SITEURL' ) ) 
  1887. return untrailingslashit( WP_SITEURL ); 
  1888.  
  1889. $option = $wpdb->get_var( $wpdb->prepare("SELECT option_value FROM $wpdb->options WHERE option_name = %s", $setting ) ); 
  1890.  
  1891. if ( 'home' == $setting && '' == $option ) 
  1892. return __get_option( 'siteurl' ); 
  1893.  
  1894. if ( 'siteurl' == $setting || 'home' == $setting || 'category_base' == $setting || 'tag_base' == $setting ) 
  1895. $option = untrailingslashit( $option ); 
  1896.  
  1897. return maybe_unserialize( $option ); 
  1898.  
  1899. /** 
  1900. * Filters for content to remove unnecessary slashes. 
  1901. * 
  1902. * @since 1.5.0 
  1903. * 
  1904. * @param string $content The content to modify. 
  1905. * @return string The de-slashed content. 
  1906. */ 
  1907. function deslash($content) { 
  1908. // Note: \\\ inside a regex denotes a single backslash. 
  1909.  
  1910. /** 
  1911. * Replace one or more backslashes followed by a single quote with 
  1912. * a single quote. 
  1913. */ 
  1914. $content = preg_replace("/\\\+'/", "'", $content); 
  1915.  
  1916. /** 
  1917. * Replace one or more backslashes followed by a double quote with 
  1918. * a double quote. 
  1919. */ 
  1920. $content = preg_replace('/\\\+"/', '"', $content); 
  1921.  
  1922. // Replace one or more backslashes with one backslash. 
  1923. $content = preg_replace("/\\\+/", "\\", $content); 
  1924.  
  1925. return $content; 
  1926.  
  1927. /** 
  1928. * Modifies the database based on specified SQL statements. 
  1929. * 
  1930. * Useful for creating new tables and updating existing tables to a new structure. 
  1931. * 
  1932. * @since 1.5.0 
  1933. * 
  1934. * @global wpdb $wpdb 
  1935. * 
  1936. * @param string|array $queries Optional. The query to run. Can be multiple queries 
  1937. * in an array, or a string of queries separated by 
  1938. * semicolons. Default empty. 
  1939. * @param bool $execute Optional. Whether or not to execute the query right away. 
  1940. * Default true. 
  1941. * @return array Strings containing the results of the various update queries. 
  1942. */ 
  1943. function dbDelta( $queries = '', $execute = true ) { 
  1944. global $wpdb; 
  1945.  
  1946. if ( in_array( $queries, array( '', 'all', 'blog', 'global', 'ms_global' ), true ) ) 
  1947. $queries = wp_get_db_schema( $queries ); 
  1948.  
  1949. // Separate individual queries into an array 
  1950. if ( !is_array($queries) ) { 
  1951. $queries = explode( ';', $queries ); 
  1952. $queries = array_filter( $queries ); 
  1953.  
  1954. /** 
  1955. * Filters the dbDelta SQL queries. 
  1956. * 
  1957. * @since 3.3.0 
  1958. * 
  1959. * @param array $queries An array of dbDelta SQL queries. 
  1960. */ 
  1961. $queries = apply_filters( 'dbdelta_queries', $queries ); 
  1962.  
  1963. $cqueries = array(); // Creation Queries 
  1964. $iqueries = array(); // Insertion Queries 
  1965. $for_update = array(); 
  1966.  
  1967. // Create a tablename index for an array ($cqueries) of queries 
  1968. foreach ($queries as $qry) { 
  1969. if ( preg_match( "|CREATE TABLE ([^ ]*)|", $qry, $matches ) ) { 
  1970. $cqueries[ trim( $matches[1], '`' ) ] = $qry; 
  1971. $for_update[$matches[1]] = 'Created table '.$matches[1]; 
  1972. } elseif ( preg_match( "|CREATE DATABASE ([^ ]*)|", $qry, $matches ) ) { 
  1973. array_unshift( $cqueries, $qry ); 
  1974. } elseif ( preg_match( "|INSERT INTO ([^ ]*)|", $qry, $matches ) ) { 
  1975. $iqueries[] = $qry; 
  1976. } elseif ( preg_match( "|UPDATE ([^ ]*)|", $qry, $matches ) ) { 
  1977. $iqueries[] = $qry; 
  1978. } else { 
  1979. // Unrecognized query type 
  1980.  
  1981. /** 
  1982. * Filters the dbDelta SQL queries for creating tables and/or databases. 
  1983. * 
  1984. * Queries filterable via this hook contain "CREATE TABLE" or "CREATE DATABASE". 
  1985. * 
  1986. * @since 3.3.0 
  1987. * 
  1988. * @param array $cqueries An array of dbDelta create SQL queries. 
  1989. */ 
  1990. $cqueries = apply_filters( 'dbdelta_create_queries', $cqueries ); 
  1991.  
  1992. /** 
  1993. * Filters the dbDelta SQL queries for inserting or updating. 
  1994. * 
  1995. * Queries filterable via this hook contain "INSERT INTO" or "UPDATE". 
  1996. * 
  1997. * @since 3.3.0 
  1998. * 
  1999. * @param array $iqueries An array of dbDelta insert or update SQL queries. 
  2000. */ 
  2001. $iqueries = apply_filters( 'dbdelta_insert_queries', $iqueries ); 
  2002.  
  2003. $text_fields = array( 'tinytext', 'text', 'mediumtext', 'longtext' ); 
  2004. $blob_fields = array( 'tinyblob', 'blob', 'mediumblob', 'longblob' ); 
  2005.  
  2006. $global_tables = $wpdb->tables( 'global' ); 
  2007. foreach ( $cqueries as $table => $qry ) { 
  2008. // Upgrade global tables only for the main site. Don't upgrade at all if conditions are not optimal. 
  2009. if ( in_array( $table, $global_tables ) && ! wp_should_upgrade_global_tables() ) { 
  2010. unset( $cqueries[ $table ], $for_update[ $table ] ); 
  2011. continue; 
  2012.  
  2013. // Fetch the table column structure from the database 
  2014. $suppress = $wpdb->suppress_errors(); 
  2015. $tablefields = $wpdb->get_results("DESCRIBE {$table};"); 
  2016. $wpdb->suppress_errors( $suppress ); 
  2017.  
  2018. if ( ! $tablefields ) 
  2019. continue; 
  2020.  
  2021. // Clear the field and index arrays. 
  2022. $cfields = $indices = array(); 
  2023.  
  2024. // Get all of the field names in the query from between the parentheses. 
  2025. preg_match("|\((.*)\)|ms", $qry, $match2); 
  2026. $qryline = trim($match2[1]); 
  2027.  
  2028. // Separate field lines into an array. 
  2029. $flds = explode("\n", $qryline); 
  2030.  
  2031. // For every field line specified in the query. 
  2032. foreach ( $flds as $fld ) { 
  2033. $fld = trim( $fld, " \t\n\r\0\x0B, " ); // Default trim characters, plus ', '. 
  2034.  
  2035. // Extract the field name. 
  2036. preg_match( '|^([^ ]*)|', $fld, $fvals ); 
  2037. $fieldname = trim( $fvals[1], '`' ); 
  2038. $fieldname_lowercased = strtolower( $fieldname ); 
  2039.  
  2040. // Verify the found field name. 
  2041. $validfield = true; 
  2042. switch ( $fieldname_lowercased ) { 
  2043. case '': 
  2044. case 'primary': 
  2045. case 'index': 
  2046. case 'fulltext': 
  2047. case 'unique': 
  2048. case 'key': 
  2049. case 'spatial': 
  2050. $validfield = false; 
  2051.  
  2052. /** 
  2053. * Normalize the index definition. 
  2054. * 
  2055. * This is done so the definition can be compared against the result of a 
  2056. * `SHOW INDEX FROM $table_name` query which returns the current table 
  2057. * index information. 
  2058. */ 
  2059.  
  2060. // Extract type, name and columns from the definition. 
  2061. preg_match( 
  2062. '/^' 
  2063. . '(?P<index_type>' // 1) Type of the index. 
  2064. . 'PRIMARY\s+KEY|(?:UNIQUE|FULLTEXT|SPATIAL)\s+(?:KEY|INDEX)|KEY|INDEX' 
  2065. . ')' 
  2066. . '\s+' // Followed by at least one white space character. 
  2067. . '(?:' // Name of the index. Optional if type is PRIMARY KEY. 
  2068. . '`?' // Name can be escaped with a backtick. 
  2069. . '(?P<index_name>' // 2) Name of the index. 
  2070. . '(?:[0-9a-zA-Z$_-]|[\xC2-\xDF][\x80-\xBF])+' 
  2071. . ')' 
  2072. . '`?' // Name can be escaped with a backtick. 
  2073. . '\s+' // Followed by at least one white space character. 
  2074. . ')*' 
  2075. . '\(' // Opening bracket for the columns. 
  2076. . '(?P<index_columns>' 
  2077. . '.+?' // 3) Column names, index prefixes, and orders. 
  2078. . ')' 
  2079. . '\)' // Closing bracket for the columns. 
  2080. . '$/im',  
  2081. $fld,  
  2082. $index_matches 
  2083. ); 
  2084.  
  2085. // Uppercase the index type and normalize space characters. 
  2086. $index_type = strtoupper( preg_replace( '/\s+/', ' ', trim( $index_matches['index_type'] ) ) ); 
  2087.  
  2088. // 'INDEX' is a synonym for 'KEY', standardize on 'KEY'. 
  2089. $index_type = str_replace( 'INDEX', 'KEY', $index_type ); 
  2090.  
  2091. // Escape the index name with backticks. An index for a primary key has no name. 
  2092. $index_name = ( 'PRIMARY KEY' === $index_type ) ? '' : '`' . strtolower( $index_matches['index_name'] ) . '`'; 
  2093.  
  2094. // Parse the columns. Multiple columns are separated by a comma. 
  2095. $index_columns = array_map( 'trim', explode( ', ', $index_matches['index_columns'] ) ); 
  2096.  
  2097. // Normalize columns. 
  2098. foreach ( $index_columns as &$index_column ) { 
  2099. // Extract column name and number of indexed characters (sub_part). 
  2100. preg_match( 
  2101. '/' 
  2102. . '`?' // Name can be escaped with a backtick. 
  2103. . '(?P<column_name>' // 1) Name of the column. 
  2104. . '(?:[0-9a-zA-Z$_-]|[\xC2-\xDF][\x80-\xBF])+' 
  2105. . ')' 
  2106. . '`?' // Name can be escaped with a backtick. 
  2107. . '(?:' // Optional sub part. 
  2108. . '\s*' // Optional white space character between name and opening bracket. 
  2109. . '\(' // Opening bracket for the sub part. 
  2110. . '\s*' // Optional white space character after opening bracket. 
  2111. . '(?P<sub_part>' 
  2112. . '\d+' // 2) Number of indexed characters. 
  2113. . ')' 
  2114. . '\s*' // Optional white space character before closing bracket. 
  2115. . '\)' // Closing bracket for the sub part. 
  2116. . ')?' 
  2117. . '/',  
  2118. $index_column,  
  2119. $index_column_matches 
  2120. ); 
  2121.  
  2122. // Escape the column name with backticks. 
  2123. $index_column = '`' . $index_column_matches['column_name'] . '`'; 
  2124.  
  2125. // Append the optional sup part with the number of indexed characters. 
  2126. if ( isset( $index_column_matches['sub_part'] ) ) { 
  2127. $index_column .= '(' . $index_column_matches['sub_part'] . ')'; 
  2128.  
  2129. // Build the normalized index definition and add it to the list of indices. 
  2130. $indices[] = "{$index_type} {$index_name} (" . implode( ', ', $index_columns ) . ")"; 
  2131.  
  2132. // Destroy no longer needed variables. 
  2133. unset( $index_column, $index_column_matches, $index_matches, $index_type, $index_name, $index_columns ); 
  2134.  
  2135. break; 
  2136.  
  2137. // If it's a valid field, add it to the field array. 
  2138. if ( $validfield ) { 
  2139. $cfields[ $fieldname_lowercased ] = $fld; 
  2140.  
  2141. // For every field in the table. 
  2142. foreach ( $tablefields as $tablefield ) { 
  2143. $tablefield_field_lowercased = strtolower( $tablefield->Field ); 
  2144. $tablefield_type_lowercased = strtolower( $tablefield->Type ); 
  2145.  
  2146. // If the table field exists in the field array ... 
  2147. if ( array_key_exists( $tablefield_field_lowercased, $cfields ) ) { 
  2148.  
  2149. // Get the field type from the query. 
  2150. preg_match( '|`?' . $tablefield->Field . '`? ([^ ]*( unsigned)?)|i', $cfields[ $tablefield_field_lowercased ], $matches ); 
  2151. $fieldtype = $matches[1]; 
  2152. $fieldtype_lowercased = strtolower( $fieldtype ); 
  2153.  
  2154. // Is actual field type different from the field type in query? 
  2155. if ($tablefield->Type != $fieldtype) { 
  2156. $do_change = true; 
  2157. if ( in_array( $fieldtype_lowercased, $text_fields ) && in_array( $tablefield_type_lowercased, $text_fields ) ) { 
  2158. if ( array_search( $fieldtype_lowercased, $text_fields ) < array_search( $tablefield_type_lowercased, $text_fields ) ) { 
  2159. $do_change = false; 
  2160.  
  2161. if ( in_array( $fieldtype_lowercased, $blob_fields ) && in_array( $tablefield_type_lowercased, $blob_fields ) ) { 
  2162. if ( array_search( $fieldtype_lowercased, $blob_fields ) < array_search( $tablefield_type_lowercased, $blob_fields ) ) { 
  2163. $do_change = false; 
  2164.  
  2165. if ( $do_change ) { 
  2166. // Add a query to change the column type. 
  2167. $cqueries[] = "ALTER TABLE {$table} CHANGE COLUMN `{$tablefield->Field}` " . $cfields[ $tablefield_field_lowercased ]; 
  2168. $for_update[$table.'.'.$tablefield->Field] = "Changed type of {$table}.{$tablefield->Field} from {$tablefield->Type} to {$fieldtype}"; 
  2169.  
  2170. // Get the default value from the array. 
  2171. if ( preg_match( "| DEFAULT '(.*?)'|i", $cfields[ $tablefield_field_lowercased ], $matches ) ) { 
  2172. $default_value = $matches[1]; 
  2173. if ($tablefield->Default != $default_value) { 
  2174. // Add a query to change the column's default value 
  2175. $cqueries[] = "ALTER TABLE {$table} ALTER COLUMN `{$tablefield->Field}` SET DEFAULT '{$default_value}'"; 
  2176. $for_update[$table.'.'.$tablefield->Field] = "Changed default value of {$table}.{$tablefield->Field} from {$tablefield->Default} to {$default_value}"; 
  2177.  
  2178. // Remove the field from the array (so it's not added). 
  2179. unset( $cfields[ $tablefield_field_lowercased ] ); 
  2180. } else { 
  2181. // This field exists in the table, but not in the creation queries? 
  2182.  
  2183. // For every remaining field specified for the table. 
  2184. foreach ($cfields as $fieldname => $fielddef) { 
  2185. // Push a query line into $cqueries that adds the field to that table. 
  2186. $cqueries[] = "ALTER TABLE {$table} ADD COLUMN $fielddef"; 
  2187. $for_update[$table.'.'.$fieldname] = 'Added column '.$table.'.'.$fieldname; 
  2188.  
  2189. // Index stuff goes here. Fetch the table index structure from the database. 
  2190. $tableindices = $wpdb->get_results("SHOW INDEX FROM {$table};"); 
  2191.  
  2192. if ($tableindices) { 
  2193. // Clear the index array. 
  2194. $index_ary = array(); 
  2195.  
  2196. // For every index in the table. 
  2197. foreach ($tableindices as $tableindex) { 
  2198.  
  2199. // Add the index to the index data array. 
  2200. $keyname = strtolower( $tableindex->Key_name ); 
  2201. $index_ary[$keyname]['columns'][] = array('fieldname' => $tableindex->Column_name, 'subpart' => $tableindex->Sub_part); 
  2202. $index_ary[$keyname]['unique'] = ($tableindex->Non_unique == 0)?true:false; 
  2203. $index_ary[$keyname]['index_type'] = $tableindex->Index_type; 
  2204.  
  2205. // For each actual index in the index array. 
  2206. foreach ($index_ary as $index_name => $index_data) { 
  2207.  
  2208. // Build a create string to compare to the query. 
  2209. $index_string = ''; 
  2210. if ($index_name == 'primary') { 
  2211. $index_string .= 'PRIMARY '; 
  2212. } elseif ( $index_data['unique'] ) { 
  2213. $index_string .= 'UNIQUE '; 
  2214. if ( 'FULLTEXT' === strtoupper( $index_data['index_type'] ) ) { 
  2215. $index_string .= 'FULLTEXT '; 
  2216. if ( 'SPATIAL' === strtoupper( $index_data['index_type'] ) ) { 
  2217. $index_string .= 'SPATIAL '; 
  2218. $index_string .= 'KEY '; 
  2219. if ( 'primary' !== $index_name ) { 
  2220. $index_string .= '`' . $index_name . '`'; 
  2221. $index_columns = ''; 
  2222.  
  2223. // For each column in the index. 
  2224. foreach ($index_data['columns'] as $column_data) { 
  2225. if ( $index_columns != '' ) { 
  2226. $index_columns .= ', '; 
  2227.  
  2228. // Add the field to the column list string. 
  2229. $index_columns .= '`' . $column_data['fieldname'] . '`'; 
  2230. if ($column_data['subpart'] != '') { 
  2231. $index_columns .= '('.$column_data['subpart'].')'; 
  2232.  
  2233. // The alternative index string doesn't care about subparts 
  2234. $alt_index_columns = preg_replace( '/\([^)]*\)/', '', $index_columns ); 
  2235.  
  2236. // Add the column list to the index create string. 
  2237. $index_strings = array( 
  2238. "$index_string ($index_columns)",  
  2239. "$index_string ($alt_index_columns)",  
  2240. ); 
  2241.  
  2242. foreach ( $index_strings as $index_string ) { 
  2243. if ( ! ( ( $aindex = array_search( $index_string, $indices ) ) === false ) ) { 
  2244. unset( $indices[ $aindex ] ); 
  2245. break; 
  2246.  
  2247. // For every remaining index specified for the table. 
  2248. foreach ( (array) $indices as $index ) { 
  2249. // Push a query line into $cqueries that adds the index to that table. 
  2250. $cqueries[] = "ALTER TABLE {$table} ADD $index"; 
  2251. $for_update[] = 'Added index ' . $table . ' ' . $index; 
  2252.  
  2253. // Remove the original table creation query from processing. 
  2254. unset( $cqueries[ $table ], $for_update[ $table ] ); 
  2255.  
  2256. $allqueries = array_merge($cqueries, $iqueries); 
  2257. if ($execute) { 
  2258. foreach ($allqueries as $query) { 
  2259. $wpdb->query($query); 
  2260.  
  2261. return $for_update; 
  2262.  
  2263. /** 
  2264. * Updates the database tables to a new schema. 
  2265. * 
  2266. * By default, updates all the tables to use the latest defined schema, but can also 
  2267. * be used to update a specific set of tables in wp_get_db_schema(). 
  2268. * 
  2269. * @since 1.5.0 
  2270. * 
  2271. * @uses dbDelta 
  2272. * 
  2273. * @param string $tables Optional. Which set of tables to update. Default is 'all'. 
  2274. */ 
  2275. function make_db_current( $tables = 'all' ) { 
  2276. $alterations = dbDelta( $tables ); 
  2277. echo "<ol>\n"; 
  2278. foreach ($alterations as $alteration) echo "<li>$alteration</li>\n"; 
  2279. echo "</ol>\n"; 
  2280.  
  2281. /** 
  2282. * Updates the database tables to a new schema, but without displaying results. 
  2283. * 
  2284. * By default, updates all the tables to use the latest defined schema, but can 
  2285. * also be used to update a specific set of tables in wp_get_db_schema(). 
  2286. * 
  2287. * @since 1.5.0 
  2288. * 
  2289. * @see make_db_current() 
  2290. * 
  2291. * @param string $tables Optional. Which set of tables to update. Default is 'all'. 
  2292. */ 
  2293. function make_db_current_silent( $tables = 'all' ) { 
  2294. dbDelta( $tables ); 
  2295.  
  2296. /** 
  2297. * Creates a site theme from an existing theme. 
  2298. * 
  2299. * {@internal Missing Long Description}} 
  2300. * 
  2301. * @since 1.5.0 
  2302. * 
  2303. * @param string $theme_name The name of the theme. 
  2304. * @param string $template The directory name of the theme. 
  2305. * @return bool 
  2306. */ 
  2307. function make_site_theme_from_oldschool($theme_name, $template) { 
  2308. $home_path = get_home_path(); 
  2309. $site_dir = WP_CONTENT_DIR . "/themes/$template"; 
  2310.  
  2311. if (! file_exists("$home_path/index.php")) 
  2312. return false; 
  2313.  
  2314. /** 
  2315. * Copy files from the old locations to the site theme. 
  2316. * TODO: This does not copy arbitrary include dependencies. Only the standard WP files are copied. 
  2317. */ 
  2318. $files = array('index.php' => 'index.php', 'wp-layout.css' => 'style.css', 'wp-comments.php' => 'comments.php', 'wp-comments-popup.php' => 'comments-popup.php'); 
  2319.  
  2320. foreach ($files as $oldfile => $newfile) { 
  2321. if ($oldfile == 'index.php') 
  2322. $oldpath = $home_path; 
  2323. else 
  2324. $oldpath = ABSPATH; 
  2325.  
  2326. // Check to make sure it's not a new index. 
  2327. if ($oldfile == 'index.php') { 
  2328. $index = implode('', file("$oldpath/$oldfile")); 
  2329. if (strpos($index, 'WP_USE_THEMES') !== false) { 
  2330. if (! @copy(WP_CONTENT_DIR . '/themes/' . WP_DEFAULT_THEME . '/index.php', "$site_dir/$newfile")) 
  2331. return false; 
  2332.  
  2333. // Don't copy anything. 
  2334. continue; 
  2335.  
  2336. if (! @copy("$oldpath/$oldfile", "$site_dir/$newfile")) 
  2337. return false; 
  2338.  
  2339. chmod("$site_dir/$newfile", 0777); 
  2340.  
  2341. // Update the blog header include in each file. 
  2342. $lines = explode("\n", implode('', file("$site_dir/$newfile"))); 
  2343. if ($lines) { 
  2344. $f = fopen("$site_dir/$newfile", 'w'); 
  2345.  
  2346. foreach ($lines as $line) { 
  2347. if (preg_match('/require.*wp-blog-header/', $line)) 
  2348. $line = '//' . $line; 
  2349.  
  2350. // Update stylesheet references. 
  2351. $line = str_replace("<?php echo __get_option('siteurl'); ?>/wp-layout.css", "<?php bloginfo('stylesheet_url'); ?>", $line); 
  2352.  
  2353. // Update comments template inclusion. 
  2354. $line = str_replace("<?php include(ABSPATH . 'wp-comments.php'); ?>", "<?php comments_template(); ?>", $line); 
  2355.  
  2356. fwrite($f, "{$line}\n"); 
  2357. fclose($f); 
  2358.  
  2359. // Add a theme header. 
  2360. $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"; 
  2361.  
  2362. $stylelines = file_get_contents("$site_dir/style.css"); 
  2363. if ($stylelines) { 
  2364. $f = fopen("$site_dir/style.css", 'w'); 
  2365.  
  2366. fwrite($f, $header); 
  2367. fwrite($f, $stylelines); 
  2368. fclose($f); 
  2369.  
  2370. return true; 
  2371.  
  2372. /** 
  2373. * Creates a site theme from the default theme. 
  2374. * 
  2375. * {@internal Missing Long Description}} 
  2376. * 
  2377. * @since 1.5.0 
  2378. * 
  2379. * @param string $theme_name The name of the theme. 
  2380. * @param string $template The directory name of the theme. 
  2381. * @return false|void 
  2382. */ 
  2383. function make_site_theme_from_default($theme_name, $template) { 
  2384. $site_dir = WP_CONTENT_DIR . "/themes/$template"; 
  2385. $default_dir = WP_CONTENT_DIR . '/themes/' . WP_DEFAULT_THEME; 
  2386.  
  2387. // Copy files from the default theme to the site theme. 
  2388. //$files = array('index.php', 'comments.php', 'comments-popup.php', 'footer.php', 'header.php', 'sidebar.php', 'style.css'); 
  2389.  
  2390. $theme_dir = @ opendir($default_dir); 
  2391. if ($theme_dir) { 
  2392. while(($theme_file = readdir( $theme_dir )) !== false) { 
  2393. if (is_dir("$default_dir/$theme_file")) 
  2394. continue; 
  2395. if (! @copy("$default_dir/$theme_file", "$site_dir/$theme_file")) 
  2396. return; 
  2397. chmod("$site_dir/$theme_file", 0777); 
  2398. @closedir($theme_dir); 
  2399.  
  2400. // Rewrite the theme header. 
  2401. $stylelines = explode("\n", implode('', file("$site_dir/style.css"))); 
  2402. if ($stylelines) { 
  2403. $f = fopen("$site_dir/style.css", 'w'); 
  2404.  
  2405. foreach ($stylelines as $line) { 
  2406. if (strpos($line, 'Theme Name:') !== false) $line = 'Theme Name: ' . $theme_name; 
  2407. elseif (strpos($line, 'Theme URI:') !== false) $line = 'Theme URI: ' . __get_option('url'); 
  2408. elseif (strpos($line, 'Description:') !== false) $line = 'Description: Your theme.'; 
  2409. elseif (strpos($line, 'Version:') !== false) $line = 'Version: 1'; 
  2410. elseif (strpos($line, 'Author:') !== false) $line = 'Author: You'; 
  2411. fwrite($f, $line . "\n"); 
  2412. fclose($f); 
  2413.  
  2414. // Copy the images. 
  2415. umask(0); 
  2416. if (! mkdir("$site_dir/images", 0777)) { 
  2417. return false; 
  2418.  
  2419. $images_dir = @ opendir("$default_dir/images"); 
  2420. if ($images_dir) { 
  2421. while(($image = readdir($images_dir)) !== false) { 
  2422. if (is_dir("$default_dir/images/$image")) 
  2423. continue; 
  2424. if (! @copy("$default_dir/images/$image", "$site_dir/images/$image")) 
  2425. return; 
  2426. chmod("$site_dir/images/$image", 0777); 
  2427. @closedir($images_dir); 
  2428.  
  2429. /** 
  2430. * Creates a site theme. 
  2431. * 
  2432. * {@internal Missing Long Description}} 
  2433. * 
  2434. * @since 1.5.0 
  2435. * 
  2436. * @return false|string 
  2437. */ 
  2438. function make_site_theme() { 
  2439. // Name the theme after the blog. 
  2440. $theme_name = __get_option('blogname'); 
  2441. $template = sanitize_title($theme_name); 
  2442. $site_dir = WP_CONTENT_DIR . "/themes/$template"; 
  2443.  
  2444. // If the theme already exists, nothing to do. 
  2445. if ( is_dir($site_dir)) { 
  2446. return false; 
  2447.  
  2448. // We must be able to write to the themes dir. 
  2449. if (! is_writable(WP_CONTENT_DIR . "/themes")) { 
  2450. return false; 
  2451.  
  2452. umask(0); 
  2453. if (! mkdir($site_dir, 0777)) { 
  2454. return false; 
  2455.  
  2456. if (file_exists(ABSPATH . 'wp-layout.css')) { 
  2457. if (! make_site_theme_from_oldschool($theme_name, $template)) { 
  2458. // TODO: rm -rf the site theme directory. 
  2459. return false; 
  2460. } else { 
  2461. if (! make_site_theme_from_default($theme_name, $template)) 
  2462. // TODO: rm -rf the site theme directory. 
  2463. return false; 
  2464.  
  2465. // Make the new site theme active. 
  2466. $current_template = __get_option('template'); 
  2467. if ($current_template == WP_DEFAULT_THEME) { 
  2468. update_option('template', $template); 
  2469. update_option('stylesheet', $template); 
  2470. return $template; 
  2471.  
  2472. /** 
  2473. * Translate user level to user role name. 
  2474. * 
  2475. * @since 2.0.0 
  2476. * 
  2477. * @param int $level User level. 
  2478. * @return string User role name. 
  2479. */ 
  2480. function translate_level_to_role($level) { 
  2481. switch ($level) { 
  2482. case 10: 
  2483. case 9: 
  2484. case 8: 
  2485. return 'administrator'; 
  2486. case 7: 
  2487. case 6: 
  2488. case 5: 
  2489. return 'editor'; 
  2490. case 4: 
  2491. case 3: 
  2492. case 2: 
  2493. return 'author'; 
  2494. case 1: 
  2495. return 'contributor'; 
  2496. case 0: 
  2497. return 'subscriber'; 
  2498.  
  2499. /** 
  2500. * Checks the version of the installed MySQL binary. 
  2501. * 
  2502. * @since 2.1.0 
  2503. * 
  2504. * @global wpdb $wpdb 
  2505. */ 
  2506. function wp_check_mysql_version() { 
  2507. global $wpdb; 
  2508. $result = $wpdb->check_database_version(); 
  2509. if ( is_wp_error( $result ) ) 
  2510. die( $result->get_error_message() ); 
  2511.  
  2512. /** 
  2513. * Disables the Automattic widgets plugin, which was merged into core. 
  2514. * 
  2515. * @since 2.2.0 
  2516. */ 
  2517. function maybe_disable_automattic_widgets() { 
  2518. $plugins = __get_option( 'active_plugins' ); 
  2519.  
  2520. foreach ( (array) $plugins as $plugin ) { 
  2521. if ( basename( $plugin ) == 'widgets.php' ) { 
  2522. array_splice( $plugins, array_search( $plugin, $plugins ), 1 ); 
  2523. update_option( 'active_plugins', $plugins ); 
  2524. break; 
  2525.  
  2526. /** 
  2527. * Disables the Link Manager on upgrade if, at the time of upgrade, no links exist in the DB. 
  2528. * 
  2529. * @since 3.5.0 
  2530. * 
  2531. * @global int $wp_current_db_version 
  2532. * @global wpdb $wpdb WordPress database abstraction object. 
  2533. */ 
  2534. function maybe_disable_link_manager() { 
  2535. global $wp_current_db_version, $wpdb; 
  2536.  
  2537. if ( $wp_current_db_version >= 22006 && get_option( 'link_manager_enabled' ) && ! $wpdb->get_var( "SELECT link_id FROM $wpdb->links LIMIT 1" ) ) 
  2538. update_option( 'link_manager_enabled', 0 ); 
  2539.  
  2540. /** 
  2541. * Runs before the schema is upgraded. 
  2542. * 
  2543. * @since 2.9.0 
  2544. * 
  2545. * @global int $wp_current_db_version 
  2546. * @global wpdb $wpdb WordPress database abstraction object. 
  2547. */ 
  2548. function pre_schema_upgrade() { 
  2549. global $wp_current_db_version, $wpdb; 
  2550.  
  2551. // Upgrade versions prior to 2.9 
  2552. if ( $wp_current_db_version < 11557 ) { 
  2553. // Delete duplicate options. Keep the option with the highest option_id. 
  2554. $wpdb->query("DELETE o1 FROM $wpdb->options AS o1 JOIN $wpdb->options AS o2 USING (`option_name`) WHERE o2.option_id > o1.option_id"); 
  2555.  
  2556. // Drop the old primary key and add the new. 
  2557. $wpdb->query("ALTER TABLE $wpdb->options DROP PRIMARY KEY, ADD PRIMARY KEY(option_id)"); 
  2558.  
  2559. // Drop the old option_name index. dbDelta() doesn't do the drop. 
  2560. $wpdb->query("ALTER TABLE $wpdb->options DROP INDEX option_name"); 
  2561.  
  2562. // Multisite schema upgrades. 
  2563. if ( $wp_current_db_version < 25448 && is_multisite() && wp_should_upgrade_global_tables() ) { 
  2564.  
  2565. // Upgrade versions prior to 3.7 
  2566. if ( $wp_current_db_version < 25179 ) { 
  2567. // New primary key for signups. 
  2568. $wpdb->query( "ALTER TABLE $wpdb->signups ADD signup_id BIGINT(20) NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST" ); 
  2569. $wpdb->query( "ALTER TABLE $wpdb->signups DROP INDEX domain" ); 
  2570.  
  2571. if ( $wp_current_db_version < 25448 ) { 
  2572. // Convert archived from enum to tinyint. 
  2573. $wpdb->query( "ALTER TABLE $wpdb->blogs CHANGE COLUMN archived archived varchar(1) NOT NULL default '0'" ); 
  2574. $wpdb->query( "ALTER TABLE $wpdb->blogs CHANGE COLUMN archived archived tinyint(2) NOT NULL default 0" ); 
  2575.  
  2576. // Upgrade versions prior to 4.2. 
  2577. if ( $wp_current_db_version < 31351 ) { 
  2578. if ( ! is_multisite() && wp_should_upgrade_global_tables() ) { 
  2579. $wpdb->query( "ALTER TABLE $wpdb->usermeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" ); 
  2580. $wpdb->query( "ALTER TABLE $wpdb->terms DROP INDEX slug, ADD INDEX slug(slug(191))" ); 
  2581. $wpdb->query( "ALTER TABLE $wpdb->terms DROP INDEX name, ADD INDEX name(name(191))" ); 
  2582. $wpdb->query( "ALTER TABLE $wpdb->commentmeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" ); 
  2583. $wpdb->query( "ALTER TABLE $wpdb->postmeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" ); 
  2584. $wpdb->query( "ALTER TABLE $wpdb->posts DROP INDEX post_name, ADD INDEX post_name(post_name(191))" ); 
  2585.  
  2586. // Upgrade versions prior to 4.4. 
  2587. if ( $wp_current_db_version < 34978 ) { 
  2588. // If compatible termmeta table is found, use it, but enforce a proper index and update collation. 
  2589. if ( $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->termmeta}'" ) && $wpdb->get_results( "SHOW INDEX FROM {$wpdb->termmeta} WHERE Column_name = 'meta_key'" ) ) { 
  2590. $wpdb->query( "ALTER TABLE $wpdb->termmeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" ); 
  2591. maybe_convert_table_to_utf8mb4( $wpdb->termmeta ); 
  2592.  
  2593. /** 
  2594. * Install global terms. 
  2595. * 
  2596. * @since 3.0.0 
  2597. * 
  2598. * @global wpdb $wpdb 
  2599. * @global string $charset_collate 
  2600. */ 
  2601. if ( !function_exists( 'install_global_terms' ) ) : 
  2602. function install_global_terms() { 
  2603. global $wpdb, $charset_collate; 
  2604. $ms_queries = " 
  2605. CREATE TABLE $wpdb->sitecategories ( 
  2606. cat_ID bigint(20) NOT NULL auto_increment,  
  2607. cat_name varchar(55) NOT NULL default '',  
  2608. category_nicename varchar(200) NOT NULL default '',  
  2609. last_updated timestamp NOT NULL,  
  2610. PRIMARY KEY (cat_ID),  
  2611. KEY category_nicename (category_nicename),  
  2612. KEY last_updated (last_updated) 
  2613. ) $charset_collate; 
  2614. "; 
  2615. // now create tables 
  2616. dbDelta( $ms_queries ); 
  2617. endif; 
  2618.  
  2619. /** 
  2620. * Determine if global tables should be upgraded. 
  2621. * 
  2622. * This function performs a series of checks to ensure the environment allows 
  2623. * for the safe upgrading of global WordPress database tables. It is necessary 
  2624. * because global tables will commonly grow to millions of rows on large 
  2625. * installations, and the ability to control their upgrade routines can be 
  2626. * critical to the operation of large networks. 
  2627. * 
  2628. * In a future iteration, this function may use `wp_is_large_network()` to more- 
  2629. * intelligently prevent global table upgrades. Until then, we make sure 
  2630. * WordPress is on the main site of the main network, to avoid running queries 
  2631. * more than once in multi-site or multi-network environments. 
  2632. * 
  2633. * @since 4.3.0 
  2634. * 
  2635. * @return bool Whether to run the upgrade routines on global tables. 
  2636. */ 
  2637. function wp_should_upgrade_global_tables() { 
  2638.  
  2639. // Return false early if explicitly not upgrading 
  2640. if ( defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) ) { 
  2641. return false; 
  2642.  
  2643. // Assume global tables should be upgraded 
  2644. $should_upgrade = true; 
  2645.  
  2646. // Set to false if not on main network (does not matter if not multi-network) 
  2647. if ( ! is_main_network() ) { 
  2648. $should_upgrade = false; 
  2649.  
  2650. // Set to false if not on main site of current network (does not matter if not multi-site) 
  2651. if ( ! is_main_site() ) { 
  2652. $should_upgrade = false; 
  2653.  
  2654. /** 
  2655. * Filters if upgrade routines should be run on global tables. 
  2656. * 
  2657. * @param bool $should_upgrade Whether to run the upgrade routines on global tables. 
  2658. */ 
  2659. return apply_filters( 'wp_should_upgrade_global_tables', $should_upgrade ); 
.