When updating payment tokens, only update the props that have changed + fire off a hook of the changed props.

This commit is contained in:
Justin Shreve 2017-01-25 11:05:54 -08:00
parent 9ef0844ac5
commit 32651f638c
2 changed files with 19 additions and 11 deletions

View File

@ -71,18 +71,23 @@ class WC_Payment_Token_Data_Store extends WC_Data_Store_WP implements WC_Payment
global $wpdb; global $wpdb;
$payment_token_data = array( $updated_props = array();
'gateway_id' => $token->get_gateway_id( 'edit' ), $payment_token_data = array();
'token' => $token->get_token( 'edit' ), $props = array( 'gateway_id', 'token', 'user_id', 'type' );
'user_id' => $token->get_user_id( 'edit' ), $changed_props = array_keys( $token->get_changes() );
'type' => $token->get_type( '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->update(
$wpdb->prefix . 'woocommerce_payment_tokens', $wpdb->prefix . 'woocommerce_payment_tokens',
$payment_token_data, $payment_token_data,
array( 'token_id' => $token->get_id( 'edit' ) ) array( 'token_id' => $token->get_id( 'edit' ) )
); );
}
$token->save_meta_data(); $token->save_meta_data();
$token->apply_changes(); $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() ); 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() ); do_action( 'woocommerce_payment_token_updated', $token->get_id() );
} }

View File

@ -122,10 +122,12 @@ class WC_Tests_Payment_Token extends WC_Unit_Test_Case {
$token = WC_Helper_Payment_Token::create_stub_token( __FUNCTION__ ); $token = WC_Helper_Payment_Token::create_stub_token( __FUNCTION__ );
$this->assertEquals( __FUNCTION__, $token->get_extra() ); $this->assertEquals( __FUNCTION__, $token->get_extra() );
$token->set_extra( ':)' ); $token->set_extra( ':)' );
$token->set_user_id( 2 );
$token->save(); $token->save();
$token = new WC_Payment_Token_Stub( $token->get_id() ); $token = new WC_Payment_Token_Stub( $token->get_id() );
$this->assertEquals( ':)', $token->get_extra() ); $this->assertEquals( ':)', $token->get_extra() );
$this->assertEquals( 2, $token->get_user_id() );
} }
/** /**