| @@ -11,15 +11,15 @@ | ||
| 11 | 11 | |
| 12 | 12 | /** |
| 13 | 13 | * Maps primary capabilities |
| 14 | 14 | * |
| 15 | - * @since bbPress (r4242) | |
| 15 | + * @since 2.2.0 bbPress (r4242) | |
| 16 | 16 | * |
| 17 | - * @param array $caps Capabilities for meta capability | |
| 18 | - * @param string $cap Capability name | |
| 19 | - * @param int $user_id User id | |
| 20 | - * @param mixed $args Arguments | |
| 21 | - * @uses apply_filters() Filter mapped results | |
| 17 | + * @param array $caps Capabilities for meta capability. | |
| 18 | + * @param string $cap Capability name. | |
| 19 | + * @param int $user_id User id. | |
| 20 | + * @param array $args Arguments. | |
| 21 | + * | |
| 22 | 22 | * @return array Actual capabilities for meta capability |
| 23 | 23 | */ |
| 24 | 24 | function bbp_map_primary_meta_caps( $caps = array(), $cap = '', $user_id = 0, $args = array() ) { |
| 25 | 25 | |
| @@ -24,37 +24,136 @@ | ||
| 24 | 24 | function bbp_map_primary_meta_caps( $caps = array(), $cap = '', $user_id = 0, $args = array() ) { |
| 25 | 25 | |
| 26 | 26 | // What capability is being checked? |
| 27 | 27 | switch ( $cap ) { |
| 28 | - case 'spectate' : | |
| 28 | + case 'spectate' : | |
| 29 | + | |
| 30 | + // Do not allow inactive users. | |
| 31 | + if ( bbp_is_user_inactive( $user_id ) ) { | |
| 32 | + $caps = array( 'do_not_allow' ); | |
| 33 | + | |
| 34 | + // Default to the current cap. | |
| 35 | + } else { | |
| 36 | + $caps = array( $cap ); | |
| 37 | + } | |
| 38 | + break; | |
| 39 | + | |
| 29 | 40 | case 'participate' : |
| 30 | - case 'moderate' : | |
| 31 | 41 | |
| 32 | - // Do not allow inactive users | |
| 42 | + // Do not allow inactive users. | |
| 33 | 43 | if ( bbp_is_user_inactive( $user_id ) ) { |
| 34 | 44 | $caps = array( 'do_not_allow' ); |
| 35 | 45 | |
| 36 | - // Moderators are always participants | |
| 46 | + // Default to the current cap. | |
| 37 | 47 | } else { |
| 38 | 48 | $caps = array( $cap ); |
| 39 | 49 | } |
| 50 | + break; | |
| 40 | 51 | |
| 52 | + case 'moderate' : | |
| 53 | + | |
| 54 | + // Do not allow inactive users. | |
| 55 | + if ( bbp_is_user_inactive( $user_id ) ) { | |
| 56 | + $caps = array( 'do_not_allow' ); | |
| 57 | + | |
| 58 | + // Keymasters can always moderate. | |
| 59 | + } elseif ( bbp_is_user_keymaster( $user_id ) ) { | |
| 60 | + $caps = array( 'spectate' ); | |
| 61 | + | |
| 62 | + // Check if user can moderate forum. | |
| 63 | + } elseif ( bbp_allow_forum_mods() ) { | |
| 64 | + $caps = array( $cap ); | |
| 65 | + | |
| 66 | + // Bail if no post to check. | |
| 67 | + if ( empty( $args[0] ) ) { | |
| 68 | + break; | |
| 69 | + } | |
| 70 | + | |
| 71 | + // Get the post. | |
| 72 | + $_post = get_post( $args[0] ); | |
| 73 | + if ( empty( $_post ) ) { | |
| 74 | + break; | |
| 75 | + } | |
| 76 | + | |
| 77 | + // Get forum ID for specific type of post. | |
| 78 | + switch ( $_post->post_type ) { | |
| 79 | + | |
| 80 | + // Forum. | |
| 81 | + case bbp_get_forum_post_type() : | |
| 82 | + $forum_id = bbp_get_forum_id( $_post->ID ); | |
| 83 | + break; | |
| 84 | + | |
| 85 | + // Topic. | |
| 86 | + case bbp_get_topic_post_type() : | |
| 87 | + $forum_id = bbp_get_topic_forum_id( $_post->ID ); | |
| 88 | + break; | |
| 89 | + | |
| 90 | + // Reply. | |
| 91 | + case bbp_get_reply_post_type() : | |
| 92 | + $forum_id = bbp_get_reply_forum_id( $_post->ID ); | |
| 93 | + break; | |
| 94 | + | |
| 95 | + // Any other post type defaults to 0. | |
| 96 | + default : | |
| 97 | + $forum_id = 0; | |
| 98 | + break; | |
| 99 | + } | |
| 100 | + | |
| 101 | + // Bail if no forum ID. | |
| 102 | + if ( empty( $forum_id ) ) { | |
| 103 | + break; | |
| 104 | + } | |
| 105 | + | |
| 106 | + // User is mod of this forum | |
| 107 | + if ( bbp_is_object_of_user( $forum_id, $user_id, '_bbp_moderator_id' ) ) { | |
| 108 | + $caps = array( 'spectate' ); | |
| 109 | + } | |
| 110 | + } | |
| 111 | + | |
| 41 | 112 | break; |
| 113 | + | |
| 114 | + /** Super Moderators **************************************************/ | |
| 115 | + | |
| 116 | + case 'edit_user' : | |
| 117 | + case 'promote_user' : | |
| 118 | + | |
| 119 | + // Moderators can edit users if super moderators is enabled. | |
| 120 | + if ( bbp_allow_super_mods() && ! is_admin() && bbp_is_single_user_edit() ) { | |
| 121 | + | |
| 122 | + // Get the user ID | |
| 123 | + $_user_id = ! empty( $args[0] ) | |
| 124 | + ? (int) $args[0] | |
| 125 | + : bbp_get_displayed_user_id(); | |
| 126 | + | |
| 127 | + // Users can always edit themselves, so only map for others. | |
| 128 | + if ( ! empty( $_user_id ) && ( $_user_id !== $user_id ) ) { | |
| 129 | + | |
| 130 | + // Super moderators cannot edit keymasters or site administrators. | |
| 131 | + if ( | |
| 132 | + ! bbp_is_user_keymaster( $_user_id ) | |
| 133 | + && ! user_can( $_user_id, 'manage_options' ) | |
| 134 | + && ! is_super_admin( $_user_id ) | |
| 135 | + ) { | |
| 136 | + $caps = array( 'moderate' ); | |
| 137 | + } | |
| 138 | + } | |
| 139 | + } | |
| 140 | + | |
| 141 | + break; | |
| 42 | 142 | } |
| 43 | 143 | |
| 44 | - return apply_filters( 'bbp_map_primary_meta_caps', $caps, $cap, $user_id, $args ); | |
| 144 | + // Filter & return | |
| 145 | + return (array) apply_filters( 'bbp_map_primary_meta_caps', $caps, $cap, $user_id, $args ); | |
| 45 | 146 | } |
| 46 | 147 | |
| 47 | 148 | /** |
| 48 | - * Return a user's main role | |
| 149 | + * Set a user's role in the forums | |
| 49 | 150 | * |
| 50 | - * @since bbPress (r3860) | |
| 151 | + * @since 2.1.0 bbPress (r3860) | |
| 51 | 152 | * |
| 52 | 153 | * @param int $user_id |
| 53 | - * @uses bbp_get_user_id() To get the user id | |
| 54 | - * @uses get_userdata() To get the user data | |
| 55 | - * @uses apply_filters() Calls 'bbp_set_user_role' with the role and user id | |
| 56 | - * @return string | |
| 154 | + * | |
| 155 | + * @return mixed False if no change. String of new role if changed. | |
| 57 | 156 | */ |
| 58 | 157 | function bbp_set_user_role( $user_id = 0, $new_role = '' ) { |
| 59 | 158 | |
| 60 | 159 | // Validate user id |
| @@ -61,19 +160,19 @@ | ||
| 61 | 160 | $user_id = bbp_get_user_id( $user_id, false, false ); |
| 62 | 161 | $user = get_userdata( $user_id ); |
| 63 | 162 | |
| 64 | 163 | // User exists |
| 65 | - if ( !empty( $user ) ) { | |
| 164 | + if ( ! empty( $user ) ) { | |
| 66 | 165 | |
| 67 | - // Get users forum role | |
| 166 | + // Get user forum role | |
| 68 | 167 | $role = bbp_get_user_role( $user_id ); |
| 69 | 168 | |
| 70 | 169 | // User already has this role so no new role is set |
| 71 | - if ( $new_role == $role ) { | |
| 170 | + if ( $new_role === $role ) { | |
| 72 | 171 | $new_role = false; |
| 73 | 172 | |
| 74 | - // Users role is different than the new role | |
| 75 | - } else { | |
| 173 | + // User role is different than the new (valid) role | |
| 174 | + } elseif ( bbp_is_valid_role( $new_role ) ) { | |
| 76 | 175 | |
| 77 | 176 | // Remove the old role |
| 78 | 177 | if ( ! empty( $role ) ) { |
| 79 | 178 | $user->remove_role( $role ); |
| @@ -79,18 +178,19 @@ | ||
| 79 | 178 | $user->remove_role( $role ); |
| 80 | 179 | } |
| 81 | 180 | |
| 82 | 181 | // Add the new role |
| 83 | - if ( !empty( $new_role ) ) { | |
| 182 | + if ( ! empty( $new_role ) ) { | |
| 84 | 183 | $user->add_role( $new_role ); |
| 85 | 184 | } |
| 86 | 185 | } |
| 87 | 186 | |
| 88 | - // User does don exist so return false | |
| 187 | + // User does not exist so return false | |
| 89 | 188 | } else { |
| 90 | 189 | $new_role = false; |
| 91 | 190 | } |
| 92 | 191 | |
| 192 | + // Filter & return | |
| 93 | 193 | return apply_filters( 'bbp_set_user_role', $new_role, $user_id, $user ); |
| 94 | 194 | } |
| 95 | 195 | |
| 96 | 196 | /** |
| @@ -95,33 +195,39 @@ | ||
| 95 | 195 | |
| 96 | 196 | /** |
| 97 | 197 | * Return a user's forums role |
| 98 | 198 | * |
| 99 | - * @since bbPress (r3860) | |
| 199 | + * @since 2.1.0 bbPress (r3860) | |
| 100 | 200 | * |
| 101 | 201 | * @param int $user_id |
| 102 | - * @uses bbp_get_user_id() To get the user id | |
| 103 | - * @uses get_userdata() To get the user data | |
| 104 | - * @uses apply_filters() Calls 'bbp_get_user_role' with the role and user id | |
| 202 | + * | |
| 105 | 203 | * @return string |
| 106 | 204 | */ |
| 107 | 205 | function bbp_get_user_role( $user_id = 0 ) { |
| 108 | 206 | |
| 109 | 207 | // Validate user id |
| 110 | - $user_id = bbp_get_user_id( $user_id, false, false ); | |
| 208 | + $user_id = bbp_get_user_id( $user_id ); | |
| 111 | 209 | $user = get_userdata( $user_id ); |
| 112 | 210 | $role = false; |
| 113 | 211 | |
| 114 | - // User has roles so lets | |
| 212 | + // User has roles so look for a bbPress one | |
| 115 | 213 | if ( ! empty( $user->roles ) ) { |
| 116 | - $roles = array_intersect( array_values( $user->roles ), array_keys( bbp_get_dynamic_roles() ) ); | |
| 117 | 214 | |
| 118 | - // If there's a role in the array, use the first one | |
| 119 | - if ( !empty( $roles ) ) { | |
| 120 | - $role = array_shift( array_values( $roles ) ); | |
| 215 | + // Look for a bbPress role | |
| 216 | + $roles = array_intersect( | |
| 217 | + array_values( $user->roles ), | |
| 218 | + array_keys( bbp_get_dynamic_roles() ) | |
| 219 | + ); | |
| 220 | + | |
| 221 | + // If there's a role in the array, use the first one. This isn't very | |
| 222 | + // smart, but since roles aren't exactly hierarchical, and bbPress | |
| 223 | + // does not yet have a UI for multiple user roles, it's fine for now. | |
| 224 | + if ( ! empty( $roles ) ) { | |
| 225 | + $role = array_shift( $roles ); | |
| 121 | 226 | } |
| 122 | 227 | } |
| 123 | 228 | |
| 229 | + // Filter & return | |
| 124 | 230 | return apply_filters( 'bbp_get_user_role', $role, $user_id, $user ); |
| 125 | 231 | } |
| 126 | 232 | |
| 127 | 233 | /** |
| @@ -126,90 +232,207 @@ | ||
| 126 | 232 | |
| 127 | 233 | /** |
| 128 | 234 | * Return a user's blog role |
| 129 | 235 | * |
| 130 | - * @since bbPress (r4446) | |
| 236 | + * @since 2.3.0 bbPress (r4446) | |
| 131 | 237 | * |
| 132 | 238 | * @param int $user_id |
| 133 | - * @uses bbp_get_user_id() To get the user id | |
| 134 | - * @uses get_userdata() To get the user data | |
| 135 | - * @uses apply_filters() Calls 'bbp_get_user_blog_role' with the role and user id | |
| 239 | + * | |
| 136 | 240 | * @return string |
| 137 | 241 | */ |
| 138 | 242 | function bbp_get_user_blog_role( $user_id = 0 ) { |
| 139 | - global $wp_roles; | |
| 140 | 243 | |
| 141 | - // This really shold not be necessary anymore, and will likely be removed | |
| 142 | - // at a later date. If roles aren't loaded yet, something else is wrong. | |
| 143 | - if ( ! isset( $wp_roles ) ) | |
| 144 | - $wp_roles = new WP_Roles(); | |
| 145 | - | |
| 146 | 244 | // Validate user id |
| 147 | - $user_id = bbp_get_user_id( $user_id, false, false ); | |
| 148 | - $user = get_userdata( $user_id ); | |
| 149 | - $role = false; | |
| 150 | - $all_roles = apply_filters( 'editable_roles', $wp_roles->roles ); | |
| 245 | + $user_id = bbp_get_user_id( $user_id ); | |
| 246 | + $user = get_userdata( $user_id ); | |
| 247 | + $role = false; | |
| 151 | 248 | |
| 152 | 249 | // User has roles so lets |
| 153 | 250 | if ( ! empty( $user->roles ) ) { |
| 154 | - $roles = array_intersect( array_values( $user->roles ), array_keys( $all_roles ) ); | |
| 155 | 251 | |
| 156 | - // If there's a role in the array, use the first one | |
| 157 | - if ( !empty( $roles ) ) { | |
| 158 | - $role = array_shift( array_values( $roles ) ); | |
| 252 | + // Look for a non bbPress role | |
| 253 | + $roles = array_intersect( | |
| 254 | + array_values( $user->roles ), | |
| 255 | + array_keys( bbp_get_blog_roles() ) | |
| 256 | + ); | |
| 257 | + | |
| 258 | + // If there's a role in the array, use the first one. This isn't very | |
| 259 | + // smart, but since roles aren't exactly hierarchical, and WordPress | |
| 260 | + // does not yet have a UI for multiple user roles, it's fine for now. | |
| 261 | + if ( ! empty( $roles ) ) { | |
| 262 | + $role = array_shift( $roles ); | |
| 159 | 263 | } |
| 160 | 264 | } |
| 161 | 265 | |
| 266 | + // Filter & return | |
| 162 | 267 | return apply_filters( 'bbp_get_user_blog_role', $role, $user_id, $user ); |
| 163 | 268 | } |
| 164 | 269 | |
| 165 | 270 | /** |
| 166 | - * Helper function hooked to 'bbp_edit_user_profile_update' action to save or | |
| 167 | - * update user roles and capabilities. | |
| 271 | + * Helper function to save or update user roles and capabilities. | |
| 168 | 272 | * |
| 169 | - * @since bbPress (r4235) | |
| 273 | + * By default, this is hooked to the `bbp_profile_update` action which fires | |
| 274 | + * after a user profile is updated to avoid being stomped by set_role(). | |
| 170 | 275 | * |
| 276 | + * @since 2.2.0 bbPress (r4235) | |
| 277 | + * | |
| 171 | 278 | * @param int $user_id |
| 172 | - * @uses bbp_reset_user_caps() to reset caps | |
| 173 | - * @usse bbp_save_user_caps() to save caps | |
| 279 | + * | |
| 280 | + * @return void If no user ID passed, invalid request, trying to set own role, | |
| 281 | + * current user cannot promote the passing user, or no role passed | |
| 174 | 282 | */ |
| 175 | 283 | function bbp_profile_update_role( $user_id = 0 ) { |
| 176 | 284 | |
| 177 | - // Bail if no user ID was passed | |
| 178 | - if ( empty( $user_id ) ) | |
| 285 | + // Bail if no user ID | |
| 286 | + $user_id = bbp_get_user_id( $user_id, false, false ); | |
| 287 | + if ( empty( $user_id ) ) { | |
| 179 | 288 | return; |
| 289 | + } | |
| 180 | 290 | |
| 291 | + // Bail if not a user profile form post request | |
| 292 | + if ( ! bbp_is_user_profile_form_post_request( $user_id ) ) { | |
| 293 | + return; | |
| 294 | + } | |
| 295 | + | |
| 296 | + // Bail if trying to set their own role | |
| 297 | + if ( bbp_is_user_home_edit() ) { | |
| 298 | + return; | |
| 299 | + } | |
| 300 | + | |
| 301 | + // Bail if the current user cannot edit the forum role. | |
| 302 | + if ( ! bbp_current_user_can_edit_user_field( 'forum_role', $user_id ) ) { | |
| 303 | + return; | |
| 304 | + } | |
| 305 | + | |
| 306 | + // Bail if current user cannot promote the passing user | |
| 307 | + if ( ! current_user_can( 'promote_user', $user_id ) ) { | |
| 308 | + return; | |
| 309 | + } | |
| 310 | + | |
| 181 | 311 | // Bail if no role |
| 182 | - if ( ! isset( $_POST['bbp-forums-role'] ) ) | |
| 312 | + if ( ! isset( $_POST['bbp-forums-role'] ) || ! is_string( $_POST['bbp-forums-role'] ) ) { | |
| 183 | 313 | return; |
| 314 | + } | |
| 184 | 315 | |
| 185 | - // Fromus role we want the user to have | |
| 186 | - $new_role = sanitize_text_field( $_POST['bbp-forums-role'] ); | |
| 187 | - $forums_role = bbp_get_user_role( $user_id ); | |
| 316 | + // Forums role we want the user to have. | |
| 317 | + $new_role = sanitize_key( wp_unslash( $_POST['bbp-forums-role'] ) ); | |
| 188 | 318 | |
| 319 | + // Bail if the current user cannot assign this forum role. | |
| 320 | + if ( ! array_key_exists( $new_role, bbp_get_user_editable_forum_roles( $user_id ) ) ) { | |
| 321 | + return; | |
| 322 | + } | |
| 323 | + | |
| 189 | 324 | // Set the new forums role |
| 190 | - if ( $new_role != $forums_role ) { | |
| 191 | - bbp_set_user_role( $user_id, $new_role ); | |
| 325 | + bbp_set_user_role( $user_id, $new_role ); | |
| 326 | +} | |
| 327 | + | |
| 328 | +/** | |
| 329 | + * Return the forum roles the current user may assign to another user. | |
| 330 | + * | |
| 331 | + * @since 2.6.17 | |
| 332 | + * | |
| 333 | + * @param int $user_id User being edited. Defaults to the displayed user. | |
| 334 | + * @return array Filtered array of editable forum roles. | |
| 335 | + */ | |
| 336 | +function bbp_get_user_editable_forum_roles( $user_id = 0 ) { | |
| 337 | + $user_id = bbp_get_user_id( $user_id, false, false ); | |
| 338 | + $roles = bbp_get_dynamic_roles(); | |
| 339 | + | |
| 340 | + // Moderators may assign non-staff roles by default. | |
| 341 | + if ( ! bbp_is_user_keymaster() ) { | |
| 342 | + unset( | |
| 343 | + $roles[ bbp_get_keymaster_role() ], | |
| 344 | + $roles[ bbp_get_moderator_role() ] | |
| 345 | + ); | |
| 192 | 346 | } |
| 347 | + | |
| 348 | + // Filter & return. | |
| 349 | + return (array) apply_filters( 'bbp_get_user_editable_forum_roles', $roles, $user_id, bbp_get_current_user_id() ); | |
| 193 | 350 | } |
| 194 | 351 | |
| 195 | 352 | /** |
| 353 | + * Return whether the current user may edit a user-profile field. | |
| 354 | + * | |
| 355 | + * @since 2.6.17 | |
| 356 | + * | |
| 357 | + * @param string $field Profile field group: profile, email, password, | |
| 358 | + * site_role, or forum_role. | |
| 359 | + * @param int $user_id User being edited. Defaults to the displayed user. | |
| 360 | + * @return bool Whether the field may be edited. | |
| 361 | + */ | |
| 362 | +function bbp_current_user_can_edit_user_field( $field = 'profile', $user_id = 0 ) { | |
| 363 | + $user_id = bbp_get_user_id( $user_id, false, false ); | |
| 364 | + $current_user_id = bbp_get_current_user_id(); | |
| 365 | + | |
| 366 | + // Default to the general profile-edit capability. | |
| 367 | + $retval = current_user_can( 'edit_user', $user_id ); | |
| 368 | + | |
| 369 | + // Apply narrower defaults to sensitive field groups. | |
| 370 | + switch ( $field ) { | |
| 371 | + case 'password': | |
| 372 | + $retval = ! empty( $user_id ) | |
| 373 | + && ! empty( $current_user_id ) | |
| 374 | + && $retval | |
| 375 | + && ( ( $user_id === $current_user_id ) | |
| 376 | + || bbp_is_user_keymaster( $current_user_id ) | |
| 377 | + || current_user_can( 'manage_options' ) ); | |
| 378 | + break; | |
| 379 | + | |
| 380 | + case 'site_role': | |
| 381 | + $retval = current_user_can( 'promote_users' ) | |
| 382 | + && current_user_can( 'promote_user', $user_id ); | |
| 383 | + break; | |
| 384 | + | |
| 385 | + case 'forum_role': | |
| 386 | + $retval = current_user_can( 'promote_user', $user_id ); | |
| 387 | + break; | |
| 388 | + } | |
| 389 | + | |
| 390 | + // Filter & return. | |
| 391 | + return (bool) apply_filters( 'bbp_current_user_can_edit_user_field', $retval, $field, $user_id, $current_user_id ); | |
| 392 | +} | |
| 393 | + | |
| 394 | +/** | |
| 395 | + * Check if a role string is valid | |
| 396 | + * | |
| 397 | + * @since 2.6.5 | |
| 398 | + * | |
| 399 | + * @param string $role | |
| 400 | + * | |
| 401 | + * @return bool True if role is valid. False if role is not valid. | |
| 402 | + */ | |
| 403 | +function bbp_is_valid_role( $role = '' ) { | |
| 404 | + | |
| 405 | + // Default return value | |
| 406 | + $retval = false; | |
| 407 | + | |
| 408 | + // Skip if no role to check | |
| 409 | + if ( ! empty( $role ) && is_string( $role ) ) { | |
| 410 | + | |
| 411 | + // Get the dynamic role IDs | |
| 412 | + $roles = array_keys( bbp_get_dynamic_roles() ); | |
| 413 | + | |
| 414 | + // Skip if no known role IDs | |
| 415 | + if ( ! empty( $roles ) ) { | |
| 416 | + | |
| 417 | + // Is role in dynamic roles array? | |
| 418 | + $retval = in_array( $role, $roles, true ); | |
| 419 | + } | |
| 420 | + } | |
| 421 | + | |
| 422 | + // Filter & return | |
| 423 | + return (bool) apply_filters( 'bbp_is_valid_role', $retval, $role ); | |
| 424 | +} | |
| 425 | + | |
| 426 | +/** | |
| 196 | 427 | * Add the default role to the current user if needed |
| 197 | 428 | * |
| 198 | 429 | * This function will bail if the forum is not global in a multisite |
| 199 | 430 | * installation of WordPress, or if the user is marked as spam or deleted. |
| 200 | 431 | * |
| 201 | - * @since bbPress (r3380) | |
| 432 | + * @since 2.0.0 bbPress (r3380) | |
| 202 | 433 | * |
| 203 | - * @uses is_user_logged_in() To bail if user is not logged in | |
| 204 | - * @uses bbp_get_user_role() To bail if user already has a role | |
| 205 | - * @uses bbp_is_user_inactive() To bail if user is inactive | |
| 206 | - * @uses bbp_allow_global_access() To know whether to save role to database | |
| 207 | - * @uses bbp_get_user_role_map() To get the WP to BBP role map array | |
| 208 | - * @uses bbp_get_default_role() To get the site's default forums role | |
| 209 | - * @uses get_option() | |
| 210 | - * | |
| 211 | - * @return If not multisite, not global, or user is deleted/spammed | |
| 434 | + * @return void If not multisite, not global, or user is deleted/spammed | |
| 212 | 435 | */ |
| 213 | 436 | function bbp_set_current_user_default_role() { |
| 214 | 437 | |
| 215 | 438 | /** Sanity ****************************************************************/ |
| @@ -214,29 +437,34 @@ | ||
| 214 | 437 | |
| 215 | 438 | /** Sanity ****************************************************************/ |
| 216 | 439 | |
| 217 | 440 | // Bail if deactivating bbPress |
| 218 | - if ( bbp_is_deactivation() ) | |
| 441 | + if ( bbp_is_deactivation() ) { | |
| 219 | 442 | return; |
| 443 | + } | |
| 220 | 444 | |
| 221 | 445 | // Catch all, to prevent premature user initialization |
| 222 | - if ( ! did_action( 'set_current_user' ) ) | |
| 446 | + if ( ! did_action( 'set_current_user' ) ) { | |
| 223 | 447 | return; |
| 448 | + } | |
| 224 | 449 | |
| 225 | 450 | // Bail if not logged in or already a member of this site |
| 226 | - if ( ! is_user_logged_in() ) | |
| 451 | + if ( ! is_user_logged_in() ) { | |
| 227 | 452 | return; |
| 453 | + } | |
| 228 | 454 | |
| 229 | 455 | // Get the current user ID |
| 230 | 456 | $user_id = bbp_get_current_user_id(); |
| 231 | 457 | |
| 232 | 458 | // Bail if user already has a forums role |
| 233 | - if ( bbp_get_user_role( $user_id ) ) | |
| 459 | + if ( bbp_get_user_role( $user_id ) ) { | |
| 234 | 460 | return; |
| 461 | + } | |
| 235 | 462 | |
| 236 | 463 | // Bail if user is marked as spam or is deleted |
| 237 | - if ( bbp_is_user_inactive( $user_id ) ) | |
| 464 | + if ( bbp_is_user_inactive( $user_id ) ) { | |
| 238 | 465 | return; |
| 466 | + } | |
| 239 | 467 | |
| 240 | 468 | /** Ready *****************************************************************/ |
| 241 | 469 | |
| 242 | 470 | // Load up bbPress once |
| @@ -252,26 +480,22 @@ | ||
| 252 | 480 | $role_map = bbp_get_user_role_map(); |
| 253 | 481 | |
| 254 | 482 | /** Forum Role ************************************************************/ |
| 255 | 483 | |
| 256 | - // Use a mapped role | |
| 257 | - if ( isset( $role_map[$user_role] ) ) { | |
| 258 | - $new_role = $role_map[$user_role]; | |
| 484 | + // Use a mapped role or default role | |
| 485 | + $new_role = empty( $user_role ) || ! isset( $role_map[ $user_role ] ) | |
| 486 | + ? bbp_get_default_role() | |
| 487 | + : $role_map[ $user_role ]; | |
| 259 | 488 | |
| 260 | - // Use the default role | |
| 261 | - } else { | |
| 262 | - $new_role = bbp_get_default_role(); | |
| 263 | - } | |
| 264 | - | |
| 265 | 489 | /** Add or Map ************************************************************/ |
| 266 | 490 | |
| 267 | 491 | // Add the user to the site |
| 268 | - if ( true == $add_to_site ) { | |
| 269 | - $bbp->current_user->add_role( $new_role ); | |
| 492 | + if ( true === $add_to_site ) { | |
| 493 | + bbp_set_user_role( $user_id, $new_role ); | |
| 270 | 494 | |
| 271 | 495 | // Don't add the user, but still give them the correct caps dynamically |
| 272 | - } else { | |
| 273 | - $bbp->current_user->caps[$new_role] = true; | |
| 496 | + } else { | |
| 497 | + $bbp->current_user->caps[ $new_role ] = true; | |
| 274 | 498 | $bbp->current_user->get_role_caps(); |
| 275 | 499 | } |
| 276 | 500 | } |
| 277 | 501 | |
| @@ -279,9 +503,9 @@ | ||
| 279 | 503 | * Return a map of WordPress roles to bbPress roles. Used to automatically grant |
| 280 | 504 | * appropriate bbPress roles to WordPress users that wouldn't already have a |
| 281 | 505 | * role in the forums. Also guarantees WordPress admins get the Keymaster role. |
| 282 | 506 | * |
| 283 | - * @since bbPress (r4334) | |
| 507 | + * @since 2.2.0 bbPress (r4334) | |
| 284 | 508 | * |
| 285 | 509 | * @return array Filtered array of WordPress roles to bbPress roles |
| 286 | 510 | */ |
| 287 | 511 | function bbp_get_user_role_map() { |
| @@ -288,16 +512,19 @@ | ||
| 288 | 512 | |
| 289 | 513 | // Get the default role once here |
| 290 | 514 | $default_role = bbp_get_default_role(); |
| 291 | 515 | |
| 292 | - // Return filtered results, forcing admins to keymasters. | |
| 293 | - return (array) apply_filters( 'bbp_get_user_role_map', array ( | |
| 294 | - 'administrator' => bbp_get_keymaster_role(), | |
| 295 | - 'editor' => $default_role, | |
| 296 | - 'author' => $default_role, | |
| 297 | - 'contributor' => $default_role, | |
| 298 | - 'subscriber' => $default_role | |
| 299 | - ) ); | |
| 516 | + // Filter & return | |
| 517 | + return (array) apply_filters( | |
| 518 | + 'bbp_get_user_role_map', | |
| 519 | + array( | |
| 520 | + 'administrator' => bbp_get_keymaster_role(), | |
| 521 | + 'editor' => $default_role, | |
| 522 | + 'author' => $default_role, | |
| 523 | + 'contributor' => $default_role, | |
| 524 | + 'subscriber' => $default_role | |
| 525 | + ) | |
| 526 | + ); | |
| 300 | 527 | } |
| 301 | 528 | |
| 302 | 529 | /** User Status ***************************************************************/ |
| 303 | 530 | |
| @@ -303,9 +530,9 @@ | ||
| 303 | 530 | |
| 304 | 531 | /** |
| 305 | 532 | * Checks if the user has been marked as a spammer. |
| 306 | 533 | * |
| 307 | - * @since bbPress (r3355) | |
| 534 | + * @since 2.0.0 bbPress (r3355) | |
| 308 | 535 | * |
| 309 | 536 | * @param int $user_id int The ID for the user. |
| 310 | 537 | * @return bool True if spammer, False if not. |
| 311 | 538 | */ |
| @@ -311,14 +538,16 @@ | ||
| 311 | 538 | */ |
| 312 | 539 | function bbp_is_user_spammer( $user_id = 0 ) { |
| 313 | 540 | |
| 314 | 541 | // Default to current user |
| 315 | - if ( empty( $user_id ) && is_user_logged_in() ) | |
| 542 | + if ( empty( $user_id ) && is_user_logged_in() ) { | |
| 316 | 543 | $user_id = bbp_get_current_user_id(); |
| 544 | + } | |
| 317 | 545 | |
| 318 | 546 | // No user to check |
| 319 | - if ( empty( $user_id ) ) | |
| 547 | + if ( empty( $user_id ) ) { | |
| 320 | 548 | return false; |
| 549 | + } | |
| 321 | 550 | |
| 322 | 551 | // Assume user is not spam |
| 323 | 552 | $is_spammer = false; |
| 324 | 553 | |
| @@ -329,190 +558,198 @@ | ||
| 329 | 558 | if ( empty( $user ) ) { |
| 330 | 559 | $is_spammer = false; |
| 331 | 560 | |
| 332 | 561 | // Check if spam |
| 333 | - } elseif ( !empty( $user->spam ) ) { | |
| 562 | + } elseif ( ! empty( $user->spam ) ) { | |
| 334 | 563 | $is_spammer = true; |
| 335 | 564 | } |
| 336 | 565 | |
| 337 | - return (bool) apply_filters( 'bp_core_is_user_spammer', $is_spammer ); | |
| 566 | + // Filter & return | |
| 567 | + return (bool) apply_filters( 'bbp_core_is_user_spammer', $is_spammer ); | |
| 338 | 568 | } |
| 339 | 569 | |
| 340 | 570 | /** |
| 341 | 571 | * Mark a users topics and replies as spam when the user is marked as spam |
| 342 | 572 | * |
| 343 | - * @since bbPress (r3405) | |
| 573 | + * @since 2.0.0 bbPress (r3405) | |
| 344 | 574 | * |
| 345 | - * @global WPDB $wpdb | |
| 346 | 575 | * @param int $user_id Optional. User ID to spam. Defaults to displayed user. |
| 347 | - | |
| 348 | - * @uses bbp_is_single_user() | |
| 349 | - * @uses bbp_is_user_home() | |
| 350 | - * @uses bbp_get_displayed_user_field() | |
| 351 | - * @uses is_super_admin() | |
| 352 | - * @uses get_blogs_of_user() | |
| 353 | - * @uses get_current_blog_id() | |
| 354 | - * @uses bbp_get_topic_post_type() | |
| 355 | - * @uses bbp_get_reply_post_type() | |
| 356 | - * @uses switch_to_blog() | |
| 357 | - * @uses get_post_type() | |
| 358 | - * @uses bbp_spam_topic() | |
| 359 | - * @uses bbp_spam_reply() | |
| 360 | - * @uses restore_current_blog() | |
| 361 | 576 | * |
| 362 | - * @return If no user ID passed | |
| 577 | + * @return bool If no user ID passed. | |
| 363 | 578 | */ |
| 364 | 579 | function bbp_make_spam_user( $user_id = 0 ) { |
| 365 | 580 | |
| 366 | 581 | // Use displayed user if it's not yourself |
| 367 | - if ( empty( $user_id ) && bbp_is_single_user() && !bbp_is_user_home() ) | |
| 582 | + if ( empty( $user_id ) && bbp_is_single_user() && ! bbp_is_user_home() ) { | |
| 368 | 583 | $user_id = bbp_get_displayed_user_id(); |
| 584 | + } | |
| 369 | 585 | |
| 370 | 586 | // Bail if no user ID |
| 371 | - if ( empty( $user_id ) ) | |
| 372 | - return; | |
| 587 | + if ( empty( $user_id ) ) { | |
| 588 | + return false; | |
| 589 | + } | |
| 373 | 590 | |
| 374 | - // Bail if user ID is super admin | |
| 375 | - if ( is_super_admin( $user_id ) ) | |
| 376 | - return; | |
| 591 | + // Bail if user ID is keymaster | |
| 592 | + if ( bbp_is_user_keymaster( $user_id ) ) { | |
| 593 | + return false; | |
| 594 | + } | |
| 377 | 595 | |
| 378 | 596 | // Arm the torpedos |
| 379 | - global $wpdb; | |
| 597 | + $bbp_db = bbp_db(); | |
| 380 | 598 | |
| 381 | 599 | // Get the blog IDs of the user to mark as spam |
| 382 | 600 | $blogs = get_blogs_of_user( $user_id, true ); |
| 383 | 601 | |
| 384 | 602 | // If user has no blogs, they are a guest on this site |
| 385 | - if ( empty( $blogs ) ) | |
| 386 | - $blogs[$wpdb->blogid] = array(); | |
| 603 | + if ( empty( $blogs ) ) { | |
| 604 | + $blogs[ $bbp_db->blogid ] = array(); | |
| 605 | + } | |
| 387 | 606 | |
| 388 | - // Make array of post types to mark as spam | |
| 389 | - $post_types = array( bbp_get_topic_post_type(), bbp_get_reply_post_type() ); | |
| 390 | - $post_types = "'" . implode( "', '", $post_types ) . "'"; | |
| 391 | - $status = bbp_get_public_status_id(); | |
| 607 | + // Process replies before topics so topic status helpers run last | |
| 608 | + $post_types = array( bbp_get_reply_post_type(), bbp_get_topic_post_type() ); | |
| 392 | 609 | |
| 610 | + // Get array of statuses to mark as spam | |
| 611 | + $post_statuses = bbp_get_public_topic_statuses(); | |
| 612 | + $post_statuses = "'" . implode( "', '", $post_statuses ) . "'"; | |
| 613 | + | |
| 393 | 614 | // Loop through blogs and remove their posts |
| 394 | 615 | foreach ( (array) array_keys( $blogs ) as $blog_id ) { |
| 395 | 616 | |
| 396 | - // Switch to the blog ID | |
| 397 | - switch_to_blog( $blog_id ); | |
| 617 | + // Switch to the site ID | |
| 618 | + bbp_switch_to_site( $blog_id ); | |
| 398 | 619 | |
| 399 | - // Get topics and replies | |
| 400 | - $posts = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} WHERE post_author = {$user_id} AND post_status = '{$status}' AND post_type IN ({$post_types})" ); | |
| 620 | + foreach ( $post_types as $post_type ) { | |
| 401 | 621 | |
| 402 | - // Loop through posts and spam them | |
| 403 | - if ( !empty( $posts ) ) { | |
| 404 | - foreach ( $posts as $post_id ) { | |
| 622 | + // Get posts of this type | |
| 623 | + $query = $bbp_db->prepare( "SELECT ID FROM {$bbp_db->posts} WHERE post_author = %d AND post_status IN ( {$post_statuses} ) AND post_type = %s", $user_id, $post_type ); | |
| 624 | + $posts = $bbp_db->get_col( $query ); | |
| 405 | 625 | |
| 406 | - // The routines for topics ang replies are different, so use the | |
| 407 | - // correct one based on the post type | |
| 408 | - switch ( get_post_type( $post_id ) ) { | |
| 626 | + // Loop through posts and spam them | |
| 627 | + if ( ! empty( $posts ) ) { | |
| 628 | + foreach ( $posts as $post_id ) { | |
| 409 | 629 | |
| 410 | - case bbp_get_topic_post_type() : | |
| 411 | - bbp_spam_topic( $post_id ); | |
| 412 | - break; | |
| 630 | + // The routines for topics and replies are different, so use the | |
| 631 | + // correct one based on the post type | |
| 632 | + switch ( $post_type ) { | |
| 413 | 633 | |
| 414 | - case bbp_get_reply_post_type() : | |
| 415 | - bbp_spam_reply( $post_id ); | |
| 416 | - break; | |
| 634 | + case bbp_get_topic_post_type() : | |
| 635 | + bbp_spam_topic( $post_id ); | |
| 636 | + break; | |
| 637 | + | |
| 638 | + case bbp_get_reply_post_type() : | |
| 639 | + bbp_spam_reply( $post_id ); | |
| 640 | + break; | |
| 641 | + } | |
| 417 | 642 | } |
| 418 | 643 | } |
| 419 | 644 | } |
| 420 | 645 | |
| 421 | - // Switch back to current blog | |
| 422 | - restore_current_blog(); | |
| 646 | + // Switch back to current site | |
| 647 | + bbp_restore_current_site(); | |
| 423 | 648 | } |
| 649 | + | |
| 650 | + // Delete user options | |
| 651 | + bbp_delete_user_options( $user_id ); | |
| 652 | + | |
| 653 | + // Success | |
| 654 | + return true; | |
| 424 | 655 | } |
| 425 | 656 | |
| 426 | 657 | /** |
| 427 | 658 | * Mark a users topics and replies as spam when the user is marked as spam |
| 428 | 659 | * |
| 429 | - * @since bbPress (r3405) | |
| 660 | + * @since 2.0.0 bbPress (r3405) | |
| 430 | 661 | * |
| 431 | - * @global WPDB $wpdb | |
| 432 | 662 | * @param int $user_id Optional. User ID to unspam. Defaults to displayed user. |
| 433 | 663 | * |
| 434 | - * @uses bbp_is_single_user() | |
| 435 | - * @uses bbp_is_user_home() | |
| 436 | - * @uses bbp_get_displayed_user_field() | |
| 437 | - * @uses is_super_admin() | |
| 438 | - * @uses get_blogs_of_user() | |
| 439 | - * @uses bbp_get_topic_post_type() | |
| 440 | - * @uses bbp_get_reply_post_type() | |
| 441 | - * @uses switch_to_blog() | |
| 442 | - * @uses get_post_type() | |
| 443 | - * @uses bbp_unspam_topic() | |
| 444 | - * @uses bbp_unspam_reply() | |
| 445 | - * @uses restore_current_blog() | |
| 446 | - * | |
| 447 | - * @return If no user ID passed | |
| 664 | + * @return bool If no user ID passed. | |
| 448 | 665 | */ |
| 449 | 666 | function bbp_make_ham_user( $user_id = 0 ) { |
| 450 | 667 | |
| 451 | 668 | // Use displayed user if it's not yourself |
| 452 | - if ( empty( $user_id ) && bbp_is_single_user() && !bbp_is_user_home() ) | |
| 453 | - $user_id = bbp_get_displayed_user_field(); | |
| 669 | + if ( empty( $user_id ) && bbp_is_single_user() && ! bbp_is_user_home() ) { | |
| 670 | + $user_id = bbp_get_displayed_user_id(); | |
| 671 | + } | |
| 454 | 672 | |
| 455 | 673 | // Bail if no user ID |
| 456 | - if ( empty( $user_id ) ) | |
| 457 | - return; | |
| 674 | + if ( empty( $user_id ) ) { | |
| 675 | + return false; | |
| 676 | + } | |
| 458 | 677 | |
| 459 | - // Bail if user ID is super admin | |
| 460 | - if ( is_super_admin( $user_id ) ) | |
| 461 | - return; | |
| 678 | + // Bail if user ID is keymaster | |
| 679 | + if ( bbp_is_user_keymaster( $user_id ) ) { | |
| 680 | + return false; | |
| 681 | + } | |
| 462 | 682 | |
| 463 | 683 | // Arm the torpedos |
| 464 | - global $wpdb; | |
| 684 | + $bbp_db = bbp_db(); | |
| 465 | 685 | |
| 466 | 686 | // Get the blog IDs of the user to mark as spam |
| 467 | 687 | $blogs = get_blogs_of_user( $user_id, true ); |
| 468 | 688 | |
| 469 | 689 | // If user has no blogs, they are a guest on this site |
| 470 | - if ( empty( $blogs ) ) | |
| 471 | - $blogs[$wpdb->blogid] = array(); | |
| 690 | + if ( empty( $blogs ) ) { | |
| 691 | + $blogs[ $bbp_db->blogid ] = array(); | |
| 692 | + } | |
| 472 | 693 | |
| 473 | - // Make array of post types to mark as spam | |
| 474 | - $post_types = array( bbp_get_topic_post_type(), bbp_get_reply_post_type() ); | |
| 475 | - $post_types = "'" . implode( "', '", $post_types ) . "'"; | |
| 476 | - $status = bbp_get_spam_status_id(); | |
| 694 | + // Process replies before topics so topic status helpers run last | |
| 695 | + $post_types = array( bbp_get_reply_post_type(), bbp_get_topic_post_type() ); | |
| 477 | 696 | |
| 697 | + // Get array of statuses to unmark as spam | |
| 698 | + $post_statuses = array( bbp_get_spam_status_id() ); | |
| 699 | + $post_statuses = "'" . implode( "', '", $post_statuses ) . "'"; | |
| 700 | + | |
| 478 | 701 | // Loop through blogs and remove their posts |
| 479 | 702 | foreach ( (array) array_keys( $blogs ) as $blog_id ) { |
| 480 | 703 | |
| 481 | - // Switch to the blog ID | |
| 482 | - switch_to_blog( $blog_id ); | |
| 704 | + // Switch to the site ID | |
| 705 | + bbp_switch_to_site( $blog_id ); | |
| 483 | 706 | |
| 484 | - // Get topics and replies | |
| 485 | - $posts = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} WHERE post_author = {$user_id} AND post_status = '{$status}' AND post_type IN ({$post_types})" ); | |
| 707 | + foreach ( $post_types as $post_type ) { | |
| 486 | 708 | |
| 487 | - // Loop through posts and spam them | |
| 488 | - if ( !empty( $posts ) ) { | |
| 489 | - foreach ( $posts as $post_id ) { | |
| 709 | + // Get posts of this type | |
| 710 | + $query = $bbp_db->prepare( "SELECT ID FROM {$bbp_db->posts} WHERE post_author = %d AND post_status IN ( {$post_statuses} ) AND post_type = %s", $user_id, $post_type ); | |
| 711 | + $posts = $bbp_db->get_col( $query ); | |
| 490 | 712 | |
| 491 | - // The routines for topics ang replies are different, so use the | |
| 492 | - // correct one based on the post type | |
| 493 | - switch ( get_post_type( $post_id ) ) { | |
| 713 | + // Loop through posts and unspam them | |
| 714 | + if ( ! empty( $posts ) ) { | |
| 715 | + foreach ( $posts as $post_id ) { | |
| 494 | 716 | |
| 495 | - case bbp_get_topic_post_type() : | |
| 496 | - bbp_unspam_topic( $post_id ); | |
| 497 | - break; | |
| 717 | + // The routines for topics and replies are different, so use the | |
| 718 | + // correct one based on the post type | |
| 719 | + switch ( $post_type ) { | |
| 498 | 720 | |
| 499 | - case bbp_get_reply_post_type() : | |
| 500 | - bbp_unspam_reply( $post_id ); | |
| 501 | - break; | |
| 721 | + case bbp_get_topic_post_type() : | |
| 722 | + bbp_unspam_topic( $post_id ); | |
| 723 | + break; | |
| 724 | + | |
| 725 | + case bbp_get_reply_post_type() : | |
| 726 | + bbp_unspam_reply( $post_id ); | |
| 727 | + break; | |
| 728 | + } | |
| 502 | 729 | } |
| 503 | 730 | } |
| 504 | 731 | } |
| 505 | 732 | |
| 506 | - // Switch back to current blog | |
| 507 | - restore_current_blog(); | |
| 733 | + // Switch back to current site | |
| 734 | + bbp_restore_current_site(); | |
| 508 | 735 | } |
| 736 | + | |
| 737 | + // Update topic & reply counts | |
| 738 | + bbp_update_user_topic_count( $user_id, bbp_get_user_topic_count_raw( $user_id ) ); | |
| 739 | + bbp_update_user_reply_count( $user_id, bbp_get_user_reply_count_raw( $user_id ) ); | |
| 740 | + | |
| 741 | + // Update last posted (to now) | |
| 742 | + bbp_update_user_last_posted( $user_id ); | |
| 743 | + | |
| 744 | + // Success | |
| 745 | + return true; | |
| 509 | 746 | } |
| 510 | 747 | |
| 511 | 748 | /** |
| 512 | 749 | * Checks if the user has been marked as deleted. |
| 513 | 750 | * |
| 514 | - * @since bbPress (r3355) | |
| 751 | + * @since 2.0.0 bbPress (r3355) | |
| 515 | 752 | * |
| 516 | 753 | * @param int $user_id int The ID for the user. |
| 517 | 754 | * @return bool True if deleted, False if not. |
| 518 | 755 | */ |
| @@ -518,14 +755,16 @@ | ||
| 518 | 755 | */ |
| 519 | 756 | function bbp_is_user_deleted( $user_id = 0 ) { |
| 520 | 757 | |
| 521 | 758 | // Default to current user |
| 522 | - if ( empty( $user_id ) && is_user_logged_in() ) | |
| 759 | + if ( empty( $user_id ) && is_user_logged_in() ) { | |
| 523 | 760 | $user_id = bbp_get_current_user_id(); |
| 761 | + } | |
| 524 | 762 | |
| 525 | 763 | // No user to check |
| 526 | - if ( empty( $user_id ) ) | |
| 764 | + if ( empty( $user_id ) ) { | |
| 527 | 765 | return false; |
| 766 | + } | |
| 528 | 767 | |
| 529 | 768 | // Assume user is not deleted |
| 530 | 769 | $is_deleted = false; |
| 531 | 770 | |
| @@ -536,45 +775,41 @@ | ||
| 536 | 775 | if ( empty( $user ) ) { |
| 537 | 776 | $is_deleted = true; |
| 538 | 777 | |
| 539 | 778 | // Check if deleted |
| 540 | - } elseif ( !empty( $user->deleted ) ) { | |
| 779 | + } elseif ( ! empty( $user->deleted ) ) { | |
| 541 | 780 | $is_deleted = true; |
| 542 | 781 | } |
| 543 | 782 | |
| 544 | - return (bool) apply_filters( 'bp_core_is_user_deleted', $is_deleted ); | |
| 783 | + // Filter & return | |
| 784 | + return (bool) apply_filters( 'bbp_core_is_user_deleted', $is_deleted ); | |
| 545 | 785 | } |
| 546 | 786 | |
| 547 | 787 | /** |
| 548 | 788 | * Checks if user is active |
| 549 | 789 | * |
| 550 | - * @since bbPress (r3502) | |
| 790 | + * @since 2.0.0 bbPress (r3502) | |
| 551 | 791 | * |
| 552 | - * @uses is_user_logged_in() To check if user is logged in | |
| 553 | - * @uses bbp_get_displayed_user_id() To get current user ID | |
| 554 | - * @uses bbp_is_user_spammer() To check if user is spammer | |
| 555 | - * @uses bbp_is_user_deleted() To check if user is deleted | |
| 556 | - * | |
| 557 | 792 | * @param int $user_id The user ID to check |
| 558 | 793 | * @return bool True if public, false if not |
| 559 | 794 | */ |
| 560 | 795 | function bbp_is_user_active( $user_id = 0 ) { |
| 561 | 796 | |
| 562 | - // Default to current user | |
| 563 | - if ( empty( $user_id ) && is_user_logged_in() ) | |
| 564 | - $user_id = bbp_get_current_user_id(); | |
| 565 | - | |
| 566 | 797 | // No user to check |
| 567 | - if ( empty( $user_id ) ) | |
| 798 | + $user_id = bbp_get_user_id( $user_id, false, true ); | |
| 799 | + if ( empty( $user_id ) ) { | |
| 568 | 800 | return false; |
| 801 | + } | |
| 569 | 802 | |
| 570 | 803 | // Check spam |
| 571 | - if ( bbp_is_user_spammer( $user_id ) ) | |
| 804 | + if ( bbp_is_user_spammer( $user_id ) ) { | |
| 572 | 805 | return false; |
| 806 | + } | |
| 573 | 807 | |
| 574 | 808 | // Check deleted |
| 575 | - if ( bbp_is_user_deleted( $user_id ) ) | |
| 809 | + if ( bbp_is_user_deleted( $user_id ) ) { | |
| 576 | 810 | return false; |
| 811 | + } | |
| 577 | 812 | |
| 578 | 813 | // Assume true if not spam or deleted |
| 579 | 814 | return true; |
| 580 | 815 | } |
| @@ -581,46 +816,41 @@ | ||
| 581 | 816 | |
| 582 | 817 | /** |
| 583 | 818 | * Checks if user is not active. |
| 584 | 819 | * |
| 585 | - * @since bbPress (r3502) | |
| 820 | + * @since 2.0.0 bbPress (r3502) | |
| 586 | 821 | * |
| 587 | - * @uses is_user_logged_in() To check if user is logged in | |
| 588 | - * @uses bbp_get_displayed_user_id() To get current user ID | |
| 589 | - * @uses bbp_is_user_active() To check if user is active | |
| 590 | - * | |
| 591 | 822 | * @param int $user_id The user ID to check. Defaults to current user ID |
| 592 | 823 | * @return bool True if inactive, false if active |
| 593 | 824 | */ |
| 594 | 825 | function bbp_is_user_inactive( $user_id = 0 ) { |
| 826 | + return ! bbp_is_user_active( $user_id ); | |
| 827 | +} | |
| 595 | 828 | |
| 596 | - // Default to current user | |
| 597 | - if ( empty( $user_id ) && is_user_logged_in() ) | |
| 598 | - $user_id = bbp_get_current_user_id(); | |
| 829 | +/** | |
| 830 | + * Checks if user is a keymaster | |
| 831 | + * | |
| 832 | + * @since 2.3.0 bbPress (r4783) | |
| 833 | + * | |
| 834 | + * @param int $user_id | |
| 835 | + * @return bool True if keymaster, false if not | |
| 836 | + */ | |
| 837 | +function bbp_is_user_keymaster( $user_id = 0 ) { | |
| 838 | + $_user_id = bbp_get_user_id( $user_id, false, true ); | |
| 839 | + $retval = user_can( $_user_id, 'keep_gate' ); | |
| 599 | 840 | |
| 600 | - // No user to check | |
| 601 | - if ( empty( $user_id ) ) | |
| 602 | - return false; | |
| 603 | - | |
| 604 | - // Return the inverse of active | |
| 605 | - return !bbp_is_user_active( $user_id ); | |
| 841 | + // Filter & return | |
| 842 | + return (bool) apply_filters( 'bbp_is_user_keymaster', $retval, $_user_id, $user_id ); | |
| 606 | 843 | } |
| 607 | 844 | |
| 608 | 845 | /** |
| 609 | 846 | * Does a user have a profile for the current site |
| 610 | 847 | * |
| 611 | - * @since bbPress (r4362) | |
| 848 | + * @since 2.2.0 bbPress (r4362) | |
| 612 | 849 | * |
| 613 | 850 | * @param int $user_id User ID to check |
| 614 | - * @param int $blog_id Blog ID to check | |
| 615 | 851 | * |
| 616 | - * @uses bbp_get_user_id() To verify the user ID | |
| 617 | - * @uses get_userdata() To get the user's data | |
| 618 | - * @uses is_super_admin() To determine if user can see inactive users | |
| 619 | - * @uses bbp_is_user_inactive() To check if user is spammer or deleted | |
| 620 | - * @uses apply_filters() To allow override of this functions result | |
| 621 | - * | |
| 622 | - * @return boolean Whether or not the user has a profile on this blog_id | |
| 852 | + * @return bool Whether or not the user has a profile on this blog_id. | |
| 623 | 853 | */ |
| 624 | 854 | function bbp_user_has_profile( $user_id = 0 ) { |
| 625 | 855 | |
| 626 | 856 | // Assume every user has a profile |
| @@ -635,12 +865,92 @@ | ||
| 635 | 865 | // No user found, return false |
| 636 | 866 | if ( empty( $user ) ) { |
| 637 | 867 | $retval = false; |
| 638 | 868 | |
| 639 | - // User is inactive, and current user is not a super admin | |
| 640 | - } elseif ( ! is_super_admin() && bbp_is_user_inactive( $user->ID ) ) { | |
| 869 | + // User is inactive, and current user is not a keymaster | |
| 870 | + } elseif ( ! bbp_is_user_keymaster() && bbp_is_user_inactive( $user->ID ) ) { | |
| 641 | 871 | $retval = false; |
| 642 | 872 | } |
| 643 | 873 | |
| 644 | - // Filter and return | |
| 874 | + // Filter & return | |
| 645 | 875 | return (bool) apply_filters( 'bbp_show_user_profile', $retval, $user_id ); |
| 876 | +} | |
| 877 | + | |
| 878 | +/** Moderators ****************************************************************/ | |
| 879 | + | |
| 880 | +/** | |
| 881 | + * Add a moderator to an object | |
| 882 | + * | |
| 883 | + * @since 2.6.0 bbPress (r6056) | |
| 884 | + * | |
| 885 | + * @param int $object_id Traditionally a post ID | |
| 886 | + * @param int $user_id User ID | |
| 887 | + * @param string $object_type Type of meta (post,term,user,comment) | |
| 888 | + * | |
| 889 | + * @return bool | |
| 890 | + */ | |
| 891 | +function bbp_add_moderator( $object_id = 0, $user_id = 0, $object_type = 'post' ) { | |
| 892 | + return bbp_add_user_to_object( $object_id, $user_id, '_bbp_moderator_id', $object_type ); | |
| 893 | +} | |
| 894 | + | |
| 895 | +/** | |
| 896 | + * Remove a moderator user ID from an object | |
| 897 | + * | |
| 898 | + * @since 2.6.0 bbPress (r6056) | |
| 899 | + * | |
| 900 | + * @param int $object_id Traditionally a post ID | |
| 901 | + * @param int $user_id User ID | |
| 902 | + * @param string $object_type Type of meta (post,term,user,comment) | |
| 903 | + * | |
| 904 | + * @return bool | |
| 905 | + */ | |
| 906 | +function bbp_remove_moderator( $object_id = 0, $user_id = 0, $object_type = 'post' ) { | |
| 907 | + return bbp_remove_user_from_object( $object_id, $user_id, '_bbp_moderator_id', $object_type ); | |
| 908 | +} | |
| 909 | + | |
| 910 | +/** | |
| 911 | + * Get user IDs of moderators for an object | |
| 912 | + * | |
| 913 | + * @since 2.6.0 bbPress (r6056) | |
| 914 | + * | |
| 915 | + * @param int $object_id Traditionally a post ID | |
| 916 | + * @param string $object_type Type of meta (post,term,user,comment) | |
| 917 | + * | |
| 918 | + * @return array | |
| 919 | + */ | |
| 920 | +function bbp_get_moderator_ids( $object_id = 0, $object_type = 'post' ) { | |
| 921 | + return bbp_get_users_for_object( $object_id, '_bbp_moderator_id', $object_type ); | |
| 922 | +} | |
| 923 | + | |
| 924 | +/** | |
| 925 | + * Get moderators for a specific object ID. Will return global moderators when | |
| 926 | + * object ID is empty. | |
| 927 | + * | |
| 928 | + * @since 2.6.0 bbPress (r6056) | |
| 929 | + * | |
| 930 | + * @param int $object_id Traditionally a post ID | |
| 931 | + * @param string $object_type Type of meta (post,term,user,comment) | |
| 932 | + * | |
| 933 | + * @return array | |
| 934 | + */ | |
| 935 | +function bbp_get_moderators( $object_id = 0, $object_type = 'post' ) { | |
| 936 | + | |
| 937 | + // Get global moderators | |
| 938 | + if ( empty( $object_id ) ) { | |
| 939 | + $users = get_users( | |
| 940 | + array( | |
| 941 | + 'role__in' => bbp_get_moderator_role(), | |
| 942 | + ) | |
| 943 | + ); | |
| 944 | + | |
| 945 | + // Get object moderators | |
| 946 | + } else { | |
| 947 | + $users = get_users( | |
| 948 | + array( | |
| 949 | + 'include' => bbp_get_moderator_ids( $object_id, $object_type ), | |
| 950 | + ) | |
| 951 | + ); | |
| 952 | + } | |
| 953 | + | |
| 954 | + // Filter & return | |
| 955 | + return (array) apply_filters( 'bbp_get_moderators', $users, $object_id, $object_type ); | |
| 646 | 956 | } |