Completed
Push — master ( 8c878f...ef7f01 )
by Radoslav
01:53
created

WC_Stripe_Admin_Notices   F

Complexity

Total Complexity 78

Size/Duplication

Total Lines 318
Duplicated Lines 12.58 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 40
loc 318
rs 2.16
c 0
b 0
f 0
wmc 78
lcom 1
cbo 1

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A add_admin_notice() 0 7 1
A admin_notices() 0 25 4
A get_payment_methods() 0 13 1
F stripe_check_environment() 40 103 39
A payment_methods_check_environment() 0 17 5
D hide_notices() 0 64 21
A get_setting_link() 0 7 3
A stripe_updated() 0 8 3

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like WC_Stripe_Admin_Notices often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use WC_Stripe_Admin_Notices, and based on these observations, apply Extract Interface, too.

1
<?php
2
if ( ! defined( 'ABSPATH' ) ) {
3
	exit;
4
}
5
6
/**
7
 * Class that represents admin notices.
8
 *
9
 * @since 4.1.0
10
 */
11
class WC_Stripe_Admin_Notices {
12
	/**
13
	 * Notices (array)
14
	 * @var array
15
	 */
16
	public $notices = array();
17
18
	/**
19
	 * Constructor
20
	 *
21
	 * @since 4.1.0
22
	 */
23
	public function __construct() {
24
		add_action( 'admin_notices', array( $this, 'admin_notices' ) );
25
		add_action( 'wp_loaded', array( $this, 'hide_notices' ) );
26
		add_action( 'woocommerce_stripe_updated', array( $this, 'stripe_updated' ) );
27
	}
28
29
	/**
30
	 * Allow this class and other classes to add slug keyed notices (to avoid duplication).
31
	 *
32
	 * @since 1.0.0
33
	 * @version 4.0.0
34
	 */
35
	public function add_admin_notice( $slug, $class, $message, $dismissible = false ) {
36
		$this->notices[ $slug ] = array(
37
			'class'       => $class,
38
			'message'     => $message,
39
			'dismissible' => $dismissible,
40
		);
41
	}
42
43
	/**
44
	 * Display any notices we've collected thus far.
45
	 *
46
	 * @since 1.0.0
47
	 * @version 4.0.0
48
	 */
49
	public function admin_notices() {
50
		if ( ! current_user_can( 'manage_woocommerce' ) ) {
51
			return;
52
		}
53
54
		// Main Stripe payment method.
55
		$this->stripe_check_environment();
56
57
		// All other payment methods.
58
		$this->payment_methods_check_environment();
59
60
		foreach ( (array) $this->notices as $notice_key => $notice ) {
61
			echo '<div class="' . esc_attr( $notice['class'] ) . '" style="position:relative;">';
62
63
			if ( $notice['dismissible'] ) {
64
				?>
65
				<a href="<?php echo esc_url( wp_nonce_url( add_query_arg( 'wc-stripe-hide-notice', $notice_key ), 'wc_stripe_hide_notices_nonce', '_wc_stripe_notice_nonce' ) ); ?>" class="woocommerce-message-close notice-dismiss" style="position:relative;float:right;padding:9px 0px 9px 9px 9px;text-decoration:none;"></a>
66
				<?php
67
			}
68
69
			echo '<p>';
70
			echo wp_kses( $notice['message'], array( 'a' => array( 'href' => array(), 'target' => array() ) ) );
71
			echo '</p></div>';
72
		}
73
	}
74
75
	/**
76
	 * List of available payment methods.
77
	 *
78
	 * @since 4.1.0
79
	 * @return array
80
	 */
81
	public function get_payment_methods() {
82
		return array(
83
			'Alipay'     => 'WC_Gateway_Stripe_Alipay',
84
			'Bancontact' => 'WC_Gateway_Stripe_Bancontact',
85
			'EPS'        => 'WC_Gateway_Stripe_EPS',
86
			'Giropay'    => 'WC_Gateway_Stripe_Giropay',
87
			'iDeal'      => 'WC_Gateway_Stripe_Ideal',
88
			'Multibanco' => 'WC_Gateway_Stripe_Multibanco',
89
			'P24'        => 'WC_Gateway_Stripe_p24',
90
			'SEPA'       => 'WC_Gateway_Stripe_Sepa',
91
			'SOFORT'     => 'WC_Gateway_Stripe_Sofort',
92
		);
93
	}
94
95
	/**
96
	 * The backup sanity check, in case the plugin is activated in a weird way,
97
	 * or the environment changes after activation. Also handles upgrade routines.
98
	 *
99
	 * @since 1.0.0
100
	 * @version 4.0.0
101
	 */
102
	public function stripe_check_environment() {
103
		$show_style_notice  = get_option( 'wc_stripe_show_style_notice' );
104
		$show_ssl_notice    = get_option( 'wc_stripe_show_ssl_notice' );
105
		$show_keys_notice   = get_option( 'wc_stripe_show_keys_notice' );
106
		$show_3ds_notice    = get_option( 'wc_stripe_show_3ds_notice' );
107
		$show_phpver_notice = get_option( 'wc_stripe_show_phpver_notice' );
108
		$show_wcver_notice  = get_option( 'wc_stripe_show_wcver_notice' );
109
		$show_curl_notice   = get_option( 'wc_stripe_show_curl_notice' );
110
		$options            = get_option( 'woocommerce_stripe_settings' );
111
		$testmode           = ( isset( $options['testmode'] ) && 'yes' === $options['testmode'] ) ? true : false;
112
		$test_pub_key       = isset( $options['test_publishable_key'] ) ? $options['test_publishable_key'] : '';
113
		$test_secret_key    = isset( $options['test_secret_key'] ) ? $options['test_secret_key'] : '';
114
		$live_pub_key       = isset( $options['publishable_key'] ) ? $options['publishable_key'] : '';
115
		$live_secret_key    = isset( $options['secret_key'] ) ? $options['secret_key'] : '';
116
		$three_d_secure     = isset( $options['three_d_secure'] ) && 'yes' === $options['three_d_secure'];
117
118
		if ( isset( $options['enabled'] ) && 'yes' === $options['enabled'] ) {
119
			if ( empty( $show_3ds_notice ) && $three_d_secure ) {
120
				$url = 'https://stripe.com/docs/payments/3d-secure#three-ds-radar';
121
122
				/* translators: 1) A URL that explains Stripe Radar. */
123
				$message = __( 'WooCommerce Stripe - We see that you had the "Require 3D secure when applicable" setting turned on. This setting is not available here anymore, because it is now replaced by Stripe Radar. You can learn more about it <a href="%s" target="_blank">here</a>.', 'woocommerce-gateway-stripe' );
124
125
				$this->add_admin_notice( '3ds', 'notice notice-warning', sprintf( $message, $url ), true );
126
			}
127
128
			if ( empty( $show_style_notice ) ) {
129
				/* translators: 1) int version 2) int version */
130
				$message = __( 'WooCommerce Stripe - We recently made changes to Stripe that may impact the appearance of your checkout. If your checkout has changed unexpectedly, please follow these <a href="https://docs.woocommerce.com/document/stripe/#section-48" target="_blank">instructions</a> to fix.', 'woocommerce-gateway-stripe' );
131
132
				$this->add_admin_notice( 'style', 'notice notice-warning', $message, true );
133
134
				return;
135
			}
136
137 View Code Duplication
			if ( empty( $show_phpver_notice ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
138
				if ( version_compare( phpversion(), WC_STRIPE_MIN_PHP_VER, '<' ) ) {
139
					/* translators: 1) int version 2) int version */
140
					$message = __( 'WooCommerce Stripe - The minimum PHP version required for this plugin is %1$s. You are running %2$s.', 'woocommerce-gateway-stripe' );
141
142
					$this->add_admin_notice( 'phpver', 'error', sprintf( $message, WC_STRIPE_MIN_PHP_VER, phpversion() ), true );
143
144
					return;
145
				}
146
			}
147
148 View Code Duplication
			if ( empty( $show_wcver_notice ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
149
				if ( version_compare( WC_VERSION, WC_STRIPE_MIN_WC_VER, '<' ) ) {
150
					/* translators: 1) int version 2) int version */
151
					$message = __( 'WooCommerce Stripe - The minimum WooCommerce version required for this plugin is %1$s. You are running %2$s.', 'woocommerce-gateway-stripe' );
152
153
					$this->add_admin_notice( 'wcver', 'notice notice-warning', sprintf( $message, WC_STRIPE_MIN_WC_VER, WC_VERSION ), true );
154
155
					return;
156
				}
157
			}
158
159
			if ( empty( $show_curl_notice ) ) {
160
				if ( ! function_exists( 'curl_init' ) ) {
161
					$this->add_admin_notice( 'curl', 'notice notice-warning', __( 'WooCommerce Stripe - cURL is not installed.', 'woocommerce-gateway-stripe' ), true );
162
				}
163
			}
164
165
			if ( empty( $show_keys_notice ) ) {
166
				$secret = WC_Stripe_API::get_secret_key();
167
168
				if ( empty( $secret ) && ! ( isset( $_GET['page'], $_GET['section'] ) && 'wc-settings' === $_GET['page'] && 'stripe' === $_GET['section'] ) ) {
169
					$setting_link = $this->get_setting_link();
170
					/* translators: 1) link */
171
					$this->add_admin_notice( 'keys', 'notice notice-warning', sprintf( __( 'Stripe is almost ready. To get started, <a href="%s">set your Stripe account keys</a>.', 'woocommerce-gateway-stripe' ), $setting_link ), true );
172
				}
173
174
				// Check if keys are entered properly per live/test mode.
175
				if ( $testmode ) {
176 View Code Duplication
					if (
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
177
						! empty( $test_pub_key ) && ! preg_match( '/^pk_test_/', $test_pub_key )
178
						|| ( ! empty( $test_secret_key ) && ! preg_match( '/^sk_test_/', $test_secret_key )
179
						&& ! empty( $test_secret_key ) && ! preg_match( '/^rk_test_/', $test_secret_key ) ) ) {
180
						$setting_link = $this->get_setting_link();
181
						/* translators: 1) link */
182
						$this->add_admin_notice( 'keys', 'notice notice-error', sprintf( __( 'Stripe is in test mode however your test keys may not be valid. Test keys start with pk_test and sk_test or rk_test. Please go to your settings and, <a href="%s">set your Stripe account keys</a>.', 'woocommerce-gateway-stripe' ), $setting_link ), true );
183
					}
184 View Code Duplication
				} else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
185
					if (
186
						! empty( $live_pub_key ) && ! preg_match( '/^pk_live_/', $live_pub_key )
187
						|| ( ! empty( $live_secret_key ) && ! preg_match( '/^sk_live_/', $live_secret_key )
188
						&& ! empty( $live_secret_key ) && ! preg_match( '/^rk_live_/', $live_secret_key ) ) ) {
189
						$setting_link = $this->get_setting_link();
190
						/* translators: 1) link */
191
						$this->add_admin_notice( 'keys', 'notice notice-error', sprintf( __( 'Stripe is in live mode however your test keys may not be valid. Live keys start with pk_live and sk_live or rk_live. Please go to your settings and, <a href="%s">set your Stripe account keys</a>.', 'woocommerce-gateway-stripe' ), $setting_link ), true );
192
					}
193
				}
194
			}
195
196
			if ( empty( $show_ssl_notice ) ) {
197
				// Show message if enabled and FORCE SSL is disabled and WordpressHTTPS plugin is not detected.
198
				if ( ! wc_checkout_is_https() ) {
199
					/* translators: 1) link */
200
					$this->add_admin_notice( 'ssl', 'notice notice-warning', sprintf( __( 'Stripe is enabled, but a SSL certificate is not detected. Your checkout may not be secure! Please ensure your server has a valid <a href="%1$s" target="_blank">SSL certificate</a>', 'woocommerce-gateway-stripe' ), 'https://en.wikipedia.org/wiki/Transport_Layer_Security' ), true );
201
				}
202
			}
203
		}
204
	}
205
206
	/**
207
	 * Environment check for all other payment methods.
208
	 *
209
	 * @since 4.1.0
210
	 */
211
	public function payment_methods_check_environment() {
212
		$payment_methods = $this->get_payment_methods();
213
214
		foreach ( $payment_methods as $method => $class ) {
215
			$show_notice = get_option( 'wc_stripe_show_' . strtolower( $method ) . '_notice' );
216
			$gateway     = new $class();
217
218
			if ( 'yes' !== $gateway->enabled || 'no' === $show_notice ) {
219
				continue;
220
			}
221
222
			if ( ! in_array( get_woocommerce_currency(), $gateway->get_supported_currency() ) ) {
223
				/* translators: %1$s Payment method, %2$s List of supported currencies */
224
				$this->add_admin_notice( $method, 'notice notice-error', sprintf( __( '%1$s is enabled - it requires store currency to be set to %2$s', 'woocommerce-gateway-stripe' ), $method, implode( ', ', $gateway->get_supported_currency() ) ), true );
225
			}
226
		}
227
	}
228
229
	/**
230
	 * Hides any admin notices.
231
	 *
232
	 * @since 4.0.0
233
	 * @version 4.0.0
234
	 */
235
	public function hide_notices() {
236
		if ( isset( $_GET['wc-stripe-hide-notice'] ) && isset( $_GET['_wc_stripe_notice_nonce'] ) ) {
237
			if ( ! wp_verify_nonce( $_GET['_wc_stripe_notice_nonce'], 'wc_stripe_hide_notices_nonce' ) ) {
238
				wp_die( __( 'Action failed. Please refresh the page and retry.', 'woocommerce-gateway-stripe' ) );
239
			}
240
241
			if ( ! current_user_can( 'manage_woocommerce' ) ) {
242
				wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
243
			}
244
245
			$notice = wc_clean( $_GET['wc-stripe-hide-notice'] );
246
247
			switch ( $notice ) {
248
				case 'style':
249
					update_option( 'wc_stripe_show_style_notice', 'no' );
250
					break;
251
				case 'phpver':
252
					update_option( 'wc_stripe_show_phpver_notice', 'no' );
253
					break;
254
				case 'wcver':
255
					update_option( 'wc_stripe_show_wcver_notice', 'no' );
256
					break;
257
				case 'curl':
258
					update_option( 'wc_stripe_show_curl_notice', 'no' );
259
					break;
260
				case 'ssl':
261
					update_option( 'wc_stripe_show_ssl_notice', 'no' );
262
					break;
263
				case 'keys':
264
					update_option( 'wc_stripe_show_keys_notice', 'no' );
265
					break;
266
				case '3ds':
267
					update_option( 'wc_stripe_show_3ds_notice', 'no' );
268
					break;
269
				case 'Alipay':
270
					update_option( 'wc_stripe_show_alipay_notice', 'no' );
271
					break;
272
				case 'Bancontact':
273
					update_option( 'wc_stripe_show_bancontact_notice', 'no' );
274
					break;
275
				case 'EPS':
276
					update_option( 'wc_stripe_show_eps_notice', 'no' );
277
					break;
278
				case 'Giropay':
279
					update_option( 'wc_stripe_show_giropay_notice', 'no' );
280
					break;
281
				case 'iDeal':
282
					update_option( 'wc_stripe_show_ideal_notice', 'no' );
283
					break;
284
				case 'Multibanco':
285
					update_option( 'wc_stripe_show_multibanco_notice', 'no' );
286
					break;
287
				case 'P24':
288
					update_option( 'wc_stripe_show_p24_notice', 'no' );
289
					break;
290
				case 'SEPA':
291
					update_option( 'wc_stripe_show_sepa_notice', 'no' );
292
					break;
293
				case 'SOFORT':
294
					update_option( 'wc_stripe_show_sofort_notice', 'no' );
295
					break;
296
			}
297
		}
298
	}
299
300
	/**
301
	 * Get setting link.
302
	 *
303
	 * @since 1.0.0
304
	 *
305
	 * @return string Setting link
306
	 */
307
	public function get_setting_link() {
308
		$use_id_as_section = function_exists( 'WC' ) ? version_compare( WC()->version, '2.6', '>=' ) : false;
309
310
		$section_slug = $use_id_as_section ? 'stripe' : strtolower( 'WC_Gateway_Stripe' );
311
312
		return admin_url( 'admin.php?page=wc-settings&tab=checkout&section=' . $section_slug );
313
	}
314
315
	/**
316
	 * Saves options in order to hide notices based on the gateway's version.
317
	 *
318
	 * @since 4.3.0
319
	 */
320
	public function stripe_updated() {
321
		$previous_version = get_option( 'wc_stripe_version' );
322
323
		// Only show the style notice if the plugin was installed and older than 4.1.4.
324
		if ( empty( $previous_version ) || version_compare( $previous_version, '4.1.4', 'ge' ) ) {
325
			update_option( 'wc_stripe_show_style_notice', 'no' );
326
		}
327
	}
328
}
329
330
new WC_Stripe_Admin_Notices();
331