From 32651f638cf814adbc48da33053b417b51d39cf5 Mon Sep 17 00:00:00 2001 From: Justin Shreve Date: Wed, 25 Jan 2017 11:05:54 -0800 Subject: [PATCH] When updating payment tokens, only update the props that have changed + fire off a hook of the changed props. --- .../class-wc-payment-token-data-store.php | 28 +++++++++++-------- .../payment-tokens/payment-token.php | 2 ++ 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/includes/data-stores/class-wc-payment-token-data-store.php b/includes/data-stores/class-wc-payment-token-data-store.php index 08d7f443046..a85ed661374 100644 --- a/includes/data-stores/class-wc-payment-token-data-store.php +++ b/includes/data-stores/class-wc-payment-token-data-store.php @@ -71,18 +71,23 @@ class WC_Payment_Token_Data_Store extends WC_Data_Store_WP implements WC_Payment global $wpdb; - $payment_token_data = array( - 'gateway_id' => $token->get_gateway_id( 'edit' ), - 'token' => $token->get_token( 'edit' ), - 'user_id' => $token->get_user_id( 'edit' ), - 'type' => $token->get_type( 'edit' ), - ); + $updated_props = array(); + $payment_token_data = array(); + $props = array( 'gateway_id', 'token', 'user_id', 'type' ); + $changed_props = array_keys( $token->get_changes() ); - $wpdb->update( - $wpdb->prefix . 'woocommerce_payment_tokens', - $payment_token_data, - array( 'token_id' => $token->get_id( 'edit' ) ) - ); + foreach ( $changed_props as $prop ) { + $updated_props[] = $prop; + $payment_token_data[ $prop ] = $token->{"get_" . $prop}( 'edit' ); + } + + if ( ! empty( $payment_token_data ) ) { + $wpdb->update( + $wpdb->prefix . 'woocommerce_payment_tokens', + $payment_token_data, + array( 'token_id' => $token->get_id( 'edit' ) ) + ); + } $token->save_meta_data(); $token->apply_changes(); @@ -92,6 +97,7 @@ class WC_Payment_Token_Data_Store extends WC_Data_Store_WP implements WC_Payment WC_Payment_Tokens::set_users_default( $token->get_user_id(), $token->get_id() ); } + do_action( 'woocommerce_payment_token_object_updated_props', $token, $updated_props ); do_action( 'woocommerce_payment_token_updated', $token->get_id() ); } diff --git a/tests/unit-tests/payment-tokens/payment-token.php b/tests/unit-tests/payment-tokens/payment-token.php index 9033784dc3e..99662280ecb 100644 --- a/tests/unit-tests/payment-tokens/payment-token.php +++ b/tests/unit-tests/payment-tokens/payment-token.php @@ -122,10 +122,12 @@ class WC_Tests_Payment_Token extends WC_Unit_Test_Case { $token = WC_Helper_Payment_Token::create_stub_token( __FUNCTION__ ); $this->assertEquals( __FUNCTION__, $token->get_extra() ); $token->set_extra( ':)' ); + $token->set_user_id( 2 ); $token->save(); $token = new WC_Payment_Token_Stub( $token->get_id() ); $this->assertEquals( ':)', $token->get_extra() ); + $this->assertEquals( 2, $token->get_user_id() ); } /**