Skip to content

Commit e504009

Browse files
committed
Add dequeue form for checkout and bump to v1.3.0
1 parent 537b897 commit e504009

File tree

11 files changed

+258
-97
lines changed

11 files changed

+258
-97
lines changed

includes/API/Orders.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ public function rest_request_before_callbacks( $response, $handler, $request ) {
8888
unset( $error_data['params']['line_items'], $error_data['details']['line_items'] );
8989
}
9090
}
91+
92+
// Check if 'line_items[X][parent_name]' has 'rest_invalid_type'
93+
// Use a regular expression to match 'line_items[X][parent_name]', where X is a number
94+
if ( $line_items_details['code'] === 'rest_invalid_type' &&
95+
preg_match( '/^line_items\[\d+\]\[parent_name\]$/', $line_items_details['data']['param'] ) ) {
96+
unset( $error_data['params']['line_items'], $error_data['details']['line_items'] );
97+
}
9198
}
9299

93100
// Check if the invalid parameter was 'billing'

includes/Init.php

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,6 @@ public function __construct() {
3434
// Headers for API discoverability
3535
add_filter( 'rest_pre_serve_request', array( $this, 'rest_pre_serve_request' ), 5, 4 );
3636
add_action( 'send_headers', array( $this, 'send_headers' ), 99, 1 );
37-
38-
// Hack - remove when possible
39-
add_filter( 'woocommerce_rest_shop_order_schema', array( $this, 'shop_order_schema' ), 10, 1 );
40-
}
41-
42-
/**
43-
* Hack to fix the shop order schema.
44-
*
45-
* @TODO - submit a PR to WooCommerce
46-
*/
47-
public function shop_order_schema( array $schema ): array {
48-
if ( isset( $schema['line_items']['items']['properties']['parent_name']['type'] ) ) {
49-
if ( 'string' === $schema['line_items']['items']['properties']['parent_name']['type'] ) {
50-
$schema['line_items']['items']['properties']['parent_name']['type'] = 'mixed';
51-
}
52-
}
53-
54-
return $schema;
5537
}
5638

5739
/**

includes/Services/Settings.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,20 @@ class Settings {
3333
'order_status' => 'wc-completed',
3434
'admin_emails' => true,
3535
'customer_emails' => true,
36+
// this is used in the POS, not in WP Admin (at the moment)
37+
'dequeue_script_handles' => array(
38+
'admin-bar',
39+
'wc-add-to-cart',
40+
),
41+
'dequeue_style_handles' => array(
42+
'admin-bar',
43+
'woocommerce-general',
44+
'woocommerce-inline',
45+
'woocommerce-layout',
46+
'woocommerce-smallscreen',
47+
'woocommerce-blocktheme',
48+
'wp-block-library',
49+
),
3650
),
3751
'payment_gateways' => array(
3852
'default_gateway' => 'pos_cash',

includes/Templates/Login.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function __construct() {
2323
* @return void
2424
*/
2525
private function send_headers() {
26-
header( 'Content-Security-Policy: frame-ancestors http://localhost:* https://localhost:*' );
26+
header( 'Content-Security-Policy: frame-ancestors *' );
2727
}
2828

