PluginProbe
bbPress / trunk
bbPress vtrunk
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 / admin / users.php

users.php in bbPress trunk, at includes/admin/users.php

351 lines 9.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * bbPress Users Admin Class.
5 *
6 * @package bbPress
7 * @subpackage Administration
8 */
9
10 // Exit if accessed directly
11 defined( 'ABSPATH' ) || exit;
12
13 if ( ! class_exists( 'BBP_Users_Admin' ) ) :
14 /**
15 * Loads bbPress users admin area.
16 *
17 * @package bbPress
18 * @subpackage Administration
19 * @since 2.0.0 bbPress (r2464)
20 */
21 class BBP_Users_Admin {
22
23 /**
24 * The bbPress users admin loader.
25 *
26 * @since 2.0.0 bbPress (r2515)
27 */
28 public function __construct() {
29 $this->setup_actions();
30 }
31
32 /**
33 * Setup the admin hooks, actions and filters.
34 *
35 * @since 2.0.0 bbPress (r2646)
36 *
37 * @access private
38 */
39 private function setup_actions() {
40
41 // Bail if in network admin
42 if ( is_network_admin() ) {
43 return;
44 }
45
46 // User profile edit/display actions
47 add_action( 'edit_user_profile', array( $this, 'secondary_role_display' ) );
48
49 // WordPress user screen
50 // Remove the bottom list table "change forum role" dropdown from WordPress < 4.6.
51 // See https://bbpress.trac.wordpress.org/ticket/2906.
52 if ( bbp_get_major_wp_version() < 4.6 ) {
53 add_action( 'restrict_manage_users', array( __CLASS__, 'user_role_bulk_dropdown' ) );
54 } else {
55 add_action( 'restrict_manage_users', array( $this, 'user_role_bulk_dropdown' ), 10, 1 );
56 }
57 add_filter( 'manage_users_columns', array( $this, 'user_role_column' ), 10, 1 );
58 add_filter( 'manage_users_custom_column', array( $this, 'user_role_row' ), 10, 3 );
59
60 // Only list bbPress roles under Forum Role, remove from WordPress' > 4.4 Site Role list.
61 if ( bbp_get_major_wp_version() >= 4.4 ) {
62 add_filter( 'get_role_list', array( $this, 'user_role_list_filter' ), 10, 2 );
63 }
64
65 // User List Table
66 add_action( 'load-users.php', array( $this, 'user_role_bulk_change' ), 10, 1 );
67 add_action( 'user_row_actions', array( $this, 'user_row_actions' ), 10, 2 );
68
69 /** Dependencies ******************************************************/
70
71 // Allow plugins to modify these actions
72 do_action_ref_array( 'bbp_admin_users_loaded', array( &$this ) );
73 }
74
75 /**
76 * Default interface for setting a forum role.
77 *
78 * @since 2.2.0 bbPress (r4285)
79 *
80 * @param WP_User $profileuser User data
81 * @return bool Always false
82 */
83 public static function secondary_role_display( $profileuser ) {
84
85 // Bail if current user cannot edit users
86 if ( ! current_user_can( 'edit_user', $profileuser->ID ) ) {
87 return;
88 }
89
90 // Get the roles
91 $dynamic_roles = bbp_get_dynamic_roles();
92
93 // Only keymasters can set other keymasters
94 if ( ! bbp_is_user_keymaster() ) {
95 unset( $dynamic_roles[ bbp_get_keymaster_role() ] );
96 } ?>
97
98 <h2><?php esc_html_e( 'Forums', 'bbpress' ); ?></h2>
99
100 <table class="form-table">
101 <tbody>
102 <tr>
103 <th><label for="bbp-forums-role"><?php esc_html_e( 'Forum Role', 'bbpress' ); ?></label></th>
104 <td>
105
106 <?php $user_role = bbp_get_user_role( $profileuser->ID ); ?>
107
108 <select name="bbp-forums-role" id="bbp-forums-role">
109
110 <?php if ( ! empty( $user_role ) ) : ?>
111
112 <option value=""><?php esc_html_e( '&mdash; No role for these forums &mdash;', 'bbpress' ); ?></option>
113
114 <?php else : ?>
115
116 <option value="" selected="selected"><?php esc_html_e( '&mdash; No role for these forums &mdash;', 'bbpress' ); ?></option>
117
118 <?php endif; ?>
119
120 <?php foreach ( $dynamic_roles as $role => $details ) : ?>
121
122 <option <?php selected( $user_role, $role ); ?> value="<?php echo esc_attr( $role ); ?>"><?php echo bbp_translate_user_role( $details['name'] ); ?></option>
123
124 <?php endforeach; ?>
125
126 </select>
127 </td>
128 </tr>
129
130 </tbody>
131 </table>
132
133 <?php
134 }
135
136 /**
137 * Add bulk forums role dropdown to the WordPress users table.
138 *
139 * @since 2.2.0 bbPress (r4360)
140 * @since 2.6.0 bbPress (r6055) Introduced the `$which` parameter.
141 *
142 * @param string $which The location of the extra table nav markup: 'top' or 'bottom'.
143 */
144 public static function user_role_bulk_dropdown( $which ) {
145
146 // Remove the bottom list table "change forum role" dropdown from WordPress < 4.6.
147 // See https://bbpress.trac.wordpress.org/ticket/2906.
148 if ( bbp_get_major_wp_version() < 4.6 ) {
149 remove_action( 'restrict_manage_users', array( __CLASS__, 'user_role_bulk_dropdown' ) );
150 }
151
152 // Bail if current user cannot promote users
153 if ( ! current_user_can( 'promote_users' ) ) {
154 return;
155 }
156
157 // Get the roles
158 $dynamic_roles = bbp_get_dynamic_roles();
159
160 // Only keymasters can set other keymasters
161 if ( ! bbp_is_user_keymaster() ) {
162 unset( $dynamic_roles[ bbp_get_keymaster_role() ] );
163 }
164
165 $select_id = 'bottom' === $which ? 'bbp-new-role2' : 'bbp-new-role';
166 $button_id = 'bottom' === $which ? 'bbp-change-role2' : 'bbp-change-role';
167 ?>
168
169 <label class="screen-reader-text" for="<?php echo $select_id; ?>"><?php esc_html_e( 'Change forum role to&hellip;', 'bbpress' ); ?></label>
170 <select name="<?php echo $select_id; ?>" id="<?php echo $select_id; ?>" style="display:inline-block; float:none;">
171 <option value=''><?php esc_html_e( 'Change forum role to&hellip;', 'bbpress' ); ?></option>
172 <?php foreach ( $dynamic_roles as $role => $details ) : ?>
173 <option value="<?php echo esc_attr( $role ); ?>"><?php echo bbp_translate_user_role( $details['name'] ); ?></option>
174 <?php endforeach; ?>
175 </select><?php submit_button( esc_html__( 'Change', 'bbpress' ), 'secondary', $button_id, false );
176
177 wp_nonce_field( 'bbp-bulk-users', 'bbp-bulk-users-nonce' );
178 }
179
180 /**
181 * Process bulk dropdown form submission from the WordPress Users Table.
182 *
183 * @since 2.2.0 bbPress (r4365)
184 */
185 public function user_role_bulk_change() {
186
187 // Bail if no users specified
188 if ( empty( $_REQUEST['users'] ) ) {
189 return;
190 }
191
192 // Bail if this isn't a bbPress action
193 if ( ( empty( $_REQUEST['bbp-new-role'] ) && empty( $_REQUEST['bbp-new-role2'] ) ) || ( empty( $_REQUEST['bbp-change-role'] ) && empty( $_REQUEST['bbp-change-role2'] ) ) ) {
194 return;
195 }
196
197 $new_role = false;
198 if ( ! empty( $_REQUEST['bbp-change-role2'] ) && ! empty( $_REQUEST['bbp-new-role2'] ) ) {
199 $new_role = $_REQUEST['bbp-new-role2'];
200 } elseif ( ! empty( $_REQUEST['bbp-change-role'] ) && ! empty( $_REQUEST['bbp-new-role'] ) ) {
201 $new_role = $_REQUEST['bbp-new-role'];
202 }
203
204 // Check that the new role exists
205 $dynamic_roles = bbp_get_dynamic_roles();
206 if ( ! $new_role || empty( $dynamic_roles[ $new_role ] ) ) {
207 return;
208 }
209
210 // Bail if nonce check fails
211 check_admin_referer( 'bbp-bulk-users', 'bbp-bulk-users-nonce' );
212
213 // Bail if current user cannot promote users
214 if ( ! current_user_can( 'promote_users' ) ) {
215 return;
216 }
217
218 // Get the current user ID
219 $current_user_id = (int) bbp_get_current_user_id();
220
221 // Run through user ids
222 foreach ( (array) $_REQUEST['users'] as $user_id ) {
223 $user_id = (int) $user_id;
224
225 // Don't let a user change their own role
226 if ( $user_id === $current_user_id ) {
227 continue;
228 }
229
230 // Set up user and role data
231 $user_role = bbp_get_user_role( $user_id );
232 $new_role = sanitize_text_field( $new_role );
233
234 // Only keymasters can set other keymasters
235 if ( in_array( bbp_get_keymaster_role(), array( $user_role, $new_role ), true ) && ! bbp_is_user_keymaster() ) {
236 continue;
237 }
238
239 // Set the new forums role
240 if ( $new_role !== $user_role ) {
241 bbp_set_user_role( $user_id, $new_role );
242 }
243 }
244 }
245
246 /**
247 * Add a "View" link for each user.
248 *
249 * @since 2.6.0 bbPress (r6502)
250 *
251 * @param array $actions
252 * @param WP_User $user
253 *
254 * @return array Actions with 'view' link added to them.
255 */
256 public function user_row_actions( $actions = array(), $user = false ) {
257
258 // Reverse
259 $actions = array_reverse( $actions );
260
261 // Add the view action link
262 $actions['view'] = '<a href="' . esc_url( bbp_get_user_profile_url( $user->ID ) ) . '" class="bbp-user-profile-link">' . esc_html__( 'View', 'bbpress' ) . '</a>';
263
264 // Re-reverse
265 return array_reverse( $actions );
266 }
267
268 /**
269 * Add Forum Role column to the WordPress Users table, and change the
270 * core role title to "Site Role".
271 *
272 * @since 2.2.0 bbPress (r4337)
273 *
274 * @param array $columns Users table columns.
275 * @return array $columns
276 */
277 public static function user_role_column( $columns = array() ) {
278
279 // New title for old Role column
280 $columns['role'] = esc_html__( 'Site Role', 'bbpress' );
281
282 // New column
283 $bbp_user_role = array(
284 'bbp_user_role' => esc_html__( 'Forum Role', 'bbpress' )
285 );
286
287 // Make sure role columns are next to each other
288 $role_pos = array_search( 'role', array_keys( $columns ), true );
289 $result = array_slice( $columns, 0, $role_pos + 1 );
290 $result = array_merge( $result, $bbp_user_role );
291
292 // Merge and return
293 return array_merge( $result, array_slice( $columns, $role_pos ) );
294 }
295
296 /**
297 * Return user's forums role for display in the WordPress Users list table.
298 *
299 * @since 2.2.0 bbPress (r4337)
300 *
301 * @param string $retval
302 * @param string $column_name
303 * @param int $user_id
304 *
305 * @return string Displayable bbPress user role.
306 */
307 public static function user_role_row( $retval = '', $column_name = '', $user_id = 0 ) {
308
309 // User role column
310 if ( 'bbp_user_role' === $column_name ) {
311
312 // Get the users role
313 $user_role = bbp_get_user_role( $user_id );
314 $retval = false;
315
316 // Translate user role for display
317 if ( ! empty( $user_role ) ) {
318 $roles = bbp_get_dynamic_roles();
319 $retval = bbp_translate_user_role( $roles[ $user_role ]['name'] );
320 }
321 }
322
323 // Pass retval through
324 return $retval;
325 }
326
327 /**
328 * Filter the list of roles included in the WordPress site role list.
329 *
330 * Ensures forum roles are only displayed under the Forum Role list in the
331 * WordPress Users list table.
332 *
333 * @since 2.6.0 bbPress (r6051)
334 *
335 * @return array $roles
336 */
337 public static function user_role_list_filter( $roles, $user ) {
338
339 // Get the users role
340 $user_role = bbp_get_user_role( $user->ID );
341
342 if ( ! empty( $user_role ) ) {
343 unset( $roles[ $user_role ] );
344 }
345
346 return $roles;
347 }
348 }
349 new BBP_Users_Admin();
350 endif; // class exists
351