| 1 |
<?php |
| 2 |
|
| 3 |
// Do not allow the file to be called directly. |
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
/** |
| 9 |
* This class is used to log any user related action. |
| 10 |
*/ |
| 11 |
class P_Event_Users extends P_Event_Log { |
| 12 |
|
| 13 |
/** |
| 14 |
* Hook into the actions so we can log it. |
| 15 |
* |
| 16 |
* @return void |
| 17 |
*/ |
| 18 |
public function __construct() { |
| 19 |
add_action( 'wp_login', [ &$this, 'login' ], 10, 2 ); |
| 20 |
add_action( 'delete_user', [ &$this, 'deleteUser' ] ); |
| 21 |
add_action( 'user_register', [ &$this, 'register' ] ); |
| 22 |
add_action( 'profile_update', [ &$this, 'profileUpdate' ] ); |
| 23 |
add_action( 'validate_password_reset', [ &$this, 'resetPassword' ], 10, 2 ); |
| 24 |
add_filter( 'wp_login_failed', [ &$this, 'wrongPassword' ] ); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* When a user is logging in. |
| 29 |
* |
| 30 |
* @param string $user_login |
| 31 |
* @param object $user |
| 32 |
* @return void |
| 33 |
*/ |
| 34 |
public function login( $user_login, $user ) { |
| 35 |
$this->insert( |
| 36 |
[ |
| 37 |
'action' => 'logged in', |
| 38 |
'object' => 'user', |
| 39 |
'object_id' => $user->ID, |
| 40 |
'object_name' => esc_html( $user->user_nicename ), |
| 41 |
] |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* When a new account has been created. |
| 47 |
* |
| 48 |
* @param integer $user_id |
| 49 |
* @return void |
| 50 |
*/ |
| 51 |
public function register( $user_id ) { |
| 52 |
$user = get_user_by( 'id', $user_id ); |
| 53 |
$this->insert( |
| 54 |
[ |
| 55 |
'action' => 'registered', |
| 56 |
'object' => 'user', |
| 57 |
'object_id' => $user->ID, |
| 58 |
'object_name' => esc_html( $user->user_nicename ), |
| 59 |
] |
| 60 |
); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* When a user is deleted. |
| 65 |
* |
| 66 |
* @param integer $user_id |
| 67 |
* @return void |
| 68 |
*/ |
| 69 |
public function deleteUser( $user_id ) { |
| 70 |
$user = get_user_by( 'id', $user_id ); |
| 71 |
$this->insert( |
| 72 |
[ |
| 73 |
'action' => 'deleted', |
| 74 |
'object' => 'user', |
| 75 |
'object_id' => $user->ID, |
| 76 |
'object_name' => esc_html( $user->user_nicename ), |
| 77 |
] |
| 78 |
); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* When a profile is updated. |
| 83 |
* |
| 84 |
* @param integer $user_id |
| 85 |
* @return void |
| 86 |
*/ |
| 87 |
public function profileUpdate( $user_id ) { |
| 88 |
$user = get_user_by( 'id', $user_id ); |
| 89 |
$this->insert( |
| 90 |
[ |
| 91 |
'action' => 'updated', |
| 92 |
'object' => 'user', |
| 93 |
'object_id' => $user->ID, |
| 94 |
'object_name' => esc_html( $user->user_nicename ), |
| 95 |
] |
| 96 |
); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* When a failed login attempt was made due to wrong password. |
| 101 |
* |
| 102 |
* @param string $username |
| 103 |
* @return void |
| 104 |
*/ |
| 105 |
public function wrongPassword( $username ) { |
| 106 |
$this->insert( |
| 107 |
[ |
| 108 |
'action' => 'failed login', |
| 109 |
'object' => 'user', |
| 110 |
'object_id' => 0, |
| 111 |
'object_name' => esc_html( $username ), |
| 112 |
] |
| 113 |
); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* When a password is being reset through the reset password procedure. |
| 118 |
* |
| 119 |
* @param object $errors |
| 120 |
* @param object $user |
| 121 |
* @return void |
| 122 |
*/ |
| 123 |
public function resetPassword( $errors, $user ) { |
| 124 |
// validate_password_reset also fires when the form is merely displayed (GET); |
| 125 |
// only log when a new password was actually submitted. |
| 126 |
if ( is_a( $user, 'WP_User' ) && isset( $_POST['pass1'] ) && ! empty( $_POST['pass1'] ) ) { |
| 127 |
$this->insert( |
| 128 |
[ |
| 129 |
'action' => 'password reset', |
| 130 |
'object' => 'user', |
| 131 |
'object_id' => 0, |
| 132 |
'object_name' => esc_html( $user->user_login ), |
| 133 |
] |
| 134 |
); |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|