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

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

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