Skip to:
Content

bbPress.org


Ignore:
Timestamp:
09/04/2012 08:31:57 AM (14 years ago)
Author:
johnjamesjacoby
Message:

Capabilities:

  • Bring back bbp_add_caps() and bbp_remove_caps().
  • Remove role mask, and more aggressively add registered user to site with default role when user visits the site for the first time.
  • See #1942.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/plugin/bbp-includes/bbp-core-caps.php

    r4180 r4184  
    1010// Exit if accessed directly
    1111if ( !defined( 'ABSPATH' ) ) exit;
     12
     13/**
     14 * Adds capabilities to WordPress user roles.
     15 *
     16 * @since bbPress (r2608)
     17 */
     18function bbp_add_caps() {
     19        global $wp_roles;
     20
     21        // Load roles if not set
     22        if ( ! isset( $wp_roles ) )
     23                $wp_roles = new WP_Roles();
     24
     25        // Loop through available roles and add caps
     26        foreach( $wp_roles->role_objects as $role ) {
     27                foreach ( bbp_get_caps_for_role( $role->name ) as $cap ) {
     28                        $role->add_cap( $cap );
     29                }
     30        }
     31
     32        do_action( 'bbp_add_caps' );
     33}
     34
     35/**
     36 * Removes capabilities from WordPress user roles.
     37 *
     38 * @since bbPress (r2608)
     39 */
     40function bbp_remove_caps() {
     41        global $wp_roles;
     42
     43        // Load roles if not set
     44        if ( ! isset( $wp_roles ) )
     45                $wp_roles = new WP_Roles();
     46
     47        // Loop through available roles and remove caps
     48        foreach( $wp_roles->role_objects as $role ) {
     49                foreach ( bbp_get_caps_for_role( $role->name ) as $cap ) {
     50                        $role->remove_cap( $cap );
     51                }
     52        }
     53
     54        do_action( 'bbp_remove_caps' );
     55}
    1256
    1357/**
     
    707751
    708752/**
    709  * Give a user the default role when creating a topic/reply on a site they do
    710  * not have a role on.
    711  *
    712  * @since bbPress (r3410)
    713  *
    714  * @uses bbp_allow_global_access()
    715  * @uses bbp_is_user_inactive()
    716  * @uses is_user_logged_in()
    717  * @uses current_user_can()
    718  * @uses WP_User::set_role()
    719  *
    720  * @return If user is not spam/deleted or is already capable
    721  */
    722 function bbp_global_access_auto_role() {
    723 
    724         // Bail if forum is not global
    725         if ( ! bbp_allow_global_access() )
    726                 return;
    727 
    728         // Bail if not logged in or already a member of this site
    729         if ( ! is_user_logged_in() || is_user_member_of_blog() )
    730                 return;
    731 
    732         // Bail if user is not active
    733         if ( bbp_is_user_inactive() )
    734                 return;
    735 
    736         // Give the user the default role
    737         if ( ! current_user_can( 'bbp_masked' ) )
    738                 return;
    739 
    740         // Make sure the bbp_masked cap doesn't get saved to the DB
    741         bbpress()->current_user->remove_cap( 'bbp_masked' );
    742 
    743         // Set user to default role for blog
    744         bbpress()->current_user->set_role( get_option( 'default_role', 'subscriber' ) );
    745 }
    746 
    747 /**
    748753 * Add the default role and mapped bbPress caps to the current user if needed
    749754 *
     
    781786                return;
    782787
    783         // Set all caps to true
    784         foreach ( bbp_get_caps_for_role() as $cap )
    785                 $default_caps[$cap] = true;
    786 
    787         // Add 'read' cap just in case
    788         $default_caps['bbp_masked'] = true;
    789 
    790         // Allow global access caps to be manipulated
    791         $default_caps = apply_filters( 'bbp_global_access_default_caps', $default_caps );
    792 
    793788        // Assign the role and mapped caps to the current user
    794         $bbp = bbpress();
    795         $bbp->current_user->caps     = $default_caps;
    796         $bbp->current_user->allcaps  = $default_caps;
     789        bbpress()->current_user->set_role( get_option( 'default_role', 'subscriber' ) );
    797790}
    798791
     
    850843                case 'bbp_tools_import_page'     : // Tools - Import Page
    851844                case 'bbp_tools_reset_page'      : // Tools - Reset Page
    852                 case 'bbp_settings?page'         : // Settings Page
     845                case 'bbp_settings_page'         : // Settings Page
    853846                case 'bbp_settings_main'         : // Settings - General
    854847                case 'bbp_settings_theme_compat' : // Settings - Theme compat
     
    888881
    889882/**
    890  * Adds capabilities to WordPress user roles.
    891  *
    892  * @since bbPress (r2608)
    893  * @deprecated since version 2.2
    894  */
    895 function bbp_add_caps() {
    896         _doing_it_wrong( 'bbp_add_caps', __( 'Use mapped capabilities instead', 'bbpress' ), '2.2' );
    897 }
    898 
    899 /**
    900  * Removes capabilities from WordPress user roles.
    901  *
    902  * @since bbPress (r2608)
    903  * @deprecated since version 2.2
    904  */
    905 function bbp_remove_caps() {
    906         _doing_it_wrong( 'bbp_remove_caps', __( 'Special forum roles no longer exist. Use mapped capabilities instead', 'bbpress' ), '2.2' );
    907 }
    908 
    909 /**
    910883 * The anonymous role for unregistered users
    911884 *
Note: See TracChangeset for help on using the changeset viewer.