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

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

260 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * bbPress Reply Capabilites
5 *
6 * Used to map reply capabilities to WordPress's existing capabilities.
7 *
8 * @package bbPress
9 * @subpackage Capabilities
10 */
11
12 /**
13 * Return reply capabilities
14 *
15 * @since 2.0.0 bbPress (r2593)
16 *
17 * @return array Reply capabilities
18 */
19 function bbp_get_reply_caps() {
20
21 /**
22 * Filters the reply capabilities.
23 *
24 * @since 2.0.0
25 *
26 * @param array $caps An array of reply capabilities with keys mapping to WordPress capabilities.
27 */
28 return (array) apply_filters(
29 'bbp_get_reply_caps',
30 array(
31 'edit_posts' => 'edit_replies',
32 'edit_others_posts' => 'edit_others_replies',
33 'publish_posts' => 'publish_replies',
34 'read_private_posts' => 'read_private_replies',
35 'delete_posts' => 'delete_replies',
36 'delete_others_posts' => 'delete_others_replies'
37 )
38 );
39 }
40
41 /**
42 * Maps topic capabilities
43 *
44 * @since 2.2.0 bbPress (r4242)
45 *
46 * @param array $caps Capabilities for meta capability.
47 * @param string $cap Capability name.
48 * @param int $user_id User id.
49 * @param array $args Arguments.
50 *
51 * @return array Actual capabilities for meta capability
52 */
53 function bbp_map_reply_meta_caps( $caps = array(), $cap = '', $user_id = 0, $args = array() ) {
54
55 // What capability is being checked?
56 switch ( $cap ) {
57
58 /** Reading ***********************************************************/
59
60 case 'read_reply' :
61
62 // User cannot spectate
63 if ( ! user_can( $user_id, 'spectate' ) && ! bbp_is_anonymous() ) {
64 $caps = array( 'do_not_allow' );
65
66 // Do some post ID based logic
67 } else {
68
69 // Bail if no post ID
70 if ( empty( $args[0] ) ) {
71 break;
72 }
73
74 // Get the post.
75 $_post = get_post( $args[0] );
76 if ( ! empty( $_post ) ) {
77
78 // Get post type object
79 $post_type = get_post_type_object( $_post->post_type );
80
81 // Post is public
82 if ( bbp_get_public_status_id() === $_post->post_status ) {
83
84 // Anonymous users do not have caps, but can 'exist'
85 if ( bbp_is_anonymous() ) {
86 $caps = array( 'exist' );
87
88 // Registered users need the 'spectate' cap
89 } else {
90 $caps = array( 'spectate' );
91 }
92
93 // User is author so allow read
94 } elseif ( (int) $user_id === (int) $_post->post_author ) {
95 $caps = array( 'spectate' );
96
97 // Moderators can always edit forum content
98 } elseif ( user_can( $user_id, 'moderate', $_post->ID ) ) {
99 $caps = array( 'spectate' );
100
101 // Unknown so map to private posts
102 } else {
103 $caps = array( $post_type->cap->read_private_posts );
104 }
105 }
106 }
107
108 break;
109
110 /** Publishing ********************************************************/
111
112 case 'publish_replies' :
113
114 // Moderators can always publish
115 if ( user_can( $user_id, 'moderate' ) ) {
116 $caps = array( 'moderate' );
117 }
118
119 break;
120
121 /** Editing ***********************************************************/
122
123 // Used primarily in wp-admin
124 case 'edit_replies' :
125 case 'edit_others_replies' :
126
127 // Moderators can always edit
128 if ( user_can( $user_id, 'moderate' ) ) {
129 $caps = array( 'moderate' );
130
131 // Otherwise, check forum
132 } else {
133 $forum_id = bbp_get_forum_id();
134
135 // Moderators can always edit forum content
136 if ( user_can( $user_id, 'moderate', $forum_id ) ) {
137 $caps = array( 'spectate' );
138
139 // Fallback to do_not_allow
140 } else {
141 $caps = array( 'do_not_allow' );
142 }
143 }
144
145 break;
146
147 // Used everywhere
148 case 'edit_reply' :
149
150 // Bail if no post ID
151 if ( empty( $args[0] ) ) {
152 break;
153 }
154
155 // Get the post.
156 $_post = get_post( $args[0] );
157 if ( ! empty( $_post ) ) {
158
159 // Get post type object
160 $post_type = get_post_type_object( $_post->post_type );
161 $forum_id = bbp_get_reply_forum_id( $_post->ID );
162
163 // Anonymous users cannot edit existing replies
164 if ( empty( $user_id ) ) {
165 $caps = array( 'do_not_allow' );
166
167 // Add 'do_not_allow' cap if user is spam or deleted
168 } elseif ( bbp_is_user_inactive( $user_id ) ) {
169 $caps = array( 'do_not_allow' );
170
171 // User cannot edit a reply in a restricted forum they cannot read
172 } elseif ( bbp_is_forum_restricted_for_user( $forum_id, $user_id ) ) {
173 $caps = array( 'do_not_allow' );
174
175 // Moderators can always edit forum content
176 } elseif ( user_can( $user_id, 'moderate', $_post->ID ) ) {
177 $caps = array( 'spectate' );
178
179 // Allow author or mod to edit if not in admin, unless past edit lock time
180 } elseif ( ! is_admin() && ( (int) $user_id === (int) $_post->post_author ) ) {
181
182 // If editing...
183 if ( bbp_is_reply_edit() ) {
184
185 // Only allow if not past the edit-lock period
186 $caps = ! bbp_past_edit_lock( $_post->post_date_gmt )
187 ? array( $post_type->cap->edit_posts )
188 : array( 'do_not_allow' );
189
190 // Otherwise...
191 } else {
192 $caps = array( $post_type->cap->edit_posts );
193 }
194
195 // Fallback to edit_others_posts.
196 } else {
197 $caps = array( $post_type->cap->edit_others_posts );
198 }
199 }
200
201 break;
202
203 /** Deleting **********************************************************/
204
205 case 'delete_reply' :
206
207 // Bail if no post ID
208 if ( empty( $args[0] ) ) {
209 break;
210 }
211
212 // Get the post
213 $_post = get_post( $args[0] );
214 if ( ! empty( $_post ) ) {
215
216 // Get post type object
217 $post_type = get_post_type_object( $_post->post_type );
218
219 // Add 'do_not_allow' cap if user is spam or deleted
220 if ( bbp_is_user_inactive( $user_id ) ) {
221 $caps = array( 'do_not_allow' );
222
223 // Moderators can always edit forum content
224 } elseif ( user_can( $user_id, 'moderate', $_post->ID ) ) {
225 $caps = array( 'spectate' );
226
227 // User is author so allow delete if not in admin
228 } elseif ( ! is_admin() && ( (int) $user_id === (int) $_post->post_author ) ) {
229 $caps = array( $post_type->cap->delete_posts );
230
231 // Unknown so map to delete_others_posts
232 } else {
233 $caps = array( $post_type->cap->delete_others_posts );
234 }
235 }
236
237 break;
238
239 // Moderation override
240 case 'delete_replies' :
241 case 'delete_others_replies' :
242
243 // Moderators can always delete
244 if ( user_can( $user_id, 'moderate' ) ) {
245 $caps = array( 'moderate' );
246 }
247
248 break;
249
250 /** Admin *************************************************************/
251
252 case 'bbp_replies_admin' :
253 $caps = array( 'edit_replies' );
254 break;
255 }
256
257 // Filter & return
258 return (array) apply_filters( 'bbp_map_reply_meta_caps', $caps, $cap, $user_id, $args );
259 }
260