Skip to:
Content

bbPress.org

Changeset 4346


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.
Location:
trunk/includes/users
Files:
2 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}
  • trunk/includes/users/functions.php

    r4340 r4346  
    10491049}
    10501050
    1051 /** User Status ***************************************************************/
    1052 
    1053 /**
    1054  * Checks if the user has been marked as a spammer.
    1055  *
    1056  * @since bbPress (r3355)
    1057  *
    1058  * @param int $user_id int The ID for the user.
    1059  * @return bool True if spammer, False if not.
    1060  */
    1061 function bbp_is_user_spammer( $user_id = 0 ) {
    1062 
    1063         // Default to current user
    1064         if ( empty( $user_id ) && is_user_logged_in() )
    1065                 $user_id = bbp_get_current_user_id();
    1066 
    1067         // No user to check
    1068         if ( empty( $user_id ) )
    1069                 return false;
    1070 
    1071         // Assume user is not spam
    1072         $is_spammer = false;
    1073 
    1074         // Get user data
    1075         $user = get_userdata( $user_id );
    1076 
    1077         // No user found
    1078         if ( empty( $user ) ) {
    1079                 $is_spammer = false;
    1080 
    1081         // User found
    1082         } else {
    1083 
    1084                 // Check if spam
    1085                 if ( !empty( $user->spam ) )
    1086                         $is_spammer = true;
    1087 
    1088                 if ( 1 == $user->user_status )
    1089                         $is_spammer = true;
    1090         }
    1091 
    1092         return apply_filters( 'bp_core_is_user_spammer', (bool) $is_spammer );
    1093 }
    1094 
    1095 /**
    1096  * Mark a users topics and replies as spam when the user is marked as spam
    1097  *
    1098  * @since bbPress (r3405)
    1099  *
    1100  * @global WPDB $wpdb
    1101  * @param int $user_id Optional. User ID to spam. Defaults to displayed user.
    1102 
    1103  * @uses bbp_is_single_user()
    1104  * @uses bbp_is_user_home()
    1105  * @uses bbp_get_displayed_user_field()
    1106  * @uses is_super_admin()
    1107  * @uses get_blogs_of_user()
    1108  * @uses get_current_blog_id()
    1109  * @uses bbp_get_topic_post_type()
    1110  * @uses bbp_get_reply_post_type()
    1111  * @uses switch_to_blog()
    1112  * @uses get_post_type()
    1113  * @uses bbp_spam_topic()
    1114  * @uses bbp_spam_reply()
    1115  * @uses restore_current_blog()
    1116  *
    1117  * @return If no user ID passed
    1118  */
    1119 function bbp_make_spam_user( $user_id = 0 ) {
    1120 
    1121         // Use displayed user if it's not yourself
    1122         if ( empty( $user_id ) && bbp_is_single_user() && !bbp_is_user_home() )
    1123                 $user_id = bbp_get_displayed_user_id();
    1124 
    1125         // Bail if no user ID
    1126         if ( empty( $user_id ) )
    1127                 return;
    1128 
    1129         // Bail if user ID is super admin
    1130         if ( is_super_admin( $user_id ) )
    1131                 return;
    1132 
    1133         // Arm the torpedos
    1134         global $wpdb;
    1135 
    1136         // Get the blog IDs of the user to mark as spam
    1137         $blogs = get_blogs_of_user( $user_id, true );
    1138 
    1139         // If user has no blogs, they are a guest on this site
    1140         if ( empty( $blogs ) )
    1141                 $blogs[$wpdb->blogid] = array();
    1142 
    1143         // Make array of post types to mark as spam
    1144         $post_types  = array( bbp_get_topic_post_type(), bbp_get_reply_post_type() );
    1145         $post_types  = "'" . implode( "', '", $post_types ) . "'";
    1146         $status      = bbp_get_public_status_id();
    1147 
    1148         // Loop through blogs and remove their posts
    1149         foreach ( (array) array_keys( $blogs ) as $blog_id ) {
    1150 
    1151                 // Switch to the blog ID
    1152                 switch_to_blog( $blog_id );
    1153 
    1154                 // Get topics and replies
    1155                 $posts = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} WHERE post_author = {$user_id} AND post_status = '{$status}' AND post_type IN ({$post_types})" );
    1156 
    1157                 // Loop through posts and spam them
    1158                 if ( !empty( $posts ) ) {
    1159                         foreach ( $posts as $post_id ) {
    1160 
    1161                                 // The routines for topics ang replies are different, so use the
    1162                                 // correct one based on the post type
    1163                                 switch ( get_post_type( $post_id ) ) {
    1164 
    1165                                         case bbp_get_topic_post_type() :
    1166                                                 bbp_spam_topic( $post_id );
    1167                                                 break;
    1168 
    1169                                         case bbp_get_reply_post_type() :
    1170                                                 bbp_spam_reply( $post_id );
    1171                                                 break;
    1172                                 }
    1173                         }
    1174                 }
    1175 
    1176                 // Switch back to current blog
    1177                 restore_current_blog();
    1178         }
    1179 }
    1180 
    1181 /**
    1182  * Mark a users topics and replies as spam when the user is marked as spam
    1183  *
    1184  * @since bbPress (r3405)
    1185  *
    1186  * @global WPDB $wpdb
    1187  * @param int $user_id Optional. User ID to unspam. Defaults to displayed user.
    1188  *
    1189  * @uses bbp_is_single_user()
    1190  * @uses bbp_is_user_home()
    1191  * @uses bbp_get_displayed_user_field()
    1192  * @uses is_super_admin()
    1193  * @uses get_blogs_of_user()
    1194  * @uses bbp_get_topic_post_type()
    1195  * @uses bbp_get_reply_post_type()
    1196  * @uses switch_to_blog()
    1197  * @uses get_post_type()
    1198  * @uses bbp_unspam_topic()
    1199  * @uses bbp_unspam_reply()
    1200  * @uses restore_current_blog()
    1201  *
    1202  * @return If no user ID passed
    1203  */
    1204 function bbp_make_ham_user( $user_id = 0 ) {
    1205 
    1206         // Use displayed user if it's not yourself
    1207         if ( empty( $user_id ) && bbp_is_single_user() && !bbp_is_user_home() )
    1208                 $user_id = bbp_get_displayed_user_field();
    1209 
    1210         // Bail if no user ID
    1211         if ( empty( $user_id ) )
    1212                 return;
    1213 
    1214         // Bail if user ID is super admin
    1215         if ( is_super_admin( $user_id ) )
    1216                 return;
    1217 
    1218         // Arm the torpedos
    1219         global $wpdb;
    1220 
    1221         // Get the blog IDs of the user to mark as spam
    1222         $blogs = get_blogs_of_user( $user_id, true );
    1223 
    1224         // If user has no blogs, they are a guest on this site
    1225         if ( empty( $blogs ) )
    1226                 $blogs[$wpdb->blogid] = array();
    1227 
    1228         // Make array of post types to mark as spam
    1229         $post_types = array( bbp_get_topic_post_type(), bbp_get_reply_post_type() );
    1230         $post_types = "'" . implode( "', '", $post_types ) . "'";
    1231         $status     = bbp_get_spam_status_id();
    1232 
    1233         // Loop through blogs and remove their posts
    1234         foreach ( (array) array_keys( $blogs ) as $blog_id ) {
    1235 
    1236                 // Switch to the blog ID
    1237                 switch_to_blog( $blog_id );
    1238 
    1239                 // Get topics and replies
    1240                 $posts = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} WHERE post_author = {$user_id} AND post_status = '{$status}' AND post_type IN ({$post_types})" );
    1241 
    1242                 // Loop through posts and spam them
    1243                 if ( !empty( $posts ) ) {
    1244                         foreach ( $posts as $post_id ) {
    1245 
    1246                                 // The routines for topics ang replies are different, so use the
    1247                                 // correct one based on the post type
    1248                                 switch ( get_post_type( $post_id ) ) {
    1249 
    1250                                         case bbp_get_topic_post_type() :
    1251                                                 bbp_unspam_topic( $post_id );
    1252                                                 break;
    1253 
    1254                                         case bbp_get_reply_post_type() :
    1255                                                 bbp_unspam_reply( $post_id );
    1256                                                 break;
    1257                                 }
    1258                         }
    1259                 }
    1260 
    1261                 // Switch back to current blog
    1262                 restore_current_blog();
    1263         }
    1264 }
    1265 
    1266 /**
    1267  * Checks if the user has been marked as deleted.
    1268  *
    1269  * @since bbPress (r3355)
    1270  *
    1271  * @param int $user_id int The ID for the user.
    1272  * @return bool True if deleted, False if not.
    1273  */
    1274 function bbp_is_user_deleted( $user_id = 0 ) {
    1275 
    1276         // Default to current user
    1277         if ( empty( $user_id ) && is_user_logged_in() )
    1278                 $user_id = bbp_get_current_user_id();
    1279 
    1280         // No user to check
    1281         if ( empty( $user_id ) )
    1282                 return false;
    1283 
    1284         // Assume user is not deleted
    1285         $is_deleted = false;
    1286 
    1287         // Get user data
    1288         $user = get_userdata( $user_id );
    1289 
    1290         // No user found
    1291         if ( empty( $user ) ) {
    1292                 $is_deleted = true;
    1293 
    1294         // User found
    1295         } else {
    1296 
    1297                 // Check if deleted
    1298                 if ( !empty( $user->deleted ) )
    1299                         $is_deleted = true;
    1300 
    1301                 if ( 2 == $user->user_status )
    1302                         $is_deleted = true;
    1303 
    1304         }
    1305 
    1306         return apply_filters( 'bp_core_is_user_deleted', (bool) $is_deleted );
    1307 }
    1308 
    1309 /**
    1310  * Checks if user is active
    1311  *
    1312  * @since bbPress (r3502)
    1313  *
    1314  * @uses is_user_logged_in() To check if user is logged in
    1315  * @uses bbp_get_displayed_user_id() To get current user ID
    1316  * @uses bbp_is_user_spammer() To check if user is spammer
    1317  * @uses bbp_is_user_deleted() To check if user is deleted
    1318  *
    1319  * @param int $user_id The user ID to check
    1320  * @return bool True if public, false if not
    1321  */
    1322 function bbp_is_user_active( $user_id = 0 ) {
    1323 
    1324         // Default to current user
    1325         if ( empty( $user_id ) && is_user_logged_in() )
    1326                 $user_id = bbp_get_current_user_id();
    1327 
    1328         // No user to check
    1329         if ( empty( $user_id ) )
    1330                 return false;
    1331 
    1332         // Check spam
    1333         if ( bbp_is_user_spammer( $user_id ) )
    1334                 return false;
    1335 
    1336         // Check deleted
    1337         if ( bbp_is_user_deleted( $user_id ) )
    1338                 return false;
    1339 
    1340         // Assume true if not spam or deleted
    1341         return true;
    1342 }
    1343 
    1344 /**
    1345  * Checks if user is not active.
    1346  *
    1347  * @since bbPress (r3502)
    1348  *
    1349  * @uses is_user_logged_in() To check if user is logged in
    1350  * @uses bbp_get_displayed_user_id() To get current user ID
    1351  * @uses bbp_is_user_active() To check if user is active
    1352  *
    1353  * @param int $user_id The user ID to check. Defaults to current user ID
    1354  * @return bool True if inactive, false if active
    1355  */
    1356 function bbp_is_user_inactive( $user_id = 0 ) {
    1357 
    1358         // Default to current user
    1359         if ( empty( $user_id ) && is_user_logged_in() )
    1360                 $user_id = bbp_get_current_user_id();
    1361 
    1362         // No user to check
    1363         if ( empty( $user_id ) )
    1364                 return false;
    1365 
    1366         // Return the inverse of active
    1367         return !bbp_is_user_active( $user_id );
    1368 }
    1369 
    1370 /**
    1371  * Return a user's main role
    1372  *
    1373  * @since bbPress (r3860)
    1374  *
    1375  * @param int $user_id
    1376  * @uses bbp_get_user_id() To get the user id
    1377  * @uses get_userdata() To get the user data
    1378  * @uses apply_filters() Calls 'bbp_set_user_role' with the role and user id
    1379  * @return string
    1380  */
    1381 function bbp_set_user_role( $user_id = 0, $new_role = '' ) {
    1382 
    1383         // Validate user id
    1384         $user_id = bbp_get_user_id( $user_id, false, false );
    1385         $user    = get_userdata( $user_id );
    1386 
    1387         // User exists
    1388         if ( !empty( $user ) ) {
    1389 
    1390                 // Get users forum role
    1391                 $role = bbp_get_user_role( $user_id );
    1392 
    1393                 // User already has this role so no new role is set
    1394                 if ( $new_role == $role ) {
    1395                         $new_role = false;
    1396 
    1397                 // Users role is different than the new role
    1398                 } else {
    1399 
    1400                         // Remove the old role
    1401                         if ( ! empty( $role ) ) {
    1402                                 $user->remove_role( $role );
    1403                         }
    1404 
    1405                         // Add the new role
    1406                         $user->add_role( $new_role );
    1407                 }
    1408 
    1409         // User does don exist so return false
    1410         } else {
    1411                 $new_role = false;
    1412         }
    1413 
    1414         return apply_filters( 'bbp_set_user_role', $new_role, $user_id, $user );
    1415 }
    1416 
    1417 /**
    1418  * Return a user's main role
    1419  *
    1420  * @since bbPress (r3860)
    1421  *
    1422  * @param int $user_id
    1423  * @uses bbp_get_user_id() To get the user id
    1424  * @uses get_userdata() To get the user data
    1425  * @uses apply_filters() Calls 'bbp_get_user_role' with the role and user id
    1426  * @return string
    1427  */
    1428 function bbp_get_user_role( $user_id = 0 ) {
    1429 
    1430         // Validate user id
    1431         $user_id = bbp_get_user_id( $user_id, false, false );
    1432         $user    = get_userdata( $user_id );
    1433         $role    = false;
    1434 
    1435         // User has roles so lets
    1436         if ( ! empty( $user->roles ) ) {
    1437                 $roles = array_intersect( array_values( $user->roles ), array_keys( bbp_get_editable_roles() ) );
    1438 
    1439                 // If there's a role in the array, use the first one
    1440                 if ( !empty( $roles ) ) {
    1441                         $role = array_shift( array_values( $roles ) );
    1442                 }
    1443         }
    1444 
    1445         return apply_filters( 'bbp_get_user_role', $role, $user_id, $user );
    1446 }
    1447 
    14481051/** Premissions ***************************************************************/
    14491052
Note: See TracChangeset for help on using the changeset viewer.