|
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 bbPress (r2593) |
|
16 * |
|
17 * @uses apply_filters() Calls 'bbp_get_forum_caps' with the capabilities |
|
18 * @return array Forum capabilities |
|
19 */ |
|
20 function bbp_get_forum_caps() { |
|
21 return apply_filters( 'bbp_get_forum_caps', array ( |
|
22 'edit_posts' => 'edit_forums', |
|
23 'edit_others_posts' => 'edit_others_forums', |
|
24 'publish_posts' => 'publish_forums', |
|
25 'read_private_posts' => 'read_private_forums', |
|
26 'read_hidden_posts' => 'read_hidden_forums', |
|
27 'delete_posts' => 'delete_forums', |
|
28 'delete_others_posts' => 'delete_others_forums' |
|
29 ) ); |
|
30 } |
|
31 |
|
32 /** |
|
33 * Maps forum capabilities |
|
34 * |
|
35 * @since 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 mixed $args Arguments |
|
41 * @uses get_post() To get the post |
|
42 * @uses get_post_type_object() To get the post type object |
|
43 * @uses apply_filters() Filter capability map results |
|
44 * @return array Actual capabilities for meta capability |
|
45 */ |
|
46 function bbp_map_forum_meta_caps( $caps = array(), $cap = '', $user_id = 0, $args = array() ) { |
|
47 |
|
48 // What capability is being checked? |
|
49 switch ( $cap ) { |
|
50 |
|
51 /** Reading ***********************************************************/ |
|
52 |
|
53 case 'read_private_forums' : |
|
54 case 'read_hidden_forums' : |
|
55 |
|
56 // Moderators can always read private/hidden forums |
|
57 if ( user_can( $user_id, 'moderate' ) ) { |
|
58 $caps = array( 'moderate' ); |
|
59 } |
|
60 |
|
61 break; |
|
62 |
|
63 case 'read_forum' : |
|
64 |
|
65 // User cannot spectate |
|
66 if ( ! user_can( $user_id, 'spectate' ) ) { |
|
67 $caps = array( 'do_not_allow' ); |
|
68 |
|
69 // Do some post ID based logic |
|
70 } else { |
|
71 |
|
72 // Get the post |
|
73 $_post = get_post( $args[0] ); |
|
74 if ( !empty( $_post ) ) { |
|
75 |
|
76 // Get caps for post type object |
|
77 $post_type = get_post_type_object( $_post->post_type ); |
|
78 |
|
79 // Post is public |
|
80 if ( bbp_get_public_status_id() == $_post->post_status ) { |
|
81 $caps = array( 'spectate' ); |
|
82 |
|
83 // User is author so allow read |
|
84 } elseif ( (int) $user_id == (int) $_post->post_author ) { |
|
85 $caps = array( 'spectate' ); |
|
86 |
|
87 // Unknown so map to private posts |
|
88 } else { |
|
89 $caps = array( $post_type->cap->read_private_posts ); |
|
90 } |
|
91 } |
|
92 } |
|
93 |
|
94 break; |
|
95 |
|
96 /** Publishing ********************************************************/ |
|
97 |
|
98 case 'publish_forums' : |
|
99 |
|
100 // Moderators can always edit |
|
101 if ( user_can( $user_id, 'moderate' ) ) { |
|
102 $caps = array( 'moderate' ); |
|
103 } |
|
104 |
|
105 break; |
|
106 |
|
107 /** Editing ***********************************************************/ |
|
108 |
|
109 // Used primarily in wp-admin |
|
110 case 'edit_forums' : |
|
111 case 'edit_others_forums' : |
|
112 |
|
113 // Moderators can always edit |
|
114 if ( user_can( $user_id, 'moderate' ) ) { |
|
115 $caps = array( 'moderate' ); |
|
116 } |
|
117 |
|
118 break; |
|
119 |
|
120 // Used everywhere |
|
121 case 'edit_forum' : |
|
122 |
|
123 // Get the post |
|
124 $_post = get_post( $args[0] ); |
|
125 if ( !empty( $_post ) ) { |
|
126 |
|
127 // Get caps for post type object |
|
128 $post_type = get_post_type_object( $_post->post_type ); |
|
129 $caps = array(); |
|
130 |
|
131 // Add 'do_not_allow' cap if user is spam or deleted |
|
132 if ( bbp_is_user_inactive( $user_id ) ) { |
|
133 $caps[] = 'do_not_allow'; |
|
134 |
|
135 // User is author so allow edit |
|
136 } elseif ( (int) $user_id == (int) $_post->post_author ) { |
|
137 $caps[] = $post_type->cap->edit_posts; |
|
138 |
|
139 // Unknown, so map to edit_others_posts |
|
140 } else { |
|
141 $caps[] = $post_type->cap->edit_others_posts; |
|
142 } |
|
143 } |
|
144 |
|
145 break; |
|
146 |
|
147 /** Deleting **********************************************************/ |
|
148 |
|
149 // Allow forum authors to delete forums (for BuddyPress groups, etc) |
|
150 case 'delete_forum' : |
|
151 |
|
152 // Get the post |
|
153 $_post = get_post( $args[0] ); |
|
154 if ( !empty( $_post ) ) { |
|
155 |
|
156 // Get caps for post type object |
|
157 $post_type = get_post_type_object( $_post->post_type ); |
|
158 $caps = array(); |
|
159 |
|
160 // Add 'do_not_allow' cap if user is spam or deleted |
|
161 if ( bbp_is_user_inactive( $user_id ) ) { |
|
162 $caps[] = 'do_not_allow'; |
|
163 |
|
164 // User is author so allow to delete |
|
165 } elseif ( (int) $user_id == (int) $_post->post_author ) { |
|
166 $caps[] = $post_type->cap->delete_posts; |
|
167 |
|
168 // Unknown so map to delete_others_posts |
|
169 } else { |
|
170 $caps[] = $post_type->cap->delete_others_posts; |
|
171 } |
|
172 } |
|
173 |
|
174 break; |
|
175 |
|
176 /** Admin *************************************************************/ |
|
177 |
|
178 case 'bbp_forums_admin' : |
|
179 $caps = array( 'manage_options' ); |
|
180 break; |
|
181 } |
|
182 |
|
183 return apply_filters( 'bbp_map_forum_meta_caps', $caps, $cap, $user_id, $args ); |
|
184 } |