| 1 |
<?php |
| 2 |
/** |
| 3 |
* class-groups-user-capability.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 |
// phpcs:disable PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 27 |
|
| 28 |
/** |
| 29 |
* User Capability OPM. |
| 30 |
*/ |
| 31 |
class Groups_User_Capability { |
| 32 |
|
| 33 |
/** |
| 34 |
* Hook into appropriate actions. |
| 35 |
* |
| 36 |
* @see wp_delete_user() |
| 37 |
* @see remove_user_from_blog() |
| 38 |
*/ |
| 39 |
public static function init() { |
| 40 |
|
| 41 |
// when a user is deleted, user-capabilities must be removed |
| 42 |
// triggered by wp_delete_user() |
| 43 |
add_action( 'deleted_user', array( __CLASS__, 'deleted_user' ) ); |
| 44 |
// when a capability is deleted the relationship must also be resolved |
| 45 |
add_action( 'groups_deleted_capability', array( __CLASS__, 'groups_deleted_capability' ) ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Persist a user-capability relation. |
| 50 |
* |
| 51 |
* @param array $map attributes - must provide user_id and capability_id |
| 52 |
* |
| 53 |
* @return true on success, otherwise false |
| 54 |
*/ |
| 55 |
public static function create( $map ) { |
| 56 |
|
| 57 |
global $wpdb; |
| 58 |
|
| 59 |
$result = false; |
| 60 |
|
| 61 |
$user_id = isset( $map['user_id'] ) ? $map['user_id'] : null; |
| 62 |
$capability_id = isset( $map['capability_id'] ) ? $map['capability_id'] : null; |
| 63 |
|
| 64 |
// avoid nonsense requests |
| 65 |
if ( !empty( $capability_id) ) { |
| 66 |
// make sure user and capability exist |
| 67 |
if ( ( false !== Groups_Utility::id( $user_id ) ) && get_user_by( 'id', $user_id ) && Groups_Capability::read( $capability_id ) ) { |
| 68 |
$user_capability_table = _groups_get_tablename( 'user_capability' ); |
| 69 |
// don't try to create duplicate entries |
| 70 |
// also it would raise an error for duplicate PK |
| 71 |
if ( 0 === intval( $wpdb->get_var( $wpdb->prepare( |
| 72 |
"SELECT COUNT(*) FROM $user_capability_table WHERE user_id = %d AND capability_id = %d", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 73 |
Groups_Utility::id( $user_id ), |
| 74 |
Groups_Utility::id( $capability_id ) |
| 75 |
) ) ) ) { |
| 76 |
$data = array( |
| 77 |
'user_id' => Groups_Utility::id( $user_id ), |
| 78 |
'capability_id' => Groups_Utility::id( $capability_id ) |
| 79 |
); |
| 80 |
$formats = array( '%d', '%d' ); |
| 81 |
if ( $wpdb->insert( $user_capability_table, $data, $formats ) ) { |
| 82 |
$result = true; |
| 83 |
do_action( 'groups_created_user_capability', $user_id, $capability_id ); |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
} |
| 89 |
return $result; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Retrieve a user-capability relation. |
| 94 |
* |
| 95 |
* @param int $user_id user's id |
| 96 |
* @param int $capability_id capability's id |
| 97 |
* |
| 98 |
* @return object upon success, otherwise false |
| 99 |
*/ |
| 100 |
public static function read( $user_id, $capability_id ) { |
| 101 |
global $wpdb; |
| 102 |
$result = false; |
| 103 |
|
| 104 |
$user_capability_table = _groups_get_tablename( 'user_capability' ); |
| 105 |
$user_capability = $wpdb->get_row( $wpdb->prepare( |
| 106 |
"SELECT * FROM $user_capability_table WHERE user_id = %d AND capability_id = %d", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 107 |
Groups_Utility::id( $user_id ), |
| 108 |
Groups_Utility::id( $capability_id ) |
| 109 |
) ); |
| 110 |
if ( $user_capability !== null ) { |
| 111 |
$result = $user_capability; |
| 112 |
} |
| 113 |
return $result; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Update user-capability relation. |
| 118 |
* |
| 119 |
* As the relation has no properties that could be updated, this method does nothing and will return false. |
| 120 |
* |
| 121 |
* @param array $map |
| 122 |
* |
| 123 |
* @return true if successful, false otherwise |
| 124 |
*/ |
| 125 |
public static function update( $map ) { |
| 126 |
$result = false; |
| 127 |
// @since 2.20.0 do not process |
| 128 |
if ( false ) { |
| 129 |
$capability_id = isset( $map['capability_id'] ) ? $map['capability_id'] : null; |
| 130 |
$user_id = isset( $map['user_id'] ) ? $map['user_id'] : null; |
| 131 |
if ( $capability_id !== null && $user_id !== null ) { |
| 132 |
// make sure user and capability exist |
| 133 |
if ( ( false !== Groups_Utility::id( $user_id ) ) && get_user_by( 'id', $user_id ) && Groups_Capability::read( $capability_id ) ) { |
| 134 |
$result = true; |
| 135 |
do_action( 'groups_updated_user_capability', $user_id, $capability_id ); |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
return $result; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Remove user-capability relation. |
| 144 |
* |
| 145 |
* @param int $user_id |
| 146 |
* @param int $capability_id |
| 147 |
* |
| 148 |
* @return true if successful, false otherwise |
| 149 |
*/ |
| 150 |
public static function delete( $user_id, $capability_id ) { |
| 151 |
|
| 152 |
global $wpdb; |
| 153 |
$result = false; |
| 154 |
|
| 155 |
// avoid nonsense requests |
| 156 |
if ( !empty( $capability_id ) ) { |
| 157 |
// to allow deletion of an entry after a user has been deleted, |
| 158 |
// we don't check if the user exists |
| 159 |
$user_capability_table = _groups_get_tablename( 'user_capability' ); |
| 160 |
// get rid of it |
| 161 |
$rows = $wpdb->query( $wpdb->prepare( |
| 162 |
"DELETE FROM $user_capability_table WHERE user_id = %d AND capability_id = %d", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 163 |
Groups_Utility::id( $user_id ), |
| 164 |
Groups_Utility::id( $capability_id ) |
| 165 |
) ); |
| 166 |
// must have affected a row, otherwise no great success |
| 167 |
$result = ( $rows !== false ) && ( $rows > 0 ); |
| 168 |
if ( $result ) { |
| 169 |
do_action( 'groups_deleted_user_capability', $user_id, $capability_id ); |
| 170 |
} |
| 171 |
} |
| 172 |
return $result; |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Hooks into the deleted_user action to remove the deleted user from |
| 177 |
* all capabilities it is related to. |
| 178 |
* |
| 179 |
* @param int $user_id |
| 180 |
*/ |
| 181 |
public static function deleted_user( $user_id ) { |
| 182 |
global $wpdb; |
| 183 |
|
| 184 |
$user_capability_table = _groups_get_tablename( 'user_capability' ); |
| 185 |
$rows = $wpdb->get_results( $wpdb->prepare( |
| 186 |
"SELECT * FROM $user_capability_table WHERE user_id = %d", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 187 |
Groups_Utility::id( $user_id ) |
| 188 |
) ); |
| 189 |
if ( $rows ) { |
| 190 |
foreach ( $rows as $row ) { |
| 191 |
// don't optimize that in preference of a standard deletion |
| 192 |
// process (trigger actions ...) |
| 193 |
self::delete( $row->user_id, $row->capability_id ); |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Hooks into groups_deleted_capability to resolve all existing relations |
| 200 |
* between users and the deleted capability. |
| 201 |
* |
| 202 |
* @param int $capability_id |
| 203 |
*/ |
| 204 |
public static function groups_deleted_capability( $capability_id ) { |
| 205 |
global $wpdb; |
| 206 |
|
| 207 |
$user_capability_table = _groups_get_tablename( 'user_capability' ); |
| 208 |
$rows = $wpdb->get_results( $wpdb->prepare( |
| 209 |
"SELECT * FROM $user_capability_table WHERE capability_id = %d", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 210 |
Groups_Utility::id( $capability_id ) |
| 211 |
) ); |
| 212 |
if ( $rows ) { |
| 213 |
foreach ( $rows as $row ) { |
| 214 |
// do NOT 'optimize' (must trigger actions ... same as above) |
| 215 |
self::delete( $row->user_id, $row->capability_id ); |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
Groups_User_Capability::init(); |
| 221 |
|