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0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / includes / class-wc-form-handler.php
woocommerce / includes Last commit date
abstracts 1 year ago admin 1 year ago blocks 1 year ago cli 1 year ago customizer 2 years ago data-stores 1 year ago emails 1 year ago export 1 year ago gateways 1 year ago import 1 year ago integrations 2 years ago interfaces 1 year ago legacy 1 year ago libraries 1 year ago log-handlers 2 years ago payment-tokens 5 years ago product-usage 1 year ago queue 4 years ago react-admin 1 year ago rest-api 1 year ago shipping 1 year ago shortcodes 1 year ago theme-support 2 years ago tracks 1 year ago traits 5 years ago walkers 5 years ago wccom-site 1 year ago widgets 1 year ago class-wc-ajax.php 1 year ago class-wc-auth.php 1 year ago class-wc-autoloader.php 1 year ago class-wc-background-emailer.php 5 years ago class-wc-background-updater.php 5 years ago class-wc-brands-brand-settings-manager.php 1 year ago class-wc-brands-coupons.php 1 year ago class-wc-brands.php 1 year ago class-wc-breadcrumb.php 5 years ago class-wc-cache-helper.php 1 year ago class-wc-cart-fees.php 2 years ago class-wc-cart-session.php 1 year ago class-wc-cart-totals.php 2 years ago class-wc-cart.php 1 year ago class-wc-checkout.php 1 year ago class-wc-cli.php 1 year ago class-wc-comments.php 3 years ago class-wc-countries.php 1 year ago class-wc-coupon.php 1 year ago class-wc-customer-download-log.php 5 years ago class-wc-customer-download.php 1 year ago class-wc-customer.php 2 years ago class-wc-data-exception.php 8 years ago class-wc-data-store.php 3 years ago class-wc-datetime.php 4 years ago class-wc-deprecated-action-hooks.php 2 years ago class-wc-deprecated-filter-hooks.php 3 years ago class-wc-discounts.php 1 year ago class-wc-download-handler.php 1 year ago class-wc-emails.php 1 year ago class-wc-embed.php 1 year ago class-wc-form-handler.php 1 year ago class-wc-frontend-scripts.php 1 year ago class-wc-geo-ip.php 1 year ago class-wc-geolite-integration.php 6 years ago class-wc-geolocation.php 1 year ago class-wc-https.php 2 years ago class-wc-install.php 1 year ago class-wc-integrations.php 5 years ago class-wc-log-levels.php 2 years ago class-wc-logger.php 1 year ago class-wc-meta-data.php 4 years ago class-wc-order-factory.php 2 years ago class-wc-order-item-coupon.php 4 years ago class-wc-order-item-fee.php 1 year ago class-wc-order-item-meta.php 4 years ago class-wc-order-item-product.php 1 year ago class-wc-order-item-shipping.php 1 year ago class-wc-order-item-tax.php 4 years ago class-wc-order-item.php 1 year ago class-wc-order-query.php 4 years ago class-wc-order-refund.php 1 year ago class-wc-order.php 1 year ago class-wc-payment-gateways.php 1 year ago class-wc-payment-tokens.php 3 years ago class-wc-post-data.php 1 year ago class-wc-post-types.php 1 year ago class-wc-privacy-background-process.php 5 years ago class-wc-privacy-erasers.php 1 year ago class-wc-privacy-exporters.php 4 years ago class-wc-privacy.php 1 year ago class-wc-product-attribute.php 4 years ago class-wc-product-download.php 2 years ago class-wc-product-external.php 1 year ago class-wc-product-factory.php 1 year ago class-wc-product-grouped.php 1 year ago class-wc-product-query.php 1 year ago class-wc-product-simple.php 1 year ago class-wc-product-variable.php 1 year ago class-wc-product-variation.php 1 year ago class-wc-query.php 1 year ago class-wc-rate-limiter.php 4 years ago class-wc-regenerate-images-request.php 3 years ago class-wc-regenerate-images.php 1 year ago class-wc-register-wp-admin-settings.php 4 years ago class-wc-rest-authentication.php 1 year ago class-wc-rest-exception.php 5 years ago class-wc-session-handler.php 2 years ago class-wc-shipping-rate.php 1 year ago class-wc-shipping-zone.php 5 years ago class-wc-shipping-zones.php 5 years ago class-wc-shipping.php 4 years ago class-wc-shortcodes.php 1 year ago class-wc-structured-data.php 1 year ago class-wc-tax.php 2 years ago class-wc-template-loader.php 2 years ago class-wc-tracker.php 1 year ago class-wc-validation.php 2 years ago class-wc-webhook.php 1 year ago class-woocommerce.php 1 year ago wc-account-functions.php 1 year ago wc-attribute-functions.php 1 year ago wc-brands-functions.php 1 year ago wc-cart-functions.php 1 year ago wc-conditional-functions.php 1 year ago wc-core-functions.php 1 year ago wc-coupon-functions.php 3 years ago wc-deprecated-functions.php 2 years ago wc-formatting-functions.php 1 year ago wc-notice-functions.php 2 years ago wc-order-functions.php 1 year ago wc-order-item-functions.php 3 years ago wc-page-functions.php 1 year ago wc-product-functions.php 1 year ago wc-rest-functions.php 1 year ago wc-stock-functions.php 1 year ago wc-template-functions.php 1 year ago wc-template-hooks.php 1 year ago wc-term-functions.php 3 years ago wc-update-functions.php 1 year ago wc-user-functions.php 1 year ago wc-webhook-functions.php 1 year ago wc-widget-functions.php 5 years ago
class-wc-form-handler.php
1168 lines
1 <?php
2 /**
3 * Handle frontend forms.
4 *
5 * @package WooCommerce\Classes\
6 */
7
8 use Automattic\WooCommerce\Enums\OrderStatus;
9 use Automattic\WooCommerce\Enums\ProductType;
10
11 defined( 'ABSPATH' ) || exit;
12
13 /**
14 * WC_Form_Handler class.
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__, 'update_cart_action' ), 20 );
32 add_action( 'wp_loaded', array( __CLASS__, 'add_to_cart_action' ), 20 );
33
34 // May need $wp global to access query vars.
35 add_action( 'wp', array( __CLASS__, 'pay_action' ), 20 );
36 add_action( 'wp', array( __CLASS__, 'add_payment_method_action' ), 20 );
37 add_action( 'wp', array( __CLASS__, 'delete_payment_method_action' ), 20 );
38 add_action( 'wp', array( __CLASS__, 'set_default_payment_method_action' ), 20 );
39 }
40
41 /**
42 * Remove key and user ID (or user login, as a fallback) from query string, set cookie, and redirect to account page to show the form.
43 */
44 public static function redirect_reset_password_link() {
45 if ( is_account_page() && isset( $_GET['key'] ) && ( isset( $_GET['id'] ) || isset( $_GET['login'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
46
47 // If available, get $user_id from query string parameter for fallback purposes.
48 if ( isset( $_GET['login'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
49 $user = get_user_by( 'login', sanitize_user( wp_unslash( $_GET['login'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
50 $user_id = $user ? $user->ID : 0;
51 } else {
52 $user_id = absint( $_GET['id'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
53 }
54
55 // If the reset token is not for the current user, ignore the reset request (don't redirect).
56 $logged_in_user_id = get_current_user_id();
57 if ( $logged_in_user_id && $logged_in_user_id !== $user_id ) {
58 wc_add_notice( __( 'This password reset key is for a different user account. Please log out and try again.', 'woocommerce' ), 'error' );
59 return;
60 }
61
62 $action = isset( $_GET['action'] ) ? sanitize_text_field( wp_unslash( $_GET['action'] ) ) : '';
63 $value = sprintf( '%d:%s', $user_id, wp_unslash( $_GET['key'] ) ); // phpcs:ignore
64 WC_Shortcode_My_Account::set_reset_password_cookie( $value );
65 wp_safe_redirect(
66 add_query_arg(
67 array(
68 'show-reset-form' => 'true',
69 'action' => $action,
70 ),
71 wc_lostpassword_url()
72 )
73 );
74 exit;
75 }
76 }
77
78 /**
79 * Save and and update a billing or shipping address if the
80 * form was submitted through the user account page.
81 */
82 public static function save_address() {
83 global $wp;
84
85 $nonce_value = wc_get_var( $_REQUEST['woocommerce-edit-address-nonce'], wc_get_var( $_REQUEST['_wpnonce'], '' ) ); // @codingStandardsIgnoreLine.
86
87 if ( ! wp_verify_nonce( $nonce_value, 'woocommerce-edit_address' ) ) {
88 return;
89 }
90
91 if ( empty( $_POST['action'] ) || 'edit_address' !== $_POST['action'] ) {
92 return;
93 }
94
95 wc_nocache_headers();
96
97 $user_id = get_current_user_id();
98
99 if ( $user_id <= 0 ) {
100 return;
101 }
102
103 $customer = new WC_Customer( $user_id );
104
105 if ( ! $customer ) {
106 return;
107 }
108
109 $address_type = isset( $wp->query_vars['edit-address'] ) ? wc_edit_address_i18n( sanitize_title( $wp->query_vars['edit-address'] ), true ) : 'billing';
110
111 if ( ! isset( $_POST[ $address_type . '_country' ] ) ) {
112 return;
113 }
114
115 $address = WC()->countries->get_address_fields( wc_clean( wp_unslash( $_POST[ $address_type . '_country' ] ) ), $address_type . '_' );
116
117 foreach ( $address as $key => $field ) {
118 if ( ! isset( $field['type'] ) ) {
119 $field['type'] = 'text';
120 }
121
122 // Get Value.
123 if ( 'checkbox' === $field['type'] ) {
124 $value = (int) isset( $_POST[ $key ] );
125 } else {
126 $value = isset( $_POST[ $key ] ) ? wc_clean( wp_unslash( $_POST[ $key ] ) ) : '';
127 }
128
129 // Hook to allow modification of value.
130 $value = apply_filters( 'woocommerce_process_myaccount_field_' . $key, $value );
131
132 // Validation: Required fields.
133 if ( ! empty( $field['required'] ) && empty( $value ) ) {
134 /* translators: %s: Field name. */
135 wc_add_notice( sprintf( __( '%s is a required field.', 'woocommerce' ), $field['label'] ), 'error', array( 'id' => $key ) );
136 }
137
138 if ( ! empty( $value ) ) {
139 // Validation and formatting rules.
140 if ( ! empty( $field['validate'] ) && is_array( $field['validate'] ) ) {
141 foreach ( $field['validate'] as $rule ) {
142 switch ( $rule ) {
143 case 'postcode':
144 $country = wc_clean( wp_unslash( $_POST[ $address_type . '_country' ] ) );
145 $value = wc_format_postcode( $value, $country );
146
147 if ( '' !== $value && ! WC_Validation::is_postcode( $value, $country ) ) {
148 switch ( $country ) {
149 case 'IE':
150 $postcode_validation_notice = __( 'Please enter a valid Eircode.', 'woocommerce' );
151 break;
152 default:
153 $postcode_validation_notice = __( 'Please enter a valid postcode / ZIP.', 'woocommerce' );
154 }
155 wc_add_notice( $postcode_validation_notice, 'error' );
156 }
157 break;
158 case 'phone':
159 if ( '' !== $value && ! WC_Validation::is_phone( $value ) ) {
160 /* translators: %s: Phone number. */
161 wc_add_notice( sprintf( __( '%s is not a valid phone number.', 'woocommerce' ), '<strong>' . $field['label'] . '</strong>' ), 'error' );
162 }
163 break;
164 case 'email':
165 $value = strtolower( $value );
166
167 if ( ! is_email( $value ) ) {
168 /* translators: %s: Email address. */
169 wc_add_notice( sprintf( __( '%s is not a valid email address.', 'woocommerce' ), '<strong>' . $field['label'] . '</strong>' ), 'error' );
170 }
171 break;
172 }
173 }
174 }
175 }
176
177 try {
178 // Set prop in customer object.
179 if ( is_callable( array( $customer, "set_$key" ) ) ) {
180 $customer->{"set_$key"}( $value );
181 } else {
182 $customer->update_meta_data( $key, $value );
183 }
184 } catch ( WC_Data_Exception $e ) {
185 // Set notices. Ignore invalid billing email, since is already validated.
186 if ( 'customer_invalid_billing_email' !== $e->getErrorCode() ) {
187 wc_add_notice( $e->getMessage(), 'error' );
188 }
189 }
190 }
191
192 /**
193 * Hook: woocommerce_after_save_address_validation.
194 *
195 * Allow developers to add custom validation logic and throw an error to prevent save.
196 *
197 * @since 3.6.0
198 * @param int $user_id User ID being saved.
199 * @param string $address_type Type of address; 'billing' or 'shipping'.
200 * @param array $address The address fields.
201 * @param WC_Customer $customer The customer object being saved.
202 */
203 do_action( 'woocommerce_after_save_address_validation', $user_id, $address_type, $address, $customer );
204
205 if ( 0 < wc_notice_count( 'error' ) ) {
206 return;
207 }
208
209 $customer->save();
210
211 wc_add_notice( __( 'Address changed successfully.', 'woocommerce' ) );
212
213 /**
214 * Hook: woocommerce_customer_save_address.
215 *
216 * Fires after a customer address has been saved.
217 *
218 * @since 3.6.0
219 * @param int $user_id User ID being saved.
220 * @param string $address_type Type of address; 'billing' or 'shipping'.
221 */
222 do_action( 'woocommerce_customer_save_address', $user_id, $address_type );
223
224 wp_safe_redirect( wc_get_endpoint_url( 'edit-address', '', wc_get_page_permalink( 'myaccount' ) ) );
225 exit;
226 }
227
228 /**
229 * Save the password/account details and redirect back to the my account page.
230 */
231 public static function save_account_details() {
232 $nonce_value = wc_get_var( $_REQUEST['save-account-details-nonce'], wc_get_var( $_REQUEST['_wpnonce'], '' ) ); // @codingStandardsIgnoreLine.
233
234 if ( ! wp_verify_nonce( $nonce_value, 'save_account_details' ) ) {
235 return;
236 }
237
238 if ( empty( $_POST['action'] ) || 'save_account_details' !== $_POST['action'] ) {
239 return;
240 }
241
242 wc_nocache_headers();
243
244 $user_id = get_current_user_id();
245
246 if ( $user_id <= 0 ) {
247 return;
248 }
249
250 $account_first_name = ! empty( $_POST['account_first_name'] ) ? wc_clean( wp_unslash( $_POST['account_first_name'] ) ) : '';
251 $account_last_name = ! empty( $_POST['account_last_name'] ) ? wc_clean( wp_unslash( $_POST['account_last_name'] ) ) : '';
252 $account_display_name = ! empty( $_POST['account_display_name'] ) ? wc_clean( wp_unslash( $_POST['account_display_name'] ) ) : '';
253 $account_email = ! empty( $_POST['account_email'] ) ? wc_clean( wp_unslash( $_POST['account_email'] ) ) : '';
254 $pass_cur = ! empty( $_POST['password_current'] ) ? $_POST['password_current'] : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash
255 $pass1 = ! empty( $_POST['password_1'] ) ? $_POST['password_1'] : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash
256 $pass2 = ! empty( $_POST['password_2'] ) ? $_POST['password_2'] : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash
257 $save_pass = true;
258
259 // Current user data.
260 $current_user = get_user_by( 'id', $user_id );
261 $current_first_name = $current_user->first_name;
262 $current_last_name = $current_user->last_name;
263 $current_email = $current_user->user_email;
264
265 // New user data.
266 $user = new stdClass();
267 $user->ID = $user_id;
268 $user->first_name = $account_first_name;
269 $user->last_name = $account_last_name;
270 $user->display_name = $account_display_name;
271
272 // Prevent display name to be changed to email.
273 if ( is_email( $account_display_name ) ) {
274 wc_add_notice( __( 'Display name cannot be changed to email address due to privacy concern.', 'woocommerce' ), 'error' );
275 }
276
277 // Handle required fields.
278 $required_fields = apply_filters(
279 'woocommerce_save_account_details_required_fields',
280 array(
281 'account_first_name' => __( 'First name', 'woocommerce' ),
282 'account_last_name' => __( 'Last name', 'woocommerce' ),
283 'account_display_name' => __( 'Display name', 'woocommerce' ),
284 'account_email' => __( 'Email address', 'woocommerce' ),
285 )
286 );
287
288 foreach ( $required_fields as $field_key => $field_name ) {
289 if ( empty( $_POST[ $field_key ] ) ) {
290 /* translators: %s: Field name. */
291 wc_add_notice( sprintf( __( '%s is a required field.', 'woocommerce' ), '<strong>' . esc_html( $field_name ) . '</strong>' ), 'error', array( 'id' => $field_key ) );
292 }
293 }
294
295 if ( $account_email ) {
296 $account_email = sanitize_email( $account_email );
297 if ( ! is_email( $account_email ) ) {
298 wc_add_notice( __( 'Please provide a valid email address.', 'woocommerce' ), 'error' );
299 } elseif ( email_exists( $account_email ) && $account_email !== $current_user->user_email ) {
300 wc_add_notice( __( 'This email address is already registered.', 'woocommerce' ), 'error' );
301 }
302 $user->user_email = $account_email;
303 }
304
305 if ( ! empty( $pass_cur ) && empty( $pass1 ) && empty( $pass2 ) ) {
306 wc_add_notice( __( 'Please fill out all password fields.', 'woocommerce' ), 'error' );
307 $save_pass = false;
308 } elseif ( ! empty( $pass1 ) && empty( $pass_cur ) ) {
309 wc_add_notice( __( 'Please enter your current password.', 'woocommerce' ), 'error' );
310 $save_pass = false;
311 } elseif ( ! empty( $pass1 ) && empty( $pass2 ) ) {
312 wc_add_notice( __( 'Please re-enter your password.', 'woocommerce' ), 'error' );
313 $save_pass = false;
314 } elseif ( ( ! empty( $pass1 ) || ! empty( $pass2 ) ) && $pass1 !== $pass2 ) {
315 wc_add_notice( __( 'New passwords do not match.', 'woocommerce' ), 'error' );
316 $save_pass = false;
317 } elseif ( ! empty( $pass1 ) && ! wp_check_password( $pass_cur, $current_user->user_pass, $current_user->ID ) ) {
318 wc_add_notice( __( 'Your current password is incorrect.', 'woocommerce' ), 'error' );
319 $save_pass = false;
320 }
321
322 if ( $pass1 && $save_pass ) {
323 $user->user_pass = $pass1;
324 }
325
326 // Allow plugins to return their own errors.
327 $errors = new WP_Error();
328 do_action_ref_array( 'woocommerce_save_account_details_errors', array( &$errors, &$user ) );
329
330 if ( $errors->get_error_messages() ) {
331 foreach ( $errors->get_error_messages() as $error ) {
332 wc_add_notice( $error, 'error' );
333 }
334 }
335
336 if ( wc_notice_count( 'error' ) === 0 ) {
337 wp_update_user( $user );
338
339 // Update customer object to keep data in sync.
340 $customer = new WC_Customer( $user->ID );
341
342 if ( $customer ) {
343 // Keep billing data in sync if data changed.
344 if ( isset( $user->user_email ) && is_email( $user->user_email ) && $current_email !== $user->user_email ) {
345 $customer->set_billing_email( $user->user_email );
346 }
347
348 if ( $current_first_name !== $user->first_name ) {
349 $customer->set_billing_first_name( $user->first_name );
350 }
351
352 if ( $current_last_name !== $user->last_name ) {
353 $customer->set_billing_last_name( $user->last_name );
354 }
355
356 $customer->save();
357 }
358
359 /**
360 * Hook: woocommerce_save_account_details.
361 *
362 * @since 3.6.0
363 * @param int $user_id User ID being saved.
364 */
365 do_action( 'woocommerce_save_account_details', $user->ID );
366
367 // Notices are checked here so that if something created a notice during the save hooks above, the redirect will not happen.
368 if ( 0 === wc_notice_count( 'error' ) ) {
369 wc_add_notice( __( 'Account details changed successfully.', 'woocommerce' ) );
370 wp_safe_redirect( wc_get_endpoint_url( 'edit-account', '', wc_get_page_permalink( 'myaccount' ) ) );
371 exit;
372 }
373 }
374 }
375
376 /**
377 * Process the checkout form.
378 */
379 public static function checkout_action() {
380 if ( isset( $_POST['woocommerce_checkout_place_order'] ) || isset( $_POST['woocommerce_checkout_update_totals'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
381 wc_nocache_headers();
382
383 if ( WC()->cart->is_empty() ) {
384 wp_safe_redirect( wc_get_cart_url() );
385 exit;
386 }
387
388 wc_maybe_define_constant( 'WOOCOMMERCE_CHECKOUT', true );
389
390 WC()->checkout()->process_checkout();
391 }
392 }
393
394 /**
395 * Process the pay form.
396 *
397 * @throws Exception On payment error.
398 */
399 public static function pay_action() {
400 global $wp;
401
402 if ( isset( $_POST['woocommerce_pay'], $_GET['key'] ) ) {
403 wc_nocache_headers();
404
405 $nonce_value = wc_get_var( $_REQUEST['woocommerce-pay-nonce'], wc_get_var( $_REQUEST['_wpnonce'], '' ) ); // @codingStandardsIgnoreLine.
406
407 if ( ! wp_verify_nonce( $nonce_value, 'woocommerce-pay' ) ) {
408 return;
409 }
410
411 ob_start();
412
413 // Pay for existing order.
414 $order_key = wp_unslash( $_GET['key'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
415 $order_id = absint( $wp->query_vars['order-pay'] );
416 $order = wc_get_order( $order_id );
417
418 if ( $order_id === $order->get_id() && hash_equals( $order->get_order_key(), $order_key ) && $order->needs_payment() ) {
419
420 do_action( 'woocommerce_before_pay_action', $order );
421
422 WC()->customer->set_props(
423 array(
424 'billing_country' => $order->get_billing_country() ? $order->get_billing_country() : null,
425 'billing_state' => $order->get_billing_state() ? $order->get_billing_state() : null,
426 'billing_postcode' => $order->get_billing_postcode() ? $order->get_billing_postcode() : null,
427 'billing_city' => $order->get_billing_city() ? $order->get_billing_city() : null,
428 )
429 );
430 WC()->customer->save();
431
432 if ( ! empty( $_POST['terms-field'] ) && empty( $_POST['terms'] ) ) {
433 wc_add_notice( __( 'Please read and accept the terms and conditions to proceed with your order.', 'woocommerce' ), 'error' );
434 return;
435 }
436
437 // Update payment method.
438 if ( $order->needs_payment() ) {
439 try {
440 $payment_method_id = isset( $_POST['payment_method'] ) ? wc_clean( wp_unslash( $_POST['payment_method'] ) ) : false;
441
442 if ( ! $payment_method_id ) {
443 throw new Exception( __( 'Invalid payment method.', 'woocommerce' ) );
444 }
445
446 $available_gateways = WC()->payment_gateways->get_available_payment_gateways();
447 $payment_method = isset( $available_gateways[ $payment_method_id ] ) ? $available_gateways[ $payment_method_id ] : false;
448
449 if ( ! $payment_method ) {
450 throw new Exception( __( 'Invalid payment method.', 'woocommerce' ) );
451 }
452
453 $order->set_payment_method( $payment_method );
454 $order->save();
455
456 $payment_method->validate_fields();
457
458 if ( 0 === wc_notice_count( 'error' ) ) {
459
460 $result = $payment_method->process_payment( $order_id );
461
462 // Redirect to success/confirmation/payment page.
463 if ( isset( $result['result'] ) && 'success' === $result['result'] ) {
464 $result['order_id'] = $order_id;
465
466 $result = apply_filters( 'woocommerce_payment_successful_result', $result, $order_id );
467
468 wp_redirect( $result['redirect'] ); //phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect
469 exit;
470 }
471 }
472 } catch ( Exception $e ) {
473 wc_add_notice( $e->getMessage(), 'error' );
474 }
475 } else {
476 // No payment was required for order.
477 $order->payment_complete();
478 wp_safe_redirect( $order->get_checkout_order_received_url() );
479 exit;
480 }
481
482 do_action( 'woocommerce_after_pay_action', $order );
483
484 }
485 }
486 }
487
488 /**
489 * Process the add payment method form.
490 */
491 public static function add_payment_method_action() {
492 if ( isset( $_POST['woocommerce_add_payment_method'], $_POST['payment_method'] ) ) {
493 wc_nocache_headers();
494
495 $nonce_value = wc_get_var( $_REQUEST['woocommerce-add-payment-method-nonce'], wc_get_var( $_REQUEST['_wpnonce'], '' ) ); // @codingStandardsIgnoreLine.
496
497 if ( ! wp_verify_nonce( $nonce_value, 'woocommerce-add-payment-method' ) ) {
498 return;
499 }
500
501 if ( ! apply_filters( 'woocommerce_add_payment_method_form_is_valid', true ) ) {
502 return;
503 }
504
505 // Test rate limit.
506 $current_user_id = get_current_user_id();
507 $rate_limit_id = 'add_payment_method_' . $current_user_id;
508 $delay = (int) apply_filters( 'woocommerce_payment_gateway_add_payment_method_delay', 20 );
509
510 if ( WC_Rate_Limiter::retried_too_soon( $rate_limit_id ) ) {
511 wc_add_notice(
512 sprintf(
513 /* translators: %d number of seconds */
514 _n(
515 'You cannot add a new payment method so soon after the previous one. Please wait for %d second.',
516 'You cannot add a new payment method so soon after the previous one. Please wait for %d seconds.',
517 $delay,
518 'woocommerce'
519 ),
520 $delay
521 ),
522 'error'
523 );
524 return;
525 }
526
527 WC_Rate_Limiter::set_rate_limit( $rate_limit_id, $delay );
528
529 ob_start();
530
531 $payment_method_id = wc_clean( wp_unslash( $_POST['payment_method'] ) );
532 $available_gateways = WC()->payment_gateways->get_available_payment_gateways();
533
534 if ( isset( $available_gateways[ $payment_method_id ] ) ) {
535 $gateway = $available_gateways[ $payment_method_id ];
536
537 if ( ! $gateway->supports( 'add_payment_method' ) && ! $gateway->supports( 'tokenization' ) ) {
538 wc_add_notice( __( 'Invalid payment gateway.', 'woocommerce' ), 'error' );
539 return;
540 }
541
542 $gateway->validate_fields();
543
544 if ( wc_notice_count( 'error' ) > 0 ) {
545 return;
546 }
547
548 $result = $gateway->add_payment_method();
549
550 if ( 'success' === $result['result'] ) {
551 wc_add_notice( __( 'Payment method successfully added.', 'woocommerce' ) );
552 }
553
554 if ( 'failure' === $result['result'] ) {
555 wc_add_notice( __( 'Unable to add payment method to your account.', 'woocommerce' ), 'error' );
556 }
557
558 if ( ! empty( $result['redirect'] ) ) {
559 wp_redirect( $result['redirect'] ); //phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect
560 exit();
561 }
562 }
563 }
564 }
565
566 /**
567 * Process the delete payment method form.
568 */
569 public static function delete_payment_method_action() {
570 global $wp;
571
572 if ( isset( $wp->query_vars['delete-payment-method'] ) ) {
573 wc_nocache_headers();
574
575 $token_id = absint( $wp->query_vars['delete-payment-method'] );
576 $token = WC_Payment_Tokens::get( $token_id );
577
578 if ( is_null( $token ) || get_current_user_id() !== $token->get_user_id() || ! isset( $_REQUEST['_wpnonce'] ) || false === wp_verify_nonce( wp_unslash( $_REQUEST['_wpnonce'] ), 'delete-payment-method-' . $token_id ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
579 wc_add_notice( __( 'Invalid payment method.', 'woocommerce' ), 'error' );
580 } else {
581 WC_Payment_Tokens::delete( $token_id );
582 wc_add_notice( __( 'Payment method deleted.', 'woocommerce' ) );
583 }
584
585 wp_safe_redirect( wc_get_account_endpoint_url( 'payment-methods' ) );
586 exit();
587 }
588 }
589
590 /**
591 * Process the delete payment method form.
592 */
593 public static function set_default_payment_method_action() {
594 global $wp;
595
596 if ( isset( $wp->query_vars['set-default-payment-method'] ) ) {
597 wc_nocache_headers();
598
599 $token_id = absint( $wp->query_vars['set-default-payment-method'] );
600 $token = WC_Payment_Tokens::get( $token_id );
601
602 if ( is_null( $token ) || get_current_user_id() !== $token->get_user_id() || ! isset( $_REQUEST['_wpnonce'] ) || false === wp_verify_nonce( wp_unslash( $_REQUEST['_wpnonce'] ), 'set-default-payment-method-' . $token_id ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
603 wc_add_notice( __( 'Invalid payment method.', 'woocommerce' ), 'error' );
604 } else {
605 WC_Payment_Tokens::set_users_default( $token->get_user_id(), intval( $token_id ) );
606 wc_add_notice( __( 'This payment method was successfully set as your default.', 'woocommerce' ) );
607 }
608
609 wp_safe_redirect( wc_get_account_endpoint_url( 'payment-methods' ) );
610 exit();
611 }
612 }
613
614 /**
615 * Remove from cart/update.
616 */
617 public static function update_cart_action() {
618 if ( ! ( isset( $_REQUEST['apply_coupon'] ) || isset( $_REQUEST['remove_coupon'] ) || isset( $_REQUEST['remove_item'] ) || isset( $_REQUEST['undo_item'] ) || isset( $_REQUEST['update_cart'] ) || isset( $_REQUEST['proceed'] ) ) ) {
619 return;
620 }
621
622 wc_nocache_headers();
623
624 $nonce_value = wc_get_var( $_REQUEST['woocommerce-cart-nonce'], wc_get_var( $_REQUEST['_wpnonce'], '' ) ); // @codingStandardsIgnoreLine.
625
626 if ( ! empty( $_POST['apply_coupon'] ) && ! empty( $_POST['coupon_code'] ) ) {
627 WC()->cart->add_discount( wc_format_coupon_code( wp_unslash( $_POST['coupon_code'] ) ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
628
629 } elseif ( isset( $_GET['remove_coupon'] ) ) {
630 WC()->cart->remove_coupon( wc_format_coupon_code( urldecode( wp_unslash( $_GET['remove_coupon'] ) ) ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
631
632 } elseif ( ! empty( $_GET['remove_item'] ) && wp_verify_nonce( $nonce_value, 'woocommerce-cart' ) ) {
633 $cart_item_key = sanitize_text_field( wp_unslash( $_GET['remove_item'] ) );
634 $cart_item = WC()->cart->get_cart_item( $cart_item_key );
635
636 if ( $cart_item ) {
637 WC()->cart->remove_cart_item( $cart_item_key );
638
639 $product = wc_get_product( $cart_item['product_id'] );
640
641 /* translators: %s: Item name. */
642 $item_removed_title = apply_filters( 'woocommerce_cart_item_removed_title', $product ? sprintf( _x( '&ldquo;%s&rdquo;', 'Item name in quotes', 'woocommerce' ), $product->get_name() ) : __( 'Item', 'woocommerce' ), $cart_item );
643
644 // Don't show undo link if removed item is out of stock.
645 if ( $product && $product->is_in_stock() && $product->has_enough_stock( $cart_item['quantity'] ) ) {
646 /* Translators: %s Product title. */
647 $removed_notice = sprintf( __( '%s removed.', 'woocommerce' ), $item_removed_title );
648 $removed_notice .= ' <a href="' . esc_url( wc_get_cart_undo_url( $cart_item_key ) ) . '" class="restore-item">' . __( 'Undo?', 'woocommerce' ) . '</a>';
649 } else {
650 /* Translators: %s Product title. */
651 $removed_notice = sprintf( __( '%s removed.', 'woocommerce' ), $item_removed_title );
652 }
653
654 wc_add_notice( $removed_notice, apply_filters( 'woocommerce_cart_item_removed_notice_type', 'success' ) );
655 }
656
657 if ( wp_get_referer() ) {
658 wp_safe_redirect( remove_query_arg( array( 'remove_item', 'add-to-cart', 'added-to-cart', 'order_again', '_wpnonce' ), add_query_arg( 'removed_item', '1', wp_get_referer() ) ) );
659 exit;
660 }
661 } elseif ( ! empty( $_GET['undo_item'] ) && isset( $_GET['_wpnonce'] ) && wp_verify_nonce( $nonce_value, 'woocommerce-cart' ) ) {
662
663 // Undo Cart Item.
664 $cart_item_key = sanitize_text_field( wp_unslash( $_GET['undo_item'] ) );
665
666 WC()->cart->restore_cart_item( $cart_item_key );
667
668 if ( wp_get_referer() ) {
669 wp_safe_redirect( remove_query_arg( array( 'undo_item', '_wpnonce' ), wp_get_referer() ) );
670 exit;
671 }
672 }
673
674 // Update Cart - checks apply_coupon too because they are in the same form.
675 if ( ( ! empty( $_POST['apply_coupon'] ) || ! empty( $_POST['update_cart'] ) || ! empty( $_POST['proceed'] ) ) && wp_verify_nonce( $nonce_value, 'woocommerce-cart' ) ) {
676
677 $cart_updated = false;
678 $cart_totals = isset( $_POST['cart'] ) ? wp_unslash( $_POST['cart'] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
679
680 if ( ! WC()->cart->is_empty() && is_array( $cart_totals ) ) {
681 foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
682
683 $_product = $values['data'];
684
685 // Skip product if no updated quantity was posted.
686 if ( ! isset( $cart_totals[ $cart_item_key ] ) || ! isset( $cart_totals[ $cart_item_key ]['qty'] ) ) {
687 continue;
688 }
689
690 // Sanitize.
691 $quantity = apply_filters( 'woocommerce_stock_amount_cart_item', wc_stock_amount( preg_replace( '/[^0-9\.]/', '', $cart_totals[ $cart_item_key ]['qty'] ) ), $cart_item_key );
692
693 if ( '' === $quantity || $quantity === $values['quantity'] ) {
694 continue;
695 }
696
697 // Update cart validation.
698 $passed_validation = apply_filters( 'woocommerce_update_cart_validation', true, $cart_item_key, $values, $quantity );
699
700 // is_sold_individually.
701 if ( $_product->is_sold_individually() && $quantity > 1 ) {
702 /* Translators: %s Product title. */
703 wc_add_notice( sprintf( __( 'You can only have 1 %s in your cart.', 'woocommerce' ), $_product->get_name() ), 'error' );
704 $passed_validation = false;
705 }
706
707 if ( $passed_validation ) {
708 WC()->cart->set_quantity( $cart_item_key, $quantity, false );
709 $cart_updated = true;
710 }
711 }
712 }
713
714 // Trigger action - let 3rd parties update the cart if they need to and update the $cart_updated variable.
715 $cart_updated = apply_filters( 'woocommerce_update_cart_action_cart_updated', $cart_updated );
716
717 if ( $cart_updated ) {
718 WC()->cart->calculate_totals();
719 }
720
721 if ( ! empty( $_POST['proceed'] ) ) {
722 wp_safe_redirect( wc_get_checkout_url() );
723 exit;
724 } elseif ( $cart_updated ) {
725 wc_add_notice( __( 'Cart updated.', 'woocommerce' ), apply_filters( 'woocommerce_cart_updated_notice_type', 'success' ) );
726
727 if ( wp_get_referer() ) {
728 wp_safe_redirect( remove_query_arg( array( 'remove_coupon', 'add-to-cart' ), wp_get_referer() ) );
729 exit;
730 }
731 }
732 }
733 }
734
735 /**
736 * Place a previous order again.
737 *
738 * @deprecated 3.5.0 Logic moved to cart session handling.
739 */
740 public static function order_again() {
741 wc_deprecated_function( 'WC_Form_Handler::order_again', '3.5', 'This method should not be called manually.' );
742 }
743
744 /**
745 * Cancel a pending order.
746 */
747 public static function cancel_order() {
748 if (
749 isset( $_GET['cancel_order'] ) &&
750 isset( $_GET['order'] ) &&
751 isset( $_GET['order_id'] ) &&
752 ( isset( $_GET['_wpnonce'] ) && wp_verify_nonce( wp_unslash( $_GET['_wpnonce'] ), 'woocommerce-cancel_order' ) ) // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
753 ) {
754 wc_nocache_headers();
755
756 $order_key = wp_unslash( $_GET['order'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
757 $order_id = absint( $_GET['order_id'] );
758 $order = wc_get_order( $order_id );
759 /**
760 * Filter valid order statuses for cancel.
761 *
762 * @since 3.5.0
763 *
764 * @param array $valid_statuses Array of valid order statuses for cancel.
765 * @param WC_Order $order Order object.
766 */
767 $valid_statuses = apply_filters( 'woocommerce_valid_order_statuses_for_cancel', array( OrderStatus::PENDING, OrderStatus::FAILED ), $order );
768 $user_can_cancel = current_user_can( 'cancel_order', $order_id );
769 $order_can_cancel = $order->has_status( $valid_statuses );
770 $redirect = isset( $_GET['redirect'] ) ? wp_unslash( $_GET['redirect'] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
771
772 if ( $user_can_cancel && $order_can_cancel && $order->get_id() === $order_id && hash_equals( $order->get_order_key(), $order_key ) ) {
773
774 // Cancel the order + restore stock.
775 WC()->session->set( 'order_awaiting_payment', false );
776 $order->update_status( OrderStatus::CANCELLED, __( 'Order cancelled by customer.', 'woocommerce' ) );
777
778 wc_add_notice( apply_filters( 'woocommerce_order_cancelled_notice', __( 'Your order was cancelled.', 'woocommerce' ) ), apply_filters( 'woocommerce_order_cancelled_notice_type', 'notice' ) );
779
780 do_action( 'woocommerce_cancelled_order', $order->get_id() );
781
782 } elseif ( $user_can_cancel && ! $order_can_cancel ) {
783 wc_add_notice( __( 'Your order can no longer be cancelled. Please contact us if you need assistance.', 'woocommerce' ), 'error' );
784 } else {
785 wc_add_notice( __( 'Invalid order.', 'woocommerce' ), 'error' );
786 }
787
788 if ( $redirect ) {
789 wp_safe_redirect( $redirect );
790 exit;
791 }
792 }
793 }
794
795 /**
796 * Add to cart action.
797 *
798 * Checks for a valid request, does validation (via hooks) and then redirects if valid.
799 *
800 * @param bool $url (default: false) URL to redirect to.
801 */
802 public static function add_to_cart_action( $url = false ) {
803 if ( ! isset( $_REQUEST['add-to-cart'] ) || ! is_numeric( wp_unslash( $_REQUEST['add-to-cart'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
804 return;
805 }
806
807 wc_nocache_headers();
808
809 $product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( wp_unslash( $_REQUEST['add-to-cart'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
810 $was_added_to_cart = false;
811 $adding_to_cart = wc_get_product( $product_id );
812
813 if ( ! $adding_to_cart ) {
814 return;
815 }
816
817 $add_to_cart_handler = apply_filters( 'woocommerce_add_to_cart_handler', $adding_to_cart->get_type(), $adding_to_cart );
818
819 if ( ProductType::VARIABLE === $add_to_cart_handler || ProductType::VARIATION === $add_to_cart_handler ) {
820 $was_added_to_cart = self::add_to_cart_handler_variable( $product_id );
821 } elseif ( ProductType::GROUPED === $add_to_cart_handler ) {
822 $was_added_to_cart = self::add_to_cart_handler_grouped( $product_id );
823 } elseif ( has_action( 'woocommerce_add_to_cart_handler_' . $add_to_cart_handler ) ) {
824 do_action( 'woocommerce_add_to_cart_handler_' . $add_to_cart_handler, $url ); // Custom handler.
825 } else {
826 $was_added_to_cart = self::add_to_cart_handler_simple( $product_id );
827 }
828
829 // If we added the product to the cart we can now optionally do a redirect.
830 if ( $was_added_to_cart && 0 === wc_notice_count( 'error' ) ) {
831 $url = apply_filters( 'woocommerce_add_to_cart_redirect', $url, $adding_to_cart );
832
833 if ( $url ) {
834 wp_safe_redirect( $url );
835 exit;
836 } elseif ( 'yes' === get_option( 'woocommerce_cart_redirect_after_add' ) ) {
837 wp_safe_redirect( wc_get_cart_url() );
838 exit;
839 }
840 }
841 }
842
843 /**
844 * Handle adding simple products to the cart.
845 *
846 * @since 2.4.6 Split from add_to_cart_action.
847 * @param int $product_id Product ID to add to the cart.
848 * @return bool success or not
849 */
850 private static function add_to_cart_handler_simple( $product_id ) {
851 $quantity = empty( $_REQUEST['quantity'] ) ? 1 : wc_stock_amount( wp_unslash( $_REQUEST['quantity'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
852 $passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity );
853
854 if ( $passed_validation && false !== WC()->cart->add_to_cart( $product_id, $quantity ) ) {
855 wc_add_to_cart_message( array( $product_id => $quantity ), true );
856 return true;
857 }
858 return false;
859 }
860
861 /**
862 * Handle adding grouped products to the cart.
863 *
864 * @since 2.4.6 Split from add_to_cart_action.
865 * @param int $product_id Product ID to add to the cart.
866 * @return bool success or not
867 */
868 private static function add_to_cart_handler_grouped( $product_id ) {
869 $was_added_to_cart = false;
870 $added_to_cart = array();
871 $items = isset( $_REQUEST['quantity'] ) && is_array( $_REQUEST['quantity'] ) ? wp_unslash( $_REQUEST['quantity'] ) : array(); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
872
873 if ( ! empty( $items ) ) {
874 $quantity_set = false;
875
876 foreach ( $items as $item => $quantity ) {
877 $quantity = wc_stock_amount( $quantity );
878 if ( $quantity <= 0 ) {
879 continue;
880 }
881 $quantity_set = true;
882
883 // Add to cart validation.
884 $passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $item, $quantity );
885
886 // Suppress total recalculation until finished.
887 remove_action( 'woocommerce_add_to_cart', array( WC()->cart, 'calculate_totals' ), 20, 0 );
888
889 if ( $passed_validation && false !== WC()->cart->add_to_cart( $item, $quantity ) ) {
890 $was_added_to_cart = true;
891 $added_to_cart[ $item ] = $quantity;
892 }
893
894 add_action( 'woocommerce_add_to_cart', array( WC()->cart, 'calculate_totals' ), 20, 0 );
895 }
896
897 if ( ! $was_added_to_cart && ! $quantity_set ) {
898 wc_add_notice( __( 'Please choose the quantity of items you wish to add to your cart&hellip;', 'woocommerce' ), 'error' );
899 } elseif ( $was_added_to_cart ) {
900 wc_add_to_cart_message( $added_to_cart );
901 WC()->cart->calculate_totals();
902 return true;
903 }
904 } elseif ( $product_id ) {
905 /* Link on product archives */
906 wc_add_notice( __( 'Please choose a product to add to your cart&hellip;', 'woocommerce' ), 'error' );
907 }
908 return false;
909 }
910
911 /**
912 * Handle adding variable products to the cart.
913 *
914 * @since 2.4.6 Split from add_to_cart_action.
915 * @throws Exception If add to cart fails.
916 * @param int $product_id Product ID to add to the cart.
917 * @return bool success or not
918 */
919 private static function add_to_cart_handler_variable( $product_id ) {
920 $variation_id = empty( $_REQUEST['variation_id'] ) ? '' : absint( wp_unslash( $_REQUEST['variation_id'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
921 $quantity = empty( $_REQUEST['quantity'] ) ? 1 : wc_stock_amount( wp_unslash( $_REQUEST['quantity'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
922 $variations = array();
923
924 $product = wc_get_product( $product_id );
925
926 foreach ( $_REQUEST as $key => $value ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
927 if ( 'attribute_' !== substr( $key, 0, 10 ) ) {
928 continue;
929 }
930
931 $variations[ sanitize_title( wp_unslash( $key ) ) ] = wp_unslash( $value );
932 }
933
934 $passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity, $variation_id, $variations );
935
936 if ( ! $passed_validation ) {
937 return false;
938 }
939
940 // Prevent parent variable product from being added to cart.
941 if ( empty( $variation_id ) && $product && $product->is_type( ProductType::VARIABLE ) ) {
942 /* translators: 1: product link, 2: product name */
943 wc_add_notice( sprintf( __( 'Please choose product options by visiting <a href="%1$s" title="%2$s">%2$s</a>.', 'woocommerce' ), esc_url( get_permalink( $product_id ) ), esc_html( $product->get_name() ) ), 'error' );
944
945 return false;
946 }
947
948 if ( false !== WC()->cart->add_to_cart( $product_id, $quantity, $variation_id, $variations ) ) {
949 wc_add_to_cart_message( array( $product_id => $quantity ), true );
950 return true;
951 }
952
953 return false;
954 }
955
956 /**
957 * Process the login form.
958 *
959 * @throws Exception On login error.
960 */
961 public static function process_login() {
962
963 static $valid_nonce = null;
964
965 if ( null === $valid_nonce ) {
966 // The global form-login.php template used `_wpnonce` in template versions < 3.3.0.
967 $nonce_value = wc_get_var( $_REQUEST['woocommerce-login-nonce'], wc_get_var( $_REQUEST['_wpnonce'], '' ) ); // @codingStandardsIgnoreLine.
968
969 $valid_nonce = wp_verify_nonce( $nonce_value, 'woocommerce-login' );
970 }
971
972 if ( isset( $_POST['login'], $_POST['username'], $_POST['password'] ) && $valid_nonce ) {
973
974 try {
975 $creds = array(
976 'user_login' => trim( wp_unslash( $_POST['username'] ) ), // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
977 'user_password' => $_POST['password'], // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash
978 'remember' => isset( $_POST['rememberme'] ), // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
979 );
980
981 $validation_error = new WP_Error();
982 $validation_error = apply_filters( 'woocommerce_process_login_errors', $validation_error, $creds['user_login'], $creds['user_password'] );
983
984 if ( $validation_error->get_error_code() ) {
985 throw new Exception( '<strong>' . __( 'Error:', 'woocommerce' ) . '</strong> ' . $validation_error->get_error_message() );
986 }
987
988 if ( empty( $creds['user_login'] ) ) {
989 throw new Exception( '<strong>' . __( 'Error:', 'woocommerce' ) . '</strong> ' . __( 'Username is required.', 'woocommerce' ) );
990 }
991
992 // On multisite, ensure user exists on current site, if not add them before allowing login.
993 if ( is_multisite() ) {
994 $user_data = get_user_by( is_email( $creds['user_login'] ) ? 'email' : 'login', $creds['user_login'] );
995
996 if ( $user_data && ! is_user_member_of_blog( $user_data->ID, get_current_blog_id() ) ) {
997 add_user_to_blog( get_current_blog_id(), $user_data->ID, 'customer' );
998 }
999 }
1000
1001 // Perform the login.
1002 $user = wp_signon( apply_filters( 'woocommerce_login_credentials', $creds ), is_ssl() );
1003
1004 if ( is_wp_error( $user ) ) {
1005 throw new Exception( $user->get_error_message() );
1006 } else {
1007
1008 if ( ! empty( $_POST['redirect'] ) ) {
1009 $redirect = wp_unslash( $_POST['redirect'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1010 } elseif ( wc_get_raw_referer() ) {
1011 $redirect = wc_get_raw_referer();
1012 } else {
1013 $redirect = wc_get_page_permalink( 'myaccount' );
1014 }
1015
1016 $redirect = remove_query_arg( array( 'wc_error', 'password-reset' ), $redirect );
1017
1018 wp_redirect( wp_validate_redirect( apply_filters( 'woocommerce_login_redirect', $redirect, $user ), wc_get_page_permalink( 'myaccount' ) ) ); // phpcs:ignore
1019 exit;
1020 }
1021 } catch ( Exception $e ) {
1022 wc_add_notice( apply_filters( 'login_errors', $e->getMessage() ), 'error' );
1023 do_action( 'woocommerce_login_failed' );
1024 }
1025 }
1026 }
1027
1028 /**
1029 * Handle lost password form.
1030 */
1031 public static function process_lost_password() {
1032 if ( isset( $_POST['wc_reset_password'], $_POST['user_login'] ) ) {
1033 $nonce_value = wc_get_var( $_REQUEST['woocommerce-lost-password-nonce'], wc_get_var( $_REQUEST['_wpnonce'], '' ) ); // @codingStandardsIgnoreLine.
1034
1035 if ( ! wp_verify_nonce( $nonce_value, 'lost_password' ) ) {
1036 return;
1037 }
1038
1039 $success = WC_Shortcode_My_Account::retrieve_password();
1040
1041 // If successful, redirect to my account with query arg set.
1042 if ( $success ) {
1043 wp_safe_redirect( add_query_arg( 'reset-link-sent', 'true', wc_get_account_endpoint_url( 'lost-password' ) ) );
1044 exit;
1045 }
1046 }
1047 }
1048
1049 /**
1050 * Handle reset password form.
1051 */
1052 public static function process_reset_password() {
1053 $nonce_value = wc_get_var( $_REQUEST['woocommerce-reset-password-nonce'], wc_get_var( $_REQUEST['_wpnonce'], '' ) ); // @codingStandardsIgnoreLine.
1054
1055 if ( ! wp_verify_nonce( $nonce_value, 'reset_password' ) ) {
1056 return;
1057 }
1058
1059 $posted_fields = array( 'wc_reset_password', 'password_1', 'password_2', 'reset_key', 'reset_login' );
1060
1061 foreach ( $posted_fields as $field ) {
1062 if ( ! isset( $_POST[ $field ] ) ) {
1063 return;
1064 }
1065
1066 if ( in_array( $field, array( 'password_1', 'password_2' ), true ) ) {
1067 // Don't unslash password fields
1068 // @see https://github.com/woocommerce/woocommerce/issues/23922.
1069 $posted_fields[ $field ] = $_POST[ $field ]; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash
1070 } else {
1071 $posted_fields[ $field ] = wp_unslash( $_POST[ $field ] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1072 }
1073 }
1074
1075 $user = WC_Shortcode_My_Account::check_password_reset_key( $posted_fields['reset_key'], $posted_fields['reset_login'] );
1076
1077 if ( $user instanceof WP_User ) {
1078 if ( empty( $posted_fields['password_1'] ) ) {
1079 wc_add_notice( __( 'Please enter your password.', 'woocommerce' ), 'error' );
1080 }
1081
1082 if ( $posted_fields['password_1'] !== $posted_fields['password_2'] ) {
1083 wc_add_notice( __( 'Passwords do not match.', 'woocommerce' ), 'error' );
1084 }
1085
1086 $errors = new WP_Error();
1087
1088 do_action( 'validate_password_reset', $errors, $user );
1089
1090 wc_add_wp_error_notices( $errors );
1091
1092 if ( 0 === wc_notice_count( 'error' ) ) {
1093 WC_Shortcode_My_Account::reset_password( $user, $posted_fields['password_1'] );
1094
1095 do_action( 'woocommerce_customer_reset_password', $user );
1096
1097 wp_safe_redirect( add_query_arg( 'password-reset', 'true', wc_get_page_permalink( 'myaccount' ) ) );
1098 exit;
1099 }
1100 }
1101 }
1102
1103 /**
1104 * Process the registration form.
1105 *
1106 * @throws Exception On registration error.
1107 */
1108 public static function process_registration() {
1109 $nonce_value = isset( $_POST['_wpnonce'] ) ? wp_unslash( $_POST['_wpnonce'] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1110 $nonce_value = isset( $_POST['woocommerce-register-nonce'] ) ? wp_unslash( $_POST['woocommerce-register-nonce'] ) : $nonce_value; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1111
1112 if ( isset( $_POST['register'], $_POST['email'] ) && wp_verify_nonce( $nonce_value, 'woocommerce-register' ) ) {
1113 $username = 'no' === get_option( 'woocommerce_registration_generate_username' ) && isset( $_POST['username'] ) ? wp_unslash( $_POST['username'] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1114 $password = 'no' === get_option( 'woocommerce_registration_generate_password' ) && isset( $_POST['password'] ) ? $_POST['password'] : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash
1115 $email = wp_unslash( $_POST['email'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1116
1117 try {
1118 $validation_error = new WP_Error();
1119 $validation_error = apply_filters( 'woocommerce_process_registration_errors', $validation_error, $username, $password, $email );
1120 $validation_errors = $validation_error->get_error_messages();
1121
1122 if ( 1 === count( $validation_errors ) ) {
1123 throw new Exception( $validation_error->get_error_message() );
1124 } elseif ( $validation_errors ) {
1125 foreach ( $validation_errors as $message ) {
1126 wc_add_notice( '<strong>' . __( 'Error:', 'woocommerce' ) . '</strong> ' . $message, 'error' );
1127 }
1128 throw new Exception();
1129 }
1130
1131 $new_customer = wc_create_new_customer( sanitize_email( $email ), wc_clean( $username ), $password );
1132
1133 if ( is_wp_error( $new_customer ) ) {
1134 throw new Exception( $new_customer->get_error_message() );
1135 }
1136
1137 if ( 'yes' === get_option( 'woocommerce_registration_generate_password' ) ) {
1138 wc_add_notice( __( 'Your account was created successfully and a password has been sent to your email address.', 'woocommerce' ) );
1139 } else {
1140 wc_add_notice( __( 'Your account was created successfully. Your login details have been sent to your email address.', 'woocommerce' ) );
1141 }
1142
1143 // Only redirect after a forced login - otherwise output a success notice.
1144 if ( apply_filters( 'woocommerce_registration_auth_new_customer', true, $new_customer ) ) {
1145 wc_set_customer_auth_cookie( $new_customer );
1146
1147 if ( ! empty( $_POST['redirect'] ) ) {
1148 $redirect = wp_sanitize_redirect( wp_unslash( $_POST['redirect'] ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1149 } elseif ( wc_get_raw_referer() ) {
1150 $redirect = wc_get_raw_referer();
1151 } else {
1152 $redirect = wc_get_page_permalink( 'myaccount' );
1153 }
1154
1155 wp_redirect( wp_validate_redirect( apply_filters( 'woocommerce_registration_redirect', $redirect ), wc_get_page_permalink( 'myaccount' ) ) ); //phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect
1156 exit;
1157 }
1158 } catch ( Exception $e ) {
1159 if ( $e->getMessage() ) {
1160 wc_add_notice( '<strong>' . __( 'Error:', 'woocommerce' ) . '</strong> ' . $e->getMessage(), 'error' );
1161 }
1162 }
1163 }
1164 }
1165 }
1166
1167 WC_Form_Handler::init();
1168