2013-08-09 16:11:15 +00:00
< ? php
2014-02-07 18:35:30 +00:00
if ( ! defined ( 'ABSPATH' ) ) {
exit ; // Exit if accessed directly
}
2013-08-09 16:11:15 +00:00
/**
* Handle frontend forms
*
* @ class WC_Form_Handler
2014-05-28 13:52:50 +00:00
* @ version 2.2 . 0
2013-08-09 16:11:15 +00:00
* @ package WooCommerce / Classes /
* @ category Class
* @ author WooThemes
*/
class WC_Form_Handler {
/**
2014-05-28 13:52:50 +00:00
* Hook in methods
2013-08-09 16:11:15 +00:00
*/
2014-05-28 13:52:50 +00:00
public static function init () {
add_action ( 'template_redirect' , array ( __CLASS__ , 'save_address' ) );
add_action ( 'template_redirect' , array ( __CLASS__ , 'save_account_details' ) );
add_action ( 'init' , array ( __CLASS__ , 'checkout_action' ), 20 );
add_action ( 'init' , array ( __CLASS__ , 'process_login' ) );
add_action ( 'init' , array ( __CLASS__ , 'process_registration' ) );
add_action ( 'init' , array ( __CLASS__ , 'process_reset_password' ) );
add_action ( 'init' , array ( __CLASS__ , 'cancel_order' ) );
add_action ( 'init' , array ( __CLASS__ , 'order_again' ) );
add_action ( 'init' , array ( __CLASS__ , 'update_cart_action' ) );
add_action ( 'init' , array ( __CLASS__ , 'add_to_cart_action' ) );
add_action ( 'wp' , array ( __CLASS__ , 'pay_action' ), 20 );
add_action ( 'wp' , array ( __CLASS__ , 'add_payment_method_action' ), 20 );
2013-08-09 16:11:15 +00:00
}
/**
* Save and and update a billing or shipping address if the
* form was submitted through the user account page .
*/
2014-05-28 13:52:50 +00:00
public static function save_address () {
global $wp ;
2013-08-09 16:11:15 +00:00
2014-02-07 18:35:30 +00:00
if ( 'POST' !== strtoupper ( $_SERVER [ 'REQUEST_METHOD' ] ) ) {
2013-08-09 16:11:15 +00:00
return ;
2014-02-07 18:35:30 +00:00
}
2013-08-09 16:11:15 +00:00
2014-03-06 13:11:20 +00:00
if ( empty ( $_POST [ 'action' ] ) || ( 'edit_address' !== $_POST [ 'action' ] ) || empty ( $_POST [ '_wpnonce' ] ) ) {
2013-08-09 16:11:15 +00:00
return ;
2014-02-07 18:35:30 +00:00
}
2013-08-09 16:11:15 +00:00
wp_verify_nonce ( $_POST [ '_wpnonce' ], 'woocommerce-edit_address' );
$user_id = get_current_user_id ();
2014-02-07 18:35:30 +00:00
if ( $user_id <= 0 ) {
2013-09-19 13:39:49 +00:00
return ;
2014-02-07 18:35:30 +00:00
}
2013-08-09 16:11:15 +00:00
2014-04-24 21:18:41 +00:00
$load_address = isset ( $wp -> query_vars [ 'edit-address' ] ) ? wc_edit_address_i18n ( sanitize_key ( $wp -> query_vars [ 'edit-address' ] ), true ) : 'billing' ;
2013-08-09 16:11:15 +00:00
2013-11-25 14:01:32 +00:00
$address = WC () -> countries -> get_address_fields ( esc_attr ( $_POST [ $load_address . '_country' ] ), $load_address . '_' );
2013-08-09 16:11:15 +00:00
2013-09-19 13:39:49 +00:00
foreach ( $address as $key => $field ) {
2013-08-09 16:11:15 +00:00
2014-02-07 18:35:30 +00:00
if ( ! isset ( $field [ 'type' ] ) ) {
2013-09-19 13:39:49 +00:00
$field [ 'type' ] = 'text' ;
2014-02-07 18:35:30 +00:00
}
2013-08-09 16:11:15 +00:00
// Get Value
2013-09-19 13:39:49 +00:00
switch ( $field [ 'type' ] ) {
2013-08-09 16:11:15 +00:00
case " checkbox " :
2013-09-19 13:39:49 +00:00
$_POST [ $key ] = isset ( $_POST [ $key ] ) ? 1 : 0 ;
2013-08-09 16:11:15 +00:00
break ;
default :
2013-11-25 13:34:21 +00:00
$_POST [ $key ] = isset ( $_POST [ $key ] ) ? wc_clean ( $_POST [ $key ] ) : '' ;
2013-08-09 16:11:15 +00:00
break ;
2013-09-19 13:39:49 +00:00
}
2013-08-09 16:11:15 +00:00
// Hook to allow modification of value
2013-09-19 13:39:49 +00:00
$_POST [ $key ] = apply_filters ( 'woocommerce_process_myaccount_field_' . $key , $_POST [ $key ] );
2013-08-09 16:11:15 +00:00
// Validation: Required fields
2014-02-07 18:35:30 +00:00
if ( ! empty ( $field [ 'required' ] ) && empty ( $_POST [ $key ] ) ) {
2013-11-13 04:29:03 +00:00
wc_add_notice ( $field [ 'label' ] . ' ' . __ ( 'is a required field.' , 'woocommerce' ), 'error' );
2014-02-07 18:35:30 +00:00
}
2013-09-19 13:39:49 +00:00
2014-06-25 10:06:42 +00:00
if ( ! empty ( $_POST [ $key ] ) ) {
2014-06-20 09:28:04 +00:00
// Validation rules
if ( ! empty ( $field [ 'validate' ] ) && is_array ( $field [ 'validate' ] ) ) {
foreach ( $field [ 'validate' ] as $rule ) {
switch ( $rule ) {
case 'postcode' :
$_POST [ $key ] = strtoupper ( str_replace ( ' ' , '' , $_POST [ $key ] ) );
if ( ! WC_Validation :: is_postcode ( $_POST [ $key ], $_POST [ $load_address . '_country' ] ) ) {
wc_add_notice ( __ ( 'Please enter a valid postcode/ZIP.' , 'woocommerce' ), 'error' );
} else {
$_POST [ $key ] = wc_format_postcode ( $_POST [ $key ], $_POST [ $load_address . '_country' ] );
}
break ;
case 'phone' :
$_POST [ $key ] = wc_format_phone_number ( $_POST [ $key ] );
2013-09-19 13:39:49 +00:00
2014-06-20 09:28:04 +00:00
if ( ! WC_Validation :: is_phone ( $_POST [ $key ] ) ) {
wc_add_notice ( '<strong>' . $field [ 'label' ] . '</strong> ' . __ ( 'is not a valid phone number.' , 'woocommerce' ), 'error' );
}
break ;
case 'email' :
$_POST [ $key ] = strtolower ( $_POST [ $key ] );
2013-09-19 13:39:49 +00:00
2014-06-20 09:28:04 +00:00
if ( ! is_email ( $_POST [ $key ] ) ) {
wc_add_notice ( '<strong>' . $field [ 'label' ] . '</strong> ' . __ ( 'is not a valid email address.' , 'woocommerce' ), 'error' );
}
break ;
}
2013-09-19 13:39:49 +00:00
}
}
}
}
2013-08-09 16:11:15 +00:00
2013-11-27 16:15:53 +00:00
if ( wc_notice_count ( 'error' ) == 0 ) {
2013-08-09 16:11:15 +00:00
2014-02-07 18:35:30 +00:00
foreach ( $address as $key => $field ) {
2013-09-19 13:39:49 +00:00
update_user_meta ( $user_id , $key , $_POST [ $key ] );
2014-02-07 18:35:30 +00:00
}
2013-08-09 16:11:15 +00:00
2013-11-13 04:32:29 +00:00
wc_add_notice ( __ ( 'Address changed successfully.' , 'woocommerce' ) );
2013-08-09 16:11:15 +00:00
2013-09-10 13:31:13 +00:00
do_action ( 'woocommerce_customer_save_address' , $user_id , $load_address );
2013-08-09 16:11:15 +00:00
2013-11-25 14:07:22 +00:00
wp_safe_redirect ( get_permalink ( wc_get_page_id ( 'myaccount' ) ) );
2013-08-09 16:11:15 +00:00
exit ;
}
}
/**
* Save the password / account details and redirect back to the my account page .
*/
2014-05-28 13:52:50 +00:00
public static function save_account_details () {
2013-08-09 16:11:15 +00:00
2014-02-07 18:35:30 +00:00
if ( 'POST' !== strtoupper ( $_SERVER [ 'REQUEST_METHOD' ] ) ) {
2013-08-09 16:11:15 +00:00
return ;
2014-02-07 18:35:30 +00:00
}
2013-08-09 16:11:15 +00:00
2014-03-06 13:11:20 +00:00
if ( empty ( $_POST [ 'action' ] ) || ( 'save_account_details' !== $_POST [ 'action' ] ) || empty ( $_POST [ '_wpnonce' ] ) ) {
2013-08-09 16:11:15 +00:00
return ;
2014-02-07 18:35:30 +00:00
}
2013-08-09 16:11:15 +00:00
wp_verify_nonce ( $_POST [ '_wpnonce' ], 'woocommerce-save_account_details' );
$update = true ;
$errors = new WP_Error ();
$user = new stdClass ();
$user -> ID = ( int ) get_current_user_id ();
$current_user = get_user_by ( 'id' , $user -> ID );
2014-02-07 18:35:30 +00:00
if ( $user -> ID <= 0 ) {
2013-08-09 16:11:15 +00:00
return ;
2014-02-07 18:35:30 +00:00
}
2013-08-09 16:11:15 +00:00
2013-11-25 13:34:21 +00:00
$account_first_name = ! empty ( $_POST [ 'account_first_name' ] ) ? wc_clean ( $_POST [ 'account_first_name' ] ) : '' ;
$account_last_name = ! empty ( $_POST [ 'account_last_name' ] ) ? wc_clean ( $_POST [ 'account_last_name' ] ) : '' ;
2014-04-14 06:29:22 +00:00
$account_email = ! empty ( $_POST [ 'account_email' ] ) ? sanitize_email ( $_POST [ 'account_email' ] ) : '' ;
2014-04-22 19:01:57 +00:00
$pass_cur = ! empty ( $_POST [ 'password_current' ] ) ? $_POST [ 'password_current' ] : '' ;
2014-04-14 06:29:22 +00:00
$pass1 = ! empty ( $_POST [ 'password_1' ] ) ? $_POST [ 'password_1' ] : '' ;
$pass2 = ! empty ( $_POST [ 'password_2' ] ) ? $_POST [ 'password_2' ] : '' ;
2014-04-22 19:01:57 +00:00
$save_pass = true ;
2013-08-09 16:11:15 +00:00
$user -> first_name = $account_first_name ;
$user -> last_name = $account_last_name ;
$user -> user_email = $account_email ;
$user -> display_name = $user -> first_name ;
2014-02-07 18:35:30 +00:00
if ( empty ( $account_first_name ) || empty ( $account_last_name ) ) {
2013-11-13 04:29:03 +00:00
wc_add_notice ( __ ( 'Please enter your name.' , 'woocommerce' ), 'error' );
2014-02-07 18:35:30 +00:00
}
2013-08-09 16:11:15 +00:00
2014-02-07 18:35:30 +00:00
if ( empty ( $account_email ) || ! is_email ( $account_email ) ) {
2013-11-13 04:29:03 +00:00
wc_add_notice ( __ ( 'Please provide a valid email address.' , 'woocommerce' ), 'error' );
2014-02-07 18:35:30 +00:00
} elseif ( email_exists ( $account_email ) && $account_email !== $current_user -> user_email ) {
2013-11-13 04:29:03 +00:00
wc_add_notice ( __ ( 'This email address is already registered.' , 'woocommerce' ), 'error' );
2014-02-07 18:35:30 +00:00
}
2013-08-09 16:11:15 +00:00
2014-04-22 19:01:57 +00:00
if ( ! empty ( $pass1 ) && ! wp_check_password ( $pass_cur , $current_user -> user_pass , $current_user -> ID ) ) {
wc_add_notice ( __ ( 'Your current password is incorrect.' , 'woocommerce' ), 'error' );
$save_pass = false ;
}
if ( ! empty ( $pass_cur ) && empty ( $pass1 ) && empty ( $pass2 ) ) {
wc_add_notice ( __ ( 'Please fill out all password fields.' , 'woocommerce' ), 'error' );
$save_pass = false ;
} elseif ( ! empty ( $pass1 ) && empty ( $pass_cur ) ) {
wc_add_notice ( __ ( 'Please enter your current password.' , 'woocommerce' ), 'error' );
$save_pass = false ;
} elseif ( ! empty ( $pass1 ) && empty ( $pass2 ) ) {
2013-11-13 04:29:03 +00:00
wc_add_notice ( __ ( 'Please re-enter your password.' , 'woocommerce' ), 'error' );
2014-04-22 19:01:57 +00:00
$save_pass = false ;
2014-02-07 18:35:30 +00:00
} elseif ( ! empty ( $pass1 ) && $pass1 !== $pass2 ) {
2013-11-13 04:29:03 +00:00
wc_add_notice ( __ ( 'Passwords do not match.' , 'woocommerce' ), 'error' );
2014-04-22 19:01:57 +00:00
$save_pass = false ;
}
if ( $pass1 && $save_pass ) {
$user -> user_pass = $pass1 ;
2014-02-07 18:35:30 +00:00
}
2013-08-09 16:11:15 +00:00
// Allow plugins to return their own errors.
do_action_ref_array ( 'user_profile_update_errors' , array ( & $errors , $update , & $user ) );
2014-02-07 18:35:30 +00:00
if ( $errors -> get_error_messages () ) {
foreach ( $errors -> get_error_messages () as $error ) {
2013-11-13 04:29:03 +00:00
wc_add_notice ( $error , 'error' );
2014-02-07 18:35:30 +00:00
}
}
2013-08-09 16:11:15 +00:00
2014-04-22 19:01:57 +00:00
if ( wc_notice_count ( 'error' ) === 0 ) {
2013-08-09 16:11:15 +00:00
wp_update_user ( $user ) ;
2013-11-13 04:32:29 +00:00
wc_add_notice ( __ ( 'Account details changed successfully.' , 'woocommerce' ) );
2013-08-09 16:11:15 +00:00
do_action ( 'woocommerce_save_account_details' , $user -> ID );
2013-11-25 14:07:22 +00:00
wp_safe_redirect ( get_permalink ( wc_get_page_id ( 'myaccount' ) ) );
2013-08-09 16:11:15 +00:00
exit ;
}
}
/**
* Process the checkout form .
*/
2014-05-28 13:52:50 +00:00
public static function checkout_action () {
2013-08-09 16:11:15 +00:00
if ( isset ( $_POST [ 'woocommerce_checkout_place_order' ] ) || isset ( $_POST [ 'woocommerce_checkout_update_totals' ] ) ) {
if ( sizeof ( WC () -> cart -> get_cart () ) == 0 ) {
2013-11-25 14:07:22 +00:00
wp_redirect ( get_permalink ( wc_get_page_id ( 'cart' ) ) );
2013-08-09 16:11:15 +00:00
exit ;
}
2014-02-07 18:35:30 +00:00
if ( ! defined ( 'WOOCOMMERCE_CHECKOUT' ) ) {
2013-08-09 16:11:15 +00:00
define ( 'WOOCOMMERCE_CHECKOUT' , true );
2014-02-07 18:35:30 +00:00
}
2013-08-09 16:11:15 +00:00
2014-05-28 13:52:50 +00:00
WC () -> checkout () -> process_checkout ();
2013-08-09 16:11:15 +00:00
}
}
/**
* Process the pay form .
*/
2014-05-28 13:52:50 +00:00
public static function pay_action () {
2013-08-09 16:11:15 +00:00
global $wp ;
2014-03-06 13:11:20 +00:00
if ( isset ( $_POST [ 'woocommerce_pay' ] ) && isset ( $_POST [ '_wpnonce' ] ) && wp_verify_nonce ( $_POST [ '_wpnonce' ], 'woocommerce-pay' ) ) {
2013-08-09 16:11:15 +00:00
ob_start ();
// Pay for existing order
2014-04-17 16:27:39 +00:00
$order_key = $_GET [ 'key' ];
$order_id = absint ( $wp -> query_vars [ 'order-pay' ] );
2014-08-15 12:29:21 +00:00
$order = wc_get_order ( $order_id );
2014-04-24 21:18:41 +00:00
2014-04-17 16:27:39 +00:00
$valid_order_statuses = apply_filters ( 'woocommerce_valid_order_statuses_for_payment' , array ( 'pending' , 'failed' ), $order );
2013-08-09 16:11:15 +00:00
2014-06-03 09:45:33 +00:00
if ( $order -> id == $order_id && $order -> order_key == $order_key && $order -> has_status ( $valid_order_statuses ) ) {
2013-08-09 16:11:15 +00:00
// Set customer location to order location
2014-02-07 18:35:30 +00:00
if ( $order -> billing_country ) {
2013-08-09 16:11:15 +00:00
WC () -> customer -> set_country ( $order -> billing_country );
2014-02-07 18:35:30 +00:00
}
if ( $order -> billing_state ) {
2013-08-09 16:11:15 +00:00
WC () -> customer -> set_state ( $order -> billing_state );
2014-02-07 18:35:30 +00:00
}
if ( $order -> billing_postcode ) {
2013-08-09 16:11:15 +00:00
WC () -> customer -> set_postcode ( $order -> billing_postcode );
2014-02-07 18:35:30 +00:00
}
if ( $order -> billing_city ) {
2013-08-09 16:11:15 +00:00
WC () -> customer -> set_city ( $order -> billing_city );
2014-02-07 18:35:30 +00:00
}
2013-08-09 16:11:15 +00:00
// Update payment method
2013-10-16 13:14:15 +00:00
if ( $order -> needs_payment () ) {
2013-11-25 13:34:21 +00:00
$payment_method = wc_clean ( $_POST [ 'payment_method' ] );
2013-08-09 16:11:15 +00:00
$available_gateways = WC () -> payment_gateways -> get_available_payment_gateways ();
// Update meta
update_post_meta ( $order_id , '_payment_method' , $payment_method );
2014-02-07 18:35:30 +00:00
if ( isset ( $available_gateways [ $payment_method ] ) ) {
2013-08-09 16:11:15 +00:00
$payment_method_title = $available_gateways [ $payment_method ] -> get_title ();
2014-02-07 18:35:30 +00:00
}
2013-08-09 16:11:15 +00:00
update_post_meta ( $order_id , '_payment_method_title' , $payment_method_title );
// Validate
$available_gateways [ $payment_method ] -> validate_fields ();
// Process
2013-11-27 16:15:53 +00:00
if ( wc_notice_count ( 'error' ) == 0 ) {
2013-08-09 16:11:15 +00:00
$result = $available_gateways [ $payment_method ] -> process_payment ( $order_id );
// Redirect to success/confirmation/payment page
2014-02-07 18:35:30 +00:00
if ( 'success' == $result [ 'result' ] ) {
2013-08-09 16:11:15 +00:00
wp_redirect ( $result [ 'redirect' ] );
exit ;
}
}
} else {
// No payment was required for order
$order -> payment_complete ();
wp_safe_redirect ( $order -> get_checkout_order_received_url () );
exit ;
}
}
}
}
2013-11-01 17:08:44 +00:00
/**
* Process the add payment method form .
*/
2014-05-28 13:52:50 +00:00
public static function add_payment_method_action () {
2014-03-06 13:11:20 +00:00
if ( isset ( $_POST [ 'woocommerce_add_payment_method' ] ) && isset ( $_POST [ '_wpnonce' ] ) && wp_verify_nonce ( $_POST [ '_wpnonce' ], 'woocommerce-add-payment-method' ) ) {
2013-11-01 17:08:44 +00:00
ob_start ();
2013-11-25 13:34:21 +00:00
$payment_method = wc_clean ( $_POST [ 'payment_method' ] );
2013-11-01 17:08:44 +00:00
$available_gateways = WC () -> payment_gateways -> get_available_payment_gateways ();
// Validate
$available_gateways [ $payment_method ] -> validate_fields ();
// Process
if ( wc_error_count () == 0 ) {
$result = $available_gateways [ $payment_method ] -> add_payment_method ();
// Redirect to success/confirmation/payment page
if ( $result [ 'result' ] == 'success' ) {
2013-11-01 21:14:14 +00:00
wc_add_message ( __ ( 'Payment method added.' , 'woocommerce' ) );
2013-11-05 04:54:18 +00:00
wp_redirect ( $result [ 'redirect' ] );
2013-11-01 21:14:00 +00:00
exit ();
2013-11-01 17:08:44 +00:00
}
}
}
}
2013-08-09 16:11:15 +00:00
/**
* Remove from cart / update .
*/
2014-05-28 13:52:50 +00:00
public static function update_cart_action () {
2013-12-31 14:02:50 +00:00
// Add Discount
if ( ! empty ( $_POST [ 'apply_coupon' ] ) && ! empty ( $_POST [ 'coupon_code' ] ) ) {
WC () -> cart -> add_discount ( sanitize_text_field ( $_POST [ 'coupon_code' ] ) );
}
// Remove Coupon Codes
elseif ( isset ( $_GET [ 'remove_coupon' ] ) ) {
WC () -> cart -> remove_coupon ( wc_clean ( $_GET [ 'remove_coupon' ] ) );
}
2013-08-09 16:11:15 +00:00
// Remove from cart
2014-03-06 13:11:20 +00:00
elseif ( ! empty ( $_GET [ 'remove_item' ] ) && isset ( $_GET [ '_wpnonce' ] ) && wp_verify_nonce ( $_GET [ '_wpnonce' ], 'woocommerce-cart' ) ) {
2013-08-09 16:11:15 +00:00
WC () -> cart -> set_quantity ( $_GET [ 'remove_item' ], 0 );
2013-11-13 04:32:29 +00:00
wc_add_notice ( __ ( 'Cart updated.' , 'woocommerce' ) );
2013-08-09 16:11:15 +00:00
2014-09-19 03:24:31 +00:00
$referer = wp_get_referer () ? remove_query_arg ( array ( 'add-to-cart' , 'remove_item' ), wp_get_referer () ) : WC () -> cart -> get_cart_url ();
2013-08-09 16:11:15 +00:00
wp_safe_redirect ( $referer );
exit ;
2013-12-31 14:02:50 +00:00
}
// Update Cart - checks apply_coupon too because they are in the same form
2014-03-06 13:11:20 +00:00
if ( ( ! empty ( $_POST [ 'apply_coupon' ] ) || ! empty ( $_POST [ 'update_cart' ] ) || ! empty ( $_POST [ 'proceed' ] ) ) && isset ( $_POST [ '_wpnonce' ] ) && wp_verify_nonce ( $_POST [ '_wpnonce' ], 'woocommerce-cart' ) ) {
2013-08-09 16:11:15 +00:00
2013-12-31 14:02:50 +00:00
$cart_updated = false ;
$cart_totals = isset ( $_POST [ 'cart' ] ) ? $_POST [ 'cart' ] : '' ;
2013-08-09 16:11:15 +00:00
if ( sizeof ( WC () -> cart -> get_cart () ) > 0 ) {
foreach ( WC () -> cart -> get_cart () as $cart_item_key => $values ) {
$_product = $values [ 'data' ];
// Skip product if no updated quantity was posted
2014-02-07 18:35:30 +00:00
if ( ! isset ( $cart_totals [ $cart_item_key ][ 'qty' ] ) ) {
2013-08-09 16:11:15 +00:00
continue ;
2014-02-07 18:35:30 +00:00
}
2013-08-09 16:11:15 +00:00
// Sanitize
2014-06-25 10:25:28 +00:00
$quantity = apply_filters ( 'woocommerce_stock_amount_cart_item' , wc_stock_amount ( preg_replace ( " /[^0-9 \ .]/ " , '' , $cart_totals [ $cart_item_key ][ 'qty' ] ) ), $cart_item_key );
2013-08-09 16:11:15 +00:00
2014-02-07 18:35:30 +00:00
if ( '' === $quantity || $quantity == $values [ 'quantity' ] )
2013-08-09 16:11:15 +00:00
continue ;
// Update cart validation
2014-02-07 18:27:07 +00:00
$passed_validation = apply_filters ( 'woocommerce_update_cart_validation' , true , $cart_item_key , $values , $quantity );
2013-08-09 16:11:15 +00:00
2014-02-07 18:27:07 +00:00
// is_sold_individually
2013-08-09 16:11:15 +00:00
if ( $_product -> is_sold_individually () && $quantity > 1 ) {
2013-11-13 04:29:03 +00:00
wc_add_notice ( sprintf ( __ ( 'You can only have 1 %s in your cart.' , 'woocommerce' ), $_product -> get_title () ), 'error' );
2013-08-09 16:11:15 +00:00
$passed_validation = false ;
}
2014-02-07 18:35:30 +00:00
if ( $passed_validation ) {
2014-02-07 18:27:07 +00:00
WC () -> cart -> set_quantity ( $cart_item_key , $quantity , false );
2014-02-07 18:35:30 +00:00
}
2013-08-09 16:11:15 +00:00
2014-02-07 18:27:07 +00:00
$cart_updated = true ;
2013-08-09 16:11:15 +00:00
}
2014-02-26 12:14:50 +00:00
}
2014-03-03 15:15:32 +00:00
// Trigger action - let 3rd parties update the cart if they need to and update the $cart_updated variable
$cart_updated = apply_filters ( 'woocommerce_update_cart_action_cart_updated' , $cart_updated );
2013-08-19 14:19:44 +00:00
2014-03-03 15:15:32 +00:00
if ( $cart_updated ) {
2014-02-26 12:14:50 +00:00
// Recalc our totals
2013-08-19 14:19:44 +00:00
WC () -> cart -> calculate_totals ();
2013-08-09 16:11:15 +00:00
}
if ( ! empty ( $_POST [ 'proceed' ] ) ) {
wp_safe_redirect ( WC () -> cart -> get_checkout_url () );
exit ;
2013-12-31 14:02:50 +00:00
} elseif ( $cart_updated ) {
2013-11-13 04:32:29 +00:00
wc_add_notice ( __ ( 'Cart updated.' , 'woocommerce' ) );
2014-02-26 12:14:50 +00:00
$referer = remove_query_arg ( 'remove_coupon' , ( wp_get_referer () ? wp_get_referer () : WC () -> cart -> get_cart_url () ) );
2013-08-09 16:11:15 +00:00
wp_safe_redirect ( $referer );
exit ;
}
}
}
/**
* Place a previous order again .
*/
2014-05-28 13:52:50 +00:00
public static function order_again () {
2013-08-09 16:11:15 +00:00
// Nothing to do
2014-03-06 13:11:20 +00:00
if ( ! isset ( $_GET [ 'order_again' ] ) || ! is_user_logged_in () || ! isset ( $_GET [ '_wpnonce' ] ) || ! wp_verify_nonce ( $_GET [ '_wpnonce' ], 'woocommerce-order_again' ) ) {
2013-08-09 16:11:15 +00:00
return ;
2014-02-07 18:35:30 +00:00
}
2013-08-09 16:11:15 +00:00
// Clear current cart
2013-11-25 14:01:32 +00:00
WC () -> cart -> empty_cart ();
2013-08-09 16:11:15 +00:00
// Load the previous order - Stop if the order does not exist
2014-08-15 12:29:21 +00:00
$order = wc_get_order ( absint ( $_GET [ 'order_again' ] ) );
2013-08-09 16:11:15 +00:00
2014-02-07 18:35:30 +00:00
if ( empty ( $order -> id ) ) {
2013-08-09 16:11:15 +00:00
return ;
2014-02-07 18:35:30 +00:00
}
2013-08-09 16:11:15 +00:00
2014-06-03 09:45:33 +00:00
if ( ! $order -> has_status ( 'completed' ) ) {
2013-08-09 16:11:15 +00:00
return ;
2014-02-07 18:35:30 +00:00
}
2013-08-09 16:11:15 +00:00
2013-09-13 15:37:26 +00:00
// Make sure the user is allowed to order again. By default it check if the
2013-09-06 14:39:45 +00:00
// previous order belonged to the current user.
2014-02-07 18:35:30 +00:00
if ( ! current_user_can ( 'order_again' , $order -> id ) ) {
2013-08-09 16:11:15 +00:00
return ;
2014-02-07 18:35:30 +00:00
}
2013-08-09 16:11:15 +00:00
// Copy products from the order to the cart
foreach ( $order -> get_items () as $item ) {
// Load all product info including variation data
$product_id = ( int ) apply_filters ( 'woocommerce_add_to_cart_product_id' , $item [ 'product_id' ] );
$quantity = ( int ) $item [ 'qty' ];
$variation_id = ( int ) $item [ 'variation_id' ];
$variations = array ();
$cart_item_data = apply_filters ( 'woocommerce_order_again_cart_item_data' , array (), $item , $order );
foreach ( $item [ 'item_meta' ] as $meta_name => $meta_value ) {
2013-12-02 11:09:30 +00:00
if ( taxonomy_is_product_attribute ( $meta_name ) ) {
2013-08-09 16:11:15 +00:00
$variations [ $meta_name ] = $meta_value [ 0 ];
2014-02-07 18:35:30 +00:00
} elseif ( meta_is_product_attribute ( $meta_name , $meta_value , $product_id ) ) {
2013-08-20 11:17:51 +00:00
$variations [ $meta_name ] = $meta_value [ 0 ];
2013-12-02 11:09:30 +00:00
}
2013-08-09 16:11:15 +00:00
}
// Add to cart validation
2014-02-07 18:35:30 +00:00
if ( ! apply_filters ( 'woocommerce_add_to_cart_validation' , true , $product_id , $quantity , $variation_id , $variations , $cart_item_data ) ) {
continue ;
}
2013-08-09 16:11:15 +00:00
2013-11-25 14:01:32 +00:00
WC () -> cart -> add_to_cart ( $product_id , $quantity , $variation_id , $variations , $cart_item_data );
2013-08-09 16:11:15 +00:00
}
do_action ( 'woocommerce_ordered_again' , $order -> id );
// Redirect to cart
2013-11-13 04:32:29 +00:00
wc_add_notice ( __ ( 'The cart has been filled with the items from your previous order.' , 'woocommerce' ) );
2013-11-25 14:01:32 +00:00
wp_safe_redirect ( WC () -> cart -> get_cart_url () );
2013-08-09 16:11:15 +00:00
exit ;
}
/**
* Cancel a pending order .
*/
2014-05-28 13:52:50 +00:00
public static function cancel_order () {
2013-12-03 14:07:11 +00:00
if ( isset ( $_GET [ 'cancel_order' ] ) && isset ( $_GET [ 'order' ] ) && isset ( $_GET [ 'order_id' ] ) ) {
2013-08-09 16:11:15 +00:00
2014-01-26 09:19:17 +00:00
$order_key = $_GET [ 'order' ];
2013-12-03 14:07:11 +00:00
$order_id = absint ( $_GET [ 'order_id' ] );
2014-08-15 12:29:21 +00:00
$order = wc_get_order ( $order_id );
2013-12-03 14:07:11 +00:00
$user_can_cancel = current_user_can ( 'cancel_order' , $order_id );
2014-06-03 09:45:33 +00:00
$order_can_cancel = $order -> has_status ( apply_filters ( 'woocommerce_valid_order_statuses_for_cancel' , array ( 'pending' , 'failed' ) ) );
2014-01-26 09:19:17 +00:00
$redirect = $_GET [ 'redirect' ];
2013-08-09 16:11:15 +00:00
2014-06-03 09:45:33 +00:00
if ( $order -> has_status ( 'cancelled' ) ) {
2014-02-24 10:39:43 +00:00
// Already cancelled - take no action
2014-03-06 13:11:20 +00:00
} elseif ( $user_can_cancel && $order_can_cancel && $order -> id == $order_id && $order -> order_key == $order_key && isset ( $_GET [ '_wpnonce' ] ) && wp_verify_nonce ( $_GET [ '_wpnonce' ], 'woocommerce-cancel_order' ) ) {
2013-08-09 16:11:15 +00:00
// Cancel the order + restore stock
$order -> cancel_order ( __ ( 'Order cancelled by customer.' , 'woocommerce' ) );
// Message
2014-03-26 09:58:51 +00:00
wc_add_notice ( apply_filters ( 'woocommerce_order_cancelled_notice' , __ ( 'Your order was cancelled.' , 'woocommerce' ) ), apply_filters ( 'woocommerce_order_cancelled_notice_type' , 'notice' ) );
2013-08-09 16:11:15 +00:00
do_action ( 'woocommerce_cancelled_order' , $order -> id );
2013-12-03 14:07:11 +00:00
} elseif ( $user_can_cancel && ! $order_can_cancel ) {
wc_add_notice ( __ ( 'Your order can no longer be cancelled. Please contact us if you need assistance.' , 'woocommerce' ), 'error' );
} else {
2013-11-13 04:29:03 +00:00
wc_add_notice ( __ ( 'Invalid order.' , 'woocommerce' ), 'error' );
2013-12-03 14:07:11 +00:00
}
2013-08-09 16:11:15 +00:00
2013-12-30 14:09:46 +00:00
if ( $redirect ) {
wp_safe_redirect ( $redirect );
exit ;
}
2013-12-03 14:07:11 +00:00
}
2013-08-09 16:11:15 +00:00
}
/**
* Add to cart action
*
* Checks for a valid request , does validation ( via hooks ) and then redirects if valid .
*
* @ param bool $url ( default : false )
*/
2014-05-28 13:52:50 +00:00
public static function add_to_cart_action ( $url = false ) {
2014-02-07 18:35:30 +00:00
if ( empty ( $_REQUEST [ 'add-to-cart' ] ) || ! is_numeric ( $_REQUEST [ 'add-to-cart' ] ) ) {
2013-08-09 16:11:15 +00:00
return ;
2014-02-07 18:35:30 +00:00
}
2013-08-09 16:11:15 +00:00
$product_id = apply_filters ( 'woocommerce_add_to_cart_product_id' , absint ( $_REQUEST [ 'add-to-cart' ] ) );
$was_added_to_cart = false ;
$added_to_cart = array ();
2014-08-19 10:09:29 +00:00
$adding_to_cart = wc_get_product ( $product_id );
2013-08-09 16:11:15 +00:00
$add_to_cart_handler = apply_filters ( 'woocommerce_add_to_cart_handler' , $adding_to_cart -> product_type , $adding_to_cart );
2014-02-07 18:27:07 +00:00
// Variable product handling
if ( 'variable' === $add_to_cart_handler ) {
2013-08-09 16:11:15 +00:00
2014-02-07 18:27:07 +00:00
$variation_id = empty ( $_REQUEST [ 'variation_id' ] ) ? '' : absint ( $_REQUEST [ 'variation_id' ] );
2014-06-25 10:25:28 +00:00
$quantity = empty ( $_REQUEST [ 'quantity' ] ) ? 1 : wc_stock_amount ( $_REQUEST [ 'quantity' ] );
2014-02-07 18:27:07 +00:00
$all_variations_set = true ;
$variations = array ();
2013-08-09 16:11:15 +00:00
// Only allow integer variation ID - if its not set, redirect to the product page
if ( empty ( $variation_id ) ) {
2013-11-13 04:29:03 +00:00
wc_add_notice ( __ ( 'Please choose product options…' , 'woocommerce' ), 'error' );
2013-08-09 16:11:15 +00:00
return ;
}
$attributes = $adding_to_cart -> get_attributes ();
2014-08-19 10:09:29 +00:00
$variation = wc_get_product ( $variation_id );
2013-08-09 16:11:15 +00:00
// Verify all attributes
foreach ( $attributes as $attribute ) {
2014-02-07 18:35:30 +00:00
if ( ! $attribute [ 'is_variation' ] ) {
2014-02-07 18:27:07 +00:00
continue ;
2014-02-07 18:35:30 +00:00
}
2014-02-07 18:27:07 +00:00
$taxonomy = 'attribute_' . sanitize_title ( $attribute [ 'name' ] );
if ( isset ( $_REQUEST [ $taxonomy ] ) ) {
// Get value from post data
// Don't use wc_clean as it destroys sanitized characters
$value = sanitize_title ( trim ( stripslashes ( $_REQUEST [ $taxonomy ] ) ) );
// Get valid value from variation
$valid_value = $variation -> variation_data [ $taxonomy ];
// Allow if valid
if ( $valid_value == '' || $valid_value == $value ) {
2014-02-07 18:35:30 +00:00
if ( $attribute [ 'is_taxonomy' ] ) {
2014-02-07 18:27:07 +00:00
$variations [ $taxonomy ] = $value ;
2014-02-07 18:35:30 +00:00
}
2014-02-07 18:27:07 +00:00
else {
// For custom attributes, get the name from the slug
$options = array_map ( 'trim' , explode ( WC_DELIMITER , $attribute [ 'value' ] ) );
foreach ( $options as $option ) {
if ( sanitize_title ( $option ) == $value ) {
$value = $option ;
break ;
}
}
$variations [ $taxonomy ] = $value ;
}
continue ;
}
2013-08-09 16:11:15 +00:00
}
2014-02-07 18:27:07 +00:00
$all_variations_set = false ;
}
2013-08-09 16:11:15 +00:00
2014-02-07 18:27:07 +00:00
if ( $all_variations_set ) {
// Add to cart validation
$passed_validation = apply_filters ( 'woocommerce_add_to_cart_validation' , true , $product_id , $quantity , $variation_id , $variations );
2013-08-09 16:11:15 +00:00
2014-02-07 18:27:07 +00:00
if ( $passed_validation ) {
2013-08-09 16:11:15 +00:00
if ( WC () -> cart -> add_to_cart ( $product_id , $quantity , $variation_id , $variations ) ) {
2013-11-25 12:44:32 +00:00
wc_add_to_cart_message ( $product_id );
2013-08-09 16:11:15 +00:00
$was_added_to_cart = true ;
$added_to_cart [] = $product_id ;
}
}
2014-02-07 18:27:07 +00:00
} else {
wc_add_notice ( __ ( 'Please choose product options…' , 'woocommerce' ), 'error' );
return ;
2013-11-18 13:35:38 +00:00
}
2013-08-09 16:11:15 +00:00
2014-02-07 18:27:07 +00:00
// Grouped Products
} elseif ( 'grouped' === $add_to_cart_handler ) {
2013-08-09 16:11:15 +00:00
if ( ! empty ( $_REQUEST [ 'quantity' ] ) && is_array ( $_REQUEST [ 'quantity' ] ) ) {
$quantity_set = false ;
foreach ( $_REQUEST [ 'quantity' ] as $item => $quantity ) {
2014-02-07 18:35:30 +00:00
if ( $quantity <= 0 ) {
2013-08-09 16:11:15 +00:00
continue ;
2014-02-07 18:35:30 +00:00
}
2013-08-09 16:11:15 +00:00
$quantity_set = true ;
// Add to cart validation
$passed_validation = apply_filters ( 'woocommerce_add_to_cart_validation' , true , $item , $quantity );
if ( $passed_validation ) {
if ( WC () -> cart -> add_to_cart ( $item , $quantity ) ) {
$was_added_to_cart = true ;
$added_to_cart [] = $item ;
}
}
}
if ( $was_added_to_cart ) {
2013-11-25 12:44:32 +00:00
wc_add_to_cart_message ( $added_to_cart );
2013-08-09 16:11:15 +00:00
}
if ( ! $was_added_to_cart && ! $quantity_set ) {
2013-11-13 04:29:03 +00:00
wc_add_notice ( __ ( 'Please choose the quantity of items you wish to add to your cart…' , 'woocommerce' ), 'error' );
2013-08-09 16:11:15 +00:00
return ;
}
} elseif ( $product_id ) {
/* Link on product archives */
2013-11-13 04:29:03 +00:00
wc_add_notice ( __ ( 'Please choose a product to add to your cart…' , 'woocommerce' ), 'error' );
2013-08-09 16:11:15 +00:00
return ;
}
// Simple Products
2014-02-07 18:27:07 +00:00
} else {
2013-08-09 16:11:15 +00:00
2014-06-25 10:25:28 +00:00
$quantity = empty ( $_REQUEST [ 'quantity' ] ) ? 1 : wc_stock_amount ( $_REQUEST [ 'quantity' ] );
2013-08-09 16:11:15 +00:00
// Add to cart validation
$passed_validation = apply_filters ( 'woocommerce_add_to_cart_validation' , true , $product_id , $quantity );
if ( $passed_validation ) {
2014-02-07 18:27:07 +00:00
// Add the product to the cart
if ( WC () -> cart -> add_to_cart ( $product_id , $quantity ) ) {
wc_add_to_cart_message ( $product_id );
$was_added_to_cart = true ;
$added_to_cart [] = $product_id ;
}
2013-08-09 16:11:15 +00:00
}
2014-02-07 18:27:07 +00:00
}
2013-08-09 16:11:15 +00:00
2014-02-07 18:27:07 +00:00
// If we added the product to the cart we can now optionally do a redirect.
if ( $was_added_to_cart && wc_notice_count ( 'error' ) == 0 ) {
2013-08-09 16:11:15 +00:00
$url = apply_filters ( 'add_to_cart_redirect' , $url );
// If has custom URL redirect there
if ( $url ) {
wp_safe_redirect ( $url );
exit ;
}
// Redirect to cart option
elseif ( get_option ( 'woocommerce_cart_redirect_after_add' ) == 'yes' ) {
wp_safe_redirect ( WC () -> cart -> get_cart_url () );
exit ;
}
2014-02-07 18:27:07 +00:00
}
2013-08-09 16:11:15 +00:00
}
/**
* Process the login form .
*/
2014-05-28 13:52:50 +00:00
public static function process_login () {
2014-03-06 11:37:48 +00:00
if ( ! empty ( $_POST [ 'login' ] ) && ! empty ( $_POST [ '_wpnonce' ] ) ) {
2013-08-09 16:11:15 +00:00
wp_verify_nonce ( $_POST [ '_wpnonce' ], 'woocommerce-login' );
try {
2013-11-13 11:49:34 +00:00
$creds = array ();
2013-12-29 13:11:54 +00:00
2013-11-13 11:49:34 +00:00
$validation_error = new WP_Error ();
$validation_error = apply_filters ( 'woocommerce_process_login_errors' , $validation_error , $_POST [ 'username' ], $_POST [ 'password' ] );
2014-01-03 11:06:28 +00:00
if ( $validation_error -> get_error_code () ) {
2013-11-13 11:49:34 +00:00
throw new Exception ( '<strong>' . __ ( 'Error' , 'woocommerce' ) . ':</strong> ' . $validation_error -> get_error_message () );
2014-01-03 11:06:28 +00:00
}
2013-08-09 16:11:15 +00:00
2014-01-03 11:06:28 +00:00
if ( empty ( $_POST [ 'username' ] ) ) {
2013-08-09 16:11:15 +00:00
throw new Exception ( '<strong>' . __ ( 'Error' , 'woocommerce' ) . ':</strong> ' . __ ( 'Username is required.' , 'woocommerce' ) );
2014-01-03 11:06:28 +00:00
}
2013-11-13 11:49:34 +00:00
2014-01-03 11:06:28 +00:00
if ( empty ( $_POST [ 'password' ] ) ) {
2013-08-09 16:11:15 +00:00
throw new Exception ( '<strong>' . __ ( 'Error' , 'woocommerce' ) . ':</strong> ' . __ ( 'Password is required.' , 'woocommerce' ) );
2014-01-03 11:06:28 +00:00
}
2013-08-09 16:11:15 +00:00
2014-02-14 11:47:12 +00:00
if ( is_email ( $_POST [ 'username' ] ) && apply_filters ( 'woocommerce_get_username_from_email' , true ) ) {
2013-08-09 16:11:15 +00:00
$user = get_user_by ( 'email' , $_POST [ 'username' ] );
2014-01-03 11:06:28 +00:00
if ( isset ( $user -> user_login ) ) {
2013-08-09 16:11:15 +00:00
$creds [ 'user_login' ] = $user -> user_login ;
2014-01-03 11:06:28 +00:00
} else {
2013-08-09 16:11:15 +00:00
throw new Exception ( '<strong>' . __ ( 'Error' , 'woocommerce' ) . ':</strong> ' . __ ( 'A user could not be found with this email address.' , 'woocommerce' ) );
2014-01-03 11:06:28 +00:00
}
2013-08-09 16:11:15 +00:00
} else {
$creds [ 'user_login' ] = $_POST [ 'username' ];
}
$creds [ 'user_password' ] = $_POST [ 'password' ];
2013-10-25 15:10:09 +00:00
$creds [ 'remember' ] = isset ( $_POST [ 'rememberme' ] );
2013-08-09 16:11:15 +00:00
$secure_cookie = is_ssl () ? true : false ;
2013-08-10 19:31:17 +00:00
$user = wp_signon ( apply_filters ( 'woocommerce_login_credentials' , $creds ), $secure_cookie );
2013-08-09 16:11:15 +00:00
if ( is_wp_error ( $user ) ) {
throw new Exception ( $user -> get_error_message () );
} else {
if ( ! empty ( $_POST [ 'redirect' ] ) ) {
2014-10-01 18:24:11 +00:00
$redirect = $_POST [ 'redirect' ];
2013-08-09 16:11:15 +00:00
} elseif ( wp_get_referer () ) {
2014-10-01 18:24:11 +00:00
$redirect = wp_get_referer ();
2013-08-09 16:11:15 +00:00
} else {
2014-10-01 18:24:11 +00:00
$redirect = get_permalink ( wc_get_page_id ( 'myaccount' ) );
2013-08-09 16:11:15 +00:00
}
// Feedback
2013-11-13 04:32:29 +00:00
wc_add_notice ( sprintf ( __ ( 'You are now logged in as <strong>%s</strong>' , 'woocommerce' ), $user -> display_name ) );
2013-08-09 16:11:15 +00:00
wp_redirect ( apply_filters ( 'woocommerce_login_redirect' , $redirect , $user ) );
exit ;
}
2014-01-03 11:06:28 +00:00
2013-08-09 16:11:15 +00:00
} catch ( Exception $e ) {
2013-11-13 04:29:03 +00:00
wc_add_notice ( apply_filters ( 'login_errors' , $e -> getMessage () ), 'error' );
2013-08-09 16:11:15 +00:00
}
}
}
/**
* Handle reset password form
*/
2014-05-28 13:52:50 +00:00
public static function process_reset_password () {
2014-02-07 18:35:30 +00:00
if ( ! isset ( $_POST [ 'wc_reset_password' ] ) ) {
2013-08-09 16:11:15 +00:00
return ;
2014-02-07 18:35:30 +00:00
}
2013-08-09 16:11:15 +00:00
// process lost password form
2014-03-06 13:11:20 +00:00
if ( isset ( $_POST [ 'user_login' ] ) && isset ( $_POST [ '_wpnonce' ] ) ) {
2013-08-09 16:11:15 +00:00
wp_verify_nonce ( $_POST [ '_wpnonce' ], 'woocommerce-lost_password' );
WC_Shortcode_My_Account :: retrieve_password ();
}
// process reset password form
2014-03-06 13:11:20 +00:00
if ( isset ( $_POST [ 'password_1' ] ) && isset ( $_POST [ 'password_2' ] ) && isset ( $_POST [ 'reset_key' ] ) && isset ( $_POST [ 'reset_login' ] ) && isset ( $_POST [ '_wpnonce' ] ) ) {
2013-08-09 16:11:15 +00:00
// verify reset key again
$user = WC_Shortcode_My_Account :: check_password_reset_key ( $_POST [ 'reset_key' ], $_POST [ 'reset_login' ] );
if ( is_object ( $user ) ) {
// save these values into the form again in case of errors
2013-11-25 13:34:21 +00:00
$args [ 'key' ] = wc_clean ( $_POST [ 'reset_key' ] );
$args [ 'login' ] = wc_clean ( $_POST [ 'reset_login' ] );
2013-08-09 16:11:15 +00:00
wp_verify_nonce ( $_POST [ '_wpnonce' ], 'woocommerce-reset_password' );
2014-02-07 18:35:30 +00:00
if ( empty ( $_POST [ 'password_1' ] ) || empty ( $_POST [ 'password_2' ] ) ) {
2013-11-13 04:29:03 +00:00
wc_add_notice ( __ ( 'Please enter your password.' , 'woocommerce' ), 'error' );
2013-08-09 16:11:15 +00:00
$args [ 'form' ] = 'reset_password' ;
}
2014-02-07 18:35:30 +00:00
if ( $_POST [ 'password_1' ] !== $_POST [ 'password_2' ] ) {
2013-11-13 04:29:03 +00:00
wc_add_notice ( __ ( 'Passwords do not match.' , 'woocommerce' ), 'error' );
2013-08-09 16:11:15 +00:00
$args [ 'form' ] = 'reset_password' ;
}
2013-10-18 01:24:06 +00:00
$errors = new WP_Error ();
do_action ( 'validate_password_reset' , $errors , $user );
2014-02-07 18:35:30 +00:00
if ( $errors -> get_error_messages () ) {
foreach ( $errors -> get_error_messages () as $error ) {
2013-11-13 04:29:03 +00:00
wc_add_notice ( $error , 'error' );
2014-02-07 18:35:30 +00:00
}
}
2013-10-18 01:24:06 +00:00
2014-02-07 18:35:30 +00:00
if ( 0 == wc_notice_count ( 'error' ) ) {
2013-08-09 16:11:15 +00:00
2014-04-14 06:29:22 +00:00
WC_Shortcode_My_Account :: reset_password ( $user , $_POST [ 'password_1' ] );
2013-08-09 16:11:15 +00:00
do_action ( 'woocommerce_customer_reset_password' , $user );
2014-02-26 13:13:53 +00:00
wp_redirect ( add_query_arg ( 'reset' , 'true' , remove_query_arg ( array ( 'key' , 'login' ) ) ) );
2013-08-09 16:11:15 +00:00
exit ;
}
}
}
}
/**
* Process the registration form .
*/
2014-05-28 13:52:50 +00:00
public static function process_registration () {
2013-08-09 16:11:15 +00:00
if ( ! empty ( $_POST [ 'register' ] ) ) {
2014-01-03 11:06:28 +00:00
wp_verify_nonce ( $_POST [ 'register' ], 'woocommerce-register' );
2014-04-02 02:45:25 +00:00
if ( 'no' === get_option ( 'woocommerce_registration_generate_username' ) ) {
2014-02-07 18:26:51 +00:00
$_username = $_POST [ 'username' ];
} else {
$_username = '' ;
}
2014-04-02 02:45:25 +00:00
if ( 'no' === get_option ( 'woocommerce_registration_generate_password' ) ) {
$_password = $_POST [ 'password' ];
} else {
$_password = '' ;
}
2014-01-03 11:06:28 +00:00
try {
$validation_error = new WP_Error ();
2014-04-02 02:45:25 +00:00
$validation_error = apply_filters ( 'woocommerce_process_registration_errors' , $validation_error , $_username , $_password , $_POST [ 'email' ] );
2014-01-03 11:06:28 +00:00
if ( $validation_error -> get_error_code () ) {
throw new Exception ( '<strong>' . __ ( 'Error' , 'woocommerce' ) . ':</strong> ' . $validation_error -> get_error_message () );
}
2013-08-09 16:11:15 +00:00
2014-01-03 11:06:28 +00:00
} catch ( Exception $e ) {
wc_add_notice ( $e -> getMessage (), 'error' );
return ;
2013-12-29 13:11:54 +00:00
}
2014-02-07 18:26:51 +00:00
$username = ! empty ( $_username ) ? wc_clean ( $_username ) : '' ;
2014-04-14 06:29:22 +00:00
$email = ! empty ( $_POST [ 'email' ] ) ? sanitize_email ( $_POST [ 'email' ] ) : '' ;
$password = $_password ;
2013-12-29 13:11:54 +00:00
2013-08-09 16:11:15 +00:00
// Anti-spam trap
if ( ! empty ( $_POST [ 'email_2' ] ) ) {
2013-11-13 04:29:03 +00:00
wc_add_notice ( '<strong>' . __ ( 'ERROR' , 'woocommerce' ) . '</strong>: ' . __ ( 'Anti-spam field was filled in.' , 'woocommerce' ), 'error' );
2013-08-09 16:11:15 +00:00
return ;
}
2013-11-25 12:52:53 +00:00
$new_customer = wc_create_new_customer ( $email , $username , $password );
2013-08-09 16:11:15 +00:00
if ( is_wp_error ( $new_customer ) ) {
2013-11-13 04:29:03 +00:00
wc_add_notice ( $new_customer -> get_error_message (), 'error' );
2013-08-09 16:11:15 +00:00
return ;
}
2013-11-25 12:52:53 +00:00
wc_set_customer_auth_cookie ( $new_customer );
2013-08-09 16:11:15 +00:00
// Redirect
if ( wp_get_referer () ) {
$redirect = esc_url ( wp_get_referer () );
} else {
2013-11-25 14:07:22 +00:00
$redirect = esc_url ( get_permalink ( wc_get_page_id ( 'myaccount' ) ) );
2013-08-09 16:11:15 +00:00
}
wp_redirect ( apply_filters ( 'woocommerce_registration_redirect' , $redirect ) );
exit ;
}
}
}
2014-05-28 13:52:50 +00:00
WC_Form_Handler :: init ();