Completed
Push — master ( f6d20e...4e14c4 )
by Mike
11:10
created

WC_Form_Handler::pay_action()   C

Complexity

Conditions 19
Paths 18

Size

Total Lines 79
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 19
eloc 41
c 2
b 0
f 0
nc 18
nop 0
dl 0
loc 79
rs 5.0683

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
if ( ! defined( 'ABSPATH' ) ) {
4
	exit; // Exit if accessed directly
5
}
6
7
/**
8
 * Handle frontend forms.
9
 *
10
 * @class 		WC_Form_Handler
11
 * @version		2.2.0
12
 * @package		WooCommerce/Classes/
13
 * @category	Class
14
 * @author 		WooThemes
15
 */
16
class WC_Form_Handler {
17
18
	/**
19
	 * Hook in methods.
20
	 */
21
	public static function init() {
22
		add_action( 'template_redirect', array( __CLASS__, 'redirect_reset_password_link' ) );
23
		add_action( 'template_redirect', array( __CLASS__, 'save_address' ) );
24
		add_action( 'template_redirect', array( __CLASS__, 'save_account_details' ) );
25
		add_action( 'wp_loaded', array( __CLASS__, 'checkout_action' ), 20 );
26
		add_action( 'wp_loaded', array( __CLASS__, 'process_login' ), 20 );
27
		add_action( 'wp_loaded', array( __CLASS__, 'process_registration' ), 20 );
28
		add_action( 'wp_loaded', array( __CLASS__, 'process_lost_password' ), 20 );
29
		add_action( 'wp_loaded', array( __CLASS__, 'process_reset_password' ), 20 );
30
		add_action( 'wp_loaded', array( __CLASS__, 'cancel_order' ), 20 );
31
		add_action( 'wp_loaded', array( __CLASS__, 'order_again' ), 20 );
32
		add_action( 'wp_loaded', array( __CLASS__, 'update_cart_action' ), 20 );
33
		add_action( 'wp_loaded', array( __CLASS__, 'add_to_cart_action' ), 20 );
34
35
		// May need $wp global to access query vars.
36
		add_action( 'wp', array( __CLASS__, 'pay_action' ), 20 );
37
		add_action( 'wp', array( __CLASS__, 'add_payment_method_action' ), 20 );
38
		add_action( 'wp', array( __CLASS__, 'delete_payment_method_action' ), 20 );
39
		add_action( 'wp', array( __CLASS__, 'set_default_payment_method_action' ), 20 );
40
	}
41
42
	/**
43
	 * Remove key and login from querystring, set cookie, and redirect to account page to show the form.
44
	 */
45
	public static function redirect_reset_password_link() {
46
		if ( is_account_page() && ! empty( $_GET['key'] ) && ! empty( $_GET['login'] ) ) {
47
			$value = sprintf( '%s:%s', wp_unslash( $_GET['login'] ), wp_unslash( $_GET['key'] ) );
48
			WC_Shortcode_My_Account::set_reset_password_cookie( $value );
49
50
			wp_safe_redirect( add_query_arg( 'show-reset-form', 'true', wc_lostpassword_url() ) );
51
			exit;
52
		}
53
	}
54
55
	/**
56
	 * Save and and update a billing or shipping address if the
57
	 * form was submitted through the user account page.
58
	 */
59
	public static function save_address() {
60
		global $wp;
61
62
		if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) ) {
63
			return;
64
		}
65
66 View Code Duplication
		if ( empty( $_POST['action'] ) || 'edit_address' !== $_POST['action'] || empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'woocommerce-edit_address' ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
			return;
68
		}
69
70
		$user_id = get_current_user_id();
71
72
		if ( $user_id <= 0 ) {
73
			return;
74
		}
75
76
		$load_address = isset( $wp->query_vars['edit-address'] ) ? wc_edit_address_i18n( sanitize_title( $wp->query_vars['edit-address'] ), true ) : 'billing';
77
78
		$address = WC()->countries->get_address_fields( esc_attr( $_POST[ $load_address . '_country' ] ), $load_address . '_' );
79
80
		foreach ( $address as $key => $field ) {
81
82
			if ( ! isset( $field['type'] ) ) {
83
				$field['type'] = 'text';
84
			}
85
86
			// Get Value.
87
			switch ( $field['type'] ) {
88
				case 'checkbox' :
89
					$_POST[ $key ] = isset( $_POST[ $key ] ) ? 1 : 0;
90
				break;
91
				default :
92
					$_POST[ $key ] = isset( $_POST[ $key ] ) ? wc_clean( $_POST[ $key ] ) : '';
93
				break;
94
			}
95
96
			// Hook to allow modification of value.
97
			$_POST[ $key ] = apply_filters( 'woocommerce_process_myaccount_field_' . $key, $_POST[ $key ] );
98
99
			// Validation: Required fields.
100
			if ( ! empty( $field['required'] ) && empty( $_POST[ $key ] ) ) {
101
				wc_add_notice( $field['label'] . ' ' . __( 'is a required field.', 'woocommerce' ), 'error' );
102
			}
103
104
			if ( ! empty( $_POST[ $key ] ) ) {
105
106
				// Validation rules
107
				if ( ! empty( $field['validate'] ) && is_array( $field['validate'] ) ) {
108
					foreach ( $field['validate'] as $rule ) {
109
						switch ( $rule ) {
110
							case 'postcode' :
111
								$_POST[ $key ] = strtoupper( str_replace( ' ', '', $_POST[ $key ] ) );
112
113
								if ( ! WC_Validation::is_postcode( $_POST[ $key ], $_POST[ $load_address . '_country' ] ) ) {
114
									wc_add_notice( __( 'Please enter a valid postcode/ZIP.', 'woocommerce' ), 'error' );
115
								} else {
116
									$_POST[ $key ] = wc_format_postcode( $_POST[ $key ], $_POST[ $load_address . '_country' ] );
117
								}
118
							break;
119
							case 'phone' :
120
								$_POST[ $key ] = wc_format_phone_number( $_POST[ $key ] );
121
122
								if ( ! WC_Validation::is_phone( $_POST[ $key ] ) ) {
123
									wc_add_notice( '<strong>' . $field['label'] . '</strong> ' . __( 'is not a valid phone number.', 'woocommerce' ), 'error' );
124
								}
125
							break;
126
							case 'email' :
127
								$_POST[ $key ] = strtolower( $_POST[ $key ] );
128
129
								if ( ! is_email( $_POST[ $key ] ) ) {
130
									wc_add_notice( '<strong>' . $field['label'] . '</strong> ' . __( 'is not a valid email address.', 'woocommerce' ), 'error' );
131
								}
132
							break;
133
						}
134
					}
135
				}
136
			}
137
		}
