PluginProbe
Groups – Memberships and Access Control / 1.1.4
Groups – Memberships and Access Control v1.1.4
4.7.1 4.7.0 4.6.0 4.5.0 4.4.0 4.3.0 trunk 1.0.0-beta-1 1.0.0-beta-2 1.0.0-beta-3 1.0.0-beta-3b 1.0.0-beta-3c 1.0.0-beta-3d 1.1.4 1.1.5 1.10.0 1.10.1 1.10.2 1.10.3 1.11.0 1.11.1 1.11.2 1.11.3 1.12.0 1.13.0 All 131 releases
groups / lib / admin / class-groups-admin-users.php

class-groups-admin-users.php in Groups – Memberships and Access Control 1.1.4, at lib/admin/class-groups-admin-users.php

187 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 class Groups_Admin_Users {
22
23 const GROUPS = 'groups_user_groups';
24
25 /**
26 * Hooks into filters to add the Groups column to the users table.
27 */
28 public static function init() {
29 // we hook this on admin_init so that current_user_can() is available
30 add_action( 'admin_init', array( __CLASS__, 'setup' ) );
31 }
32
33 /**
34 * Adds the filters and actions only for users who have the right
35 * Groups permissions.
36 */
37 public static function setup() {
38 if ( current_user_can( GROUPS_ACCESS_GROUPS ) ) {
39 // filters to display the user's groups
40 add_filter( 'manage_users_columns', array( __CLASS__, 'manage_users_columns' ) );
41 // args: unknown, string $column_name, int $user_id
42 add_filter( 'manage_users_custom_column', array( __CLASS__, 'manage_users_custom_column' ), 10, 3 );
43 }
44 if ( current_user_can( GROUPS_ADMINISTER_GROUPS ) ) {
45 if ( !is_network_admin() ) {
46 add_action( 'admin_head', array( __CLASS__, 'admin_head' ) );
47 // allow to add or remove selected users to groups
48 add_action( 'load-users.php', array( __CLASS__, 'load_users' ) );
49 }
50 }
51 }
52
53 /**
54 * Adds the group add/remove buttons after the last action box.
55 */
56 public static function admin_head() {
57 global $pagenow, $wpdb;
58 if ( ( $pagenow == 'users.php' ) && empty( $_GET['page'] ) ) {
59
60 $group_table = _groups_get_tablename( "group" );
61 // groups select
62 $groups_select = "<select name='group_id'>";
63 $groups = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $group_table ORDER BY name" ) );
64 foreach( $groups as $group ) {
65 $groups_select .= "<option value='" . esc_attr( $group->group_id ) . "'>" . wp_filter_nohtml_kses( $group->name ) . "</option>";
66 }
67 $groups_select .= "</select>";
68
69 // we add this inside the form that contains the bulk
70 // action and role change buttons
71 $box = "<div class='alignleft actions' style='padding-left:1em'>";
72 $box .= "<input class='button' type='submit' name='add-to-group' value='" . __( "Add", GROUPS_PLUGIN_DOMAIN ) . "'/>";
73 $box .= "&nbsp;/&nbsp;";
74 $box .= "<input class='button' type='submit' name='remove-from-group' value='" . __( "Remove", GROUPS_PLUGIN_DOMAIN ) . "'/>";
75 $box .= "&nbsp;";
76 $box .= __( 'selected to / from group:', GROUPS_PLUGIN_DOMAIN );
77 $box .= "&nbsp;";
78 $box .= "<label class='screen-reader-text' for='group_id'>" . __( 'Group', GROUPS_PLUGIN_DOMAIN ) . "</label>";
79 $box .= $groups_select;
80 $box .= "</div>";
81
82 $nonce = wp_nonce_field( 'user-group', 'bulk-user-group-nonce', true, false );
83 $nonce = str_replace('"', "'", $nonce );
84 $box .= $nonce;
85
86 // @todo replace when a hook allows us to add actions to the users table
87 // Another option is to extend the users table and implement it in extra_tablenav()
88 // but that would require either to replace the users admin screen with
89 // that extended one or create our own section, e.g. Groups > Users.
90 echo '<script type="text/javascript">';
91 echo '
92 jQuery(function(){
93 jQuery(".tablenav.top .alignleft.actions:last").after("' . $box . '");
94 });
95 ';
96 echo '</script>';
97
98 }
99 }
100
101 /**
102 * Adds or removes users to/from groups.
103 */
104 public static function load_users() {
105 if ( current_user_can( GROUPS_ADMINISTER_GROUPS ) ) {
106 $group_id = isset( $_REQUEST['group_id'] ) ? $_REQUEST['group_id'] : null;
107 $users = isset( $_REQUEST['users'] ) ? $_REQUEST['users'] : null;
108 $action = null;
109 if ( !empty( $_REQUEST['add-to-group'] ) ) {
110 $action = 'add';
111 } else if ( !empty( $_REQUEST['remove-from-group'] ) ) {
112 $action = 'remove';
113 }
114 if ( $group_id !== null && $users !== null && $action !== null ) {
115 if ( wp_verify_nonce( $_REQUEST['bulk-user-group-nonce'], 'user-group' ) ) {
116 foreach( $users as $user_id ) {
117 switch ( $action ) {
118 case 'add':
119 if ( !Groups_User_Group::read( $user_id, $group_id ) ) {
120 Groups_User_Group::create(
121 array(
122 'user_id' => $user_id,
123 'group_id' => $group_id
124 )
125 );
126 }
127 break;
128 case 'remove':
129 if ( Groups_User_Group::read( $user_id, $group_id ) ) {
130 Groups_User_Group::delete( $user_id, $group_id );
131 }
132 break;
133 }
134 }
135 }
136 }
137 }
138 }
139
140 /**
141 * Adds a new column to the users table to show the groups that users
142 * belong to.
143 *
144 * @param array $column_headers
145 * @return array column headers
146 */
147 public static function manage_users_columns( $column_headers ) {
148 $column_headers[self::GROUPS] = __( 'Groups', GROUPS_PLUGIN_DOMAIN );
149 return $column_headers;
150 }
151
152 /**
153 * Renders custom column content.
154 *
155 * $foo : both filter invocations in class-wp-ms-users-list-table.php and
156 * class-wp-users-list-table.php pass ''
157 *
158 * @param unknown_type $foo
159 * @param string $column_name
160 * @param int $user_id
161 * @return string custom column content
162 */
163 public static function manage_users_custom_column( $foo, $column_name, $user_id ) {
164 $output = '';
165 switch ( $column_name ) {
166 case self::GROUPS :
167 $groups_user = new Groups_User( $user_id );
168 $groups = $groups_user->groups;
169 if ( count( $groups ) > 0 ) {
170 $output .= '<ul>';
171 foreach( $groups as $group ) {
172 $output .= '<li>';
173 $output .= wp_filter_nohtml_kses( $group->name );
174 $output .= '</li>';
175 }
176 $output .= '</ul>';
177 } else {
178 $output .= __( '--' );
179 }
180 break;
181 }
182 return $output;
183 }
184
185 }
186 Groups_Admin_Users::init();
187