| 1 |
<?php |
| 2 |
/** |
| 3 |
* Login template |
| 4 |
* NOTE: This is the modal login template, ie: JWT expired, not the web login |
| 5 |
* |
| 6 |
* @package WooCommercePOS |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace WCPOS\WooCommercePOS\Templates; |
| 10 |
|
| 11 |
use WCPOS\WooCommercePOS\Services\Auth; |
| 12 |
use WCPOS\WooCommercePOS\Services\Cashier; |
| 13 |
use WP_User; |
| 14 |
|
| 15 |
/** |
| 16 |
* Login template |
| 17 |
*/ |
| 18 |
class Login { |
| 19 |
/** |
| 20 |
* Constructor. |
| 21 |
*/ |
| 22 |
public function __construct() { |
| 23 |
remove_action( 'login_init', 'send_frame_options_header', 10 ); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Get the login template. |
| 28 |
* |
| 29 |
* @return void |
| 30 |
*/ |
| 31 |
public function get_template(): void { |
| 32 |
$login_attempt = isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'woocommerce_pos_login' ); |
| 33 |
|
| 34 |
// Login attempt detected. |
| 35 |
$error_string = ''; |
| 36 |
if ( $login_attempt ) { |
| 37 |
$creds = array(); |
| 38 |
$creds['user_login'] = isset( $_POST['log'] ) ? sanitize_text_field( wp_unslash( $_POST['log'] ) ) : ''; |
| 39 |
$creds['user_password'] = isset( $_POST['pwd'] ) ? wp_unslash( $_POST['pwd'] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Passwords must not be sanitized as it can strip valid characters. |
| 40 |
// $creds['remember'] = isset( $_POST['rememberme'] ); |
| 41 |
|
| 42 |
$user = wp_signon( $creds, false ); |
| 43 |
|
| 44 |
if ( is_wp_error( $user ) ) { |
| 45 |
foreach ( $user->errors as $error ) { |
| 46 |
$error_string .= '<p class="error">' . $error[0] . '</p>'; |
| 47 |
} |
| 48 |
} elseif ( ! Cashier::instance()->can_open_pos( $user ) ) { |
| 49 |
$error_string .= '<p class="error">' . esc_html( Cashier::instance()->missing_pos_capabilities_message( $user ) ) . '</p>'; |
| 50 |
} else { |
| 51 |
wp_set_current_user( $user->ID ); |
| 52 |
$this->login_success( $user ); |
| 53 |
exit; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
do_action( 'woocommerce_pos_login_init' ); |
| 58 |
|
| 59 |
do_action( 'woocommerce_pos_login_form_login' ); |
| 60 |
|
| 61 |
include woocommerce_pos_locate_template( 'login.php' ); |
| 62 |
exit; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Login success |
| 67 |
* |
| 68 |
* @param WP_User $user The authenticated user. |
| 69 |
* |
| 70 |
* @return void |
| 71 |
*/ |
| 72 |
private function login_success( WP_User $user ) { |
| 73 |
$auth_service = Auth::instance(); |
| 74 |
$user_data = $auth_service->get_user_data( $user ); |
| 75 |
$stores = array_map( |
| 76 |
function ( $store ) { |
| 77 |
return $store->get_data(); |
| 78 |
}, |
| 79 |
wcpos_get_stores() |
| 80 |
); |
| 81 |
$user_data['stores'] = $stores; |
| 82 |
$credentials = wp_json_encode( $user_data ); |
| 83 |
|
| 84 |
echo '<script> |
| 85 |
(function() { |
| 86 |
var credentials = ' . wp_json_encode( $user_data ) . " |
| 87 |
|
| 88 |
// Check if postMessage function exists for window.top |
| 89 |
if (typeof window.top.postMessage === 'function') { |
| 90 |
window.top.postMessage({ |
| 91 |
action: 'wcpos-wp-credentials', |
| 92 |
payload: credentials |
| 93 |
}, '*'); |
| 94 |
} |
| 95 |
|
| 96 |
// Check if ReactNativeWebView object and postMessage function exists |
| 97 |
if (typeof window.ReactNativeWebView !== 'undefined' && typeof window.ReactNativeWebView.postMessage === 'function') { |
| 98 |
window.ReactNativeWebView.postMessage(JSON.stringify({ |
| 99 |
action: 'wcpos-wp-credentials', |
| 100 |
payload: credentials |
| 101 |
})); |
| 102 |
} |
| 103 |
})(); |
| 104 |
</script>" . "\n"; |
| 105 |
} |
| 106 |
} |
| 107 |
|