PluginProbe
bbPress / 2.6.15
bbPress v2.6.15
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 2.2.2 All 71 releases
bbpress / includes / replies / capabilities.php

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

238 lines 5.7 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 // Filter & return
22 return (array) apply_filters( 'bbp_get_reply_caps', array(
23 'edit_posts' => 'edit_replies',
24 'edit_others_posts' => 'edit_others_replies',
25 'publish_posts' => 'publish_replies',
26 'read_private_posts' => 'read_private_replies',
27 'delete_posts' => 'delete_replies',
28 'delete_others_posts' => 'delete_others_replies'
29 ) );
30 }
31
32 /**
33 * Maps topic capabilities
34 *
35 * @since 2.2.0 bbPress (r4242)
36 *
37 * @param array $caps Capabilities for meta capability.
38 * @param string $cap Capability name.
39 * @param int $user_id User id.
40 * @param array $args Arguments.
41 *
42 * @return array Actual capabilities for meta capability
43 */
44 function bbp_map_reply_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_reply' :
52
53 // User cannot spectate
54 if ( ! user_can( $user_id, 'spectate' ) ) {
55 $caps = array( 'do_not_allow' );
56
57 // Do some post ID based logic
58 } else {
59
60 // Bail if no post ID
61 if ( empty( $args[0] ) ) {
62 break;
63 }
64
65 // Get the post.
66 $_post = get_post( $args[0] );
67 if ( ! empty( $_post ) ) {
68
69 // Get post type object
70 $post_type = get_post_type_object( $_post->post_type );
71
72 // Post is public
73 if ( bbp_get_public_status_id() === $_post->post_status ) {
74 $caps = array( 'spectate' );
75
76 // User is author so allow read
77 } elseif ( (int) $user_id === (int) $_post->post_author ) {
78 $caps = array( 'spectate' );
79
80 // Moderators can always edit forum content
81 } elseif ( user_can( $user_id, 'moderate', $_post->ID ) ) {
82 $caps = array( 'spectate' );
83
84 // Unknown so map to private posts
85 } else {
86 $caps = array( $post_type->cap->read_private_posts );
87 }
88 }
89 }
90
91 break;
92
93 /** Publishing ********************************************************/
94
95 case 'publish_replies' :
96
97 // Moderators can always publish
98 if ( user_can( $user_id, 'moderate' ) ) {
99 $caps = array( 'moderate' );
100 }
101
102 break;
103
104 /** Editing ***********************************************************/
105
106 // Used primarily in wp-admin
107 case 'edit_replies' :
108 case 'edit_others_replies' :
109
110 // Moderators can always edit
111 if ( user_can( $user_id, 'moderate' ) ) {
112 $caps = array( 'moderate' );
113
114 // Otherwise, check forum
115 } else {
116 $forum_id = bbp_get_forum_id();
117
118 // Moderators can always edit forum content
119 if ( user_can( $user_id, 'moderate', $forum_id ) ) {
120 $caps = array( 'spectate' );
121
122 // Fallback to do_not_allow
123 } else {
124 $caps = array( 'do_not_allow' );
125 }
126 }
127
128 break;
129
130 // Used everywhere
131 case 'edit_reply' :
132
133 // Bail if no post ID
134 if ( empty( $args[0] ) ) {
135 break;
136 }
137
138 // Get the post.
139 $_post = get_post( $args[0] );
140 if ( ! empty( $_post ) ) {
141
142 // Get post type object
143 $post_type = get_post_type_object( $_post->post_type );
144
145 // Anonymous users cannot edit existing replies
146 if ( empty( $user_id ) ) {
147 $caps = array( 'do_not_allow' );
148
149 // Add 'do_not_allow' cap if user is spam or deleted
150 } elseif ( bbp_is_user_inactive( $user_id ) ) {
151 $caps = array( 'do_not_allow' );
152
153 // Moderators can always edit forum content
154 } elseif ( user_can( $user_id, 'moderate', $_post->ID ) ) {
155 $caps = array( 'spectate' );
156
157 // Allow author or mod to edit if not in admin, unless past edit lock time
158 } elseif ( ! is_admin() && ( (int) $user_id === (int) $_post->post_author ) ) {
159
160 // If editing...
161 if ( bbp_is_reply_edit() ) {
162
163 // Only allow if not past the edit-lock period
164 $caps = ! bbp_past_edit_lock( $_post->post_date_gmt )
165 ? array( $post_type->cap->edit_posts )
166 : array( 'do_not_allow' );
167
168 // Otherwise...
169 } else {
170 $caps = array( $post_type->cap->edit_posts );
171 }
172
173 // Fallback to edit_others_posts.
174 } else {
175 $caps = array( $post_type->cap->edit_others_posts );
176 }
177 }
178
179 break;
180
181 /** Deleting **********************************************************/
182
183 case 'delete_reply' :
184
185 // Bail if no post ID
186 if ( empty( $args[0] ) ) {
187 break;
188 }
189
190 // Get the post
191 $_post = get_post( $args[0] );
192 if ( ! empty( $_post ) ) {
193
194 // Get post type object
195 $post_type = get_post_type_object( $_post->post_type );
196
197 // Add 'do_not_allow' cap if user is spam or deleted
198 if ( bbp_is_user_inactive( $user_id ) ) {
199 $caps = array( 'do_not_allow' );
200
201 // Moderators can always edit forum content
202 } elseif ( user_can( $user_id, 'moderate', $_post->ID ) ) {
203 $caps = array( 'spectate' );
204
205 // User is author so allow delete if not in admin
206 } elseif ( ! is_admin() && ( (int) $user_id === (int) $_post->post_author ) ) {
207 $caps = array( $post_type->cap->delete_posts );
208
209 // Unknown so map to delete_others_posts
210 } else {
211 $caps = array( $post_type->cap->delete_others_posts );
212 }
213 }
214
215 break;
216
217 // Moderation override
218 case 'delete_replies' :
219 case 'delete_others_replies' :
220
221 // Moderators can always delete
222 if ( user_can( $user_id, 'moderate' ) ) {
223 $caps = array( 'moderate' );
224 }
225
226 break;
227
228 /** Admin *************************************************************/
229
230 case 'bbp_replies_admin' :
231 $caps = array( 'edit_replies' );
232 break;
233 }
234
235 // Filter & return
236 return (array) apply_filters( 'bbp_map_reply_meta_caps', $caps, $cap, $user_id, $args );
237 }
238