rahulsprajapati /
user-approval
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Allow user authentication only on allowed/approved users. |
||
| 4 | * |
||
| 5 | * @package user-approval |
||
| 6 | */ |
||
| 7 | |||
| 8 | namespace User_Approval\Authenticate; |
||
| 9 | |||
| 10 | use WP_Error; |
||
| 11 | use WP_User; |
||
| 12 | use function User_Approval\filter_input; |
||
| 13 | use function User_Approval\get_default_user_role; |
||
| 14 | use function User_Approval\is_default_role_user; |
||
| 15 | use const User_Approval\STATUS_APPROVED; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 16 | use const User_Approval\STATUS_BLOCKED; |
||
|
0 ignored issues
–
show
|
|||
| 17 | use const User_Approval\STATUS_META_KEY; |
||
|
0 ignored issues
–
show
|
|||
| 18 | |||
| 19 | /** |
||
| 20 | 1 | * Hook up all the filters and actions. |
|
| 21 | */ |
||
| 22 | 1 | function bootstrap() { |
|
| 23 | 1 | add_action( 'lostpassword_post', __NAMESPACE__ . '\\block_non_approved_user_request', 1 ); |
|
| 24 | |||
| 25 | add_filter( 'wp_authenticate_user', __NAMESPACE__ . '\\authenticate_user_by_status' ); |
||
| 26 | } |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Block non approved user to generate forgot password email/link. |
||
| 30 | * |
||
| 31 | * @param WP_Error $errors A WP_Error object containing any errors generated |
||
| 32 | * by using invalid credentials. |
||
| 33 | */ |
||
| 34 | 1 | function block_non_approved_user_request( $errors ) { |
|
| 35 | 1 | ||
| 36 | // Do not do anything if there is already an error. |
||
| 37 | if ( $errors->get_error_code() ) { |
||
| 38 | 1 | return; |
|
| 39 | } |
||
| 40 | 1 | ||
| 41 | 1 | $login = filter_input( INPUT_POST, 'user_login', FILTER_SANITIZE_STRING ); |
|
| 42 | 1 | ||
| 43 | $user = is_email( $login ) |
||
| 44 | ? get_user_by( 'email', $login ) |
||
| 45 | 1 | : get_user_by( 'login', $login ); |
|
| 46 | 1 | ||
| 47 | if ( |
||
| 48 | 1 | ! $user instanceof WP_User |
|
| 49 | || ! in_array( get_default_user_role(), $user->roles, true ) |
||
| 50 | ) { |
||
| 51 | 1 | return; |
|
| 52 | } |
||
| 53 | 1 | ||
| 54 | 1 | $user_status = get_user_meta( $user->ID, STATUS_META_KEY, true ); |
|
| 55 | 1 | ||
| 56 | 1 | if ( STATUS_APPROVED !== $user_status ) { |
|
| 57 | $errors->add( |
||
| 58 | 'unapproved_user', |
||
| 59 | 1 | __( '<strong>ERROR</strong>: Your account is not active.', 'user-approval' ) |
|
| 60 | ); |
||
| 61 | } |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Authenticate user based on the user status. |
||
| 66 | * |
||
| 67 | * @param WP_User|WP_Error $user WP_User or WP_Error object if a previous |
||
| 68 | * callback failed authentication. |
||
| 69 | * |
||
| 70 | * @return WP_Error|WP_User |
||
| 71 | 1 | */ |
|
| 72 | 1 | function authenticate_user_by_status( $user ) { |
|
| 73 | |||
| 74 | if ( ! is_default_role_user( $user ) ) { |
||
| 75 | 1 | return $user; |
|
| 76 | } |
||
| 77 | 1 | ||
| 78 | 1 | $user_status = get_user_meta( $user->ID, STATUS_META_KEY, true ); |
|
|
0 ignored issues
–
show
|
|||
| 79 | 1 | ||
| 80 | 1 | switch ( $user_status ) { |
|
| 81 | 1 | case STATUS_BLOCKED: |
|
| 82 | 1 | $denied_message = __( '<strong>ERROR</strong>: Your account access has been blocked to this site.', 'user-approval' ); |
|
| 83 | 1 | $user_data = new WP_Error( 'blocked_access', $denied_message ); |
|
| 84 | 1 | break; |
|
| 85 | case STATUS_APPROVED: |
||
| 86 | 1 | $user_data = $user; |
|
| 87 | 1 | break; |
|
| 88 | 1 | default: |
|
| 89 | $pending_message = __( '<strong>ERROR</strong>: Your account is still pending approval.', 'user-approval' ); |
||
| 90 | $user_data = new WP_Error( 'pending_approval', $pending_message ); |
||
| 91 | 1 | break; |
|
| 92 | } |
||
| 93 | |||
| 94 | return $user_data; |
||
| 95 | } |
||
| 96 |