138
139
		if ( wc_notice_count( 'error' ) == 0 ) {
140
141
			foreach ( $address as $key => $field ) {
142
				update_user_meta( $user_id, $key, $_POST[ $key ] );
143
			}
144
145
			wc_add_notice( __( 'Address changed successfully.', 'woocommerce' ) );
146
147
			do_action( 'woocommerce_customer_save_address', $user_id, $load_address );
148
149
			wp_safe_redirect( wc_get_endpoint_url( 'edit-address', '', wc_get_page_permalink( 'myaccount' ) ) );
150
			exit;
151
		}
152
	}
153
154
	/**
155
	 * Save the password/account details and redirect back to the my account page.
156
	 */
157
	public static function save_account_details() {
158
159
		if ( 'POST' !== strtoupper( $_SERVER[ 'REQUEST_METHOD' ] ) ) {
160
			return;
161
		}
162
163 View Code Duplication
		if ( empty( $_POST[ 'action' ] ) || 'save_account_details' !== $_POST[ 'action' ] || empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'save_account_details' ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
164
			return;
165
		}
166
167
		$errors       = new WP_Error();
168
		$user         = new stdClass();
169
170
		$user->ID     = (int) get_current_user_id();
171
		$current_user = get_user_by( 'id', $user->ID );
172
173
		if ( $user->ID <= 0 ) {
174
			return;
175
		}
176
177
		$account_first_name = ! empty( $_POST[ 'account_first_name' ] ) ? wc_clean( $_POST[ 'account_first_name' ] ) : '';
178
		$account_last_name  = ! empty( $_POST[ 'account_last_name' ] ) ? wc_clean( $_POST[ 'account_last_name' ] ) : '';
179
		$account_email      = ! empty( $_POST[ 'account_email' ] ) ? sanitize_email( $_POST[ 'account_email' ] ) : '';
180
		$pass_cur           = ! empty( $_POST[ 'password_current' ] ) ? $_POST[ 'password_current' ] : '';
181
		$pass1              = ! empty( $_POST[ 'password_1' ] ) ? $_POST[ 'password_1' ] : '';
182
		$pass2              = ! empty( $_POST[ 'password_2' ] ) ? $_POST[ 'password_2' ] : '';
183
		$save_pass          = true;
184
185
		$user->first_name   = $account_first_name;
186
		$user->last_name    = $account_last_name;
187
188
		// Prevent emails being displayed, or leave alone.
189
		$user->display_name = is_email( $current_user->display_name ) ? $user->first_name : $current_user->display_name;
190
191
		// Handle required fields
192
		$required_fields = apply_filters( 'woocommerce_save_account_details_required_fields', array(
193
			'account_first_name' => __( 'First Name', 'woocommerce' ),
194
			'account_last_name'  => __( 'Last Name', 'woocommerce' ),
195
			'account_email'      => __( 'Email address', 'woocommerce' ),
196
		) );
197
198
		foreach ( $required_fields as $field_key => $field_name ) {
199
			$value = wc_clean( $_POST[ $field_key ] );
200
			if ( empty( $value ) ) {
201
				wc_add_notice( '<strong>' . esc_html( $field_name ) . '</strong> ' . __( 'is a required field.', 'woocommerce' ), 'error' );
202
			}
203
		}
204
205
		if ( $account_email ) {
206
			if ( ! is_email( $account_email ) ) {
207
				wc_add_notice( __( 'Please provide a valid email address.', 'woocommerce' ), 'error' );
208
			} elseif ( email_exists( $account_email ) && $account_email !== $current_user->user_email ) {
209
				wc_add_notice( __( 'This email address is already registered.', 'woocommerce' ), 'error' );
210
			}
211
			$user->user_email = $account_email;
212
		}
213
214
		if ( ! empty( $pass_cur ) && empty( $pass1 ) && empty( $pass2 ) ) {
215
			wc_add_notice( __( 'Please fill out all password fields.', 'woocommerce' ), 'error' );
216
			$save_pass = false;
217 View Code Duplication
		} elseif ( ! empty( $pass1 ) && empty( $pass_cur ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
218
			wc_add_notice( __( 'Please enter your current password.', 'woocommerce' ), 'error' );
219
			$save_pass = false;
220
		} elseif ( ! empty( $pass1 ) && empty( $pass2 ) ) {
221
			wc_add_notice( __( 'Please re-enter your password.', 'woocommerce' ), 'error' );
222
			$save_pass = false;
223 View Code Duplication
		} elseif ( ( ! empty( $pass1 ) || ! empty( $pass2 ) ) && $pass1 !== $pass2 ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
224
			wc_add_notice( __( 'New passwords do not match.', 'woocommerce' ), 'error' );
225
			$save_pass = false;
226
		} elseif ( ! empty( $pass1 ) && ! wp_check_password( $pass_cur, $current_user->user_pass, $current_user->ID ) ) {
227
			wc_add_notice( __( 'Your current password is incorrect.', 'woocommerce' ), 'error' );
228
			$save_pass = false;
229
		}
230
231
		if ( $pass1 && $save_pass ) {
232
			$user->user_pass = $pass1;
233
		}
234
235
		// Allow plugins to return their own errors.
236
		do_action_ref_array( 'woocommerce_save_account_details_errors', array( &$errors, &$user ) );
237
238
		if ( $errors->get_error_messages() ) {
239
			foreach ( $errors->get_error_messages() as $error ) {
240
				wc_add_notice( $error, 'error' );
241
			}
242
		}
243
244
		if ( wc_notice_count( 'error' ) === 0 ) {
245
246
			wp_update_user( $user ) ;
247
248
			wc_add_notice( __( 'Account details changed successfully.', 'woocommerce' ) );
249
250
			do_action( 'woocommerce_save_account_details', $user->ID );
251
252
			wp_safe_redirect( wc_get_page_permalink( 'myaccount' ) );
253
			exit;
254
		}
255
	}
256
257
	/**
258
	 * Process the checkout form.
259
	 */
260
	public static function checkout_action() {
261
		if ( isset( $_POST['woocommerce_checkout_place_order'] ) || isset( $_POST['woocommerce_checkout_update_totals'] ) ) {
262
263
			if ( WC()->cart->is_empty() ) {
264
				wp_redirect( wc_get_page_permalink( 'cart' ) );
265
				exit;
266
			}
267
268
			if ( ! defined( 'WOOCOMMERCE_CHECKOUT' ) ) {
269
				define( 'WOOCOMMERCE_CHECKOUT', true );
270
			}
271
272
			WC()->checkout()->process_checkout();
273
		}
274
	}
275
276
	/**
277
	 * Process the pay form.
278
	 */
279
	public static function pay_action() {
280
		global $wp;
281
282
		if ( isset( $_POST['woocommerce_pay'] ) && isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'woocommerce-pay' ) ) {
283
284
			ob_start();
285
286
			// Pay for existing order
287
			$order_key  = $_GET['key'];
288
			$order_id   = absint( $wp->query_vars['order-pay'] );
289
			$order      = wc_get_order( $order_id );
290
291
			if ( $order->get_id() == $order_id && $order->get_order_key() == $order_key && $order->needs_payment() ) {
292
293
				do_action( 'woocommerce_before_pay_action', $order );
294
295
				WC()->customer->set_props( array(
296
					'billing_country'  => $order->get_billing_country() ? $order->get_billing_country()   : null,
297
					'billing_state'    => $order->get_billing_state() ? $order->get_billing_state()       : null,
298
					'billing_postcode' => $order->get_billing_postcode() ? $order->get_billing_postcode() : null,
299
					'billing_city'     => $order->get_billing_city() ? $order->get_billing_city()         : null,
300
				) );
301
				WC()->customer->save();
302
303
				// Terms
304
				if ( ! empty( $_POST['terms-field'] ) && empty( $_POST['terms'] ) ) {
305
					wc_add_notice( __( 'You must accept our Terms &amp; Conditions.', 'woocommerce' ), 'error' );
306
					return;
307
				}
308
309
				// Update payment method
310
				if ( $order->needs_payment() ) {
311
					$payment_method     = isset( $_POST['payment_method'] ) ? wc_clean( $_POST['payment_method'] ) : false;
312
					$available_gateways = WC()->payment_gateways->get_available_payment_gateways();
313
314
					if ( ! $payment_method ) {
315
						wc_add_notice( __( 'Invalid payment method.', 'woocommerce' ), 'error' );
316
						return;
317
					}
318
319
					// Update meta
320
					update_post_meta( $order_id, '_payment_method', $payment_method );
321
322
					if ( isset( $available_gateways[ $payment_method ] ) ) {
323
						$payment_method_title = $available_gateways[ $payment_method ]->get_title();
324
					} else {
325
						$payment_method_title = '';
326
					}
327
328
					update_post_meta( $order_id, '_payment_method_title', $payment_method_title );
329
330
					// Validate
331
					$available_gateways[ $payment_method ]->validate_fields();
332
333
					// Process
334
					if ( wc_notice_count( 'error' ) == 0 ) {
335
336
						$result = $available_gateways[ $payment_method ]->process_payment( $order_id );
337
338
						// Redirect to success/confirmation/payment page
339
						if ( 'success' === $result['result'] ) {
340
							wp_redirect( $result['redirect'] );
341
							exit;
342
						}
343
					}
344
345
				} else {
346
					// No payment was required for order
347
					$order->payment_complete();
348
					wp_safe_redirect( $order->get_checkout_order_received_url() );
349
					exit;
350
				}
351
352
				do_action( 'woocommerce_after_pay_action', $order );
353
354
			}
355
356
		}
357
	}
