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