Skip to:
Content

bbPress.org


Ignore:
Timestamp:
11/06/2012 01:21:55 AM (13 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/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.