2929
/**

includes/Templates/Payment.php

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

1010
use Exception;
11+
use WCPOS\WooCommercePOS\Logger;
12+
use WCPOS\WooCommercePOS\Services\Settings;
1113
use function define;
1214
use function defined;
1315

@@ -24,6 +26,7 @@ class Payment {
2426

2527
public function __construct( int $order_id ) {
2628
$this->order_id = $order_id;
29+
$this->check_troubleshooting_form_submission();
2730
//$this->gateway_id = isset( $_GET['gateway'] ) ? sanitize_key( wp_unslash( $_GET['gateway'] ) ) : '';
2831

2932
// this is a checkout page
@@ -45,7 +48,7 @@ public function __construct( int $order_id ) {
4548
remove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 );
4649
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );
4750

48-
add_action( 'wp_enqueue_scripts', array( $this, 'remove_scripts' ), 100 );
51+
add_action( 'wp_enqueue_scripts', array( $this, 'remove_scripts_and_styles' ), 100 );
4952

5053
add_filter( 'option_woocommerce_tax_display_cart', array( $this, 'tax_display_cart' ), 10, 2 );
5154
}
@@ -57,69 +60,114 @@ public function __construct( int $order_id ) {
5760
*
5861
* @return void
5962
*/
60-
public function remove_scripts(): void {
61-
global $wp_styles, $wp_scripts;
62-
63-
// Exclude list of handles
64-
// @TODO - this should be a filter
65-
$exclude_list = array(
66-
'admin-bar',
67-
'woocommerce-general',
68-
'woocommerce-inline',
69-
'woocommerce-layout',
70-
'woocommerce-smallscreen',
71-
'woocommerce-blocktheme',
72-
'wp-block-library', // are we using blocks?
73-
);
74-
75-
// Include list of handles
76-
// @TODO - this should be a filter
77-
$include_list = array();
78-
79-
// Get the active theme's directory
80-
$active_theme_directory = basename( get_template_directory() );
81-
82-
// Loop through all enqueued styles
83-
foreach ( $wp_styles->queue as $handle ) {
84-
// Skip blacklisted handles
85-
if ( in_array( $handle, $include_list ) ) {
86-
continue;
87-
}
88-
89-
$src = $wp_styles->registered[ $handle ]->src;
90-
91-
// Check if the source URL contains the active theme's directory
92-
if ( strpos( $src, $active_theme_directory ) !== false || in_array( $handle, $exclude_list ) ) {
93-
wp_dequeue_style( $handle );
94-
}
95-
}
96-
97-
// Loop through all enqueued scripts
98-
foreach ( $wp_scripts->queue as $handle ) {
99-
// Skip blacklisted handles
100-
if ( in_array( $handle, $include_list ) ) {
101-
continue;
102-
}
103-
104-
$src = $wp_scripts->registered[ $handle ]->src;
105-
106-
// Check if the source URL contains the active theme's directory
107-
if ( strpos( $src, $active_theme_directory ) !== false || in_array( $handle, $exclude_list ) ) {
108-
wp_dequeue_script( $handle );
109-
}
110-
}
111-
112-
}
113-
114-
115-
public function get_template(): void {
63+
/**
64+
* Remove enqueued scripts and styles.
65+
*
66+
* This function dequeues all scripts and styles that are not specified in the WooCommerce POS settings,
67+
* unless they are specifically included by the 'woocommerce_pos_payment_template_dequeue_script_handles'
68+
* and 'woocommerce_pos_payment_template_dequeue_style_handles' filters.
69+
*
70+
* @since 1.3.0
71+
*/
72+
public function remove_scripts_and_styles(): void {
73+
global $wp_styles, $wp_scripts;
74+
75+
/**
76+
* List of script handles to exclude from the payment template.
77+
*
78+
* @since 1.3.0
79+
*/
80+
$script_exclude_list = apply_filters(
81+
'woocommerce_pos_payment_template_dequeue_script_handles',
82+
woocommerce_pos_get_settings( 'checkout', 'dequeue_script_handles' )
83+
);
84+
85+
/**
86+
* List of style handles to exclude from the payment template.
87+
*
88+
* @since 1.3.0
89+
*/
90+
$style_exclude_list = apply_filters(
91+
'woocommerce_pos_payment_template_dequeue_style_handles',
92+
woocommerce_pos_get_settings( 'checkout', 'dequeue_style_handles' )
93+
);
94+
95+
// Loop through all enqueued styles and dequeue those that are in the exclusion list
96+
if ( is_array( $style_exclude_list ) ) {
97+
foreach ( $wp_styles->queue as $handle ) {
98+
if ( in_array( $handle, $style_exclude_list ) ) {
99+
wp_dequeue_style( $handle );
100+
}
101+
}
102+
}
103+
104+
// Loop through all enqueued scripts and dequeue those that are in the exclusion list
105+
if ( is_array( $script_exclude_list ) ) {
106+
foreach ( $wp_scripts->queue as $handle ) {
107+
if ( in_array( $handle, $script_exclude_list ) ) {
108+
wp_dequeue_script( $handle );
109+
}
110+
}
111+
}
112+
}
113+
114+
115+
private function check_troubleshooting_form_submission() {
116+
// Check if our form has been submitted
117+
if ( isset( $_POST['troubleshooting_form_nonce'] ) ) {
118+
// Verify the nonce
119+
if ( ! wp_verify_nonce( $_POST['troubleshooting_form_nonce'], 'troubleshooting_form_action' ) ) {
120+
// Nonce doesn't verify, we should stop execution here
121+
die( 'Nonce value cannot be verified.' );
122+
}
123+
124+
// This will hold your sanitized data
125+
$sanitized_data = array();
126+
127+
// Sanitize all_styles array
128+
if ( isset( $_POST['all_styles'] ) && is_array( $_POST['all_styles'] ) ) {
129+
$sanitized_data['all_styles'] = array_map( 'sanitize_text_field', $_POST['all_styles'] );
130+
}
131+
132+
// Sanitize styles array
133+
if ( isset( $_POST['styles'] ) && is_array( $_POST['styles'] ) ) {
134+
$sanitized_data['styles'] = array_map( 'sanitize_text_field', $_POST['styles'] );
135+
}
136+
137+
// Sanitize all_scripts array
138+
if ( isset( $_POST['all_scripts'] ) && is_array( $_POST['all_scripts'] ) ) {
139+
$sanitized_data['all_scripts'] = array_map( 'sanitize_text_field', $_POST['all_scripts'] );
140+
}
141+
142+
// Sanitize scripts array
143+
if ( isset( $_POST['scripts'] ) && is_array( $_POST['scripts'] ) ) {
144+
$sanitized_data['scripts'] = array_map( 'sanitize_text_field', $_POST['scripts'] );
145+
}
146+
147+
// Calculate unchecked styles and scripts
148+
$unchecked_styles = isset( $sanitized_data['all_styles'], $sanitized_data['styles'] ) ? array_diff( $sanitized_data['all_styles'], $sanitized_data['styles'] ) : array();
149+
$unchecked_scripts = isset( $sanitized_data['all_scripts'], $sanitized_data['scripts'] ) ? array_diff( $sanitized_data['all_scripts'], $sanitized_data['scripts'] ) : array();
150+
151+
// @TODO - the save settings function should allow saving by key
152+
$settings = new Settings();
153+
$checkout_settings = $settings->get_checkout_settings();
154+
$new_settings = array_replace_recursive(
155+
$checkout_settings,
156+
array( 'dequeue_style_handles' => $unchecked_styles ),
157+
array( 'dequeue_script_handles' => $unchecked_scripts )
158+
);
159+
$settings->save_settings( 'checkout', $new_settings );
160+
}
161+
}
162+
163+
public function get_template(): void {
116164
if ( ! defined( 'WOOCOMMERCE_CHECKOUT' ) ) {
117165
define( 'WOOCOMMERCE_CHECKOUT', true );
118166
}
119167

120-
// if ( ! $this->gateway_id ) {
121-
// wp_die( esc_html__( 'No gateway selected', 'woocommerce-pos' ) );
122-
// }
168+
// if ( ! $this->gateway_id ) {
169+
// wp_die( esc_html__( 'No gateway selected', 'woocommerce-pos' ) );
170+
// }
123171

124172
do_action( 'woocommerce_pos_before_pay' );
125173

@@ -165,10 +213,10 @@ public function get_template(): void {
165213
WC()->payment_gateways()->init();
166214
$available_gateways = WC()->payment_gateways->get_available_payment_gateways();
167215

168-
// if ( isset( $available_gateways[ $this->gateway_id ] ) ) {
169-
// $gateway = $available_gateways[ $this->gateway_id ];
170-
// $gateway->chosen = true;
171-
// }
216+
// if ( isset( $available_gateways[ $this->gateway_id ] ) ) {
217+
// $gateway = $available_gateways[ $this->gateway_id ];
218+
// $gateway->chosen = true;
219+
// }
172220

173221
$order_button_text = apply_filters( 'woocommerce_pay_order_button_text', __( 'Pay for order', 'woocommerce-pos' ) );
174222

includes/Templates/Receipt.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ class Receipt {
2222
public function __construct( int $order_id ) {
2323
$this->order_id = $order_id;
2424

25-
add_action( 'woocommerce_pos_receipt_head' , array( $this, 'receipt_head' ) );
25+
add_filter( 'show_admin_bar', '__return_false' );
26+
add_action( 'woocommerce_pos_receipt_head', array( $this, 'receipt_head' ) );
2627
}
2728

2829
/**
@@ -59,9 +60,9 @@ public function get_template(): void {
5960
wp_die( esc_html__( 'Sorry, this order is invalid.', 'woocommerce-pos' ) );
6061
}
6162

62-
// if ( ! $order->is_paid() ) {
63-
// wp_die( esc_html__( 'Sorry, this order has not been paid.', 'woocommerce-pos' ) );
64-
// }
63+
// if ( ! $order->is_paid() ) {
64+
// wp_die( esc_html__( 'Sorry, this order has not been paid.', 'woocommerce-pos' ) );
65+
// }
6566

6667
if ( isset( $_GET['template'] ) ) {
6768
if ( 'legacy' === $_GET['template'] ) {

includes/Templates/Received.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ class Received {
1818

1919
public function __construct( int $order_id ) {
2020
$this->order_id = $order_id;
21-
}
21+
22+
add_filter('show_admin_bar', '__return_false');
23+
}
2224

2325

2426
public function get_template(): void {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@wcpos/woocommerce-pos",
3-
"version": "1.2.4",
3+
"version": "1.3.0",
44
"description": "A simple front-end for taking WooCommerce orders at the Point of Sale.",
55
"main": "index.js",
66
"workspaces": {

readme.txt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
Contributors: kilbot
33
Tags: cart, e-commerce, ecommerce, inventory, point-of-sale, pos, sales, sell, shop, shopify, store, vend, woocommerce, wordpress-ecommerce
44
Requires at least: 5.6 & WooCommerce 5.3
5-
Tested up to: 6.2
6-
Stable tag: 1.2.4
5+
Tested up to: 6.3
6+
Stable tag: 1.3.0
77
License: GPL-3.0
88
License URI: http://www.gnu.org/licenses/gpl-3.0.html
99

@@ -63,6 +63,13 @@ There is more information on our website at [https://wcpos.com](https://wcpos.co
6363

6464
== Changelog ==
6565

66+
= 1.3.0 - 2023/07/27 =
67+
* Major stability improvements!
68+
* Fix: Login is now via JWT - no more 'Cookie nonce' errors
69+
* Fix: Many fixes to local data sync and search should make the POS much more enjoyable to use
70+
* Add: Dequeue WordPress styles and scripts on POS checkout page
71+
* Fix: various fixes to the POS settings in WP Admin
72+
6673
= 1.2.4 - 2023/07/25 =
6774
* Fix: empty products effecting some users due to malformed meta_data
6875

0 commit comments

Comments
 (0)