| @@ -11,54 +11,61 @@ | ||
| 11 | 11 | |
| 12 | 12 | /** |
| 13 | 13 | * Return topic capabilities |
| 14 | 14 | * |
| 15 | - * @since bbPress (r2593) | |
| 15 | + * @since 2.0.0 bbPress (r2593) | |
| 16 | 16 | * |
| 17 | - * @uses apply_filters() Calls 'bbp_get_topic_caps' with the capabilities | |
| 18 | 17 | * @return array Topic capabilities |
| 19 | 18 | */ |
| 20 | 19 | function bbp_get_topic_caps() { |
| 21 | - return apply_filters( 'bbp_get_topic_caps', array ( | |
| 22 | - 'edit_posts' => 'edit_topics', | |
| 23 | - 'edit_others_posts' => 'edit_others_topics', | |
| 24 | - 'publish_posts' => 'publish_topics', | |
| 25 | - 'read_private_posts' => 'read_private_topics', | |
| 26 | - 'read_hidden_posts' => 'read_hidden_topics', | |
| 27 | - 'delete_posts' => 'delete_topics', | |
| 28 | - 'delete_others_posts' => 'delete_others_topics' | |
| 29 | - ) ); | |
| 20 | + | |
| 21 | + // Filter & return | |
| 22 | + return (array) apply_filters( | |
| 23 | + 'bbp_get_topic_caps', | |
| 24 | + array( | |
| 25 | + 'edit_posts' => 'edit_topics', | |
| 26 | + 'edit_others_posts' => 'edit_others_topics', | |
| 27 | + 'publish_posts' => 'publish_topics', | |
| 28 | + 'read_private_posts' => 'read_private_topics', | |
| 29 | + 'read_hidden_posts' => 'read_hidden_topics', | |
| 30 | + 'delete_posts' => 'delete_topics', | |
| 31 | + 'delete_others_posts' => 'delete_others_topics' | |
| 32 | + ) | |
| 33 | + ); | |
| 30 | 34 | } |
| 31 | 35 | |
| 32 | 36 | /** |
| 33 | 37 | * Return topic tag capabilities |
| 34 | 38 | * |
| 35 | - * @since bbPress (r2593) | |
| 39 | + * @since 2.0.0 bbPress (r2593) | |
| 36 | 40 | * |
| 37 | - * @uses apply_filters() Calls 'bbp_get_topic_tag_caps' with the capabilities | |
| 41 | + * | |
| 38 | 42 | * @return array Topic tag capabilities |
| 39 | 43 | */ |
| 40 | 44 | function bbp_get_topic_tag_caps() { |
| 41 | - return apply_filters( 'bbp_get_topic_tag_caps', array ( | |
| 42 | - 'manage_terms' => 'manage_topic_tags', | |
| 43 | - 'edit_terms' => 'edit_topic_tags', | |
| 44 | - 'delete_terms' => 'delete_topic_tags', | |
| 45 | - 'assign_terms' => 'assign_topic_tags' | |
| 46 | - ) ); | |
| 45 | + | |
| 46 | + // Filter & return | |
| 47 | + return (array) apply_filters( | |
| 48 | + 'bbp_get_topic_tag_caps', | |
| 49 | + array( | |
| 50 | + 'manage_terms' => 'manage_topic_tags', | |
| 51 | + 'edit_terms' => 'edit_topic_tags', | |
| 52 | + 'delete_terms' => 'delete_topic_tags', | |
| 53 | + 'assign_terms' => 'assign_topic_tags' | |
| 54 | + ) | |
| 55 | + ); | |
| 47 | 56 | } |
| 48 | 57 | |
| 49 | 58 | /** |
| 50 | 59 | * Maps topic capabilities |
| 51 | 60 | * |
| 52 | - * @since bbPress (r4242) | |
| 61 | + * @since 2.2.0 bbPress (r4242) | |
| 53 | 62 | * |
| 54 | - * @param array $caps Capabilities for meta capability | |
| 55 | - * @param string $cap Capability name | |
| 56 | - * @param int $user_id User id | |
| 57 | - * @param mixed $args Arguments | |
| 58 | - * @uses get_post() To get the post | |
| 59 | - * @uses get_post_type_object() To get the post type object | |
| 60 | - * @uses apply_filters() Filter capability map results | |
| 63 | + * @param array $caps Capabilities for meta capability. | |
| 64 | + * @param string $cap Capability name. | |
| 65 | + * @param int $user_id User id. | |
| 66 | + * @param array $args Arguments. | |
| 67 | + * | |
| 61 | 68 | * @return array Actual capabilities for meta capability |
| 62 | 69 | */ |
| 63 | 70 | function bbp_map_topic_meta_caps( $caps = array(), $cap = '', $user_id = 0, $args = array() ) { |
| 64 | 71 | |
| @@ -69,27 +76,44 @@ | ||
| 69 | 76 | |
| 70 | 77 | case 'read_topic' : |
| 71 | 78 | |
| 72 | 79 | // User cannot spectate |
| 73 | - if ( ! user_can( $user_id, 'spectate' ) ) { | |
| 80 | + if ( ! user_can( $user_id, 'spectate' ) && ! bbp_is_anonymous() ) { | |
| 74 | 81 | $caps = array( 'do_not_allow' ); |
| 75 | 82 | |
| 76 | 83 | // Do some post ID based logic |
| 77 | 84 | } else { |
| 78 | 85 | |
| 79 | - // Get the post | |
| 86 | + // Bail if no post ID | |
| 87 | + if ( empty( $args[0] ) ) { | |
| 88 | + break; | |
| 89 | + } | |
| 90 | + | |
| 91 | + // Get the post. | |
| 80 | 92 | $_post = get_post( $args[0] ); |
| 81 | - if ( !empty( $_post ) ) { | |
| 93 | + if ( ! empty( $_post ) ) { | |
| 82 | 94 | |
| 83 | 95 | // Get caps for post type object |
| 84 | 96 | $post_type = get_post_type_object( $_post->post_type ); |
| 85 | 97 | |
| 86 | 98 | // Post is public |
| 87 | - if ( bbp_get_public_status_id() == $_post->post_status ) { | |
| 99 | + if ( bbp_get_public_status_id() === $_post->post_status ) { | |
| 100 | + | |
| 101 | + // Anonymous users do not have caps, but can 'exist' | |
| 102 | + if ( bbp_is_anonymous() ) { | |
| 103 | + $caps = array( 'exist' ); | |
| 104 | + | |
| 105 | + // Registered users need the 'spectate' cap | |
| 106 | + } else { | |
| 107 | + $caps = array( 'spectate' ); | |
| 108 | + } | |
| 109 | + | |
| 110 | + // User is author so allow read | |
| 111 | + } elseif ( (int) $user_id === (int) $_post->post_author ) { | |
| 88 | 112 | $caps = array( 'spectate' ); |
| 89 | 113 | |
| 90 | - // User is author so allow read | |
| 91 | - } elseif ( (int) $user_id == (int) $_post->post_author ) { | |
| 114 | + // Moderators can always edit forum content | |
| 115 | + } elseif ( user_can( $user_id, 'moderate', $_post->ID ) ) { | |
| 92 | 116 | $caps = array( 'spectate' ); |
| 93 | 117 | |
| 94 | 118 | // Unknown so map to private posts |
| 95 | 119 | } else { |
| @@ -119,8 +143,21 @@ | ||
| 119 | 143 | |
| 120 | 144 | // Moderators can always edit |
| 121 | 145 | if ( user_can( $user_id, 'moderate' ) ) { |
| 122 | 146 | $caps = array( $cap ); |
| 147 | + | |
| 148 | + // Otherwise, check forum | |
| 149 | + } else { | |
| 150 | + $forum_id = bbp_get_forum_id(); | |
| 151 | + | |
| 152 | + // Moderators can always edit forum content | |
| 153 | + if ( user_can( $user_id, 'moderate', $forum_id ) ) { | |
| 154 | + $caps = array( 'spectate' ); | |
| 155 | + | |
| 156 | + // Fallback to do_not_allow | |
| 157 | + } else { | |
| 158 | + $caps = array( 'do_not_allow' ); | |
| 159 | + } | |
| 123 | 160 | } |
| 124 | 161 | |
| 125 | 162 | break; |
| 126 | 163 | |
| @@ -126,27 +163,60 @@ | ||
| 126 | 163 | |
| 127 | 164 | // Used everywhere |
| 128 | 165 | case 'edit_topic' : |
| 129 | 166 | |
| 130 | - // Get the post | |
| 167 | + // Bail if no post ID | |
| 168 | + if ( empty( $args[0] ) ) { | |
| 169 | + break; | |
| 170 | + } | |
| 171 | + | |
| 172 | + // Get the post. | |
| 131 | 173 | $_post = get_post( $args[0] ); |
| 132 | - if ( !empty( $_post ) ) { | |
| 174 | + if ( ! empty( $_post ) ) { | |
| 133 | 175 | |
| 134 | 176 | // Get caps for post type object |
| 135 | 177 | $post_type = get_post_type_object( $_post->post_type ); |
| 136 | - $caps = array(); | |
| 178 | + $forum_id = bbp_get_topic_forum_id( $_post->ID ); | |
| 137 | 179 | |
| 180 | + // Anonymous users cannot edit existing topics | |
| 181 | + if ( empty( $user_id ) ) { | |
| 182 | + $caps = array( 'do_not_allow' ); | |
| 183 | + | |
| 138 | 184 | // Add 'do_not_allow' cap if user is spam or deleted |
| 139 | - if ( bbp_is_user_inactive( $user_id ) ) { | |
| 140 | - $caps[] = 'do_not_allow'; | |
| 185 | + } elseif ( bbp_is_user_inactive( $user_id ) ) { | |
| 186 | + $caps = array( 'do_not_allow' ); | |
| 141 | 187 | |
| 142 | - // User is author so allow edit | |
| 143 | - } elseif ( (int) $user_id == (int) $_post->post_author ) { | |
| 144 | - $caps[] = $post_type->cap->edit_posts; | |
| 188 | + // User cannot edit a topic in a restricted forum they cannot read | |
| 189 | + } elseif ( bbp_is_forum_restricted_for_user( $forum_id, $user_id ) ) { | |
| 190 | + $caps = array( 'do_not_allow' ); | |
| 145 | 191 | |
| 192 | + // Moderators can always edit forum content | |
| 193 | + } elseif ( user_can( $user_id, 'moderate', $_post->ID ) ) { | |
| 194 | + $caps = array( 'spectate' ); | |
| 195 | + | |
| 196 | + // User is author so allow edit if not in admin | |
| 197 | + } elseif ( ! is_admin() && ( (int) $user_id === (int) $_post->post_author ) ) { | |
| 198 | + | |
| 199 | + // If merging or splitting... | |
| 200 | + if ( bbp_is_topic_merge() || bbp_is_topic_split() ) { | |
| 201 | + $caps = array( 'moderate' ); | |
| 202 | + | |
| 203 | + // If editing... | |
| 204 | + } elseif ( bbp_is_topic_edit() ) { | |
| 205 | + | |
| 206 | + // Only allow if not past the edit-lock period | |
| 207 | + $caps = ! bbp_past_edit_lock( $_post->post_date_gmt ) | |
| 208 | + ? array( $post_type->cap->edit_posts ) | |
| 209 | + : array( 'do_not_allow' ); | |
| 210 | + | |
| 211 | + // Otherwise... | |
| 212 | + } else { | |
| 213 | + $caps = array( $post_type->cap->edit_posts ); | |
| 214 | + } | |
| 215 | + | |
| 146 | 216 | // Unknown, so map to edit_others_posts |
| 147 | 217 | } else { |
| 148 | - $caps[] = $post_type->cap->edit_others_posts; | |
| 218 | + $caps = array( $post_type->cap->edit_others_posts ); | |
| 149 | 219 | } |
| 150 | 220 | } |
| 151 | 221 | |
| 152 | 222 | break; |
| @@ -154,27 +224,35 @@ | ||
| 154 | 224 | /** Deleting **********************************************************/ |
| 155 | 225 | |
| 156 | 226 | case 'delete_topic' : |
| 157 | 227 | |
| 158 | - // Get the post | |
| 228 | + // Bail if no post ID | |
| 229 | + if ( empty( $args[0] ) ) { | |
| 230 | + break; | |
| 231 | + } | |
| 232 | + | |
| 233 | + // Get the post. | |
| 159 | 234 | $_post = get_post( $args[0] ); |
| 160 | - if ( !empty( $_post ) ) { | |
| 235 | + if ( ! empty( $_post ) ) { | |
| 161 | 236 | |
| 162 | 237 | // Get caps for post type object |
| 163 | 238 | $post_type = get_post_type_object( $_post->post_type ); |
| 164 | - $caps = array(); | |
| 165 | 239 | |
| 166 | 240 | // Add 'do_not_allow' cap if user is spam or deleted |
| 167 | 241 | if ( bbp_is_user_inactive( $user_id ) ) { |
| 168 | - $caps[] = 'do_not_allow'; | |
| 242 | + $caps = array( 'do_not_allow' ); | |
| 169 | 243 | |
| 170 | 244 | // Moderators can always edit forum content |
| 171 | - } elseif ( user_can( $user_id, 'moderate' ) ) { | |
| 172 | - $caps[] = 'moderate'; | |
| 245 | + } elseif ( user_can( $user_id, 'moderate', $_post->ID ) ) { | |
| 246 | + $caps = array( 'spectate' ); | |
| 173 | 247 | |
| 248 | + // User is author so allow delete if not in admin | |
| 249 | + } elseif ( ! is_admin() && ( (int) $user_id === (int) $_post->post_author ) ) { | |
| 250 | + $caps = array( $post_type->cap->delete_posts ); | |
| 251 | + | |
| 174 | 252 | // Unknown so map to delete_others_posts |
| 175 | 253 | } else { |
| 176 | - $caps[] = $post_type->cap->delete_others_posts; | |
| 254 | + $caps = array( $post_type->cap->delete_others_posts ); | |
| 177 | 255 | } |
| 178 | 256 | } |
| 179 | 257 | |
| 180 | 258 | break; |
| @@ -192,25 +270,26 @@ | ||
| 192 | 270 | |
| 193 | 271 | /** Admin *************************************************************/ |
| 194 | 272 | |
| 195 | 273 | case 'bbp_topics_admin' : |
| 196 | - $caps = array( 'moderate' ); | |
| 274 | + $caps = array( 'edit_topics' ); | |
| 197 | 275 | break; |
| 198 | 276 | } |
| 199 | 277 | |
| 200 | - return apply_filters( 'bbp_map_topic_meta_caps', $caps, $cap, $user_id, $args ); | |
| 278 | + // Filter & return | |
| 279 | + return (array) apply_filters( 'bbp_map_topic_meta_caps', $caps, $cap, $user_id, $args ); | |
| 201 | 280 | } |
| 202 | 281 | |
| 203 | 282 | /** |
| 204 | 283 | * Maps topic tag capabilities |
| 205 | 284 | * |
| 206 | - * @since bbPress (r4242) | |
| 285 | + * @since 2.2.0 bbPress (r4242) | |
| 207 | 286 | * |
| 208 | 287 | * @param array $caps Capabilities for meta capability |
| 209 | 288 | * @param string $cap Capability name |
| 210 | 289 | * @param int $user_id User id |
| 211 | - * @param mixed $args Arguments | |
| 212 | - * @uses apply_filters() Filter capability map results | |
| 290 | + * @param array $args Arguments | |
| 291 | + * | |
| 213 | 292 | * @return array Actual capabilities for meta capability |
| 214 | 293 | */ |
| 215 | 294 | function bbp_map_topic_tag_meta_caps( $caps, $cap, $user_id, $args ) { |
| 216 | 295 | |
| @@ -215,12 +294,142 @@ | ||
| 215 | 294 | function bbp_map_topic_tag_meta_caps( $caps, $cap, $user_id, $args ) { |
| 216 | 295 | |
| 217 | 296 | // What capability is being checked? |
| 218 | 297 | switch ( $cap ) { |
| 219 | - case 'manage_topic_tags' : | |
| 220 | - case 'edit_topic_tags' : | |
| 221 | - case 'delete_topic_tags' : | |
| 222 | - case 'assign_topic_tags' : | |
| 298 | + | |
| 299 | + /** Assignment ********************************************************/ | |
| 300 | + | |
| 301 | + case 'assign_topic_tags' : | |
| 302 | + | |
| 303 | + // Get post | |
| 304 | + $post_id = ! empty( $args[0] ) | |
| 305 | + ? get_post( $args[0] )->ID | |
| 306 | + : 0; | |
| 307 | + | |
| 308 | + // Add 'do_not_allow' cap if user is spam or deleted | |
| 309 | + if ( bbp_is_user_inactive( $user_id ) ) { | |
| 310 | + $caps = array( 'do_not_allow' ); | |
| 311 | + | |
| 312 | + // Moderators can always assign | |
| 313 | + } elseif ( user_can( $user_id, 'moderate', $post_id ) ) { | |
| 314 | + $caps = array( 'moderate' ); | |
| 315 | + | |
| 316 | + // Do not allow if topic tags are disabled | |
| 317 | + } elseif ( ! bbp_allow_topic_tags() ) { | |
| 318 | + $caps = array( 'do_not_allow' ); | |
| 319 | + } | |
| 320 | + | |
| 321 | + break; | |
| 322 | + | |
| 323 | + case 'remove_topic_tag' : | |
| 324 | + | |
| 325 | + $topic_id = ! empty( $args[0] ) | |
| 326 | + ? bbp_get_topic_id( $args[0] ) | |
| 327 | + : 0; | |
| 328 | + $tag_id = ! empty( $args[1] ) | |
| 329 | + ? absint( $args[1] ) | |
| 330 | + : 0; | |
| 331 | + | |
| 332 | + // Do not allow invalid topic-tag relationships | |
| 333 | + if ( empty( $topic_id ) || empty( $tag_id ) || ! has_term( $tag_id, bbp_get_topic_tag_tax_id(), $topic_id ) ) { | |
| 334 | + $caps = array( 'do_not_allow' ); | |
| 335 | + | |
| 336 | + // Add 'do_not_allow' cap if user is spam or deleted | |
| 337 | + } elseif ( bbp_is_user_inactive( $user_id ) ) { | |
| 338 | + $caps = array( 'do_not_allow' ); | |
| 339 | + | |
| 340 | + // Moderators can always remove | |
| 341 | + } elseif ( user_can( $user_id, 'moderate', $topic_id ) ) { | |
| 342 | + $caps = array( 'moderate' ); | |
| 343 | + | |
| 344 | + // Fallback to assigning topic tags | |
| 345 | + } else { | |
| 346 | + $caps = array( 'assign_topic_tags' ); | |
| 347 | + } | |
| 348 | + | |
| 349 | + break; | |
| 350 | + | |
| 351 | + /** Management ********************************************************/ | |
| 352 | + | |
| 353 | + case 'manage_topic_tags' : | |
| 354 | + | |
| 355 | + // Moderators can always edit | |
| 356 | + if ( user_can( $user_id, 'moderate' ) ) { | |
| 357 | + $caps = array( 'moderate' ); | |
| 358 | + } | |
| 359 | + | |
| 360 | + break; | |
| 361 | + | |
| 362 | + /** Editing ***********************************************************/ | |
| 363 | + | |
| 364 | + case 'edit_topic_tags' : | |
| 365 | + | |
| 366 | + // Moderators can always edit | |
| 367 | + if ( user_can( $user_id, 'moderate' ) ) { | |
| 368 | + $caps = array( 'moderate' ); | |
| 369 | + } | |
| 370 | + | |
| 371 | + break; | |
| 372 | + | |
| 373 | + case 'edit_topic_tag' : | |
| 374 | + | |
| 375 | + // Get the term | |
| 376 | + $_tag = get_term( $args[0], bbp_get_topic_tag_tax_id() ); | |
| 377 | + if ( ! empty( $_tag ) ) { | |
| 378 | + | |
| 379 | + // Add 'do_not_allow' cap if user is spam or deleted | |
| 380 | + if ( bbp_is_user_inactive( $user_id ) ) { | |
| 381 | + $caps = array( 'do_not_allow' ); | |
| 382 | + | |
| 383 | + // Moderators can always edit topic tags | |
| 384 | + } elseif ( user_can( $user_id, 'moderate', $_tag->term_id ) ) { | |
| 385 | + $caps = array( 'spectate' ); | |
| 386 | + | |
| 387 | + // Fallback to edit_terms. | |
| 388 | + } else { | |
| 389 | + $taxonomy = get_taxonomy( bbp_get_topic_tag_tax_id() ); | |
| 390 | + $caps = array( $taxonomy->cap->edit_terms ); | |
| 391 | + } | |
| 392 | + } | |
| 393 | + | |
| 394 | + break; | |
| 395 | + | |
| 396 | + /** Deleting **********************************************************/ | |
| 397 | + | |
| 398 | + case 'delete_topic_tags' : | |
| 399 | + | |
| 400 | + // Moderators can always edit | |
| 401 | + if ( user_can( $user_id, 'moderate' ) ) { | |
| 402 | + $caps = array( 'moderate' ); | |
| 403 | + } | |
| 404 | + | |
| 405 | + break; | |
| 406 | + | |
| 407 | + case 'delete_topic_tag' : | |
| 408 | + | |
| 409 | + // Get the term | |
| 410 | + $_tag = get_term( $args[0], bbp_get_topic_tag_tax_id() ); | |
| 411 | + if ( ! empty( $_tag ) ) { | |
| 412 | + | |
| 413 | + // Add 'do_not_allow' cap if user is spam or deleted | |
| 414 | + if ( bbp_is_user_inactive( $user_id ) ) { | |
| 415 | + $caps = array( 'do_not_allow' ); | |
| 416 | + | |
| 417 | + // Moderators can always delete topic tags | |
| 418 | + } elseif ( user_can( $user_id, 'moderate', $_tag->term_id ) ) { | |
| 419 | + $caps = array( 'spectate' ); | |
| 420 | + | |
| 421 | + // Fallback to delete_terms. | |
| 422 | + } else { | |
| 423 | + $taxonomy = get_taxonomy( $_tag->post_type ); | |
| 424 | + $caps = array( $taxonomy->cap->delete_terms ); | |
| 425 | + } | |
| 426 | + } | |
| 427 | + | |
| 428 | + break; | |
| 429 | + | |
| 430 | + /** Admin *************************************************************/ | |
| 431 | + | |
| 223 | 432 | case 'bbp_topic_tags_admin' : |
| 224 | 433 | |
| 225 | 434 | // Moderators can always edit |
| 226 | 435 | if ( user_can( $user_id, 'moderate' ) ) { |
| @@ -225,8 +434,11 @@ | ||
| 225 | 434 | // Moderators can always edit |
| 226 | 435 | if ( user_can( $user_id, 'moderate' ) ) { |
| 227 | 436 | $caps = array( 'moderate' ); |
| 228 | 437 | } |
| 438 | + | |
| 439 | + break; | |
| 229 | 440 | } |
| 230 | 441 | |
| 231 | - return apply_filters( 'bbp_map_topic_tag_meta_caps', $caps, $cap, $user_id, $args ); | |
| 442 | + // Filter & return | |
| 443 | + return (array) apply_filters( 'bbp_map_topic_tag_meta_caps', $caps, $cap, $user_id, $args ); | |
| 232 | 444 | } |