Skip to content

Commit

Permalink
add custom login template
Browse files Browse the repository at this point in the history
  • Loading branch information
kilbot committed Jul 10, 2023
1 parent 0f97f40 commit 4b2d0da
Show file tree
Hide file tree
Showing 9 changed files with 264 additions and 93 deletions.
2 changes: 1 addition & 1 deletion includes/Init.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public function send_headers(): void {
header( 'Access-Control-Allow-Origin: *' );
header( 'Access-Control-Expose-Headers: Link' );
}
}
}

/**
* Loads POS integrations with third party plugins.
Expand Down
140 changes: 89 additions & 51 deletions includes/Templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
namespace WCPOS\WooCommercePOS;

use WC_Order;
use WCPOS\WooCommercePOS\Templates\Frontend;

/**
*
Expand All @@ -17,43 +16,47 @@ class Templates {
/**
* @var string POS frontend slug
*/
private $pos_slug;
private $pos_regex;

/**
* @var string POS login slug
*/
private $pos_login_regex;

/**
* @var string POS checkout slug
* @note 'wcpos-checkout' slug is used instead 'checkout' to avoid conflicts with WC checkout
* eg: x-frame-options: SAMEORIGIN
*/
private $pos_checkout_slug;
private $pos_checkout_regex;

/**
* @var string regex match for frontend rewite_rule
*/
private $pos_rewrite_regex;

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

$this->add_rewrite_rules();

public function __construct() {
$this->pos_slug = Admin\Permalink::get_slug();
$this->pos_rewrite_regex = '^' . $this->pos_slug . '/?';
$this->pos_checkout_slug = 'wcpos-checkout';
$this->pos_checkout_rewrite_regex = '^' . $this->pos_checkout_slug . '/([a-z-]+)/([0-9]+)[/]?$';

// Note: 'order-pay' and 'order-received' rewrite tags are added by WC
add_rewrite_tag( '%wcpos%', '([^&]+)' );
add_rewrite_tag( '%wcpos-receipt%', '([^&]+)' );
add_rewrite_rule( $this->pos_rewrite_regex, 'index.php?wcpos=1', 'top' );
add_rewrite_rule( $this->pos_checkout_rewrite_regex, 'index.php?$matches[1]=$matches[2]&wcpos=1', 'top' );
add_filter( 'option_rewrite_rules', array( $this, 'rewrite_rules' ), 1 );
add_action( 'template_redirect', array( $this, 'template_redirect' ), 1 );

add_filter( 'woocommerce_get_checkout_order_received_url', array( $this, 'order_received_url' ), 10, 2 );
}

/**
* @NOTE: 'order-pay' and 'order-received' rewrite tags are added by WC
*
* @return void
*/
private function add_rewrite_rules() {
add_rewrite_tag( '%wcpos%', '([^&]+)' );
add_rewrite_tag( '%wcpos-receipt%', '([^&]+)' );
add_rewrite_tag( '%wcpos-login%', '([^&]+)' );
add_rewrite_rule( $this->pos_regex, 'index.php?wcpos=1', 'top' );
add_rewrite_rule( $this->pos_login_regex, 'index.php?wcpos-login=1', 'top' );
add_rewrite_rule( $this->pos_checkout_regex, 'index.php?$matches[1]=$matches[2]&wcpos=1', 'top' );
}

/**
* Make sure cache contains POS rewrite rules.
*
Expand All @@ -62,7 +65,7 @@ public function __construct() {
* @return array|bool
*/
public function rewrite_rules( $rules ) {
return isset( $rules[ $this->pos_rewrite_regex ], $rules[ $this->pos_checkout_rewrite_regex ] ) ? $rules : false;
return isset( $rules[ $this->pos_regex ], $rules[ $this->pos_login_regex ], $rules[ $this->pos_checkout_regex ] ) ? $rules : false;
}

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

$query_var_classname_map = array(
'order-pay' => __NAMESPACE__ . '\\Templates\\Payment',
'order-received' => __NAMESPACE__ . '\\Templates\\Received',
'wcpos-receipt' => __NAMESPACE__ . '\\Templates\\Receipt',
);

if ( $wp->matched_rule === $this->pos_checkout_rewrite_regex ) {
foreach ( $query_var_classname_map as $query_var => $classname ) {
if ( isset( $wp->query_vars[ $query_var ] ) ) {
$order_id = absint( $wp->query_vars[ $query_var ] );

if ( class_exists( $classname ) && $order_id ) {
$template = new $classname( $order_id );
$template->get_template();
exit;
} else {
wp_die( esc_html__( 'Template not found.', 'woocommerce-pos' ) );
}
}
}
}

if ( $wp->matched_rule === $this->pos_rewrite_regex ) {
$template = new Frontend();
$template->get_template();
exit;
}
$rewrite_rules_to_templates = array(
$this->pos_regex => __NAMESPACE__ . '\\Templates\\Frontend',
$this->pos_login_regex => __NAMESPACE__ . '\\Templates\\Login',
$this->pos_checkout_regex => array(
'order-pay' => __NAMESPACE__ . '\\Templates\\Payment',
'order-received' => __NAMESPACE__ . '\\Templates\\Received',
'wcpos-receipt' => __NAMESPACE__ . '\\Templates\\Receipt',
),
);

foreach ( $rewrite_rules_to_templates as $rule => $classname ) {
if ( $wp->matched_rule === $rule ) {
if ( is_array( $classname ) ) {
$this->load_checkout_template( $classname );
} else {
$this->load_template( $classname );
}
exit;
}
}
}

