Changeset 6302
- Timestamp:
- 09/04/2012 01:45:36 PM (14 years ago)
- File:
-
- 1 edited
-
trunk/bp-core/bp-core-caps.php (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/bp-core/bp-core-caps.php
r6285 r6302 10 10 // Exit if accessed directly 11 11 if ( !defined( 'ABSPATH' ) ) exit; 12 13 /**14 * Adds BuddyPress-specific user roles.15 *16 * This is called on plugin activation.17 *18 * @since BuddyPress (1.6)19 *20 * @uses get_option() To get the default role21 * @uses get_role() To get the default role object22 * @uses add_role() To add our own roles23 * @uses do_action() Calls 'bp_add_roles'24 */25 function bp_add_roles() {26 27 // Get new role names28 $moderator_role = bp_get_moderator_role();29 $participant_role = bp_get_participant_role();30 31 // Add the Moderator role and add the default role caps.32 // Mod caps are added by the bp_add_caps() function33 $default = get_role( get_option( 'default_role' ) );34 35 // If role does not exist, default to read cap36 if ( empty( $default->capabilities ) )37 $default->capabilities = array( 'read' );38 39 // Moderators are default role + community moderating caps in bp_add_caps()40 add_role( $moderator_role, 'Community Moderator', $default->capabilities );41 42 // Forum Subscribers are auto added to sites with global communities43 add_role( $participant_role, 'Community Participant', $default->capabilities );44 45 do_action( 'bp_add_roles' );46 }47 12 48 13 /** … … 64 29 $wp_roles = new WP_Roles(); 65 30 66 // Loop through available roles 67 foreach( $wp_roles->roles as $role => $details ) { 68 69 // Load this role 70 $this_role = get_role( $role ); 71 72 // Loop through caps for this role and remove them 73 foreach ( bp_get_caps_for_role( $role ) as $cap ) { 74 $this_role->add_cap( $cap ); 31 // Loop through available roles and add them 32 foreach( $wp_roles->role_objects as $role ) { 33 foreach ( bp_get_caps_for_role( $role->name ) as $cap ) { 34 $role->add_cap( $cap ); 75 35 } 76 36 } … … 97 57 $wp_roles = new WP_Roles(); 98 58 99 // Loop through available roles 100 foreach( $wp_roles->roles as $role => $details ) { 101 102 // Load this role 103 $this_role = get_role( $role ); 104 105 // Loop through caps for this role and remove them 106 foreach ( bp_get_caps_for_role( $role ) as $cap ) { 107 $this_role->remove_cap( $cap ); 59 // Loop through available roles and remove them 60 foreach( $wp_roles->role_objects as $role ) { 61 foreach ( bp_get_caps_for_role( $role->name ) as $cap ) { 62 $role->remove_cap( $cap ); 108 63 } 109 64 } 110 65 111 66 do_action( 'bp_remove_caps' ); 112 }113 114 /**115 * Removes BuddyPress-specific user roles.116 *117 * This is called on plugin deactivation.118 *119 * @since BuddyPress (1.6)120 *121 * @uses remove_role() To remove our roles122 * @uses do_action() Calls 'bp_remove_roles'123 */124 function bp_remove_roles() {125 126 // Get new role names127 $moderator_role = bp_get_moderator_role();128 $participant_role = bp_get_participant_role();129 130 // Remove the Moderator role131 remove_role( $moderator_role );132 133 // Remove the Moderator role134 remove_role( $participant_role );135 136 do_action( 'bp_remove_roles' );137 67 } 138 68 … … 184 114 function bp_get_caps_for_role( $role = '' ) { 185 115 186 // Get new role names187 $moderator_role = bp_get_moderator_role();188 $participant_role = bp_get_participant_role();189 190 116 // Which role are we looking for? 191 117 switch ( $role ) { … … 200 126 break; 201 127 202 // Moderator203 case $moderator_role :204 $caps = array(205 // Misc206 'bp_moderate',207 );208 209 break;210 211 // WordPress Core Roles212 128 case 'editor' : 213 129 case 'author' : 214 130 case 'contributor' : 215 131 case 'subscriber' : 216 217 // BuddyPress Participant Role218 case $participant_role :219 132 default : 220 133 $caps = array(); … … 242 155 * @return If user is not spam/deleted or is already capable 243 156 */ 244 function bp_global_access_auto_role() { 245 246 // Bail if not multisite or community is not global 247 if ( !is_multisite() || !bp_allow_global_access() ) 157 function bbp_set_current_user_default_role() { 158 159 // Bail if not multisite or not root blog 160 if ( ! is_multisite() || ! bp_is_root_blog() ) 161 return; 162 163 // Bail if user is not logged in or already a member 164 if ( ! is_user_logged_in() || is_user_member_of_blog() ) 248 165 return; 249 166 … … 252 169 return; 253 170 254 // Bail if user is not logged in 255 if ( !is_user_logged_in() ) 256 return; 257 258 // Give the user the 'Forum Participant' role 259 if ( current_user_can( 'bp_masked' ) ) { 260 global $bp; 261 262 // Get the default role 263 $default_role = bp_get_participant_role(); 264 265 // Set the current users default role 266 $bp->current_user->set_role( $default_role ); 267 } 268 } 269 270 /** 271 * The participant role for registered users without roles 272 * 273 * This is primarily for multisite compatibility when users without roles on 274 * sites that have global communities enabled 275 * 276 * @since BuddyPress (1.6) 277 * 278 * @param string $role 279 * @uses apply_filters() 280 * @return string 281 */ 282 function bp_get_participant_role() { 283 284 // Hardcoded participant role 285 $role = 'bp_participant'; 286 287 // Allow override 288 return apply_filters( 'bp_get_participant_role', $role ); 289 } 290 291 /** 292 * The moderator role for BuddyPress users 293 * 294 * @since BuddyPress (1.6) 295 * 296 * @param string $role 297 * @uses apply_filters() 298 * @return string 299 */ 300 function bp_get_moderator_role() { 301 302 // Hardcoded moderated user role 303 $role = 'bp_moderator'; 304 305 // Allow override 306 return apply_filters( 'bp_get_moderator_role', $role ); 307 } 308 309 /** 310 * Add the default role and mapped BuddyPress caps to the current user if needed 311 * 312 * This function will bail if the community is not global in a multisite 313 * installation of WordPress, or if the user is marked as spam or deleted. 314 * 315 * @since BuddyPress (1.6) 316 * 317 * @uses is_multisite() 318 * @uses bp_allow_global_access() 319 * @uses bp_is_user_inactive() 320 * @uses is_user_logged_in() 321 * @uses current_user_can() 322 * @uses get_option() 323 * @uses bp_get_caps_for_role() 324 * 325 * @global BuddyPress $bp 326 * @return If not multisite, not global, or user is deleted/spammed 327 */ 328 function bp_global_access_role_mask() { 329 330 // Bail if not multisite or community is not global 331 if ( !is_multisite() || !bp_allow_global_access() ) 332 return; 333 334 // Bail if user is marked as spam or is deleted 335 if ( bp_is_user_inactive() ) 336 return; 337 338 // Normal user is logged in but has no caps 339 if ( is_user_logged_in() && !current_user_can( 'read' ) ) { 340 341 // Define local variable 342 $mapped_meta_caps = array(); 343 344 // Assign user the minimal participant role to map caps to 345 $default_role = bp_get_participant_role(); 346 347 // Get BuddyPress caps for the default role 348 $caps_for_role = bp_get_caps_for_role( $default_role ); 349 350 // Set all caps to true 351 foreach ( $caps_for_role as $cap ) { 352 $mapped_meta_caps[$cap] = true; 353 } 354 355 // Add 'read' cap just in case 356 $mapped_meta_caps['read'] = true; 357 $mapped_meta_caps['bp_masked'] = true; 358 359 // Allow global access caps to be manipulated 360 $mapped_meta_caps = apply_filters( 'bp_global_access_mapped_meta_caps', $mapped_meta_caps ); 361 362 // Assign the role and mapped caps to the current user 363 global $bp; 364 $bp->current_user->roles[0] = $default_role; 365 $bp->current_user->caps = $mapped_meta_caps; 366 $bp->current_user->allcaps = $mapped_meta_caps; 367 } 171 // Set the current users default role 172 buddypress()->current_user->set_role( bp_get_option( 'default_role', 'subscriber' ) ); 368 173 } 369 174 … … 424 229 add_filter( 'user_has_cap', '_bp_enforce_bp_moderate_cap_for_admins', 10, 3 ); 425 230 426 ?> 231 /** Deprecated ****************************************************************/ 232 233 /** 234 * Adds BuddyPress-specific user roles. 235 * 236 * This is called on plugin activation. 237 * 238 * @since BuddyPress (1.6) 239 * 240 * @deprecated since version 1.7 241 */ 242 function bp_add_roles() { 243 _doing_it_wrong( 'bp_add_roles', __( 'Special community roles no longer exist. Use mapped capabilities instead', 'buddypress' ), '1.7' ); 244 } 245 246 /** 247 * Removes BuddyPress-specific user roles. 248 * 249 * This is called on plugin deactivation. 250 * 251 * @since BuddyPress (1.6) 252 * 253 * @deprecated since version 1.7 254 */ 255 function bp_remove_roles() { 256 _doing_it_wrong( 'bp_remove_roles', __( 'Special community roles no longer exist. Use mapped capabilities instead', 'buddypress' ), '1.7' ); 257 } 258 259 260 /** 261 * The participant role for registered users without roles 262 * 263 * This is primarily for multisite compatibility when users without roles on 264 * sites that have global communities enabled 265 * 266 * @since BuddyPress (1.6) 267 * 268 * @deprecated since version 1.7 269 */ 270 function bp_get_participant_role() { 271 _doing_it_wrong( 'bp_get_participant_role', __( 'Special community roles no longer exist. Use mapped capabilities instead', 'buddypress' ), '1.7' ); 272 } 273 274 /** 275 * The moderator role for BuddyPress users 276 * 277 * @since BuddyPress (1.6) 278 * 279 * @deprecated since version 1.7 280 */ 281 function bp_get_moderator_role() { 282 _doing_it_wrong( 'bp_get_moderator_role', __( 'Special community roles no longer exist. Use mapped capabilities instead', 'buddypress' ), '1.7' ); 283 }
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)