Completed
Pull Request — master (#1320)
by
unknown
01:59
created
includes/class-wc-stripe-apple-pay-registration.php 1 patch
Spacing   +56 added lines, -56 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( 'updated_option', array( $this, 'verify_domain_on_new_secret_key' ), 10, 3 );
56
+		add_action('woocommerce_stripe_updated', array($this, 'verify_domain_if_needed'));
57
+		add_action('updated_option', array($this, 'verify_domain_on_new_secret_key'), 10, 3);
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,31 +211,31 @@  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
 
236
-			update_option( 'woocommerce_stripe_settings', $this->stripe_settings );
236
+			update_option('woocommerce_stripe_settings', $this->stripe_settings);
237 237
 
238
-			WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() );
238
+			WC_Stripe_Logger::log('Error: ' . $e->getMessage());
239 239
 		}
240 240
 	}
241 241
 
@@ -245,8 +245,8 @@  discard block
 block discarded – undo
245 245
 	 * @since 4.5.3
246 246
 	 * @version 4.5.3
247 247
 	 */
248
-	public function verify_domain_on_new_secret_key( $option, $prev_settings, $settings ) {
249
-		if ( 'woocommerce_stripe_settings' !== $option ) {
248
+	public function verify_domain_on_new_secret_key($option, $prev_settings, $settings) {
249
+		if ('woocommerce_stripe_settings' !== $option) {
250 250
 			return;
251 251
 		}
252 252
 
@@ -256,7 +256,7 @@  discard block
 block discarded – undo
256 256
 		$this->stripe_settings = $settings;
257 257
 		$this->secret_key = $this->get_secret_key();
258 258
 
259
-		if ( ! empty( $this->secret_key ) && $this->secret_key !== $prev_secret_key ) {
259
+		if ( ! empty($this->secret_key) && $this->secret_key !== $prev_secret_key) {
260 260
 			$this->verify_domain();
261 261
 		}
262 262
 	}
@@ -268,7 +268,7 @@  discard block
 block discarded – undo
268 268
 	 * @version 4.5.3
269 269
 	 */
270 270
 	public function verify_domain_if_needed() {
271
-		if ( $this->apple_pay_domain_set ) {
271
+		if ($this->apple_pay_domain_set) {
272 272
 			$this->update_domain_association_file();
273 273
 		} else {
274 274
 			$this->verify_domain();
@@ -281,15 +281,15 @@  discard block
 block discarded – undo
281 281
 	 * @since 4.0.6
282 282
 	 */
283 283
 	public function admin_notices() {
284
-		if ( ! $this->stripe_enabled ) {
284
+		if ( ! $this->stripe_enabled) {
285 285
 			return;
286 286
 		}
287 287
 
288
-		if ( ! current_user_can( 'manage_woocommerce' ) ) {
288
+		if ( ! current_user_can('manage_woocommerce')) {
289 289
 			return;
290 290
 		}
291 291
 
292
-		if ( $this->payment_request && ! empty( $this->apple_pay_verify_notice ) ) {
292
+		if ($this->payment_request && ! empty($this->apple_pay_verify_notice)) {
293 293
 			$allowed_html = array(
294 294
 				'a' => array(
295 295
 					'href'  => array(),
@@ -297,7 +297,7 @@  discard block
 block discarded – undo
297 297
 				),
298 298
 			);
299 299
 
300
-			echo '<div class="error stripe-apple-pay-message"><p>' . wp_kses( make_clickable( $this->apple_pay_verify_notice ), $allowed_html ) . '</p></div>';
300
+			echo '<div class="error stripe-apple-pay-message"><p>' . wp_kses(make_clickable($this->apple_pay_verify_notice), $allowed_html) . '</p></div>';
301 301
 		}
302 302
 
303 303
 		/**
@@ -305,9 +305,9 @@  discard block
 block discarded – undo
305 305
 		 * when setting screen is displayed. So if domain verification is not set,
306 306
 		 * something went wrong so lets notify user.
307 307
 		 */
308
-		if ( ! empty( $this->secret_key ) && $this->payment_request && ! $this->apple_pay_domain_set ) {
308
+		if ( ! empty($this->secret_key) && $this->payment_request && ! $this->apple_pay_domain_set) {
309 309
 			/* translators: 1) HTML anchor open tag 2) HTML anchor closing tag */
310
-			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>';
310
+			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>';
311 311
 		}
312 312
 	}
313 313
 }
Please login to merge, or discard this patch.