Skip to content

Commit

Permalink
Add dequeue form for checkout and bump to v1.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
kilbot committed Jul 27, 2023
1 parent 537b897 commit e504009
Show file tree
Hide file tree
Showing 11 changed files with 258 additions and 97 deletions.
7 changes: 7 additions & 0 deletions includes/API/Orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ public function rest_request_before_callbacks( $response, $handler, $request ) {
unset( $error_data['params']['line_items'], $error_data['details']['line_items'] );
}
}

// Check if 'line_items[X][parent_name]' has 'rest_invalid_type'
// Use a regular expression to match 'line_items[X][parent_name]', where X is a number
if ( $line_items_details['code'] === 'rest_invalid_type' &&
preg_match( '/^line_items\[\d+\]\[parent_name\]$/', $line_items_details['data']['param'] ) ) {
unset( $error_data['params']['line_items'], $error_data['details']['line_items'] );
}
}

// Check if the invalid parameter was 'billing'
Expand Down
18 changes: 0 additions & 18 deletions includes/Init.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,6 @@ public function __construct() {
// Headers for API discoverability
add_filter( 'rest_pre_serve_request', array( $this, 'rest_pre_serve_request' ), 5, 4 );
add_action( 'send_headers', array( $this, 'send_headers' ), 99, 1 );

// Hack - remove when possible
add_filter( 'woocommerce_rest_shop_order_schema', array( $this, 'shop_order_schema' ), 10, 1 );
}

/**
* Hack to fix the shop order schema.
*
* @TODO - submit a PR to WooCommerce
*/
public function shop_order_schema( array $schema ): array {
if ( isset( $schema['line_items']['items']['properties']['parent_name']['type'] ) ) {
if ( 'string' === $schema['line_items']['items']['properties']['parent_name']['type'] ) {
$schema['line_items']['items']['properties']['parent_name']['type'] = 'mixed';
}
}

return $schema;
}

/**
Expand Down
14 changes: 14 additions & 0 deletions includes/Services/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ class Settings {
'order_status' => 'wc-completed',
'admin_emails' => true,
'customer_emails' => true,
// this is used in the POS, not in WP Admin (at the moment)
'dequeue_script_handles' => array(
'admin-bar',
'wc-add-to-cart',
),
'dequeue_style_handles' => array(
'admin-bar',
'woocommerce-general',
'woocommerce-inline',
'woocommerce-layout',
'woocommerce-smallscreen',
'woocommerce-blocktheme',
'wp-block-library',
),
),
'payment_gateways' => array(
'default_gateway' => 'pos_cash',
Expand Down
2 changes: 1 addition & 1 deletion includes/Templates/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __construct() {
* @return void
*/
private function send_headers() {
header( 'Content-Security-Policy: frame-ancestors http://localhost:* https://localhost:*' );
header( 'Content-Security-Policy: frame-ancestors *' );
}

/**
Expand Down
176 changes: 112 additions & 64 deletions includes/Templates/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
namespace WCPOS\WooCommercePOS\Templates;

use Exception;
use WCPOS\WooCommercePOS\Logger;
use WCPOS\WooCommercePOS\Services\Settings;
use function define;
use function defined;

Expand All @@ -24,6 +26,7 @@ class Payment {

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

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

add_action( 'wp_enqueue_scripts', array( $this, 'remove_scripts' ), 100 );
add_action( 'wp_enqueue_scripts', array( $this, 'remove_scripts_and_styles' ), 100 );

add_filter( 'option_woocommerce_tax_display_cart', array( $this, 'tax_display_cart' ), 10, 2 );
}
Expand All @@ -57,69 +60,114 @@ public function __construct( int $order_id ) {
*
* @return void
*/
public function remove_scripts(): void {
global $wp_styles, $wp_scripts;

// Exclude list of handles
// @TODO - this should be a filter
$exclude_list = array(
'admin-bar',
'woocommerce-general',
'woocommerce-inline',
'woocommerce-layout',
'woocommerce-smallscreen',
'woocommerce-blocktheme',
'wp-block-library', // are we using blocks?
);

// Include list of handles
// @TODO - this should be a filter
$include_list = array();

// Get the active theme's directory
$active_theme_directory = basename( get_template_directory() );

// Loop through all enqueued styles
foreach ( $wp_styles->queue as $handle ) {
// Skip blacklisted handles
if ( in_array( $handle, $include_list ) ) {
continue;
}

$src = $wp_styles->registered[ $handle ]->src;

// Check if the source URL contains the active theme's directory
if ( strpos( $src, $active_theme_directory ) !== false || in_array( $handle, $exclude_list ) ) {
wp_dequeue_style( $handle );
}
}

// Loop through all enqueued scripts
foreach ( $wp_scripts->queue as $handle ) {
// Skip blacklisted handles
if ( in_array( $handle, $include_list ) ) {
continue;
}

$src = $wp_scripts->registered[ $handle ]->src;

// Check if the source URL contains the active theme's directory
if ( strpos( $src, $active_theme_directory ) !== false || in_array( $handle, $exclude_list ) ) {
wp_dequeue_script( $handle );
}
}

}


