PluginProbe
Forumax – AI Powered Advanced Community Forum Plugin / trunk
Forumax – AI Powered Advanced Community Forum Plugin vtrunk
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 / forums / capabilities.php

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

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