@@ 118-176 (lines=59) @@ | ||
115 | * @param string $password Password for authentication. |
|
116 | * @return WP_User|WP_Error WP_User on success, WP_Error on failure. |
|
117 | */ |
|
118 | function wp_authenticate_username_password($user, $username, $password) { |
|
119 | if ( $user instanceof WP_User ) { |
|
120 | return $user; |
|
121 | } |
|
122 | ||
123 | if ( empty($username) || empty($password) ) { |
|
124 | if ( is_wp_error( $user ) ) |
|
125 | return $user; |
|
126 | ||
127 | $error = new WP_Error(); |
|
128 | ||
129 | if ( empty($username) ) |
|
130 | $error->add('empty_username', __('<strong>ERROR</strong>: The username field is empty.')); |
|
131 | ||
132 | if ( empty($password) ) |
|
133 | $error->add('empty_password', __('<strong>ERROR</strong>: The password field is empty.')); |
|
134 | ||
135 | return $error; |
|
136 | } |
|
137 | ||
138 | $user = get_user_by('login', $username); |
|
139 | ||
140 | if ( !$user ) { |
|
141 | return new WP_Error( 'invalid_username', |
|
142 | __( '<strong>ERROR</strong>: Invalid username.' ) . |
|
143 | ' <a href="' . wp_lostpassword_url() . '">' . |
|
144 | __( 'Lost your password?' ) . |
|
145 | '</a>' |
|
146 | ); |
|
147 | } |
|
148 | ||
149 | /** |
|
150 | * Filters whether the given user can be authenticated with the provided $password. |
|
151 | * |
|
152 | * @since 2.5.0 |
|
153 | * |
|
154 | * @param WP_User|WP_Error $user WP_User or WP_Error object if a previous |
|
155 | * callback failed authentication. |
|
156 | * @param string $password Password to check against the user. |
|
157 | */ |
|
158 | $user = apply_filters( 'wp_authenticate_user', $user, $password ); |
|
159 | if ( is_wp_error($user) ) |
|
160 | return $user; |
|
161 | ||
162 | if ( ! wp_check_password( $password, $user->user_pass, $user->ID ) ) { |
|
163 | return new WP_Error( 'incorrect_password', |
|
164 | sprintf( |
|
165 | /* translators: %s: user name */ |
|
166 | __( '<strong>ERROR</strong>: The password you entered for the username %s is incorrect.' ), |
|
167 | '<strong>' . $username . '</strong>' |
|
168 | ) . |
|
169 | ' <a href="' . wp_lostpassword_url() . '">' . |
|
170 | __( 'Lost your password?' ) . |
|
171 | '</a>' |
|
172 | ); |
|
173 | } |
|
174 | ||
175 | return $user; |
|
176 | } |
|
177 | ||
178 | /** |
|
179 | * Authenticates a user using the email and password. |
|
@@ 189-248 (lines=60) @@ | ||
186 | * @param string $password Password for authentication. |
|
187 | * @return WP_User|WP_Error WP_User on success, WP_Error on failure. |
|
188 | */ |
|
189 | function wp_authenticate_email_password( $user, $email, $password ) { |
|
190 | if ( $user instanceof WP_User ) { |
|
191 | return $user; |
|
192 | } |
|
193 | ||
194 | if ( empty( $email ) || empty( $password ) ) { |
|
195 | if ( is_wp_error( $user ) ) { |
|
196 | return $user; |
|
197 | } |
|
198 | ||
199 | $error = new WP_Error(); |
|
200 | ||
201 | if ( empty( $email ) ) { |
|
202 | $error->add( 'empty_username', __( '<strong>ERROR</strong>: The email field is empty.' ) ); // Uses 'empty_username' for back-compat with wp_signon() |
|
203 | } |
|
204 | ||
205 | if ( empty( $password ) ) { |
|
206 | $error->add( 'empty_password', __( '<strong>ERROR</strong>: The password field is empty.' ) ); |
|
207 | } |
|
208 | ||
209 | return $error; |
|
210 | } |
|
211 | ||
212 | if ( ! is_email( $email ) ) { |
|
213 | return $user; |
|
214 | } |
|
215 | ||
216 | $user = get_user_by( 'email', $email ); |
|
217 | ||
218 | if ( ! $user ) { |
|
219 | return new WP_Error( 'invalid_email', |
|
220 | __( '<strong>ERROR</strong>: Invalid email address.' ) . |
|
221 | ' <a href="' . wp_lostpassword_url() . '">' . |
|
222 | __( 'Lost your password?' ) . |
|
223 | '</a>' |
|
224 | ); |
|
225 | } |
|
226 | ||
227 | /** This filter is documented in wp-includes/user.php */ |
|
228 | $user = apply_filters( 'wp_authenticate_user', $user, $password ); |
|
229 | ||
230 | if ( is_wp_error( $user ) ) { |
|
231 | return $user; |
|
232 | } |
|
233 | ||
234 | if ( ! wp_check_password( $password, $user->user_pass, $user->ID ) ) { |
|
235 | return new WP_Error( 'incorrect_password', |
|
236 | sprintf( |
|
237 | /* translators: %s: email address */ |
|
238 | __( '<strong>ERROR</strong>: The password you entered for the email address %s is incorrect.' ), |
|
239 | '<strong>' . $email . '</strong>' |
|
240 | ) . |
|
241 | ' <a href="' . wp_lostpassword_url() . '">' . |
|
242 | __( 'Lost your password?' ) . |
|
243 | '</a>' |
|
244 | ); |
|
245 | } |
|
246 | ||
247 | return $user; |
|
248 | } |
|
249 | ||
250 | /** |
|
251 | * Authenticate the user using the WordPress auth cookie. |