Completed
Push — master ( e3a5fe...3adadd )
by Roy
05:10
created

WC_Stripe_Admin_Notices::add_admin_notice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 4
dl 7
loc 7
rs 9.4285
c 0
b 0
f 0
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
	}
27
28
	/**
29
	 * Allow this class and other classes to add slug keyed notices (to avoid duplication).
30
	 *
31
	 * @since 1.0.0
32
	 * @version 4.0.0
33
	 */
34 View Code Duplication
	public function add_admin_notice( $slug, $class, $message, $dismissible = false ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
35
		$this->notices[ $slug ] = array(
36
			'class'       => $class,
37
			'message'     => $message,
38
			'dismissible' => $dismissible,
39
		);
40
	}
41
42
	/**
43
	 * Display any notices we've collected thus far.
44
	 *
45
	 * @since 1.0.0
46
	 * @version 4.0.0
47
	 */
48
	public function admin_notices() {
49
		if ( ! current_user_can( 'manage_woocommerce' ) ) {
50
			return;
51
		}
52
53
		// Main Stripe payment method.
54
		$this->stripe_check_environment();
55
56
		// All other payment methods.
57
		$this->payment_methods_check_environment();
58
59
		foreach ( (array) $this->notices as $notice_key => $notice ) {
60
			echo '<div class="' . esc_attr( $notice['class'] ) . '" style="position:relative;">';
61
62
			if ( $notice['dismissible'] ) {
63
			?>
64
				<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:absolute;right:1px;padding:9px;text-decoration:none;"></a>
65
			<?php
66
			}
67
68
			echo '<p>';
69
			echo wp_kses( $notice['message'], array( 'a' => array( 'href' => array() ) ) );
70
			echo '</p></div>';
71
		}
72
	}
73
74
	/**
75
	 * List of available payment methods.
76
	 *
77
	 * @since 4.1.0
78
	 * @return array
79
	 */
80
	public function get_payment_methods() {
81
		return array(
82
			'Alipay'     => 'WC_Gateway_Stripe_Alipay',
83
			'Bancontact' => 'WC_Gateway_Stripe_Bancontact',
84
			'Bitcoin'    => 'WC_Gateway_Stripe_Bitcoin',
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_ssl_notice  = get_option( 'wc_stripe_show_ssl_notice' );
104
		$show_keys_notice = get_option( 'wc_stripe_show_keys_notice' );
105
		$options          = get_option( 'woocommerce_stripe_settings' );
106
		$testmode         = ( isset( $options['testmode'] ) && 'yes' === $options['testmode'] ) ? true : false;
107
		$test_pub_key     = isset( $options['test_publishable_key'] ) ? $options['test_publishable_key'] : '';
108
		$test_secret_key  = isset( $options['test_secret_key'] ) ? $options['test_secret_key'] : '';
109
		$live_pub_key     = isset( $options['publishable_key'] ) ? $options['publishable_key'] : '';
110
		$live_secret_key  = isset( $options['secret_key'] ) ? $options['secret_key'] : '';
111
112
		if ( isset( $options['enabled'] ) && 'yes' === $options['enabled'] && empty( $show_keys_notice ) ) {
113
			$secret = WC_Stripe_API::get_secret_key();
114
115
			if ( empty( $secret ) && ! ( isset( $_GET['page'], $_GET['section'] ) && 'wc-settings' === $_GET['page'] && 'stripe' === $_GET['section'] ) ) {
116
				$setting_link = $this->get_setting_link();
117
				/* translators: 1) link */
118
				$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 );
119
			}
120
121
			// Check if keys are entered properly per live/test mode.
122
			if ( $testmode ) {
123 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...
124
					! empty( $test_pub_key ) && ! preg_match( '/^pk_test_/', $test_pub_key )
125
					|| ( ! empty( $test_secret_key ) && ! preg_match( '/^sk_test_/', $test_secret_key )
126
					&& ! empty( $test_secret_key ) && ! preg_match( '/^rk_test_/', $test_secret_key ) ) )
127
				{
128
					$setting_link = $this->get_setting_link();
129
					/* translators: 1) link */
130
					$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 );
131
				}
132 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...
133
				if (
134
					! empty( $live_pub_key ) && ! preg_match( '/^pk_live_/', $live_pub_key )
135
					|| ( ! empty( $live_secret_key ) && ! preg_match( '/^sk_live_/', $live_secret_key )
136
					&& ! empty( $live_secret_key ) && ! preg_match( '/^rk_live_/', $live_secret_key ) ) )
137
				{
138
					$setting_link = $this->get_setting_link();
139
					/* translators: 1) link */
140
					$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 );
141
				}
142
			}
143
		}
