| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* bbPress Capabilites |
| 5 |
* |
| 6 |
* @package bbPress |
| 7 |
* @subpackage Capabilities |
| 8 |
*/ |
| 9 |
|
| 10 |
// Redirect if accessed directly |
| 11 |
if ( !defined( 'ABSPATH' ) ) exit; |
| 12 |
|
| 13 |
/** |
| 14 |
* Adds bbPress-specific user roles. |
| 15 |
* |
| 16 |
* This is called on plugin activation. |
| 17 |
* |
| 18 |
* @since bbPress (r2741) |
| 19 |
* |
| 20 |
* @uses get_option() To get the default role |
| 21 |
* @uses get_role() To get the default role object |
| 22 |
* @uses add_role() To add our own roles |
| 23 |
* @uses do_action() Calls 'bbp_add_roles' |
| 24 |
*/ |
| 25 |
function bbp_add_roles() { |
| 26 |
|
| 27 |
// Add the Moderator role and add the default role caps. Mod caps are added by the bbp_add_caps () function |
| 28 |
$default = get_role( get_option( 'default_role' ) ); |
| 29 |
|
| 30 |
// Moderators are default role + forum moderating caps in bbp_add_caps() |
| 31 |
add_role( 'bbp_moderator', __( 'Forum Moderator', 'bbpress' ), $default->capabilities ); |
| 32 |
|
| 33 |
do_action( 'bbp_add_roles' ); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Adds capabilities to WordPress user roles. |
| 38 |
* |
| 39 |
* This is called on plugin activation. |
| 40 |
* |
| 41 |
* @since bbPress (r2608) |
| 42 |
* |
| 43 |
* @uses get_role() To get the administrator, default and moderator roles |
| 44 |
* @uses WP_Role::add_cap() To add various capabilities |
| 45 |
* @uses do_action() Calls 'bbp_add_caps' |
| 46 |
*/ |
| 47 |
function bbp_add_caps() { |
| 48 |
global $wp_roles; |
| 49 |
|
| 50 |
// Loop through available roles |
| 51 |
foreach( $wp_roles->roles as $role => $details ) { |
| 52 |
|
| 53 |
// Load this role |
| 54 |
$this_role = get_role( $role ); |
| 55 |
|
| 56 |
// Loop through caps for this role and remove them |
| 57 |
foreach ( bbp_get_caps_for_role( $role ) as $cap ) { |
| 58 |
$this_role->add_cap( $cap ); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
do_action( 'bbp_add_caps' ); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Removes capabilities from WordPress user roles. |
| 67 |
* |
| 68 |
* This is called on plugin deactivation. |
| 69 |
* |
| 70 |
* @since bbPress (r2608) |
| 71 |
* |
| 72 |
* @uses get_role() To get the administrator and default roles |
| 73 |
* @uses WP_Role::remove_cap() To remove various capabilities |
| 74 |
* @uses do_action() Calls 'bbp_remove_caps' |
| 75 |
*/ |
| 76 |
function bbp_remove_caps() { |
| 77 |
global $wp_roles; |
| 78 |
|
| 79 |
// Loop through available roles |
| 80 |
foreach( $wp_roles->roles as $role => $details ) { |
| 81 |
|
| 82 |
// Load this role |
| 83 |
$this_role = get_role( $role ); |
| 84 |
|
| 85 |
// Loop through caps for this role and remove them |
| 86 |
foreach ( bbp_get_caps_for_role( $role ) as $cap ) { |
| 87 |
$this_role->remove_cap( $cap ); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
do_action( 'bbp_remove_caps' ); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Removes bbPress-specific user roles. |
| 96 |
* |
| 97 |
* This is called on plugin deactivation. |
| 98 |
* |
| 99 |
* @since bbPress (r2741) |
| 100 |
* |
| 101 |
* @uses remove_role() To remove our roles |
| 102 |
* @uses do_action() Calls 'bbp_remove_roles' |
| 103 |
*/ |
| 104 |
function bbp_remove_roles() { |
| 105 |
|
| 106 |
// Remove the Moderator role |
| 107 |
remove_role( 'bbp_moderator' ); |
| 108 |
|
| 109 |
do_action( 'bbp_remove_roles' ); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Maps forum/topic/reply caps to built in WordPress caps |
| 114 |
* |
| 115 |
* @since bbPress (r2593) |
| 116 |
* |
| 117 |
* @param array $caps Capabilities for meta capability |
| 118 |
* @param string $cap Capability name |
| 119 |
* @param int $user_id User id |
| 120 |
* @param mixed $args Arguments |
| 121 |
* @uses get_post() To get the post |
| 122 |
* @uses get_post_type_object() To get the post type object |
| 123 |
* @uses apply_filters() Calls 'bbp_map_meta_caps' with caps, cap, user id and |
| 124 |
* args |
| 125 |
* @return array Actual capabilities for meta capability |
| 126 |
*/ |
| 127 |
function bbp_map_meta_caps( $caps, $cap, $user_id, $args ) { |
| 128 |
|
| 129 |
switch ( $cap ) { |
| 130 |
|
| 131 |
// Reading |
| 132 |
case 'read_forum' : |
| 133 |
case 'read_topic' : |
| 134 |
case 'read_reply' : |
| 135 |
|
| 136 |
if ( $post = get_post( $args[0] ) ) { |
| 137 |
$caps = array(); |
| 138 |
$post_type = get_post_type_object( $post->post_type ); |
| 139 |
|
| 140 |
if ( 'private' != $post->post_status ) |
| 141 |
$caps[] = 'read'; |
| 142 |
elseif ( (int) $user_id == (int) $post->post_author ) |
| 143 |
$caps[] = 'read'; |
| 144 |
else |
| 145 |
$caps[] = $post_type->cap->read_private_posts; |
| 146 |
} |
| 147 |
|
| 148 |
break; |
| 149 |
|
| 150 |
// Editing |
| 151 |
case 'edit_forum' : |
| 152 |
case 'edit_topic' : |
| 153 |
case 'edit_reply' : |
| 154 |
|
| 155 |
if ( $post = get_post( $args[0] ) ) { |
| 156 |
$caps = array(); |
| 157 |
$post_type = get_post_type_object( $post->post_type ); |
| 158 |
|
| 159 |
if ( (int) $user_id == (int) $post->post_author ) |
| 160 |
$caps[] = $post_type->cap->edit_posts; |
| 161 |
else |
| 162 |
$caps[] = $post_type->cap->edit_others_posts; |
| 163 |
} |
| 164 |
|
| 165 |
break; |
| 166 |
|
| 167 |
// Deleting |
| 168 |
case 'delete_forum' : |
| 169 |
|
| 170 |
if ( $post = get_post( $args[0] ) ) { |
| 171 |
$caps = array(); |
| 172 |
$post_type = get_post_type_object( $post->post_type ); |
| 173 |
|
| 174 |
if ( (int) $user_id == (int) $post->post_author ) |
| 175 |
$caps[] = $post_type->cap->delete_posts; |
| 176 |
else |
| 177 |
$caps[] = $post_type->cap->delete_others_posts; |
| 178 |
} |
| 179 |
|
| 180 |
break; |
| 181 |
|
| 182 |
case 'delete_topic' : |
| 183 |
case 'delete_reply' : |
| 184 |
|
| 185 |
if ( $post = get_post( $args[0] ) ) { |
| 186 |
$caps = array(); |
| 187 |
$post_type = get_post_type_object( $post->post_type ); |
| 188 |
$caps[] = $post_type->cap->delete_others_posts; |
| 189 |
} |
| 190 |
|
| 191 |
break; |
| 192 |
} |
| 193 |
|
| 194 |
return apply_filters( 'bbp_map_meta_caps', $caps, $cap, $user_id, $args ); |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Return forum capabilities |
| 199 |
* |
| 200 |
* @since bbPress (r2593) |
| 201 |
* |
| 202 |
* @uses apply_filters() Calls 'bbp_get_forum_caps' with the capabilities |
| 203 |
* @return array Forum capabilities |
| 204 |
*/ |
| 205 |
function bbp_get_forum_caps() { |
| 206 |
|
| 207 |
// Forum meta caps |
| 208 |
$caps = array ( |
| 209 |
'delete_posts' => 'delete_forums', |
| 210 |
'delete_others_posts' => 'delete_others_forums' |
| 211 |
); |
| 212 |
|
| 213 |
return apply_filters( 'bbp_get_forum_caps', $caps ); |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Return topic capabilities |
| 218 |
* |
| 219 |
* @since bbPress (r2593) |
| 220 |
* |
| 221 |
* @uses apply_filters() Calls 'bbp_get_topic_caps' with the capabilities |
| 222 |
* @return array Topic capabilities |
| 223 |
*/ |
| 224 |
function bbp_get_topic_caps() { |
| 225 |
|
| 226 |
// Topic meta caps |
| 227 |
$caps = array ( |
| 228 |
'delete_posts' => 'delete_topics', |
| 229 |
'delete_others_posts' => 'delete_others_topics' |
| 230 |
); |
| 231 |
|
| 232 |
return apply_filters( 'bbp_get_topic_caps', $caps ); |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Return reply capabilities |
| 237 |
* |
| 238 |
* @since bbPress (r2593) |
| 239 |
* |
| 240 |
* @uses apply_filters() Calls 'bbp_get_reply_caps' with the capabilities |
| 241 |
* @return array Reply capabilities |
| 242 |
*/ |
| 243 |
function bbp_get_reply_caps () { |
| 244 |
|
| 245 |
// Reply meta caps |
| 246 |
$caps = array ( |
| 247 |
'edit_posts' => 'edit_replies', |
| 248 |
'edit_others_posts' => 'edit_others_replies', |
| 249 |
'publish_posts' => 'publish_replies', |
| 250 |
'read_private_posts' => 'read_private_replies', |
| 251 |
'delete_posts' => 'delete_replies', |
| 252 |
'delete_others_posts' => 'delete_others_replies' |
| 253 |
); |
| 254 |
|
| 255 |
return apply_filters( 'bbp_get_reply_caps', $caps ); |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* Return topic tag capabilities |
| 260 |
* |
| 261 |
* @since bbPress (r2593) |
| 262 |
* |
| 263 |
* @uses apply_filters() Calls 'bbp_get_topic_tag_caps' with the capabilities |
| 264 |
* @return array Topic tag capabilities |
| 265 |
*/ |
| 266 |
function bbp_get_topic_tag_caps () { |
| 267 |
|
| 268 |
// Topic tag meta caps |
| 269 |
$caps = array ( |
| 270 |
'manage_terms' => 'manage_topic_tags', |
| 271 |
'edit_terms' => 'edit_topic_tags', |
| 272 |
'delete_terms' => 'delete_topic_tags', |
| 273 |
'assign_terms' => 'assign_topic_tags' |
| 274 |
); |
| 275 |
|
| 276 |
return apply_filters( 'bbp_get_topic_tag_caps', $caps ); |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
/** |
| 281 |
* Returns an array of capabilities based on the role that is being requested. |
| 282 |
* |
| 283 |
* @since bbPress (r2994) |
| 284 |
* |
| 285 |
* @param string $role Optional. Defaults to The role to load caps for |
| 286 |
* @uses apply_filters() Allow return value to be filtered |
| 287 |
* |
| 288 |
* @return array Capabilities for $role |
| 289 |
*/ |
| 290 |
function bbp_get_caps_for_role( $role = '' ) { |
| 291 |
|
| 292 |
// Which role are we looking for? |
| 293 |
switch ( $role ) { |
| 294 |
|
| 295 |
// Administrator |
| 296 |
case 'administrator' : |
| 297 |
|
| 298 |
$caps = array( |
| 299 |
|
| 300 |
// Forum caps |
| 301 |
'publish_forums', |
| 302 |
'edit_forums', |
| 303 |
'edit_others_forums', |
| 304 |
'delete_forums', |
| 305 |
'delete_others_forums', |
| 306 |
'read_private_forums', |
| 307 |
'read_hidden_forums', |
| 308 |
|
| 309 |
// Topic caps |
| 310 |
'publish_topics', |
| 311 |
'edit_topics', |
| 312 |
'edit_others_topics', |
| 313 |
'delete_topics', |
| 314 |
'delete_others_topics', |
| 315 |
'read_private_topics', |
| 316 |
|
| 317 |
// Reply caps |
| 318 |
'publish_replies', |
| 319 |
'edit_replies', |
| 320 |
'edit_others_replies', |
| 321 |
'delete_replies', |
| 322 |
'delete_others_replies', |
| 323 |
'read_private_replies', |
| 324 |
|
| 325 |
// Topic tag caps |
| 326 |
'manage_topic_tags', |
| 327 |
'edit_topic_tags', |
| 328 |
'delete_topic_tags', |
| 329 |
'assign_topic_tags', |
| 330 |
|
| 331 |
// Misc |
| 332 |
'moderate', |
| 333 |
'throttle', |
| 334 |
'view_trash' |
| 335 |
); |
| 336 |
|
| 337 |
break; |
| 338 |
|
| 339 |
// Moderator |
| 340 |
case 'bbp_moderator' : |
| 341 |
|
| 342 |
$caps = array( |
| 343 |
|
| 344 |
// Forum caps |
| 345 |
'read_private_forums', |
| 346 |
'read_hidden_forums', |
| 347 |
|
| 348 |
// Topic caps |
| 349 |
'publish_topics', |
| 350 |
'edit_topics', |
| 351 |
'edit_others_topics', |
| 352 |
'delete_topics', |
| 353 |
'delete_others_topics', |
| 354 |
'read_private_topics', |
| 355 |
|
| 356 |
// Reply caps |
| 357 |
'publish_replies', |
| 358 |
'edit_replies', |
| 359 |
'edit_others_replies', |
| 360 |
'delete_replies', |
| 361 |
'delete_others_replies', |
| 362 |
'read_private_replies', |
| 363 |
|
| 364 |
// Topic tag caps |
| 365 |
'manage_topic_tags', |
| 366 |
'edit_topic_tags', |
| 367 |
'delete_topic_tags', |
| 368 |
'assign_topic_tags', |
| 369 |
|
| 370 |
// Misc |
| 371 |
'moderate', |
| 372 |
'throttle', |
| 373 |
'view_trash', |
| 374 |
); |
| 375 |
|
| 376 |
break; |
| 377 |
|
| 378 |
// Other specific roles |
| 379 |
case 'editor' : |
| 380 |
case 'author' : |
| 381 |
case 'contributor' : |
| 382 |
case 'subscriber' : |
| 383 |
default : |
| 384 |
|
| 385 |
$caps = array( |
| 386 |
|
| 387 |
// Topic caps |
| 388 |
'publish_topics', |
| 389 |
'edit_topics', |
| 390 |
|
| 391 |
// Reply caps |
| 392 |
'publish_replies', |
| 393 |
'edit_replies', |
| 394 |
|
| 395 |
// Topic tag caps |
| 396 |
'assign_topic_tags', |
| 397 |
|
| 398 |
); |
| 399 |
|
| 400 |
break; |
| 401 |
} |
| 402 |
|
| 403 |
return apply_filters( 'bbp_get_caps_for_role', $caps, $role ); |
| 404 |
} |
| 405 |
|
| 406 |
?> |
| 407 |
|