PluginProbe
bbPress / 2.6.17
bbPress v2.6.17
2.6.17 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 All 72 releases
bbpress / includes / topics / capabilities.php

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

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