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