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