| 1 |
<?php |
| 2 |
if ( !defined( 'ABSPATH' ) ) { exit; } |
| 3 |
|
| 4 |
if( !class_exists( 'LGFRLoginForm' ) ){ |
| 5 |
class LGFRLoginForm{ |
| 6 |
function __construct(){ |
| 7 |
add_action('wp_ajax_nopriv_lgfr_login', [$this, 'onSubmit']); |
| 8 |
add_action('wp_ajax_nopriv_lgfr_password_reset', [$this, 'reset_password_callback']); |
| 9 |
} |
| 10 |
|
| 11 |
function onSubmit(){ |
| 12 |
$nonce = sanitize_text_field($_POST['nonce']); |
| 13 |
$username = sanitize_text_field($_POST['user_login']); |
| 14 |
$password = sanitize_text_field($_POST['user_password']); |
| 15 |
$remember = sanitize_text_field($_POST['remember']) === 'true'; |
| 16 |
|
| 17 |
if(!wp_verify_nonce($nonce, 'wp_ajax')){ |
| 18 |
wp_send_json_error([ |
| 19 |
'message' => 'Invalid request', |
| 20 |
]); |
| 21 |
} |
| 22 |
|
| 23 |
$user = wp_signon([ |
| 24 |
'user_login' => $username, |
| 25 |
'user_password' => $password, |
| 26 |
'remember' => $remember |
| 27 |
]); |
| 28 |
|
| 29 |
if ( is_wp_error( $user ) ) { |
| 30 |
wp_send_json_error([ |
| 31 |
'message' => $user->get_error_message() |
| 32 |
]); |
| 33 |
} |
| 34 |
wp_send_json_success([ |
| 35 |
'message' => 'Successfully logged in.', |
| 36 |
'user' => $user |
| 37 |
]); |
| 38 |
} |
| 39 |
|
| 40 |
function reset_password_callback(){ |
| 41 |
global $wpdb, $current_site; |
| 42 |
|
| 43 |
$nonce = sanitize_text_field($_POST['nonce']); |
| 44 |
$user_email = sanitize_text_field($_POST['email']); |
| 45 |
$user = get_user_by('email', $user_email); |
| 46 |
$user_login = isset($user->data->user_login) ? $user->data->user_login : false; |
| 47 |
|
| 48 |
|
| 49 |
if(!wp_verify_nonce($nonce, 'wp_ajax') || !$user){ |
| 50 |
wp_send_json_error([ |
| 51 |
'message' => 'Invalid request', |
| 52 |
]); |
| 53 |
} |
| 54 |
|
| 55 |
$key = get_password_reset_key($user); |
| 56 |
if(is_wp_error($key)){ |
| 57 |
wp_send_json_error(['message' => $key->get_error_message()]); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
$message = __('Someone requested that the password be reset for the following account:', 'b-blocks') . "\r\n\r\n"; |
| 62 |
$message .= network_home_url( '/' ) . "\r\n\r\n"; |
| 63 |
$message .= sprintf(__('Username:', 'b-blocks').' %s', $user_login) . "\r\n\r\n"; |
| 64 |
$message .= __('If this was a mistake, just ignore this email and nothing will happen.', 'b-blocks') . "\r\n\r\n"; |
| 65 |
$message .= __('To reset your password, visit the following address:', 'b-blocks') . "\r\n\r\n"; |
| 66 |
$message .= network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user_login), 'login') . ">\r\n"; |
| 67 |
|
| 68 |
if ( is_multisite() ){ |
| 69 |
$blogname = $GLOBALS['current_site']->site_name; |
| 70 |
}else{ |
| 71 |
// The blogname option is escaped with esc_html on the way into the database in sanitize_option |
| 72 |
// we want to reverse this for the plain text arena of emails. |
| 73 |
$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES); |
| 74 |
} |
| 75 |
|
| 76 |
$title = sprintf('[%s]'. __(' Password Reset', 'b-blocks'), $blogname ); |
| 77 |
|
| 78 |
$title = apply_filters('retrieve_password_title', $title); |
| 79 |
$message = apply_filters('retrieve_password_message', $message, $key); |
| 80 |
|
| 81 |
if ( $message && !wp_mail($user_email, $title, $message) ){ |
| 82 |
wp_send_json_error([ |
| 83 |
'message' => __('The e-mail could not be sent.', 'b-blocks') . "<br />\n" . __('Possible reason: your host may have disabled the mail() function...', 'b-blocks') |
| 84 |
]); |
| 85 |
} |
| 86 |
|
| 87 |
wp_send_json_success([ |
| 88 |
'message' => 'Successfully send mail!', |
| 89 |
]); |
| 90 |
} |
| 91 |
} |
| 92 |
new LGFRLoginForm(); |
| 93 |
} |