| @@ -11,37 +11,38 @@ | ||
| 11 | 11 | |
| 12 | 12 | /** |
| 13 | 13 | * Return forum capabilities |
| 14 | 14 | * |
| 15 | - * @since bbPress (r2593) | |
| 15 | + * @since 2.0.0 bbPress (r2593) | |
| 16 | 16 | * |
| 17 | - * @uses apply_filters() Calls 'bbp_get_forum_caps' with the capabilities | |
| 18 | 17 | * @return array Forum capabilities |
| 19 | 18 | */ |
| 20 | 19 | function bbp_get_forum_caps() { |
| 21 | - return apply_filters( 'bbp_get_forum_caps', array ( | |
| 22 | - 'edit_posts' => 'edit_forums', | |
| 23 | - 'edit_others_posts' => 'edit_others_forums', | |
| 24 | - 'publish_posts' => 'publish_forums', | |
| 25 | - 'read_private_posts' => 'read_private_forums', | |
| 26 | - 'read_hidden_posts' => 'read_hidden_forums', | |
| 27 | - 'delete_posts' => 'delete_forums', | |
| 28 | - 'delete_others_posts' => 'delete_others_forums' | |
| 29 | - ) ); | |
| 20 | + | |
| 21 | + // Filter & return | |
| 22 | + return (array) apply_filters( | |
| 23 | + 'bbp_get_forum_caps', | |
| 24 | + array( | |
| 25 | + 'edit_posts' => 'edit_forums', | |
| 26 | + 'edit_others_posts' => 'edit_others_forums', | |
| 27 | + 'publish_posts' => 'publish_forums', | |
| 28 | + 'read_private_posts' => 'read_private_forums', | |
| 29 | + 'read_hidden_posts' => 'read_hidden_forums', | |
| 30 | + 'delete_posts' => 'delete_forums', | |
| 31 | + 'delete_others_posts' => 'delete_others_forums' | |
| 32 | + ) | |
| 33 | + ); | |
| 30 | 34 | } |
| 31 | 35 | |
| 32 | 36 | /** |
| 33 | 37 | * Maps forum capabilities |
| 34 | 38 | * |
| 35 | - * @since bbPress (r4242) | |
| 39 | + * @since 2.2.0 bbPress (r4242) | |
| 36 | 40 | * |
| 37 | 41 | * @param array $caps Capabilities for meta capability |
| 38 | 42 | * @param string $cap Capability name |
| 39 | 43 | * @param int $user_id User id |
| 40 | - * @param mixed $args Arguments | |
| 41 | - * @uses get_post() To get the post | |
| 42 | - * @uses get_post_type_object() To get the post type object | |
| 43 | - * @uses apply_filters() Filter capability map results | |
| 44 | + * @param array $args Arguments | |
| 44 | 45 | * @return array Actual capabilities for meta capability |
| 45 | 46 | */ |
| 46 | 47 | function bbp_map_forum_meta_caps( $caps = array(), $cap = '', $user_id = 0, $args = array() ) { |
| 47 | 48 | |
| @@ -62,32 +63,64 @@ | ||
| 62 | 63 | |
| 63 | 64 | case 'read_forum' : |
| 64 | 65 | |
| 65 | 66 | // User cannot spectate |
| 66 | - if ( ! user_can( $user_id, 'spectate' ) ) { | |
| 67 | + if ( ! user_can( $user_id, 'spectate' ) && ! bbp_is_anonymous() ) { | |
| 67 | 68 | $caps = array( 'do_not_allow' ); |
| 68 | 69 | |
| 69 | 70 | // Do some post ID based logic |
| 70 | 71 | } else { |
| 71 | 72 | |
| 72 | - // Get the post | |
| 73 | + // Bail if no post ID | |
| 74 | + if ( empty( $args[0] ) ) { | |
| 75 | + break; | |
| 76 | + } | |
| 77 | + | |
| 78 | + // Get the post. | |
| 73 | 79 | $_post = get_post( $args[0] ); |
| 74 | - if ( !empty( $_post ) ) { | |
| 80 | + if ( ! empty( $_post ) ) { | |
| 75 | 81 | |
| 82 | + // Allow moderators of this forum through restricted ancestors | |
| 83 | + $parent_id = bbp_get_forum_parent_id( $_post->ID ); | |
| 84 | + if ( ! empty( $parent_id ) && ! bbp_is_user_forum_moderator( $user_id, $_post->ID ) && bbp_is_forum_restricted_for_user( $parent_id, $user_id ) ) { | |
| 85 | + $caps = array( 'do_not_allow' ); | |
| 86 | + break; | |
| 87 | + } | |
| 88 | + | |
| 76 | 89 | // Get caps for post type object |
| 77 | 90 | $post_type = get_post_type_object( $_post->post_type ); |
| 78 | 91 | |
| 79 | 92 | // Post is public |
| 80 | - if ( bbp_get_public_status_id() == $_post->post_status ) { | |
| 93 | + if ( bbp_get_public_status_id() === $_post->post_status ) { | |
| 94 | + | |
| 95 | + // Anonymous users do not have caps, but can 'exist' | |
| 96 | + if ( bbp_is_anonymous() ) { | |
| 97 | + $caps = array( 'exist' ); | |
| 98 | + | |
| 99 | + // Registered users need the 'spectate' cap | |
| 100 | + } else { | |
| 101 | + $caps = array( 'spectate' ); | |
| 102 | + } | |
| 103 | + | |
| 104 | + // User is author so allow read | |
| 105 | + } elseif ( (int) $user_id === (int) $_post->post_author ) { | |
| 81 | 106 | $caps = array( 'spectate' ); |
| 82 | 107 | |
| 83 | - // User is author so allow read | |
| 84 | - } elseif ( (int) $user_id == (int) $_post->post_author ) { | |
| 108 | + // Moderators can always read forum content | |
| 109 | + } elseif ( user_can( $user_id, 'moderate', $_post->ID ) ) { | |
| 85 | 110 | $caps = array( 'spectate' ); |
| 86 | 111 | |
| 87 | - // Unknown so map to private posts | |
| 112 | + // Private | |
| 113 | + } elseif ( bbp_get_hidden_status_id() === $_post->post_status ) { | |
| 114 | + $caps = array( $post_type->cap->read_hidden_posts ); | |
| 115 | + | |
| 116 | + // Hidden | |
| 117 | + } elseif ( bbp_get_private_status_id() === $_post->post_status ) { | |
| 118 | + $caps = array( $post_type->cap->read_private_posts ); | |
| 119 | + | |
| 120 | + // Unknown, so map to private | |
| 88 | 121 | } else { |
| 89 | - $caps = array( $post_type->cap->read_private_forums ); | |
| 122 | + $caps = array( $post_type->cap->read_private_posts ); | |
| 90 | 123 | } |
| 91 | 124 | } |
| 92 | 125 | } |
| 93 | 126 | |
| @@ -110,36 +143,65 @@ | ||
| 110 | 143 | case 'edit_forums' : |
| 111 | 144 | case 'edit_others_forums' : |
| 112 | 145 | |
| 113 | 146 | // Moderators can always edit |
| 114 | - if ( user_can( $user_id, 'moderate' ) ) { | |
| 115 | - $caps = array( 'moderate' ); | |
| 147 | + if ( bbp_is_user_keymaster( $user_id ) ) { | |
| 148 | + $caps = array( 'spectate' ); | |
| 149 | + | |
| 150 | + // Otherwise, block | |
| 151 | + } else { | |
| 152 | + $caps = array( 'do_not_allow' ); | |
| 116 | 153 | } |
| 117 | 154 | |
| 118 | 155 | break; |
| 119 | 156 | |
| 157 | + // Used for forum status, type, and visibility | |
| 158 | + case 'manage_forum_attributes' : | |
| 159 | + | |
| 160 | + // Bail if no forum ID | |
| 161 | + if ( empty( $args[0] ) ) { | |
| 162 | + $caps = array( 'do_not_allow' ); | |
| 163 | + break; | |
| 164 | + } | |
| 165 | + | |
| 166 | + // Only allow this capability for forums | |
| 167 | + $_post = get_post( $args[0] ); | |
| 168 | + $caps = ( ! empty( $_post ) && ( bbp_get_forum_post_type() === $_post->post_type ) ) | |
| 169 | + ? array( 'assign_moderators' ) | |
| 170 | + : array( 'do_not_allow' ); | |
| 171 | + | |
| 172 | + break; | |
| 173 | + | |
| 120 | 174 | // Used everywhere |
| 121 | 175 | case 'edit_forum' : |
| 122 | 176 | |
| 123 | - // Get the post | |
| 177 | + // Bail if no post ID | |
| 178 | + if ( empty( $args[0] ) ) { | |
| 179 | + break; | |
| 180 | + } | |
| 181 | + | |
| 182 | + // Get the post. | |
| 124 | 183 | $_post = get_post( $args[0] ); |
| 125 | - if ( !empty( $_post ) ) { | |
| 184 | + if ( ! empty( $_post ) ) { | |
| 126 | 185 | |
| 127 | 186 | // Get caps for post type object |
| 128 | 187 | $post_type = get_post_type_object( $_post->post_type ); |
| 129 | - $caps = array(); | |
| 130 | 188 | |
| 131 | 189 | // Add 'do_not_allow' cap if user is spam or deleted |
| 132 | 190 | if ( bbp_is_user_inactive( $user_id ) ) { |
| 133 | - $caps[] = 'do_not_allow'; | |
| 191 | + $caps = array( 'do_not_allow' ); | |
| 134 | 192 | |
| 135 | - // User is author so allow edit | |
| 136 | - } elseif ( (int) $user_id == (int) $_post->post_author ) { | |
| 137 | - $caps[] = $post_type->cap->edit_posts; | |
| 193 | + // Moderators can always read forum content | |
| 194 | + } elseif ( user_can( $user_id, 'moderate', $_post->ID ) ) { | |
| 195 | + $caps = array( 'spectate' ); | |
| 138 | 196 | |
| 197 | + // User is author so allow edit if not in admin | |
| 198 | + } elseif ( ! is_admin() && ( (int) $user_id === (int) $_post->post_author ) ) { | |
| 199 | + $caps = array( $post_type->cap->edit_posts ); | |
| 200 | + | |
| 139 | 201 | // Unknown, so map to edit_others_posts |
| 140 | 202 | } else { |
| 141 | - $caps[] = $post_type->cap->edit_others_posts; | |
| 203 | + $caps = array( $post_type->cap->edit_others_posts ); | |
| 142 | 204 | } |
| 143 | 205 | } |
| 144 | 206 | |
| 145 | 207 | break; |
| @@ -148,27 +210,31 @@ | ||
| 148 | 210 | |
| 149 | 211 | // Allow forum authors to delete forums (for BuddyPress groups, etc) |
| 150 | 212 | case 'delete_forum' : |
| 151 | 213 | |
| 152 | - // Get the post | |
| 214 | + // Bail if no post ID | |
| 215 | + if ( empty( $args[0] ) ) { | |
| 216 | + break; | |
| 217 | + } | |
| 218 | + | |
| 219 | + // Get the post. | |
| 153 | 220 | $_post = get_post( $args[0] ); |
| 154 | - if ( !empty( $_post ) ) { | |
| 221 | + if ( ! empty( $_post ) ) { | |
| 155 | 222 | |
| 156 | 223 | // Get caps for post type object |
| 157 | 224 | $post_type = get_post_type_object( $_post->post_type ); |
| 158 | - $caps = array(); | |
| 159 | 225 | |
| 160 | 226 | // Add 'do_not_allow' cap if user is spam or deleted |
| 161 | 227 | if ( bbp_is_user_inactive( $user_id ) ) { |
| 162 | - $caps[] = 'do_not_allow'; | |
| 228 | + $caps = array( 'do_not_allow' ); | |
| 163 | 229 | |
| 164 | 230 | // User is author so allow to delete |
| 165 | - } elseif ( (int) $user_id == (int) $_post->post_author ) { | |
| 166 | - $caps[] = $post_type->cap->delete_posts; | |
| 231 | + } elseif ( (int) $user_id === (int) $_post->post_author ) { | |
| 232 | + $caps = array( $post_type->cap->delete_posts ); | |
| 167 | 233 | |
| 168 | 234 | // Unknown so map to delete_others_posts |
| 169 | 235 | } else { |
| 170 | - $caps[] = $post_type->cap->delete_others_posts; | |
| 236 | + $caps = array( $post_type->cap->delete_others_posts ); | |
| 171 | 237 | } |
| 172 | 238 | } |
| 173 | 239 | |
| 174 | 240 | break; |
| @@ -174,11 +240,73 @@ | ||
| 174 | 240 | break; |
| 175 | 241 | |
| 176 | 242 | /** Admin *************************************************************/ |
| 177 | 243 | |
| 244 | + // Forum admin area. | |
| 178 | 245 | case 'bbp_forums_admin' : |
| 179 | - $caps = array( 'manage_options' ); | |
| 246 | + $caps = array( 'edit_forums' ); | |
| 180 | 247 | break; |
| 181 | 248 | } |
| 182 | 249 | |
| 183 | - return apply_filters( 'bbp_map_forum_meta_caps', $caps, $cap, $user_id, $args ); | |
| 250 | + // Filter & return | |
| 251 | + return (array) apply_filters( 'bbp_map_forum_meta_caps', $caps, $cap, $user_id, $args ); | |
| 252 | +} | |
| 253 | + | |
| 254 | +/** | |
| 255 | + * Can a user moderate a forum? | |
| 256 | + * | |
| 257 | + * @since 2.6.0 bbPress (r5834) | |
| 258 | + * | |
| 259 | + * @param int $user_id User id. | |
| 260 | + * @param int $forum_id Forum id. | |
| 261 | + * | |
| 262 | + * @return bool Return true if user is moderator of forum | |
| 263 | + */ | |
| 264 | +function bbp_is_user_forum_moderator( $user_id = 0, $forum_id = 0 ) { | |
| 265 | + $user_id = bbp_get_user_id( $user_id, false, empty( $user_id ) ); | |
| 266 | + $forum_id = bbp_get_forum_id( $forum_id ); | |
| 267 | + $retval = user_can( $user_id, 'moderate', $forum_id ); | |
| 268 | + | |
| 269 | + // Filter & return | |
| 270 | + return (bool) apply_filters( 'bbp_is_user_forum_moderator', $retval, $user_id, $forum_id ); | |
| 271 | +} | |
| 272 | + | |
| 273 | +/** | |
| 274 | + * Filter an array of forum IDs that are being excluded, and remove any forum | |
| 275 | + * IDs a user explicitly has access to. | |
| 276 | + * | |
| 277 | + * This typically means private or hidden forums the user has moderation rights | |
| 278 | + * to, but it can be filtered to mean just about anything. | |
| 279 | + * | |
| 280 | + * This function filters the return values of the following functions: | |
| 281 | + * - `bbp_get_private_forum_ids()` | |
| 282 | + * - `bbp_get_hidden_forum_ids()` | |
| 283 | + * | |
| 284 | + * @since 2.6.0 bbPress (r6426) | |
| 285 | + * | |
| 286 | + * @param array $forum_ids Forum IDs to check if the user ID is a moderator of | |
| 287 | + * @param int $user_id User ID to check if is a moderator of forums | |
| 288 | + * | |
| 289 | + * @return array | |
| 290 | + */ | |
| 291 | +function bbp_allow_forums_of_user( $forum_ids = array(), $user_id = 0 ) { | |
| 292 | + $user_id = bbp_get_user_id( $user_id, false, empty( $user_id ) ); | |
| 293 | + | |
| 294 | + // Store the original forum IDs | |
| 295 | + $original_forum_ids = $forum_ids; | |
| 296 | + | |
| 297 | + // Per-forum Moderators | |
| 298 | + if ( bbp_allow_forum_mods() ) { | |
| 299 | + | |
| 300 | + // Loop through forum IDs | |
| 301 | + foreach ( $forum_ids as $key => $forum_id ) { | |
| 302 | + | |
| 303 | + // Unset forum ID if user is a moderator or can otherwise read it | |
| 304 | + if ( bbp_is_user_forum_moderator( $user_id, $forum_id ) || user_can( $user_id, 'read_forum', $forum_id ) ) { | |
| 305 | + unset( $forum_ids[ $key ] ); | |
| 306 | + } | |
| 307 | + } | |
| 308 | + } | |
| 309 | + | |
| 310 | + // Filter & return | |
| 311 | + return (array) apply_filters( 'bbp_allow_forums_of_user', $forum_ids, $user_id, $original_forum_ids ); | |
| 184 | 312 | } |