PluginProbe
bbPress / 2.6.15
bbPress v2.6.15
trunk 2.0 2.0-beta-1 2.0-beta-2b 2.0-beta-3 2.0-beta-3b 2.0-rc-2 2.0-rc-3 2.0-rc-4 2.0-rc-5 2.0.1 2.0.2 2.0.3 2.1 2.1-beta-1 2.1-rc1 2.1-rc2 2.1-rc3 2.1-rc4 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.2.2 All 71 releases
bbpress / includes / topics / capabilities.php

capabilities.php in bbPress 2.6.15, at includes/topics/capabilities.php

398 lines 9.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * bbPress Topic Capabilites
5 *
6 * Used to map topic capabilities to WordPress's existing capabilities.
7 *
8 * @package bbPress
9 * @subpackage Capabilities
10 */
11
12 /**
13 * Return topic capabilities
14 *
15 * @since 2.0.0 bbPress (r2593)
16 *
17 * @return array Topic capabilities
18 */
19 function bbp_get_topic_caps() {
20
21 // Filter & return
22 return (array) apply_filters( 'bbp_get_topic_caps', array(
23 'edit_posts' => 'edit_topics',
24 'edit_others_posts' => 'edit_others_topics',
25 'publish_posts' => 'publish_topics',
26 'read_private_posts' => 'read_private_topics',
27 'read_hidden_posts' => 'read_hidden_topics',
28 'delete_posts' => 'delete_topics',
29 'delete_others_posts' => 'delete_others_topics'
30 ) );
31 }
32
33 /**
34 * Return topic tag capabilities
35 *
36 * @since 2.0.0 bbPress (r2593)
37 *
38 *
39 * @return array Topic tag capabilities
40 */
41 function bbp_get_topic_tag_caps() {
42
43 // Filter & return
44 return (array) apply_filters( 'bbp_get_topic_tag_caps', array(
45 'manage_terms' => 'manage_topic_tags',
46 'edit_terms' => 'edit_topic_tags',
47 'delete_terms' => 'delete_topic_tags',
48 'assign_terms' => 'assign_topic_tags'
49 ) );
50 }
51
52 /**
53 * Maps topic capabilities
54 *
55 * @since 2.2.0 bbPress (r4242)
56 *
57 * @param array $caps Capabilities for meta capability.
58 * @param string $cap Capability name.
59 * @param int $user_id User id.
60 * @param array $args Arguments.
61 *
62 * @return array Actual capabilities for meta capability
63 */
64 function bbp_map_topic_meta_caps( $caps = array(), $cap = '', $user_id = 0, $args = array() ) {
65
66 // What capability is being checked?
67 switch ( $cap ) {
68
69 /** Reading ***********************************************************/
70
71 case 'read_topic' :
72
73 // User cannot spectate
74 if ( ! user_can( $user_id, 'spectate' ) ) {
75 $caps = array( 'do_not_allow' );
76
77 // Do some post ID based logic
78 } else {
79
80 // Bail if no post ID
81 if ( empty( $args[0] ) ) {
82 break;
83 }
84
85 // Get the post.
86 $_post = get_post( $args[0] );
87 if ( ! empty( $_post ) ) {
88
89 // Get caps for post type object
90 $post_type = get_post_type_object( $_post->post_type );
91
92 // Post is public
93 if ( bbp_get_public_status_id() === $_post->post_status ) {
94 $caps = array( 'spectate' );
95
96 // User is author so allow read
97 } elseif ( (int) $user_id === (int) $_post->post_author ) {
98 $caps = array( 'spectate' );
99
100 // Moderators can always edit forum content
101 } elseif ( user_can( $user_id, 'moderate', $_post->ID ) ) {
102 $caps = array( 'spectate' );
103
104 // Unknown so map to private posts
105 } else {
106 $caps = array( $post_type->cap->read_private_posts );
107 }
108 }
109 }
110
111 break;
112
113 /** Publishing ********************************************************/
114
115 case 'publish_topics' :
116
117 // Moderators can always publish
118 if ( user_can( $user_id, 'moderate' ) ) {
119 $caps = array( 'moderate' );
120 }
121
122 break;
123
124 /** Editing ***********************************************************/
125
126 // Used primarily in wp-admin
127 case 'edit_topics' :
128 case 'edit_others_topics' :
129
130 // Moderators can always edit
131 if ( user_can( $user_id, 'moderate' ) ) {
132 $caps = array( $cap );
133
134 // Otherwise, check forum
135 } else {
136 $forum_id = bbp_get_forum_id();
137
138 // Moderators can always edit forum content
139 if ( user_can( $user_id, 'moderate', $forum_id ) ) {
140 $caps = array( 'spectate' );
141
142 // Fallback to do_not_allow
143 } else {
144 $caps = array( 'do_not_allow' );
145 }
146 }
147
148 break;
149
150 // Used everywhere
151 case 'edit_topic' :
152
153 // Bail if no post ID
154 if ( empty( $args[0] ) ) {
155 break;
156 }
157
158 // Get the post.
159 $_post = get_post( $args[0] );
160 if ( ! empty( $_post ) ) {
161
162 // Get caps for post type object
163 $post_type = get_post_type_object( $_post->post_type );
164
165 // Anonymous users cannot edit existing topics
166 if ( empty( $user_id ) ) {
167 $caps = array( 'do_not_allow' );
168
169 // Add 'do_not_allow' cap if user is spam or deleted
170 } elseif ( bbp_is_user_inactive( $user_id ) ) {
171 $caps = array( 'do_not_allow' );
172
173 // Moderators can always edit forum content
174 } elseif ( user_can( $user_id, 'moderate', $_post->ID ) ) {
175 $caps = array( 'spectate' );
176
177 // User is author so allow edit if not in admin
178 } elseif ( ! is_admin() && ( (int) $user_id === (int) $_post->post_author ) ) {
179
180 // If merging or splitting...
181 if ( bbp_is_topic_merge() || bbp_is_topic_split() ) {
182 $caps = array( 'moderate' );
183
184 // If editing...
185 } elseif ( bbp_is_topic_edit() ) {
186
187 // Only allow if not past the edit-lock period
188 $caps = ! bbp_past_edit_lock( $_post->post_date_gmt )
189 ? array( $post_type->cap->edit_posts )
190 : array( 'do_not_allow' );
191
192 // Otherwise...
193 } else {
194 $caps = array( $post_type->cap->edit_posts );
195 }
196
197 // Unknown, so map to edit_others_posts
198 } else {
199 $caps = array( $post_type->cap->edit_others_posts );
200 }
201 }
202
203 break;
204
205 /** Deleting **********************************************************/
206
207 case 'delete_topic' :
208
209 // Bail if no post ID
210 if ( empty( $args[0] ) ) {
211 break;
212 }
213
214 // Get the post.
215 $_post = get_post( $args[0] );
216 if ( ! empty( $_post ) ) {
217
218 // Get caps for post type object
219 $post_type = get_post_type_object( $_post->post_type );
220
221 // Add 'do_not_allow' cap if user is spam or deleted
222 if ( bbp_is_user_inactive( $user_id ) ) {
223 $caps = array( 'do_not_allow' );
224
225 // Moderators can always edit forum content
226 } elseif ( user_can( $user_id, 'moderate', $_post->ID ) ) {
227 $caps = array( 'spectate' );
228
229 // User is author so allow delete if not in admin
230 } elseif ( ! is_admin() && ( (int) $user_id === (int) $_post->post_author ) ) {
231 $caps = array( $post_type->cap->delete_posts );
232
233 // Unknown so map to delete_others_posts
234 } else {
235 $caps = array( $post_type->cap->delete_others_posts );
236 }
237 }
238
239 break;
240
241 // Moderation override
242 case 'delete_topics' :
243 case 'delete_others_topics' :
244
245 // Moderators can always delete
246 if ( user_can( $user_id, 'moderate' ) ) {
247 $caps = array( $cap );
248 }
249
250 break;
251
252 /** Admin *************************************************************/
253
254 case 'bbp_topics_admin' :
255 $caps = array( 'edit_topics' );
256 break;
257 }
258
259 // Filter & return
260 return (array) apply_filters( 'bbp_map_topic_meta_caps', $caps, $cap, $user_id, $args );
261 }
262
263 /**
264 * Maps topic tag capabilities
265 *
266 * @since 2.2.0 bbPress (r4242)
267 *
268 * @param array $caps Capabilities for meta capability
269 * @param string $cap Capability name
270 * @param int $user_id User id
271 * @param array $args Arguments
272 *
273 * @return array Actual capabilities for meta capability
274 */
275 function bbp_map_topic_tag_meta_caps( $caps, $cap, $user_id, $args ) {
276
277 // What capability is being checked?
278 switch ( $cap ) {
279
280 /** Assignment ********************************************************/
281
282 case 'assign_topic_tags' :
283
284 // Get post
285 $post_id = ! empty( $args[0] )
286 ? get_post( $args[0] )->ID
287 : 0;
288
289 // Add 'do_not_allow' cap if user is spam or deleted
290 if ( bbp_is_user_inactive( $user_id ) ) {
291 $caps = array( 'do_not_allow' );
292
293 // Moderators can always assign
294 } elseif ( user_can( $user_id, 'moderate', $post_id ) ) {
295 $caps = array( 'moderate' );
296
297 // Do not allow if topic tags are disabled
298 } elseif ( ! bbp_allow_topic_tags() ) {
299 $caps = array( 'do_not_allow' );
300 }
301
302 break;
303
304 /** Management ********************************************************/
305
306 case 'manage_topic_tags' :
307
308 // Moderators can always edit
309 if ( user_can( $user_id, 'moderate' ) ) {
310 $caps = array( 'moderate' );
311 }
312
313 break;
314
315 /** Editing ***********************************************************/
316
317 case 'edit_topic_tags' :
318
319 // Moderators can always edit
320 if ( user_can( $user_id, 'moderate' ) ) {
321 $caps = array( 'moderate' );
322 }
323
324 break;
325
326 case 'edit_topic_tag' :
327
328 // Get the term
329 $_tag = get_term( $args[0], bbp_get_topic_tag_tax_id() );
330 if ( ! empty( $_tag ) ) {
331
332 // Add 'do_not_allow' cap if user is spam or deleted
333 if ( bbp_is_user_inactive( $user_id ) ) {
334 $caps = array( 'do_not_allow' );
335
336 // Moderators can always edit topic tags
337 } elseif ( user_can( $user_id, 'moderate', $_tag->term_id ) ) {
338 $caps = array( 'spectate' );
339
340 // Fallback to edit_terms.
341 } else {
342 $taxonomy = get_taxonomy( bbp_get_topic_tag_tax_id() );
343 $caps = array( $taxonomy->cap->edit_terms );
344 }
345 }
346
347 break;
348
349 /** Deleting **********************************************************/
350
351 case 'delete_topic_tags' :
352
353 // Moderators can always edit
354 if ( user_can( $user_id, 'moderate' ) ) {
355 $caps = array( 'moderate' );
356 }
357
358 break;
359
360 case 'delete_topic_tag' :
361
362 // Get the term
363 $_tag = get_term( $args[0], bbp_get_topic_tag_tax_id() );
364 if ( ! empty( $_tag ) ) {
365
366 // Add 'do_not_allow' cap if user is spam or deleted
367 if ( bbp_is_user_inactive( $user_id ) ) {
368 $caps = array( 'do_not_allow' );
369
370 // Moderators can always delete topic tags
371 } elseif ( user_can( $user_id, 'moderate', $_tag->term_id ) ) {
372 $caps = array( 'spectate' );
373
374 // Fallback to delete_terms.
375 } else {
376 $taxonomy = get_taxonomy( $_tag->post_type );
377 $caps = array( $taxonomy->cap->delete_terms );
378 }
379 }
380
381 break;
382
383 /** Admin *************************************************************/
384
385 case 'bbp_topic_tags_admin' :
386
387 // Moderators can always edit
388 if ( user_can( $user_id, 'moderate' ) ) {
389 $caps = array( 'moderate' );
390 }
391
392 break;
393 }
394
395 // Filter & return
396 return (array) apply_filters( 'bbp_map_topic_tag_meta_caps', $caps, $cap, $user_id, $args );
397 }
398