WC_Form_Handler::process_login
Process the login form.
Method of the class: WC_Form_Handler{}
Hooks from the method
Returns
null. Nothing (null).
Usage
$result = WC_Form_Handler::process_login();
WC_Form_Handler::process_login() WC Form Handler::process login code WC 10.4.3
public static function process_login() {
static $valid_nonce = null;
if ( null === $valid_nonce ) {
// The global form-login.php template used `_wpnonce` in template versions < 3.3.0.
$nonce_value = wc_get_var( $_REQUEST['woocommerce-login-nonce'], wc_get_var( $_REQUEST['_wpnonce'], '' ) ); // @codingStandardsIgnoreLine.
$valid_nonce = wp_verify_nonce( $nonce_value, 'woocommerce-login' );
}
if ( isset( $_POST['login'], $_POST['username'], $_POST['password'] ) && is_string( $_POST['username'] ) && is_string( $_POST['password'] ) && $valid_nonce ) {
try {
$creds = array(
'user_login' => trim( wp_unslash( $_POST['username'] ) ), // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
'user_password' => $_POST['password'], // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash
'remember' => isset( $_POST['rememberme'] ), // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
);
$validation_error = new WP_Error();
$validation_error = apply_filters( 'woocommerce_process_login_errors', $validation_error, $creds['user_login'], $creds['user_password'] );
if ( $validation_error->get_error_code() ) {
throw new Exception( '<strong>' . __( 'Error:', 'woocommerce' ) . '</strong> ' . $validation_error->get_error_message() );
}
if ( empty( $creds['user_login'] ) ) {
throw new Exception( '<strong>' . __( 'Error:', 'woocommerce' ) . '</strong> ' . __( 'Username is required.', 'woocommerce' ) );
}
// On multisite, ensure user exists on current site, if not add them before allowing login.
if ( is_multisite() ) {
$user_data = get_user_by( is_email( $creds['user_login'] ) ? 'email' : 'login', $creds['user_login'] );
if ( $user_data && ! is_user_member_of_blog( $user_data->ID, get_current_blog_id() ) ) {
add_user_to_blog( get_current_blog_id(), $user_data->ID, 'customer' );
}
}
// Perform the login.
$user = wp_signon( apply_filters( 'woocommerce_login_credentials', $creds ), is_ssl() );
if ( is_wp_error( $user ) ) {
throw new Exception( $user->get_error_message() );
} else {
if ( ! empty( $_POST['redirect'] ) ) {
$redirect = wp_unslash( $_POST['redirect'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
} elseif ( wc_get_raw_referer() ) {
$redirect = wc_get_raw_referer();
} else {
$redirect = wc_get_page_permalink( 'myaccount' );
}
$redirect = remove_query_arg( array( 'wc_error', 'password-reset' ), $redirect );
wp_redirect( wp_validate_redirect( apply_filters( 'woocommerce_login_redirect', $redirect, $user ), wc_get_page_permalink( 'myaccount' ) ) ); // phpcs:ignore
exit;
}
} catch ( Exception $e ) {
wc_add_notice( apply_filters( 'login_errors', $e->getMessage() ), 'error' );
do_action( 'woocommerce_login_failed' );
}
}
}