1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
<?php
defined( 'ABSPATH' ) || exit();
class WC_Stripe_Admin_User_Edit {
public static function init() {
add_action( 'edit_user_profile', array( __CLASS__, 'output' ) );
add_action( 'show_user_profile', array( __CLASS__, 'output' ) );
add_action( 'edit_user_profile_update', array( __CLASS__, 'save' ) );
add_action( 'personal_options_update', array( __CLASS__, 'save' ) );
}
public static function output( $user ) {
wp_enqueue_style( 'wc-stripe-admin-style' );
remove_filter( 'woocommerce_get_customer_payment_tokens', 'wc_stripe_get_customer_payment_tokens' );
$tokens = WC_Payment_Tokens::get_customer_tokens( $user->ID );
$payment_methods = array(
'live' => array(),
'test' => array(),
);
foreach ( $tokens as $token ) {
if ( $token instanceof WC_Payment_Token_Stripe ) {
if ( 'live' === $token->get_environment() ) {
$payment_methods['live'][] = $token;
} else {
$payment_methods['test'][] = $token;
}
}
}
if ( current_user_can( 'manage_woocommerce' ) ) {
include stripe_wc()->plugin_path() . 'includes/admin/views/html-user-profile.php';
}
}
public static function save( $user_id ) {
if ( ! current_user_can( 'manage_woocommerce' ) ) {
return;
}
$modes = array( 'test', 'live' );
if ( isset( $_POST['wc_stripe_live_id'] ) ) {
$old_live_id = wc_stripe_get_customer_id( $user_id, 'live' );
wc_stripe_delete_customer( $user_id, 'live', true );
wc_stripe_save_customer( wc_clean( $_POST['wc_stripe_live_id'] ), $user_id, 'live' );
}
if ( isset( $_POST['wc_stripe_test_id'] ) ) {
$old_test_id = wc_stripe_get_customer_id( $user_id, 'test' );
wc_stripe_delete_customer( $user_id, 'test', true );
wc_stripe_save_customer( wc_clean( $_POST['wc_stripe_test_id'] ), $user_id, 'test' );
}
foreach ( $modes as $mode ) {
if ( isset( $_POST[ $mode . '_payment_method_actions' ] ) ) {
switch ( wc_clean( $_POST[ $mode . '_payment_method_actions' ] ) ) {
case 'delete':
if ( isset( $_POST['payment_methods'], $_POST['payment_methods'][ $mode ] ) ) {
$tokens = wc_clean( $_POST['payment_methods'][ $mode ] );
foreach ( $tokens as $token_id ) {
WC_Payment_Tokens::delete( absint( $token_id ) );
}
}
break;
}
}
}
$changes = array(
'live' => $old_live_id !== wc_stripe_get_customer_id( $user_id, 'live' ),
'test' => $old_test_id !== wc_stripe_get_customer_id( $user_id, 'test' ),
);
remove_action( 'woocommerce_payment_token_deleted', 'wc_stripe_woocommerce_payment_token_deleted', 10 );
remove_action( 'woocommerce_get_customer_payment_tokens', 'wc_stripe_get_customer_payment_tokens' );
foreach ( $changes as $mode => $change ) {
if ( $change ) {
$tokens = WC_Payment_Tokens::get_customer_tokens( $user_id );
foreach ( $tokens as $token ) {
if ( $token instanceof WC_Payment_Token_Stripe ) {
if ( $mode === $token->get_environment() ) {
WC_Payment_Tokens::delete( $token->get_id() );
}
}
}
if ( ( $customer_id = wc_stripe_get_customer_id( $user_id, $mode ) ) ) {
WC_Stripe_Customer_Manager::sync_payment_methods( $customer_id, $user_id, $mode );
}
}
}
}
}
WC_Stripe_Admin_User_Edit::init();