358
359
	/**
360
	 * Process the add payment method form.
361
	 */
362
	public static function add_payment_method_action() {
363
		if ( isset( $_POST['woocommerce_add_payment_method'], $_POST['payment_method'], $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'woocommerce-add-payment-method' ) ) {
364
365
			ob_start();
366
367
			$payment_method = wc_clean( $_POST['payment_method'] );
368
369
			$available_gateways = WC()->payment_gateways->get_available_payment_gateways();
370
			// Validate
371
			$available_gateways[ $payment_method ]->validate_fields();
372
373
			// Process
374
			if ( wc_notice_count( 'wc_errors' ) == 0 ) {
375
				$result = $available_gateways[ $payment_method ]->add_payment_method();
376
				// Redirect to success/confirmation/payment page
377
				if ( $result['result'] == 'success' ) {
378
					wc_add_notice( __( 'Payment method added.', 'woocommerce' ) );
379
					wp_redirect( $result['redirect'] );
380
					exit();
381
				}
382
383
			}
384
385
		}
386
387
	}
388
389
	/**
390
	 * Process the delete payment method form.
391
	 */
392 View Code Duplication
	public static function delete_payment_method_action() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
393
		global $wp;
394
395
		if ( isset( $wp->query_vars['delete-payment-method'] ) ) {
396
397
			$token_id = absint( $wp->query_vars['delete-payment-method'] );
398
			$token = WC_Payment_Tokens::get( $token_id );
399
			$delete = true;
400
401
			if ( is_null( $token ) ) {
402
				wc_add_notice( __( 'Invalid payment method', 'woocommerce' ), 'error' );
403
				$delete = false;
404
			}
405
406
			if ( get_current_user_id() !== $token->get_user_id() ) {
407
				wc_add_notice( __( 'Invalid payment method', 'woocommerce' ), 'error' );
408
				$delete = false;
409
			}
410
411
			if ( false === wp_verify_nonce( $_REQUEST['_wpnonce'], 'delete-payment-method-' . $token_id ) ) {
412
				wc_add_notice( __( 'Invalid payment method', 'woocommerce' ), 'error' );
413
				$delete = false;
414
			}
415
416
			if ( $delete ) {
417
				WC_Payment_Tokens::delete( $token_id );
418
				wc_add_notice( __( 'Payment method deleted.', 'woocommerce' ) );
419
			}
420
421
			wp_redirect( wc_get_account_endpoint_url( 'payment-methods' ) );
422
			exit();
423
		}
