Move meta (when creating) to its own sub meta array.

This commit is contained in:
Justin Shreve 2016-02-04 13:24:29 -08:00
parent f66a8b6478
commit f66b9e3681
4 changed files with 37 additions and 43 deletions

View File

@ -163,7 +163,7 @@ if ( ! defined( 'ABSPATH' ) ) {
* @return mixed array representation
*/
public function __data_format() {
return array_merge( $this->data, $this->meta );
return array_merge( $this->data, array( 'meta' => $this->meta ) );
}
}

View File

@ -53,27 +53,18 @@ class WC_Payment_Tokens {
}
global $wpdb;
unset( $args['token_id'] );
// We need to separate our meta fields since they are stored in a separate table
$core_fields = self::get_token_core_fields();
$core_fields_to_create = array();
foreach ( $core_fields as $core_field ) {
if ( isset( $args[ $core_field ] ) ) {
$core_fields_to_create[ $core_field ] = $args[ $core_field ];
unset( $args[ $core_field ] );
}
}
$meta = $args['meta'];
unset( $args['token_id'], $args['meta'] );
// Store the main token in the database
$wpdb->insert( $wpdb->prefix . 'woocommerce_payment_tokens', $core_fields_to_create );
$wpdb->insert( $wpdb->prefix . 'woocommerce_payment_tokens', $args );
$token_id = $wpdb->insert_id;
foreach ( $args as $meta_key => $meta_value ) {
foreach ( $meta as $meta_key => $meta_value ) {
add_metadata( 'payment_token', $token_id, $meta_key, $meta_value, true );
}
do_action( 'woocommerce_payment_token_created', $token_id );
return self::generate_token( $token_id, $core_fields_to_create );
return self::generate_token( $token_id, $args );
}
/**
@ -88,31 +79,22 @@ class WC_Payment_Tokens {
}
$args = $token->__data_format();
unset( $args['token_id'] );
$meta = $args['meta'];
if ( ! self::validate( $args ) ) {
return; // @todo throw an error
}
// We need to separate our meta fields since they are stored in a separate table
$core_fields = self::get_token_core_fields();
$core_fields_to_create = array();
foreach ( $core_fields as $core_field ) {
if ( isset( $args[ $core_field ] ) ) {
$core_fields_to_update[ $core_field ] = $args[ $core_field ];
unset( $args[ $core_field ] );
}
}
unset( $args['token_id'], $args['meta'] );
global $wpdb;
// Update our fields
$wpdb->update( $wpdb->prefix . 'woocommerce_payment_tokens', $core_fields_to_update, array( 'token_id' => $token_id ) );
foreach ( $args as $meta_key => $meta_value ) {
$wpdb->update( $wpdb->prefix . 'woocommerce_payment_tokens', $args, array( 'token_id' => $token_id ) );
foreach ( $meta as $meta_key => $meta_value ) {
update_metadata( 'payment_token', $token_id, $meta_key, $meta_value );
}
do_action( 'woocommerce_payment_token_updated', $token_id );
return self::generate_token( $token_id, $core_fields_to_update );
return self::generate_token( $token_id, $args );
}
/**
@ -205,14 +187,6 @@ class WC_Payment_Tokens {
return apply_filters( 'woocommerce_get_order_payment_tokens', $tokens, $order_id );
}
/**
* Returns an array of fields used in all payment tokens
* @return array Core fields
*/
private static function get_token_core_fields() {
return apply_filters( 'woocommerce_payment_token_core_fields', array( 'gateway_id', 'token', 'user_id', 'type', 'is_default' ) );
}
/**
* Generates a token object
* @param int $token_id ID of the token being returned

View File

@ -33,19 +33,19 @@ class WC_Payment_Token_CC extends WC_Payment_Token {
*/
public static function validate( $args ) {
if ( empty( $args['last4'] ) ) {
if ( empty( $args['meta']['last4'] ) ) {
return false;
}
if ( empty( $args['expiry_year'] ) ) {
if ( empty( $args['meta']['expiry_year'] ) ) {
return false;
}
if ( empty( $args['expiry_month'] ) ) {
if ( empty( $args['meta']['expiry_month'] ) ) {
return false;
}
if ( empty ( $args['card_type'] ) ) {
if ( empty ( $args['meta']['card_type'] ) ) {
return false;
}
@ -112,4 +112,24 @@ class WC_Payment_Token_CC extends WC_Payment_Token {
$this->meta['expiry_month'] = $month;
}
/**
* Returns the last four digits
*
* @since 2.6.0
* @return string Last 4 digits
*/
public function get_last4() {
return isset( $this->meta['last4'] ) ? $this->meta['last4'] : null;
}
/**
* Set the last four digits
*
* @since 2.6.0
* @param string $last4
*/
public function set_last4( $last4 ) {
$this->meta['last4'] = $last4;
}
}

View File

@ -28,7 +28,7 @@ class WC_Payment_Token_eCheck extends WC_Payment_Token {
* @return boolean True if the passed data is valid
*/
public static function validate( $args ) {
if ( empty( $args['last4'] ) ) {
if ( empty( $args['meta']['last4'] ) ) {
return false;
}
return true;