Skip to:
Content

bbPress.org


Ignore:
Timestamp:
03/20/2017 10:44:00 AM (8 years ago)
Author:
johnjamesjacoby
Message:

Moderation: Allow per-forum moderators to edit topics & replies inside of forums they have moderation control over.

This feature require the following changes:

  • Prefer read_forum capability check over read_private_forums or read_hidden_forums, and include a $forum_id parameter to assist map_meta_cap filters
  • Prefer edit_others_topics|replies over moderate where appropriate, to ensure capability mappings work as intended
  • Introduce bbp_get_public_topic_statuses() to replace several duplicate occurrences of the same array usage (also allow these to be filtered)
  • Introduce bbp_is_topic_public() (not to be confused with bbp_is_topic_published()) to provide parity with bbp_is_forum_public() and also utilize bbp_get_public_topic_statuses() from above
  • Add local caching to bbp_exclude_forum_ids() as a performance optimization to reduce the depth of current_user_can() calls when private & hidden forums are in use
  • Add user_can( 'moderate' ) capability checks to various mappings, to ensure forum moderators can read/edit/delete content inside of the individual forums they are moderators of
  • Use bbp_get_user_id() where appropriate, rather than casting as int
  • Various surrounding code clean-ups

See #2593.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/includes/forums/functions.php

    r6341 r6384  
    142142
    143143    // User cannot create forums
    144     if ( !current_user_can( 'publish_forums' ) ) {
     144    if ( ! current_user_can( 'publish_forums' ) ) {
    145145        bbp_add_error( 'bbp_forum_permission', __( '<strong>ERROR</strong>: You do not have permission to create new forums.', 'bbpress' ) );
    146146        return;
     
    208208
    209209        // Forum is closed and user cannot access
    210         if ( bbp_is_forum_closed( $forum_parent_id ) && !current_user_can( 'edit_forum', $forum_parent_id ) ) {
     210        if ( bbp_is_forum_closed( $forum_parent_id ) && ! current_user_can( 'edit_forum', $forum_parent_id ) ) {
    211211            bbp_add_error( 'bbp_new_forum_forum_closed', __( '<strong>ERROR</strong>: This forum has been closed to new forums.', 'bbpress' ) );
    212212        }
    213213
    214214        // Forum is private and user cannot access
    215         if ( bbp_is_forum_private( $forum_parent_id ) && !current_user_can( 'read_private_forums' ) ) {
     215        if ( bbp_is_forum_private( $forum_parent_id ) && ! current_user_can( 'read_forum', $forum_parent_id ) ) {
    216216            bbp_add_error( 'bbp_new_forum_forum_private', __( '<strong>ERROR</strong>: This forum is private and you do not have the capability to read or create new forums in it.', 'bbpress' ) );
    217217        }
    218218
    219219        // Forum is hidden and user cannot access
    220         if ( bbp_is_forum_hidden( $forum_parent_id ) && !current_user_can( 'read_hidden_forums' ) ) {
     220        if ( bbp_is_forum_hidden( $forum_parent_id ) && ! current_user_can( 'read_forum', $forum_parent_id ) ) {
    221221            bbp_add_error( 'bbp_new_forum_forum_hidden', __( '<strong>ERROR</strong>: This forum is hidden and you do not have the capability to read or create new forums in it.', 'bbpress' ) );
    222222        }
     
    422422
    423423    // User cannot edit this forum
    424     } elseif ( !current_user_can( 'edit_forum', $forum_id ) ) {
     424    } elseif ( ! current_user_can( 'edit_forum', $forum_id ) ) {
    425425        bbp_add_error( 'bbp_edit_forum_permission', __( '<strong>ERROR</strong>: You do not have permission to edit that forum.', 'bbpress' ) );
    426426        return;
     
    448448
    449449        // Forum is closed and user cannot access
    450         if ( bbp_is_forum_closed( $forum_parent_id ) && !current_user_can( 'edit_forum', $forum_parent_id ) ) {
     450        if ( bbp_is_forum_closed( $forum_parent_id ) && ! current_user_can( 'edit_forum', $forum_parent_id ) ) {
    451451            bbp_add_error( 'bbp_edit_forum_forum_closed', __( '<strong>ERROR</strong>: This forum has been closed to new forums.', 'bbpress' ) );
    452452        }
    453453
    454454        // Forum is private and user cannot access
    455         if ( bbp_is_forum_private( $forum_parent_id ) && !current_user_can( 'read_private_forums' ) ) {
     455        if ( bbp_is_forum_private( $forum_parent_id ) && ! current_user_can( 'read_forum', $forum_parent_id ) ) {
    456456            bbp_add_error( 'bbp_edit_forum_forum_private', __( '<strong>ERROR</strong>: This forum is private and you do not have the capability to read or create new forums in it.', 'bbpress' ) );
    457457        }
    458458
    459459        // Forum is hidden and user cannot access
    460         if ( bbp_is_forum_hidden( $forum_parent_id ) && !current_user_can( 'read_hidden_forums' ) ) {
     460        if ( bbp_is_forum_hidden( $forum_parent_id ) && ! current_user_can( 'read_forum', $forum_parent_id ) ) {
    461461            bbp_add_error( 'bbp_edit_forum_forum_hidden', __( '<strong>ERROR</strong>: This forum is hidden and you do not have the capability to read or create new forums in it.', 'bbpress' ) );
    462462        }
     
    20892089 */
    20902090function bbp_exclude_forum_ids( $type = 'string' ) {
     2091    static $types = array();
    20912092
    20922093    // Setup arrays
    20932094    $private = $hidden = $meta_query = $forum_ids = array();
    20942095
    2095     // Default return value
    2096     switch ( $type ) {
    2097         case 'string' :
    2098             $retval = '';
    2099             break;
    2100 
    2101         case 'array'  :
    2102             $retval = array();
    2103             break;
    2104 
    2105         case 'meta_query' :
    2106             $retval = array( array() ) ;
    2107             break;
    2108     }
    2109 
    2110     // Exclude for everyone but keymasters
    2111     if ( ! bbp_is_user_keymaster() ) {
    2112 
    2113         // Private forums
    2114         if ( !current_user_can( 'read_private_forums' ) ) {
    2115             $private = bbp_get_private_forum_ids();
    2116         }
    2117 
    2118         // Hidden forums
    2119         if ( !current_user_can( 'read_hidden_forums' ) ) {
    2120             $hidden  = bbp_get_hidden_forum_ids();
    2121         }
    2122 
    2123         // Merge private and hidden forums together
    2124         $forum_ids = (array) array_filter( wp_parse_id_list( array_merge( $private, $hidden ) ) );
    2125 
    2126         // There are forums that need to be excluded
    2127         if ( ! empty( $forum_ids ) ) {
    2128 
    2129             switch ( $type ) {
    2130 
    2131                 // Separate forum ID's into a comma separated string
    2132                 case 'string' :
    2133                     $retval = implode( ',', $forum_ids );
    2134                     break;
    2135 
    2136                 // Use forum_ids array
    2137                 case 'array'  :
    2138                     $retval = $forum_ids;
    2139                     break;
    2140 
    2141                 // Build a meta_query
    2142                 case 'meta_query' :
    2143                     $retval = array(
    2144                         'key'     => '_bbp_forum_id',
    2145                         'value'   => implode( ',', $forum_ids ),
    2146                         'type'    => 'NUMERIC',
    2147                         'compare' => ( 1 < count( $forum_ids ) ) ? 'NOT IN' : '!='
    2148                     );
    2149                     break;
     2096    // Capability performance optimization
     2097    if ( ! empty( $types[ $type ] ) ) {
     2098        $retval = $types[ $type ];
     2099
     2100    // Populate forum exclude type
     2101    } else {
     2102
     2103        // Default return value
     2104        switch ( $type ) {
     2105            case 'string' :
     2106                $retval = '';
     2107                break;
     2108
     2109            case 'array'  :
     2110                $retval = array();
     2111                break;
     2112
     2113            case 'meta_query' :
     2114                $retval = array( array() ) ;
     2115                break;
     2116        }
     2117
     2118        // Exclude for everyone but keymasters
     2119        if ( ! bbp_is_user_keymaster() ) {
     2120
     2121            // Private forums
     2122            if ( ! current_user_can( 'read_private_forums' ) ) {
     2123                $private = bbp_get_private_forum_ids();
    21502124            }
    2151         }
     2125
     2126            // Hidden forums
     2127            if ( ! current_user_can( 'read_hidden_forums' ) ) {
     2128                $hidden  = bbp_get_hidden_forum_ids();
     2129            }
     2130
     2131            // Merge private and hidden forums together
     2132            $forum_ids = (array) array_filter( wp_parse_id_list( array_merge( $private, $hidden ) ) );
     2133
     2134            // There are forums that need to be excluded
     2135            if ( ! empty( $forum_ids ) ) {
     2136
     2137                switch ( $type ) {
     2138
     2139                    // Separate forum ID's into a comma separated string
     2140                    case 'string' :
     2141                        $retval = implode( ',', $forum_ids );
     2142                        break;
     2143
     2144                    // Use forum_ids array
     2145                    case 'array'  :
     2146                        $retval = $forum_ids;
     2147                        break;
     2148
     2149                    // Build a meta_query
     2150                    case 'meta_query' :
     2151                        $retval = array(
     2152                            'key'     => '_bbp_forum_id',
     2153                            'value'   => implode( ',', $forum_ids ),
     2154                            'type'    => 'NUMERIC',
     2155                            'compare' => ( 1 < count( $forum_ids ) ) ? 'NOT IN' : '!='
     2156                        );
     2157                        break;
     2158                }
     2159            }
     2160        }
     2161
     2162        // Store return value in static types array
     2163        $types[ $type ] = $retval;
    21522164    }
    21532165
     
    23992411
    24002412    // If forum is explicitly hidden and user not capable, set 404
    2401     if ( ! empty( $forum_id ) && bbp_is_forum_hidden( $forum_id ) && !current_user_can( 'read_hidden_forums' ) ) {
     2413    if ( ! empty( $forum_id ) && bbp_is_forum_hidden( $forum_id ) && ! current_user_can( 'read_forum', $forum_id ) ) {
    24022414        bbp_set_404();
    24032415    }
     
    24542466
    24552467    // If forum is explicitly hidden and user not capable, set 404
    2456     if ( ! empty( $forum_id ) && bbp_is_forum_private( $forum_id ) && !current_user_can( 'read_private_forums' ) ) {
     2468    if ( ! empty( $forum_id ) && bbp_is_forum_private( $forum_id ) && ! current_user_can( 'read_forum', $forum_id ) ) {
    24572469        bbp_set_404();
    24582470    }
     
    24622474
    24632475/**
    2464  * Redirect if unathorized user is attempting to edit a forum
     2476 * Redirect if unauthorized user is attempting to edit a forum
    24652477 *
    24662478 * @since 2.1.0 bbPress (r3607)
Note: See TracChangeset for help on using the changeset viewer.