| @@ -11,36 +11,44 @@ | ||
| 11 | 11 | |
| 12 | 12 | /** |
| 13 | 13 | * Return reply capabilities |
| 14 | 14 | * |
| 15 | - * @since bbPress (r2593) | |
| 15 | + * @since 2.0.0 bbPress (r2593) | |
| 16 | 16 | * |
| 17 | - * @uses apply_filters() Calls 'bbp_get_reply_caps' with the capabilities | |
| 18 | 17 | * @return array Reply capabilities |
| 19 | 18 | */ |
| 20 | 19 | function bbp_get_reply_caps() { |
| 21 | - return apply_filters( 'bbp_get_reply_caps', array ( | |
| 22 | - 'edit_posts' => 'edit_replies', | |
| 23 | - 'edit_others_posts' => 'edit_others_replies', | |
| 24 | - 'publish_posts' => 'publish_replies', | |
| 25 | - 'read_private_posts' => 'read_private_replies', | |
| 26 | - 'delete_posts' => 'delete_replies', | |
| 27 | - 'delete_others_posts' => 'delete_others_replies' | |
| 28 | - ) ); | |
| 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 | + ); | |
| 29 | 39 | } |
| 30 | 40 | |
| 31 | 41 | /** |
| 32 | 42 | * Maps topic capabilities |
| 33 | 43 | * |
| 34 | - * @since bbPress (r4242) | |
| 44 | + * @since 2.2.0 bbPress (r4242) | |
| 35 | 45 | * |
| 36 | - * @param array $caps Capabilities for meta capability | |
| 37 | - * @param string $cap Capability name | |
| 38 | - * @param int $user_id User id | |
| 39 | - * @param mixed $args Arguments | |
| 40 | - * @uses get_post() To get the post | |
| 41 | - * @uses get_post_type_object() To get the post type object | |
| 42 | - * @uses apply_filters() Filter mapped results | |
| 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 | + * | |
| 43 | 51 | * @return array Actual capabilities for meta capability |
| 44 | 52 | */ |
| 45 | 53 | function bbp_map_reply_meta_caps( $caps = array(), $cap = '', $user_id = 0, $args = array() ) { |
| 46 | 54 | |
| @@ -51,27 +59,44 @@ | ||
| 51 | 59 | |
| 52 | 60 | case 'read_reply' : |
| 53 | 61 | |
| 54 | 62 | // User cannot spectate |
| 55 | - if ( ! user_can( $user_id, 'spectate' ) ) { | |
| 63 | + if ( ! user_can( $user_id, 'spectate' ) && ! bbp_is_anonymous() ) { | |
| 56 | 64 | $caps = array( 'do_not_allow' ); |
| 57 | 65 | |
| 58 | 66 | // Do some post ID based logic |
| 59 | 67 | } else { |
| 60 | 68 | |
| 61 | - // Get the post | |
| 69 | + // Bail if no post ID | |
| 70 | + if ( empty( $args[0] ) ) { | |
| 71 | + break; | |
| 72 | + } | |
| 73 | + | |
| 74 | + // Get the post. | |
| 62 | 75 | $_post = get_post( $args[0] ); |
| 63 | - if ( !empty( $_post ) ) { | |
| 76 | + if ( ! empty( $_post ) ) { | |
| 64 | 77 | |
| 65 | - // Get caps for post type object | |
| 78 | + // Get post type object | |
| 66 | 79 | $post_type = get_post_type_object( $_post->post_type ); |
| 67 | 80 | |
| 68 | 81 | // Post is public |
| 69 | - if ( bbp_get_public_status_id() == $_post->post_status ) { | |
| 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 ) { | |
| 70 | 95 | $caps = array( 'spectate' ); |
| 71 | 96 | |
| 72 | - // User is author so allow read | |
| 73 | - } elseif ( (int) $user_id == (int) $_post->post_author ) { | |
| 97 | + // Moderators can always edit forum content | |
| 98 | + } elseif ( user_can( $user_id, 'moderate', $_post->ID ) ) { | |
| 74 | 99 | $caps = array( 'spectate' ); |
| 75 | 100 | |
| 76 | 101 | // Unknown so map to private posts |
| 77 | 102 | } else { |
| @@ -101,8 +126,21 @@ | ||
| 101 | 126 | |
| 102 | 127 | // Moderators can always edit |
| 103 | 128 | if ( user_can( $user_id, 'moderate' ) ) { |
| 104 | 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 | + } | |
| 105 | 143 | } |
| 106 | 144 | |
| 107 | 145 | break; |
| 108 | 146 | |
| @@ -108,27 +146,56 @@ | ||
| 108 | 146 | |
| 109 | 147 | // Used everywhere |
| 110 | 148 | case 'edit_reply' : |
| 111 | 149 | |
| 112 | - // Get the post | |
| 150 | + // Bail if no post ID | |
| 151 | + if ( empty( $args[0] ) ) { | |
| 152 | + break; | |
| 153 | + } | |
| 154 | + | |
| 155 | + // Get the post. | |
| 113 | 156 | $_post = get_post( $args[0] ); |
| 114 | - if ( !empty( $_post ) ) { | |
| 157 | + if ( ! empty( $_post ) ) { | |
| 115 | 158 | |
| 116 | - // Get caps for post type object | |
| 159 | + // Get post type object | |
| 117 | 160 | $post_type = get_post_type_object( $_post->post_type ); |
| 118 | - $caps = array(); | |
| 161 | + $forum_id = bbp_get_reply_forum_id( $_post->ID ); | |
| 119 | 162 | |
| 163 | + // Anonymous users cannot edit existing replies | |
| 164 | + if ( empty( $user_id ) ) { | |
| 165 | + $caps = array( 'do_not_allow' ); | |
| 166 | + | |
| 120 | 167 | // Add 'do_not_allow' cap if user is spam or deleted |
| 121 | - if ( bbp_is_user_inactive( $user_id ) ) { | |
| 122 | - $caps[] = 'do_not_allow'; | |
| 168 | + } elseif ( bbp_is_user_inactive( $user_id ) ) { | |
| 169 | + $caps = array( 'do_not_allow' ); | |
| 123 | 170 | |
| 124 | - // User is author so allow edit | |
| 125 | - } elseif ( (int) $user_id == (int) $_post->post_author ) { | |
| 126 | - $caps[] = $post_type->cap->edit_posts; | |
| 171 | + // User cannot edit a reply in a restricted forum they cannot read | |
| 172 | + } elseif ( bbp_is_forum_restricted_for_user( $forum_id, $user_id ) ) { | |
| 173 | + $caps = array( 'do_not_allow' ); | |
| 127 | 174 | |
| 128 | - // Unknown, so map to edit_others_posts | |
| 175 | + // Moderators can always edit forum content | |
| 176 | + } elseif ( user_can( $user_id, 'moderate', $_post->ID ) ) { | |
| 177 | + $caps = array( 'spectate' ); | |
| 178 | + | |
| 179 | + // Allow author or mod to edit if not in admin, unless past edit lock time | |
| 180 | + } elseif ( ! is_admin() && ( (int) $user_id === (int) $_post->post_author ) ) { | |
| 181 | + | |
| 182 | + // If editing... | |
| 183 | + if ( bbp_is_reply_edit() ) { | |
| 184 | + | |
| 185 | + // Only allow if not past the edit-lock period | |
| 186 | + $caps = ! bbp_past_edit_lock( $_post->post_date_gmt ) | |
| 187 | + ? array( $post_type->cap->edit_posts ) | |
| 188 | + : array( 'do_not_allow' ); | |
| 189 | + | |
| 190 | + // Otherwise... | |
| 191 | + } else { | |
| 192 | + $caps = array( $post_type->cap->edit_posts ); | |
| 193 | + } | |
| 194 | + | |
| 195 | + // Fallback to edit_others_posts. | |
| 129 | 196 | } else { |
| 130 | - $caps[] = $post_type->cap->edit_others_posts; | |
| 197 | + $caps = array( $post_type->cap->edit_others_posts ); | |
| 131 | 198 | } |
| 132 | 199 | } |
| 133 | 200 | |
| 134 | 201 | break; |
| @@ -136,27 +203,35 @@ | ||
| 136 | 203 | /** Deleting **********************************************************/ |
| 137 | 204 | |
| 138 | 205 | case 'delete_reply' : |
| 139 | 206 | |
| 207 | + // Bail if no post ID | |
| 208 | + if ( empty( $args[0] ) ) { | |
| 209 | + break; | |
| 210 | + } | |
| 211 | + | |
| 140 | 212 | // Get the post |
| 141 | 213 | $_post = get_post( $args[0] ); |
| 142 | - if ( !empty( $_post ) ) { | |
| 214 | + if ( ! empty( $_post ) ) { | |
| 143 | 215 | |
| 144 | - // Get caps for post type object | |
| 216 | + // Get post type object | |
| 145 | 217 | $post_type = get_post_type_object( $_post->post_type ); |
| 146 | - $caps = array(); | |
| 147 | 218 | |
| 148 | 219 | // Add 'do_not_allow' cap if user is spam or deleted |
| 149 | 220 | if ( bbp_is_user_inactive( $user_id ) ) { |
| 150 | - $caps[] = 'do_not_allow'; | |
| 221 | + $caps = array( 'do_not_allow' ); | |
| 151 | 222 | |
| 152 | 223 | // Moderators can always edit forum content |
| 153 | - } elseif ( user_can( $user_id, 'moderate' ) ) { | |
| 154 | - $caps[] = 'moderate'; | |
| 224 | + } elseif ( user_can( $user_id, 'moderate', $_post->ID ) ) { | |
| 225 | + $caps = array( 'spectate' ); | |
| 155 | 226 | |
| 227 | + // User is author so allow delete if not in admin | |
| 228 | + } elseif ( ! is_admin() && ( (int) $user_id === (int) $_post->post_author ) ) { | |
| 229 | + $caps = array( $post_type->cap->delete_posts ); | |
| 230 | + | |
| 156 | 231 | // Unknown so map to delete_others_posts |
| 157 | 232 | } else { |
| 158 | - $caps[] = $post_type->cap->delete_others_posts; | |
| 233 | + $caps = array( $post_type->cap->delete_others_posts ); | |
| 159 | 234 | } |
| 160 | 235 | } |
| 161 | 236 | |
| 162 | 237 | break; |
| @@ -174,10 +249,11 @@ | ||
| 174 | 249 | |
| 175 | 250 | /** Admin *************************************************************/ |
| 176 | 251 | |
| 177 | 252 | case 'bbp_replies_admin' : |
| 178 | - $caps = array( 'moderate' ); | |
| 253 | + $caps = array( 'edit_replies' ); | |
| 179 | 254 | break; |
| 180 | 255 | } |
| 181 | 256 | |
| 182 | - return apply_filters( 'bbp_map_reply_meta_caps', $caps, $cap, $user_id, $args ); | |
| 257 | + // Filter & return | |
| 258 | + return (array) apply_filters( 'bbp_map_reply_meta_caps', $caps, $cap, $user_id, $args ); | |
| 183 | 259 | } |