424
425
	}
426
427
	/**
428
	 * Process the delete payment method form.
429
	 */
430 View Code Duplication
	public static function set_default_payment_method_action() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
431
		global $wp;
432
433
		if ( isset( $wp->query_vars['set-default-payment-method'] ) ) {
434
435
			$token_id = absint( $wp->query_vars['set-default-payment-method'] );
436
			$token = WC_Payment_Tokens::get( $token_id );
437
			$delete = true;
438
439
			if ( is_null( $token ) ) {
440
				wc_add_notice( __( 'Invalid payment method', 'woocommerce' ), 'error' );
441
				$delete = false;
442
			}
443
444
			if ( get_current_user_id() !== $token->get_user_id() ) {
445
				wc_add_notice( __( 'Invalid payment method', 'woocommerce' ), 'error' );
446
				$delete = false;
447
			}
448
449
			if ( false === wp_verify_nonce( $_REQUEST['_wpnonce'], 'set-default-payment-method-' . $token_id ) ) {
450
				wc_add_notice( __( 'Invalid payment method', 'woocommerce' ), 'error' );
451
				$delete = false;
452
			}
453
454
			if ( $delete ) {
455
				WC_Payment_Tokens::set_users_default( $token->get_user_id(), intval( $token_id ) );
456
				wc_add_notice( __( 'This payment method was successfully set as your default.', 'woocommerce' ) );
457
			}
458
459
			wp_redirect( wc_get_account_endpoint_url( 'payment-methods' ) );
460
			exit();
461
		}
462
463
	}
464
465
	/**
466
	 * Remove from cart/update.
467
	 */
468
	public static function update_cart_action() {
469
470
		// Add Discount
471
		if ( ! empty( $_POST['apply_coupon'] ) && ! empty( $_POST['coupon_code'] ) ) {
472
			WC()->cart->add_discount( sanitize_text_field( $_POST['coupon_code'] ) );
473
		}
474
475
		// Remove Coupon Codes
476
		elseif ( isset( $_GET['remove_coupon'] ) ) {
477
			WC()->cart->remove_coupon( wc_clean( $_GET['remove_coupon'] ) );
478
		}
479
480
		// Remove from cart
481
		elseif ( ! empty( $_GET['remove_item'] ) && isset( $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'], 'woocommerce-cart' ) ) {
482
			$cart_item_key = sanitize_text_field( $_GET['remove_item'] );
483
484
			if ( $cart_item = WC()->cart->get_cart_item( $cart_item_key ) ) {
485
				WC()->cart->remove_cart_item( $cart_item_key );
486
487
				$product = wc_get_product( $cart_item['product_id'] );
488
489
				$item_removed_title = apply_filters( 'woocommerce_cart_item_removed_title', $product ? $product->get_title() : __( 'Item', 'woocommerce' ), $cart_item );
490
491
				// Don't show undo link if removed item is out of stock.
492
				if ( $product->is_in_stock() && $product->has_enough_stock( $cart_item['quantity'] ) ) {
493
					$removed_notice  = sprintf( __( '%s removed.', 'woocommerce' ), $item_removed_title );
494
					$removed_notice .= ' <a href="' . esc_url( WC()->cart->get_undo_url( $cart_item_key ) ) . '">' . __( 'Undo?', 'woocommerce' ) . '</a>';
495
				} else {
496
					$removed_notice = sprintf( __( '%s removed.', 'woocommerce' ), $item_removed_title );
497
				}
498
499
				wc_add_notice( $removed_notice );
500
			}
501
502
			$referer  = wp_get_referer() ? remove_query_arg( array( 'remove_item', 'add-to-cart', 'added-to-cart' ), add_query_arg( 'removed_item', '1', wp_get_referer() ) ) : wc_get_cart_url();
503
			wp_safe_redirect( $referer );
504
			exit;
505
		}
506
507
		// Undo Cart Item
508
		elseif ( ! empty( $_GET['undo_item'] ) && isset( $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'], 'woocommerce-cart' ) ) {
509
			$cart_item_key = sanitize_text_field( $_GET['undo_item'] );
510
511
			WC()->cart->restore_cart_item( $cart_item_key );
512
513
			$referer  = wp_get_referer() ? remove_query_arg( array( 'undo_item', '_wpnonce' ), wp_get_referer() ) : wc_get_cart_url();
514
			wp_safe_redirect( $referer );
515
			exit;
516
		}
517
518
		// Update Cart - checks apply_coupon too because they are in the same form
519
		if ( ( ! empty( $_POST['apply_coupon'] ) || ! empty( $_POST['update_cart'] ) || ! empty( $_POST['proceed'] ) ) && isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'woocommerce-cart' ) ) {
520
521
			$cart_updated = false;
522
			$cart_totals  = isset( $_POST['cart'] ) ? $_POST['cart'] : '';
523
524
			if ( ! WC()->cart->is_empty() && is_array( $cart_totals ) ) {
525
				foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
526
527
					$_product = $values['data'];
528
529
					// Skip product if no updated quantity was posted
530
					if ( ! isset( $cart_totals[ $cart_item_key ] ) || ! isset( $cart_totals[ $cart_item_key ]['qty'] ) ) {
531
						continue;
532
					}
533
534
					// Sanitize
535
					$quantity = apply_filters( 'woocommerce_stock_amount_cart_item', wc_stock_amount( preg_replace( "/[^0-9\.]/", '', $cart_totals[ $cart_item_key ]['qty'] ) ), $cart_item_key );
536
537
					if ( '' === $quantity || $quantity == $values['quantity'] )
538
						continue;
539
540
					// Update cart validation
541
					$passed_validation 	= apply_filters( 'woocommerce_update_cart_validation', true, $cart_item_key, $values, $quantity );
542
543
					// is_sold_individually
544
					if ( $_product->is_sold_individually() && $quantity > 1 ) {
545
						wc_add_notice( sprintf( __( 'You can only have 1 %s in your cart.', 'woocommerce' ), $_product->get_title() ), 'error' );
546
						$passed_validation = false;
547
					}
548
549
					if ( $passed_validation ) {
550
						WC()->cart->set_quantity( $cart_item_key, $quantity, false );
551
						$cart_updated = true;
552
					}
553
554
				}
555
			}
556
557
			// Trigger action - let 3rd parties update the cart if they need to and update the $cart_updated variable
558
			$cart_updated = apply_filters( 'woocommerce_update_cart_action_cart_updated', $cart_updated );
559
560
			if ( $cart_updated ) {
561
				// Recalc our totals
562
				WC()->cart->calculate_totals();
563
			}
564
565
			if ( ! empty( $_POST['proceed'] ) ) {
566
				wp_safe_redirect( wc_get_checkout_url() );
567
				exit;
568
			} elseif ( $cart_updated ) {
569
				wc_add_notice( __( 'Cart updated.', 'woocommerce' ) );
570
				$referer = remove_query_arg( 'remove_coupon', ( wp_get_referer() ? wp_get_referer() : wc_get_cart_url() ) );
571
				wp_safe_redirect( $referer );
572
				exit;
573
			}
574
		}
575
	}
