Merge pull request #10995 from antoscarface/enhancements

Avoid leave query string on URL after delete and set default actions for token
This commit is contained in:
Mike Jolley 2016-05-26 16:22:05 +01:00
commit 0c4fbd84d6
4 changed files with 80 additions and 67 deletions

View File

@ -34,6 +34,8 @@ class WC_Form_Handler {
// May need $wp global to access query vars. // May need $wp global to access query vars.
add_action( 'wp', array( __CLASS__, 'pay_action' ), 20 ); add_action( 'wp', array( __CLASS__, 'pay_action' ), 20 );
add_action( 'wp', array( __CLASS__, 'add_payment_method_action' ), 20 ); add_action( 'wp', array( __CLASS__, 'add_payment_method_action' ), 20 );
add_action( 'wp', array( __CLASS__, 'delete_payment_method_action' ), 20 );
add_action( 'wp', array( __CLASS__, 'set_default_payment_method_action' ), 20 );
} }
/** /**
@ -378,6 +380,82 @@ class WC_Form_Handler {
} }
/**
* Process the delete payment method form.
*/
public static function delete_payment_method_action() {
global $wp;
if ( isset( $wp->query_vars['delete-payment-method'] ) ) {
$token_id = absint( $wp->query_vars['delete-payment-method'] );
$token = WC_Payment_Tokens::get( $token_id );
$delete = true;
if ( is_null( $token ) ) {
wc_add_notice( __( 'Invalid payment method', 'woocommerce' ), 'error' );
$delete = false;
}
if ( get_current_user_id() !== $token->get_user_id() ) {
wc_add_notice( __( 'Invalid payment method', 'woocommerce' ), 'error' );
$delete = false;
}
if ( false === wp_verify_nonce( $_REQUEST['_wpnonce'], 'delete-payment-method-' . $token_id ) ) {
wc_add_notice( __( 'Invalid payment method', 'woocommerce' ), 'error' );
$delete = false;
}
if ( $delete ) {
WC_Payment_Tokens::delete( $token_id );
wc_add_notice( __( 'Payment method deleted.', 'woocommerce' ) );
}
wp_redirect( wc_get_account_endpoint_url( 'payment-methods' ) );
exit();
}
}
/**
* Process the delete payment method form.
*/
public static function set_default_payment_method_action() {
global $wp;
if ( isset( $wp->query_vars['set-default-payment-method'] ) ) {
$token_id = absint( $wp->query_vars['set-default-payment-method'] );
$token = WC_Payment_Tokens::get( $token_id );
$delete = true;
if ( is_null( $token ) ) {
wc_add_notice( __( 'Invalid payment method', 'woocommerce' ), 'error' );
$delete = false;
}
if ( get_current_user_id() !== $token->get_user_id() ) {
wc_add_notice( __( 'Invalid payment method', 'woocommerce' ), 'error' );
$delete = false;
}
if ( false === wp_verify_nonce( $_REQUEST['_wpnonce'], 'set-default-payment-method-' . $token_id ) ) {
wc_add_notice( __( 'Invalid payment method', 'woocommerce' ), 'error' );
$delete = false;
}
if ( $delete ) {
WC_Payment_Tokens::set_users_default( $token->get_user_id(), intval( $token_id ) );
wc_add_notice( __( 'This payment method was successfully set as your default.', 'woocommerce' ) );
}
wp_redirect( wc_get_account_endpoint_url( 'payment-methods' ) );
exit();
}
}
/** /**
* Remove from cart/update. * Remove from cart/update.
*/ */

View File

@ -204,6 +204,8 @@ class WC_Payment_Tokens {
array( 'is_default' => 1 ), array( 'is_default' => 1 ),
array( 'token_id' => $token->get_id(), array( 'token_id' => $token->get_id(),
) ); ) );
do_action( 'woocommerce_payment_token_set_default', $token_id, $token );
} else { } else {
$token->set_default( false ); $token->set_default( false );
$wpdb->update( $wpdb->update(
@ -213,7 +215,6 @@ class WC_Payment_Tokens {
) ); ) );
} }
} }
do_action( 'woocommerce_payment_token_set_default', $token_id );
} }
/** /**

View File

@ -357,68 +357,4 @@ class WC_Shortcode_My_Account {
} }
/**
* Deletes a payment method from a users list and displays a message to the user
*
* @since 2.6
* @param int $id Payment Token ID
*/
public static function delete_payment_method( $id ) {
$token = WC_Payment_Tokens::get( $id );
if ( is_null( $token ) ) {
wc_add_notice( __( 'Invalid payment method', 'woocommerce' ), 'error' );
woocommerce_account_payment_methods();
return false;
}
if ( get_current_user_id() !== $token->get_user_id() ) {
wc_add_notice( __( 'Invalid payment method', 'woocommerce' ), 'error' );
woocommerce_account_payment_methods();
return false;
}
if ( false === wp_verify_nonce( $_REQUEST['_wpnonce'], 'delete-payment-method-' . $id ) ) {
wc_add_notice( __( 'Invalid payment method', 'woocommerce' ), 'error' );
woocommerce_account_payment_methods();
return false;
}
WC_Payment_Tokens::delete( $id );
wc_add_notice( __( 'Payment method deleted.', 'woocommerce' ) );
woocommerce_account_payment_methods();
}
/**
* Sets a payment method as default and displays a message to the user
*
* @since 2.6
* @param int $id Payment Token ID
*/
public static function set_default_payment_method( $id ) {
$token = WC_Payment_Tokens::get( $id );
if ( is_null( $token ) ) {
wc_add_notice( __( 'Invalid payment method', 'woocommerce' ), 'error' );
woocommerce_account_payment_methods();
return false;
}
if ( get_current_user_id() !== $token->get_user_id() ) {
wc_add_notice( __( 'Invalid payment method', 'woocommerce' ), 'error' );
woocommerce_account_payment_methods();
return false;
}
if ( false === wp_verify_nonce( $_REQUEST['_wpnonce'], 'set-default-payment-method-' . $id ) ) {
wc_add_notice( __( 'Invalid payment method', 'woocommerce' ), 'error' );
woocommerce_account_payment_methods();
return false;
}
WC_Payment_Tokens::set_users_default( $token->get_user_id(), intval( $id ) );
wc_add_notice( __( 'This payment method was successfully set as your default.', 'woocommerce' ) );
woocommerce_account_payment_methods();
}
} }

View File

@ -263,5 +263,3 @@ add_action( 'woocommerce_account_edit-address_endpoint', 'woocommerce_account_ed
add_action( 'woocommerce_account_payment-methods_endpoint', 'woocommerce_account_payment_methods' ); add_action( 'woocommerce_account_payment-methods_endpoint', 'woocommerce_account_payment_methods' );
add_action( 'woocommerce_account_add-payment-method_endpoint', 'woocommerce_account_add_payment_method' ); add_action( 'woocommerce_account_add-payment-method_endpoint', 'woocommerce_account_add_payment_method' );
add_action( 'woocommerce_account_edit-account_endpoint', 'woocommerce_account_edit_account' ); add_action( 'woocommerce_account_edit-account_endpoint', 'woocommerce_account_edit_account' );
add_action( 'woocommerce_account_set-default-payment-method_endpoint', array( 'WC_Shortcode_My_Account', 'set_default_payment_method' ) );
add_action( 'woocommerce_account_delete-payment-method_endpoint', array( 'WC_Shortcode_My_Account', 'delete_payment_method' ) );