Skip to:
Content

bbPress.org

Changeset 4290


Ignore:
Timestamp:
11/02/2012 06:34:53 AM (14 years ago)
Author:
johnjamesjacoby
Message:

Capabilities:

  • Add support for forum specific roles in core/capabilities.php.
  • See #1939.
File:
1 edited

Legend:

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

    r4249 r4290  
    1414// Exit if accessed directly
    1515if ( !defined( 'ABSPATH' ) ) exit;
     16
     17/**
     18 * Whether or not to show advanced capability editing when editing a user.
     19 *
     20 * @param type $default
     21 * @return type
     22 */
     23function bbp_use_advanced_capability_editor( $default = false ) {
     24        return apply_filters( 'bbp_use_advanced_capability_editor', $default );
     25}
    1626
    1727/** Output ********************************************************************/
     
    136146                switch( $capability ) {
    137147
    138                         // Misc
     148                        // Primary
     149                        case 'spectator' :
     150                                $retval = __( 'Spectate forum discussion', 'bbpress' );
     151                                break;
    139152                        case 'participate' :
    140153                                $retval = __( 'Participate in forums', 'bbpress' );
     
    292305        switch ( $role ) {
    293306
    294                 // Administrator
    295                 case 'administrator' :
     307                // Keymaster
     308                case bbp_get_keymaster_role() :
    296309                        $caps = array(
    297310
    298311                                // Primary caps
    299                                 'participate',
    300                                 'moderate',
    301                                 'throttle',
    302                                 'view_trash',
     312                                'spectate'               => true,
     313                                'participate'            => true,
     314                                'moderate'               => true,
     315                                'throttle'               => true,
     316                                'view_trash'             => true,
    303317
    304318                                // Forum caps
    305                                 'publish_forums',
    306                                 'edit_forums',
    307                                 'edit_others_forums',
    308                                 'delete_forums',
    309                                 'delete_others_forums',
    310                                 'read_private_forums',
    311                                 'read_hidden_forums',
     319                                'publish_forums'         => true,
     320                                'edit_forums'            => true,
     321                                'edit_others_forums'     => true,
     322                                'delete_forums'          => true,
     323                                'delete_others_forums'   => true,
     324                                'read_private_forums'    => true,
     325                                'read_hidden_forums'     => true,
    312326
    313327                                // Topic caps
    314                                 'publish_topics',
    315                                 'edit_topics',
    316                                 'edit_others_topics',
    317                                 'delete_topics',
    318                                 'delete_others_topics',
    319                                 'read_private_topics',
     328                                'publish_topics'         => true,
     329                                'edit_topics'            => true,
     330                                'edit_others_topics'     => true,
     331                                'delete_topics'          => true,
     332                                'delete_others_topics'   => true,
     333                                'read_private_topics'    => true,
    320334
    321335                                // Reply caps
    322                                 'publish_replies',
    323                                 'edit_replies',
    324                                 'edit_others_replies',
    325                                 'delete_replies',
    326                                 'delete_others_replies',
    327                                 'read_private_replies',
     336                                'publish_replies'        => true,
     337                                'edit_replies'           => true,
     338                                'edit_others_replies'    => true,
     339                                'delete_replies'         => true,
     340                                'delete_others_replies'  => true,
     341                                'read_private_replies'   => true,
    328342
    329343                                // Topic tag caps
    330                                 'manage_topic_tags',
    331                                 'edit_topic_tags',
    332                                 'delete_topic_tags',
    333                                 'assign_topic_tags'
     344                                'manage_topic_tags'      => true,
     345                                'edit_topic_tags'        => true,
     346                                'delete_topic_tags'      => true,
     347                                'assign_topic_tags'      => true
    334348                        );
    335349
    336350                        break;
    337351
    338                 // Any other role
     352                // Moderator
     353                case bbp_get_moderator_role() :
     354                        $caps = array(
     355
     356                                // Primary caps
     357                                'spectate'              => true,
     358                                'participate'           => true,
     359                                'moderate'              => true,
     360                                'throttle'              => true,
     361                                'view_trash'            => false,
     362
     363                                // Forum caps
     364                                'publish_forums'        => true,
     365                                'edit_forums'           => true,
     366                                'edit_others_forums'    => false,
     367                                'delete_forums'         => false,
     368                                'delete_others_forums'  => false,
     369                                'read_private_forums'   => true,
     370                                'read_hidden_forums'    => false,
     371
     372                                // Topic caps
     373                                'publish_topics'        => true,
     374                                'edit_topics'           => true,
     375                                'edit_others_topics'    => true,
     376                                'delete_topics'         => true,
     377                                'delete_others_topics'  => true,
     378                                'read_private_topics'   => true,
     379
     380                                // Reply caps
     381                                'publish_replies'       => true,
     382                                'edit_replies'          => true,
     383                                'edit_others_replies'   => true,
     384                                'delete_replies'        => true,
     385                                'delete_others_replies' => true,
     386                                'read_private_replies'  => true,
     387
     388                                // Topic tag caps
     389                                'manage_topic_tags'     => true,
     390                                'edit_topic_tags'       => true,
     391                                'delete_topic_tags'     => true,
     392                                'assign_topic_tags'     => true,                               
     393                        );
     394
     395                        break;
     396
     397                // Spectators can only read
     398                case bbp_get_spectator_role()   :
     399                        $caps = array(
     400
     401                                // Primary caps
     402                                'spectate'              => true,
     403                                'participate'           => false,
     404                                'moderate'              => false,
     405                                'throttle'              => false,
     406                                'view_trash'            => false,
     407
     408                                // Forum caps
     409                                'publish_forums'        => false,
     410                                'edit_forums'           => false,
     411                                'edit_others_forums'    => false,
     412                                'delete_forums'         => false,
     413                                'delete_others_forums'  => false,
     414                                'read_private_forums'   => false,
     415                                'read_hidden_forums'    => false,
     416
     417                                // Topic caps
     418                                'publish_topics'        => false,
     419                                'edit_topics'           => false,
     420                                'edit_others_topics'    => false,
     421                                'delete_topics'         => false,
     422                                'delete_others_topics'  => false,
     423                                'read_private_topics'   => false,
     424
     425                                // Reply caps
     426                                'publish_replies'       => false,
     427                                'edit_replies'          => false,
     428                                'edit_others_replies'   => false,
     429                                'delete_replies'        => false,
     430                                'delete_others_replies' => false,
     431                                'read_private_replies'  => false,
     432
     433                                // Topic tag caps
     434                                'manage_topic_tags'     => false,
     435                                'edit_topic_tags'       => false,
     436                                'delete_topic_tags'     => false,
     437                                'assign_topic_tags'     => false,                               
     438                        );
     439
     440                        break;
     441
     442                // Explicitly blocked
     443                case bbp_get_blocked_role() :
     444                        $caps = array(
     445
     446                                // Primary caps
     447                                'spectate'              => false,
     448                                'participate'           => false,
     449                                'moderate'              => false,
     450                                'throttle'              => false,
     451                                'view_trash'            => false,
     452
     453                                // Forum caps
     454                                'publish_forums'        => false,
     455                                'edit_forums'           => false,
     456                                'edit_others_forums'    => false,
     457                                'delete_forums'         => false,
     458                                'delete_others_forums'  => false,
     459                                'read_private_forums'   => false,
     460                                'read_hidden_forums'    => false,
     461
     462                                // Topic caps
     463                                'publish_topics'        => false,
     464                                'edit_topics'           => false,
     465                                'edit_others_topics'    => false,
     466                                'delete_topics'         => false,
     467                                'delete_others_topics'  => false,
     468                                'read_private_topics'   => false,
     469
     470                                // Reply caps
     471                                'publish_replies'       => false,
     472                                'edit_replies'          => false,
     473                                'edit_others_replies'   => false,
     474                                'delete_replies'        => false,
     475                                'delete_others_replies' => false,
     476                                'read_private_replies'  => false,
     477
     478                                // Topic tag caps
     479                                'manage_topic_tags'     => false,
     480                                'edit_topic_tags'       => false,
     481                                'delete_topic_tags'     => false,
     482                                'assign_topic_tags'     => false,                               
     483                        );
     484
     485                        break;
     486
     487                // Participant/Default
     488                case bbp_get_anonymous_role()   :
     489                case bbp_get_participant_role() :
    339490                default :
    340491                        $caps = array(
    341492
    342493                                // Primary caps
    343                                 'participate',
     494                                'spectate'              => true,
     495                                'participate'           => true,
     496                                'moderate'              => false,
     497                                'throttle'              => false,
     498                                'view_trash'            => false,
    344499
    345500                                // Forum caps
    346                                 'read_private_forums',
     501                                'publish_forums'        => false,
     502                                'edit_forums'           => false,
     503                                'edit_others_forums'    => false,
     504                                'delete_forums'         => false,
     505                                'delete_others_forums'  => false,
     506                                'read_private_forums'   => true,
     507                                'read_hidden_forums'    => false,
    347508
    348509                                // Topic caps
    349                                 'publish_topics',
    350                                 'edit_topics',
     510                                'publish_topics'        => true,
     511                                'edit_topics'           => true,
     512                                'edit_others_topics'    => false,
     513                                'delete_topics'         => false,
     514                                'delete_others_topics'  => false,
     515                                'read_private_topics'   => false,
    351516
    352517                                // Reply caps
    353                                 'publish_replies',
    354                                 'edit_replies',
     518                                'publish_replies'       => true,
     519                                'edit_replies'          => true,
     520                                'edit_others_replies'   => false,
     521                                'delete_replies'        => false,
     522                                'delete_others_replies' => false,
     523                                'read_private_replies'  => false,
    355524
    356525                                // Topic tag caps
    357                                 'assign_topic_tags'
     526                                'manage_topic_tags'     => false,
     527                                'edit_topic_tags'       => false,
     528                                'delete_topic_tags'     => false,
     529                                'assign_topic_tags'     => true,                               
    358530                        );
    359531
     
    378550        // Loop through available roles and add caps
    379551        foreach( $wp_roles->role_objects as $role ) {
    380                 foreach ( bbp_get_caps_for_role( $role->name ) as $cap ) {
    381                         $role->add_cap( $cap );
     552                foreach ( bbp_get_caps_for_role( $role->name ) as $cap => $value ) {
     553                        $role->add_cap( $cap, $value );
    382554                }
    383555        }
     
    400572        // Loop through available roles and remove caps
    401573        foreach( $wp_roles->role_objects as $role ) {
    402                 foreach ( bbp_get_caps_for_role( $role->name ) as $cap ) {
     574                foreach ( array_keys( bbp_get_caps_for_role( $role->name ) ) as $cap ) {
    403575                        $role->remove_cap( $cap );
    404576                }
     
    408580}
    409581
     582/** Forum Roles ***************************************************************/
     583
     584/**
     585 * Add the bbPress roles to the $wp_roles global.
     586 *
     587 * We do this to avoid adding these values to the database.
     588 *
     589 * @since bbPress (rxxxx)
     590 * @global type $wp_roles
     591 */
     592function bbp_add_forums_roles() {
     593        global $wp_roles, $wp_user_roles;
     594
     595        if ( ! isset( $wp_roles ) )
     596                $wp_roles = new WP_Roles();
     597
     598        // Loop through bbPress's roles and add them to the global $wp_roles
     599        foreach( bbp_get_forums_editable_roles() as $role_id => $details ) {
     600                $wp_roles->roles[$role_id]        = $details;
     601                $wp_roles->role_objects[$role_id] = new WP_Role( $details['name'], $details['capabilities'] );
     602                $wp_roles->role_names[$role_id]   = $details['name'];
     603        }
     604
     605        // Force WordPress not to use the DB
     606        $wp_user_roles = $wp_roles;
     607}
     608
     609/**
     610 * Fetch a filtered list of forum roles that the current user is
     611 * allowed to have.
     612 *
     613 * Simple function who's main purpose is to allow filtering of the
     614 * list of forum roles so that plugins can remove inappropriate ones depending
     615 * on the situation or user making edits.
     616 *
     617 * Specifically because without filtering, anyone with the edit_users
     618 * capability can edit others to be administrators, even if they are
     619 * only editors or authors. This filter allows admins to delegate
     620 * user management.
     621 *
     622 * @since bbPress (r4284)
     623 *
     624 * @return array
     625 */
     626function bbp_get_forums_editable_roles() {
     627        return (array) apply_filters( 'bbp_get_forums_editable_roles', array(
     628
     629                // Keymaster
     630                bbp_get_keymaster_role() => array(
     631                        'name'         => __( 'Keymaster', 'bbpress' ),
     632                        'capabilities' => bbp_get_caps_for_role( bbp_get_keymaster_role() )
     633                ),
     634
     635                // Moderator
     636                bbp_get_moderator_role() => array(
     637                        'name'         => __( 'Moderator', 'bbpress' ),
     638                        'capabilities' => bbp_get_caps_for_role( bbp_get_moderator_role() )
     639                ),
     640
     641                // Participant
     642                bbp_get_participant_role() => array(
     643                        'name'         => __( 'Participant', 'bbpress' ),
     644                        'capabilities' => bbp_get_caps_for_role( bbp_get_participant_role() )
     645                ),
     646
     647                // Spectator
     648                bbp_get_spectator_role() => array(
     649                        'name'         => __( 'Spectator', 'bbpress' ),
     650                        'capabilities' => bbp_get_caps_for_role( bbp_get_spectator_role() )
     651                ),
     652
     653                // Blocked
     654                bbp_get_blocked_role() => array(
     655                        'name'         => __( 'Blocked', 'bbpress' ),
     656                        'capabilities' => bbp_get_caps_for_role( bbp_get_blocked_role() )
     657                )
     658        ) );
     659}
     660
     661/**
     662 * The keymaster role for bbPress users
     663 *
     664 * @since bbPress (r4284)
     665 *
     666 * @uses apply_filters() Allow override of hardcoded keymaster role
     667 * @return string
     668 */
     669function bbp_get_keymaster_role() {
     670        return apply_filters( 'bbp_get_keymaster_role', 'bbp_keymaster' );
     671}
     672
     673/**
     674 * The moderator role for bbPress users
     675 *
     676 * @since bbPress (r3410)
     677 *
     678 * @uses apply_filters() Allow override of hardcoded moderator role
     679 * @return string
     680 */
     681function bbp_get_moderator_role() {
     682        return apply_filters( 'bbp_get_moderator_role', 'bbp_moderator' );
     683}
     684
     685/**
     686 * The participant role for registered user that can participate in forums
     687 *
     688 * @since bbPress (r3410)
     689 *
     690 * @uses apply_filters() Allow override of hardcoded participant role
     691 * @return string
     692 */
     693function bbp_get_participant_role() {
     694        return apply_filters( 'bbp_get_participant_role', 'bbp_participant' );
     695}
     696
     697/**
     698 * The spectator role is for registered users without any capabilities
     699 *
     700 * @since bbPress (r3860)
     701 *
     702 * @uses apply_filters() Allow override of hardcoded spectator role
     703 * @return string
     704 */
     705function bbp_get_spectator_role() {
     706        return apply_filters( 'bbp_get_spectator_role', 'bbp_spectator' );
     707}
     708
     709/**
     710 * The anonymous role for any user without a forum role
     711 *
     712 * @since bbPress (r3860)
     713 *
     714 * @uses apply_filters() Allow override of hardcoded anonymous role
     715 * @return string
     716 */
     717function bbp_get_anonymous_role() {
     718        return apply_filters( 'bbp_get_anonymous_role', 'bbp_anonymous' );
     719}
     720
     721/**
     722 * The blocked role is for registered users that cannot spectate or participate
     723 *
     724 * @since bbPress (r4284)
     725 *
     726 * @uses apply_filters() Allow override of hardcoded blocked role
     727 * @return string
     728 */
     729function bbp_get_blocked_role() {
     730        return apply_filters( 'bbp_get_blocked_role', 'bbp_blocked' );
     731}
     732
    410733/** Deprecated ****************************************************************/
    411734
     
    429752        _doing_it_wrong( 'bbp_remove_roles', __( 'Special forum roles no longer exist. Use mapped capabilities instead', 'bbpress' ), '2.2' );
    430753}
    431 
    432 /**
    433  * The anonymous role for unregistered users
    434  *
    435  * @since bbPress (r3860)
    436  *
    437  * @deprecated since version 2.2
    438  */
    439 function bbp_get_anonymous_role() {
    440         _doing_it_wrong( 'bbp_get_anonymous_role', __( 'Special forum roles no longer exist. Use mapped capabilities instead', 'bbpress' ), '2.2' );
    441 }
    442 
    443 /**
    444  * The participant role for registered users without roles
    445  *
    446  * @since bbPress (r3410)
    447  *
    448  * @deprecated since version 2.2
    449  */
    450 function bbp_get_participant_role() {
    451         _doing_it_wrong( 'bbp_get_participant_role', __( 'Special forum roles no longer exist. Use mapped capabilities instead', 'bbpress' ), '2.2' );
    452 }
    453 
    454 /**
    455  * The moderator role for bbPress users
    456  *
    457  * @since bbPress (r3410)
    458  *
    459  * @deprecated since version 2.2
    460  */
    461 function bbp_get_moderator_role() {
    462         _doing_it_wrong( 'bbp_get_moderator_role', __( 'Special forum roles no longer exist. Use mapped capabilities instead', 'bbpress' ), '2.2' );
    463 }
Note: See TracChangeset for help on using the changeset viewer.