PluginProbe
Forumax – AI Powered Advanced Community Forum Plugin / 2.4.2
Forumax – AI Powered Advanced Community Forum Plugin v2.4.2
2.4.4 2.4.3 2.4.2 2.4.1 2.4.0 trunk 1.0.8 1.1.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 2.0.0 2.1.0 All 29 releases
bbp-core / lib / bbpress / includes / topics / capabilities.php

capabilities.php in Forumax – AI Powered Advanced Community Forum Plugin 2.4.2, at lib/bbpress/includes/topics/capabilities.php

394 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 // Allow WordPress administrators for REST API / Gutenberg access.
135 } elseif ( user_can( $user_id, 'manage_options' ) ) {
136 $caps = array( 'manage_options' );
137
138 // Otherwise, check forum
139 } else {
140 $forum_id = bbp_get_forum_id();
141
142 // Moderators can always edit forum content
143 if ( user_can( $user_id, 'moderate', $forum_id ) ) {
144 $caps = array( 'spectate' );
145
146 // Fallback to do_not_allow
147 } else {
148 $caps = array( 'do_not_allow' );
149 }
150 }
151
152 break;
153
154 // Used everywhere
155 case 'edit_topic' :
156
157 // Bail if no post ID
158 if ( empty( $args[0] ) ) {
159 break;
160 }
161
162 // Get the post.
163 $_post = get_post( $args[0] );
164 if ( ! empty( $_post ) ) {
165
166 // Get caps for post type object
167 $post_type = get_post_type_object( $_post->post_type );
168
169 // Add 'do_not_allow' cap if user is spam or deleted
170 if ( 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, unless it's past edit lock time
178 } elseif ( ! is_admin() && ( (int) $user_id === (int) $_post->post_author ) ) {
179
180 // If editing...
181 if ( bbp_is_topic_edit() ) {
182
183 // Only allow if not past the edit-lock period
184 $caps = ! bbp_past_edit_lock( $_post->post_date_gmt )
185 ? array( $post_type->cap->edit_posts )
186 : array( 'do_not_allow' );
187
188 // Otherwise...
189 } else {
190 $caps = array( $post_type->cap->edit_posts );
191 }
192
193 // Unknown, so map to edit_others_posts
194 } else {
195 $caps = array( $post_type->cap->edit_others_posts );
196 }
197 }
198
199 break;
200
201 /** Deleting **********************************************************/
202
203 case 'delete_topic' :
204
205 // Bail if no post ID
206 if ( empty( $args[0] ) ) {
207 break;
208 }
209
210 // Get the post.
211 $_post = get_post( $args[0] );
212 if ( ! empty( $_post ) ) {
213
214 // Get caps for post type object
215 $post_type = get_post_type_object( $_post->post_type );
216
217 // Add 'do_not_allow' cap if user is spam or deleted
218 if ( bbp_is_user_inactive( $user_id ) ) {
219 $caps = array( 'do_not_allow' );
220
221 // Moderators can always edit forum content
222 } elseif ( user_can( $user_id, 'moderate', $_post->ID ) ) {
223 $caps = array( 'spectate' );
224
225 // User is author so allow delete if not in admin
226 } elseif ( ! is_admin() && ( (int) $user_id === (int) $_post->post_author ) ) {
227 $caps = array( $post_type->cap->delete_posts );
228
229 // Unknown so map to delete_others_posts
230 } else {
231 $caps = array( $post_type->cap->delete_others_posts );
232 }
233 }
234
235 break;
236
237 // Moderation override
238 case 'delete_topics' :
239 case 'delete_others_topics' :
240
241 // Moderators can always delete
242 if ( user_can( $user_id, 'moderate' ) ) {
243 $caps = array( $cap );
244 }
245
246 break;
247
248 /** Admin *************************************************************/
249
250 case 'bbp_topics_admin' :
251 $caps = array( 'edit_topics' );
252 break;
253 }
254
255 // Filter & return
256 return (array) apply_filters( 'bbp_map_topic_meta_caps', $caps, $cap, $user_id, $args );
257 }
258
259 /**
260 * Maps topic tag capabilities
261 *
262 * @since 2.2.0 bbPress (r4242)
263 *
264 * @param array $caps Capabilities for meta capability
265 * @param string $cap Capability name
266 * @param int $user_id User id
267 * @param array $args Arguments
268 *
269 * @return array Actual capabilities for meta capability
270 */
271 function bbp_map_topic_tag_meta_caps( $caps, $cap, $user_id, $args ) {
272
273 // What capability is being checked?
274 switch ( $cap ) {
275
276 /** Assignment ********************************************************/
277
278 case 'assign_topic_tags' :
279
280 // Get post
281 $post_id = ! empty( $args[0] )
282 ? get_post( $args[0] )->ID
283 : 0;
284
285 // Add 'do_not_allow' cap if user is spam or deleted
286 if ( bbp_is_user_inactive( $user_id ) ) {
287 $caps = array( 'do_not_allow' );
288
289 // Moderators can always assign
290 } elseif ( user_can( $user_id, 'moderate', $post_id ) ) {
291 $caps = array( 'moderate' );
292
293 // Do not allow if topic tags are disabled
294 } elseif ( ! bbp_allow_topic_tags() ) {
295 $caps = array( 'do_not_allow' );
296 }
297
298 break;
299
300 /** Management ********************************************************/
301
302 case 'manage_topic_tags' :
303
304 // Moderators can always edit
305 if ( user_can( $user_id, 'moderate' ) ) {
306 $caps = array( 'moderate' );
307 }
308
309 break;
310
311 /** Editing ***********************************************************/
312
313 case 'edit_topic_tags' :
314
315 // Moderators can always edit
316 if ( user_can( $user_id, 'moderate' ) ) {
317 $caps = array( 'moderate' );
318 }
319
320 break;
321
322 case 'edit_topic_tag' :
323
324 // Get the term
325 $_tag = get_term( $args[0], bbp_get_topic_tag_tax_id() );
326 if ( ! empty( $_tag ) ) {
327
328 // Add 'do_not_allow' cap if user is spam or deleted
329 if ( bbp_is_user_inactive( $user_id ) ) {
330 $caps = array( 'do_not_allow' );
331
332 // Moderators can always edit topic tags
333 } elseif ( user_can( $user_id, 'moderate', $_tag->term_id ) ) {
334 $caps = array( 'spectate' );
335
336 // Fallback to edit_terms.
337 } else {
338 $taxonomy = get_taxonomy( bbp_get_topic_tag_tax_id() );
339 $caps = array( $taxonomy->cap->edit_terms );
340 }
341 }
342
343 break;
344
345 /** Deleting **********************************************************/
346
347 case 'delete_topic_tags' :
348
349 // Moderators can always edit
350 if ( user_can( $user_id, 'moderate' ) ) {
351 $caps = array( 'moderate' );
352 }
353
354 break;
355
356 case 'delete_topic_tag' :
357
358 // Get the term
359 $_tag = get_term( $args[0], bbp_get_topic_tag_tax_id() );
360 if ( ! empty( $_tag ) ) {
361
362 // Add 'do_not_allow' cap if user is spam or deleted
363 if ( bbp_is_user_inactive( $user_id ) ) {
364 $caps = array( 'do_not_allow' );
365
366 // Moderators can always delete topic tags
367 } elseif ( user_can( $user_id, 'moderate', $_tag->term_id ) ) {
368 $caps = array( 'spectate' );
369
370 // Fallback to delete_terms.
371 } else {
372 $taxonomy = get_taxonomy( $_tag->post_type );
373 $caps = array( $taxonomy->cap->delete_terms );
374 }
375 }
376
377 break;
378
379 /** Admin *************************************************************/
380
381 case 'bbp_topic_tags_admin' :
382
383 // Moderators can always edit
384 if ( user_can( $user_id, 'moderate' ) ) {
385 $caps = array( 'moderate' );
386 }
387
388 break;
389 }
390
391 // Filter & return
392 return (array) apply_filters( 'bbp_map_topic_tag_meta_caps', $caps, $cap, $user_id, $args );
393 }
394