Skip to:
Content

bbPress.org

Changeset 6384


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.

Location:
trunk/src
Files:
20 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/includes/admin/replies.php

    r6312 r6384  
    426426
    427427        // Current user cannot edit this reply
    428         if ( !current_user_can( 'edit_reply', $reply_id ) ) {
     428        if ( ! current_user_can( 'edit_reply', $reply_id ) ) {
    429429            return $reply_id;
    430430        }
     
    487487
    488488            // What is the user doing here?
    489             if ( !current_user_can( 'moderate', $reply->ID ) ) {
     489            if ( ! current_user_can( 'moderate', $reply->ID ) ) {
    490490                wp_die( __( 'You do not have permission to do that.', 'bbpress' ) );
    491491            }
  • trunk/src/includes/admin/topics.php

    r6334 r6384  
    550550
    551551        // Bail if current user cannot edit this topic
    552         if ( !current_user_can( 'edit_topic', $topic_id ) ) {
     552        if ( ! current_user_can( 'edit_topic', $topic_id ) ) {
    553553            return $topic_id;
    554554        }
     
    617617
    618618            // What is the user doing here?
    619             if ( !current_user_can( 'moderate', $topic->ID ) ) {
     619            if ( ! current_user_can( 'moderate', $topic->ID ) ) {
    620620                wp_die( __( 'You do not have permission to do that.', 'bbpress' ) );
    621621            }
     
    979979            // Close
    980980            // Show the 'close' and 'open' link on published and closed posts only
    981             if ( in_array( $topic->post_status, array( bbp_get_public_status_id(), bbp_get_closed_status_id() ) ) ) {
     981            if ( bbp_is_topic_public( $topic->ID ) ) {
    982982                $close_uri = wp_nonce_url( add_query_arg( array( 'topic_id' => $topic->ID, 'action' => 'bbp_toggle_topic_close' ), remove_query_arg( array( 'bbp_topic_toggle_notice', 'topic_id', 'failed', 'super' ) ) ), 'close-topic_' . $topic->ID );
    983983                if ( bbp_is_topic_open( $topic->ID ) ) {
  • trunk/src/includes/common/classes.php

    r6317 r6384  
    255255                && ( bbp_get_forum_post_type() === $object->post_type )
    256256                && ( bbp_is_forum_category( $object->ID )
    257                     || ( !current_user_can( 'edit_forum', $object->ID ) && bbp_is_forum_closed( $object->ID )
     257                    || ( ! current_user_can( 'edit_forum', $object->ID ) && bbp_is_forum_closed( $object->ID )
    258258                )
    259259            ) ) {
  • trunk/src/includes/common/functions.php

    r6298 r6384  
    564564        $last_posted = get_transient( '_bbp_' . bbp_current_author_ip() . '_last_posted' );
    565565
    566         if ( ! empty( $last_posted ) && time() < $last_posted + $throttle_time ) {
     566        if ( ! empty( $last_posted ) && ( time() < ( $last_posted + $throttle_time ) ) ) {
    567567            return false;
    568568        }
     
    573573        $last_posted = bbp_get_user_last_posted( $author_id );
    574574
    575         if ( isset( $last_posted ) && time() < $last_posted + $throttle_time && !current_user_can( 'throttle' ) ) {
     575        if ( isset( $last_posted ) && ( time() < ( $last_posted + $throttle_time ) ) && ! user_can( $author_id, 'throttle' ) ) {
    576576            return false;
    577577        }
     
    17751775                                'post_type'      => bbp_get_reply_post_type(),
    17761776                                'post_parent'    => 'any',
    1777                                 'post_status'    => array( bbp_get_public_status_id(), bbp_get_closed_status_id() ),
     1777                                'post_status'    => bbp_get_public_topic_statuses(),
    17781778                                'posts_per_page' => bbp_get_replies_per_rss_page(),
    17791779                                'order'          => 'DESC',
     
    17931793                                'post_type'      => bbp_get_topic_post_type(),
    17941794                                'post_parent'    => bbp_get_forum_id(),
    1795                                 'post_status'    => array( bbp_get_public_status_id(), bbp_get_closed_status_id() ),
     1795                                'post_status'    => bbp_get_public_topic_statuses(),
    17961796                                'posts_per_page' => bbp_get_topics_per_rss_page(),
    17971797                                'order'          => 'DESC'
     
    18151815                                'post_type'      => array( bbp_get_reply_post_type(), bbp_get_topic_post_type() ),
    18161816                                'post_parent'    => 'any',
    1817                                 'post_status'    => array( bbp_get_public_status_id(), bbp_get_closed_status_id() ),
     1817                                'post_status'    => bbp_get_public_topic_statuses(),
    18181818                                'posts_per_page' => bbp_get_replies_per_rss_page(),
    18191819                                'order'          => 'DESC',
  • trunk/src/includes/common/widgets.php

    r6196 r6384  
    746746                    'post_parent'         => $settings['parent_forum'],
    747747                    'posts_per_page'      => (int) $settings['max_shown'],
    748                     'post_status'         => array( bbp_get_public_status_id(), bbp_get_closed_status_id() ),
     748                    'post_status'         => bbp_get_public_topic_statuses(),
    749749                    'ignore_sticky_posts' => true,
    750750                    'no_found_rows'       => true,
     
    762762                    'post_parent'         => $settings['parent_forum'],
    763763                    'posts_per_page'      => (int) $settings['max_shown'],
    764                     'post_status'         => array( bbp_get_public_status_id(), bbp_get_closed_status_id() ),
     764                    'post_status'         => bbp_get_public_topic_statuses(),
    765765                    'ignore_sticky_posts' => true,
    766766                    'no_found_rows'       => true,
     
    779779                    'post_parent'         => $settings['parent_forum'],
    780780                    'posts_per_page'      => (int) $settings['max_shown'],
    781                     'post_status'         => array( bbp_get_public_status_id(), bbp_get_closed_status_id() ),
     781                    'post_status'         => bbp_get_public_topic_statuses(),
    782782                    'ignore_sticky_posts' => true,
    783783                    'no_found_rows'       => true,
     
    11311131        $widget_query = new WP_Query( array(
    11321132            'post_type'           => bbp_get_reply_post_type(),
    1133             'post_status'         => array( bbp_get_public_status_id(), bbp_get_closed_status_id() ),
     1133            'post_status'         => bbp_get_public_topic_statuses(),
    11341134            'posts_per_page'      => (int) $settings['max_shown'],
    11351135            'ignore_sticky_posts' => true,
  • trunk/src/includes/extend/buddypress/activity.php

    r6372 r6384  
    448448
    449449        // Validate activity data
    450         $user_id  = (int) $topic_author_id;
     450        $user_id  = bbp_get_user_id( $topic_author_id );
    451451        $topic_id = bbp_get_topic_id( $topic_id );
    452452        $forum_id = bbp_get_forum_id( $forum_id );
     
    548548
    549549        // Action based on new status
    550         if ( in_array( $post->post_status, array( bbp_get_public_status_id(), bbp_get_closed_status_id() ) ) ) {
     550        if ( bbp_is_topic_public( $post->ID ) ) {
    551551
    552552            // Validate topic data
     
    597597
    598598        // Validate activity data
    599         $user_id  = (int) $reply_author_id;
     599        $user_id  = bbp_get_user_id( $reply_author_id );
    600600        $reply_id = bbp_get_reply_id( $reply_id );
    601601        $topic_id = bbp_get_topic_id( $topic_id );
  • trunk/src/includes/extend/buddypress/notifications.php

    r6262 r6384  
    196196
    197197    // Check current user's ability to edit the user
    198     } elseif ( !current_user_can( 'edit_user', $user_id ) ) {
     198    } elseif ( ! current_user_can( 'edit_user', $user_id ) ) {
    199199        bbp_add_error( 'bbp_notification_permission', __( '<strong>ERROR</strong>: You do not have permission to mark notifications for that user.', 'bbpress' ) );
    200200    }
  • trunk/src/includes/forums/capabilities.php

    r6221 r6384  
    8585                        $caps = array( 'spectate' );
    8686
    87                     // Unknown so map to private posts
     87                    // Moderators can always read forum content
     88                    } elseif ( user_can( $user_id, 'moderate', $_post->ID ) ) {
     89                        $caps = array( 'spectate' );
     90
     91                    // Private
     92                    } elseif ( bbp_get_hidden_status_id() === $_post->post_status ) {
     93                        $caps = array( $post_type->cap->read_hidden_posts );
     94
     95                    // Hidden
     96                    } elseif ( bbp_get_private_status_id() === $_post->post_status ) {
     97                        $caps = array( $post_type->cap->read_private_posts );
     98
     99                    // Unknown, so map to private
    88100                    } else {
    89101                        $caps = array( $post_type->cap->read_private_posts );
     
    131143                // Get caps for post type object
    132144                $post_type = get_post_type_object( $_post->post_type );
    133                 $caps      = array();
    134145
    135146                // Add 'do_not_allow' cap if user is spam or deleted
    136147                if ( bbp_is_user_inactive( $user_id ) ) {
    137                     $caps[] = 'do_not_allow';
     148                    $caps = array( 'do_not_allow' );
    138149
    139150                // User is author so allow edit if not in admin
    140151                } elseif ( ! is_admin() && ( (int) $user_id === (int) $_post->post_author ) ) {
    141                     $caps[] = $post_type->cap->edit_posts;
     152                    $caps = array( $post_type->cap->edit_posts );
     153
     154                // Moderators can always read forum content
     155                } elseif ( user_can( $user_id, 'moderate', $_post->ID ) ) {
     156                    $caps = array( 'spectate' );
    142157
    143158                // Unknown, so map to edit_others_posts
    144159                } else {
    145                     $caps[] = $post_type->cap->edit_others_posts;
     160                    $caps = array( $post_type->cap->edit_others_posts );
    146161                }
    147162            }
     
    160175                // Get caps for post type object
    161176                $post_type = get_post_type_object( $_post->post_type );
    162                 $caps      = array();
    163177
    164178                // Add 'do_not_allow' cap if user is spam or deleted
    165179                if ( bbp_is_user_inactive( $user_id ) ) {
    166                     $caps[] = 'do_not_allow';
     180                    $caps = array( 'do_not_allow' );
    167181
    168182                // User is author so allow to delete
    169183                } elseif ( (int) $user_id === (int) $_post->post_author ) {
    170                     $caps[] = $post_type->cap->delete_posts;
     184                    $caps = array( $post_type->cap->delete_posts );
    171185
    172186                // Unknown so map to delete_others_posts
    173187                } else {
    174                     $caps[] = $post_type->cap->delete_others_posts;
     188                    $caps = array( $post_type->cap->delete_others_posts );
    175189                }
    176190            }
  • 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)
  • trunk/src/includes/forums/template.php

    r6357 r6384  
    694694    // Use passed integer as post_parent
    695695    if ( is_numeric( $args ) ) {
    696         $args = array( 'post_parent' => $args );
     696        $args = array( 'post_parent' => bbp_get_forum_id( $args ) );
    697697    }
    698698
     
    729729        'no_found_rows'       => true
    730730    ), 'forum_get_subforums' );
     731
     732    // Ensure post_parent is properly set
    731733    $r['post_parent'] = bbp_get_forum_id( $r['post_parent'] );
    732734
     
    735737
    736738    // No forum passed
    737     $sub_forums = ! empty( $r['post_parent'] ) ? $get_posts->query( $r ) : array();
     739    $sub_forums = ! empty( $r['post_parent'] )
     740        ? $get_posts->query( $r )
     741        : array();
    738742
    739743    return (array) apply_filters( 'bbp_forum_get_subforums', $sub_forums, $r, $args );
     
    13011305
    13021306            // Hidden link
    1303             $retval .= ! bbp_get_view_all()
     1307            $retval .= ! bbp_get_view_all( 'edit_others_topics' )
    13041308                ? " <a href='" . esc_url( bbp_add_view_all( $link, true ) ) . "'>" . esc_html( $extra ) . "</a>"
    13051309                : " {$extra}";
     
    19441948 */
    19451949function bbp_suppress_private_forum_meta( $retval, $forum_id ) {
    1946     if ( bbp_is_forum_private( $forum_id, false ) && ! current_user_can( 'read_private_forums' ) ) {
     1950    if ( bbp_is_forum_private( $forum_id, false ) && ! current_user_can( 'read_forum', $forum_id ) ) {
    19471951        $retval = '-';
    19481952    }
     
    19781982
    19791983        // What post type are we looking at?
    1980         $post_type = get_post_field( 'post_type', $args['post_id'] );
    1981 
    1982         switch ( $post_type ) {
     1984        switch ( get_post_type( $args['post_id'] ) ) {
    19831985
    19841986            // Topic
    19851987            case bbp_get_topic_post_type() :
    1986                 if ( bbp_is_forum_private( bbp_get_topic_forum_id( $args['post_id'] ) ) ) {
    1987                     $retval = '';
    1988                 }
    1989 
     1988                $forum_id = bbp_get_topic_forum_id( $args['post_id'] );
    19901989                break;
    19911990
    19921991            // Reply
    19931992            case bbp_get_reply_post_type() :
    1994                 if ( bbp_is_forum_private( bbp_get_reply_forum_id( $args['post_id'] ) ) ) {
    1995                     $retval = '';
    1996                 }
    1997 
     1993                $forum_id = bbp_get_reply_forum_id( $args['post_id'] );
    19981994                break;
    19991995
    20001996            // Post
    20011997            default :
    2002                 if ( bbp_is_forum_private( $args['post_id'] ) ) {
    2003                     $retval = '';
    2004                 }
    2005 
     1998                $forum_id = bbp_get_forum_id( $args['post_id'] );
    20061999                break;
    20072000        }
    2008     }
    2009 
    2010     return apply_filters( 'bbp_suppress_private_author_link', $retval );
     2001
     2002        // Hide if forum is private
     2003        if ( bbp_is_forum_private( $forum_id ) ) {
     2004            $retval = '';
     2005        }
     2006    }
     2007
     2008    return apply_filters( 'bbp_suppress_private_author_link', $retval, $author_link, $args );
    20112009}
    20122010
  • trunk/src/includes/replies/capabilities.php

    r6265 r6384  
    7878                        $caps = array( 'spectate' );
    7979
     80                    // Moderators can always edit forum content
     81                    } elseif ( user_can( $user_id, 'moderate', $_post->ID ) ) {
     82                        $caps = array( 'spectate' );
     83
    8084                    // Unknown so map to private posts
    8185                    } else {
     
    108112                $caps = array( 'moderate' );
    109113
    110             // Otherwise, block
     114            // Otherwise, check forum
    111115            } else {
    112                 $caps = array( 'do_not_allow' );
     116                $forum_id = bbp_get_forum_id();
     117
     118                // Moderators can always edit forum content
     119                if ( user_can( $user_id, 'moderate', $forum_id ) ) {
     120                    $caps = array( 'spectate' );
     121
     122                // Fallback to do_not_allow
     123                } else {
     124                    $caps = array( 'do_not_allow' );
     125                }
    113126            }
    114127
     
    124137                // Get post type object
    125138                $post_type = get_post_type_object( $_post->post_type );
    126                 $caps      = array();
    127139
    128140                // Add 'do_not_allow' cap if user is spam or deleted
    129141                if ( bbp_is_user_inactive( $user_id ) ) {
    130                     $caps[] = 'do_not_allow';
     142                    $caps = array( 'do_not_allow' );
    131143
    132144                // User is author so allow edit if not in admin
    133145                } elseif ( ! is_admin() && ( (int) $user_id === (int) $_post->post_author ) ) {
    134                     $caps[] = $post_type->cap->edit_posts;
    135 
    136                 // User is a per-forum moderator, make sure they can spectate.
    137                 } elseif ( bbp_is_user_forum_moderator( $user_id, bbp_get_reply_forum_id( $_post->ID ) ) ) {
     146                    $caps = array( $post_type->cap->edit_posts );
     147
     148                // Moderators can always edit forum content
     149                } elseif ( user_can( $user_id, 'moderate', $_post->ID ) ) {
    138150                    $caps = array( 'spectate' );
    139151
    140152                // Fallback to edit_others_posts.
    141153                } else {
    142                     $caps[] = $post_type->cap->edit_others_posts;
     154                    $caps = array( $post_type->cap->edit_others_posts );
    143155                }
    144156            }
     
    156168                // Get post type object
    157169                $post_type = get_post_type_object( $_post->post_type );
    158                 $caps      = array();
    159170
    160171                // Add 'do_not_allow' cap if user is spam or deleted
    161172                if ( bbp_is_user_inactive( $user_id ) ) {
    162                     $caps[] = 'do_not_allow';
    163 
    164                 // Moderators can always edit forum content
    165                 } elseif ( user_can( $user_id, 'moderate' ) ) {
    166                     $caps[] = 'moderate';
     173                    $caps = array( 'do_not_allow' );
    167174
    168175                // User is author so allow delete if not in admin
    169176                } elseif ( ! is_admin() && ( (int) $user_id === (int) $_post->post_author ) ) {
    170                     $caps[] = $post_type->cap->delete_posts;
     177                    $caps = array( $post_type->cap->delete_posts );
     178
     179                // Moderators can always edit forum content
     180                } elseif ( user_can( $user_id, 'moderate', $_post->ID ) ) {
     181                     $caps = array( 'spectate' );
    171182
    172183                // Unknown so map to delete_others_posts
    173184                } else {
    174                     $caps[] = $post_type->cap->delete_others_posts;
     185                    $caps = array( $post_type->cap->delete_others_posts );
    175186                }
    176187            }
  • trunk/src/includes/replies/functions.php

    r6369 r6384  
    179179
    180180        // User cannot create replies
    181         if ( !current_user_can( 'publish_replies' ) ) {
     181        if ( ! current_user_can( 'publish_replies' ) ) {
    182182            bbp_add_error( 'bbp_reply_permission', __( '<strong>ERROR</strong>: You do not have permission to reply.', 'bbpress' ) );
    183183        }
     
    271271
    272272            // Forum is closed and user cannot access
    273             if ( bbp_is_forum_closed( $forum_id ) && !current_user_can( 'edit_forum', $forum_id ) ) {
     273            if ( bbp_is_forum_closed( $forum_id ) && ! current_user_can( 'edit_forum', $forum_id ) ) {
    274274                bbp_add_error( 'bbp_new_reply_forum_closed', __( '<strong>ERROR</strong>: This forum has been closed to new replies.', 'bbpress' ) );
    275275            }
    276276
    277277            // Forum is private and user cannot access
    278             if ( bbp_is_forum_private( $forum_id ) ) {
    279                 if ( !current_user_can( 'read_private_forums' ) ) {
    280                     bbp_add_error( 'bbp_new_reply_forum_private', __( '<strong>ERROR</strong>: This forum is private and you do not have the capability to read or create new replies in it.', 'bbpress' ) );
    281                 }
     278            if ( bbp_is_forum_private( $forum_id ) && ! current_user_can( 'read_forum', $forum_id ) ) {
     279                bbp_add_error( 'bbp_new_reply_forum_private', __( '<strong>ERROR</strong>: This forum is private and you do not have the capability to read or create new replies in it.', 'bbpress' ) );
    282280
    283281            // Forum is hidden and user cannot access
    284             } elseif ( bbp_is_forum_hidden( $forum_id ) ) {
    285                 if ( !current_user_can( 'read_hidden_forums' ) ) {
    286                     bbp_add_error( 'bbp_new_reply_forum_hidden', __( '<strong>ERROR</strong>: This forum is hidden and you do not have the capability to read or create new replies in it.', 'bbpress' ) );
    287                 }
     282            } elseif ( bbp_is_forum_hidden( $forum_id ) && ! current_user_can( 'read_forum', $forum_id ) ) {
     283                bbp_add_error( 'bbp_new_reply_forum_hidden', __( '<strong>ERROR</strong>: This forum is hidden and you do not have the capability to read or create new replies in it.', 'bbpress' ) );
    288284            }
    289285        }
     
    572568
    573569            // User cannot edit this reply
    574             if ( !current_user_can( 'edit_reply', $reply_id ) ) {
     570            if ( ! current_user_can( 'edit_reply', $reply_id ) ) {
    575571                bbp_add_error( 'bbp_edit_reply_permission', __( '<strong>ERROR</strong>: You do not have permission to edit that reply.', 'bbpress' ) );
    576572                return;
     
    614610
    615611            // Forum is closed and user cannot access
    616             if ( bbp_is_forum_closed( $forum_id ) && !current_user_can( 'edit_forum', $forum_id ) ) {
     612            if ( bbp_is_forum_closed( $forum_id ) && ! current_user_can( 'edit_forum', $forum_id ) ) {
    617613                bbp_add_error( 'bbp_edit_reply_forum_closed', __( '<strong>ERROR</strong>: This forum has been closed to new replies.', 'bbpress' ) );
    618614            }
    619615
    620616            // Forum is private and user cannot access
    621             if ( bbp_is_forum_private( $forum_id ) ) {
    622                 if ( !current_user_can( 'read_private_forums' ) ) {
    623                     bbp_add_error( 'bbp_edit_reply_forum_private', __( '<strong>ERROR</strong>: This forum is private and you do not have the capability to read or create new replies in it.', 'bbpress' ) );
    624                 }
     617            if ( bbp_is_forum_private( $forum_id ) && ! current_user_can( 'read_forum', $forum_id ) ) {
     618                bbp_add_error( 'bbp_edit_reply_forum_private', __( '<strong>ERROR</strong>: This forum is private and you do not have the capability to read or create new replies in it.', 'bbpress' ) );
    625619
    626620            // Forum is hidden and user cannot access
    627             } elseif ( bbp_is_forum_hidden( $forum_id ) ) {
    628                 if ( !current_user_can( 'read_hidden_forums' ) ) {
    629                     bbp_add_error( 'bbp_edit_reply_forum_hidden', __( '<strong>ERROR</strong>: This forum is hidden and you do not have the capability to read or create new replies in it.', 'bbpress' ) );
    630                 }
     621            } elseif ( bbp_is_forum_hidden( $forum_id ) && ! current_user_can( 'read_forum', $forum_id ) ) {
     622                bbp_add_error( 'bbp_edit_reply_forum_hidden', __( '<strong>ERROR</strong>: This forum is hidden and you do not have the capability to read or create new replies in it.', 'bbpress' ) );
    631623            }
    632624        }
     
    889881
    890882    } else {
    891         if ( empty( $is_edit ) && !current_user_can( 'throttle' ) ) {
     883        if ( empty( $is_edit ) && ! current_user_can( 'throttle' ) ) {
    892884            bbp_update_user_last_posted( $author_id );
    893885        }
     
    13621354
    13631355    // Use cannot edit topic
    1364     if ( !current_user_can( 'edit_topic', $source_topic->ID ) ) {
     1356    if ( ! current_user_can( 'edit_topic', $source_topic->ID ) ) {
    13651357        bbp_add_error( 'bbp_move_reply_source_permission', __( '<strong>ERROR</strong>: You do not have permission to edit the source topic.', 'bbpress' ) );
    13661358    }
     
    14001392
    14011393                // User cannot edit the destination topic
    1402                 if ( !current_user_can( 'edit_topic', $destination_topic->ID ) ) {
     1394                if ( ! current_user_can( 'edit_topic', $destination_topic->ID ) ) {
    14031395                    bbp_add_error( 'bbp_move_reply_destination_permission', __( '<strong>ERROR</strong>: You do not have permission to edit the destination topic.', 'bbpress' ) );
    14041396                }
  • trunk/src/includes/replies/template.php

    r6363 r6384  
    151151
    152152    // What are the default allowed statuses (based on user caps)
    153     if ( bbp_get_view_all() ) {
     153    if ( bbp_get_view_all( 'edit_others_replies' ) ) {
    154154
    155155        // Default view=all statuses
  • trunk/src/includes/topics/capabilities.php

    r6265 r6384  
    9696                        $caps = array( 'spectate' );
    9797
     98                    // Moderators can always edit forum content
     99                    } elseif ( user_can( $user_id, 'moderate', $_post->ID ) ) {
     100                        $caps = array( 'spectate' );
     101
    98102                    // Unknown so map to private posts
    99103                    } else {
     
    126130                $caps = array( $cap );
    127131
    128             // Otherwise, block
     132            // Otherwise, check forum
    129133            } else {
    130                 $caps = array( 'do_not_allow' );
     134                $forum_id = bbp_get_forum_id();
     135
     136                // Moderators can always edit forum content
     137                if ( user_can( $user_id, 'moderate', $forum_id ) ) {
     138                    $caps = array( 'spectate' );
     139
     140                // Fallback to do_not_allow
     141                } else {
     142                    $caps = array( 'do_not_allow' );
     143                }
    131144            }
    132145
     
    142155                // Get caps for post type object
    143156                $post_type = get_post_type_object( $_post->post_type );
    144                 $caps      = array();
    145157
    146158                // Add 'do_not_allow' cap if user is spam or deleted
    147159                if ( bbp_is_user_inactive( $user_id ) ) {
    148                     $caps[] = 'do_not_allow';
     160                    $caps = array( 'do_not_allow' );
    149161
    150162                // User is author so allow edit if not in admin
    151163                } elseif ( ! is_admin() && ( (int) $user_id === (int) $_post->post_author ) ) {
    152                     $caps[] = $post_type->cap->edit_posts;
     164                    $caps = array( $post_type->cap->edit_posts );
     165
     166                // Moderators can always edit forum content
     167                } elseif ( user_can( $user_id, 'moderate', $_post->ID ) ) {
     168                    $caps = array( 'spectate' );
    153169
    154170                // Unknown, so map to edit_others_posts
    155171                } else {
    156 
    157                     // If user is a per-forum moderator, make sure they can spectate.
    158                     if ( bbp_is_user_forum_moderator( $user_id, bbp_get_topic_forum_id( $_post->ID ) ) ) {
    159                         $caps = array( 'spectate' );
    160 
    161                     // Fallback to edit_others_posts.
    162                     } else {
    163                         $caps[] = $post_type->cap->edit_others_posts;
    164                     }
     172                    $caps = array( $post_type->cap->edit_others_posts );
    165173                }
    166174            }
     
    178186                // Get caps for post type object
    179187                $post_type = get_post_type_object( $_post->post_type );
    180                 $caps      = array();
    181188
    182189                // Add 'do_not_allow' cap if user is spam or deleted
    183190                if ( bbp_is_user_inactive( $user_id ) ) {
    184                     $caps[] = 'do_not_allow';
     191                    $caps = array( 'do_not_allow' );
    185192
    186193                // Moderators can always edit forum content
    187                 } elseif ( user_can( $user_id, 'moderate' ) ) {
    188                     $caps[] = 'moderate';
     194                } elseif ( user_can( $user_id, 'moderate', $_post->ID ) ) {
     195                    $caps = array( 'spectate' );
    189196
    190197                // User is author so allow delete if not in admin
    191198                } elseif ( ! is_admin() && ( (int) $user_id === (int) $_post->post_author ) ) {
    192                     $caps[] = $post_type->cap->delete_posts;
     199                    $caps = array( $post_type->cap->delete_posts );
    193200
    194201                // Unknown so map to delete_others_posts
    195202                } else {
    196                     $caps[] = $post_type->cap->delete_others_posts;
     203                    $caps = array( $post_type->cap->delete_others_posts );
    197204                }
    198205            }
  • trunk/src/includes/topics/functions.php

    r6341 r6384  
    260260
    261261            // Forum is private and user cannot access
    262             if ( bbp_is_forum_private( $forum_id ) ) {
    263                 if ( ! current_user_can( 'read_private_forums' ) ) {
    264                     bbp_add_error( 'bbp_new_topic_forum_private', __( '<strong>ERROR</strong>: This forum is private and you do not have the capability to read or create new topics in it.', 'bbpress' ) );
    265                 }
     262            if ( bbp_is_forum_private( $forum_id ) && ! current_user_can( 'read_forum', $forum_id ) ) {
     263                bbp_add_error( 'bbp_new_topic_forum_private', __( '<strong>ERROR</strong>: This forum is private and you do not have the capability to read or create new topics in it.', 'bbpress' ) );
    266264
    267265            // Forum is hidden and user cannot access
    268             } elseif ( bbp_is_forum_hidden( $forum_id ) ) {
    269                 if ( ! current_user_can( 'read_hidden_forums' ) ) {
    270                     bbp_add_error( 'bbp_new_topic_forum_hidden', __( '<strong>ERROR</strong>: This forum is hidden and you do not have the capability to read or create new topics in it.', 'bbpress' ) );
    271                 }
     266            } elseif ( bbp_is_forum_hidden( $forum_id ) && ! current_user_can( 'read_forum', $forum_id ) ) {
     267                bbp_add_error( 'bbp_new_topic_forum_hidden', __( '<strong>ERROR</strong>: This forum is hidden and you do not have the capability to read or create new topics in it.', 'bbpress' ) );
    272268            }
    273269        }
     
    562558
    563559            // Forum is private and user cannot access
    564             if ( bbp_is_forum_private( $forum_id ) ) {
    565                 if ( ! current_user_can( 'read_private_forums' ) ) {
    566                     bbp_add_error( 'bbp_edit_topic_forum_private', __( '<strong>ERROR</strong>: This forum is private and you do not have the capability to read or create new topics in it.', 'bbpress' ) );
    567                 }
     560            if ( bbp_is_forum_private( $forum_id ) && ! current_user_can( 'read_forum', $forum_id ) ) {
     561                bbp_add_error( 'bbp_edit_topic_forum_private', __( '<strong>ERROR</strong>: This forum is private and you do not have the capability to read or create new topics in it.', 'bbpress' ) );
    568562
    569563            // Forum is hidden and user cannot access
    570             } elseif ( bbp_is_forum_hidden( $forum_id ) ) {
    571                 if ( ! current_user_can( 'read_hidden_forums' ) ) {
    572                     bbp_add_error( 'bbp_edit_topic_forum_hidden', __( '<strong>ERROR</strong>: This forum is hidden and you do not have the capability to read or create new topics in it.', 'bbpress' ) );
    573                 }
     564            } elseif ( bbp_is_forum_hidden( $forum_id ) && ! current_user_can( 'read_forum', $forum_id ) ) {
     565                bbp_add_error( 'bbp_edit_topic_forum_hidden', __( '<strong>ERROR</strong>: This forum is hidden and you do not have the capability to read or create new topics in it.', 'bbpress' ) );
    574566            }
    575567        }
     
    619611
    620612        // Set post status to pending if public or closed
    621         if ( in_array( $topic->post_status, array( bbp_get_public_status_id(), bbp_get_closed_status_id() ) ) ) {
     613        if ( bbp_is_topic_public( $topic->ID ) ) {
    622614            $topic_status = bbp_get_pending_status_id();
    623615        }
     
    746738
    747739        // View all?
    748         $view_all = bbp_get_view_all();
     740        $view_all = bbp_get_view_all( 'edit_others_replies' );
    749741
    750742        // Get the topic URL
  • trunk/src/includes/topics/template.php

    r6362 r6384  
    173173
    174174    // What are the default allowed statuses (based on user caps)
    175     if ( bbp_get_view_all() ) {
     175    if ( bbp_get_view_all( 'edit_others_topics' ) ) {
    176176
    177177        // Default view=all statuses
     
    310310
    311311                // What are the default allowed statuses (based on user caps)
    312                 if ( bbp_get_view_all() ) {
     312                if ( bbp_get_view_all( 'edit_others_topics' ) ) {
    313313                    $sticky_query['post_status'] = $r['post_status'];
    314314
     
    900900        }
    901901
     902        // Maybe add view-all args
     903        $add_args = bbp_get_view_all( 'edit_others_replies' )
     904            ? array( 'view' => 'all' )
     905            : false;
     906
    902907        // Add pagination to query object
    903908        $pagination_links = paginate_links( array(
     
    909914            'mid_size'  => 2,
    910915            'end_size'  => 3,
    911             'add_args'  => ( bbp_get_view_all() ) ? array( 'view' => 'all' ) : false
     916            'add_args'  => $add_args
    912917        ) );
    913918
     
    11631168
    11641169/**
     1170 * Return array of public topic statuses.
     1171 *
     1172 * @since 2.6.0 bbPress (r6383)
     1173 *
     1174 * @uses bbp_get_public_status_id()
     1175 * @uses bbp_get_closed_status_id()
     1176 *
     1177 * @return array
     1178 */
     1179function bbp_get_public_topic_statuses() {
     1180    $statuses = bbp_get_public_topic_statuses();
     1181
     1182    return (array) apply_filters( 'bbp_get_public_topic_statuses', $statuses );
     1183}
     1184
     1185/**
    11651186 * Is the topic closed to new replies?
    11661187 *
     
    11971218
    11981219/**
    1199  * Is the topic not spam or deleted?
     1220 * Is the topic publicly viewable?
     1221 *
     1222 * @since 2.6.0 bbPress (r6383)
     1223 *
     1224 * @param int $topic_id Optional. Topic id
     1225 * @uses bbp_get_topic_id() To get the topic id
     1226 * @uses bbp_get_topic_status() To get the topic status
     1227 * @uses apply_filters() Calls 'bbp_is_topic_public' with the topic id
     1228 * @return bool True if public, false if not.
     1229 */
     1230function bbp_is_topic_public( $topic_id = 0 ) {
     1231    $topic_id  = bbp_get_topic_id( $topic_id );
     1232    $status    = bbp_get_topic_status( $topic_id );
     1233    $public    = bbp_get_public_topic_statuses();
     1234    $is_public = in_array( $status, $public, true );
     1235
     1236    return (bool) apply_filters( 'bbp_is_topic_public', (bool) $is_public, $topic_id );
     1237}
     1238
     1239/**
     1240 * Does the topic have a published status?
    12001241 *
    12011242 * @since 2.0.0 bbPress (r3496)
     
    22412282
    22422283            // Hidden link
    2243             $retval .= ! bbp_get_view_all()
     2284            $retval .= ! bbp_get_view_all( 'edit_others_replies' )
    22442285                ? " <a href='" . esc_url( bbp_add_view_all( $link, true ) ) . "'>" . esc_html( $extra ) . "</a>"
    22452286                : " {$extra}";
  • trunk/src/includes/users/capabilities.php

    r6294 r6384  
    8585                    // Forum.
    8686                    case bbp_get_forum_post_type() :
    87                         $forum_id = $_post->ID;
     87                        $forum_id = bbp_get_forum_id( $_post->ID );
    8888                        break;
    8989
  • trunk/src/includes/users/template.php

    r6335 r6384  
    21532153
    21542154    // Forum is private, and user can see it
    2155     } elseif ( bbp_is_forum_private( $forum_id, $r['check_ancestors'] ) && user_can( $user_id, 'read_private_forums' ) ) {
     2155    } elseif ( bbp_is_forum_private( $forum_id, $r['check_ancestors'] ) && user_can( $user_id, 'read_forum', $forum_id ) ) {
    21562156        $retval = true;
    21572157
    21582158    // Forum is hidden, and user can see it
    2159     } elseif ( bbp_is_forum_hidden ( $forum_id, $r['check_ancestors'] ) && user_can( $user_id, 'read_hidden_forums'  ) ) {
     2159    } elseif ( bbp_is_forum_hidden ( $forum_id, $r['check_ancestors'] ) && user_can( $user_id, 'read_forum', $forum_id  ) ) {
    21602160        $retval = true;
    21612161    }
  • trunk/src/templates/default/bbpress/form-topic-merge.php

    r6258 r6384  
    5353                                            'post_type'   => bbp_get_topic_post_type(),
    5454                                            'post_parent' => bbp_get_topic_forum_id( bbp_get_topic_id() ),
    55                                             'post_status' => array( bbp_get_public_status_id(), bbp_get_closed_status_id() ),
     55                                            'post_status' => bbp_get_public_topic_statuses(),
    5656                                            'selected'    => -1,
    5757                                            'exclude'     => bbp_get_topic_id(),
  • trunk/src/templates/default/bbpress/form-topic-split.php

    r6258 r6384  
    6060                                            'post_type'   => bbp_get_topic_post_type(),
    6161                                            'post_parent' => bbp_get_topic_forum_id( bbp_get_topic_id() ),
    62                                             'post_status' => array( bbp_get_public_status_id(), bbp_get_closed_status_id() ),
     62                                            'post_status' => bbp_get_public_topic_statuses(),
    6363                                            'selected'    => -1,
    6464                                            'exclude'     => bbp_get_topic_id(),
Note: See TracChangeset for help on using the changeset viewer.