Skip to content

Commit 4b2d0da

Browse files
committed
add custom login template
1 parent 0f97f40 commit 4b2d0da

File tree

9 files changed

+264
-93
lines changed

9 files changed

+264
-93
lines changed

includes/Init.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public function send_headers(): void {
160160
header( 'Access-Control-Allow-Origin: *' );
161161
header( 'Access-Control-Expose-Headers: Link' );
162162
}
163-
}
163+
}
164164

165165
/**
166166
* Loads POS integrations with third party plugins.

includes/Templates.php

Lines changed: 89 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
namespace WCPOS\WooCommercePOS;
99

1010
use WC_Order;
11-
use WCPOS\WooCommercePOS\Templates\Frontend;
1211

1312
/**
1413
*
@@ -17,43 +16,47 @@ class Templates {
1716
/**
1817
* @var string POS frontend slug
1918
*/
20-
private $pos_slug;
19+
private $pos_regex;
20+
21+
/**
22+
* @var string POS login slug
23+
*/
24+
private $pos_login_regex;
2125

2226
/**
2327
* @var string POS checkout slug
2428
* @note 'wcpos-checkout' slug is used instead 'checkout' to avoid conflicts with WC checkout
2529
* eg: x-frame-options: SAMEORIGIN
2630
*/
27-
private $pos_checkout_slug;
31+
private $pos_checkout_regex;
2832

29-
/**
30-
* @var string regex match for frontend rewite_rule
31-
*/
32-
private $pos_rewrite_regex;
3333