576
577
	/**
578
	 * Place a previous order again.
579
	 */
580
	public static function order_again() {
581
582
		// Nothing to do
583
		if ( ! isset( $_GET['order_again'] ) || ! is_user_logged_in() || ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( $_GET['_wpnonce'], 'woocommerce-order_again' ) ) {
584
			return;
585
		}
586
587
		// Clear current cart
588
		WC()->cart->empty_cart();
589
590
		// Load the previous order - Stop if the order does not exist
591
		$order = wc_get_order( absint( $_GET['order_again'] ) );
592
593
		if ( ! $order->get_id() ) {
594
			return;
595
		}
596
597
		if ( ! $order->has_status( 'completed' ) ) {
598
			return;
599
		}
600
601
		// Make sure the user is allowed to order again. By default it check if the
602
		// previous order belonged to the current user.
603
		if ( ! current_user_can( 'order_again', $order->get_id() ) ) {
604
			return;
605
		}
606
607
		// Copy products from the order to the cart
608
		foreach ( $order->get_items() as $item ) {
609
			// Load all product info including variation data
610
			$product_id   = (int) apply_filters( 'woocommerce_add_to_cart_product_id', $item->get_product_id() );
611
			$quantity     = $item->get_quantity();
612
			$variation_id = $item->get_variation_id();
613
			$variations   = array();
614
			$cart_item_data = apply_filters( 'woocommerce_order_again_cart_item_data', array(), $item, $order );
615
616
			foreach ( $item->get_meta_data() as $meta ) {
617
				if ( taxonomy_is_product_attribute( $meta->meta_key ) ) {
618
					$variations[ $meta->meta_key ] = $meta->meta_value;
619
				} elseif ( meta_is_product_attribute( $meta->meta_key, $meta->meta_value, $product_id ) ) {
620
					$variations[ $meta->meta_key ] = $meta->meta_value;
621
				}
622
			}
623
624
			// Add to cart validation
625
			if ( ! apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity, $variation_id, $variations, $cart_item_data ) ) {
626
				continue;
627
			}
628
629
			WC()->cart->add_to_cart( $product_id, $quantity, $variation_id, $variations, $cart_item_data );
630
		}
631
632
		do_action( 'woocommerce_ordered_again', $order->get_id() );
633
634
		// Redirect to cart
635
		wc_add_notice( __( 'The cart has been filled with the items from your previous order.', 'woocommerce' ) );
636
		wp_safe_redirect( wc_get_cart_url() );
637
		exit;
638
	}
639
640
	/**
641
	 * Cancel a pending order.
642
	 */
