88namespace WCPOS \WooCommercePOS ;
99
1010use 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
0 commit comments