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