643
	public static function cancel_order() {
644
		if ( isset( $_GET['cancel_order'] ) && isset( $_GET['order'] ) && isset( $_GET['order_id'] ) ) {
645
646
			$order_key        = $_GET['order'];
647
			$order_id         = absint( $_GET['order_id'] );
648
			$order            = wc_get_order( $order_id );
649
			$user_can_cancel  = current_user_can( 'cancel_order', $order_id );
650
			$order_can_cancel = $order->has_status( apply_filters( 'woocommerce_valid_order_statuses_for_cancel', array( 'pending', 'failed' ) ) );
651
			$redirect         = $_GET['redirect'];
652
653
			if ( $order->has_status( 'cancelled' ) ) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
654
				// Already cancelled - take no action
655
			} elseif ( $user_can_cancel && $order_can_cancel && $order->get_id() === $order_id && $order->get_order_key() === $order_key ) {
656
657
				// Cancel the order + restore stock
658
				WC()->session->set( 'order_awaiting_payment', false );
659
				$order->update_status( 'cancelled', __( 'Order cancelled by customer.', 'woocommerce' ) );
660
661
				// Message
662
				wc_add_notice( apply_filters( 'woocommerce_order_cancelled_notice', __( 'Your order was cancelled.', 'woocommerce' ) ), apply_filters( 'woocommerce_order_cancelled_notice_type', 'notice' ) );
663
664
				do_action( 'woocommerce_cancelled_order', $order->get_id() );
665
666
			} elseif ( $user_can_cancel && ! $order_can_cancel ) {
667
				wc_add_notice( __( 'Your order can no longer be cancelled. Please contact us if you need assistance.', 'woocommerce' ), 'error' );
668
			} else {
669
				wc_add_notice( __( 'Invalid order.', 'woocommerce' ), 'error' );
670
			}
671
672
			if ( $redirect ) {
673
				wp_safe_redirect( $redirect );
674
				exit;
675
			}
676
		}
677
	}
678
679
	/**
680
	 * Add to cart action.
681
	 *
682
	 * Checks for a valid request, does validation (via hooks) and then redirects if valid.
683
	 *
684
	 * @param bool $url (default: false)
685
	 */
686
	public static function add_to_cart_action( $url = false ) {
687
		if ( empty( $_REQUEST['add-to-cart'] ) || ! is_numeric( $_REQUEST['add-to-cart'] ) ) {
688
			return;
689
		}
690
691
		$product_id          = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $_REQUEST['add-to-cart'] ) );
692
		$was_added_to_cart   = false;
693
		$adding_to_cart      = wc_get_product( $product_id );
694
695
		if ( ! $adding_to_cart ) {
696
			return;
697
		}
698
699
		$add_to_cart_handler = apply_filters( 'woocommerce_add_to_cart_handler', $adding_to_cart->product_type, $adding_to_cart );
700
701
		// Variable product handling
702
		if ( 'variable' === $add_to_cart_handler ) {
703
			$was_added_to_cart = self::add_to_cart_handler_variable( $product_id );
704
705
		// Grouped Products
706
		} elseif ( 'grouped' === $add_to_cart_handler ) {
707
			$was_added_to_cart = self::add_to_cart_handler_grouped( $product_id );
708
709
		// Custom Handler
710
		} elseif ( has_action( 'woocommerce_add_to_cart_handler_' . $add_to_cart_handler ) ){
711
			do_action( 'woocommerce_add_to_cart_handler_' . $add_to_cart_handler, $url );
712
713
		// Simple Products
714
		} else {
715
			$was_added_to_cart = self::add_to_cart_handler_simple( $product_id );
716
		}
717
718
		// If we added the product to the cart we can now optionally do a redirect.
719
		if ( $was_added_to_cart && wc_notice_count( 'error' ) === 0 ) {
720
			// If has custom URL redirect there
721
			if ( $url = apply_filters( 'woocommerce_add_to_cart_redirect', $url ) ) {
722
				wp_safe_redirect( $url );
723
				exit;
724
			} elseif ( get_option( 'woocommerce_cart_redirect_after_add' ) === 'yes' ) {
725
				wp_safe_redirect( wc_get_cart_url() );
726
				exit;
727
			}
728
		}
729
	}
730
731
	/**
732
	 * Handle adding simple products to the cart.
733
	 * @since 2.4.6 Split from add_to_cart_action
734
	 * @param int $product_id
735
	 * @return bool success or not
736
	 */
737
	private static function add_to_cart_handler_simple( $product_id ) {
738
		$quantity 			= empty( $_REQUEST['quantity'] ) ? 1 : wc_stock_amount( $_REQUEST['quantity'] );
739
		$passed_validation 	= apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity );
740
741 View Code Duplication
		if ( $passed_validation && WC()->cart->add_to_cart( $product_id, $quantity ) !== false ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
742
			wc_add_to_cart_message( array( $product_id => $quantity ), true );
743
			return true;
744
		}
745
		return false;
746
	}
747
748
	/**
749
	 * Handle adding grouped products to the cart.
750
	 * @since 2.4.6 Split from add_to_cart_action
751
	 * @param int $product_id
752
	 * @return bool success or not
753
	 */
754
	private static function add_to_cart_handler_grouped( $product_id ) {
755
		$was_added_to_cart = false;
756
		$added_to_cart     = array();
757
758
		if ( ! empty( $_REQUEST['quantity'] ) && is_array( $_REQUEST['quantity'] ) ) {
759
			$quantity_set = false;
760
761
			foreach ( $_REQUEST['quantity'] as $item => $quantity ) {
762
				if ( $quantity <= 0 ) {
763
					continue;
764
				}
765
				$quantity_set = true;
766
767
				// Add to cart validation
768
				$passed_validation 	= apply_filters( 'woocommerce_add_to_cart_validation', true, $item, $quantity );
769
770
				if ( $passed_validation && WC()->cart->add_to_cart( $item, $quantity ) !== false ) {
771
					$was_added_to_cart = true;
772
					$added_to_cart[ $item ] = $quantity;
773
				}
774
			}
775
776
			if ( ! $was_added_to_cart && ! $quantity_set ) {
777
				wc_add_notice( __( 'Please choose the quantity of items you wish to add to your cart&hellip;', 'woocommerce' ), 'error' );
778
			} elseif ( $was_added_to_cart ) {
779
				wc_add_to_cart_message( $added_to_cart );
780
				return true;
781
			}
782
783
		} elseif ( $product_id ) {
784
			/* Link on product archives */
785
			wc_add_notice( __( 'Please choose a product to add to your cart&hellip;', 'woocommerce' ), 'error' );
786
		}
787
		return false;
788
	}
