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