| 1 |
<?php |
| 2 |
class Easyel_Login_Register { |
| 3 |
public function __construct() { |
| 4 |
add_action( 'wp_ajax_eel_login', [$this, 'easyel_handle_login'] ); |
| 5 |
add_action( 'wp_ajax_nopriv_eel_login', [$this, 'easyel_handle_login'] ); |
| 6 |
add_action( 'wp_ajax_eel_register', [$this, 'easyel_handle_register'] ); |
| 7 |
add_action( 'wp_ajax_nopriv_eel_register', [$this, 'easyel_handle_register'] ); |
| 8 |
} |
| 9 |
|
| 10 |
/** |
| 11 |
* Handle login form submission |
| 12 |
*/ |
| 13 |
public function easyel_handle_login() { |
| 14 |
|
| 15 |
if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] )) , 'easy_elements_nonce' ) ) { |
| 16 |
return wp_send_json_error( ['msg' => 'security failed!'] ); |
| 17 |
} |
| 18 |
|
| 19 |
$creds = [ |
| 20 |
'user_login' => !empty($_POST['user']) ? sanitize_user( wp_unslash( $_POST['user'] ) ) : '', |
| 21 |
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash |
| 22 |
'user_password' => !empty($_POST['pwd']) ? wp_unslash( $_POST['pwd'] ) : '', |
| 23 |
'remember' => !empty($_POST['remember']) ? sanitize_text_field( wp_unslash( $_POST['remember'] ) ) : '', |
| 24 |
]; |
| 25 |
|
| 26 |
$user = wp_signon( $creds, true ); |
| 27 |
|
| 28 |
if ( is_wp_error( $user ) ) { |
| 29 |
// store error message for render |
| 30 |
$msg = $user->get_error_message(); |
| 31 |
wp_send_json_error( ['msg' => $msg, 'creds' => $creds] ); |
| 32 |
} else { |
| 33 |
wp_send_json_success(); |
| 34 |
} |
| 35 |
|
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Handle registration form submission |
| 40 |
*/ |
| 41 |
public function easyel_handle_register() { |
| 42 |
|
| 43 |
if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] )), 'easy_elements_nonce' ) ) { |
| 44 |
return wp_send_json_error( ['msg' => 'Security failed!'] ); |
| 45 |
} |
| 46 |
|
| 47 |
$custom_meta = !empty($_POST['custom_meta']) |
| 48 |
? map_deep( wp_unslash( $_POST['custom_meta'] ), 'sanitize_text_field' ) |
| 49 |
: []; |
| 50 |
|
| 51 |
$consent = !empty($_POST['consent']) ? 'yes' : 'no'; |
| 52 |
|
| 53 |
$user_data = [ |
| 54 |
'user_login' => ! empty( $_POST['user_login'] ) |
| 55 |
? sanitize_user( wp_unslash( $_POST['user_login'] ), true ) |
| 56 |
: '', |
| 57 |
'user_email' => !empty($_POST['user_email']) ? sanitize_email( wp_unslash($_POST['user_email']) ) : '', |
| 58 |
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash |
| 59 |
'user_pass' => !empty($_POST['user_pass']) ? wp_unslash($_POST['user_pass']) : '', |
| 60 |
'role' => !empty($_POST['role']) ? sanitize_text_field( wp_unslash($_POST['role']) ) : 'subscriber', |
| 61 |
'first_name' => !empty($_POST['first_name']) ? sanitize_text_field( wp_unslash($_POST['first_name']) ) : '', |
| 62 |
'last_name' => !empty($_POST['last_name']) ? sanitize_text_field( wp_unslash($_POST['last_name']) ) : '', |
| 63 |
'display_name' => !empty($_POST['display_name']) ? sanitize_text_field( wp_unslash($_POST['display_name']) ) : '', |
| 64 |
'user_nicename' => !empty($_POST['user_nicename']) ? sanitize_text_field( wp_unslash($_POST['user_nicename']) ) : '', |
| 65 |
'nickname' => !empty($_POST['nickname']) ? sanitize_text_field( wp_unslash($_POST['nickname']) ) : '', |
| 66 |
'user_url' => !empty($_POST['user_url']) ? esc_url_raw( wp_unslash($_POST['user_url']) ) : '', |
| 67 |
'description' => !empty($_POST['description']) ? sanitize_textarea_field( wp_unslash($_POST['description']) ) : '', |
| 68 |
]; |
| 69 |
|
| 70 |
$auto_login = !empty($_POST['auto_login']) ? 'yes' : 'no'; |
| 71 |
|
| 72 |
$user_id = wp_insert_user( $user_data ); |
| 73 |
|
| 74 |
if ( is_wp_error( $user_id ) ) { |
| 75 |
return wp_send_json_error( ['msg' => $user_id->get_error_message()] ); |
| 76 |
} |
| 77 |
|
| 78 |
// Save meta |
| 79 |
foreach ( $custom_meta as $key => $value ) { |
| 80 |
update_user_meta( $user_id, $key, $value ); |
| 81 |
} |
| 82 |
|
| 83 |
update_user_meta( $user_id, 'consent', $consent ); |
| 84 |
|
| 85 |
$msg = 'User created successfully'; |
| 86 |
|
| 87 |
// Auto login |
| 88 |
if ( $auto_login === 'yes' ) { |
| 89 |
$creds = [ |
| 90 |
'user_login' => $user_data['user_login'], |
| 91 |
'user_password' => $user_data['user_pass'], |
| 92 |
'remember' => true, |
| 93 |
]; |
| 94 |
$user = wp_signon( $creds, true ); |
| 95 |
|
| 96 |
if ( is_wp_error( $user ) ) { |
| 97 |
$msg = $user->get_error_message(); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
return wp_send_json_success( ['msg' => $msg] ); |
| 102 |
} |
| 103 |
|
| 104 |
} |
| 105 |
new Easyel_Login_Register(); |