/includes/core/update.php

  1. <?php 
  2.  
  3. /** 
  4. * bbPress Updater 
  5. * 
  6. * @package bbPress 
  7. * @subpackage Updater 
  8. */ 
  9.  
  10. // Exit if accessed directly 
  11. if ( !defined( 'ABSPATH' ) ) exit; 
  12.  
  13. /** 
  14. * If there is no raw DB version, this is the first installation 
  15. * 
  16. * @since bbPress (r3764) 
  17. * 
  18. * @uses get_option() 
  19. * @uses bbp_get_db_version() To get bbPress's database version 
  20. * @return bool True if update, False if not 
  21. */ 
  22. function bbp_is_install() { 
  23. return ! bbp_get_db_version_raw(); 
  24.  
  25. /** 
  26. * Compare the bbPress version to the DB version to determine if updating 
  27. * 
  28. * @since bbPress (r3421) 
  29. * 
  30. * @uses get_option() 
  31. * @uses bbp_get_db_version() To get bbPress's database version 
  32. * @return bool True if update, False if not 
  33. */ 
  34. function bbp_is_update() { 
  35. $raw = (int) bbp_get_db_version_raw(); 
  36. $cur = (int) bbp_get_db_version(); 
  37. $retval = (bool) ( $raw < $cur ); 
  38. return $retval; 
  39.  
  40. /** 
  41. * Determine if bbPress is being activated 
  42. * 
  43. * Note that this function currently is not used in bbPress core and is here 
  44. * for third party plugins to use to check for bbPress activation. 
  45. * 
  46. * @since bbPress (r3421) 
  47. * 
  48. * @return bool True if activating bbPress, false if not 
  49. */ 
  50. function bbp_is_activation( $basename = '' ) { 
  51. global $pagenow; 
  52.  
  53. $bbp = bbpress(); 
  54. $action = false; 
  55.  
  56. // Bail if not in admin/plugins 
  57. if ( ! ( is_admin() && ( 'plugins.php' === $pagenow ) ) ) { 
  58. return false; 
  59.  
  60. if ( ! empty( $_REQUEST['action'] ) && ( '-1' !== $_REQUEST['action'] ) ) { 
  61. $action = $_REQUEST['action']; 
  62. } elseif ( ! empty( $_REQUEST['action2'] ) && ( '-1' !== $_REQUEST['action2'] ) ) { 
  63. $action = $_REQUEST['action2']; 
  64.  
  65. // Bail if not activating 
  66. if ( empty( $action ) || !in_array( $action, array( 'activate', 'activate-selected' ) ) ) { 
  67. return false; 
  68.  
  69. // The plugin(s) being activated 
  70. if ( $action === 'activate' ) { 
  71. $plugins = isset( $_GET['plugin'] ) ? array( $_GET['plugin'] ) : array(); 
  72. } else { 
  73. $plugins = isset( $_POST['checked'] ) ? (array) $_POST['checked'] : array(); 
  74.  
  75. // Set basename if empty 
  76. if ( empty( $basename ) && !empty( $bbp->basename ) ) { 
  77. $basename = $bbp->basename; 
  78.  
  79. // Bail if no basename 
  80. if ( empty( $basename ) ) { 
  81. return false; 
  82.  
  83. // Is bbPress being activated? 
  84. return in_array( $basename, $plugins ); 
  85.  
  86. /** 
  87. * Determine if bbPress is being deactivated 
  88. * 
  89. * @since bbPress (r3421) 
  90. * @return bool True if deactivating bbPress, false if not 
  91. */ 
  92. function bbp_is_deactivation( $basename = '' ) { 
  93. global $pagenow; 
  94.  
  95. $bbp = bbpress(); 
  96. $action = false; 
  97.  
  98. // Bail if not in admin/plugins 
  99. if ( ! ( is_admin() && ( 'plugins.php' === $pagenow ) ) ) { 
  100. return false; 
  101.  
  102. if ( ! empty( $_REQUEST['action'] ) && ( '-1' !== $_REQUEST['action'] ) ) { 
  103. $action = $_REQUEST['action']; 
  104. } elseif ( ! empty( $_REQUEST['action2'] ) && ( '-1' !== $_REQUEST['action2'] ) ) { 
  105. $action = $_REQUEST['action2']; 
  106.  
  107. // Bail if not deactivating 
  108. if ( empty( $action ) || !in_array( $action, array( 'deactivate', 'deactivate-selected' ) ) ) { 
  109. return false; 
  110.  
  111. // The plugin(s) being deactivated 
  112. if ( $action === 'deactivate' ) { 
  113. $plugins = isset( $_GET['plugin'] ) ? array( $_GET['plugin'] ) : array(); 
  114. } else { 
  115. $plugins = isset( $_POST['checked'] ) ? (array) $_POST['checked'] : array(); 
  116.  
  117. // Set basename if empty 
  118. if ( empty( $basename ) && !empty( $bbp->basename ) ) { 
  119. $basename = $bbp->basename; 
  120.  
  121. // Bail if no basename 
  122. if ( empty( $basename ) ) { 
  123. return false; 
  124.  
  125. // Is bbPress being deactivated? 
  126. return in_array( $basename, $plugins ); 
  127.  
  128. /** 
  129. * Update the DB to the latest version 
  130. * 
  131. * @since bbPress (r3421) 
  132. * @uses update_option() 
  133. * @uses bbp_get_db_version() To get bbPress's database version 
  134. */ 
  135. function bbp_version_bump() { 
  136. update_option( '_bbp_db_version', bbp_get_db_version() ); 
  137.  
  138. /** 
  139. * Setup the bbPress updater 
  140. * 
  141. * @since bbPress (r3419) 
  142. * 
  143. * @uses bbp_version_updater() 
  144. * @uses bbp_version_bump() 
  145. * @uses flush_rewrite_rules() 
  146. */ 
  147. function bbp_setup_updater() { 
  148.  
  149. // Bail if no update needed 
  150. if ( ! bbp_is_update() ) 
  151. return; 
  152.  
  153. // Call the automated updater 
  154. bbp_version_updater(); 
  155.  
  156. /** 
  157. * Create a default forum, topic, and reply 
  158. * 
  159. * @since bbPress (r3767) 
  160. * @param array $args Array of arguments to override default values 
  161. */ 
  162. function bbp_create_initial_content( $args = array() ) { 
  163.  
  164. // Parse arguments against default values 
  165. $r = bbp_parse_args( $args, array( 
  166. 'forum_parent' => 0,  
  167. 'forum_status' => 'publish',  
  168. 'forum_title' => __( 'General', 'bbpress' ),  
  169. 'forum_content' => __( 'General chit-chat', 'bbpress' ),  
  170. 'topic_title' => __( 'Hello World!', 'bbpress' ),  
  171. 'topic_content' => __( 'I am the first topic in your new forums.', 'bbpress' ),  
  172. 'reply_title' => __( 'Re: Hello World!', 'bbpress' ),  
  173. 'reply_content' => __( 'Oh, and this is what a reply looks like.', 'bbpress' ),  
  174. ), 'create_initial_content' ); 
  175.  
  176. // Create the initial forum 
  177. $forum_id = bbp_insert_forum( array( 
  178. 'post_parent' => $r['forum_parent'],  
  179. 'post_status' => $r['forum_status'],  
  180. 'post_title' => $r['forum_title'],  
  181. 'post_content' => $r['forum_content'] 
  182. ) ); 
  183.  
  184. // Create the initial topic 
  185. $topic_id = bbp_insert_topic( 
  186. array( 
  187. 'post_parent' => $forum_id,  
  188. 'post_title' => $r['topic_title'],  
  189. 'post_content' => $r['topic_content'] 
  190. ),  
  191. array( 'forum_id' => $forum_id ) 
  192. ); 
  193.  
  194. // Create the initial reply 
  195. $reply_id = bbp_insert_reply( 
  196. array( 
  197. 'post_parent' => $topic_id,  
  198. 'post_title' => $r['reply_title'],  
  199. 'post_content' => $r['reply_content'] 
  200. ),  
  201. array( 
  202. 'forum_id' => $forum_id,  
  203. 'topic_id' => $topic_id 
  204. ); 
  205.  
  206. return array( 
  207. 'forum_id' => $forum_id,  
  208. 'topic_id' => $topic_id,  
  209. 'reply_id' => $reply_id 
  210. ); 
  211.  
  212. /** 
  213. * bbPress's version updater looks at what the current database version is, and 
  214. * runs whatever other code is needed. 
  215. * 
  216. * This is most-often used when the data schema changes, but should also be used 
  217. * to correct issues with bbPress meta-data silently on software update. 
  218. * 
  219. * @since bbPress (r4104) 
  220. */ 
  221. function bbp_version_updater() { 
  222.  
  223. // Get the raw database version 
  224. $raw_db_version = (int) bbp_get_db_version_raw(); 
  225.  
  226. /** 2.0 Branch ************************************************************/ 
  227.  
  228. // 2.0, 2.0.1, 2.0.2, 2.0.3 
  229. if ( $raw_db_version < 200 ) { 
  230. // No changes 
  231.  
  232. /** 2.1 Branch ************************************************************/ 
  233.  
  234. // 2.1, 2.1.1 
  235. if ( $raw_db_version < 211 ) { 
  236.  
  237. /** 
  238. * Repair private and hidden forum data 
  239. * 
  240. * @link http://bbpress.trac.wordpress.org/ticket/1891 
  241. */ 
  242. bbp_admin_repair_forum_visibility(); 
  243.  
  244. /** 2.2 Branch ************************************************************/ 
  245.  
  246. // 2.2 
  247. if ( $raw_db_version < 220 ) { 
  248.  
  249. // Remove the Moderator role from the database 
  250. remove_role( bbp_get_moderator_role() ); 
  251.  
  252. // Remove the Participant role from the database 
  253. remove_role( bbp_get_participant_role() ); 
  254.  
  255. // Remove capabilities 
  256. bbp_remove_caps(); 
  257.  
  258. /** 2.3 Branch ************************************************************/ 
  259.  
  260. // 2.3 
  261. if ( $raw_db_version < 230 ) { 
  262. // No changes 
  263.  
  264. /** All done! *************************************************************/ 
  265.  
  266. // Bump the version 
  267. bbp_version_bump(); 
  268.  
  269. // Delete rewrite rules to force a flush 
  270. bbp_delete_rewrite_rules(); 
  271.  
  272. /** 
  273. * Redirect user to bbPress's What's New page on activation 
  274. * 
  275. * @since bbPress (r4389) 
  276. * 
  277. * @internal Used internally to redirect bbPress to the about page on activation 
  278. * 
  279. * @uses is_network_admin() To bail if being network activated 
  280. * @uses set_transient() To drop the activation transient for 30 seconds 
  281. * 
  282. * @return If network admin or bulk activation 
  283. */ 
  284. function bbp_add_activation_redirect() { 
  285.  
  286. // Bail if activating from network, or bulk 
  287. if ( is_network_admin() || isset( $_GET['activate-multi'] ) ) 
  288. return; 
  289.  
  290. // Add the transient to redirect 
  291. set_transient( '_bbp_activation_redirect', true, 30 ); 
  292.  
  293. /** 
  294. * Hooked to the 'bbp_activate' action, this helper function automatically makes 
  295. * the current user a Key Master in the forums if they just activated bbPress,  
  296. * regardless of the bbp_allow_global_access() setting. 
  297. * 
  298. * @since bbPress (r4910) 
  299. * 
  300. * @internal Used to internally make the current user a keymaster on activation 
  301. * 
  302. * @uses current_user_can() to bail if user cannot activate plugins 
  303. * @uses get_current_user_id() to get the current user ID 
  304. * @uses get_current_blog_id() to get the current blog ID 
  305. * @uses is_user_member_of_blog() to bail if the current user does not have a role 
  306. * @uses bbp_is_user_keymaster() to bail if the user is already a keymaster 
  307. * @uses bbp_set_user_role() to make the current user a keymaster 
  308. * 
  309. * @return If user can't activate plugins or is already a keymaster 
  310. */ 
  311. function bbp_make_current_user_keymaster() { 
  312.  
  313. // Bail if the current user can't activate plugins since previous pageload 
  314. if ( ! current_user_can( 'activate_plugins' ) ) { 
  315. return; 
  316.  
  317. // Get the current user ID 
  318. $user_id = get_current_user_id(); 
  319. $blog_id = get_current_blog_id(); 
  320.  
  321. // Bail if user is not actually a member of this site 
  322. if ( ! is_user_member_of_blog( $user_id, $blog_id ) ) { 
  323. return; 
  324.  
  325. // Bail if the current user already has a forum role to prevent 
  326. // unexpected role and capability escalation. 
  327. if ( bbp_get_user_role( $user_id ) ) { 
  328. return; 
  329.  
  330. // Make the current user a keymaster 
  331. bbp_set_user_role( $user_id, bbp_get_keymaster_role() ); 
  332.  
  333. // Reload the current user so caps apply immediately 
  334. wp_get_current_user(); 
.