144
145
		if ( empty( $show_ssl_notice ) && isset( $options['enabled'] ) && 'yes' === $options['enabled'] ) {
146
			// Show message if enabled and FORCE SSL is disabled and WordpressHTTPS plugin is not detected.
147
			if ( ( function_exists( 'wc_site_is_https' ) && ! wc_site_is_https() ) && ( 'no' === get_option( 'woocommerce_force_ssl_checkout' ) && ! class_exists( 'WordPressHTTPS' ) ) ) {
148
				/* translators: 1) link 2) link */
149
				$this->add_admin_notice( 'ssl', 'notice notice-warning', sprintf( __( 'Stripe is enabled, but the <a href="%1$s">force SSL option</a> is disabled; your checkout may not be secure! Please enable SSL and ensure your server has a valid <a href="%2$s" target="_blank">SSL certificate</a> - Stripe will only work in test mode.', 'woocommerce-gateway-stripe' ), admin_url( 'admin.php?page=wc-settings&tab=checkout' ), 'https://en.wikipedia.org/wiki/Transport_Layer_Security' ), true );
150
			}
151
		}
152
	}
153
154
	/**
155
	 * Environment check for all other payment methods.
156
	 *
157
	 * @since 4.1.0
158
	 */
159
	public function payment_methods_check_environment() {
160
		$payment_methods = $this->get_payment_methods();
161
162
		foreach ( $payment_methods as $method => $class ) {
163
			$show_notice = get_option( 'wc_stripe_show_' . strtolower( $method ) . '_notice' );
164
			$gateway     = new $class();
165
166
			if ( 'yes' !== $gateway->enabled || 'no' === $show_notice ) {
167
				continue;
168
			}
169
170
			if ( ! in_array( get_woocommerce_currency(), $gateway->get_supported_currency() ) ) {
171
				$this->add_admin_notice( $method, 'notice notice-error', sprintf( __( '%s is enabled - it requires store currency to be set to %s', 'woocommerce-gateway-stripe' ), $method, implode( ', ', $gateway->get_supported_currency() ) ), true );
172
			}
173
		}
174
	}
175
176
	/**
177
	 * Hides any admin notices.
178
	 *
179
	 * @since 4.0.0
180
	 * @version 4.0.0
181
	 */
182
	public function hide_notices() {
183
		if ( isset( $_GET['wc-stripe-hide-notice'] ) && isset( $_GET['_wc_stripe_notice_nonce'] ) ) {
184
			if ( ! wp_verify_nonce( $_GET['_wc_stripe_notice_nonce'], 'wc_stripe_hide_notices_nonce' ) ) {
185
				wp_die( __( 'Action failed. Please refresh the page and retry.', 'woocommerce-gateway-stripe' ) );
186
			}
187
188
			if ( ! current_user_can( 'manage_woocommerce' ) ) {
189
				wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
190
			}
191
192
			$notice = wc_clean( $_GET['wc-stripe-hide-notice'] );
193
194
			switch ( $notice ) {
195
				case 'ssl':
196
					update_option( 'wc_stripe_show_ssl_notice', 'no' );
197
					break;
198
				case 'keys':
199
					update_option( 'wc_stripe_show_keys_notice', 'no' );
200
					break;
201
				case 'Alipay':
202
					update_option( 'wc_stripe_show_alipay_notice', 'no' );
203
					break;
204
				case 'Bancontact':
205
					update_option( 'wc_stripe_show_bancontact_notice', 'no' );
206
					break;
207
				case 'Bitcoin':
208
					update_option( 'wc_stripe_show_bitcoin_notice', 'no' );
209
					break;
210
				case 'EPS':
211
					update_option( 'wc_stripe_show_eps_notice', 'no' );
212
					break;
213
				case 'Giropay':
214
					update_option( 'wc_stripe_show_giropay_notice', 'no' );
215
					break;
216
				case 'iDeal':
217
					update_option( 'wc_stripe_show_ideal_notice', 'no' );
218
					break;
219
				case 'Multibanco':
220
					update_option( 'wc_stripe_show_multibanco_notice', 'no' );
221
					break;
222
				case 'P24':
223
					update_option( 'wc_stripe_show_p24_notice', 'no' );
224
					break;
225
				case 'SEPA':
226
					update_option( 'wc_stripe_show_sepa_notice', 'no' );
227
					break;
228
				case 'SOFORT':
229
					update_option( 'wc_stripe_show_sofort_notice', 'no' );
230
					break;
231
			}
232
		}
233
	}
234
235
	/**
236
	 * Get setting link.
237
	 *
238
	 * @since 1.0.0
239
	 *
240
	 * @return string Setting link
241
	 */
242
	public function get_setting_link() {
243
		$use_id_as_section = function_exists( 'WC' ) ? version_compare( WC()->version, '2.6', '>=' ) : false;
244
245
		$section_slug = $use_id_as_section ? 'stripe' : strtolower( 'WC_Gateway_Stripe' );
246
247
		return admin_url( 'admin.php?page=wc-settings&tab=checkout&section=' . $section_slug );
248
	}
249
}
250
251
new WC_Stripe_Admin_Notices();
252