| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* bbPress Reply Capabilities. |
| 5 |
* |
| 6 |
* Used to map reply capabilities to WordPress's existing capabilities. |
| 7 |
* |
| 8 |
* @package bbPress |
| 9 |
* @subpackage Capabilities |
| 10 |
*/ |
| 11 |
|
| 12 |
/** |
| 13 |
* Return reply capabilities. |
| 14 |
* |
| 15 |
* @since 2.0.0 bbPress (r2593) |
| 16 |
* |
| 17 |
* @return array Reply capabilities. |
| 18 |
*/ |
| 19 |
function bbp_get_reply_caps() { |
| 20 |
|
| 21 |
/** |
| 22 |
* Filters the reply capabilities. |
| 23 |
* |
| 24 |
* @since 2.0.0 |
| 25 |
* |
| 26 |
* @param array $caps An array of reply capabilities with keys mapping to WordPress capabilities. |
| 27 |
*/ |
| 28 |
return (array) apply_filters( |
| 29 |
'bbp_get_reply_caps', |
| 30 |
array( |
| 31 |
'edit_posts' => 'edit_replies', |
| 32 |
'edit_others_posts' => 'edit_others_replies', |
| 33 |
'publish_posts' => 'publish_replies', |
| 34 |
'read_private_posts' => 'read_private_replies', |
| 35 |
'delete_posts' => 'delete_replies', |
| 36 |
'delete_others_posts' => 'delete_others_replies' |
| 37 |
) |
| 38 |
); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Maps topic capabilities. |
| 43 |
* |
| 44 |
* @since 2.2.0 bbPress (r4242) |
| 45 |
* |
| 46 |
* @param array $caps Capabilities for meta capability. |
| 47 |
* @param string $cap Capability name. |
| 48 |
* @param int $user_id User id. |
| 49 |
* @param array $args Arguments. |
| 50 |
* |
| 51 |
* @return array Actual capabilities for meta capability. |
| 52 |
*/ |
| 53 |
function bbp_map_reply_meta_caps( $caps = array(), $cap = '', $user_id = 0, $args = array() ) { |
| 54 |
|
| 55 |
// What capability is being checked? |
| 56 |
switch ( $cap ) { |
| 57 |
|
| 58 |
/** Reading ***********************************************************/ |
| 59 |
|
| 60 |
case 'read_reply' : |
| 61 |
|
| 62 |
// User cannot spectate |
| 63 |
if ( ! user_can( $user_id, 'spectate' ) && ! bbp_is_anonymous() ) { |
| 64 |
$caps = array( 'do_not_allow' ); |
| 65 |
|
| 66 |
// Do some post ID based logic |
| 67 |
} else { |
| 68 |
|
| 69 |
// Bail if no post ID |
| 70 |
if ( empty( $args[0] ) ) { |
| 71 |
break; |
| 72 |
} |
| 73 |
|
| 74 |
// Get the post. |
| 75 |
$_post = get_post( $args[0] ); |
| 76 |
if ( ! empty( $_post ) ) { |
| 77 |
|
| 78 |
// Get post type object |
| 79 |
$post_type = get_post_type_object( $_post->post_type ); |
| 80 |
|
| 81 |
// Post is public |
| 82 |
if ( bbp_get_public_status_id() === $_post->post_status ) { |
| 83 |
|
| 84 |
// Anonymous users do not have caps, but can 'exist' |
| 85 |
if ( bbp_is_anonymous() ) { |
| 86 |
$caps = array( 'exist' ); |
| 87 |
|
| 88 |
// Registered users need the 'spectate' cap |
| 89 |
} else { |
| 90 |
$caps = array( 'spectate' ); |
| 91 |
} |
| 92 |
|
| 93 |
// User is author so allow read |
| 94 |
} elseif ( (int) $user_id === (int) $_post->post_author ) { |
| 95 |
$caps = array( 'spectate' ); |
| 96 |
|
| 97 |
// Moderators can always edit forum content |
| 98 |
} elseif ( user_can( $user_id, 'moderate', $_post->ID ) ) { |
| 99 |
$caps = array( 'spectate' ); |
| 100 |
|
| 101 |
// Unknown so map to private posts |
| 102 |
} else { |
| 103 |
$caps = array( $post_type->cap->read_private_posts ); |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
break; |
| 109 |
|
| 110 |
/** Publishing ********************************************************/ |
| 111 |
|
| 112 |
case 'publish_replies' : |
| 113 |
|
| 114 |
// Moderators can always publish |
| 115 |
if ( user_can( $user_id, 'moderate' ) ) { |
| 116 |
$caps = array( 'moderate' ); |
| 117 |
} |
| 118 |
|
| 119 |
break; |
| 120 |
|
| 121 |
/** Editing ***********************************************************/ |
| 122 |
|
| 123 |
// Used primarily in wp-admin |
| 124 |
case 'edit_replies' : |
| 125 |
case 'edit_others_replies' : |
| 126 |
|
| 127 |
// Moderators can always edit |
| 128 |
if ( user_can( $user_id, 'moderate' ) ) { |
| 129 |
$caps = array( 'moderate' ); |
| 130 |
|
| 131 |
// Otherwise, check forum |
| 132 |
} else { |
| 133 |
$forum_id = bbp_get_forum_id(); |
| 134 |
|
| 135 |
// Moderators can always edit forum content |
| 136 |
if ( user_can( $user_id, 'moderate', $forum_id ) ) { |
| 137 |
$caps = array( 'spectate' ); |
| 138 |
|
| 139 |
// Fallback to do_not_allow |
| 140 |
} else { |
| 141 |
$caps = array( 'do_not_allow' ); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
break; |
| 146 |
|
| 147 |
// Used everywhere |
| 148 |
case 'edit_reply' : |
| 149 |
|
| 150 |
// Bail if no post ID |
| 151 |
if ( empty( $args[0] ) ) { |
| 152 |
break; |
| 153 |
} |
| 154 |
|
| 155 |
// Get the post. |
| 156 |
$_post = get_post( $args[0] ); |
| 157 |
if ( ! empty( $_post ) ) { |
| 158 |
|
| 159 |
// Get post type object |
| 160 |
$post_type = get_post_type_object( $_post->post_type ); |
| 161 |
|
| 162 |
// Anonymous users cannot edit existing replies |
| 163 |
if ( empty( $user_id ) ) { |
| 164 |
$caps = array( 'do_not_allow' ); |
| 165 |
|
| 166 |
// Add 'do_not_allow' cap if user is spam or deleted |
| 167 |
} elseif ( bbp_is_user_inactive( $user_id ) ) { |
| 168 |
$caps = array( 'do_not_allow' ); |
| 169 |
|
| 170 |
// Moderators can always edit forum content |
| 171 |
} elseif ( user_can( $user_id, 'moderate', $_post->ID ) ) { |
| 172 |
$caps = array( 'spectate' ); |
| 173 |
|
| 174 |
// Allow author or mod to edit if not in admin, unless past edit lock time |
| 175 |
} elseif ( ! is_admin() && ( (int) $user_id === (int) $_post->post_author ) ) { |
| 176 |
|
| 177 |
// If editing... |
| 178 |
if ( bbp_is_reply_edit() ) { |
| 179 |
|
| 180 |
// Only allow if not past the edit-lock period |
| 181 |
$caps = ! bbp_past_edit_lock( $_post->post_date_gmt ) |
| 182 |
? array( $post_type->cap->edit_posts ) |
| 183 |
: array( 'do_not_allow' ); |
| 184 |
|
| 185 |
// Otherwise... |
| 186 |
} else { |
| 187 |
$caps = array( $post_type->cap->edit_posts ); |
| 188 |
} |
| 189 |
|
| 190 |
// Fallback to edit_others_posts. |
| 191 |
} else { |
| 192 |
$caps = array( $post_type->cap->edit_others_posts ); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
break; |
| 197 |
|
| 198 |
/** Deleting **********************************************************/ |
| 199 |
|
| 200 |
case 'delete_reply' : |
| 201 |
|
| 202 |
// Bail if no post ID |
| 203 |
if ( empty( $args[0] ) ) { |
| 204 |
break; |
| 205 |
} |
| 206 |
|
| 207 |
// Get the post |
| 208 |
$_post = get_post( $args[0] ); |
| 209 |
if ( ! empty( $_post ) ) { |
| 210 |
|
| 211 |
// Get post type object |
| 212 |
$post_type = get_post_type_object( $_post->post_type ); |
| 213 |
|
| 214 |
// Add 'do_not_allow' cap if user is spam or deleted |
| 215 |
if ( bbp_is_user_inactive( $user_id ) ) { |
| 216 |
$caps = array( 'do_not_allow' ); |
| 217 |
|
| 218 |
// Moderators can always edit forum content |
| 219 |
} elseif ( user_can( $user_id, 'moderate', $_post->ID ) ) { |
| 220 |
$caps = array( 'spectate' ); |
| 221 |
|
| 222 |
// User is author so allow delete if not in admin |
| 223 |
} elseif ( ! is_admin() && ( (int) $user_id === (int) $_post->post_author ) ) { |
| 224 |
$caps = array( $post_type->cap->delete_posts ); |
| 225 |
|
| 226 |
// Unknown so map to delete_others_posts |
| 227 |
} else { |
| 228 |
$caps = array( $post_type->cap->delete_others_posts ); |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
break; |
| 233 |
|
| 234 |
// Moderation override |
| 235 |
case 'delete_replies' : |
| 236 |
case 'delete_others_replies' : |
| 237 |
|
| 238 |
// Moderators can always delete |
| 239 |
if ( user_can( $user_id, 'moderate' ) ) { |
| 240 |
$caps = array( 'moderate' ); |
| 241 |
} |
| 242 |
|
| 243 |
break; |
| 244 |
|
| 245 |
/** Admin *************************************************************/ |
| 246 |
|
| 247 |
case 'bbp_replies_admin' : |
| 248 |
$caps = array( 'edit_replies' ); |
| 249 |
break; |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* Filters the Reply meta capabilities. |
| 254 |
* |
| 255 |
* @since 2.2.0 bbPress (r4242) |
| 256 |
* |
| 257 |
* @param array $caps The current capabilities being checked. |
| 258 |
* @param string $cap The capability being checked. |
| 259 |
* @param int $user_id The ID of the user whose capabilities are being checked. |
| 260 |
* @param array $args Additional arguments provided for the capability check. |
| 261 |
*/ |
| 262 |
return (array) apply_filters( 'bbp_map_reply_meta_caps', $caps, $cap, $user_id, $args ); |
| 263 |
} |
| 264 |
|