PluginProbe
bbPress / 2.6.19
bbPress v2.6.19
2.6.19 2.6.18 2.6.17 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 All 74 releases
bbpress / includes / admin / users.php

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

352 lines 10.2 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 (r3095)
20 */
21 class BBP_Users_Admin {
22
23 /**
24 * The bbPress users admin loader
25 *
26 * @since 2.0.0 bbPress (r3095)
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 (r3376)
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 // Remvove 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
70 /**
71 * Default interface for setting a forum role
72 *
73 * @since 2.2.0 bbPress (r4301)
74 * @since 2.6.19 bbPress (r7686) Show only assignable roles.
75 *
76 * @param WP_User $profileuser User data
77 * @return bool Always false
78 */
79 public static function secondary_role_display( $profileuser ) {
80
81 // Bail if current user cannot edit users
82 if ( ! current_user_can( 'edit_user', $profileuser->ID ) ) {
83 return;
84 }
85
86 // Get the roles this user may assign.
87 $dynamic_roles = bbp_get_user_editable_forum_roles( $profileuser->ID ); ?>
88
89 <h2><?php esc_html_e( 'Forums', 'bbpress' ); ?></h2>
90
91 <table class="form-table">
92 <tbody>
93 <tr>
94 <th><label for="bbp-forums-role"><?php esc_html_e( 'Forum Role', 'bbpress' ); ?></label></th>
95 <td>
96
97 <?php $user_role = bbp_get_user_role( $profileuser->ID ); ?>
98
99 <select name="bbp-forums-role" id="bbp-forums-role">
100
101 <?php if ( ! empty( $user_role ) ) : ?>
102
103 <option value=""><?php esc_html_e( '&mdash; No role for these forums &mdash;', 'bbpress' ); ?></option>
104
105 <?php else : ?>
106
107 <option value="" selected="selected"><?php esc_html_e( '&mdash; No role for these forums &mdash;', 'bbpress' ); ?></option>
108
109 <?php endif; ?>
110
111 <?php foreach ( $dynamic_roles as $role => $details ) : ?>
112
113 <option <?php selected( $user_role, $role ); ?> value="<?php echo esc_attr( $role ); ?>"><?php echo bbp_translate_user_role( $details['name'] ); ?></option>
114
115 <?php endforeach; ?>
116
117 </select>
118 </td>
119 </tr>
120
121 </tbody>
122 </table>
123
124 <?php
125 }
126
127 /**
128 * Add bulk forums role dropdown to the WordPress users table
129 *
130 * @since 2.2.0 bbPress (r4365)
131 * @since 2.6.0 bbPress (r6055) Introduced the `$which` parameter.
132 * @since 2.6.19 bbPress (r7686) Show only assignable roles.
133 *
134 * @param string $which The location of the extra table nav markup: 'top' or 'bottom'.
135 */
136 public static function user_role_bulk_dropdown( $which ) {
137
138 // Remove the bottom list table "change forum role" dropdown from WordPress < 4.6.
139 // See https://bbpress.trac.wordpress.org/ticket/2906.
140 if ( bbp_get_major_wp_version() < 4.6 ) {
141 remove_action( 'restrict_manage_users', array( __CLASS__, 'user_role_bulk_dropdown' ) );
142 }
143
144 // Bail if current user cannot promote users
145 if ( ! current_user_can( 'promote_users' ) ) {
146 return;
147 }
148
149 // Get the roles
150 $dynamic_roles = bbp_get_user_editable_forum_roles();
151
152 $select_id = 'bottom' === $which ? 'bbp-new-role2' : 'bbp-new-role';
153 $button_id = 'bottom' === $which ? 'bbp-change-role2' : 'bbp-change-role';
154 ?>
155
156 <label class="screen-reader-text" for="<?php echo $select_id; ?>"><?php esc_html_e( 'Change forum role to&hellip;', 'bbpress' ); ?></label>
157 <select name="<?php echo $select_id; ?>" id="<?php echo $select_id; ?>" style="display:inline-block; float:none;">
158 <option value=''><?php esc_html_e( 'Change forum role to&hellip;', 'bbpress' ); ?></option>
159 <?php foreach ( $dynamic_roles as $role => $details ) : ?>
160 <option value="<?php echo esc_attr( $role ); ?>"><?php echo bbp_translate_user_role( $details['name'] ); ?></option>
161 <?php endforeach; ?>
162 </select><?php submit_button( esc_html__( 'Change', 'bbpress' ), 'secondary', $button_id, false );
163
164 wp_nonce_field( 'bbp-bulk-users', 'bbp-bulk-users-nonce' );
165 }
166
167 /**
168 * Process bulk dropdown form submission from the WordPress Users
169 * Table
170 *
171 * @since 2.2.0 bbPress (r4365)
172 * @since 2.6.19 bbPress (r7686) Check each user and assignable role.
173 *
174 * @return bool Always false
175 */
176 public function user_role_bulk_change() {
177
178 // Bail if no users specified
179 if ( empty( $_REQUEST['users'] ) ) {
180 return;
181 }
182
183 // Bail if this isn't a bbPress action
184 if ( ( empty( $_REQUEST['bbp-new-role'] ) && empty( $_REQUEST['bbp-new-role2'] ) ) || ( empty( $_REQUEST['bbp-change-role'] ) && empty( $_REQUEST['bbp-change-role2'] ) ) ) {
185 return;
186 }
187
188 $new_role = false;
189 if ( ! empty( $_REQUEST['bbp-change-role2'] ) && ! empty( $_REQUEST['bbp-new-role2'] ) ) {
190 $new_role = $_REQUEST['bbp-new-role2'];
191 } elseif ( ! empty( $_REQUEST['bbp-change-role'] ) && ! empty( $_REQUEST['bbp-new-role'] ) ) {
192 $new_role = $_REQUEST['bbp-new-role'];
193 }
194
195 // Check that the new role exists
196 $new_role = is_string( $new_role ) ? sanitize_text_field( wp_unslash( $new_role ) ) : '';
197 $dynamic_roles = bbp_get_dynamic_roles();
198 if ( ! $new_role || empty( $dynamic_roles[ $new_role ] ) ) {
199 return;
200 }
201
202 // Bail if nonce check fails
203 check_admin_referer( 'bbp-bulk-users', 'bbp-bulk-users-nonce' );
204
205 // Bail if current user cannot promote users
206 if ( ! current_user_can( 'promote_users' ) ) {
207 return;
208 }
209
210 // Get the current user ID
211 $current_user_id = (int) bbp_get_current_user_id();
212
213 // Run through user ids
214 foreach ( (array) $_REQUEST['users'] as $user_id ) {
215 $user_id = (int) $user_id;
216
217 // Don't let a user change their own role
218 if ( $user_id === $current_user_id ) {
219 continue;
220 }
221
222 // Only change roles for users on this site who the current user may promote.
223 if ( ! is_user_member_of_blog( $user_id ) || ! bbp_current_user_can_edit_user_field( 'forum_role', $user_id ) || ! current_user_can( 'promote_user', $user_id ) ) {
224 continue;
225 }
226
227 // Set up user and role data
228 $user_role = bbp_get_user_role( $user_id );
229
230 // Check the roles allowed for this target user.
231 if ( ! array_key_exists( $new_role, bbp_get_user_editable_forum_roles( $user_id ) ) ) {
232 continue;
233 }
234
235 // Only keymasters and site administrators can change keymaster roles.
236 if ( in_array( bbp_get_keymaster_role(), array( $user_role, $new_role ), true ) && ! bbp_is_user_keymaster() && ! current_user_can( 'manage_options' ) ) {
237 continue;
238 }
239
240 // Set the new forums role
241 if ( $new_role !== $user_role ) {
242 bbp_set_user_role( $user_id, $new_role );
243 }
244 }
245 }
246
247 /**
248 * Add a "View" link for each user
249 *
250 * @since 2.6.0 bbPress (r6502)
251 *
252 * @param array $actions
253 * @param WP_User $user
254 *
255 * @return array Actions with 'view' link added to them
256 */
257 public function user_row_actions( $actions = array(), $user = false ) {
258
259 // Reverse
260 $actions = array_reverse( $actions );
261
262 // Add the view action link
263 $actions['view'] = '<a href="' . esc_url( bbp_get_user_profile_url( $user->ID ) ) . '" class="bbp-user-profile-link">' . esc_html__( 'View', 'bbpress' ) . '</a>';
264
265 // Re-reverse
266 return array_reverse( $actions );
267 }
268
269 /**
270 * Add Forum Role column to the WordPress Users table, and change the
271 * core role title to "Site Role"
272 *
273 * @since 2.2.0 bbPress (r4338)
274 *
275 * @param array $columns Users table columns
276 * @return array $columns
277 */
278 public static function user_role_column( $columns = array() ) {
279
280 // New title for old Role column
281 $columns['role'] = esc_html__( 'Site Role', 'bbpress' );
282
283 // New column
284 $bbp_user_role = array(
285 'bbp_user_role' => esc_html__( 'Forum Role', 'bbpress' )
286 );
287
288 // Make sure role columns are next to each other
289 $role_pos = array_search( 'role', array_keys( $columns ), true );
290 $result = array_slice( $columns, 0, $role_pos + 1 );
291 $result = array_merge( $result, $bbp_user_role );
292
293 // Merge and return
294 return array_merge( $result, array_slice( $columns, $role_pos ) );
295 }
296
297 /**
298 * Return user's forums role for display in the WordPress Users list table
299 *
300 * @since 2.2.0 bbPress (r4338)
301 *
302 * @param string $retval
303 * @param string $column_name
304 * @param int $user_id
305 *
306 * @return string Displayable bbPress user role
307 */
308 public static function user_role_row( $retval = '', $column_name = '', $user_id = 0 ) {
309
310 // User role column
311 if ( 'bbp_user_role' === $column_name ) {
312
313 // Get the users role
314 $user_role = bbp_get_user_role( $user_id );
315 $retval = false;
316
317 // Translate user role for display
318 if ( ! empty( $user_role ) ) {
319 $roles = bbp_get_dynamic_roles();
320 $retval = bbp_translate_user_role( $roles[ $user_role ]['name'] );
321 }
322 }
323
324 // Pass retval through
325 return $retval;
326 }
327
328 /**
329 * Filter the list of roles included in the WordPress site role list
330 *
331 * Ensures forum roles are only displayed under the Forum Role list in the
332 * WordPress Users list table
333 *
334 * @since 2.6.0 bbPress (r6052)
335 *
336 * @return array $roles
337 */
338 public static function user_role_list_filter( $roles, $user ) {
339
340 // Get the users role
341 $user_role = bbp_get_user_role( $user->ID );
342
343 if ( ! empty( $user_role ) ) {
344 unset( $roles[ $user_role ] );
345 }
346
347 return $roles;
348 }
349 }
350 new BBP_Users_Admin();
351 endif; // class exists
352