From 81f282b06c8cdccab0f33477bb4442de0e6e0ef9 Mon Sep 17 00:00:00 2001 From: Paul Kilmurray Date: Tue, 18 Jun 2024 22:07:33 +0100 Subject: [PATCH] add wp_head and wp_footer settings for payment template --- includes/Admin/Products/List_Products.php | 4 +- includes/Admin/Products/Single_Product.php | 4 +- .../variation-metabox-visibility-select.php | 1 - includes/Products.php | 2 +- includes/Services/Settings.php | 2 + includes/Templates/Payment.php | 309 ++++++++++++++++-- readme.txt | 6 +- templates/payment.php | 289 ++++++---------- .../Test_Product_Variations_Controller.php | 32 +- .../includes/API/Test_Products_Controller.php | 43 ++- tests/includes/Test_Products.php | 69 +++- tests/includes/Test_WC_API.php | 65 +++- 12 files changed, 569 insertions(+), 257 deletions(-) diff --git a/includes/Admin/Products/List_Products.php b/includes/Admin/Products/List_Products.php index 7335e459..1746b121 100644 --- a/includes/Admin/Products/List_Products.php +++ b/includes/Admin/Products/List_Products.php @@ -258,7 +258,7 @@ public function quick_edit( $column_name, $post_type ): void { * @return void */ public static function quick_edit_save( WC_Product $product ): void { - if ( ! empty( $_POST['_pos_visibility'] ) ) { + if ( isset( $_POST['_pos_visibility'] ) ) { $settings_instance = Settings::instance(); $args = array( 'post_type' => 'products', @@ -275,7 +275,7 @@ public static function quick_edit_save( WC_Product $product ): void { * @return void */ public function bulk_edit_save( WC_Product $product ): void { - if ( ! empty( $_GET['_pos_visibility'] ) ) { + if ( isset( $_GET['_pos_visibility'] ) ) { $settings_instance = Settings::instance(); $args = array( 'post_type' => 'products', diff --git a/includes/Admin/Products/Single_Product.php b/includes/Admin/Products/Single_Product.php index b4781b43..7be5ef9e 100644 --- a/includes/Admin/Products/Single_Product.php +++ b/includes/Admin/Products/Single_Product.php @@ -250,8 +250,8 @@ public function post_submitbox_misc_actions(): void { public function after_variable_attributes_pos_only_products( $loop, $variation_data, $variation ): void { $selected = ''; $settings_instance = Settings::instance(); - $pos_only = $settings_instance->is_product_pos_only( $variation->ID ); - $online_only = $settings_instance->is_product_online_only( $variation->ID ); + $pos_only = $settings_instance->is_variation_pos_only( $variation->ID ); + $online_only = $settings_instance->is_variation_online_only( $variation->ID ); // Set $selected based on the visibility status. if ( $pos_only ) { diff --git a/includes/Admin/Products/templates/variation-metabox-visibility-select.php b/includes/Admin/Products/templates/variation-metabox-visibility-select.php index 3719f3a1..9863f81d 100644 --- a/includes/Admin/Products/templates/variation-metabox-visibility-select.php +++ b/includes/Admin/Products/templates/variation-metabox-visibility-select.php @@ -21,4 +21,3 @@

- diff --git a/includes/Products.php b/includes/Products.php index adf21166..3414ee8c 100644 --- a/includes/Products.php +++ b/includes/Products.php @@ -77,7 +77,7 @@ public function product_set_stock( WC_Product $product ): void { * @return string */ public function hide_pos_only_products( $where, $query ) { - // Ensure this only runs for the main WooCommerce shop queries + // Ensure this only runs for the main WooCommerce shop queries if ( ! is_admin() && $query->is_main_query() && ( is_shop() || is_product_category() || is_product_tag() ) ) { global $wpdb; diff --git a/includes/Services/Settings.php b/includes/Services/Settings.php index 11fc5534..8ecc8afd 100644 --- a/includes/Services/Settings.php +++ b/includes/Services/Settings.php @@ -56,6 +56,8 @@ class Settings { 'woocommerce-blocktheme', 'wp-block-library', ), + 'disable_wp_head' => false, + 'disable_wp_footer' => false, ), 'payment_gateways' => array( 'default_gateway' => 'pos_cash', diff --git a/includes/Templates/Payment.php b/includes/Templates/Payment.php index 542ffe64..7cc6f28f 100644 --- a/includes/Templates/Payment.php +++ b/includes/Templates/Payment.php @@ -10,22 +10,71 @@ use Exception; use WCPOS\WooCommercePOS\Services\Settings; +/** + * + */ class Payment { /** + * The order ID. + * * @var int */ private $order_id; /** + * The gateway ID. + * * @var string */ private $gateway_id; + /** + * The order. + * + * @var \WC_Order + */ + private $order; + + /** + * The coupon nonce. + * + * @var string + */ + private $coupon_nonce; + + /** + * The troubleshooting form nonce. + * + * @var string + */ + private $troubleshooting_form_nonce; + + /** + * Disable wp_head setting. + * + * @var bool + */ + private $disable_wp_head; + + /** + * Disable wp_footer setting. + * + * @var bool + */ + private $disable_wp_footer; + + /** + * Constructor. + */ 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'] ) ) : ''; + $settings_service = Settings::instance(); + $this->disable_wp_head = (bool) $settings_service->get_settings( 'checkout', 'disable_wp_head' ); + $this->disable_wp_footer = (bool) $settings_service->get_settings( 'checkout', 'disable_wp_footer' ); + // this is a checkout page add_filter( 'woocommerce_is_checkout', '__return_true' ); // remove the terms and conditions checkbox @@ -46,8 +95,6 @@ public function __construct( int $order_id ) { remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 ); 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 ); } /** @@ -124,26 +171,8 @@ public function get_template(): void { do_action( 'woocommerce_pos_before_pay' ); try { - // get order - $order = wc_get_order( $this->order_id ); - - // Order or payment link is invalid. - if ( ! $order || $order->get_id() !== $this->order_id ) { - wp_die( esc_html__( 'Sorry, this order is invalid and cannot be paid for.', 'woocommerce-pos' ) ); - } - - // Order has already been paid for. - if ( $order->is_paid() ) { - wp_die( esc_html__( 'Sorry, this order has already been paid for.', 'woocommerce-pos' ) ); - } - - // get cashier from order meta_data with key _pos_user - $cashier = $order->get_meta( '_pos_user', true ); - $cashier = get_user_by( 'id', $cashier ); - - // create nonce for cashier to apply coupons - $coupon_nonce = wp_create_nonce( 'pos_coupon_action' ); - $troubleshooting_form_nonce = wp_create_nonce( 'troubleshooting_form_nonce' ); + // initialize order and nonces before the user is switched to customer + $this->initialize_order_and_nonces(); /* * The wp_set_current_user() function changes the global user object but it does not authenticate the user @@ -152,7 +181,7 @@ public function get_template(): void { * * @TODO - is this the best way to do this? */ - wp_set_current_user( $order->get_customer_id() ); + wp_set_current_user( $this->order->get_customer_id() ); add_filter( 'nonce_user_logged_out', array( $this, 'nonce_user_logged_out' ), 10, 2 ); // create nonce for customer @@ -181,18 +210,26 @@ public function get_template(): void { } /** - * Filters the value of the woocommerce_tax_display_cart option. - * The POS is always exclusive of tax, so we show the same for the payments page to avoid confusion. - * - * @param mixed $value Value of the option. - * @param string $option Option name. + * Initialize the order and nonce properties. */ - public function tax_display_cart( $value, $option ): string { - return 'excl'; - } + private function initialize_order_and_nonces(): void { + $this->order = wc_get_order( $this->order_id ); + if ( ! $this->order || $this->order->get_id() !== $this->order_id ) { + wp_die( esc_html__( 'Sorry, this order is invalid and cannot be paid for.', 'woocommerce-pos' ) ); + } + + if ( $this->order->is_paid() ) { + wp_die( esc_html__( 'Sorry, this order has already been paid for.', 'woocommerce-pos' ) ); + } + + $this->coupon_nonce = wp_create_nonce( 'pos_coupon_action' ); + $this->troubleshooting_form_nonce = wp_create_nonce( 'troubleshooting_form_nonce' ); + } /** + * Save the settings from the troubleshooting form. + * * @return void */ private function check_troubleshooting_form_submission(): void { @@ -235,11 +272,17 @@ private function check_troubleshooting_form_submission(): void { $unchecked_styles = isset( $sanitized_data['all_styles'] ) ? array_diff( $sanitized_data['all_styles'], $sanitized_data['styles'] ) : array(); $unchecked_scripts = isset( $sanitized_data['all_scripts'] ) ? array_diff( $sanitized_data['all_scripts'], $sanitized_data['scripts'] ) : array(); + // Sanitize disable_wp_head and disable_wp_footer options + $disable_wp_head = isset( $_POST['disable_wp_head'] ) ? (bool) $_POST['disable_wp_head'] : false; + $disable_wp_footer = isset( $_POST['disable_wp_footer'] ) ? (bool) $_POST['disable_wp_footer'] : false; + // @TODO - the save settings function should allow saving by key $checkout_settings = woocommerce_pos_get_settings( 'checkout' ); $new_settings = array_merge( $checkout_settings, array( + 'disable_wp_head' => $disable_wp_head, + 'disable_wp_footer' => $disable_wp_footer, 'dequeue_style_handles' => $unchecked_styles, 'dequeue_script_handles' => $unchecked_scripts, ) @@ -250,6 +293,210 @@ private function check_troubleshooting_form_submission(): void { } } + /** + * Render the troubleshooting form HTML. + * + * @return string + */ + public function get_troubleshooting_form_html(): string { + global $wp_styles, $wp_scripts; + $styleHandles = $wp_styles->queue; + $scriptHandles = $wp_scripts->queue; + + $style_exclude_list = apply_filters( + 'woocommerce_pos_payment_template_dequeue_style_handles', + woocommerce_pos_get_settings( 'checkout', 'dequeue_style_handles' ) + ); + + $script_exclude_list = apply_filters( + 'woocommerce_pos_payment_template_dequeue_script_handles', + woocommerce_pos_get_settings( 'checkout', 'dequeue_script_handles' ) + ); + + $mergedStyleHandles = array_unique( array_merge( $styleHandles, $style_exclude_list ) ); + $mergedScriptHandles = array_unique( array_merge( $scriptHandles, $script_exclude_list ) ); + + ob_start(); + ?> +
+
+ +
+ +
+ + + order->get_meta( '_pos_user', true ); + $cashier = get_user_by( 'id', $cashier ); + + ob_start(); + ?> +
+ : + display_name ); ?> +
+ +
+ : + ID ? esc_html__( 'Guest', 'woocommerce-pos' ) : esc_html( $customer->display_name ); ?> +
+ + + +
+
+ + + + + order->get_items( 'coupon' ); + if ( $coupons ) { + echo '

' . __( 'Applied coupons', 'woocommerce' ) . '

'; + echo '
    '; + foreach ( $coupons as $coupon ) { + echo '
  • ' . esc_html( $coupon->get_code() ) . '
  • '; + } + echo '
'; + } + ?> +
+
+ - + disable_wp_head ) { + wp_head(); + } else { + // Manually call necessary functions when wp_head is disabled. + if ( function_exists( 'wp_enqueue_block_template_skip_link' ) ) { + wp_enqueue_block_template_skip_link(); + } + } + ?>