Skip to:
Content

bbPress.org


Ignore:
Timestamp:
11/06/2012 01:21:55 AM (14 years ago)
Author:
johnjamesjacoby
Message:

Capabilities:

  • Move status and role user functions out of users/functions.php and into users/capabilities.php.
  • See #1939.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/includes/users/capabilities.php

    r4337 r4346  
    4343
    4444        return apply_filters( 'bbp_map_primary_meta_caps', $caps, $cap, $user_id, $args );
     45}
     46
     47/**
     48 * Return a user's main role
     49 *
     50 * @since bbPress (r3860)
     51 *
     52 * @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
     57 */
     58function bbp_set_user_role( $user_id = 0, $new_role = '' ) {
     59
     60        // Validate user id
     61        $user_id = bbp_get_user_id( $user_id, false, false );
     62        $user    = get_userdata( $user_id );
     63
     64        // User exists
     65        if ( !empty( $user ) ) {
     66
     67                // Get users forum role
     68                $role = bbp_get_user_role( $user_id );
     69
     70                // User already has this role so no new role is set
     71                if ( $new_role == $role ) {
     72                        $new_role = false;
     73
     74                // Users role is different than the new role
     75                } else {
     76
     77                        // Remove the old role
     78                        if ( ! empty( $role ) ) {
     79                                $user->remove_role( $role );
     80                        }
     81
     82                        // Add the new role
     83                        $user->add_role( $new_role );
     84                }
     85
     86        // User does don exist so return false
     87        } else {
     88                $new_role = false;
     89        }
     90
     91        return apply_filters( 'bbp_set_user_role', $new_role, $user_id, $user );
     92}
     93
     94/**
     95 * Return a user's main role
     96 *
     97 * @since bbPress (r3860)
     98 *
     99 * @param int $user_id
     100 * @uses bbp_get_user_id() To get the user id
     101 * @uses get_userdata() To get the user data
     102 * @uses apply_filters() Calls 'bbp_get_user_role' with the role and user id
     103 * @return string
     104 */
     105function bbp_get_user_role( $user_id = 0 ) {
     106
     107        // Validate user id
     108        $user_id = bbp_get_user_id( $user_id, false, false );
     109        $user    = get_userdata( $user_id );
     110        $role    = false;
     111
     112        // User has roles so lets
     113        if ( ! empty( $user->roles ) ) {
     114                $roles = array_intersect( array_values( $user->roles ), array_keys( bbp_get_editable_roles() ) );
     115
     116                // If there's a role in the array, use the first one
     117                if ( !empty( $roles ) ) {
     118                        $role = array_shift( array_values( $roles ) );
     119                }
     120        }
     121
     122        return apply_filters( 'bbp_get_user_role', $role, $user_id, $user );
    45123}
    46124
     
    151229        ) );
    152230}
     231
     232/** User Status ***************************************************************/
     233
     234/**
     235 * Checks if the user has been marked as a spammer.
     236 *
     237 * @since bbPress (r3355)
     238 *
     239 * @param int $user_id int The ID for the user.
     240 * @return bool True if spammer, False if not.
     241 */
     242function bbp_is_user_spammer( $user_id = 0 ) {
     243
     244        // Default to current user
     245        if ( empty( $user_id ) && is_user_logged_in() )
     246                $user_id = bbp_get_current_user_id();
     247
     248        // No user to check
     249        if ( empty( $user_id ) )
     250                return false;
     251
     252        // Assume user is not spam
     253        $is_spammer = false;
     254
     255        // Get user data
     256        $user = get_userdata( $user_id );
     257
     258        // No user found
     259        if ( empty( $user ) ) {
     260                $is_spammer = false;
     261
     262        // User found
     263        } else {
     264
     265                // Check if spam
     266                if ( !empty( $user->spam ) )
     267                        $is_spammer = true;
     268
     269                if ( 1 == $user->user_status )
     270                        $is_spammer = true;
     271        }
     272
     273        return apply_filters( 'bp_core_is_user_spammer', (bool) $is_spammer );
     274}
     275
     276/**
     277 * Mark a users topics and replies as spam when the user is marked as spam
     278 *
     279 * @since bbPress (r3405)
     280 *
     281 * @global WPDB $wpdb
     282 * @param int $user_id Optional. User ID to spam. Defaults to displayed user.
     283
     284 * @uses bbp_is_single_user()
     285 * @uses bbp_is_user_home()
     286 * @uses bbp_get_displayed_user_field()
     287 * @uses is_super_admin()
     288 * @uses get_blogs_of_user()
     289 * @uses get_current_blog_id()
     290 * @uses bbp_get_topic_post_type()
     291 * @uses bbp_get_reply_post_type()
     292 * @uses switch_to_blog()
     293 * @uses get_post_type()
     294 * @uses bbp_spam_topic()
     295 * @uses bbp_spam_reply()
     296 * @uses restore_current_blog()
     297 *
     298 * @return If no user ID passed
     299 */
     300function bbp_make_spam_user( $user_id = 0 ) {
     301
     302        // Use displayed user if it's not yourself
     303        if ( empty( $user_id ) && bbp_is_single_user() && !bbp_is_user_home() )
     304                $user_id = bbp_get_displayed_user_id();
     305
     306        // Bail if no user ID
     307        if ( empty( $user_id ) )
     308                return;
     309
     310        // Bail if user ID is super admin
     311        if ( is_super_admin( $user_id ) )
     312                return;
     313
     314        // Arm the torpedos
     315        global $wpdb;
     316
     317        // Get the blog IDs of the user to mark as spam
     318        $blogs = get_blogs_of_user( $user_id, true );
     319
     320        // If user has no blogs, they are a guest on this site
     321        if ( empty( $blogs ) )
     322                $blogs[$wpdb->blogid] = array();
     323
     324        // Make array of post types to mark as spam
     325        $post_types  = array( bbp_get_topic_post_type(), bbp_get_reply_post_type() );
     326        $post_types  = "'" . implode( "', '", $post_types ) . "'";
     327        $status      = bbp_get_public_status_id();
     328
     329        // Loop through blogs and remove their posts
     330        foreach ( (array) array_keys( $blogs ) as $blog_id ) {
     331
     332                // Switch to the blog ID
     333                switch_to_blog( $blog_id );
     334
     335                // Get topics and replies
     336                $posts = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} WHERE post_author = {$user_id} AND post_status = '{$status}' AND post_type IN ({$post_types})" );
     337
     338                // Loop through posts and spam them
     339                if ( !empty( $posts ) ) {
     340                        foreach ( $posts as $post_id ) {
     341
     342                                // The routines for topics ang replies are different, so use the
     343                                // correct one based on the post type
     344                                switch ( get_post_type( $post_id ) ) {
     345
     346                                        case bbp_get_topic_post_type() :
     347                                                bbp_spam_topic( $post_id );
     348                                                break;
     349
     350                                        case bbp_get_reply_post_type() :
     351                                                bbp_spam_reply( $post_id );
     352                                                break;
     353                                }
     354                        }
     355                }
     356
     357                // Switch back to current blog
     358                restore_current_blog();
     359        }
     360}
     361
     362/**
     363 * Mark a users topics and replies as spam when the user is marked as spam
     364 *
     365 * @since bbPress (r3405)
     366 *
     367 * @global WPDB $wpdb
     368 * @param int $user_id Optional. User ID to unspam. Defaults to displayed user.
     369 *
     370 * @uses bbp_is_single_user()
     371 * @uses bbp_is_user_home()
     372 * @uses bbp_get_displayed_user_field()
     373 * @uses is_super_admin()
     374 * @uses get_blogs_of_user()
     375 * @uses bbp_get_topic_post_type()
     376 * @uses bbp_get_reply_post_type()
     377 * @uses switch_to_blog()
     378 * @uses get_post_type()
     379 * @uses bbp_unspam_topic()
     380 * @uses bbp_unspam_reply()
     381 * @uses restore_current_blog()
     382 *
     383 * @return If no user ID passed
     384 */
     385function bbp_make_ham_user( $user_id = 0 ) {
     386
     387        // Use displayed user if it's not yourself
     388        if ( empty( $user_id ) && bbp_is_single_user() && !bbp_is_user_home() )
     389                $user_id = bbp_get_displayed_user_field();
     390
     391        // Bail if no user ID
     392        if ( empty( $user_id ) )
     393                return;
     394
     395        // Bail if user ID is super admin
     396        if ( is_super_admin( $user_id ) )
     397                return;
     398
     399        // Arm the torpedos
     400        global $wpdb;
     401
     402        // Get the blog IDs of the user to mark as spam
     403        $blogs = get_blogs_of_user( $user_id, true );
     404
     405        // If user has no blogs, they are a guest on this site
     406        if ( empty( $blogs ) )
     407                $blogs[$wpdb->blogid] = array();
     408
     409        // Make array of post types to mark as spam
     410        $post_types = array( bbp_get_topic_post_type(), bbp_get_reply_post_type() );
     411        $post_types = "'" . implode( "', '", $post_types ) . "'";
     412        $status     = bbp_get_spam_status_id();
     413
     414        // Loop through blogs and remove their posts
     415        foreach ( (array) array_keys( $blogs ) as $blog_id ) {
     416
     417                // Switch to the blog ID
     418                switch_to_blog( $blog_id );
     419
     420                // Get topics and replies
     421                $posts = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} WHERE post_author = {$user_id} AND post_status = '{$status}' AND post_type IN ({$post_types})" );
     422
     423                // Loop through posts and spam them
     424                if ( !empty( $posts ) ) {
     425                        foreach ( $posts as $post_id ) {
     426
     427                                // The routines for topics ang replies are different, so use the
     428                                // correct one based on the post type
     429                                switch ( get_post_type( $post_id ) ) {
     430
     431                                        case bbp_get_topic_post_type() :
     432                                                bbp_unspam_topic( $post_id );
     433                                                break;
     434
     435                                        case bbp_get_reply_post_type() :
     436                                                bbp_unspam_reply( $post_id );
     437                                                break;
     438                                }
     439                        }
     440                }
     441
     442                // Switch back to current blog
     443                restore_current_blog();
     444        }
     445}
     446
     447/**
     448 * Checks if the user has been marked as deleted.
     449 *
     450 * @since bbPress (r3355)
     451 *
     452 * @param int $user_id int The ID for the user.
     453 * @return bool True if deleted, False if not.
     454 */
     455function bbp_is_user_deleted( $user_id = 0 ) {
     456
     457        // Default to current user
     458        if ( empty( $user_id ) && is_user_logged_in() )
     459                $user_id = bbp_get_current_user_id();
     460
     461        // No user to check
     462        if ( empty( $user_id ) )
     463                return false;
     464
     465        // Assume user is not deleted
     466        $is_deleted = false;
     467
     468        // Get user data
     469        $user = get_userdata( $user_id );
     470
     471        // No user found
     472        if ( empty( $user ) ) {
     473                $is_deleted = true;
     474
     475        // User found
     476        } else {
     477
     478                // Check if deleted
     479                if ( !empty( $user->deleted ) )
     480                        $is_deleted = true;
     481
     482                if ( 2 == $user->user_status )
     483                        $is_deleted = true;
     484
     485        }
     486
     487        return apply_filters( 'bp_core_is_user_deleted', (bool) $is_deleted );
     488}
     489
     490/**
     491 * Checks if user is active
     492 *
     493 * @since bbPress (r3502)
     494 *
     495 * @uses is_user_logged_in() To check if user is logged in
     496 * @uses bbp_get_displayed_user_id() To get current user ID
     497 * @uses bbp_is_user_spammer() To check if user is spammer
     498 * @uses bbp_is_user_deleted() To check if user is deleted
     499 *
     500 * @param int $user_id The user ID to check
     501 * @return bool True if public, false if not
     502 */
     503function bbp_is_user_active( $user_id = 0 ) {
     504
     505        // Default to current user
     506        if ( empty( $user_id ) && is_user_logged_in() )
     507                $user_id = bbp_get_current_user_id();
     508
     509        // No user to check
     510        if ( empty( $user_id ) )
     511                return false;
     512
     513        // Check spam
     514        if ( bbp_is_user_spammer( $user_id ) )
     515                return false;
     516
     517        // Check deleted
     518        if ( bbp_is_user_deleted( $user_id ) )
     519                return false;
     520
     521        // Assume true if not spam or deleted
     522        return true;
     523}
     524
     525/**
     526 * Checks if user is not active.
     527 *
     528 * @since bbPress (r3502)
     529 *
     530 * @uses is_user_logged_in() To check if user is logged in
     531 * @uses bbp_get_displayed_user_id() To get current user ID
     532 * @uses bbp_is_user_active() To check if user is active
     533 *
     534 * @param int $user_id The user ID to check. Defaults to current user ID
     535 * @return bool True if inactive, false if active
     536 */
     537function bbp_is_user_inactive( $user_id = 0 ) {
     538
     539        // Default to current user
     540        if ( empty( $user_id ) && is_user_logged_in() )
     541                $user_id = bbp_get_current_user_id();
     542
     543        // No user to check
     544        if ( empty( $user_id ) )
     545                return false;
     546
     547        // Return the inverse of active
     548        return !bbp_is_user_active( $user_id );
     549}
Note: See TracChangeset for help on using the changeset viewer.