-
Notifications
You must be signed in to change notification settings - Fork 92
/
WP_Auth0_Profile_Delete_Data.php
94 lines (81 loc) · 2.32 KB
/
WP_Auth0_Profile_Delete_Data.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
/**
* Contains class WP_Auth0_Profile_Delete_Data.
*
* @package WP-Auth0
*
* @since 3.8.0
*/
/**
* Class WP_Auth0_Profile_Delete_Data.
* Provides UI and AJAX handlers to delete a user's Auth0 data.
*/
class WP_Auth0_Profile_Delete_Data {
/**
* WP_Auth0_UsersRepo instance.
*
* @var WP_Auth0_UsersRepo
*/
protected $users_repo;
/**
* WP_Auth0_Profile_Delete_Data constructor.
*
* @param WP_Auth0_UsersRepo $users_repo - WP_Auth0_UsersRepo instance.
*/
public function __construct( WP_Auth0_UsersRepo $users_repo ) {
$this->users_repo = $users_repo;
}
/**
* Add actions and filters for the profile page.
*
* @codeCoverageIgnore - Tested in TestProfileDeleteData::testInitHooks()
*/
public function init() {
add_action( 'edit_user_profile', array( $this, 'show_delete_identity' ) );
add_action( 'show_user_profile', array( $this, 'show_delete_identity' ) );
add_action( 'wp_ajax_auth0_delete_data', array( $this, 'delete_user_data' ) );
}
/**
* Show the delete Auth0 user data button.
* Hooked to: edit_user_profile, show_user_profile
* IMPORTANT: Internal callback use only, do not call this function directly!
*/
public function show_delete_identity() {
if ( ! isset( $GLOBALS['user_id'] ) || ! current_user_can( 'edit_users', $GLOBALS['user_id'] ) ) {
return;
}
if ( ! get_auth0userinfo( $GLOBALS['user_id'] ) ) {
return;
}
?>
<table class="form-table">
<tr>
<th>
<label><?php _e( 'Delete Auth0 Data' ); ?></label>
</th>
<td>
<input type="button" id="auth0_delete_data" class="button button-secondary"
value="<?php _e( 'Delete Auth0 Data', 'wp-auth0' ); ?>" />
</td>
</tr>
</table>
<?php
}
/**
* AJAX function to delete Auth0 data in the usermeta table.
* Hooked to: wp_ajax_auth0_delete_data
* IMPORTANT: Internal callback use only, do not call this function directly!
*/
public function delete_user_data() {
check_ajax_referer( 'delete_auth0_identity' );
if ( empty( $_POST['user_id'] ) ) {
wp_send_json_error( array( 'error' => __( 'Empty user_id', 'wp-auth0' ) ) );
}
$user_id = $_POST['user_id'];
if ( ! current_user_can( 'edit_users' ) ) {
wp_send_json_error( array( 'error' => __( 'Forbidden', 'wp-auth0' ) ) );
}
$this->users_repo->delete_auth0_object( $user_id );
wp_send_json_success();
}
}