88namespace WCPOS \WooCommercePOS \Templates ;
99
1010use Exception ;
11+ use WCPOS \WooCommercePOS \Logger ;
12+ use WCPOS \WooCommercePOS \Services \Settings ;
1113use function define ;
1214use 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
0 commit comments