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 / core / class-groups-user-group.php

class-groups-user-group.php in Groups – Memberships and Access Control 1.1.4, at lib/core/class-groups-user-group.php

269 lines 7.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * class-groups-user-group.php
4 *
5 * Copyright (c) "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_User_Group {
22
23 /**
24 * @var persisted object
25 */
26 var $user_group = null;
27
28 /**
29 * Hook into appropriate actions.
30 * @see wp_delete_user()
31 * @see remove_user_from_blog()
32 */
33 public static function init() {
34 // when a user is deleted, it must be removed from all groups it
35 // belongs to - triggered by wp_delete_user() and wpmu_delete_user()
36 add_action( "deleted_user", array( __CLASS__, "deleted_user" ) );
37
38 // when a user is removed from a blog, the user must be removed
39 // from all groups in that blog that it belongs to
40 add_action( "remove_user_from_blog", array( __CLASS__, "remove_user_from_blog" ), 10, 2 );
41 }
42
43 /**
44 * Create by user and group id.
45 * Must have been persisted.
46 * @param int $user_id
47 * @param int $group_id
48 */
49 public function __construct( $user_id, $group_id ) {
50 $this->user_group = self::read( $user_id, $group_id );
51 }
52
53 /**
54 * Retrieve a property by name.
55 *
56 * Possible properties:
57 * - user_id
58 * - group_id
59 *
60 * @param string $name property's name
61 * @return property value, will return null if property does not exist
62 */
63 public function __get( $name ) {
64 $result = null;
65 if ( $this->user_group !== null ) {
66 switch( $name ) {
67 case "user_id" :
68 case "group_id" :
69 $result = $this->user_group->$name;
70 break;
71 }
72 }
73 return $result;
74 }
75
76 /**
77 * Persist a user-group relation.
78 *
79 * @param array $map attributes - must provide user_id and group_id
80 * @return true on success, otherwise false
81 */
82 public static function create( $map ) {
83
84 global $wpdb;
85 extract( $map );
86 $result = false;
87
88 // avoid nonsense requests
89 // if ( !empty( $user_id ) && !empty( $group_id ) ) {
90 if ( !empty( $group_id ) ) {
91 // make sure user and group exist
92 if ( ( false !== Groups_Utility::id( $user_id ) ) && get_user_by( "id", $user_id ) && ( $group = Groups_Group::read( $group_id ) ) ) {
93 // only allow to add users to groups if they belong to the
94 // group's blog or we have the anonymous user
95 if ( is_user_member_of_blog( Groups_Utility::id( $user_id ) ) || ( Groups_Utility::id( $user_id ) === 0 ) ) {
96 $user_group_table = _groups_get_tablename( 'user_group' );
97 // don't try to create duplicate entries
98 // also it would raise an error for duplicate PK
99 if ( 0 === intval( $wpdb->get_var( $wpdb->prepare(
100 "SELECT COUNT(*) FROM $user_group_table WHERE user_id = %d AND group_id = %d",
101 Groups_Utility::id( $user_id ),
102 Groups_Utility::id( $group_id ) ) ) )
103 ) {
104 $data = array(
105 'user_id' => Groups_Utility::id( $user_id ),
106 'group_id' => Groups_Utility::id( $group_id )
107 );
108 $formats = array( '%d', '%d' );
109 if ( $wpdb->insert( $user_group_table, $data, $formats ) ) {
110 $result = true;
111 do_action( "groups_created_user_group", $user_id, $group_id );
112 }
113 }
114 }
115 }
116 }
117 return $result;
118 }
119
120 /**
121 * Retrieve a user-group relation.
122 *
123 * @param int $user_id user's id
124 * @param int $group_id group's id
125 * @return object upon success, otherwise false
126 */
127 public static function read( $user_id, $group_id ) {
128 global $wpdb;
129 $result = false;
130
131 $user_group_table = _groups_get_tablename( 'user_group' );
132 $user_group = $wpdb->get_row( $wpdb->prepare(
133 "SELECT * FROM $user_group_table WHERE user_id = %d AND group_id = %d",
134 Groups_Utility::id( $user_id ),
135 Groups_Utility::id( $group_id )
136 ) );
137 if ( $user_group !== null ) {
138 $result = $user_group;
139 }
140 return $result;
141 }
142
143 /**
144 * Update user-group relation.
145 *
146 * This is a relation and as the relation is, this does nothing and
147 * it SHOULD do nothing.
148 *
149 * @param array $map
150 * @return true on success, otherwise false
151 */
152 public static function update( $map ) {
153 $result = false;
154 // if ( !empty( $user_id ) && !empty( $group_id) ) {
155 if ( !empty( $group_id) ) {
156 // make sure user and group exist
157 if ( ( false !== Groups_Utility::id( $user_id ) ) && get_user_by( "id", $user_id ) && Groups_Group::read( $group_id ) ) {
158 $result = true;
159 do_action( "groups_updated_user_group", $user_id, $group_id );
160 }
161 }
162 return $result;
163 }
164
165 /**
166 * Remove user-group relation.
167 *
168 * @param int $user_id
169 * @param int $group_id
170 * @return true if successful, false otherwise
171 */
172 public static function delete( $user_id, $group_id ) {
173
174 global $wpdb;
175 $result = false;
176
177 // avoid nonsense requests
178 // if ( !empty( $user_id ) && !empty( $group_id ) ) {
179 if ( !empty( $group_id ) ) {
180 // to allow deletion of an entry after a user has been deleted,
181 // we don't check if the user exists
182 $user_group_table = _groups_get_tablename( 'user_group' );
183 // get rid of it
184 $rows = $wpdb->query( $wpdb->prepare(
185 "DELETE FROM $user_group_table WHERE user_id = %d AND group_id = %d",
186 Groups_Utility::id( $user_id ),
187 Groups_Utility::id( $group_id )
188 ) );
189 // must have affected a row, otherwise no great success
190 $result = ( $rows !== false ) && ( $rows > 0 );
191 if ( $result ) {
192 do_action( "groups_deleted_user_group", $user_id, $group_id );
193 }
194 }
195 return $result;
196 }
197
198 /**
199 * Hooks into the deleted_user action to remove the deleted user from
200 * all groups it belongs to.
201 *
202 * @param int $user_id
203 */
204 public static function deleted_user( $user_id ) {
205 global $wpdb;
206
207 $user_group_table = _groups_get_tablename( "user_group" );
208 $rows = $wpdb->get_results( $wpdb->prepare(
209 "SELECT * FROM $user_group_table WHERE user_id = %d",
210 Groups_Utility::id( $user_id )
211 ) );
212 if ( $rows ) {
213 foreach( $rows as $row ) {
214 // don't optimize that in preference of a standard deletion
215 // process (trigger actions ...)
216 self::delete( $row->user_id, $row->group_id );
217 }
218 }
219 }
220
221 /**
222 * Hooks into the remove_user_from_blog action to remove the user
223 * from groups that belong to that blog.
224 *
225 * Note that this is preemptive as there is no
226 * removed_user_from_blog action.
227 *
228 * @param int $user_id
229 * @param int $blog_id
230 */
231 public static function remove_user_from_blog( $user_id, $blog_id ) {
232
233 if ( is_multisite() ) {
234 Groups_Controller::switch_to_blog( $blog_id );
235 }
236
237 global $wpdb;
238
239 $group_table = _groups_get_tablename( "group" );
240 $user_group_table = _groups_get_tablename( "user_group" );
241 // We can end up here while a blog is being deleted, in that case,
242 // the tables have already been deleted.
243 if ( ( $wpdb->get_var( "SHOW TABLES LIKE '" . $group_table . "'" ) == $group_table ) &&
244 ( $wpdb->get_var( "SHOW TABLES LIKE '" . $user_group_table . "'" ) == $user_group_table )
245 ) {
246
247 $rows = $wpdb->get_results( $wpdb->prepare(
248 "SELECT * FROM $user_group_table
249 LEFT JOIN $group_table ON $user_group_table.group_id = $group_table.group_id
250 WHERE $user_group_table.user_id = %d
251 ",
252 Groups_Utility::id( $user_id )
253 ) );
254 if ( $rows ) {
255 foreach( $rows as $row ) {
256 // don't optimize that, favour standard deletion
257 self::delete( $row->user_id, $row->group_id );
258 }
259 }
260
261 }
262
263 if ( is_multisite() ) {
264 Groups_Controller::restore_current_blog();
265 }
266 }
267 }
268 Groups_User_Group::init();
269