/**
* Loads order templates, additionally checks query var is a valid order id
*
* @param array $classnames
*
* @return void
*/
private function load_checkout_template( array $classnames ): void {
global $wp;

foreach ( $classnames as $query_var => $classname ) {
if ( isset( $wp->query_vars[ $query_var ] ) ) {
$order_id = absint( $wp->query_vars[ $query_var ] );

if ( class_exists( $classname ) && $order_id ) {
$template = new $classname( $order_id );
$template->get_template();
return;
}
}
}

wp_die( esc_html__( 'Template not found.', 'woocommerce-pos' ) );
}

/**
* Loads all other templates
*
* @param string $classname
*
* @return void
*/
private function load_template( string $classname ): void {
if ( class_exists( $classname ) ) {
$template = new $classname();
$template->get_template();
return;
}

wp_die( esc_html__( 'Template not found.', 'woocommerce-pos' ) );
}


/**
* Just like the checkout/payment.php template, we hijack the order received url so we can display a stripped down
Expand Down
39 changes: 2 additions & 37 deletions includes/Templates/Frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,6 @@ public function get_template(): void {
// last chance before template is rendered
do_action( 'woocommerce_pos_template_redirect' );

// if modal login
if ( isset( $_GET['modal'] ) && $_GET['modal'] === '1' ) {
$this->modal_login();
exit;
}

// add head & footer actions
add_action( 'woocommerce_pos_head', array( $this, 'head' ) );
add_action( 'woocommerce_pos_footer', array( $this, 'footer' ) );
Expand All @@ -75,7 +69,7 @@ public function get_template(): void {
* @return mixed
*/
public function login_url( $login_url ) {
return add_query_arg( SHORT_NAME, '1', $login_url );
return add_query_arg( SHORT_NAME, '1', $login_url );
}

/**
Expand Down Expand Up @@ -135,7 +129,7 @@ public function footer(): void {
'last_access' => '',
'avatar_url' => get_avatar_url( $user->ID ),
'wp_nonce' => wp_create_nonce( 'wp_rest' ),
// 'jwt' => $jwt,
// 'jwt' => $jwt,
),
'stores' => $store_settings->get_stores(),
);
Expand Down Expand Up @@ -215,33 +209,4 @@ private function no_cache(): void {
do_action( 'litespeed_control_set_nocache', 'nocache WoCommerce POS web application' );

}

private function modal_login() {
$user = wp_get_current_user();
$user_data = $this->auth_service->get_user_data( $user );
$credentials = wp_json_encode( $user_data );

echo "<script>
(function() {
// Parse the order JSON from PHP
var credentials = " . $credentials . "
// Check if postMessage function exists for window.top
if (typeof window.top.postMessage === 'function') {
window.top.postMessage({
action: 'wcpos-wp-credentials',
payload: credentials
}, '*');
}
// Check if ReactNativeWebView object and postMessage function exists
if (typeof window.ReactNativeWebView !== 'undefined' && typeof window.ReactNativeWebView.postMessage === 'function') {
window.ReactNativeWebView.postMessage(JSON.stringify({
action: 'wcpos-wp-credentials',
payload: credentials
}));
}
})();
</script>" . "\n";
}
}
96 changes: 96 additions & 0 deletions includes/Templates/Login.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace WCPOS\WooCommercePOS\Templates;

use WCPOS\WooCommercePOS\Services\Auth;

class Login {
/**
*
*/
protected $auth_service;


public function __construct() {
$this->auth_service = new Auth();
$this->send_headers();
}

/**
* Send headers
*
* @return void
*/
private function send_headers() {
header( 'Content-Security-Policy: frame-ancestors http://localhost:* https://localhost:*' );
}


public function get_template(): void {
$login_attempt = isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'woocommerce_pos_login' );

// Login attempt detected
$error_string = '';
if ( $login_attempt ) {
$creds = array();
$creds['user_login'] = $_POST['log'];
$creds['user_password'] = $_POST['pwd'];
// $creds['remember'] = isset( $_POST['rememberme'] );

$user = wp_signon( $creds, false );

if ( is_wp_error( $user ) ) {
foreach ( $user->errors as $error ) {
$error_string .= '<p class="error">' . $error[0] . '</p>';
}
} else {
$this->login_success();
exit;
}
}

//
do_action( 'login_init' );

//
do_action( 'login_form_login' );

include woocommerce_pos_locate_template( 'login.php' );
exit;
}

/**
* Login success
*
* @return void
*/
private function login_success() {
$user = wp_get_current_user();
$user_data = $this->auth_service->get_user_data( $user );
$credentials = wp_json_encode( $user_data );

echo '<script>
(function() {
// Parse the order JSON from PHP
var credentials = ' . $credentials . "
// Check if postMessage function exists for window.top
if (typeof window.top.postMessage === 'function') {
window.top.postMessage({
action: 'wcpos-wp-credentials',
payload: credentials
}, '*');
}
// Check if ReactNativeWebView object and postMessage function exists
if (typeof window.ReactNativeWebView !== 'undefined' && typeof window.ReactNativeWebView.postMessage === 'function') {
window.ReactNativeWebView.postMessage(JSON.stringify({
action: 'wcpos-wp-credentials',
payload: credentials
}));
}
})();
</script>" . "\n";
}

}
Loading

0 comments on commit 4b2d0da

Please sign in to comment.