Completed
Pull Request — master (#1328)
by
unknown
02:10
created
includes/class-wc-stripe-apple-pay-registration.php 1 patch
Spacing   +60 added lines, -60 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,23 +53,23 @@  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 );
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 58
 
59
-		$this->stripe_settings         = get_option( 'woocommerce_stripe_settings', array() );
60
-		$this->stripe_enabled          = $this->get_option( 'enabled' );
61
-		$this->payment_request         = 'yes' === $this->get_option( 'payment_request', 'yes' );
62
-		$this->apple_pay_domain_set    = 'yes' === $this->get_option( 'apple_pay_domain_set', 'no' );
59
+		$this->stripe_settings         = get_option('woocommerce_stripe_settings', array());
60
+		$this->stripe_enabled          = $this->get_option('enabled');
61
+		$this->payment_request         = 'yes' === $this->get_option('payment_request', 'yes');
62
+		$this->apple_pay_domain_set    = 'yes' === $this->get_option('apple_pay_domain_set', 'no');
63 63
 		$this->apple_pay_verify_notice = '';
64 64
 		$this->secret_key              = $this->get_secret_key();
65 65
 
66
-		if ( empty( $this->stripe_settings ) ) {
66
+		if (empty($this->stripe_settings)) {
67 67
 			return;
68 68
 		}
69 69
 
70 70
 		$this->init_apple_pay();
71 71
 
72
-		add_action( 'admin_notices', array( $this, 'admin_notices' ) );
72
+		add_action('admin_notices', array($this, 'admin_notices'));
73 73
 	}
74 74
 
75 75
 	/**
@@ -80,13 +80,13 @@  discard block
 block discarded – undo
80 80
 	 * @param string default
81 81
 	 * @return string $setting_value
82 82
 	 */
