PluginProbe
Groups – Memberships and Access Control / 1.11.0
Groups – Memberships and Access Control v1.11.0
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-user-profile.php

class-groups-admin-user-profile.php in Groups – Memberships and Access Control 1.11.0, at lib/admin/class-groups-admin-user-profile.php

167 lines 5.4 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-user-profile.php
4 *
5 * Copyright (c) 2013 "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.3.11
20 */
21
22 if ( !defined( 'ABSPATH' ) ) {
23 exit;
24 }
25
26 /**
27 * Show group info on user profile pages and let admins edit group membership.
28 */
29 class Groups_Admin_User_Profile {
30
31 /**
32 * Adds user profile actions.
33 */
34 public static function init() {
35 add_action( 'show_user_profile', array( __CLASS__, 'show_user_profile' ) );
36 add_action( 'edit_user_profile', array( __CLASS__, 'edit_user_profile' ) );
37 add_action( 'personal_options_update', array( __CLASS__, 'personal_options_update' ) );
38 add_action( 'edit_user_profile_update', array( __CLASS__, 'edit_user_profile_update' ) );
39 add_action( 'admin_enqueue_scripts', array( __CLASS__, 'admin_enqueue_scripts' ) );
40 }
41
42 /**
43 * Enqueues the select script on the user-edit and profile screens.
44 */
45 public static function admin_enqueue_scripts() {
46 $screen = get_current_screen();
47 if ( isset( $screen->id ) ) {
48 switch( $screen->id ) {
49 case 'user-edit' :
50 case 'profile' :
51 require_once GROUPS_VIEWS_LIB . '/class-groups-uie.php';
52 Groups_UIE::enqueue( 'select' );
53 break;
54 }
55 }
56 }
57
58 /**
59 * Own profile.
60 * @param WP_User $user
61 */
62 public static function show_user_profile( $user ) {
63 if ( current_user_can( GROUPS_ADMINISTER_GROUPS ) ) {
64 self::edit_user_profile( $user );
65 } else {
66 $output = '<h3>' . __( 'Groups', GROUPS_PLUGIN_DOMAIN ) . '</h3>';
67 $user = new Groups_User( $user->ID );
68 $groups = $user->groups;
69 if ( is_array( $groups ) ) {
70 if ( count( $groups ) > 0 ) {
71 usort( $groups, array( __CLASS__, 'by_group_name' ) );
72 $output .= '<ul>';
73 foreach( $groups as $group ) {
74 $output .= '<li>' . wp_filter_nohtml_kses( $group->name ) . '</li>';
75 }
76 $output .= '</ul>';
77 }
78 }
79 echo $output;
80 }
81 }
82
83 /**
84 * Editing a user profile.
85 * @param WP_User $user
86 */
87 public static function edit_user_profile( $user ) {
88 global $wpdb;
89 if ( current_user_can( GROUPS_ADMINISTER_GROUPS ) ) {
90 $output = '<h3>' . __( 'Groups', GROUPS_PLUGIN_DOMAIN ) . '</h3>';
91 $user = new Groups_User( $user->ID );
92 $user_groups = $user->groups;
93 $groups_table = _groups_get_tablename( 'group' );
94 if ( $groups = $wpdb->get_results( "SELECT * FROM $groups_table ORDER BY name" ) ) {
95 $output .= '<style type="text/css">';
96 $output .= '.groups .selectize-input { font-size: inherit; }';
97 $output .= '</style>';
98 $output .= sprintf(
99 '<select id="user-groups" class="groups" name="group_ids[]" multiple="multiple" placeholder="%s" data-placeholder="%s">',
100 esc_attr( __( 'Choose groups &hellip;', GROUPS_PLUGIN_DOMAIN ) ) ,
101 esc_attr( __( 'Choose groups &hellip;', GROUPS_PLUGIN_DOMAIN ) )
102 );
103 foreach( $groups as $group ) {
104 $is_member = Groups_User_Group::read( $user->ID, $group->group_id ) ? true : false;
105 $output .= sprintf( '<option value="%d" %s>%s</option>', Groups_Utility::id( $group->group_id ), $is_member ? ' selected="selected" ' : '', wp_filter_nohtml_kses( $group->name ) );
106 }
107 $output .= '</select>';
108 $output .= Groups_UIE::render_select( '#user-groups' );
109 $output .= '<p class="description">' . __( 'The user is a member of the chosen groups.', GROUPS_PLUGIN_DOMAIN ) . '</p>';
110 }
111 echo $output;
112 }
113 }
114
115 /**
116 * Updates the group membership when a user's own profile is saved - but
117 * for group admins on their own profile page only.
118 *
119 * @param int $user_id
120 * @see Groups_Admin_User_Profile::edit_user_profile_update()
121 */
122 public static function personal_options_update( $user_id ) {
123 // We're using the same method as for editing another user's profile,
124 // but let's check for group admin here as well.
125 if ( current_user_can( GROUPS_ADMINISTER_GROUPS ) ) {
126 self::edit_user_profile_update( $user_id );
127 }
128 }
129
130 /**
131 * Updates the group membership.
132 * @param int $user_id
133 */
134 public static function edit_user_profile_update( $user_id ) {
135 global $wpdb;
136 if ( current_user_can( GROUPS_ADMINISTER_GROUPS ) ) {
137 $groups_table = _groups_get_tablename( 'group' );
138 if ( $groups = $wpdb->get_results( "SELECT * FROM $groups_table" ) ) {
139 $user_group_ids = isset( $_POST['group_ids'] ) && is_array( $_POST['group_ids'] ) ? $_POST['group_ids'] : array();
140 foreach( $groups as $group ) {
141 if ( in_array( $group->group_id, $user_group_ids ) ) {
142 if ( !Groups_User_Group::read( $user_id, $group->group_id ) ) {
143 Groups_User_Group::create( array( 'user_id' => $user_id, 'group_id' => $group->group_id ) );
144 }
145 } else {
146 if ( Groups_User_Group::read( $user_id, $group->group_id ) ) {
147 Groups_User_Group::delete( $user_id, $group->group_id );
148 }
149 }
150 }
151 }
152 }
153 }
154
155 /**
156 * usort helper
157 * @param Groups_Group $o1
158 * @param Groups_Group $o2
159 * @return int strcmp result for group names
160 */
161 public static function by_group_name( $o1, $o2 ) {
162 return strcmp( $o1->name, $o2->name );
163 }
164
165 }
166 Groups_Admin_User_Profile::init();
167