|
4 | 4 | * Plugin Name: User Activation Email
|
5 | 5 | * Plugin URI: https://github.com/NateJacobs/User-Activation-Email
|
6 | 6 | * Description: Add an activation code to the new user email sent once a user registers. The user must enter this activation code in addition to a username and password to log in successfully the first time.
|
7 |
| - * Version: 1.2.1 |
| 7 | + * Version: 1.2.2 |
8 | 8 | * License: GPL V2
|
9 | 9 | * Author: Nate Jacobs <nate@natejacobs.org>
|
10 | 10 | * Author URI: http://natejacobs.org
|
@@ -82,49 +82,65 @@ public function activate()
|
82 | 82 | */
|
83 | 83 | public function check_user_activation_code( $user, $user_login, $password )
|
84 | 84 | {
|
| 85 | + // get the error class ready |
| 86 | + $error = new \WP_Error(); |
| 87 | + |
85 | 88 | $activation_code = '';
|
86 | 89 |
|
87 |
| - // get user data by login |
| 90 | + // first check if either of the two fields are empty |
| 91 | + if ( empty( $user_login ) || empty( $password ) ) |
| 92 | + { |
| 93 | + // figure out which one |
| 94 | + if ( empty( $user_login ) ) |
| 95 | + $error->add( 'empty_username', __( 'The username field is empty.', 'user-activation-email' ) ); |
| 96 | + |
| 97 | + if ( empty( $password ) ) |
| 98 | + $error->add( 'empty_password', __( 'The password field is empty.', 'user-activation-email' ) ); |
| 99 | + |
| 100 | + // remove the ability to authenticate |
| 101 | + remove_action( 'authenticate', 'wp_authenticate_username_password', 20 ); |
| 102 | + |
| 103 | + // return appropriate error |
| 104 | + return $error; |
| 105 | + } |
| 106 | + |
88 | 107 | $user_info = get_user_by( 'login', $user_login );
|
89 |
| - |
90 |
| - // if the user has entered something in the user name box |
91 |
| - if ( $user_info ) |
| 108 | + |
| 109 | + // if the object is empty, meaning an invalid username |
| 110 | + if( empty( $user_info ) ) |
| 111 | + { |
| 112 | + // add the error message for invalid username |
| 113 | + $error->add( 'incorrect user', __( 'Username does not exist', 'user-activation-email' ) ); |
| 114 | + |
| 115 | + // remove the ability to authenticate |
| 116 | + remove_action( 'authenticate', 'wp_authenticate_username_password', 20 ); |
| 117 | + |
| 118 | + // return appropriate error |
| 119 | + return $error; |
| 120 | + } |
| 121 | + else |
92 | 122 | {
|
93 | 123 | // get the custom user meta defined during registration
|
94 | 124 | $activation_code = get_user_meta( $user_info->ID, $this->user_meta, true );
|
95 | 125 | }
|
96 |
| - if ( empty( $user_login ) || empty($password) ) |
| 126 | + |
| 127 | + if( $activation_code == 'active' ) |
97 | 128 | {
|
98 |
| - if ( empty($username) ) |
99 |
| - $user = new WP_Error( 'empty_username', __( '<strong>ERROR</strong>: The username field is empty.', 'user-activation-email' ) ); |
100 |
| - |
101 |
| - if ( empty($password) ) |
102 |
| - $user = new WP_Error( 'empty_password', __( '<strong>ERROR</strong>: The password field is empty.', 'user-activation-email' ) ); |
| 129 | + return $user; |
| 130 | + exit; |
103 | 131 | }
|
104 | 132 | else
|
105 | 133 | {
|
106 |
| - if ( $activation_code == 'active' ) |
107 |
| - { |
108 |
| - return $user; |
109 |
| - exit; |
110 |
| - } |
111 |
| - |
112 |
| - if ( !isset($_POST['activation-code'] ) ) |
113 |
| - { |
114 |
| - $_POST['activation-code'] = false; |
115 |
| - } |
116 |
| - |
117 |
| - // if the activation code entered by the user is not identical to the activation code |
118 |
| - // stored in the *_usermeta table then deny access |
119 |
| - if ( $_POST['activation-code'] !== $activation_code ) |
| 134 | + if( $_POST['activation-code'] !== $activation_code ) |
120 | 135 | {
|
121 | 136 | // register a new error with the error message set above
|
122 | 137 | $user = new WP_Error( 'access_denied', __( 'Sorry, that activation code does not match. Please try again. You can find the activation code in your welcome email.', 'user-activation-email' ) );
|
123 | 138 | // deny access to login and send back to login page
|
124 | 139 | remove_filter( 'authenticate', 'wp_authenticate_username_password', 20 );
|
| 140 | + |
| 141 | + return $user; |
125 | 142 | }
|
126 |
| - } |
127 |
| - return $user; |
| 143 | + } |
128 | 144 | }
|
129 | 145 |
|
130 | 146 | /**
|
|
0 commit comments