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