| @@ -4191,10 +4191,12 @@ | ||
| 4191 | 4191 | * |
| 4192 | 4192 | * @return array |
| 4193 | 4193 | */ |
| 4194 | 4194 | public function update_rest_api_key( $key_id, $scope, $description, $enabled = 1 ) { |
| 4195 | + $key = $this->get_rest_api_key_by( $key_id ); | |
| 4196 | + | |
| 4195 | 4197 | $permissions = in_array( $scope, array( 'read', 'write', 'delete', 'read_write' ), true ) ? sanitize_text_field( $scope ) : 'read'; |
| 4196 | - return $this->wpdb->update( | |
| 4198 | + $updated = $this->wpdb->update( | |
| 4197 | 4199 | $this->table_name( 'api_keys' ), |
| 4198 | 4200 | array( |
| 4199 | 4201 | 'description' => $description, |
| 4200 | 4202 | 'permissions' => $permissions, |
| @@ -4203,8 +4205,12 @@ | ||
| 4203 | 4205 | array( |
| 4204 | 4206 | 'key_id' => $key_id, |
| 4205 | 4207 | ) |
| 4206 | 4208 | ); |
| 4209 | + if ( $key ) { | |
| 4210 | + \MainWP_REST_Authentication::delete_api_key_cache( $key->consumer_key ); | |
| 4211 | + } | |
| 4212 | + return $updated; | |
| 4207 | 4213 | } |
| 4208 | 4214 | |
| 4209 | 4215 | |
| 4210 | 4216 | /** |
| @@ -4237,10 +4243,31 @@ | ||
| 4237 | 4243 | * |
| 4238 | 4244 | * @return array |
| 4239 | 4245 | */ |
| 4240 | 4246 | public function remove_rest_api_key( $id ) { |
| 4247 | + $key = $this->get_rest_api_key_by( $id ); | |
| 4241 | 4248 | $table_name = esc_sql( $this->table_name( 'api_keys' ) ); |
| 4242 | - return $this->wpdb->query( $this->wpdb->prepare( "DELETE FROM {$table_name} WHERE key_id = %s", $id ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name is esc_sql'd. | |
| 4249 | + $deleted = $this->wpdb->query( $this->wpdb->prepare( "DELETE FROM {$table_name} WHERE key_id = %d", $id ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name is esc_sql'd. | |
| 4250 | + if ( $key ) { | |
| 4251 | + \MainWP_REST_Authentication::delete_api_key_cache( $key->consumer_key ); | |
| 4252 | + } | |
| 4253 | + return $deleted; | |
| 4254 | + } | |
| 4255 | + | |
| 4256 | + /** | |
| 4257 | + * Delete a user's REST API keys and return their stored consumer keys. | |
| 4258 | + * | |
| 4259 | + * @param int $user_id Owner ID. | |
| 4260 | + * @param string|null $table_name Key table. Defaults to MainWP's captured table. | |
| 4261 | + * @return array Removed key rows containing consumer_key. | |
| 4262 | + */ | |
| 4263 | + public function remove_rest_api_keys_by_user( $user_id, $table_name = null ) { | |
| 4264 | + $table_name = esc_sql( null === $table_name ? $this->table_name( 'api_keys' ) : $table_name ); | |
| 4265 | + // nosec - $table_name is MainWP's own api_keys table name, never request input. | |
| 4266 | + $rows = $this->wpdb->get_results( $this->wpdb->prepare( "SELECT consumer_key FROM {$table_name} WHERE user_id = %d", $user_id ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name is esc_sql'd. | |
| 4267 | + // nosec - $table_name is MainWP's own api_keys table name, never request input. | |
| 4268 | + $this->wpdb->query( $this->wpdb->prepare( "DELETE FROM {$table_name} WHERE user_id = %d", $user_id ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name is esc_sql'd. | |
| 4269 | + return $rows; | |
| 4243 | 4270 | } |
| 4244 | 4271 | |
| 4245 | 4272 | /** |
| 4246 | 4273 | * Method get_rest_api_keys(). |