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