PluginProbe
Groups – Memberships and Access Control / 1.1.5
Groups – Memberships and Access Control v1.1.5
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.5, at lib/admin/class-groups-admin-users.php

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