PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/bbpress/includes/core/update.php

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