| @@ -3,21 +3,19 @@ | ||
| 3 | 3 | /** |
| 4 | 4 | * bbPress Updater |
| 5 | 5 | * |
| 6 | 6 | * @package bbPress |
| 7 | - * @subpackage Updater | |
| 7 | + * @subpackage Core | |
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | 10 | // Exit if accessed directly |
| 11 | -if ( !defined( 'ABSPATH' ) ) exit; | |
| 11 | +defined( 'ABSPATH' ) || exit; | |
| 12 | 12 | |
| 13 | 13 | /** |
| 14 | 14 | * If there is no raw DB version, this is the first installation |
| 15 | 15 | * |
| 16 | - * @since bbPress (r3764) | |
| 16 | + * @since 2.1.0 bbPress (r3764) | |
| 17 | 17 | * |
| 18 | - * @uses get_option() | |
| 19 | - * @uses bbp_get_db_version() To get bbPress's database version | |
| 20 | 18 | * @return bool True if update, False if not |
| 21 | 19 | */ |
| 22 | 20 | function bbp_is_install() { |
| 23 | 21 | return ! bbp_get_db_version_raw(); |
| @@ -25,12 +23,10 @@ | ||
| 25 | 23 | |
| 26 | 24 | /** |
| 27 | 25 | * Compare the bbPress version to the DB version to determine if updating |
| 28 | 26 | * |
| 29 | - * @since bbPress (r3421) | |
| 27 | + * @since 2.0.0 bbPress (r3421) | |
| 30 | 28 | * |
| 31 | - * @uses get_option() | |
| 32 | - * @uses bbp_get_db_version() To get bbPress's database version | |
| 33 | 29 | * @return bool True if update, False if not |
| 34 | 30 | */ |
| 35 | 31 | function bbp_is_update() { |
| 36 | 32 | $raw = (int) bbp_get_db_version_raw(); |
| @@ -44,27 +40,36 @@ | ||
| 44 | 40 | * |
| 45 | 41 | * Note that this function currently is not used in bbPress core and is here |
| 46 | 42 | * for third party plugins to use to check for bbPress activation. |
| 47 | 43 | * |
| 48 | - * @since bbPress (r3421) | |
| 44 | + * @since 2.0.0 bbPress (r3421) | |
| 49 | 45 | * |
| 50 | 46 | * @return bool True if activating bbPress, false if not |
| 51 | 47 | */ |
| 52 | 48 | function bbp_is_activation( $basename = '' ) { |
| 53 | - $bbp = bbpress(); | |
| 49 | + global $pagenow; | |
| 54 | 50 | |
| 51 | + $bbp = bbpress(); | |
| 55 | 52 | $action = false; |
| 56 | - if ( ! empty( $_REQUEST['action'] ) && ( '-1' != $_REQUEST['action'] ) ) | |
| 53 | + | |
| 54 | + // Bail if not in admin/plugins | |
| 55 | + if ( ! ( is_admin() && ( 'plugins.php' === $pagenow ) ) ) { | |
| 56 | + return false; | |
| 57 | + } | |
| 58 | + | |
| 59 | + if ( ! empty( $_REQUEST['action'] ) && ( '-1' !== $_REQUEST['action'] ) ) { | |
| 57 | 60 | $action = $_REQUEST['action']; |
| 58 | - elseif ( ! empty( $_REQUEST['action2'] ) && ( '-1' != $_REQUEST['action2'] ) ) | |
| 61 | + } elseif ( ! empty( $_REQUEST['action2'] ) && ( '-1' !== $_REQUEST['action2'] ) ) { | |
| 59 | 62 | $action = $_REQUEST['action2']; |
| 63 | + } | |
| 60 | 64 | |
| 61 | 65 | // Bail if not activating |
| 62 | - if ( empty( $action ) || !in_array( $action, array( 'activate', 'activate-selected' ) ) ) | |
| 66 | + if ( empty( $action ) || ! in_array( $action, array( 'activate', 'activate-selected', true ) ) ) { | |
| 63 | 67 | return false; |
| 68 | + } | |
| 64 | 69 | |
| 65 | 70 | // The plugin(s) being activated |
| 66 | - if ( $action == 'activate' ) { | |
| 71 | + if ( $action === 'activate' ) { | |
| 67 | 72 | $plugins = isset( $_GET['plugin'] ) ? array( $_GET['plugin'] ) : array(); |
| 68 | 73 | } else { |
| 69 | 74 | $plugins = isset( $_POST['checked'] ) ? (array) $_POST['checked'] : array(); |
| 70 | 75 | } |
| @@ -69,40 +74,52 @@ | ||
| 69 | 74 | $plugins = isset( $_POST['checked'] ) ? (array) $_POST['checked'] : array(); |
| 70 | 75 | } |
| 71 | 76 | |
| 72 | 77 | // Set basename if empty |
| 73 | - if ( empty( $basename ) && !empty( $bbp->basename ) ) | |
| 78 | + if ( empty( $basename ) && ! empty( $bbp->basename ) ) { | |
| 74 | 79 | $basename = $bbp->basename; |
| 80 | + } | |
| 75 | 81 | |
| 76 | 82 | // Bail if no basename |
| 77 | - if ( empty( $basename ) ) | |
| 83 | + if ( empty( $basename ) ) { | |
| 78 | 84 | return false; |
| 85 | + } | |
| 79 | 86 | |
| 80 | 87 | // Is bbPress being activated? |
| 81 | - return in_array( $basename, $plugins ); | |
| 88 | + return in_array( $basename, $plugins, true ); | |
| 82 | 89 | } |
| 83 | 90 | |
| 84 | 91 | /** |
| 85 | 92 | * Determine if bbPress is being deactivated |
| 86 | 93 | * |
| 87 | - * @since bbPress (r3421) | |
| 94 | + * @since 2.0.0 bbPress (r3421) | |
| 95 | + * | |
| 88 | 96 | * @return bool True if deactivating bbPress, false if not |
| 89 | 97 | */ |
| 90 | 98 | function bbp_is_deactivation( $basename = '' ) { |
| 91 | - $bbp = bbpress(); | |
| 99 | + global $pagenow; | |
| 92 | 100 | |
| 101 | + $bbp = bbpress(); | |
| 93 | 102 | $action = false; |
| 94 | - if ( ! empty( $_REQUEST['action'] ) && ( '-1' != $_REQUEST['action'] ) ) | |
| 103 | + | |
| 104 | + // Bail if not in admin/plugins | |
| 105 | + if ( ! ( is_admin() && ( 'plugins.php' === $pagenow ) ) ) { | |
| 106 | + return false; | |
| 107 | + } | |
| 108 | + | |
| 109 | + if ( ! empty( $_REQUEST['action'] ) && ( '-1' !== $_REQUEST['action'] ) ) { | |
| 95 | 110 | $action = $_REQUEST['action']; |
| 96 | - elseif ( ! empty( $_REQUEST['action2'] ) && ( '-1' != $_REQUEST['action2'] ) ) | |
| 111 | + } elseif ( ! empty( $_REQUEST['action2'] ) && ( '-1' !== $_REQUEST['action2'] ) ) { | |
| 97 | 112 | $action = $_REQUEST['action2']; |
| 113 | + } | |
| 98 | 114 | |
| 99 | 115 | // Bail if not deactivating |
| 100 | - if ( empty( $action ) || !in_array( $action, array( 'deactivate', 'deactivate-selected' ) ) ) | |
| 116 | + if ( empty( $action ) || ! in_array( $action, array( 'deactivate', 'deactivate-selected' ), true ) ) { | |
| 101 | 117 | return false; |
| 118 | + } | |
| 102 | 119 | |
| 103 | 120 | // The plugin(s) being deactivated |
| 104 | - if ( $action == 'deactivate' ) { | |
| 121 | + if ( $action === 'deactivate' ) { | |
| 105 | 122 | $plugins = isset( $_GET['plugin'] ) ? array( $_GET['plugin'] ) : array(); |
| 106 | 123 | } else { |
| 107 | 124 | $plugins = isset( $_POST['checked'] ) ? (array) $_POST['checked'] : array(); |
| 108 | 125 | } |
| @@ -107,45 +124,41 @@ | ||
| 107 | 124 | $plugins = isset( $_POST['checked'] ) ? (array) $_POST['checked'] : array(); |
| 108 | 125 | } |
| 109 | 126 | |
| 110 | 127 | // Set basename if empty |
| 111 | - if ( empty( $basename ) && !empty( $bbp->basename ) ) | |
| 128 | + if ( empty( $basename ) && ! empty( $bbp->basename ) ) { | |
| 112 | 129 | $basename = $bbp->basename; |
| 130 | + } | |
| 113 | 131 | |
| 114 | 132 | // Bail if no basename |
| 115 | - if ( empty( $basename ) ) | |
| 133 | + if ( empty( $basename ) ) { | |
| 116 | 134 | return false; |
| 135 | + } | |
| 117 | 136 | |
| 118 | 137 | // Is bbPress being deactivated? |
| 119 | - return in_array( $basename, $plugins ); | |
| 138 | + return in_array( $basename, $plugins, true ); | |
| 120 | 139 | } |
| 121 | 140 | |
| 122 | 141 | /** |
| 123 | 142 | * Update the DB to the latest version |
| 124 | 143 | * |
| 125 | - * @since bbPress (r3421) | |
| 126 | - * @uses update_option() | |
| 127 | - * @uses bbp_get_db_version() To get bbPress's database version | |
| 144 | + * @since 2.0.0 bbPress (r3421) | |
| 128 | 145 | */ |
| 129 | 146 | function bbp_version_bump() { |
| 130 | - $db_version = bbp_get_db_version(); | |
| 131 | - update_option( '_bbp_db_version', $db_version ); | |
| 147 | + update_option( '_bbp_db_version', bbp_get_db_version() ); | |
| 132 | 148 | } |
| 133 | 149 | |
| 134 | 150 | /** |
| 135 | 151 | * Setup the bbPress updater |
| 136 | 152 | * |
| 137 | - * @since bbPress (r3419) | |
| 138 | - * | |
| 139 | - * @uses bbp_version_updater() | |
| 140 | - * @uses bbp_version_bump() | |
| 141 | - * @uses flush_rewrite_rules() | |
| 153 | + * @since 2.0.0 bbPress (r3419) | |
| 142 | 154 | */ |
| 143 | 155 | function bbp_setup_updater() { |
| 144 | 156 | |
| 145 | 157 | // Bail if no update needed |
| 146 | - if ( ! bbp_is_update() ) | |
| 158 | + if ( ! bbp_is_update() ) { | |
| 147 | 159 | return; |
| 160 | + } | |
| 148 | 161 | |
| 149 | 162 | // Call the automated updater |
| 150 | 163 | bbp_version_updater(); |
| 151 | 164 | } |
| @@ -150,52 +163,96 @@ | ||
| 150 | 163 | bbp_version_updater(); |
| 151 | 164 | } |
| 152 | 165 | |
| 153 | 166 | /** |
| 167 | + * Runs when a new site is created in a multisite network, and bbPress is active | |
| 168 | + * on that site (hooked to `bbp_new_site`) | |
| 169 | + * | |
| 170 | + * @since 2.6.0 bbPress (r6779) | |
| 171 | + */ | |
| 172 | +function bbp_setup_new_site( $site_id = 0 ) { | |
| 173 | + | |
| 174 | + // Look for initial content | |
| 175 | + $created = is_multisite() | |
| 176 | + ? get_blog_option( $site_id, '_bbp_flag_initial_content', false ) | |
| 177 | + : get_option( '_bbp_flag_initial_content', false ); | |
| 178 | + | |
| 179 | + // Maybe create the initial content | |
| 180 | + if ( ! empty( $created ) ) { | |
| 181 | + bbp_create_initial_content(); | |
| 182 | + | |
| 183 | + // Flag initial content as created | |
| 184 | + is_multisite() | |
| 185 | + ? update_blog_option( $site_id, '_bbp_flag_initial_content', true ) | |
| 186 | + : update_option( '_bbp_flag_initial_content', true ); | |
| 187 | + } | |
| 188 | +} | |
| 189 | + | |
| 190 | +/** | |
| 154 | 191 | * Create a default forum, topic, and reply |
| 155 | 192 | * |
| 156 | - * @since bbPress (r3767) | |
| 193 | + * @since 2.1.0 bbPress (r3767) | |
| 194 | + * | |
| 157 | 195 | * @param array $args Array of arguments to override default values |
| 158 | 196 | */ |
| 159 | 197 | function bbp_create_initial_content( $args = array() ) { |
| 160 | 198 | |
| 161 | - $defaults = array( | |
| 199 | + // Cannot use bbp_get_current_user_id() during activation process | |
| 200 | + $user_id = get_current_user_id(); | |
| 201 | + | |
| 202 | + // Parse arguments against default values | |
| 203 | + $r = bbp_parse_args( $args, array( | |
| 204 | + 'forum_author' => $user_id, | |
| 162 | 205 | 'forum_parent' => 0, |
| 163 | 206 | 'forum_status' => 'publish', |
| 164 | - 'forum_title' => __( 'General', 'bbpress' ), | |
| 165 | - 'forum_content' => __( 'General chit-chat', 'bbpress' ), | |
| 166 | - 'topic_title' => __( 'Hello World!', 'bbpress' ), | |
| 167 | - 'topic_content' => __( 'I am the first topic in your new forums.', 'bbpress' ), | |
| 168 | - 'reply_title' => __( 'Re: Hello World!', 'bbpress' ), | |
| 169 | - 'reply_content' => __( 'Oh, and this is what a reply looks like.', 'bbpress' ), | |
| 170 | - ); | |
| 171 | - $r = bbp_parse_args( $args, $defaults, 'create_initial_content' ); | |
| 172 | - extract( $r ); | |
| 207 | + 'forum_title' => esc_html__( 'General', 'bbpress' ), | |
| 208 | + 'forum_content' => esc_html__( 'General Discussion', 'bbpress' ), | |
| 173 | 209 | |
| 210 | + 'topic_author' => $user_id, | |
| 211 | + 'topic_title' => esc_html__( 'Hello World!', 'bbpress' ), | |
| 212 | + 'topic_content' => esc_html__( 'This is the very first topic in these forums.', 'bbpress' ), | |
| 213 | + | |
| 214 | + 'reply_author' => $user_id, | |
| 215 | + 'reply_content' => esc_html__( 'And this is the very first reply.', 'bbpress' ), | |
| 216 | + ), 'create_initial_content' ); | |
| 217 | + | |
| 218 | + // Use the same time for each post | |
| 219 | + $current_time = time(); | |
| 220 | + $forum_time = date( 'Y-m-d H:i:s', $current_time - 60 * 60 * 80 ); | |
| 221 | + $topic_time = date( 'Y-m-d H:i:s', $current_time - 60 * 60 * 60 ); | |
| 222 | + $reply_time = date( 'Y-m-d H:i:s', $current_time - 60 * 60 * 40 ); | |
| 223 | + | |
| 174 | 224 | // Create the initial forum |
| 175 | 225 | $forum_id = bbp_insert_forum( array( |
| 176 | - 'post_parent' => $forum_parent, | |
| 177 | - 'post_status' => $forum_status, | |
| 178 | - 'post_title' => $forum_title, | |
| 179 | - 'post_content' => $forum_content | |
| 226 | + 'post_author' => $r['forum_author'], | |
| 227 | + 'post_parent' => $r['forum_parent'], | |
| 228 | + 'post_status' => $r['forum_status'], | |
| 229 | + 'post_title' => $r['forum_title'], | |
| 230 | + 'post_content' => $r['forum_content'], | |
| 231 | + 'post_date' => $forum_time | |
| 180 | 232 | ) ); |
| 181 | 233 | |
| 182 | 234 | // Create the initial topic |
| 183 | 235 | $topic_id = bbp_insert_topic( |
| 184 | 236 | array( |
| 237 | + 'post_author' => $r['topic_author'], | |
| 185 | 238 | 'post_parent' => $forum_id, |
| 186 | - 'post_title' => $topic_title, | |
| 187 | - 'post_content' => $topic_content | |
| 239 | + 'post_title' => $r['topic_title'], | |
| 240 | + 'post_content' => $r['topic_content'], | |
| 241 | + 'post_date' => $topic_time, | |
| 188 | 242 | ), |
| 189 | - array( 'forum_id' => $forum_id ) | |
| 243 | + array( | |
| 244 | + 'forum_id' => $forum_id | |
| 245 | + ) | |
| 190 | 246 | ); |
| 191 | 247 | |
| 192 | 248 | // Create the initial reply |
| 193 | 249 | $reply_id = bbp_insert_reply( |
| 194 | 250 | array( |
| 251 | + 'post_author' => $r['reply_author'], | |
| 195 | 252 | 'post_parent' => $topic_id, |
| 196 | - 'post_title' => $reply_title, | |
| 197 | - 'post_content' => $reply_content | |
| 253 | + 'post_content' => $r['reply_content'], | |
| 254 | + 'post_date' => $reply_time | |
| 198 | 255 | ), |
| 199 | 256 | array( |
| 200 | 257 | 'forum_id' => $forum_id, |
| 201 | 258 | 'topic_id' => $topic_id |
| @@ -209,15 +266,15 @@ | ||
| 209 | 266 | ); |
| 210 | 267 | } |
| 211 | 268 | |
| 212 | 269 | /** |
| 213 | - * bbPress's version updater looks at what the current database version is, and | |
| 270 | + * The version updater looks at what the current database version is, and | |
| 214 | 271 | * runs whatever other code is needed. |
| 215 | 272 | * |
| 216 | 273 | * This is most-often used when the data schema changes, but should also be used |
| 217 | 274 | * to correct issues with bbPress meta-data silently on software update. |
| 218 | 275 | * |
| 219 | - * @since bbPress (r4104) | |
| 276 | + * @since 2.2.0 bbPress (r4104) | |
| 220 | 277 | */ |
| 221 | 278 | function bbp_version_updater() { |
| 222 | 279 | |
| 223 | 280 | // Get the raw database version |
| @@ -222,42 +279,124 @@ | ||
| 222 | 279 | |
| 223 | 280 | // Get the raw database version |
| 224 | 281 | $raw_db_version = (int) bbp_get_db_version_raw(); |
| 225 | 282 | |
| 226 | - /** 2.0 Branch ************************************************************/ | |
| 283 | + // Only run updater if previous installation exists | |
| 284 | + if ( ! empty( $raw_db_version ) ) { | |
| 227 | 285 | |
| 228 | - // 2.0, 2.0.1, 2.0.2, 2.0.3 | |
| 229 | - if ( $raw_db_version < 200 ) { | |
| 230 | - // Do nothing | |
| 231 | - } | |
| 286 | + /** 2.0 Branch ********************************************************/ | |
| 232 | 287 | |
| 233 | - /** 2.1 Branch ************************************************************/ | |
| 288 | + // 2.0, 2.0.1, 2.0.2, 2.0.3 | |
| 289 | + if ( $raw_db_version < 200 ) { | |
| 290 | + // No changes | |
| 291 | + } | |
| 234 | 292 | |
| 235 | - // 2.1, 2.1.1 | |
| 236 | - if ( $raw_db_version < 211 ) { | |
| 293 | + /** 2.1 Branch ********************************************************/ | |
| 237 | 294 | |
| 238 | - /** | |
| 239 | - * Repair private and hidden forum data | |
| 240 | - * | |
| 241 | - * @link http://bbpress.trac.wordpress.org/ticket/1891 | |
| 242 | - */ | |
| 243 | - bbp_admin_repair_forum_visibility(); | |
| 244 | - } | |
| 295 | + // 2.1, 2.1.1 | |
| 296 | + if ( $raw_db_version < 211 ) { | |
| 245 | 297 | |
| 246 | - /** 2.2 Branch ************************************************************/ | |
| 298 | + /** | |
| 299 | + * Repair private and hidden forum data | |
| 300 | + * | |
| 301 | + * @link https://bbpress.trac.wordpress.org/ticket/1891 | |
| 302 | + */ | |
| 303 | + bbp_admin_repair_forum_visibility(); | |
| 304 | + } | |
| 247 | 305 | |
| 248 | - // 2.2 | |
| 249 | - if ( $raw_db_version < 220 ) { | |
| 306 | + /** 2.2 Branch ********************************************************/ | |
| 250 | 307 | |
| 251 | - // Remove bbPress 1.1 roles (BuddyPress) | |
| 252 | - remove_role( 'member' ); | |
| 253 | - remove_role( 'inactive' ); | |
| 254 | - remove_role( 'blocked' ); | |
| 255 | - remove_role( 'moderator' ); | |
| 256 | - remove_role( 'keymaster' ); | |
| 308 | + // 2.2.x | |
| 309 | + if ( $raw_db_version < 220 ) { | |
| 257 | 310 | |
| 258 | - // Refresh capabilities | |
| 259 | - bbp_remove_caps(); | |
| 311 | + // Remove any old bbPress roles | |
| 312 | + bbp_remove_roles(); | |
| 313 | + | |
| 314 | + // Remove capabilities | |
| 315 | + bbp_remove_caps(); | |
| 316 | + } | |
| 317 | + | |
| 318 | + /** 2.3 Branch ********************************************************/ | |
| 319 | + | |
| 320 | + // 2.3.x | |
| 321 | + if ( $raw_db_version < 230 ) { | |
| 322 | + // No changes | |
| 323 | + } | |
| 324 | + | |
| 325 | + /** 2.4 Branch ********************************************************/ | |
| 326 | + | |
| 327 | + // 2.4.x | |
| 328 | + if ( $raw_db_version < 240 ) { | |
| 329 | + // No changes | |
| 330 | + } | |
| 331 | + | |
| 332 | + /** 2.5 Branch ********************************************************/ | |
| 333 | + | |
| 334 | + // 2.5.x | |
| 335 | + if ( $raw_db_version < 250 ) { | |
| 336 | + // No changes | |
| 337 | + } | |
| 338 | + | |
| 339 | + /** 2.6 Branch ********************************************************/ | |
| 340 | + | |
| 341 | + // Smaller installs run the upgrades directly | |
| 342 | + if ( ! bbp_is_large_install() ) { | |
| 343 | + | |
| 344 | + /** | |
| 345 | + * Upgrade user favorites and subscriptions | |
| 346 | + */ | |
| 347 | + if ( $raw_db_version < 261 ) { | |
| 348 | + bbp_admin_upgrade_user_favorites(); | |
| 349 | + bbp_admin_upgrade_user_topic_subscriptions(); | |
| 350 | + bbp_admin_upgrade_user_forum_subscriptions(); | |
| 351 | + } | |
| 352 | + | |
| 353 | + /** | |
| 354 | + * Upgrade user engagements | |
| 355 | + */ | |
| 356 | + if ( $raw_db_version < 262 ) { | |
| 357 | + bbp_admin_upgrade_user_engagements(); | |
| 358 | + } | |
| 359 | + | |
| 360 | + /** | |
| 361 | + * Repair forum hidden reply count | |
| 362 | + */ | |
| 363 | + if ( $raw_db_version < 263 ) { | |
| 364 | + bbp_admin_repair_forum_hidden_reply_count(); | |
| 365 | + } | |
| 366 | + | |
| 367 | + // Large installs require manual intervention | |
| 368 | + } else { | |
| 369 | + | |
| 370 | + /** | |
| 371 | + * Upgrade user favorites and subscriptions | |
| 372 | + */ | |
| 373 | + if ( $raw_db_version < 261 ) { | |
| 374 | + bbp_add_pending_upgrade( 'bbp-user-favorites-move' ); | |
| 375 | + bbp_add_pending_upgrade( 'bbp-user-topic-subscriptions-move' ); | |
| 376 | + bbp_add_pending_upgrade( 'bbp-user-forum-subscriptions-move' ); | |
| 377 | + | |
| 378 | + // Set strategy to pre-2.6 on large network | |
| 379 | + update_option( '_bbp_engagements_strategy', 'user' ); | |
| 380 | + } | |
| 381 | + | |
| 382 | + /** | |
| 383 | + * Upgrade user engagements | |
| 384 | + */ | |
| 385 | + if ( $raw_db_version < 262 ) { | |
| 386 | + bbp_add_pending_upgrade( 'bbp-user-topic-engagements-move' ); | |
| 387 | + | |
| 388 | + // Set strategy to pre-2.6 on large network | |
| 389 | + update_option( '_bbp_engagements_strategy', 'user' ); | |
| 390 | + } | |
| 391 | + | |
| 392 | + /** | |
| 393 | + * Upgrade user engagements | |
| 394 | + */ | |
| 395 | + if ( $raw_db_version < 263 ) { | |
| 396 | + bbp_add_pending_upgrade( 'bbp-forum-hidden-replies' ); | |
| 397 | + } | |
| 398 | + } | |
| 260 | 399 | } |
| 261 | 400 | |
| 262 | 401 | /** All done! *************************************************************/ |
| 263 | 402 | |
| @@ -268,24 +407,225 @@ | ||
| 268 | 407 | bbp_delete_rewrite_rules(); |
| 269 | 408 | } |
| 270 | 409 | |
| 271 | 410 | /** |
| 272 | - * Redirect user to bbPress's What's New page on activation | |
| 411 | + * Redirect user to the "What's New" page on activation | |
| 273 | 412 | * |
| 274 | - * @since bbPress (r4389) | |
| 413 | + * @since 2.2.0 bbPress (r4389) | |
| 275 | 414 | * |
| 276 | 415 | * @internal Used internally to redirect bbPress to the about page on activation |
| 277 | 416 | * |
| 278 | - * @uses is_network_admin() To bail if being network activated | |
| 279 | - * @uses set_transient() To drop the activation transient for 30 seconds | |
| 280 | - * | |
| 281 | 417 | * @return If network admin or bulk activation |
| 282 | 418 | */ |
| 283 | 419 | function bbp_add_activation_redirect() { |
| 284 | 420 | |
| 285 | 421 | // Bail if activating from network, or bulk |
| 286 | - if ( is_network_admin() || isset( $_GET['activate-multi'] ) ) | |
| 422 | + if ( is_network_admin() || isset( $_GET['activate-multi'] ) ) { | |
| 287 | 423 | return; |
| 424 | + } | |
| 288 | 425 | |
| 289 | - // Add the transient to redirect | |
| 290 | - set_transient( '_bbp_activation_redirect', true, 30 ); | |
| 426 | + // Add the redirect trigger | |
| 427 | + update_user_option( get_current_user_id(), '_bbp_activation_redirect', true ); | |
| 428 | +} | |
| 429 | + | |
| 430 | +/** | |
| 431 | + * Redirect user to "What's New" page on activation | |
| 432 | + * | |
| 433 | + * @since 2.2.0 bbPress (r4389) | |
| 434 | + * | |
| 435 | + * @internal Used internally to redirect bbPress to the about page on activation | |
| 436 | + * | |
| 437 | + * @return If no transient, or in network admin, or is bulk activation | |
| 438 | + */ | |
| 439 | +function bbp_do_activation_redirect() { | |
| 440 | + | |
| 441 | + // Bail if no redirect trigger | |
| 442 | + if ( ! get_user_option( '_bbp_activation_redirect' ) ) { | |
| 443 | + return; | |
| 444 | + } | |
| 445 | + | |
| 446 | + // Delete the redirect trigger | |
| 447 | + delete_user_option( get_current_user_id(), '_bbp_activation_redirect' ); | |
| 448 | + | |
| 449 | + // Bail if activating from network, or bulk | |
| 450 | + if ( is_network_admin() || isset( $_GET['activate-multi'] ) ) { | |
| 451 | + return; | |
| 452 | + } | |
| 453 | + | |
| 454 | + // Bail if the current user cannot see the about page | |
| 455 | + if ( ! current_user_can( 'bbp_about_page' ) ) { | |
| 456 | + return; | |
| 457 | + } | |
| 458 | + | |
| 459 | + // Redirect to bbPress about page | |
| 460 | + bbp_redirect( add_query_arg( array( 'page' => 'bbp-about' ), admin_url( 'index.php' ) ) ); | |
| 461 | +} | |
| 462 | + | |
| 463 | +/** | |
| 464 | + * Hooked to the 'bbp_activate' action, this helper function automatically makes | |
| 465 | + * the current user a Key Master in the forums if they just activated bbPress, | |
| 466 | + * regardless of the bbp_allow_global_access() setting. | |
| 467 | + * | |
| 468 | + * @since 2.4.0 bbPress (r4910) | |
| 469 | + * | |
| 470 | + * @internal Used to internally make the current user a keymaster on activation | |
| 471 | + * | |
| 472 | + * @return If user can't activate plugins or is already a keymaster | |
| 473 | + */ | |
| 474 | +function bbp_make_current_user_keymaster() { | |
| 475 | + | |
| 476 | + // Catch all, to prevent premature user initialization | |
| 477 | + if ( ! did_action( 'set_current_user' ) ) { | |
| 478 | + return; | |
| 479 | + } | |
| 480 | + | |
| 481 | + // Bail if not logged in or already a member of this site | |
| 482 | + if ( ! is_user_logged_in() ) { | |
| 483 | + return; | |
| 484 | + } | |
| 485 | + | |
| 486 | + // Bail if the current user can't activate plugins since previous pageload | |
| 487 | + if ( ! current_user_can( 'activate_plugins' ) ) { | |
| 488 | + return; | |
| 489 | + } | |
| 490 | + | |
| 491 | + // Cannot use bbp_get_current_user_id() during activation process | |
| 492 | + $user_id = get_current_user_id(); | |
| 493 | + | |
| 494 | + // Get the current blog ID, to know if they should be promoted here | |
| 495 | + $blog_id = get_current_blog_id(); | |
| 496 | + | |
| 497 | + // Bail if user is not actually a member of this site | |
| 498 | + if ( ! is_user_member_of_blog( $user_id, $blog_id ) ) { | |
| 499 | + return; | |
| 500 | + } | |
| 501 | + | |
| 502 | + // Bail if the current user already has a forum role to prevent | |
| 503 | + // unexpected role and capability escalation. | |
| 504 | + if ( bbp_get_user_role( $user_id ) ) { | |
| 505 | + return; | |
| 506 | + } | |
| 507 | + | |
| 508 | + // Make the current user a keymaster | |
| 509 | + bbp_set_user_role( $user_id, bbp_get_keymaster_role() ); | |
| 510 | + | |
| 511 | + // Reload the current user so caps apply immediately | |
| 512 | + wp_get_current_user(); | |
| 513 | +} | |
| 514 | + | |
| 515 | +/** Pending Upgrades **********************************************************/ | |
| 516 | + | |
| 517 | +/** | |
| 518 | + * Return the number of pending upgrades | |
| 519 | + * | |
| 520 | + * @since 2.6.0 bbPress (r6895) | |
| 521 | + * | |
| 522 | + * @param string $type Type of pending upgrades (upgrade|repair|empty) | |
| 523 | + * | |
| 524 | + * @return int | |
| 525 | + */ | |
| 526 | +function bbp_get_pending_upgrade_count( $type = '' ) { | |
| 527 | + return count( (array) bbp_get_pending_upgrades( $type ) ); | |
| 528 | +} | |
| 529 | + | |
| 530 | +/** | |
| 531 | + * Return an array of pending upgrades | |
| 532 | + * | |
| 533 | + * @since 2.6.0 bbPress (r6895) | |
| 534 | + * | |
| 535 | + * @param string $type Type of pending upgrades (upgrade|repair|empty) | |
| 536 | + * | |
| 537 | + * @return array | |
| 538 | + */ | |
| 539 | +function bbp_get_pending_upgrades( $type = '' ) { | |
| 540 | + | |
| 541 | + // Get the pending upgrades | |
| 542 | + $retval = (array) get_option( '_bbp_db_pending_upgrades', array() ); | |
| 543 | + | |
| 544 | + // Looking for a specific type? | |
| 545 | + if ( ! empty( $type ) ) { | |
| 546 | + $tools = bbp_get_admin_repair_tools( $type ); | |
| 547 | + $plucked = array_keys( wp_list_pluck( $tools, 'type' ) ); | |
| 548 | + $retval = array_intersect( $retval, $plucked ); | |
| 549 | + } | |
| 550 | + | |
| 551 | + return (array) $retval; | |
| 552 | +} | |
| 553 | + | |
| 554 | +/** | |
| 555 | + * Add an upgrade ID to pending upgrades array | |
| 556 | + * | |
| 557 | + * @since 2.6.0 bbPress (r6895) | |
| 558 | + * | |
| 559 | + * @param string $upgrade_id | |
| 560 | + */ | |
| 561 | +function bbp_add_pending_upgrade( $upgrade_id = '' ) { | |
| 562 | + | |
| 563 | + // Get the pending upgrades option | |
| 564 | + $pending = bbp_get_pending_upgrades(); | |
| 565 | + | |
| 566 | + // Maybe add upgrade ID to end of pending array | |
| 567 | + if ( false === array_search( $upgrade_id, $pending, true ) ) { | |
| 568 | + array_push( $pending, $upgrade_id ); | |
| 569 | + } | |
| 570 | + | |
| 571 | + // Update and return | |
| 572 | + return update_option( '_bbp_db_pending_upgrades', $pending ); | |
| 573 | +} | |
| 574 | + | |
| 575 | +/** | |
| 576 | + * Add an upgrade ID to pending upgrades array | |
| 577 | + * | |
| 578 | + * @since 2.6.0 bbPress (r6895) | |
| 579 | + * | |
| 580 | + * @param string $upgrade_id | |
| 581 | + */ | |
| 582 | +function bbp_remove_pending_upgrade( $upgrade_id = '' ) { | |
| 583 | + | |
| 584 | + // Get the pending upgrades option | |
| 585 | + $pending = bbp_get_pending_upgrades(); | |
| 586 | + | |
| 587 | + // Look for this upgrade ID | |
| 588 | + $index = array_search( $upgrade_id, $pending, true ); | |
| 589 | + | |
| 590 | + // Maybe remove upgrade ID from pending array | |
| 591 | + if ( false !== $index ) { | |
| 592 | + unset( $pending[ $index ] ); | |
| 593 | + } | |
| 594 | + | |
| 595 | + // Update and return | |
| 596 | + return update_option( '_bbp_db_pending_upgrades', $pending ); | |
| 597 | +} | |
| 598 | + | |
| 599 | +/** | |
| 600 | + * Delete all pending upgrades | |
| 601 | + * | |
| 602 | + * @since 2.6.0 bbPress (r6895) | |
| 603 | + */ | |
| 604 | +function bbp_clear_pending_upgrades() { | |
| 605 | + return delete_option( '_bbp_db_pending_upgrades' ); | |
| 606 | +} | |
| 607 | + | |
| 608 | +/** | |
| 609 | + * Maybe append an upgrade count to a string | |
| 610 | + * | |
| 611 | + * @since 2.6.0 bbPress (r6896) | |
| 612 | + * | |
| 613 | + * @param string $string Text to append count to | |
| 614 | + * @param string $type Type of pending upgrades (upgrade|repair|empty) | |
| 615 | + * | |
| 616 | + * @return string | |
| 617 | + */ | |
| 618 | +function bbp_maybe_append_pending_upgrade_count( $string = '', $type = '' ) { | |
| 619 | + | |
| 620 | + // Look for an upgrade count | |
| 621 | + $count = bbp_get_pending_upgrade_count( $type ); | |
| 622 | + | |
| 623 | + // Append the count to the string | |
| 624 | + if ( ! empty( $count ) ) { | |
| 625 | + $suffix = ' <span class="awaiting-mod count-' . absint( $count ) . '"><span class="pending-count">' . bbp_number_format( $count ) . '</span></span>'; | |
| 626 | + $string = "{$string}{$suffix}"; | |
| 627 | + } | |
| 628 | + | |
| 629 | + // Return the string, maybe with a count | |
| 630 | + return $string; | |
| 291 | 631 | } |