public function get_template(): void {
/**
* Remove enqueued scripts and styles.
*
* This function dequeues all scripts and styles that are not specified in the WooCommerce POS settings,
* unless they are specifically included by the 'woocommerce_pos_payment_template_dequeue_script_handles'
* and 'woocommerce_pos_payment_template_dequeue_style_handles' filters.
*
* @since 1.3.0
*/
public function remove_scripts_and_styles(): void {
global $wp_styles, $wp_scripts;

/**
* List of script handles to exclude from the payment template.
*
* @since 1.3.0
*/
$script_exclude_list = apply_filters(
'woocommerce_pos_payment_template_dequeue_script_handles',
woocommerce_pos_get_settings( 'checkout', 'dequeue_script_handles' )
);

/**
* List of style handles to exclude from the payment template.
*
* @since 1.3.0
*/
$style_exclude_list = apply_filters(
'woocommerce_pos_payment_template_dequeue_style_handles',
woocommerce_pos_get_settings( 'checkout', 'dequeue_style_handles' )
);

// Loop through all enqueued styles and dequeue those that are in the exclusion list
if ( is_array( $style_exclude_list ) ) {
foreach ( $wp_styles->queue as $handle ) {
if ( in_array( $handle, $style_exclude_list ) ) {
wp_dequeue_style( $handle );
}
}
}

// Loop through all enqueued scripts and dequeue those that are in the exclusion list
if ( is_array( $script_exclude_list ) ) {
foreach ( $wp_scripts->queue as $handle ) {
if ( in_array( $handle, $script_exclude_list ) ) {
wp_dequeue_script( $handle );
}
}
}
}


private function check_troubleshooting_form_submission() {
// Check if our form has been submitted
if ( isset( $_POST['troubleshooting_form_nonce'] ) ) {
// Verify the nonce
if ( ! wp_verify_nonce( $_POST['troubleshooting_form_nonce'], 'troubleshooting_form_action' ) ) {
// Nonce doesn't verify, we should stop execution here
die( 'Nonce value cannot be verified.' );
}

// This will hold your sanitized data
$sanitized_data = array();

// Sanitize all_styles array
if ( isset( $_POST['all_styles'] ) && is_array( $_POST['all_styles'] ) ) {
$sanitized_data['all_styles'] = array_map( 'sanitize_text_field', $_POST['all_styles'] );
}

// Sanitize styles array
if ( isset( $_POST['styles'] ) && is_array( $_POST['styles'] ) ) {
$sanitized_data['styles'] = array_map( 'sanitize_text_field', $_POST['styles'] );
}

// Sanitize all_scripts array
if ( isset( $_POST['all_scripts'] ) && is_array( $_POST['all_scripts'] ) ) {
$sanitized_data['all_scripts'] = array_map( 'sanitize_text_field', $_POST['all_scripts'] );
}

// Sanitize scripts array
if ( isset( $_POST['scripts'] ) && is_array( $_POST['scripts'] ) ) {
$sanitized_data['scripts'] = array_map( 'sanitize_text_field', $_POST['scripts'] );
}

// Calculate unchecked styles and scripts
$unchecked_styles = isset( $sanitized_data['all_styles'], $sanitized_data['styles'] ) ? array_diff( $sanitized_data['all_styles'], $sanitized_data['styles'] ) : array();
$unchecked_scripts = isset( $sanitized_data['all_scripts'], $sanitized_data['scripts'] ) ? array_diff( $sanitized_data['all_scripts'], $sanitized_data['scripts'] ) : array();

// @TODO - the save settings function should allow saving by key
$settings = new Settings();
$checkout_settings = $settings->get_checkout_settings();
$new_settings = array_replace_recursive(
$checkout_settings,
array( 'dequeue_style_handles' => $unchecked_styles ),
array( 'dequeue_script_handles' => $unchecked_scripts )
);
$settings->save_settings( 'checkout', $new_settings );
}
}

public function get_template(): void {
if ( ! defined( 'WOOCOMMERCE_CHECKOUT' ) ) {
define( 'WOOCOMMERCE_CHECKOUT', true );
}

// if ( ! $this->gateway_id ) {
// wp_die( esc_html__( 'No gateway selected', 'woocommerce-pos' ) );
// }
// if ( ! $this->gateway_id ) {
// wp_die( esc_html__( 'No gateway selected', 'woocommerce-pos' ) );
// }

do_action( 'woocommerce_pos_before_pay' );

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

// if ( isset( $available_gateways[ $this->gateway_id ] ) ) {
// $gateway = $available_gateways[ $this->gateway_id ];
// $gateway->chosen = true;
// }
// if ( isset( $available_gateways[ $this->gateway_id ] ) ) {
// $gateway = $available_gateways[ $this->gateway_id ];
// $gateway->chosen = true;
// }

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

Expand Down
9 changes: 5 additions & 4 deletions includes/Templates/Receipt.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class Receipt {
public function __construct( int $order_id ) {
$this->order_id = $order_id;

add_action( 'woocommerce_pos_receipt_head' , array( $this, 'receipt_head' ) );
add_filter( 'show_admin_bar', '__return_false' );
add_action( 'woocommerce_pos_receipt_head', array( $this, 'receipt_head' ) );
}

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

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

if ( isset( $_GET['template'] ) ) {
if ( 'legacy' === $_GET['template'] ) {
Expand Down
4 changes: 3 additions & 1 deletion includes/Templates/Received.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ class Received {

public function __construct( int $order_id ) {
$this->order_id = $order_id;
}

add_filter('show_admin_bar', '__return_false');
}


public function get_template(): void {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@wcpos/woocommerce-pos",
"version": "1.2.4",
"version": "1.3.0",
"description": "A simple front-end for taking WooCommerce orders at the Point of Sale.",
"main": "index.js",
"workspaces": {
Expand Down
11 changes: 9 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
Contributors: kilbot
Tags: cart, e-commerce, ecommerce, inventory, point-of-sale, pos, sales, sell, shop, shopify, store, vend, woocommerce, wordpress-ecommerce
Requires at least: 5.6 & WooCommerce 5.3
Tested up to: 6.2
Stable tag: 1.2.4
Tested up to: 6.3
Stable tag: 1.3.0
License: GPL-3.0
License URI: http://www.gnu.org/licenses/gpl-3.0.html

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

== Changelog ==

= 1.3.0 - 2023/07/27 =
* Major stability improvements!
* Fix: Login is now via JWT - no more 'Cookie nonce' errors
* Fix: Many fixes to local data sync and search should make the POS much more enjoyable to use
* Add: Dequeue WordPress styles and scripts on POS checkout page
* Fix: various fixes to the POS settings in WP Admin

= 1.2.4 - 2023/07/25 =
* Fix: empty products effecting some users due to malformed meta_data

Expand Down
Loading

0 comments on commit e504009

Please sign in to comment.