789
790
	/**
791
	 * Handle adding variable products to the cart.
792
	 * @since 2.4.6 Split from add_to_cart_action
793
	 * @param int $product_id
794
	 * @return bool success or not
795
	 */
796
	private static function add_to_cart_handler_variable( $product_id ) {
797
		$adding_to_cart     = wc_get_product( $product_id );
798
		$variation_id       = empty( $_REQUEST['variation_id'] ) ? '' : absint( $_REQUEST['variation_id'] );
799
		$quantity           = empty( $_REQUEST['quantity'] ) ? 1 : wc_stock_amount( $_REQUEST['quantity'] );
800
		$missing_attributes = array();
801
		$variations         = array();
802
		$attributes         = $adding_to_cart->get_attributes();
803
804
		// If no variation ID is set, attempt to get a variation ID from posted attributes.
805
		if ( empty( $variation_id ) ) {
806
			$variation_id = $adding_to_cart->get_matching_variation( wp_unslash( $_POST ) );
807
		}
808
809
		$variation = wc_get_product( $variation_id );
810
811
		// Verify all attributes
812
		foreach ( $attributes as $attribute ) {
813
			if ( ! $attribute['is_variation'] ) {
814
				continue;
815
			}
816
817
			$taxonomy = 'attribute_' . sanitize_title( $attribute['name'] );
818
819
			if ( isset( $_REQUEST[ $taxonomy ] ) ) {
820
821
				// Get value from post data
822
				if ( $attribute['is_taxonomy'] ) {
823
					// Don't use wc_clean as it destroys sanitized characters
824
					$value = sanitize_title( stripslashes( $_REQUEST[ $taxonomy ] ) );
825
				} else {
826
					$value = wc_clean( stripslashes( $_REQUEST[ $taxonomy ] ) );
827
				}
828
829
				// Get valid value from variation
830
				$valid_value = isset( $variation->variation_data[ $taxonomy ] ) ? $variation->variation_data[ $taxonomy ] : '';
831
832
				// Allow if valid
833
				if ( '' === $valid_value || $valid_value === $value ) {
834
					$variations[ $taxonomy ] = $value;
835
					continue;
836
				}
837
838
			} else {
839
				$missing_attributes[] = wc_attribute_label( $attribute['name'] );
840
			}
841
		}
842
843
		if ( ! empty( $missing_attributes ) ) {
844
			wc_add_notice( sprintf( _n( '%s is a required field', '%s are required fields', sizeof( $missing_attributes ), 'woocommerce' ), wc_format_list_of_items( $missing_attributes ) ), 'error' );
845
		} elseif ( empty( $variation_id ) ) {
846
			wc_add_notice( __( 'Please choose product options&hellip;', 'woocommerce' ), 'error' );
847
		} else {
848
			// Add to cart validation
849
			$passed_validation 	= apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity, $variation_id, $variations );
850
851 View Code Duplication
			if ( $passed_validation && WC()->cart->add_to_cart( $product_id, $quantity, $variation_id, $variations ) !== false ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
852
				wc_add_to_cart_message( array( $product_id => $quantity ), true );
853
				return true;
854
			}
855
		}
856
		return false;
857
	}
858
859
	/**
860
	 * Process the login form.
861
	 */
