Completed
Pull Request — master (#1357)
by
unknown
01:48
created
includes/class-wc-stripe-apple-pay-registration.php 2 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -184,7 +184,7 @@
 block discarded – undo
184 184
 	 * Add to the list of publicly allowed query variables.
185 185
 	 *
186 186
 	 * @param  array $query_vars - provided public query vars.
187
-	 * @return array Updated public query vars.
187
+	 * @return string[] Updated public query vars.
188 188
 	 */
189 189
 	public function whitelist_domain_association_query_param( $query_vars ) {
190 190
 		$query_vars[] = 'apple-developer-merchantid-domain-association';
Please login to merge, or discard this patch.
Spacing   +81 added lines, -81 removed lines patch added patch discarded remove patch
@@ -5,7 +5,7 @@  discard block
 block discarded – undo
5 5
  * @since 4.0.6
6 6
  */
7 7
 
8
-if ( ! defined( 'ABSPATH' ) ) {
8
+if ( ! defined('ABSPATH')) {
9 9
 	exit;
10 10
 }
11 11
 
@@ -53,27 +53,27 @@  discard block
 block discarded – undo
53 53
 	public $apple_pay_verify_notice;
54 54
 
55 55
 	public function __construct() {
56
-		add_action( 'woocommerce_stripe_updated', array( $this, 'verify_domain_if_needed' ) );
57
-		add_action( 'update_option_woocommerce_stripe_settings', array( $this, 'verify_domain_on_new_secret_key' ), 10, 2 );
58
-
59
-		add_action( 'init', array( $this, 'add_domain_association_rewrite_rule' ) );
60
-		add_filter( 'query_vars', array( $this, 'whitelist_domain_association_query_param' ), 10, 1 );
61
-		add_action( 'parse_request', array( $this, 'parse_domain_association_request' ), 10, 1 );
62
-
63
-		$this->stripe_settings         = get_option( 'woocommerce_stripe_settings', array() );
64
-		$this->stripe_enabled          = $this->get_option( 'enabled' );
65
-		$this->payment_request         = 'yes' === $this->get_option( 'payment_request', 'yes' );
66
-		$this->apple_pay_domain_set    = 'yes' === $this->get_option( 'apple_pay_domain_set', 'no' );
67
-		$this->apple_pay_rewrite_flush = 'yes' === $this->get_option( 'apple_pay_rewrite_flush', 'no' );
56
+		add_action('woocommerce_stripe_updated', array($this, 'verify_domain_if_needed'));
57
+		add_action('update_option_woocommerce_stripe_settings', array($this, 'verify_domain_on_new_secret_key'), 10, 2);
58
+
59
+		add_action('init', array($this, 'add_domain_association_rewrite_rule'));
60
+		add_filter('query_vars', array($this, 'whitelist_domain_association_query_param'), 10, 1);
61
+		add_action('parse_request', array($this, 'parse_domain_association_request'), 10, 1);
62
+
63
+		$this->stripe_settings         = get_option('woocommerce_stripe_settings', array());
64
+		$this->stripe_enabled          = $this->get_option('enabled');
65
+		$this->payment_request         = 'yes' === $this->get_option('payment_request', 'yes');
66
+		$this->apple_pay_domain_set    = 'yes' === $this->get_option('apple_pay_domain_set', 'no');
67
+		$this->apple_pay_rewrite_flush = 'yes' === $this->get_option('apple_pay_rewrite_flush', 'no');
68 68
 		$this->apple_pay_verify_notice = '';
69 69
 		$this->secret_key              = $this->get_secret_key();
70 70
 
71
-		if ( empty( $this->stripe_settings ) ) {
71
+		if (empty($this->stripe_settings)) {
72 72
 			return;
73 73
 		}
74 74
 
75
-		add_action( 'init', array( $this, 'init_apple_pay' ) );
76
-		add_action( 'admin_notices', array( $this, 'admin_notices' ) );
75
+		add_action('init', array($this, 'init_apple_pay'));
76
+		add_action('admin_notices', array($this, 'admin_notices'));
77 77
 	}
78 78
 
79 79
 	/**
@@ -84,13 +84,13 @@  discard block
 block discarded – undo
84 84
 	 * @param string default
85 85
 	 * @return string $setting_value
86 86
 	 */
87
-	public function get_option( $setting = '', $default = '' ) {
88
-		if ( empty( $this->stripe_settings ) ) {
87
+	public function get_option($setting = '', $default = '') {
88
+		if (empty($this->stripe_settings)) {
89 89
 			return $default;
90 90
 		}
91 91
 
92
-		if ( ! empty( $this->stripe_settings[ $setting ] ) ) {
93
-			return $this->stripe_settings[ $setting ];
92
+		if ( ! empty($this->stripe_settings[$setting])) {
93
+			return $this->stripe_settings[$setting];
94 94
 		}
95 95
 
96 96
 		return $default;
@@ -103,8 +103,8 @@  discard block
 block discarded – undo
103 103
 	 * @return string Secret key.
104 104
 	 */
105 105
 	private function get_secret_key() {
106
-		$testmode = 'yes' === $this->get_option( 'testmode', 'no' );
107
-		return $testmode ? $this->get_option( 'test_secret_key' ) : $this->get_option( 'secret_key' );
106
+		$testmode = 'yes' === $this->get_option('testmode', 'no');
107
+		return $testmode ? $this->get_option('test_secret_key') : $this->get_option('secret_key');
108 108
 	}
109 109
 
110 110
 	/**
@@ -116,9 +116,9 @@  discard block
 block discarded – undo
116 116
 	public function init_apple_pay() {
117 117
 		if (
118 118
 			is_admin() &&
119
-			isset( $_GET['page'] ) && 'wc-settings' === $_GET['page'] &&
120
-			isset( $_GET['tab'] ) && 'checkout' === $_GET['tab'] &&
121
-			isset( $_GET['section'] ) && 'stripe' === $_GET['section'] &&
119
+			isset($_GET['page']) && 'wc-settings' === $_GET['page'] &&
120
+			isset($_GET['tab']) && 'checkout' === $_GET['tab'] &&
121
+			isset($_GET['section']) && 'stripe' === $_GET['section'] &&
122 122
 			$this->payment_request
123 123
 		) {
124 124
 			$this->verify_domain();
@@ -132,9 +132,9 @@  discard block
 block discarded – undo
132 132
 	 * @version 3.1.0
133 133
 	 * @param string $secret_key
134 134
 	 */
135
-	private function register_domain_with_apple( $secret_key = '' ) {
136
-		if ( empty( $secret_key ) ) {
137
-			throw new Exception( __( 'Unable to verify domain - missing secret key.', 'woocommerce-gateway-stripe' ) );
135
+	private function register_domain_with_apple($secret_key = '') {
136
+		if (empty($secret_key)) {
137
+			throw new Exception(__('Unable to verify domain - missing secret key.', 'woocommerce-gateway-stripe'));
138 138
 		}
139 139
 
140 140
 		$endpoint = 'https://api.stripe.com/v1/apple_pay/domains';
@@ -152,22 +152,22 @@  discard block
 block discarded – undo
152 152
 			$endpoint,
153 153
 			array(
154 154
 				'headers' => $headers,
155
-				'body'    => http_build_query( $data ),
155
+				'body'    => http_build_query($data),
156 156
 			)
157 157
 		);
158 158
 
159
-		if ( is_wp_error( $response ) ) {
159
+		if (is_wp_error($response)) {
160 160
 			/* translators: error message */
161
-			throw new Exception( sprintf( __( 'Unable to verify domain - %s', 'woocommerce-gateway-stripe' ), $response->get_error_message() ) );
161
+			throw new Exception(sprintf(__('Unable to verify domain - %s', 'woocommerce-gateway-stripe'), $response->get_error_message()));
162 162
 		}
163 163
 
164
-		if ( 200 !== $response['response']['code'] ) {
165
-			$parsed_response = json_decode( $response['body'] );
164
+		if (200 !== $response['response']['code']) {
165
+			$parsed_response = json_decode($response['body']);
166 166
 
167 167
 			$this->apple_pay_verify_notice = $parsed_response->error->message;
168 168
 
169 169
 			/* translators: error message */
170
-			throw new Exception( sprintf( __( 'Unable to verify domain - %s', 'woocommerce-gateway-stripe' ), $parsed_response->error->message ) );
170
+			throw new Exception(sprintf(__('Unable to verify domain - %s', 'woocommerce-gateway-stripe'), $parsed_response->error->message));
171 171
 		}
172 172
 	}
173 173
 
@@ -177,7 +177,7 @@  discard block
 block discarded – undo
177 177
 	public function add_domain_association_rewrite_rule() {
178 178
 		$regex    = '^\.well-known\/apple-developer-merchantid-domain-association$';
179 179
 		$redirect = 'index.php?apple-developer-merchantid-domain-association=1';
180
-		add_rewrite_rule( $regex, $redirect, 'top' );
180
+		add_rewrite_rule($regex, $redirect, 'top');
181 181
 	}
182 182
 
183 183
 	/**
@@ -186,7 +186,7 @@  discard block
 block discarded – undo
186 186
 	 * @param  array $query_vars - provided public query vars.
187 187
 	 * @return array Updated public query vars.
188 188
 	 */
189
-	public function whitelist_domain_association_query_param( $query_vars ) {
189
+	public function whitelist_domain_association_query_param($query_vars) {
190 190
 		$query_vars[] = 'apple-developer-merchantid-domain-association';
191 191
 		return $query_vars;
192 192
 	}
@@ -196,19 +196,19 @@  discard block
 block discarded – undo
196 196
 	 *
197 197
 	 * @param WP WordPress environment object.
198 198
 	 */
199
-	public function parse_domain_association_request( $wp ) {
199
+	public function parse_domain_association_request($wp) {
200 200
 		if (
201
-			! isset( $wp->query_vars['apple-developer-merchantid-domain-association'] ) ||
201
+			! isset($wp->query_vars['apple-developer-merchantid-domain-association']) ||
202 202
 			'1' !== $wp->query_vars['apple-developer-merchantid-domain-association']
203 203
 		) {
204 204
 			return;
205 205
 		}
206 206
 
207 207
 		$path     = WC_STRIPE_PLUGIN_PATH . '/' . 'apple-developer-merchantid-domain-association';
208
-		$contents = file_get_contents( $path );
208
+		$contents = file_get_contents($path);
209 209
 
210
-		header( 'Content-Type: application/octet-stream' );
211
-		echo esc_html( $contents );
210
+		header('Content-Type: application/octet-stream');
211
+		echo esc_html($contents);
212 212
 
213 213
 		exit;
214 214
 	}
@@ -223,43 +223,43 @@  discard block
 block discarded – undo
223 223
 	 * @since 4.3.0
224 224
 	 * @return bool True on success, false on failure.
225 225
 	 */
226
-	public function update_domain_association_file( $force = false ) {
227
-		$path     = untrailingslashit( $_SERVER['DOCUMENT_ROOT'] );
226
+	public function update_domain_association_file($force = false) {
227
+		$path     = untrailingslashit($_SERVER['DOCUMENT_ROOT']);
228 228
 		$dir      = '.well-known';
229 229
 		$file     = 'apple-developer-merchantid-domain-association';
230 230
 		$fullpath = $path . '/' . $dir . '/' . $file;
231 231
 
232
-		$existing_contents = @file_get_contents( $fullpath );
233
-		$new_contents = @file_get_contents( WC_STRIPE_PLUGIN_PATH . '/' . $file );
234
-		if ( ( ! $existing_contents && ! $force ) || $existing_contents === $new_contents ) {
232
+		$existing_contents = @file_get_contents($fullpath);
233
+		$new_contents = @file_get_contents(WC_STRIPE_PLUGIN_PATH . '/' . $file);
234
+		if (( ! $existing_contents && ! $force) || $existing_contents === $new_contents) {
235 235
 			return true;
236 236
 		}
237 237
 
238 238
 		$error = null;
239
-		if ( ! file_exists( $path . '/' . $dir ) ) {
240
-			if ( ! @mkdir( $path . '/' . $dir, 0755 ) ) { // @codingStandardsIgnoreLine
241
-				$error = __( 'Unable to create domain association folder to domain root.', 'woocommerce-gateway-stripe' );
239
+		if ( ! file_exists($path . '/' . $dir)) {
240
+			if ( ! @mkdir($path . '/' . $dir, 0755)) { // @codingStandardsIgnoreLine
241
+				$error = __('Unable to create domain association folder to domain root.', 'woocommerce-gateway-stripe');
242 242
 			}
243 243
 		}
244
-		if ( ! @copy( WC_STRIPE_PLUGIN_PATH . '/' . $file, $fullpath ) ) { // @codingStandardsIgnoreLine
245
-			$error = __( 'Unable to copy domain association file to domain root.', 'woocommerce-gateway-stripe' );
244
+		if ( ! @copy(WC_STRIPE_PLUGIN_PATH . '/' . $file, $fullpath)) { // @codingStandardsIgnoreLine
245
+			$error = __('Unable to copy domain association file to domain root.', 'woocommerce-gateway-stripe');
246 246
 		}
247 247
 
248
-		if ( isset( $error ) ) {
248
+		if (isset($error)) {
249 249
 			$url            = 'https://' . $_SERVER['HTTP_HOST'] . '/' . $dir . '/' . $file;
250
-			$response       = wp_remote_get( $url );
251
-			$already_hosted = wp_remote_retrieve_body( $response ) === $new_contents;
252
-			if ( ! $already_hosted ) {
250
+			$response       = wp_remote_get($url);
251
+			$already_hosted = wp_remote_retrieve_body($response) === $new_contents;
252
+			if ( ! $already_hosted) {
253 253
 				WC_Stripe_Logger::log(
254 254
 					'Error: ' . $error . ' ' .
255 255
 					/* translators: expected domain association file URL */
256
-					sprintf( __( 'To enable Apple Pay, domain association file must be hosted at %s.', 'woocommerce-gateway-stripe' ), $url )
256
+					sprintf(__('To enable Apple Pay, domain association file must be hosted at %s.', 'woocommerce-gateway-stripe'), $url)
257 257
 				);
258 258
 			}
259 259
 			return $already_hosted;
260 260
 		}
261 261
 
262
-		WC_Stripe_Logger::log( 'Domain association file updated.' );
262
+		WC_Stripe_Logger::log('Domain association file updated.');
263 263
 		return true;
264 264
 	}
265 265
 
@@ -270,33 +270,33 @@  discard block
 block discarded – undo
270 270
 	 * @version 3.1.0
271 271
 	 */
272 272
 	public function verify_domain() {
273
-		if ( ! $this->apple_pay_rewrite_flush ) {
273
+		if ( ! $this->apple_pay_rewrite_flush) {
274 274
 			flush_rewrite_rules();
275 275
 			$this->stripe_settings['apple_pay_rewrite_flush'] = 'yes';
276 276
 			$this->apple_pay_rewrite_flush = true;
277
-			update_option( 'woocommerce_stripe_settings', $this->stripe_settings );
277
+			update_option('woocommerce_stripe_settings', $this->stripe_settings);
278 278
 		}
279 279
 
280 280
 		try {
281 281
 			// At this point then the domain association folder and file should be available.
282 282
 			// Proceed to verify/and or verify again.
283
-			$this->register_domain_with_apple( $this->secret_key );
283
+			$this->register_domain_with_apple($this->secret_key);
284 284
 
285 285
 			// No errors to this point, verification success!
286 286
 			$this->stripe_settings['apple_pay_domain_set'] = 'yes';
287 287
 			$this->apple_pay_domain_set                    = true;
288 288
 
289
-			update_option( 'woocommerce_stripe_settings', $this->stripe_settings );
289
+			update_option('woocommerce_stripe_settings', $this->stripe_settings);
290 290
 
291
-			WC_Stripe_Logger::log( 'Your domain has been verified with Apple Pay!' );
291
+			WC_Stripe_Logger::log('Your domain has been verified with Apple Pay!');
292 292
 
293
-		} catch ( Exception $e ) {
293
+		} catch (Exception $e) {
294 294
 			$this->stripe_settings['apple_pay_domain_set'] = 'no';
295 295
 			$this->apple_pay_domain_set                    = false;
296 296
 
297
-			update_option( 'woocommerce_stripe_settings', $this->stripe_settings );
297
+			update_option('woocommerce_stripe_settings', $this->stripe_settings);
298 298
 
299
-			WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() );
299
+			WC_Stripe_Logger::log('Error: ' . $e->getMessage());
300 300
 		}
301 301
 	}
302 302
 
@@ -306,14 +306,14 @@  discard block
 block discarded – undo
306 306
 	 * @since 4.5.3
307 307
 	 * @version 4.5.3
308 308
 	 */
309
-	public function verify_domain_on_new_secret_key( $prev_settings, $settings ) {
309
+	public function verify_domain_on_new_secret_key($prev_settings, $settings) {
310 310
 		$this->stripe_settings = $prev_settings;
311 311
 		$prev_secret_key = $this->get_secret_key();
312 312
 
313 313
 		$this->stripe_settings = $settings;
314 314
 		$this->secret_key = $this->get_secret_key();
315 315
 
316
-		if ( ! empty( $this->secret_key ) && $this->secret_key !== $prev_secret_key ) {
316
+		if ( ! empty($this->secret_key) && $this->secret_key !== $prev_secret_key) {
317 317
 			$this->verify_domain();
318 318
 		}
319 319
 	}
@@ -325,7 +325,7 @@  discard block
 block discarded – undo
325 325
 	 * @version 4.5.3
326 326
 	 */
327 327
 	public function verify_domain_if_needed() {
328
-		if ( $this->payment_request ) {
328
+		if ($this->payment_request) {
329 329
 			$this->verify_domain();
330 330
 		}
331 331
 	}
@@ -336,16 +336,16 @@  discard block
 block discarded – undo
336 336
 	 * @since 4.0.6
337 337
 	 */
338 338
 	public function admin_notices() {
339
-		if ( ! $this->stripe_enabled || ! $this->payment_request ) {
339
+		if ( ! $this->stripe_enabled || ! $this->payment_request) {
340 340
 			return;
341 341
 		}
342 342
 
343
-		if ( ! current_user_can( 'manage_woocommerce' ) ) {
343
+		if ( ! current_user_can('manage_woocommerce')) {
344 344
 			return;
345 345
 		}
346 346
 
347
-		$empty_notice = empty( $this->apple_pay_verify_notice );
348
-		if ( $empty_notice && ( $this->apple_pay_domain_set || empty( $this->secret_key ) ) ) {
347
+		$empty_notice = empty($this->apple_pay_verify_notice);
348
+		if ($empty_notice && ($this->apple_pay_domain_set || empty($this->secret_key))) {
349 349
 			return;
350 350
 		}
351 351
 
@@ -354,28 +354,28 @@  discard block
 block discarded – undo
354 354
 		 * when setting screen is displayed. So if domain verification is not set,
355 355
 		 * something went wrong so lets notify user.
356 356
 		 */
357
-		$allowed_html                      = array(
357
+		$allowed_html = array(
358 358
 			'a' => array(
359 359
 				'href'  => array(),
360 360
 				'title' => array(),
361 361
 			),
362 362
 		);
363
-		$verification_failed_without_error = __( 'Apple Pay domain verification failed.', 'woocommerce-gateway-stripe' );
364
-		$verification_failed_with_error    = __( 'Apple Pay domain verification failed with the following error:', 'woocommerce-gateway-stripe' );
363
+		$verification_failed_without_error = __('Apple Pay domain verification failed.', 'woocommerce-gateway-stripe');
364
+		$verification_failed_with_error    = __('Apple Pay domain verification failed with the following error:', 'woocommerce-gateway-stripe');
365 365
 		$check_log_text                    = sprintf(
366 366
 			/* translators: 1) HTML anchor open tag 2) HTML anchor closing tag */
367
-			esc_html__( 'Please check the %1$slogs%2$s for more details on this issue. Logging must be enabled to see recorded logs.', 'woocommerce-gateway-stripe' ),
368
-			'<a href="' . admin_url( 'admin.php?page=wc-status&tab=logs' ) . '">',
367
+			esc_html__('Please check the %1$slogs%2$s for more details on this issue. Logging must be enabled to see recorded logs.', 'woocommerce-gateway-stripe'),
368
+			'<a href="' . admin_url('admin.php?page=wc-status&tab=logs') . '">',
369 369
 			'</a>'
370 370
 		);
371 371
 
372 372
 		?>
373 373
 		<div class="error stripe-apple-pay-message">
374
-			<?php if ( $empty_notice ) : ?>
375
-				<p><?php echo esc_html( $verification_failed_without_error ); ?></p>
374
+			<?php if ($empty_notice) : ?>
375
+				<p><?php echo esc_html($verification_failed_without_error); ?></p>
376 376
 			<?php else : ?>
377
-				<p><?php echo esc_html( $verification_failed_with_error ); ?></p>
378
-				<p><i><?php echo wp_kses( make_clickable( esc_html( $this->apple_pay_verify_notice ) ), $allowed_html ); ?></i></p>
377
+				<p><?php echo esc_html($verification_failed_with_error); ?></p>
378
+				<p><i><?php echo wp_kses(make_clickable(esc_html($this->apple_pay_verify_notice)), $allowed_html); ?></i></p>
379 379
 			<?php endif; ?>
380 380
 			<p><?php echo $check_log_text; ?></p>
381 381
 		</div>
Please login to merge, or discard this patch.