| 1 |
<?php |
| 2 |
/** |
| 3 |
* class-groups-admin-users.php |
| 4 |
* |
| 5 |
* Copyright (c) 2012 "kento" Karim Rahimpur www.itthinx.com |
| 6 |
* |
| 7 |
* This code is released under the GNU General Public License. |
| 8 |
* See COPYRIGHT.txt and LICENSE.txt. |
| 9 |
* |
| 10 |
* This code is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* This header and all notices must be kept intact. |
| 16 |
* |
| 17 |
* @author Karim Rahimpur |
| 18 |
* @package groups |
| 19 |
* @since groups 1.0.0 |
| 20 |
*/ |
| 21 |
|
| 22 |
if ( !defined( 'ABSPATH' ) ) { |
| 23 |
exit; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Users admin integration with Groups. |
| 28 |
*/ |
| 29 |
class Groups_Admin_Users { |
| 30 |
|
| 31 |
const GROUPS = 'groups_user_groups'; |
| 32 |
|
| 33 |
/** |
| 34 |
* Hooks into filters to add the Groups column to the users table. |
| 35 |
*/ |
| 36 |
public static function init() { |
| 37 |
// we hook this on admin_init so that current_user_can() is available |
| 38 |
add_action( 'admin_init', array( __CLASS__, 'setup' ) ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Adds the filters and actions only for users who have the right |
| 43 |
* Groups permissions. |
| 44 |
*/ |
| 45 |
public static function setup() { |
| 46 |
if ( current_user_can( GROUPS_ACCESS_GROUPS ) ) { |
| 47 |
// filters to display the user's groups |
| 48 |
add_filter( 'manage_users_columns', array( __CLASS__, 'manage_users_columns' ) ); |
| 49 |
// args: unknown, string $column_name, int $user_id |
| 50 |
add_filter( 'manage_users_custom_column', array( __CLASS__, 'manage_users_custom_column' ), 10, 3 ); |
| 51 |
} |
| 52 |
if ( current_user_can( GROUPS_ADMINISTER_GROUPS ) ) { |
| 53 |
if ( !is_network_admin() ) { |
| 54 |
// scripts |
| 55 |
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'admin_enqueue_scripts' ) ); |
| 56 |
// styles |
| 57 |
add_action( 'admin_head', array( __CLASS__, 'admin_head' ) ); |
| 58 |
// allow to add or remove selected users to groups |
| 59 |
add_action( 'load-users.php', array( __CLASS__, 'load_users' ) ); |
| 60 |
// add links to filter users by group |
| 61 |
add_filter( 'views_users', array( __CLASS__, 'views_users' ) ); |
| 62 |
// modify query to filter users by group |
| 63 |
add_filter( 'pre_user_query', array( __CLASS__, 'pre_user_query' ) ); |
| 64 |
// WP_Users_List_Table implements extra_tablenav() where the restrict_manage_users action is invoked. |
| 65 |
// As the extra_tablenav() method does not define a generic extension point, this is |
| 66 |
// the best shot we get at inserting our group actions block (currently we're at WordPress 3.6.1). |
| 67 |
// We choose to use our own group-actions block instead of re-using the existing bulk-actions, |
| 68 |
// to have a more explicit user interface which makes it clear that these actions |
| 69 |
// are directed at relating users and groups. |
| 70 |
add_action( 'restrict_manage_users', array( __CLASS__, 'restrict_manage_users' ), 0 ); |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Modify query to filter users by group. |
| 77 |
* |
| 78 |
* @param WP_User_Query $user_query |
| 79 |
* @return WP_User_Query |
| 80 |
*/ |
| 81 |
public static function pre_user_query( $user_query ) { |
| 82 |
global $pagenow, $wpdb; |
| 83 |
if ( ( $pagenow == 'users.php' ) && empty( $_GET['page'] ) ) { |
| 84 |
if ( isset( $_REQUEST['group'] ) ) { |
| 85 |
$group_id = $_REQUEST['group']; |
| 86 |
if ( Groups_Group::read( $group_id ) ) { |
| 87 |
$group = new Groups_Group( $group_id ); |
| 88 |
$users = $group->users; |
| 89 |
$include = array(); |
| 90 |
if ( count( $users ) > 0 ) { |
| 91 |
foreach( $users as $user ) { |
| 92 |
$include[] = $user->user->ID; |
| 93 |
} |
| 94 |
} else { // no results |
| 95 |
$include[] = 0; |
| 96 |
} |
| 97 |
$ids = implode( ',', wp_parse_id_list( $include ) ); |
| 98 |
$user_query->query_where .= " AND $wpdb->users.ID IN ($ids)"; |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
return $user_query; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Enqueue scripts the group-actions. |
| 107 |
*/ |
| 108 |
public static function admin_enqueue_scripts() { |
| 109 |
|
| 110 |
global $pagenow; |
| 111 |
|
| 112 |
if ( ( $pagenow == 'users.php' ) && empty( $_GET['page'] ) ) { |
| 113 |
Groups_UIE::enqueue( 'select' ); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Adds the group add/remove buttons after the last action box. |
| 119 |
*/ |
| 120 |
public static function admin_head() { |
| 121 |
|
| 122 |
global $pagenow; |
| 123 |
|
| 124 |
if ( ( $pagenow == 'users.php' ) && empty( $_GET['page'] ) ) { |
| 125 |
|
| 126 |
// .subsubsub rule added because with views_users() the list can get long |
| 127 |
// icon distinguishes from role links |
| 128 |
echo '<style type="text/css">'; |
| 129 |
echo '.subsubsub { white-space: normal; }'; |
| 130 |
echo 'a.group { background: url(' . GROUPS_PLUGIN_URL . '/images/groups-grey-8x8.png) transparent no-repeat left center; padding-left: 10px;}'; |
| 131 |
echo '</style>'; |
| 132 |
|
| 133 |
// group-actions |
| 134 |
echo '<style type="text/css">'; |
| 135 |
echo '.groups-bulk-container { display: inline-block; line-height: 24px; padding-bottom: 1em; vertical-align: top; margin-left: 1em; margin-right: 1em; }'; |
| 136 |
echo '.groups-bulk-container .groups-select-container { display: inline-block; vertical-align: top; }'; |
| 137 |
echo '.groups-bulk-container .groups-select-container select, .groups-bulk-container select.groups-action { float: none; margin-right: 4px; vertical-align: top; }'; |
| 138 |
echo '.groups-bulk-container .selectize-control { min-width: 128px; }'; |
| 139 |
echo '.groups-bulk-container .selectize-control, .groups-bulk-container select.groups-action { margin-right: 4px; vertical-align: top; }'; |
| 140 |
echo '.groups-bulk-container .selectize-input { font-size: inherit; line-height: 18px; padding: 1px 2px 2px 2px; vertical-align: middle; }'; |
| 141 |
echo '.groups-bulk-container .selectize-input input[type="text"] { font-size: inherit; vertical-align: middle; height: 24px; }'; |
| 142 |
echo '.groups-bulk-container input.button { margin-top: 1px; vertical-align: top; }'; |
| 143 |
echo '.tablenav .actions { overflow: visible; }'; // this is important so that the selectize options aren't hidden |
| 144 |
echo '</style>'; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Renders group actions in the users table's extra_tablenav(). |
| 150 |
*/ |
| 151 |
public static function restrict_manage_users() { |
| 152 |
|
| 153 |
global $pagenow, $wpdb, $groups_select_user_groups_index; |
| 154 |
|
| 155 |
// We don't handle multiple instances so don't render another. |
| 156 |
if ( !isset( $groups_select_user_groups_index ) ) { |
| 157 |
$groups_select_user_groups_index = 0; |
| 158 |
} else { |
| 159 |
return ''; |
| 160 |
} |
| 161 |
|
| 162 |
$output = ''; |
| 163 |
|
| 164 |
if ( ( $pagenow == 'users.php' ) && empty( $_GET['page'] ) ) { |
| 165 |
$group_table = _groups_get_tablename( "group" ); |
| 166 |
// groups select |
| 167 |
$groups_table = _groups_get_tablename( 'group' ); |
| 168 |
if ( $groups = $wpdb->get_results( "SELECT * FROM $groups_table ORDER BY name" ) ) { |
| 169 |
$groups_select = sprintf( |
| 170 |
'<select id="user-groups" class="groups" name="group_ids[]" multiple="multiple" placeholder="%s" data-placeholder="%s">', |
| 171 |
esc_attr( __( 'Choose groups …', GROUPS_PLUGIN_DOMAIN ) ) , |
| 172 |
esc_attr( __( 'Choose groups …', GROUPS_PLUGIN_DOMAIN ) ) |
| 173 |
); |
| 174 |
foreach( $groups as $group ) { |
| 175 |
$is_member = false; |
| 176 |
$groups_select .= sprintf( '<option value="%d" %s>%s</option>', Groups_Utility::id( $group->group_id ), $is_member ? ' selected="selected" ' : '', wp_filter_nohtml_kses( $group->name ) ); |
| 177 |
} |
| 178 |
$groups_select .= '</select>'; |
| 179 |
|
| 180 |
} |
| 181 |
|
| 182 |
// group bulk actions added through extra_tablenav() |
| 183 |
$box = '<div id="group-bulk-actions" class="groups-bulk-container">'; |
| 184 |
$box .= '<div class="groups-select-container">'; |
| 185 |
$box .= $groups_select; |
| 186 |
$box .= '</div>'; |
| 187 |
$box .= '<select class="groups-action" name="groups-action">'; |
| 188 |
$box .= '<option selected="selected" value="-1">' . __( 'Group Actions', GROUPS_PLUGIN_DOMAIN ) . '</option>'; |
| 189 |
$box .= '<option value="add-group">' . __( 'Add to group', GROUPS_PLUGIN_DOMAIN ) . '</option>'; |
| 190 |
$box .= '<option value="remove-group">' . __( 'Remove from group', GROUPS_PLUGIN_DOMAIN ) . '</option>'; |
| 191 |
$box .= '</select>'; |
| 192 |
$box .= sprintf( '<input class="button" type="submit" name="groups" value="%s" />', __( 'Apply', GROUPS_PLUGIN_DOMAIN ) ); |
| 193 |
$box .= '</div>'; |
| 194 |
$box = str_replace( '"', "'", $box ); |
| 195 |
|
| 196 |
$nonce = wp_nonce_field( 'user-group', 'bulk-user-group-nonce', true, false ); |
| 197 |
$nonce = str_replace( '"', "'", $nonce ); |
| 198 |
$box .= $nonce; |
| 199 |
|
| 200 |
$box .= '<script type="text/javascript">'; |
| 201 |
$box .= 'if ( typeof jQuery !== "undefined" ) {'; |
| 202 |
$box .= 'jQuery("document").ready(function(){'; |
| 203 |
$box .= 'jQuery(".tablenav.top .alignleft.actions:last").after("<div id=\"groups-bulk-actions-block\" class=\"alignleft actions\"></div>");'; |
| 204 |
$box .= 'jQuery("#group-bulk-actions").appendTo(jQuery("#groups-bulk-actions-block"));'; |
| 205 |
$box .= '});'; |
| 206 |
$box .= '}'; |
| 207 |
$box .= '</script>'; |
| 208 |
|
| 209 |
$output .= $box; |
| 210 |
$output .= Groups_UIE::render_select( '#user-groups' ); |
| 211 |
} |
| 212 |
echo $output; |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Hooked on filter in class-wp-list-table.php to add links that |
| 217 |
* filter by group. |
| 218 |
* @param array $views |
| 219 |
*/ |
| 220 |
public static function views_users( $views ) { |
| 221 |
global $pagenow, $wpdb; |
| 222 |
if ( ( $pagenow == 'users.php' ) && empty( $_GET['page'] ) ) { |
| 223 |
$group_table = _groups_get_tablename( "group" ); |
| 224 |
$user_group_table = _groups_get_tablename( "user_group" ); |
| 225 |
$groups = $wpdb->get_results( "SELECT * FROM $group_table ORDER BY name" ); |
| 226 |
foreach( $groups as $group ) { |
| 227 |
$group = new Groups_Group( $group->group_id ); |
| 228 |
// Do not use $user_count = count( $group->users ); here, |
| 229 |
// as it creates a lot of unneccessary objects and can lead |
| 230 |
// to out of memory issues on large user bases. |
| 231 |
$user_count = $wpdb->get_var( $wpdb->prepare( |
| 232 |
"SELECT COUNT(user_id) FROM $user_group_table WHERE group_id = %d", |
| 233 |
Groups_Utility::id( $group->group_id ) ) ); |
| 234 |
$views[] = sprintf( |
| 235 |
'<a class="group" href="%s" title="%s">%s</a>', |
| 236 |
esc_url( add_query_arg( 'group', $group->group_id, admin_url( 'users.php' ) ) ), |
| 237 |
sprintf( '%s Group', wp_filter_nohtml_kses( $group->name ) ), |
| 238 |
sprintf( '%s <span class="count">(%s)</span>', wp_filter_nohtml_kses( $group->name ), $user_count ) |
| 239 |
); |
| 240 |
} |
| 241 |
} |
| 242 |
return $views; |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* Adds or removes users to/from groups. |
| 247 |
*/ |
| 248 |
public static function load_users() { |
| 249 |
if ( current_user_can( GROUPS_ADMINISTER_GROUPS ) ) { |
| 250 |
$users = isset( $_REQUEST['users'] ) ? $_REQUEST['users'] : null; |
| 251 |
$action = null; |
| 252 |
if ( !empty( $_REQUEST['groups'] ) ) { |
| 253 |
if ( $_GET['groups-action'] == "add-group" ) { |
| 254 |
$action = 'add'; |
| 255 |
} else if ( $_GET['groups-action'] == "remove-group" ) { |
| 256 |
$action = 'remove'; |
| 257 |
} |
| 258 |
} |
| 259 |
if ( $users !== null && $action !== null ) { |
| 260 |
if ( wp_verify_nonce( $_REQUEST['bulk-user-group-nonce'], 'user-group' ) ) { |
| 261 |
foreach( $users as $user_id ) { |
| 262 |
switch ( $action ) { |
| 263 |
case 'add': |
| 264 |
$group_ids = isset( $_GET['group_ids'] ) ? $_GET['group_ids'] : null; |
| 265 |
if ( $group_ids !== null ) { |
| 266 |
foreach ( $group_ids as $group_id ) { |
| 267 |
if ( !Groups_User_Group::read( $user_id, $group_id ) ) { |
| 268 |
Groups_User_Group::create( |
| 269 |
array( |
| 270 |
'user_id' => $user_id, |
| 271 |
'group_id' => $group_id |
| 272 |
) |
| 273 |
); |
| 274 |
} |
| 275 |
} |
| 276 |
} |
| 277 |
break; |
| 278 |
case 'remove': |
| 279 |
$group_ids = isset( $_GET['group_ids'] ) ? $_GET['group_ids'] : null; |
| 280 |
if ( $group_ids !== null ) { |
| 281 |
foreach ( $group_ids as $group_id ) { |
| 282 |
if ( Groups_User_Group::read( $user_id, $group_id ) ) { |
| 283 |
Groups_User_Group::delete( $user_id, $group_id ); |
| 284 |
} |
| 285 |
} |
| 286 |
} |
| 287 |
break; |
| 288 |
} |
| 289 |
} |
| 290 |
$referer = wp_get_referer(); |
| 291 |
if ( $referer ) { |
| 292 |
$redirect_to = remove_query_arg( array( 'action', 'action2', 'add-to-group', 'bulk-user-group-nonce', 'group_id', 'new_role', 'remove-from-group', 'users' ), $referer ); |
| 293 |
wp_redirect( $redirect_to ); |
| 294 |
exit; |
| 295 |
} |
| 296 |
} |
| 297 |
} |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
/** |
| 302 |
* Adds a new column to the users table to show the groups that users |
| 303 |
* belong to. |
| 304 |
* |
| 305 |
* @param array $column_headers |
| 306 |
* @return array column headers |
| 307 |
*/ |
| 308 |
public static function manage_users_columns( $column_headers ) { |
| 309 |
$column_headers[self::GROUPS] = __( 'Groups', GROUPS_PLUGIN_DOMAIN ); |
| 310 |
return $column_headers; |
| 311 |
} |
| 312 |
|
| 313 |
/** |
| 314 |
* Renders custom column content. |
| 315 |
* |
| 316 |
* @param string $output |
| 317 |
* @param string $column_name |
| 318 |
* @param int $user_id |
| 319 |
* @return string custom column content |
| 320 |
*/ |
| 321 |
public static function manage_users_custom_column( $output, $column_name, $user_id ) { |
| 322 |
switch ( $column_name ) { |
| 323 |
case self::GROUPS : |
| 324 |
$groups_user = new Groups_User( $user_id ); |
| 325 |
$groups = $groups_user->groups; |
| 326 |
if ( count( $groups ) > 0 ) { |
| 327 |
usort( $groups, array( __CLASS__, 'by_group_name' ) ); |
| 328 |
$output = '<ul>'; |
| 329 |
foreach( $groups as $group ) { |
| 330 |
$output .= '<li>'; |
| 331 |
$output .= wp_filter_nohtml_kses( $group->name ); |
| 332 |
$output .= '</li>'; |
| 333 |
} |
| 334 |
$output .= '</ul>'; |
| 335 |
} else { |
| 336 |
$output .= __( '--', GROUPS_PLUGIN_DOMAIN ); |
| 337 |
} |
| 338 |
break; |
| 339 |
} |
| 340 |
return $output; |
| 341 |
} |
| 342 |
|
| 343 |
/** |
| 344 |
* usort helper |
| 345 |
* @param Groups_Group $o1 |
| 346 |
* @param Groups_Group $o2 |
| 347 |
* @return int strcmp result for group names |
| 348 |
*/ |
| 349 |
public static function by_group_name( $o1, $o2 ) { |
| 350 |
return strcmp( $o1->name, $o2->name ); |
| 351 |
} |
| 352 |
} |
| 353 |
Groups_Admin_Users::init(); |
| 354 |
|