862
	public static function process_login() {
863
		$nonce_value = isset( $_POST['_wpnonce'] ) ? $_POST['_wpnonce'] : '';
864
		$nonce_value = isset( $_POST['woocommerce-login-nonce'] ) ? $_POST['woocommerce-login-nonce'] : $nonce_value;
865
866
		if ( ! empty( $_POST['login'] ) && wp_verify_nonce( $nonce_value, 'woocommerce-login' ) ) {
867
868
			try {
869
				$creds = array(
870
					'user_password' => $_POST['password'],
871
					'remember'      => isset( $_POST['rememberme'] ),
872
				);
873
874
				$username         = trim( $_POST['username'] );
875
				$validation_error = new WP_Error();
876
				$validation_error = apply_filters( 'woocommerce_process_login_errors', $validation_error, $_POST['username'], $_POST['password'] );
877
878
				if ( $validation_error->get_error_code() ) {
879
					throw new Exception( '<strong>' . __( 'Error', 'woocommerce' ) . ':</strong> ' . $validation_error->get_error_message() );
880
				}
881
882 View Code Duplication
				if ( empty( $username ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
883
					throw new Exception( '<strong>' . __( 'Error', 'woocommerce' ) . ':</strong> ' . __( 'Username is required.', 'woocommerce' ) );
884
				}
885
886 View Code Duplication
				if ( empty( $_POST['password'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
887
					throw new Exception( '<strong>' . __( 'Error', 'woocommerce' ) . ':</strong> ' . __( 'Password is required.', 'woocommerce' ) );
888
				}
889
890
				if ( is_email( $username ) && apply_filters( 'woocommerce_get_username_from_email', true ) ) {
891
					$user = get_user_by( 'email', $username );
892
893
					if ( isset( $user->user_login ) ) {
894
						$creds['user_login'] = $user->user_login;
895
					} else {
896
						throw new Exception( '<strong>' . __( 'Error', 'woocommerce' ) . ':</strong> ' . __( 'A user could not be found with this email address.', 'woocommerce' ) );
897
					}
898
899
				} else {
900
					$creds['user_login'] = $username;
901
				}
902
903
				// On multisite, ensure user exists on current site, if not add them before allowing login.
904
				if ( is_multisite() ) {
905
					$user_data = get_user_by( 'login', $username );
906
907
					if ( $user_data && ! is_user_member_of_blog( $user_data->ID, get_current_blog_id() ) ) {
908
						add_user_to_blog( get_current_blog_id(), $user_data->ID, 'customer' );
909
					}
910
				}
911
912
				// Perform the login
913
				$user = wp_signon( apply_filters( 'woocommerce_login_credentials', $creds ), is_ssl() );
914
915
				if ( is_wp_error( $user ) ) {
916
					$message = $user->get_error_message();
917
					$message = str_replace( '<strong>' . esc_html( $creds['user_login'] ) . '</strong>', '<strong>' . esc_html( $username ) . '</strong>', $message );
918
					throw new Exception( $message );
919
				} else {
920
921
					if ( ! empty( $_POST['redirect'] ) ) {
922
						$redirect = $_POST['redirect'];
923
					} elseif ( wp_get_referer() ) {
924
						$redirect = wp_get_referer();
925
					} else {
926
						$redirect = wc_get_page_permalink( 'myaccount' );
927
					}
928
929
					wp_redirect( apply_filters( 'woocommerce_login_redirect', $redirect, $user ) );
930
					exit;
931
				}
932
933
			} catch ( Exception $e ) {
934
				wc_add_notice( apply_filters('login_errors', $e->getMessage() ), 'error' );
935
			}
936
		}
937
	}
938
939
	/**
940
	 * Handle lost password form.
941
	 */
942
	public static function process_lost_password() {
943
		if ( isset( $_POST['wc_reset_password'] ) && isset( $_POST['user_login'] ) && isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'lost_password' ) ) {
944
			$success = WC_Shortcode_My_Account::retrieve_password();
945
946
			// If successful, redirect to my account with query arg set
947
			if ( $success ) {
948
				wp_redirect( add_query_arg( 'reset-link-sent', 'true', remove_query_arg( array( 'key', 'login', 'reset' ) ) ) );
949
				exit;
950
			}
951
		}
952
	}
953
954
	/**
955
	 * Handle reset password form.
956
	 */
957
	public static function process_reset_password() {
958
		$posted_fields = array( 'wc_reset_password', 'password_1', 'password_2', 'reset_key', 'reset_login', '_wpnonce' );
959
960
		foreach ( $posted_fields as $field ) {
961
			if ( ! isset( $_POST[ $field ] ) ) {
962
				return;
963
			}
964
			$posted_fields[ $field ] = $_POST[ $field ];
965
		}
966
967
		if ( ! wp_verify_nonce( $posted_fields['_wpnonce'], 'reset_password' ) ) {
968
			return;
969
		}
970
971
		$user = WC_Shortcode_My_Account::check_password_reset_key( $posted_fields['reset_key'], $posted_fields['reset_login'] );
972
973
		if ( $user instanceof WP_User ) {
974
			if ( empty( $posted_fields['password_1'] ) ) {
975
				wc_add_notice( __( 'Please enter your password.', 'woocommerce' ), 'error' );
976
			}
977
978
			if ( $posted_fields[ 'password_1' ] !== $posted_fields[ 'password_2' ] ) {
979
				wc_add_notice( __( 'Passwords do not match.', 'woocommerce' ), 'error' );
980
			}
981
982
			$errors = new WP_Error();
983
984
			do_action( 'validate_password_reset', $errors, $user );
985
986
			wc_add_wp_error_notices( $errors );
987
988
			if ( 0 === wc_notice_count( 'error' ) ) {
989
				WC_Shortcode_My_Account::reset_password( $user, $posted_fields['password_1'] );
990
991
				do_action( 'woocommerce_customer_reset_password', $user );
992
993
				wp_redirect( add_query_arg( 'password-reset', 'true', wc_get_page_permalink( 'myaccount' ) ) );
994
				exit;
995
			}
996
		}
997
	}
998
999
	/**
1000
	 * Process the registration form.
1001
	 */
1002
	public static function process_registration() {
1003
		$nonce_value = isset( $_POST['_wpnonce'] ) ? $_POST['_wpnonce'] : '';
1004
		$nonce_value = isset( $_POST['woocommerce-register-nonce'] ) ? $_POST['woocommerce-register-nonce'] : $nonce_value;
1005
1006
		if ( ! empty( $_POST['register'] ) && wp_verify_nonce( $nonce_value, 'woocommerce-register' ) ) {
1007
			$username = 'no' === get_option( 'woocommerce_registration_generate_username' ) ? $_POST['username'] : '';
1008
			$password = 'no' === get_option( 'woocommerce_registration_generate_password' ) ? $_POST['password'] : '';
1009
			$email    = $_POST['email'];
1010
1011
			try {
1012
				$validation_error = new WP_Error();
1013
				$validation_error = apply_filters( 'woocommerce_process_registration_errors', $validation_error, $username, $password, $email );
1014
1015
				if ( $validation_error->get_error_code() ) {
1016
					throw new Exception( $validation_error->get_error_message() );
1017
				}
1018
1019
				// Anti-spam trap
1020
				if ( ! empty( $_POST['email_2'] ) ) {
1021
					throw new Exception( __( 'Anti-spam field was filled in.', 'woocommerce' ) );
1022
				}
1023
1024
				$new_customer = wc_create_new_customer( sanitize_email( $email ), wc_clean( $username ), $password );
1025
1026
				if ( is_wp_error( $new_customer ) ) {
1027
					throw new Exception( $new_customer->get_error_message() );
1028
				}
1029
1030
				if ( apply_filters( 'woocommerce_registration_auth_new_customer', true, $new_customer ) ) {
1031
					wc_set_customer_auth_cookie( $new_customer );
1032
				}
1033
1034
				wp_safe_redirect( apply_filters( 'woocommerce_registration_redirect', wp_get_referer() ? wp_get_referer() : wc_get_page_permalink( 'myaccount' ) ) );
1035
				exit;
1036
1037
			} catch ( Exception $e ) {
1038
				wc_add_notice( '<strong>' . __( 'Error', 'woocommerce' ) . ':</strong> ' . $e->getMessage(), 'error' );
1039
			}
1040
		}
1041
	}
1042
}
1043
1044
WC_Form_Handler::init();
1045