| @@ -11,15 +11,16 @@ | ||
| 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 ( | |
| 20 | + | |
| 21 | + // Filter & return | |
| 22 | + return (array) apply_filters( 'bbp_get_reply_caps', array( | |
| 22 | 23 | 'edit_posts' => 'edit_replies', |
| 23 | 24 | 'edit_others_posts' => 'edit_others_replies', |
| 24 | 25 | 'publish_posts' => 'publish_replies', |
| 25 | 26 | 'read_private_posts' => 'read_private_replies', |
| @@ -30,17 +31,15 @@ | ||
| 30 | 31 | |
| 31 | 32 | /** |
| 32 | 33 | * Maps topic capabilities |
| 33 | 34 | * |
| 34 | - * @since bbPress (r4242) | |
| 35 | + * @since 2.2.0 bbPress (r4242) | |
| 35 | 36 | * |
| 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 | |
| 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 | + * | |
| 43 | 42 | * @return array Actual capabilities for meta capability |
| 44 | 43 | */ |
| 45 | 44 | function bbp_map_reply_meta_caps( $caps = array(), $cap = '', $user_id = 0, $args = array() ) { |
| 46 | 45 | |
| @@ -57,23 +56,32 @@ | ||
| 57 | 56 | |
| 58 | 57 | // Do some post ID based logic |
| 59 | 58 | } else { |
| 60 | 59 | |
| 61 | - // Get the post | |
| 60 | + // Bail if no post ID | |
| 61 | + if ( empty( $args[0] ) ) { | |
| 62 | + break; | |
| 63 | + } | |
| 64 | + | |
| 65 | + // Get the post. | |
| 62 | 66 | $_post = get_post( $args[0] ); |
| 63 | - if ( !empty( $_post ) ) { | |
| 67 | + if ( ! empty( $_post ) ) { | |
| 64 | 68 | |
| 65 | - // Get caps for post type object | |
| 69 | + // Get post type object | |
| 66 | 70 | $post_type = get_post_type_object( $_post->post_type ); |
| 67 | 71 | |
| 68 | 72 | // Post is public |
| 69 | - if ( bbp_get_public_status_id() == $_post->post_status ) { | |
| 73 | + if ( bbp_get_public_status_id() === $_post->post_status ) { | |
| 70 | 74 | $caps = array( 'spectate' ); |
| 71 | 75 | |
| 72 | 76 | // User is author so allow read |
| 73 | - } elseif ( (int) $user_id == (int) $_post->post_author ) { | |
| 77 | + } elseif ( (int) $user_id === (int) $_post->post_author ) { | |
| 74 | 78 | $caps = array( 'spectate' ); |
| 75 | 79 | |
| 80 | + // Moderators can always edit forum content | |
| 81 | + } elseif ( user_can( $user_id, 'moderate', $_post->ID ) ) { | |
| 82 | + $caps = array( 'spectate' ); | |
| 83 | + | |
| 76 | 84 | // Unknown so map to private posts |
| 77 | 85 | } else { |
| 78 | 86 | $caps = array( $post_type->cap->read_private_posts ); |
| 79 | 87 | } |
| @@ -101,8 +109,21 @@ | ||
| 101 | 109 | |
| 102 | 110 | // Moderators can always edit |
| 103 | 111 | if ( user_can( $user_id, 'moderate' ) ) { |
| 104 | 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 | + } | |
| 105 | 126 | } |
| 106 | 127 | |
| 107 | 128 | break; |
| 108 | 129 | |
| @@ -108,27 +129,47 @@ | ||
| 108 | 129 | |
| 109 | 130 | // Used everywhere |
| 110 | 131 | case 'edit_reply' : |
| 111 | 132 | |
| 112 | - // Get the post | |
| 133 | + // Bail if no post ID | |
| 134 | + if ( empty( $args[0] ) ) { | |
| 135 | + break; | |
| 136 | + } | |
| 137 | + | |
| 138 | + // Get the post. | |
| 113 | 139 | $_post = get_post( $args[0] ); |
| 114 | - if ( !empty( $_post ) ) { | |
| 140 | + if ( ! empty( $_post ) ) { | |
| 115 | 141 | |
| 116 | - // Get caps for post type object | |
| 142 | + // Get post type object | |
| 117 | 143 | $post_type = get_post_type_object( $_post->post_type ); |
| 118 | - $caps = array(); | |
| 119 | 144 | |
| 120 | 145 | // Add 'do_not_allow' cap if user is spam or deleted |
| 121 | 146 | if ( bbp_is_user_inactive( $user_id ) ) { |
| 122 | - $caps[] = 'do_not_allow'; | |
| 147 | + $caps = array( 'do_not_allow' ); | |
| 123 | 148 | |
| 124 | - // User is author so allow edit | |
| 125 | - } elseif ( (int) $user_id == (int) $_post->post_author ) { | |
| 126 | - $caps[] = $post_type->cap->edit_posts; | |
| 149 | + // Moderators can always edit forum content | |
| 150 | + } elseif ( user_can( $user_id, 'moderate', $_post->ID ) ) { | |
| 151 | + $caps = array( 'spectate' ); | |
| 127 | 152 | |
| 128 | - // Unknown, so map to edit_others_posts | |
| 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. | |
| 129 | 170 | } else { |
| 130 | - $caps[] = $post_type->cap->edit_others_posts; | |
| 171 | + $caps = array( $post_type->cap->edit_others_posts ); | |
| 131 | 172 | } |
| 132 | 173 | } |
| 133 | 174 | |
| 134 | 175 | break; |
| @@ -136,27 +177,35 @@ | ||
| 136 | 177 | /** Deleting **********************************************************/ |
| 137 | 178 | |
| 138 | 179 | case 'delete_reply' : |
| 139 | 180 | |
| 181 | + // Bail if no post ID | |
| 182 | + if ( empty( $args[0] ) ) { | |
| 183 | + break; | |
| 184 | + } | |
| 185 | + | |
| 140 | 186 | // Get the post |
| 141 | 187 | $_post = get_post( $args[0] ); |
| 142 | - if ( !empty( $_post ) ) { | |
| 188 | + if ( ! empty( $_post ) ) { | |
| 143 | 189 | |
| 144 | - // Get caps for post type object | |
| 190 | + // Get post type object | |
| 145 | 191 | $post_type = get_post_type_object( $_post->post_type ); |
| 146 | - $caps = array(); | |
| 147 | 192 | |
| 148 | 193 | // Add 'do_not_allow' cap if user is spam or deleted |
| 149 | 194 | if ( bbp_is_user_inactive( $user_id ) ) { |
| 150 | - $caps[] = 'do_not_allow'; | |
| 195 | + $caps = array( 'do_not_allow' ); | |
| 151 | 196 | |
| 152 | 197 | // Moderators can always edit forum content |
| 153 | - } elseif ( user_can( $user_id, 'moderate' ) ) { | |
| 154 | - $caps[] = 'moderate'; | |
| 198 | + } elseif ( user_can( $user_id, 'moderate', $_post->ID ) ) { | |
| 199 | + $caps = array( 'spectate' ); | |
| 155 | 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 | + | |
| 156 | 205 | // Unknown so map to delete_others_posts |
| 157 | 206 | } else { |
| 158 | - $caps[] = $post_type->cap->delete_others_posts; | |
| 207 | + $caps = array( $post_type->cap->delete_others_posts ); | |
| 159 | 208 | } |
| 160 | 209 | } |
| 161 | 210 | |
| 162 | 211 | break; |
| @@ -174,10 +223,11 @@ | ||
| 174 | 223 | |
| 175 | 224 | /** Admin *************************************************************/ |
| 176 | 225 | |
| 177 | 226 | case 'bbp_replies_admin' : |
| 178 | - $caps = array( 'moderate' ); | |
| 227 | + $caps = array( 'edit_replies' ); | |
| 179 | 228 | break; |
| 180 | 229 | } |
| 181 | 230 | |
| 182 | - return apply_filters( 'bbp_map_reply_meta_caps', $caps, $cap, $user_id, $args ); | |
| 231 | + // Filter & return | |
| 232 | + return (array) apply_filters( 'bbp_map_reply_meta_caps', $caps, $cap, $user_id, $args ); | |
| 183 | 233 | } |