Completed
Pull Request — master (#1304)
by
unknown
01:51
created
includes/class-wc-stripe-customer.php 1 patch
Spacing   +105 added lines, -105 removed lines patch added patch discarded remove patch
@@ -1,5 +1,5 @@  discard block
 block discarded – undo
1 1
 <?php
2
-if ( ! defined( 'ABSPATH' ) ) {
2
+if ( ! defined('ABSPATH')) {
3 3
 	exit;
4 4
 }
5 5
 
@@ -32,10 +32,10 @@  discard block
 block discarded – undo
32 32
 	 * Constructor
33 33
 	 * @param int $user_id The WP user ID
34 34
 	 */
35
-	public function __construct( $user_id = 0 ) {
36
-		if ( $user_id ) {
37
-			$this->set_user_id( $user_id );
38
-			$this->set_id( $this->get_id_from_meta( $user_id ) );
35
+	public function __construct($user_id = 0) {
36
+		if ($user_id) {
37
+			$this->set_user_id($user_id);
38
+			$this->set_id($this->get_id_from_meta($user_id));
39 39
 		}
40 40
 	}
41 41
 
@@ -51,15 +51,15 @@  discard block
 block discarded – undo
51 51
 	 * Set Stripe customer ID.
52 52
 	 * @param [type] $id [description]
53 53
 	 */
54
-	public function set_id( $id ) {
54
+	public function set_id($id) {
55 55
 		// Backwards compat for customer ID stored in array format. (Pre 3.0)
56
-		if ( is_array( $id ) && isset( $id['customer_id'] ) ) {
56
+		if (is_array($id) && isset($id['customer_id'])) {
57 57
 			$id = $id['customer_id'];
58 58
 
59
-			$this->update_id_in_meta( $id );
59
+			$this->update_id_in_meta($id);
60 60
 		}
61 61
 
62
-		$this->id = wc_clean( $id );
62
+		$this->id = wc_clean($id);
63 63
 	}
64 64
 
65 65
 	/**
@@ -67,15 +67,15 @@  discard block
 block discarded – undo
67 67
 	 * @return int
68 68
 	 */
69 69
 	public function get_user_id() {
70
-		return absint( $this->user_id );
70
+		return absint($this->user_id);
71 71
 	}
72 72
 
73 73
 	/**
74 74
 	 * Set User ID used by WordPress.
75 75
 	 * @param int $user_id
76 76
 	 */
77
-	public function set_user_id( $user_id ) {
78
-		$this->user_id = absint( $user_id );
77
+	public function set_user_id($user_id) {
78
+		$this->user_id = absint($user_id);
79 79
 	}
80 80
 
81 81
 	/**
@@ -83,13 +83,13 @@  discard block
 block discarded – undo
83 83
 	 * @return WP_User
84 84
 	 */
85 85
 	protected function get_user() {
86
-		return $this->get_user_id() ? get_user_by( 'id', $this->get_user_id() ) : false;
86
+		return $this->get_user_id() ? get_user_by('id', $this->get_user_id()) : false;
87 87
 	}
88 88
 
89 89
 	/**
90 90
 	 * Store data from the Stripe API about this customer
91 91
 	 */
92
-	public function set_customer_data( $data ) {
92
+	public function set_customer_data($data) {
93 93
 		$this->customer_data = $data;
94 94
 	}
95 95
 
@@ -99,26 +99,26 @@  discard block
 block discarded – undo
99 99
 	 * @param  array $args Additional arguments (optional).
100 100
 	 * @return array
101 101
 	 */
102
-	protected function generate_customer_request( $args = array() ) {
103
-		$billing_email = isset( $_POST['billing_email'] ) ? filter_var( $_POST['billing_email'], FILTER_SANITIZE_EMAIL ) : '';
102
+	protected function generate_customer_request($args = array()) {
103
+		$billing_email = isset($_POST['billing_email']) ? filter_var($_POST['billing_email'], FILTER_SANITIZE_EMAIL) : '';
104 104
 		$user          = $this->get_user();
105 105
 
106
-		if ( $user ) {
107
-			$billing_first_name = get_user_meta( $user->ID, 'billing_first_name', true );
108
-			$billing_last_name  = get_user_meta( $user->ID, 'billing_last_name', true );
106
+		if ($user) {
107
+			$billing_first_name = get_user_meta($user->ID, 'billing_first_name', true);
108
+			$billing_last_name  = get_user_meta($user->ID, 'billing_last_name', true);
109 109
 
110 110
 			// If billing first name does not exists try the user first name.
111
-			if ( empty( $billing_first_name ) ) {
112
-				$billing_first_name = get_user_meta( $user->ID, 'first_name', true );
111
+			if (empty($billing_first_name)) {
112
+				$billing_first_name = get_user_meta($user->ID, 'first_name', true);
113 113
 			}
114 114
 
115 115
 			// If billing last name does not exists try the user last name.
116
-			if ( empty( $billing_last_name ) ) {
117
-				$billing_last_name = get_user_meta( $user->ID, 'last_name', true );
116
+			if (empty($billing_last_name)) {
117
+				$billing_last_name = get_user_meta($user->ID, 'last_name', true);
118 118
 			}
119 119
 
120 120
 			// translators: %1$s First name, %2$s Second name, %3$s Username.
121
-			$description = sprintf( __( 'Name: %1$s %2$s, Username: %s', 'woocommerce-gateway-stripe' ), $billing_first_name, $billing_last_name, $user->user_login );
121
+			$description = sprintf(__('Name: %1$s %2$s, Username: %s', 'woocommerce-gateway-stripe'), $billing_first_name, $billing_last_name, $user->user_login);
122 122
 
123 123
 			$billing_full_name = "$billing_first_name $billing_last_name";
124 124
 			
@@ -128,11 +128,11 @@  discard block
 block discarded – undo
128 128
 				'description' => $description,
129 129
 			);
130 130
 		} else {
131
-			$billing_first_name = isset( $_POST['billing_first_name'] ) ? filter_var( wp_unslash( $_POST['billing_first_name'] ), FILTER_SANITIZE_STRING ) : ''; // phpcs:ignore WordPress.Security.NonceVerification
132
-			$billing_last_name  = isset( $_POST['billing_last_name'] ) ? filter_var( wp_unslash( $_POST['billing_last_name'] ), FILTER_SANITIZE_STRING ) : ''; // phpcs:ignore WordPress.Security.NonceVerification
131
+			$billing_first_name = isset($_POST['billing_first_name']) ? filter_var(wp_unslash($_POST['billing_first_name']), FILTER_SANITIZE_STRING) : ''; // phpcs:ignore WordPress.Security.NonceVerification
132
+			$billing_last_name  = isset($_POST['billing_last_name']) ? filter_var(wp_unslash($_POST['billing_last_name']), FILTER_SANITIZE_STRING) : ''; // phpcs:ignore WordPress.Security.NonceVerification
133 133
 
134 134
 			// translators: %1$s First name, %2$s Second name.
135
-			$description = sprintf( __( 'Name: %1$s %2$s, Guest', 'woocommerce-gateway-stripe' ), $billing_first_name, $billing_last_name );
135
+			$description = sprintf(__('Name: %1$s %2$s, Guest', 'woocommerce-gateway-stripe'), $billing_first_name, $billing_last_name);
136 136
 			
137 137
 			$billing_full_name = "$billing_first_name $billing_last_name";
138 138
 			
@@ -144,9 +144,9 @@  discard block
 block discarded – undo
144 144
 		}
145 145
 
146 146
 		$metadata             = array();
147
-		$defaults['metadata'] = apply_filters( 'wc_stripe_customer_metadata', $metadata, $user );
147
+		$defaults['metadata'] = apply_filters('wc_stripe_customer_metadata', $metadata, $user);
148 148
 
149
-		return wp_parse_args( $args, $defaults );
149
+		return wp_parse_args($args, $defaults);
150 150
 	}
151 151
 
152 152
 	/**
@@ -154,23 +154,23 @@  discard block
 block discarded – undo
154 154
 	 * @param array $args
155 155
 	 * @return WP_Error|int
156 156
 	 */
157
-	public function create_customer( $args = array() ) {
158
-		$args     = $this->generate_customer_request( $args );
159
-		$response = WC_Stripe_API::request( apply_filters( 'wc_stripe_create_customer_args', $args ), 'customers' );
157
+	public function create_customer($args = array()) {
158
+		$args     = $this->generate_customer_request($args);
159
+		$response = WC_Stripe_API::request(apply_filters('wc_stripe_create_customer_args', $args), 'customers');
160 160
 
161
-		if ( ! empty( $response->error ) ) {
162
-			throw new WC_Stripe_Exception( print_r( $response, true ), $response->error->message );
161
+		if ( ! empty($response->error)) {
162
+			throw new WC_Stripe_Exception(print_r($response, true), $response->error->message);
163 163
 		}
164 164
 
165
-		$this->set_id( $response->id );
165
+		$this->set_id($response->id);
166 166
 		$this->clear_cache();
167
-		$this->set_customer_data( $response );
167
+		$this->set_customer_data($response);
168 168
 
169
-		if ( $this->get_user_id() ) {
170
-			$this->update_id_in_meta( $response->id );
169
+		if ($this->get_user_id()) {
170
+			$this->update_id_in_meta($response->id);
171 171
 		}
172 172
 
173
-		do_action( 'woocommerce_stripe_add_customer', $args, $response );
173
+		do_action('woocommerce_stripe_add_customer', $args, $response);
174 174
 
175 175
 		return $response->id;
176 176
 	}
@@ -185,30 +185,30 @@  discard block
 block discarded – undo
185 185
 	 *
186 186
 	 * @throws WC_Stripe_Exception
187 187
 	 */
188
-	public function update_customer( $args = array(), $is_retry = false ) {
189
-		if ( empty( $this->get_id() ) ) {
190
-			throw new WC_Stripe_Exception( 'id_required_to_update_user', __( 'Attempting to update a Stripe customer without a customer ID.', 'woocommerce-gateway-stripe' ) );
188
+	public function update_customer($args = array(), $is_retry = false) {
189
+		if (empty($this->get_id())) {
190
+			throw new WC_Stripe_Exception('id_required_to_update_user', __('Attempting to update a Stripe customer without a customer ID.', 'woocommerce-gateway-stripe'));
191 191
 		}
192 192
 
193
-		$args     = $this->generate_customer_request( $args );
194
-		$args     = apply_filters( 'wc_stripe_update_customer_args', $args );
195
-		$response = WC_Stripe_API::request( $args, 'customers/' . $this->get_id() );
193
+		$args     = $this->generate_customer_request($args);
194
+		$args     = apply_filters('wc_stripe_update_customer_args', $args);
195
+		$response = WC_Stripe_API::request($args, 'customers/' . $this->get_id());
196 196
 
197
-		if ( ! empty( $response->error ) ) {
198
-			if ( $this->is_no_such_customer_error( $response->error ) && ! $is_retry ) {
197
+		if ( ! empty($response->error)) {
198
+			if ($this->is_no_such_customer_error($response->error) && ! $is_retry) {
199 199
 				// This can happen when switching the main Stripe account or importing users from another site.
200 200
 				// If not already retrying, recreate the customer and then try updating it again.
201 201
 				$this->recreate_customer();
202
-				return $this->update_customer( $args, true );
202
+				return $this->update_customer($args, true);
203 203
 			}
204 204
 
205
-			throw new WC_Stripe_Exception( print_r( $response, true ), $response->error->message );
205
+			throw new WC_Stripe_Exception(print_r($response, true), $response->error->message);
206 206
 		}
207 207
 
208 208
 		$this->clear_cache();
209
-		$this->set_customer_data( $response );
209
+		$this->set_customer_data($response);
210 210
 
211
-		do_action( 'woocommerce_stripe_update_customer', $args, $response );
211
+		do_action('woocommerce_stripe_update_customer', $args, $response);
212 212
 
213 213
 		return $this->get_id();
214 214
 	}
@@ -220,11 +220,11 @@  discard block
 block discarded – undo
220 220
 	 * @since 4.1.2
221 221
 	 * @param array $error
222 222
 	 */
223
-	public function is_no_such_customer_error( $error ) {
223
+	public function is_no_such_customer_error($error) {
224 224
 		return (
225 225
 			$error &&
226 226
 			'invalid_request_error' === $error->type &&
227
-			preg_match( '/No such customer/i', $error->message )
227
+			preg_match('/No such customer/i', $error->message)
228 228
 		);
229 229
 	}
230 230
 
@@ -233,9 +233,9 @@  discard block
 block discarded – undo
233 233
 	 * @param string $source_id
234 234
 	 * @return WP_Error|int
235 235
 	 */
236
-	public function add_source( $source_id ) {
237
-		if ( ! $this->get_id() ) {
238
-			$this->set_id( $this->create_customer() );
236
+	public function add_source($source_id) {
237
+		if ( ! $this->get_id()) {
238
+			$this->set_id($this->create_customer());
239 239
 		}
240 240
 
241 241
 		$response = WC_Stripe_API::request(
@@ -247,62 +247,62 @@  discard block
 block discarded – undo
247 247
 
248 248
 		$wc_token = false;
249 249
 
250
-		if ( ! empty( $response->error ) ) {
250
+		if ( ! empty($response->error)) {
251 251
 			// It is possible the WC user once was linked to a customer on Stripe
252 252
 			// but no longer exists. Instead of failing, lets try to create a
253 253
 			// new customer.
254
-			if ( $this->is_no_such_customer_error( $response->error ) ) {
254
+			if ($this->is_no_such_customer_error($response->error)) {
255 255
 				$this->recreate_customer();
256
-				return $this->add_source( $source_id );
256
+				return $this->add_source($source_id);
257 257
 			} else {
258 258
 				return $response;
259 259
 			}
260
-		} elseif ( empty( $response->id ) ) {
261
-			return new WP_Error( 'error', __( 'Unable to add payment source.', 'woocommerce-gateway-stripe' ) );
260
+		} elseif (empty($response->id)) {
261
+			return new WP_Error('error', __('Unable to add payment source.', 'woocommerce-gateway-stripe'));
262 262
 		}
263 263
 
264 264
 		// Add token to WooCommerce.
265
-		if ( $this->get_user_id() && class_exists( 'WC_Payment_Token_CC' ) ) {
266
-			if ( ! empty( $response->type ) ) {
267
-				switch ( $response->type ) {
265
+		if ($this->get_user_id() && class_exists('WC_Payment_Token_CC')) {
266
+			if ( ! empty($response->type)) {
267
+				switch ($response->type) {
268 268
 					case 'alipay':
269 269
 						break;
270 270
 					case 'sepa_debit':
271 271
 						$wc_token = new WC_Payment_Token_SEPA();
272
-						$wc_token->set_token( $response->id );
273
-						$wc_token->set_gateway_id( 'stripe_sepa' );
274
-						$wc_token->set_last4( $response->sepa_debit->last4 );
272
+						$wc_token->set_token($response->id);
273
+						$wc_token->set_gateway_id('stripe_sepa');
274
+						$wc_token->set_last4($response->sepa_debit->last4);
275 275
 						break;
276 276
 					default:
277
-						if ( 'source' === $response->object && 'card' === $response->type ) {
277
+						if ('source' === $response->object && 'card' === $response->type) {
278 278
 							$wc_token = new WC_Payment_Token_CC();
279
-							$wc_token->set_token( $response->id );
280
-							$wc_token->set_gateway_id( 'stripe' );
281
-							$wc_token->set_card_type( strtolower( $response->card->brand ) );
282
-							$wc_token->set_last4( $response->card->last4 );
283
-							$wc_token->set_expiry_month( $response->card->exp_month );
284
-							$wc_token->set_expiry_year( $response->card->exp_year );
279
+							$wc_token->set_token($response->id);
280
+							$wc_token->set_gateway_id('stripe');
281
+							$wc_token->set_card_type(strtolower($response->card->brand));
282
+							$wc_token->set_last4($response->card->last4);
283
+							$wc_token->set_expiry_month($response->card->exp_month);
284
+							$wc_token->set_expiry_year($response->card->exp_year);
285 285
 						}
286 286
 						break;
287 287
 				}
288 288
 			} else {
289 289
 				// Legacy.
290 290
 				$wc_token = new WC_Payment_Token_CC();
291
-				$wc_token->set_token( $response->id );
292
-				$wc_token->set_gateway_id( 'stripe' );
293
-				$wc_token->set_card_type( strtolower( $response->brand ) );
294
-				$wc_token->set_last4( $response->last4 );
295
-				$wc_token->set_expiry_month( $response->exp_month );
296
-				$wc_token->set_expiry_year( $response->exp_year );
291
+				$wc_token->set_token($response->id);
292
+				$wc_token->set_gateway_id('stripe');
293
+				$wc_token->set_card_type(strtolower($response->brand));
294
+				$wc_token->set_last4($response->last4);
295
+				$wc_token->set_expiry_month($response->exp_month);
296
+				$wc_token->set_expiry_year($response->exp_year);
297 297
 			}
298 298
 
299
-			$wc_token->set_user_id( $this->get_user_id() );
299
+			$wc_token->set_user_id($this->get_user_id());
300 300
 			$wc_token->save();
301 301
 		}
302 302
 
303 303
 		$this->clear_cache();
304 304
 
305
-		do_action( 'woocommerce_stripe_add_source', $this->get_id(), $wc_token, $response, $source_id );
305
+		do_action('woocommerce_stripe_add_source', $this->get_id(), $wc_token, $response, $source_id);
306 306
 
307 307
 		return $response->id;
308 308
 	}
@@ -314,13 +314,13 @@  discard block
 block discarded – undo
314 314
 	 * @return array
315 315
 	 */
316 316
 	public function get_sources() {
317
-		if ( ! $this->get_id() ) {
317
+		if ( ! $this->get_id()) {
318 318
 			return array();
319 319
 		}
320 320
 
321
-		$sources = get_transient( 'stripe_sources_' . $this->get_id() );
321
+		$sources = get_transient('stripe_sources_' . $this->get_id());
322 322
 
323
-		if ( false === $sources ) {
323
+		if (false === $sources) {
324 324
 			$response = WC_Stripe_API::request(
325 325
 				array(
326 326
 					'limit' => 100,
@@ -329,35 +329,35 @@  discard block
 block discarded – undo
329 329
 				'GET'
330 330
 			);
331 331
 
332
-			if ( ! empty( $response->error ) ) {
332
+			if ( ! empty($response->error)) {
333 333
 				return array();
334 334
 			}
335 335
 
336
-			if ( is_array( $response->data ) ) {
336
+			if (is_array($response->data)) {
337 337
 				$sources = $response->data;
338 338
 			}
339 339
 
340
-			set_transient( 'stripe_sources_' . $this->get_id(), $sources, DAY_IN_SECONDS );
340
+			set_transient('stripe_sources_' . $this->get_id(), $sources, DAY_IN_SECONDS);
341 341
 		}
342 342
 
343
-		return empty( $sources ) ? array() : $sources;
343
+		return empty($sources) ? array() : $sources;
344 344
 	}
345 345
 
346 346
 	/**
347 347
 	 * Delete a source from stripe.
348 348
 	 * @param string $source_id
349 349
 	 */
350
-	public function delete_source( $source_id ) {
351
-		if ( ! $this->get_id() ) {
350
+	public function delete_source($source_id) {
351
+		if ( ! $this->get_id()) {
352 352
 			return false;
353 353
 		}
354 354
 
355
-		$response = WC_Stripe_API::request( array(), 'customers/' . $this->get_id() . '/sources/' . sanitize_text_field( $source_id ), 'DELETE' );
355
+		$response = WC_Stripe_API::request(array(), 'customers/' . $this->get_id() . '/sources/' . sanitize_text_field($source_id), 'DELETE');
356 356
 
357 357
 		$this->clear_cache();
358 358
 
359
-		if ( empty( $response->error ) ) {
360
-			do_action( 'wc_stripe_delete_source', $this->get_id(), $response );
359
+		if (empty($response->error)) {
360
+			do_action('wc_stripe_delete_source', $this->get_id(), $response);
361 361
 
362 362
 			return true;
363 363
 		}
@@ -369,10 +369,10 @@  discard block
 block discarded – undo
369 369
 	 * Set default source in Stripe
370 370
 	 * @param string $source_id
371 371
 	 */
372
-	public function set_default_source( $source_id ) {
372
+	public function set_default_source($source_id) {
373 373
 		$response = WC_Stripe_API::request(
374 374
 			array(
375
-				'default_source' => sanitize_text_field( $source_id ),
375
+				'default_source' => sanitize_text_field($source_id),
376 376
 			),
377 377
 			'customers/' . $this->get_id(),
378 378
 			'POST'
@@ -380,8 +380,8 @@  discard block
 block discarded – undo
380 380
 
381 381
 		$this->clear_cache();
382 382
 
383
-		if ( empty( $response->error ) ) {
384
-			do_action( 'wc_stripe_set_default_source', $this->get_id(), $response );
383
+		if (empty($response->error)) {
384
+			do_action('wc_stripe_set_default_source', $this->get_id(), $response);
385 385
 
386 386
 			return true;
387 387
 		}
@@ -393,8 +393,8 @@  discard block
 block discarded – undo
393 393
 	 * Deletes caches for this users cards.
394 394
 	 */
395 395
 	public function clear_cache() {
396
-		delete_transient( 'stripe_sources_' . $this->get_id() );
397
-		delete_transient( 'stripe_customer_' . $this->get_id() );
396
+		delete_transient('stripe_sources_' . $this->get_id());
397
+		delete_transient('stripe_customer_' . $this->get_id());
398 398
 		$this->customer_data = array();
399 399
 	}
400 400
 
@@ -404,8 +404,8 @@  discard block
 block discarded – undo
404 404
 	 * @param  int $user_id The ID of the WordPress user.
405 405
 	 * @return string|bool  Either the Stripe ID or false.
406 406
 	 */
407
-	public function get_id_from_meta( $user_id ) {
408
-		return get_user_option( '_stripe_customer_id', $user_id );
407
+	public function get_id_from_meta($user_id) {
408
+		return get_user_option('_stripe_customer_id', $user_id);
409 409
 	}
410 410
 
411 411
 	/**
@@ -413,15 +413,15 @@  discard block
 block discarded – undo
413 413
 	 *
414 414
 	 * @param string $id The Stripe customer ID.
415 415
 	 */
416
-	public function update_id_in_meta( $id ) {
417
-		update_user_option( $this->get_user_id(), '_stripe_customer_id', $id, false );
416
+	public function update_id_in_meta($id) {
417
+		update_user_option($this->get_user_id(), '_stripe_customer_id', $id, false);
418 418
 	}
419 419
 
420 420
 	/**
421 421
 	 * Deletes the user ID from the meta table with the right key.
422 422
 	 */
423 423
 	public function delete_id_from_meta() {
424
-		delete_user_option( $this->get_user_id(), '_stripe_customer_id', false );
424
+		delete_user_option($this->get_user_id(), '_stripe_customer_id', false);
425 425
 	}
426 426
 
427 427
 	/**
Please login to merge, or discard this patch.