34-
/**
35-
* @var string regex match for checkout rewite_rule
36-
*/
37-
private $pos_checkout_rewrite_regex;
34+
public function __construct() {
35+
$this->pos_regex = '^' . Admin\Permalink::get_slug() . '/?';
36+
$this->pos_login_regex = '^wcpos-login/?';
37+
$this->pos_checkout_regex = '^wcpos-checkout/([a-z-]+)/([0-9]+)[/]?$';
3838

39+
$this->add_rewrite_rules();
3940

40-
public function __construct() {
41-
$this->pos_slug = Admin\Permalink::get_slug();
42-
$this->pos_rewrite_regex = '^' . $this->pos_slug . '/?';
43-
$this->pos_checkout_slug = 'wcpos-checkout';
44-
$this->pos_checkout_rewrite_regex = '^' . $this->pos_checkout_slug . '/([a-z-]+)/([0-9]+)[/]?$';
45-
46-
// Note: 'order-pay' and 'order-received' rewrite tags are added by WC
47-
add_rewrite_tag( '%wcpos%', '([^&]+)' );
48-
add_rewrite_tag( '%wcpos-receipt%', '([^&]+)' );
49-
add_rewrite_rule( $this->pos_rewrite_regex, 'index.php?wcpos=1', 'top' );
50-
add_rewrite_rule( $this->pos_checkout_rewrite_regex, 'index.php?$matches[1]=$matches[2]&wcpos=1', 'top' );
5141
add_filter( 'option_rewrite_rules', array( $this, 'rewrite_rules' ), 1 );
5242
add_action( 'template_redirect', array( $this, 'template_redirect' ), 1 );
53-
5443
add_filter( 'woocommerce_get_checkout_order_received_url', array( $this, 'order_received_url' ), 10, 2 );
5544
}
5645

46+
/**
47+
* @NOTE: 'order-pay' and 'order-received' rewrite tags are added by WC
48+
*
49+
* @return void
50+
*/
51+
private function add_rewrite_rules() {
52+
add_rewrite_tag( '%wcpos%', '([^&]+)' );
53+
add_rewrite_tag( '%wcpos-receipt%', '([^&]+)' );
54+
add_rewrite_tag( '%wcpos-login%', '([^&]+)' );
55+
add_rewrite_rule( $this->pos_regex, 'index.php?wcpos=1', 'top' );
56+
add_rewrite_rule( $this->pos_login_regex, 'index.php?wcpos-login=1', 'top' );
57+
add_rewrite_rule( $this->pos_checkout_regex, 'index.php?$matches[1]=$matches[2]&wcpos=1', 'top' );
58+
}
59+
5760
/**
5861
* Make sure cache contains POS rewrite rules.
5962
*
@@ -62,7 +65,7 @@ public function __construct() {
6265
* @return array|bool
6366
*/
6467
public function rewrite_rules( $rules ) {
65-
return isset( $rules[ $this->pos_rewrite_regex ], $rules[ $this->pos_checkout_rewrite_regex ] ) ? $rules : false;
68+
return isset( $rules[ $this->pos_regex ], $rules[ $this->pos_login_regex ], $rules[ $this->pos_checkout_regex ] ) ? $rules : false;
6669
}
6770

6871
/**
@@ -71,35 +74,70 @@ public function rewrite_rules( $rules ) {
7174
public function template_redirect(): void {
7275
global $wp;
7376

74-
$query_var_classname_map = array(
75-
'order-pay' => __NAMESPACE__ . '\\Templates\\Payment',
76-
'order-received' => __NAMESPACE__ . '\\Templates\\Received',
77-
'wcpos-receipt' => __NAMESPACE__ . '\\Templates\\Receipt',
78-
);
79-
80-
if ( $wp->matched_rule === $this->pos_checkout_rewrite_regex ) {
81-
foreach ( $query_var_classname_map as $query_var => $classname ) {
82-
if ( isset( $wp->query_vars[ $query_var ] ) ) {
83-
$order_id = absint( $wp->query_vars[ $query_var ] );
84-
85-
if ( class_exists( $classname ) && $order_id ) {
86-
$template = new $classname( $order_id );
87-
$template->get_template();
88-
exit;
89-
} else {
90-
wp_die( esc_html__( 'Template not found.', 'woocommerce-pos' ) );
91-
}
92-
}
93-
}
94-
}
95-
96-
if ( $wp->matched_rule === $this->pos_rewrite_regex ) {
97-
$template = new Frontend();
98-
$template->get_template();
99-
exit;
100-
}
77+
$rewrite_rules_to_templates = array(
78+
$this->pos_regex => __NAMESPACE__ . '\\Templates\\Frontend',
79+
$this->pos_login_regex => __NAMESPACE__ . '\\Templates\\Login',
80+
$this->pos_checkout_regex => array(
81+
'order-pay' => __NAMESPACE__ . '\\Templates\\Payment',
82+
'order-received' => __NAMESPACE__ . '\\Templates\\Received',
83+
'wcpos-receipt' => __NAMESPACE__ . '\\Templates\\Receipt',
84+
),
85+
);
86+
87+
foreach ( $rewrite_rules_to_templates as $rule => $classname ) {
88+
if ( $wp->matched_rule === $rule ) {
89+
if ( is_array( $classname ) ) {
90+
$this->load_checkout_template( $classname );
91+
} else {
92+
$this->load_template( $classname );
93+
}
94+
exit;
95+
}
96+
}
10197
}
10298

99+
/**
100+
* Loads order templates, additionally checks query var is a valid order id
101+
*
102+
* @param array $classnames
103+
*
104+
* @return void
105+
*/
106+
private function load_checkout_template( array $classnames ): void {
107+
global $wp;
108+
109+
foreach ( $classnames as $query_var => $classname ) {
110+
if ( isset( $wp->query_vars[ $query_var ] ) ) {
111+
$order_id = absint( $wp->query_vars[ $query_var ] );
112+
113+
if ( class_exists( $classname ) && $order_id ) {
114+
$template = new $classname( $order_id );
115+
$template->get_template();
116+
return;
117+
}
118+
}
119+
}
120+
121+
wp_die( esc_html__( 'Template not found.', 'woocommerce-pos' ) );
122+
}
123+
124+
/**
125+
* Loads all other templates
126+
*
127+
* @param string $classname
128+
*
129+
* @return void
130+
*/
131+
private function load_template( string $classname ): void {
132+
if ( class_exists( $classname ) ) {
133+
$template = new $classname();
134+
$template->get_template();
135+
return;
136+
}
137+
138+
wp_die( esc_html__( 'Template not found.', 'woocommerce-pos' ) );
139+
}
140+
103141

104142
/**
105143
* Just like the checkout/payment.php template, we hijack the order received url so we can display a stripped down

includes/Templates/Frontend.php

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,6 @@ public function get_template(): void {
5353
// last chance before template is rendered
5454
do_action( 'woocommerce_pos_template_redirect' );
5555

56-
// if modal login
57-
if ( isset( $_GET['modal'] ) && $_GET['modal'] === '1' ) {
58-
$this->modal_login();
59-
exit;
60-
}
61-
6256
// add head & footer actions
6357
add_action( 'woocommerce_pos_head', array( $this, 'head' ) );
6458
add_action( 'woocommerce_pos_footer', array( $this, 'footer' ) );
@@ -75,7 +69,7 @@ public function get_template(): void {
7569
* @return mixed
7670
*/
7771
public function login_url( $login_url ) {
78-
return add_query_arg( SHORT_NAME, '1', $login_url );
72+
return add_query_arg( SHORT_NAME, '1', $login_url );
7973
}
8074

8175
/**
@@ -135,7 +129,7 @@ public function footer(): void {
135129
'last_access' => '',
136130
'avatar_url' => get_avatar_url( $user->ID ),
137131
'wp_nonce' => wp_create_nonce( 'wp_rest' ),
138-
// 'jwt' => $jwt,
132+
// 'jwt' => $jwt,
139133
),
140134
'stores' => $store_settings->get_stores(),
141135
);
@@ -215,33 +209,4 @@ private function no_cache(): void {
215209
do_action( 'litespeed_control_set_nocache', 'nocache WoCommerce POS web application' );
216210

217211
}
218-
219-
private function modal_login() {
220-
$user = wp_get_current_user();
221-
$user_data = $this->auth_service->get_user_data( $user );
222-
$credentials = wp_json_encode( $user_data );
223-
224-
echo "<script>
225-
(function() {
226-
// Parse the order JSON from PHP
227-
var credentials = " . $credentials . "
228-
229-
// Check if postMessage function exists for window.top
230-
if (typeof window.top.postMessage === 'function') {
231-
window.top.postMessage({
232-
action: 'wcpos-wp-credentials',
233-
payload: credentials
234-
}, '*');
235-
}
236-
237-
// Check if ReactNativeWebView object and postMessage function exists
238-
if (typeof window.ReactNativeWebView !== 'undefined' && typeof window.ReactNativeWebView.postMessage === 'function') {
239-
window.ReactNativeWebView.postMessage(JSON.stringify({
240-
action: 'wcpos-wp-credentials',
241-
payload: credentials
242-
}));
243-
}
244-
})();
245-
</script>" . "\n";
246-
}
247212
}

includes/Templates/Login.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
namespace WCPOS\WooCommercePOS\Templates;
4+
5+
use WCPOS\WooCommercePOS\Services\Auth;
6+
7+
class Login {
8+
/**
9+
*
10+
*/
11+
protected $auth_service;
12+
13+
14+
public function __construct() {
15+
$this->auth_service = new Auth();
16+
$this->send_headers();
17+
}
18+
19+
/**
20+
* Send headers
21+
*
22+
* @return void
23+
*/
24+
private function send_headers() {
25+
header( 'Content-Security-Policy: frame-ancestors http://localhost:* https://localhost:*' );
26+
}
27+
28+
29+
public function get_template(): void {
30+
$login_attempt = isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'woocommerce_pos_login' );
31+
32+
// Login attempt detected
33+
$error_string = '';
34+
if ( $login_attempt ) {
35+
$creds = array();
36+
$creds['user_login'] = $_POST['log'];
37+
$creds['user_password'] = $_POST['pwd'];
38+
// $creds['remember'] = isset( $_POST['rememberme'] );
39+
40+
$user = wp_signon( $creds, false );
41+
42+
if ( is_wp_error( $user ) ) {
43+
foreach ( $user->errors as $error ) {
44+
$error_string .= '<p class="error">' . $error[0] . '</p>';
45+
}
46+
} else {
47+
$this->login_success();
48+
exit;
49+
}
50+
}
51+
52+
//
53+
do_action( 'login_init' );
54+
55+
//
56+
do_action( 'login_form_login' );
57+
58+
include woocommerce_pos_locate_template( 'login.php' );
59+
exit;
60+
}
61+
62+
/**
63+
* Login success
64+
*
65+
* @return void
66+
*/
67+
private function login_success() {
68+
$user = wp_get_current_user();
69+
$user_data = $this->auth_service->get_user_data( $user );
70+
$credentials = wp_json_encode( $user_data );
71+
72+
echo '<script>
73+
(function() {
74+
// Parse the order JSON from PHP
75+
var credentials = ' . $credentials . "
76+
77+
// Check if postMessage function exists for window.top
78+
if (typeof window.top.postMessage === 'function') {
79+
window.top.postMessage({
80+
action: 'wcpos-wp-credentials',
81+
payload: credentials
82+
}, '*');
83+
}
84+
85+
// Check if ReactNativeWebView object and postMessage function exists
86+
if (typeof window.ReactNativeWebView !== 'undefined' && typeof window.ReactNativeWebView.postMessage === 'function') {
87+
window.ReactNativeWebView.postMessage(JSON.stringify({
88+
action: 'wcpos-wp-credentials',
89+
payload: credentials
90+
}));
91+
}
92+
})();
93+
</script>" . "\n";
94+
}
95+
96+
}

0 commit comments

Comments
 (0)