83
-	public function get_option( $setting = '', $default = '' ) {
84
-		if ( empty( $this->stripe_settings ) ) {
83
+	public function get_option($setting = '', $default = '') {
84
+		if (empty($this->stripe_settings)) {
85 85
 			return $default;
86 86
 		}
87 87
 
88
-		if ( ! empty( $this->stripe_settings[ $setting ] ) ) {
89
-			return $this->stripe_settings[ $setting ];
88
+		if ( ! empty($this->stripe_settings[$setting])) {
89
+			return $this->stripe_settings[$setting];
90 90
 		}
91 91
 
92 92
 		return $default;
@@ -99,8 +99,8 @@  discard block
 block discarded – undo
99 99
 	 * @return string Secret key.
100 100
 	 */
101 101
 	private function get_secret_key() {
102
-		$testmode = 'yes' === $this->get_option( 'testmode', 'no' );
103
-		return $testmode ? $this->get_option( 'test_secret_key' ) : $this->get_option( 'secret_key' );
102
+		$testmode = 'yes' === $this->get_option('testmode', 'no');
103
+		return $testmode ? $this->get_option('test_secret_key') : $this->get_option('secret_key');
104 104
 	}
105 105
 
106 106
 	/**
@@ -112,9 +112,9 @@  discard block
 block discarded – undo
112 112
 	public function init_apple_pay() {
113 113
 		if (
114 114
 			is_admin() &&
115
-			isset( $_GET['page'] ) && 'wc-settings' === $_GET['page'] &&
116
-			isset( $_GET['tab'] ) && 'checkout' === $_GET['tab'] &&
117
-			isset( $_GET['section'] ) && 'stripe' === $_GET['section'] &&
115
+			isset($_GET['page']) && 'wc-settings' === $_GET['page'] &&
116
+			isset($_GET['tab']) && 'checkout' === $_GET['tab'] &&
117
+			isset($_GET['section']) && 'stripe' === $_GET['section'] &&
118 118
 			$this->payment_request
119 119
 		) {
120 120
 			$this->verify_domain();
@@ -128,9 +128,9 @@  discard block
 block discarded – undo
128 128
 	 * @version 3.1.0
129 129
 	 * @param string $secret_key
130 130
 	 */
131
-	private function register_domain_with_apple( $secret_key = '' ) {
132
-		if ( empty( $secret_key ) ) {
133
-			throw new Exception( __( 'Unable to verify domain - missing secret key.', 'woocommerce-gateway-stripe' ) );
131
+	private function register_domain_with_apple($secret_key = '') {
132
+		if (empty($secret_key)) {
133
+			throw new Exception(__('Unable to verify domain - missing secret key.', 'woocommerce-gateway-stripe'));
134 134
 		}
135 135
 
136 136
 		$endpoint = 'https://api.stripe.com/v1/apple_pay/domains';
@@ -148,22 +148,22 @@  discard block
 block discarded – undo
148 148
 			$endpoint,
149 149
 			array(
150 150
 				'headers' => $headers,
151
-				'body'    => http_build_query( $data ),
151
+				'body'    => http_build_query($data),
152 152
 			)
153 153
 		);
154 154
 
155
-		if ( is_wp_error( $response ) ) {
155
+		if (is_wp_error($response)) {
156 156
 			/* translators: error message */
157
-			throw new Exception( sprintf( __( 'Unable to verify domain - %s', 'woocommerce-gateway-stripe' ), $response->get_error_message() ) );
157
+			throw new Exception(sprintf(__('Unable to verify domain - %s', 'woocommerce-gateway-stripe'), $response->get_error_message()));
158 158
 		}
159 159
 
160
-		if ( 200 !== $response['response']['code'] ) {
161
-			$parsed_response = json_decode( $response['body'] );
160
+		if (200 !== $response['response']['code']) {
161
+			$parsed_response = json_decode($response['body']);
162 162
 
163 163
 			$this->apple_pay_verify_notice = $parsed_response->error->message;
164 164
 
165 165
 			/* translators: error message */
166
-			throw new Exception( sprintf( __( 'Unable to verify domain - %s', 'woocommerce-gateway-stripe' ), $parsed_response->error->message ) );
166
+			throw new Exception(sprintf(__('Unable to verify domain - %s', 'woocommerce-gateway-stripe'), $parsed_response->error->message));
167 167
 		}
168 168
 	}
169 169
 
@@ -177,43 +177,43 @@  discard block
 block discarded – undo
177 177
 	 * @since 4.3.0
178 178
 	 * @return bool True on success, false on failure.
179 179
 	 */
180
-	public function update_domain_association_file( $force = false ) {
181
-			$path     = untrailingslashit( $_SERVER['DOCUMENT_ROOT'] );
180
+	public function update_domain_association_file($force = false) {
181
+			$path     = untrailingslashit($_SERVER['DOCUMENT_ROOT']);
182 182
 			$dir      = '.well-known';
183 183
 			$file     = 'apple-developer-merchantid-domain-association';
184 184
 			$fullpath = $path . '/' . $dir . '/' . $file;
185 185
 
186
-			$existing_contents = @file_get_contents( $fullpath );
187
-			$new_contents = @file_get_contents( WC_STRIPE_PLUGIN_PATH . '/' . $file );
188
-			if ( ( ! $existing_contents && ! $force ) || $existing_contents === $new_contents ) {
186
+			$existing_contents = @file_get_contents($fullpath);
187
+			$new_contents = @file_get_contents(WC_STRIPE_PLUGIN_PATH . '/' . $file);
188
+			if (( ! $existing_contents && ! $force) || $existing_contents === $new_contents) {
189 189
 				return true;
190 190
 			}
191 191
 
192 192
 			$error = null;
193
-			if ( ! file_exists( $path . '/' . $dir ) ) {
194
-				if ( ! @mkdir( $path . '/' . $dir, 0755 ) ) { // @codingStandardsIgnoreLine
195
-					$error = __( 'Unable to create domain association folder to domain root.', 'woocommerce-gateway-stripe' );
193
+			if ( ! file_exists($path . '/' . $dir)) {
194
+				if ( ! @mkdir($path . '/' . $dir, 0755)) { // @codingStandardsIgnoreLine
195
+					$error = __('Unable to create domain association folder to domain root.', 'woocommerce-gateway-stripe');
196 196
 				}
197 197
 			}
198
-			if ( ! @copy( WC_STRIPE_PLUGIN_PATH . '/' . $file, $fullpath ) ) { // @codingStandardsIgnoreLine
199
-				$error = __( 'Unable to copy domain association file to domain root.', 'woocommerce-gateway-stripe' );
198
+			if ( ! @copy(WC_STRIPE_PLUGIN_PATH . '/' . $file, $fullpath)) { // @codingStandardsIgnoreLine
199
+				$error = __('Unable to copy domain association file to domain root.', 'woocommerce-gateway-stripe');
200 200
 			}
201 201
 
202
-			if ( isset( $error ) ) {
202
+			if (isset($error)) {
203 203
 				$url            = 'https://' . $_SERVER['HTTP_HOST'] . '/' . $dir . '/' . $file;
204
-				$response       = wp_remote_get( $url );
205
-				$already_hosted = wp_remote_retrieve_body( $response ) === $new_contents;
206
-				if ( ! $already_hosted ) {
204
+				$response       = wp_remote_get($url);
205
+				$already_hosted = wp_remote_retrieve_body($response) === $new_contents;
206
+				if ( ! $already_hosted) {
207 207
 					WC_Stripe_Logger::log(
208 208
 						'Error: ' . $error . ' ' .
209 209
 						/* translators: expected domain association file URL */
210
-						sprintf( __( 'To enable Apple Pay, domain association file must be hosted at %s.', 'woocommerce-gateway-stripe' ), $url )
210
+						sprintf(__('To enable Apple Pay, domain association file must be hosted at %s.', 'woocommerce-gateway-stripe'), $url)
211 211
 					);
212 212
 				}
213 213
 				return $already_hosted;
214 214
 			}
215 215
 
216
-			WC_Stripe_Logger::log( 'Domain association file updated.' );
216
+			WC_Stripe_Logger::log('Domain association file updated.');
217 217
 			return true;
218 218
 	}
219 219
 
@@ -224,31 +224,31 @@  discard block
 block discarded – undo
224 224
 	 * @version 3.1.0
225 225
 	 */
226 226
 	public function verify_domain() {
227
-		if ( ! $this->update_domain_association_file( true ) ) {
227
+		if ( ! $this->update_domain_association_file(true)) {
228 228
 			$this->stripe_settings['apple_pay_domain_set'] = 'no';
229
-			update_option( 'woocommerce_stripe_settings', $this->stripe_settings );
229
+			update_option('woocommerce_stripe_settings', $this->stripe_settings);
230 230
 			return;
231 231
 		}
232 232
 
233 233
 		try {
234 234
 			// At this point then the domain association folder and file should be available.
235 235
 			// Proceed to verify/and or verify again.
236
-			$this->register_domain_with_apple( $this->secret_key );
236
+			$this->register_domain_with_apple($this->secret_key);
237 237
 
238 238
 			// No errors to this point, verification success!
239 239
 			$this->stripe_settings['apple_pay_domain_set'] = 'yes';
240 240
 			$this->apple_pay_domain_set                    = true;
241 241
 
242
-			update_option( 'woocommerce_stripe_settings', $this->stripe_settings );
242
+			update_option('woocommerce_stripe_settings', $this->stripe_settings);
243 243
 
244
-			WC_Stripe_Logger::log( 'Your domain has been verified with Apple Pay!' );
244
+			WC_Stripe_Logger::log('Your domain has been verified with Apple Pay!');
245 245
 
246
-		} catch ( Exception $e ) {
246
+		} catch (Exception $e) {
247 247
 			$this->stripe_settings['apple_pay_domain_set'] = 'no';
248 248
 
249
-			update_option( 'woocommerce_stripe_settings', $this->stripe_settings );
249
+			update_option('woocommerce_stripe_settings', $this->stripe_settings);
250 250
 
251
-			WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() );
251
+			WC_Stripe_Logger::log('Error: ' . $e->getMessage());
252 252
 		}
253 253
 	}
254 254
 
@@ -258,14 +258,14 @@  discard block
 block discarded – undo
258 258
 	 * @since 4.5.3
259 259
 	 * @version 4.5.3
260 260
 	 */
261
-	public function verify_domain_on_new_secret_key( $prev_settings, $settings ) {
261
+	public function verify_domain_on_new_secret_key($prev_settings, $settings) {
262 262
 		$this->stripe_settings = $prev_settings;
263 263
 		$prev_secret_key = $this->get_secret_key();
264 264
 
265 265
 		$this->stripe_settings = $settings;
266 266
 		$this->secret_key = $this->get_secret_key();
267 267
 
268
-		if ( ! empty( $this->secret_key ) && $this->secret_key !== $prev_secret_key ) {
268
+		if ( ! empty($this->secret_key) && $this->secret_key !== $prev_secret_key) {
269 269
 			$this->verify_domain();
270 270
 		}
271 271
 	}
@@ -277,7 +277,7 @@  discard block
 block discarded – undo
277 277
 	 * @version 4.5.3
278 278
 	 */
279 279
 	public function verify_domain_if_needed() {
280
-		if ( $this->apple_pay_domain_set ) {
280
+		if ($this->apple_pay_domain_set) {
281 281
 			$this->update_domain_association_file();
282 282
 		} else {
283 283
 			$this->verify_domain();
@@ -290,15 +290,15 @@  discard block
 block discarded – undo
290 290
 	 * @since 4.0.6
291 291
 	 */
292 292
 	public function admin_notices() {
293
-		if ( ! $this->stripe_enabled ) {
293
+		if ( ! $this->stripe_enabled) {
294 294
 			return;
295 295
 		}
296 296
 
297
-		if ( ! current_user_can( 'manage_woocommerce' ) ) {
297
+		if ( ! current_user_can('manage_woocommerce')) {
298 298
 			return;
299 299
 		}
300 300
 
301
-		if ( $this->payment_request && ! empty( $this->apple_pay_verify_notice ) ) {
301
+		if ($this->payment_request && ! empty($this->apple_pay_verify_notice)) {
302 302
 			$allowed_html = array(
303 303
 				'a' => array(
304 304
 					'href'  => array(),
@@ -306,7 +306,7 @@  discard block
 block discarded – undo
306 306
 				),
307 307
 			);
308 308
 
309
-			echo '<div class="error stripe-apple-pay-message"><p>' . wp_kses( make_clickable( $this->apple_pay_verify_notice ), $allowed_html ) . '</p></div>';
309
+			echo '<div class="error stripe-apple-pay-message"><p>' . wp_kses(make_clickable($this->apple_pay_verify_notice), $allowed_html) . '</p></div>';
310 310
 		}
311 311
 
312 312
 		/**
@@ -314,9 +314,9 @@  discard block
 block discarded – undo
314 314
 		 * when setting screen is displayed. So if domain verification is not set,
315 315
 		 * something went wrong so lets notify user.
316 316
 		 */
317
-		if ( ! empty( $this->secret_key ) && $this->payment_request && ! $this->apple_pay_domain_set ) {
317
+		if ( ! empty($this->secret_key) && $this->payment_request && ! $this->apple_pay_domain_set) {
318 318
 			/* translators: 1) HTML anchor open tag 2) HTML anchor closing tag */
319
-			echo '<div class="error stripe-apple-pay-message"><p>' . sprintf( __( 'Apple Pay domain verification failed. Please check the %1$slog%2$s to see the issue. (Logging must be enabled to see recorded logs)', 'woocommerce-gateway-stripe' ), '<a href="' . admin_url( 'admin.php?page=wc-status&tab=logs' ) . '">', '</a>' ) . '</p></div>';
319
+			echo '<div class="error stripe-apple-pay-message"><p>' . sprintf(__('Apple Pay domain verification failed. Please check the %1$slog%2$s to see the issue. (Logging must be enabled to see recorded logs)', 'woocommerce-gateway-stripe'), '<a href="' . admin_url('admin.php?page=wc-status&tab=logs') . '">', '</a>') . '</p></div>';
320 320
 		}
321 321
 	}
322 322
 }
Please login to merge, or discard this patch.