Skip to content

Commit 33371d3

Browse files
committed
refactor: remove duplicate method and overly broad event handler
- Remove duplicate is_block_checkout_page() from WCMoneiPaymentGatewayComponent (inherits from parent) - Remove overly broad click handler on form#order_review in Apple/Google Pay classic checkout - Click handler fired on ANY click in form; coverage maintained by targeted change listener (lines 90-96), initial check (lines 85-88), and MutationObserver (lines 30-47)
1 parent 4386c2a commit 33371d3

File tree

3 files changed

+94
-92
lines changed

3 files changed

+94
-92
lines changed

assets/js/monei-apple-google-classic.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,6 @@
1919
wc_monei_form.init_apple_google_pay();
2020
}
2121
} );
22-
// On Pay for order form.
23-
$( 'form#order_review' ).on( 'click', function () {
24-
if ( wc_monei_form.is_apple_selected() ) {
25-
wc_monei_form.init_checkout_apple_google();
26-
wc_monei_form.init_apple_google_pay();
27-
}
28-
} );
2922

3023
const targetNode = document.getElementById( 'order_review' );
3124

@@ -211,6 +204,17 @@
211204
wc_monei_form.instantiate_payment_request();
212205
},
213206
instantiate_payment_request() {
207+
// Check if MONEI SDK is loaded
208+
if (
209+
typeof window.monei === 'undefined' ||
210+
typeof monei === 'undefined'
211+
) {
212+
console.error(
213+
'MONEI SDK is not loaded. Cannot initialize Apple/Google Pay.'
214+
);
215+
return;
216+
}
217+
214218
const paymentRequest = monei.PaymentRequest( {
215219
accountId: wc_monei_apple_google_params.accountId,
216220
sessionId: wc_monei_apple_google_params.sessionId,

src/Gateways/Abstracts/WCMoneiPaymentGatewayComponent.php

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -317,34 +317,6 @@ public function isBlockCheckout() {
317317
return ( isset( $_POST['monei_is_block_checkout'] ) ) ? wc_clean( wp_unslash( $_POST['monei_is_block_checkout'] ) ) === 'yes' : false; // WPCS: CSRF ok.
318318
}
319319

320-
/**
321-
* Check if the checkout page is using WooCommerce Blocks.
322-
* Used for script enqueuing to differentiate between classic and blocks checkout.
323-
*
324-
* @return bool
325-
*/
326-
public function is_block_checkout_page() {
327-
// Order-pay and add payment method pages are always classic
328-
if ( is_checkout_pay_page() || is_add_payment_method_page() ) {
329-
return false;
330-
}
331-
if ( ! is_checkout() ) {
332-
return false;
333-
}
334-
if ( ! class_exists( 'WC_Blocks_Utils' ) ) {
335-
return false;
336-
}
337-
// Check if the checkout block is present
338-
$has_block = WC_Blocks_Utils::has_block_in_page( wc_get_page_id( 'checkout' ), 'woocommerce/checkout' );
339-
340-
// Additional check: see if the traditional checkout shortcode is present
341-
$checkout_page = get_post( wc_get_page_id( 'checkout' ) );
342-
$has_shortcode = $checkout_page ? has_shortcode( $checkout_page->post_content, 'woocommerce_checkout' ) : false;
343-
344-
// If the block is present and the shortcode is not, we can be more confident it's a block checkout
345-
return $has_block && ! $has_shortcode;
346-
}
347-
348320
/**
349321
* Frontend MONEI cardholderName.
350322
*

tests/phpstan-bootstrap.php

Lines changed: 83 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,207 +1,233 @@
11
<?php
2+
23
/**
34
* PHPStan Bootstrap File
45
* Defines constants and global functions for static analysis
56
*/
67

78
// Define plugin constants
8-
define( 'MONEI_VERSION', '6.4.0' );
9-
define( 'MONEI_MAIN_FILE', __DIR__ . '/../monei.php' );
10-
define( 'MONEI_SIGNUP', 'https://dashboard.monei.com/signup' );
11-
define( 'MONEI_WEB', 'https://monei.com' );
12-
define( 'MONEI_SUPPORT', 'https://support.monei.com' );
13-
define( 'MONEI_REVIEW', 'https://wordpress.org/support/plugin/monei/reviews/' );
14-
define( 'MONEI_GATEWAY_ID', 'monei' );
9+
define('MONEI_VERSION', '6.4.0');
10+
define('MONEI_MAIN_FILE', __DIR__ . '/../monei.php');
11+
define('MONEI_SIGNUP', 'https://dashboard.monei.com/signup');
12+
define('MONEI_WEB', 'https://monei.com');
13+
define('MONEI_SUPPORT', 'https://support.monei.com');
14+
define('MONEI_REVIEW', 'https://wordpress.org/support/plugin/monei/reviews/');
15+
define('MONEI_GATEWAY_ID', 'monei');
1516

1617
// Define WordPress constants if not already defined
17-
if ( ! defined( 'ABSPATH' ) ) {
18-
define( 'ABSPATH', '/tmp/wordpress/' );
18+
if (!defined('ABSPATH')) {
19+
define('ABSPATH', '/tmp/wordpress/');
1920
}
2021

2122
// Define plugin helper functions that PHPStan should know about
22-
if ( ! function_exists( 'WC_Monei' ) ) {
23+
if (!function_exists('WC_Monei')) {
2324
/**
2425
* @return object
2526
*/
26-
function WC_Monei() {
27+
function WC_Monei()
28+
{
2729
return new class() {
28-
public function plugin_path() {
30+
public $version = MONEI_VERSION;
31+
32+
public function plugin_path()
33+
{
2934
return __DIR__ . '/..';
3035
}
31-
public function image_url( $file ) {
36+
37+
public function image_url($file)
38+
{
3239
return 'https://example.com/' . $file;
3340
}
34-
public function get_ipn_url() {
41+
42+
public function get_ipn_url()
43+
{
3544
return 'https://example.com/ipn';
3645
}
3746
};
3847
}
3948
}
4049

41-
if ( ! function_exists( 'monei_price_format' ) ) {
50+
if (!function_exists('monei_price_format')) {
4251
/**
4352
* @param float $price
4453
* @return int
4554
*/
46-
function monei_price_format( $price ) {
47-
return (int) ( $price * 100 );
55+
function monei_price_format($price)
56+
{
57+
return (int) ($price * 100);
4858
}
4959
}
5060

51-
if ( ! function_exists( 'locale_iso_639_1_code' ) ) {
61+
if (!function_exists('locale_iso_639_1_code')) {
5262
/**
5363
* @return string
5464
*/
55-
function locale_iso_639_1_code() {
65+
function locale_iso_639_1_code()
66+
{
5667
return 'en';
5768
}
5869
}
5970

60-
if ( ! function_exists( 'monei_get_settings' ) ) {
71+
if (!function_exists('monei_get_settings')) {
6172
/**
6273
* @param string $key
6374
* @param string $gateway_id
6475
* @return mixed
6576
*/
66-
function monei_get_settings( $key, $gateway_id = '' ) {
77+
function monei_get_settings($key, $gateway_id = '')
78+
{
6779
return '';
6880
}
6981
}
7082

71-
if ( ! function_exists( 'monei_get_option_key_from_order' ) ) {
83+
if (!function_exists('monei_get_option_key_from_order')) {
7284
/**
7385
* @param \WC_Order $order
7486
* @return string
7587
*/
76-
function monei_get_option_key_from_order( $order ) {
88+
function monei_get_option_key_from_order($order)
89+
{
7790
return 'monei';
7891
}
7992
}
8093

81-
if ( ! function_exists( 'wc_clean' ) ) {
94+
if (!function_exists('wc_clean')) {
8295
/**
8396
* @param string|array $var
8497
* @return string|array
8598
*/
86-
function wc_clean( $var ) {
99+
function wc_clean($var)
100+
{
87101
return $var;
88102
}
89103
}
90104

91-
if ( ! function_exists( 'wp_unslash' ) ) {
105+
if (!function_exists('wp_unslash')) {
92106
/**
93107
* @param string|array $value
94108
* @return string|array
95109
*/
96-
function wp_unslash( $value ) {
110+
function wp_unslash($value)
111+
{
97112
return $value;
98113
}
99114
}
100115

101-
if ( ! function_exists( 'wc_price' ) ) {
116+
if (!function_exists('wc_price')) {
102117
/**
103118
* @param float $price
104119
* @return string
105120
*/
106-
function wc_price( $price ) {
107-
return '$' . number_format( $price, 2 );
121+
function wc_price($price)
122+
{
123+
return '$' . number_format($price, 2);
108124
}
109125
}
110126

111-
if ( ! function_exists( 'wc_get_order' ) ) {
127+
if (!function_exists('wc_get_order')) {
112128
/**
113129
* @param int $order_id
114130
* @return \WC_Order|false
115131
*/
116-
function wc_get_order( $order_id ) {
132+
function wc_get_order($order_id)
133+
{
117134
return false;
118135
}
119136
}
120137

121-
if ( ! function_exists( 'set_transient' ) ) {
138+
if (!function_exists('set_transient')) {
122139
/**
123140
* @param string $transient
124141
* @param mixed $value
125142
* @param int $expiration
126143
* @return bool
127144
*/
128-
function set_transient( $transient, $value, $expiration = 0 ) {
145+
function set_transient($transient, $value, $expiration = 0)
146+
{
129147
return true;
130148
}
131149
}
132150

133-
if ( ! function_exists( 'get_transient' ) ) {
151+
if (!function_exists('get_transient')) {
134152
/**
135153
* @param string $transient
136154
* @return mixed
137155
*/
138-
function get_transient( $transient ) {
156+
function get_transient($transient)
157+
{
139158
return false;
140159
}
141160
}
142161

143-
if ( ! function_exists( 'delete_transient' ) ) {
162+
if (!function_exists('delete_transient')) {
144163
/**
145164
* @param string $transient
146165
* @return bool
147166
*/
148-
function delete_transient( $transient ) {
167+
function delete_transient($transient)
168+
{
149169
return true;
150170
}
151171
}
152172

153-
if ( ! function_exists( 'wp_rand' ) ) {
173+
if (!function_exists('wp_rand')) {
154174
/**
155175
* @param int $min
156176
* @param int $max
157177
* @return int
158178
*/
159-
function wp_rand( $min = 0, $max = 0 ) {
160-
return rand( $min, $max );
179+
function wp_rand($min = 0, $max = 0)
180+
{
181+
return rand($min, $max);
161182
}
162183
}
163184

164185
// Define legacy classes from includes/ directory
165-
if ( ! class_exists( 'WC_Monei_IPN' ) ) {
186+
if (!class_exists('WC_Monei_IPN')) {
166187
/**
167188
* Mock WC_Monei_IPN class for PHPStan
168189
*
169190
* @param bool $logging
170191
*/
171-
class WC_Monei_IPN {
172-
public function __construct( bool $logging = false ) {
192+
class WC_Monei_IPN
193+
{
194+
public function __construct(bool $logging = false)
195+
{
173196
// Stub implementation - parameter kept for signature compatibility
174-
unset( $logging );
197+
unset($logging);
175198
}
176199
}
177200
}
178201

179-
if ( ! class_exists( 'WC_Monei_Logger' ) ) {
202+
if (!class_exists('WC_Monei_Logger')) {
180203
/**
181204
* Mock WC_Monei_Logger class for PHPStan
182205
*/
183-
class WC_Monei_Logger {
184-
const LEVEL_INFO = 1;
206+
class WC_Monei_Logger
207+
{
208+
const LEVEL_INFO = 1;
185209
const LEVEL_WARNING = 2;
186-
const LEVEL_ERROR = 3;
187-
const LEVEL_NONE = 4;
210+
const LEVEL_ERROR = 3;
211+
const LEVEL_NONE = 4;
188212

189-
public static function log( $message, $severity = 1 ) {}
190-
public static function logDebug( $message ) {}
191-
public static function logWarning( $message ) {}
192-
public static function logError( $message ) {}
213+
public static function log($message, $severity = 1) {}
214+
public static function logDebug($message) {}
215+
public static function logWarning($message) {}
216+
public static function logError($message) {}
193217
}
194218
}
195219

196-
if ( ! class_exists( 'WC_Geolocation' ) ) {
220+
if (!class_exists('WC_Geolocation')) {
197221
/**
198222
* Mock WC_Geolocation class for PHPStan
199223
*/
200-
class WC_Geolocation {
224+
class WC_Geolocation
225+
{
201226
/**
202227
* @return string
203228
*/
204-
public static function get_ip_address() {
229+
public static function get_ip_address()
230+
{
205231
return '127.0.0.1';
206232
}
207233
}

0 commit comments

Comments
 (0)