Completed
Pull Request — master (#1321)
by
unknown
01:44
created
includes/class-wc-stripe-apple-pay-registration.php 1 patch
Spacing   +59 added lines, -59 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
 
@@ -176,31 +176,31 @@  discard block
 block discarded – undo
176 176
 	 * @since 4.3.0
177 177
 	 * @return bool True on success, false on failure.
178 178
 	 */
179
-	public function update_domain_association_file( $force = false ) {
180
-			$path     = untrailingslashit( $_SERVER['DOCUMENT_ROOT'] );
179
+	public function update_domain_association_file($force = false) {
180
+			$path     = untrailingslashit($_SERVER['DOCUMENT_ROOT']);
181 181
 			$dir      = '.well-known';
182 182
 			$file     = 'apple-developer-merchantid-domain-association';
183 183
 			$fullpath = $path . '/' . $dir . '/' . $file;
184 184
 
185
-			$existing_contents = @file_get_contents( $fullpath );
186
-			$new_contents = @file_get_contents( WC_STRIPE_PLUGIN_PATH . '/' . $file );
187
-			if ( ( ! $existing_contents && ! $force ) || $existing_contents === $new_contents ) {
185
+			$existing_contents = @file_get_contents($fullpath);
186
+			$new_contents = @file_get_contents(WC_STRIPE_PLUGIN_PATH . '/' . $file);
187
+			if (( ! $existing_contents && ! $force) || $existing_contents === $new_contents) {
188 188
 				return true;
189 189
 			}
190 190
 
191
-			if ( ! file_exists( $path . '/' . $dir ) ) {
192
-				if ( ! @mkdir( $path . '/' . $dir, 0755 ) ) { // @codingStandardsIgnoreLine
193
-					WC_Stripe_Logger::log( 'Error: ' . __( 'Unable to create domain association folder to domain root.', 'woocommerce-gateway-stripe' ) );
191
+			if ( ! file_exists($path . '/' . $dir)) {
192
+				if ( ! @mkdir($path . '/' . $dir, 0755)) { // @codingStandardsIgnoreLine
193
+					WC_Stripe_Logger::log('Error: ' . __('Unable to create domain association folder to domain root.', 'woocommerce-gateway-stripe'));
194 194
 					return false;
195 195
 				}
196 196
 			}
197 197
 
198
-			if ( ! @copy( WC_STRIPE_PLUGIN_PATH . '/' . $file, $fullpath ) ) { // @codingStandardsIgnoreLine
199
-				WC_Stripe_Logger::log( 'Error: ' . __( 'Unable to copy domain association file to domain root.', 'woocommerce-gateway-stripe' ) );
198
+			if ( ! @copy(WC_STRIPE_PLUGIN_PATH . '/' . $file, $fullpath)) { // @codingStandardsIgnoreLine
199
+				WC_Stripe_Logger::log('Error: ' . __('Unable to copy domain association file to domain root.', 'woocommerce-gateway-stripe'));
200 200
 				return false;
201 201
 			}
202 202
 
203
-			WC_Stripe_Logger::log( 'Domain association file updated.' );
203
+			WC_Stripe_Logger::log('Domain association file updated.');
204 204
 			return true;
205 205
 	}
206 206
 
@@ -211,32 +211,32 @@  discard block
 block discarded – undo
211 211
 	 * @version 3.1.0
212 212
 	 */
213 213
 	public function verify_domain() {
214
-		if ( ! $this->update_domain_association_file( true ) ) {
214
+		if ( ! $this->update_domain_association_file(true)) {
215 215
 			$this->stripe_settings['apple_pay_domain_set'] = 'no';
216
-			update_option( 'woocommerce_stripe_settings', $this->stripe_settings );
216
+			update_option('woocommerce_stripe_settings', $this->stripe_settings);
217 217
 			return;
218 218
 		}
219 219
 
220 220
 		try {
221 221
 			// At this point then the domain association folder and file should be available.
222 222
 			// Proceed to verify/and or verify again.
223
-			$this->register_domain_with_apple( $this->secret_key );
223
+			$this->register_domain_with_apple($this->secret_key);
224 224
 
225 225
 			// No errors to this point, verification success!
226 226
 			$this->stripe_settings['apple_pay_domain_set'] = 'yes';
227 227
 			$this->apple_pay_domain_set                    = true;
228 228
 
229
-			update_option( 'woocommerce_stripe_settings', $this->stripe_settings );
229
+			update_option('woocommerce_stripe_settings', $this->stripe_settings);
230 230
 
231
-			WC_Stripe_Logger::log( 'Your domain has been verified with Apple Pay!' );
231
+			WC_Stripe_Logger::log('Your domain has been verified with Apple Pay!');
232 232
 
233
-		} catch ( Exception $e ) {
233
+		} catch (Exception $e) {
234 234
 			$this->stripe_settings['apple_pay_domain_set'] = 'no';
235 235
 			$this->apple_pay_domain_set                    = false;
236 236
 
237
-			update_option( 'woocommerce_stripe_settings', $this->stripe_settings );
237
+			update_option('woocommerce_stripe_settings', $this->stripe_settings);
238 238
 
239
-			WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() );
239
+			WC_Stripe_Logger::log('Error: ' . $e->getMessage());
240 240
 		}
241 241
 	}
242 242
 
@@ -246,14 +246,14 @@  discard block
 block discarded – undo
246 246
 	 * @since 4.5.3
247 247
 	 * @version 4.5.3
248 248
 	 */
249
-	public function verify_domain_on_new_secret_key( $prev_settings, $settings ) {
249
+	public function verify_domain_on_new_secret_key($prev_settings, $settings) {
250 250
 		$this->stripe_settings = $prev_settings;
251 251
 		$prev_secret_key = $this->get_secret_key();
252 252
 
253 253
 		$this->stripe_settings = $settings;
254 254
 		$this->secret_key = $this->get_secret_key();
255 255
 
256
-		if ( ! empty( $this->secret_key ) && $this->secret_key !== $prev_secret_key ) {
256
+		if ( ! empty($this->secret_key) && $this->secret_key !== $prev_secret_key) {
257 257
 			$this->verify_domain();
258 258
 		}
259 259
 	}
@@ -265,7 +265,7 @@  discard block
 block discarded – undo
265 265
 	 * @version 4.5.3
266 266
 	 */
267 267
 	public function verify_domain_if_needed() {
268
-		if ( $this->apple_pay_domain_set ) {
268
+		if ($this->apple_pay_domain_set) {
269 269
 			$this->update_domain_association_file();
270 270
 		} else {
271 271
 			$this->verify_domain();
@@ -278,16 +278,16 @@  discard block
 block discarded – undo
278 278
 	 * @since 4.0.6
279 279
 	 */
280 280
 	public function admin_notices() {
281
-		if ( ! $this->stripe_enabled || ! $this->payment_request ) {
281
+		if ( ! $this->stripe_enabled || ! $this->payment_request) {
282 282
 			return;
283 283
 		}
284 284
 
285
-		if ( ! current_user_can( 'manage_woocommerce' ) ) {
285
+		if ( ! current_user_can('manage_woocommerce')) {
286 286
 			return;
287 287
 		}
288 288
 
289
-		$empty_notice = empty( $this->apple_pay_verify_notice );
290
-		if ( $empty_notice && ( $this->apple_pay_domain_set || empty( $this->secret_key ) ) ) {
289
+		$empty_notice = empty($this->apple_pay_verify_notice);
290
+		if ($empty_notice && ($this->apple_pay_domain_set || empty($this->secret_key))) {
291 291
 			return;
292 292
 		}
293 293
 
@@ -296,19 +296,19 @@  discard block
 block discarded – undo
296 296
 		 * when setting screen is displayed. So if domain verification is not set,
297 297
 		 * something went wrong so lets notify user.
298 298
 		 */
299
-		$allowed_html                      = array(
299
+		$allowed_html = array(
300 300
 			'a' => array(
301 301
 				'href'  => array(),
302 302
 				'title' => array(),
303 303
 			),
304 304
 		);
305
-		$error_from_stripe                 = $empty_notice ? '' : '<p>' . wp_kses( make_clickable( esc_html( $this->apple_pay_verify_notice ) ), $allowed_html ) . '.</p>';
306
-		$verification_failed_without_error = esc_html__( 'Apple Pay domain verification failed.', 'woocommerce-gateway-stripe' );
307
-		$verification_failed_with_error    = esc_html__( 'Apple Pay domain verification failed with the following error:', 'woocommerce-gateway-stripe' );
305
+		$error_from_stripe                 = $empty_notice ? '' : '<p>' . wp_kses(make_clickable(esc_html($this->apple_pay_verify_notice)), $allowed_html) . '.</p>';
306
+		$verification_failed_without_error = esc_html__('Apple Pay domain verification failed.', 'woocommerce-gateway-stripe');
307
+		$verification_failed_with_error    = esc_html__('Apple Pay domain verification failed with the following error:', 'woocommerce-gateway-stripe');
308 308
 		$check_log_text                    = sprintf(
309 309
 			/* translators: 1) HTML anchor open tag 2) HTML anchor closing tag */
310
-			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' ),
311
-			'<a href="' . admin_url( 'admin.php?page=wc-status&tab=logs' ) . '">',
310
+			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'),
311
+			'<a href="' . admin_url('admin.php?page=wc-status&tab=logs') . '">',
312 312
 			'</a>'
313 313
 		);
314 314
 
Please login to merge, or discard this patch.