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 / forums / capabilities.php

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

313 lines 8.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * bbPress Forum Capabilites
5 *
6 * Used to map forum capabilities to WordPress's existing capabilities.
7 *
8 * @package bbPress
9 * @subpackage Capabilities
10 */
11
12 /**
13 * Return forum capabilities
14 *
15 * @since 2.0.0 bbPress (r2593)
16 *
17 * @return array Forum capabilities
18 */
19 function bbp_get_forum_caps() {
20
21 // Filter & return
22 return (array) apply_filters(
23 'bbp_get_forum_caps',
24 array(
25 'edit_posts' => 'edit_forums',
26 'edit_others_posts' => 'edit_others_forums',
27 'publish_posts' => 'publish_forums',
28 'read_private_posts' => 'read_private_forums',
29 'read_hidden_posts' => 'read_hidden_forums',
30 'delete_posts' => 'delete_forums',
31 'delete_others_posts' => 'delete_others_forums'
32 )
33 );
34 }
35
36 /**
37 * Maps forum capabilities
38 *
39 * @since 2.2.0 bbPress (r4242)
40 *
41 * @param array $caps Capabilities for meta capability
42 * @param string $cap Capability name
43 * @param int $user_id User id
44 * @param array $args Arguments
45 * @return array Actual capabilities for meta capability
46 */
47 function bbp_map_forum_meta_caps( $caps = array(), $cap = '', $user_id = 0, $args = array() ) {
48
49 // What capability is being checked?
50 switch ( $cap ) {
51
52 /** Reading ***********************************************************/
53
54 case 'read_private_forums' :
55 case 'read_hidden_forums' :
56
57 // Moderators can always read private/hidden forums
58 if ( user_can( $user_id, 'moderate' ) ) {
59 $caps = array( 'moderate' );
60 }
61
62 break;
63
64 case 'read_forum' :
65
66 // User cannot spectate
67 if ( ! user_can( $user_id, 'spectate' ) && ! bbp_is_anonymous() ) {
68 $caps = array( 'do_not_allow' );
69
70 // Do some post ID based logic
71 } else {
72
73 // Bail if no post ID
74 if ( empty( $args[0] ) ) {
75 break;
76 }
77
78 // Get the post.
79 $_post = get_post( $args[0] );
80 if ( ! empty( $_post ) ) {
81
82 // Allow moderators of this forum through restricted ancestors
83 $parent_id = bbp_get_forum_parent_id( $_post->ID );
84 if ( ! empty( $parent_id ) && ! bbp_is_user_forum_moderator( $user_id, $_post->ID ) && bbp_is_forum_restricted_for_user( $parent_id, $user_id ) ) {
85 $caps = array( 'do_not_allow' );
86 break;
87 }
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
95 // Anonymous users do not have caps, but can 'exist'
96 if ( bbp_is_anonymous() ) {
97 $caps = array( 'exist' );
98
99 // Registered users need the 'spectate' cap
100 } else {
101 $caps = array( 'spectate' );
102 }
103
104 // User is author so allow read
105 } elseif ( (int) $user_id === (int) $_post->post_author ) {
106 $caps = array( 'spectate' );
107
108 // Moderators can always read forum content
109 } elseif ( user_can( $user_id, 'moderate', $_post->ID ) ) {
110 $caps = array( 'spectate' );
111
112 // Private
113 } elseif ( bbp_get_hidden_status_id() === $_post->post_status ) {
114 $caps = array( $post_type->cap->read_hidden_posts );
115
116 // Hidden
117 } elseif ( bbp_get_private_status_id() === $_post->post_status ) {
118 $caps = array( $post_type->cap->read_private_posts );
119
120 // Unknown, so map to private
121 } else {
122 $caps = array( $post_type->cap->read_private_posts );
123 }
124 }
125 }
126
127 break;
128
129 /** Publishing ********************************************************/
130
131 case 'publish_forums' :
132
133 // Moderators can always edit
134 if ( user_can( $user_id, 'moderate' ) ) {
135 $caps = array( 'moderate' );
136 }
137
138 break;
139
140 /** Editing ***********************************************************/
141
142 // Used primarily in wp-admin
143 case 'edit_forums' :
144 case 'edit_others_forums' :
145
146 // Moderators can always edit
147 if ( bbp_is_user_keymaster( $user_id ) ) {
148 $caps = array( 'spectate' );
149
150 // Otherwise, block
151 } else {
152 $caps = array( 'do_not_allow' );
153 }
154
155 break;
156
157 // Used for forum status, type, and visibility
158 case 'manage_forum_attributes' :
159
160 // Bail if no forum ID
161 if ( empty( $args[0] ) ) {
162 $caps = array( 'do_not_allow' );
163 break;
164 }
165
166 // Only allow this capability for forums
167 $_post = get_post( $args[0] );
168 $caps = ( ! empty( $_post ) && ( bbp_get_forum_post_type() === $_post->post_type ) )
169 ? array( 'assign_moderators' )
170 : array( 'do_not_allow' );
171
172 break;
173
174 // Used everywhere
175 case 'edit_forum' :
176
177 // Bail if no post ID
178 if ( empty( $args[0] ) ) {
179 break;
180 }
181
182 // Get the post.
183 $_post = get_post( $args[0] );
184 if ( ! empty( $_post ) ) {
185
186 // Get caps for post type object
187 $post_type = get_post_type_object( $_post->post_type );
188
189 // Add 'do_not_allow' cap if user is spam or deleted
190 if ( bbp_is_user_inactive( $user_id ) ) {
191 $caps = array( 'do_not_allow' );
192
193 // Moderators can always read forum content
194 } elseif ( user_can( $user_id, 'moderate', $_post->ID ) ) {
195 $caps = array( 'spectate' );
196
197 // User is author so allow edit if not in admin
198 } elseif ( ! is_admin() && ( (int) $user_id === (int) $_post->post_author ) ) {
199 $caps = array( $post_type->cap->edit_posts );
200
201 // Unknown, so map to edit_others_posts
202 } else {
203 $caps = array( $post_type->cap->edit_others_posts );
204 }
205 }
206
207 break;
208
209 /** Deleting **********************************************************/
210
211 // Allow forum authors to delete forums (for BuddyPress groups, etc)
212 case 'delete_forum' :
213
214 // Bail if no post ID
215 if ( empty( $args[0] ) ) {
216 break;
217 }
218
219 // Get the post.
220 $_post = get_post( $args[0] );
221 if ( ! empty( $_post ) ) {
222
223 // Get caps for post type object
224 $post_type = get_post_type_object( $_post->post_type );
225
226 // Add 'do_not_allow' cap if user is spam or deleted
227 if ( bbp_is_user_inactive( $user_id ) ) {
228 $caps = array( 'do_not_allow' );
229
230 // User is author so allow to delete
231 } elseif ( (int) $user_id === (int) $_post->post_author ) {
232 $caps = array( $post_type->cap->delete_posts );
233
234 // Unknown so map to delete_others_posts
235 } else {
236 $caps = array( $post_type->cap->delete_others_posts );
237 }
238 }
239
240 break;
241
242 /** Admin *************************************************************/
243
244 // Forum admin area.
245 case 'bbp_forums_admin' :
246 $caps = array( 'edit_forums' );
247 break;
248 }
249
250 // Filter & return
251 return (array) apply_filters( 'bbp_map_forum_meta_caps', $caps, $cap, $user_id, $args );
252 }
253
254 /**
255 * Can a user moderate a forum?
256 *
257 * @since 2.6.0 bbPress (r5834)
258 *
259 * @param int $user_id User id.
260 * @param int $forum_id Forum id.
261 *
262 * @return bool Return true if user is moderator of forum
263 */
264 function bbp_is_user_forum_moderator( $user_id = 0, $forum_id = 0 ) {
265 $user_id = bbp_get_user_id( $user_id, false, empty( $user_id ) );
266 $forum_id = bbp_get_forum_id( $forum_id );
267 $retval = user_can( $user_id, 'moderate', $forum_id );
268
269 // Filter & return
270 return (bool) apply_filters( 'bbp_is_user_forum_moderator', $retval, $user_id, $forum_id );
271 }
272
273 /**
274 * Filter an array of forum IDs that are being excluded, and remove any forum
275 * IDs a user explicitly has access to.
276 *
277 * This typically means private or hidden forums the user has moderation rights
278 * to, but it can be filtered to mean just about anything.
279 *
280 * This function filters the return values of the following functions:
281 * - `bbp_get_private_forum_ids()`
282 * - `bbp_get_hidden_forum_ids()`
283 *
284 * @since 2.6.0 bbPress (r6426)
285 *
286 * @param array $forum_ids Forum IDs to check if the user ID is a moderator of
287 * @param int $user_id User ID to check if is a moderator of forums
288 *
289 * @return array
290 */
291 function bbp_allow_forums_of_user( $forum_ids = array(), $user_id = 0 ) {
292 $user_id = bbp_get_user_id( $user_id, false, empty( $user_id ) );
293
294 // Store the original forum IDs
295 $original_forum_ids = $forum_ids;
296
297 // Per-forum Moderators
298 if ( bbp_allow_forum_mods() ) {
299
300 // Loop through forum IDs
301 foreach ( $forum_ids as $key => $forum_id ) {
302
303 // Unset forum ID if user is a moderator or can otherwise read it
304 if ( bbp_is_user_forum_moderator( $user_id, $forum_id ) || user_can( $user_id, 'read_forum', $forum_id ) ) {
305 unset( $forum_ids[ $key ] );
306 }
307 }
308 }
309
310 // Filter & return
311 return (array) apply_filters( 'bbp_allow_forums_of_user', $forum_ids, $user_id, $original_forum_ids );
312 }
313