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