| 1 |
<?php |
| 2 |
/** |
| 3 |
* Customer registration form handler (non-JS path). |
| 4 |
* |
| 5 |
* Mirrors the REST endpoint at /storeengine/v1/auth/register so the same |
| 6 |
* server-side behavior (username derivation, NewUserNotification email |
| 7 |
* trigger, auto-login) runs regardless of submission style. JS clients |
| 8 |
* should prefer the REST route; this handler exists so the form keeps |
| 9 |
* working without JavaScript and to match the ForgotPassword pattern. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace StoreEngine\Post; |
| 13 |
|
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
use StoreEngine\Classes\AbstractPostHandler; |
| 19 |
use StoreEngine\Utils\Helper; |
| 20 |
|
| 21 |
class Register extends AbstractPostHandler { |
| 22 |
|
| 23 |
public function __construct() { |
| 24 |
$this->actions = [ |
| 25 |
'auth/register' => [ |
| 26 |
'callback' => [ $this, 'register' ], |
| 27 |
'capability' => '', |
| 28 |
'allow_visitor_action' => true, |
| 29 |
'fields' => [ |
| 30 |
'email' => 'string', |
| 31 |
'password' => 'string', |
| 32 |
'first_name' => 'string', |
| 33 |
'last_name' => 'string', |
| 34 |
], |
| 35 |
], |
| 36 |
]; |
| 37 |
} |
| 38 |
|
| 39 |
public function register( array $payload ) { |
| 40 |
$endpoint_url = Helper::get_account_endpoint_url( 'register' ); |
| 41 |
|
| 42 |
if ( ! get_option( 'users_can_register' ) ) { |
| 43 |
wp_safe_redirect( add_query_arg( 'register_error', 'closed', $endpoint_url ) ); |
| 44 |
die(); |
| 45 |
} |
| 46 |
|
| 47 |
$email = isset( $payload['email'] ) ? sanitize_email( $payload['email'] ) : ''; |
| 48 |
$password = $payload['password'] ?? ''; |
| 49 |
$first_name = isset( $payload['first_name'] ) ? sanitize_text_field( $payload['first_name'] ) : ''; |
| 50 |
$last_name = isset( $payload['last_name'] ) ? sanitize_text_field( $payload['last_name'] ) : ''; |
| 51 |
|
| 52 |
if ( ! $email || ! is_email( $email ) ) { |
| 53 |
wp_safe_redirect( add_query_arg( 'register_error', 'invalid_email', $endpoint_url ) ); |
| 54 |
die(); |
| 55 |
} |
| 56 |
if ( '' === $password ) { |
| 57 |
wp_safe_redirect( add_query_arg( 'register_error', 'password', $endpoint_url ) ); |
| 58 |
die(); |
| 59 |
} |
| 60 |
|
| 61 |
$min_length = (int) apply_filters( 'storeengine/auth/min_password_length', 8 ); |
| 62 |
if ( strlen( $password ) < $min_length ) { |
| 63 |
wp_safe_redirect( add_query_arg( 'register_error', 'too_short', $endpoint_url ) ); |
| 64 |
die(); |
| 65 |
} |
| 66 |
|
| 67 |
if ( email_exists( $email ) ) { |
| 68 |
wp_safe_redirect( add_query_arg( 'register_error', 'email_taken', $endpoint_url ) ); |
| 69 |
die(); |
| 70 |
} |
| 71 |
|
| 72 |
// Match the REST controller's username derivation (same algorithm as |
| 73 |
// CheckoutService::create_customer) so both signup paths produce |
| 74 |
// identical usernames for the same email. |
| 75 |
$base = strstr( $email, '@', true ) ?: 'user'; |
| 76 |
$username = $base; |
| 77 |
$counter = 1; |
| 78 |
while ( username_exists( $username ) ) { |
| 79 |
$username = $base . $counter; |
| 80 |
$counter++; |
| 81 |
} |
| 82 |
|
| 83 |
$userdata = apply_filters( 'storeengine/auth/register_userdata', [ |
| 84 |
'user_login' => $username, |
| 85 |
'user_email' => $email, |
| 86 |
'user_pass' => $password, |
| 87 |
'role' => 'storeengine_customer', |
| 88 |
'display_name' => trim( $first_name . ' ' . $last_name ) ?: $username, |
| 89 |
'first_name' => $first_name, |
| 90 |
'last_name' => $last_name, |
| 91 |
], null ); |
| 92 |
|
| 93 |
$user_id = wp_insert_user( $userdata ); |
| 94 |
if ( is_wp_error( $user_id ) ) { |
| 95 |
wp_safe_redirect( add_query_arg( 'register_error', 'failed', $endpoint_url ) ); |
| 96 |
die(); |
| 97 |
} |
| 98 |
|
| 99 |
// Same hook the checkout uses on auto-register, so NewUserNotification |
| 100 |
// fires for the welcome-with-credentials email. |
| 101 |
do_action( 'storeengine/checkout/customer_created', $user_id, $userdata ); |
| 102 |
|
| 103 |
// Auto-login so the customer lands on the dashboard ready to shop. |
| 104 |
wp_clear_auth_cookie(); |
| 105 |
wp_signon( [ |
| 106 |
'user_login' => $username, |
| 107 |
'user_password' => $password, |
| 108 |
'remember' => true, |
| 109 |
], is_ssl() ); |
| 110 |
|
| 111 |
wp_safe_redirect( Helper::get_dashboard_url() ); |
| 112 |
die(); |
| 113 |
} |
| 114 |
} |
| 115 |
|