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