WC_Payment_Token_Data_Store

WC Payment Token Data Store: Custom Table.

Defined (1)

The class is defined in the following location(s).

/includes/data-stores/class-wc-payment-token-data-store.php  
  1. class WC_Payment_Token_Data_Store extends WC_Data_Store_WP implements WC_Payment_Token_Data_Store_Interface, WC_Object_Data_Store_Interface { 
  2.  
  3. /** 
  4. * Meta type. Payment tokens are a new object type. 
  5. * @var string 
  6. */ 
  7. protected $meta_type = 'payment_token'; 
  8.  
  9. /** 
  10. * If we have already saved our extra data, don't do automatic / default handling. 
  11. */ 
  12. protected $extra_data_saved = false; 
  13.  
  14. /** 
  15. * Create a new payment token in the database. 
  16. * @since 3.0.0 
  17. * @param WC_Payment_Token $token 
  18. */ 
  19. public function create( &$token ) { 
  20. if ( false === $token->validate() ) { 
  21. throw new Exception( __( 'Invalid or missing payment token fields.', 'woocommerce' ) ); 
  22.  
  23. global $wpdb; 
  24. if ( ! $token->is_default() && $token->get_user_id() > 0 ) { 
  25. $default_token = WC_Payment_Tokens::get_customer_default_token( $token->get_user_id() ); 
  26. if ( is_null( $default_token ) ) { 
  27. $token->set_default( true ); 
  28.  
  29. $payment_token_data = array( 
  30. 'gateway_id' => $token->get_gateway_id( 'edit' ),  
  31. 'token' => $token->get_token( 'edit' ),  
  32. 'user_id' => $token->get_user_id( 'edit' ),  
  33. 'type' => $token->get_type( 'edit' ),  
  34. ); 
  35.  
  36. $wpdb->insert( $wpdb->prefix . 'woocommerce_payment_tokens', $payment_token_data ); 
  37. $token_id = $wpdb->insert_id; 
  38. $token->set_id( $token_id ); 
  39. $this->save_extra_data( $token, true ); 
  40. $token->save_meta_data(); 
  41. $token->apply_changes(); 
  42.  
  43. // Make sure all other tokens are not set to default 
  44. if ( $token->is_default() && $token->get_user_id() > 0 ) { 
  45. WC_Payment_Tokens::set_users_default( $token->get_user_id(), $token_id ); 
  46.  
  47. do_action( 'woocommerce_new_payment_token', $token_id ); 
  48.  
  49. /** 
  50. * Update a payment token. 
  51. * @since 3.0.0 
  52. * @param WC_Payment_Token $token 
  53. */ 
  54. public function update( &$token ) { 
  55. if ( false === $token->validate() ) { 
  56. throw new Exception( __( 'Invalid or missing payment token fields.', 'woocommerce' ) ); 
  57.  
  58. global $wpdb; 
  59.  
  60. $updated_props = array(); 
  61. $core_props = array( 'gateway_id', 'token', 'user_id', 'type' ); 
  62. $changed_props = array_keys( $token->get_changes() ); 
  63.  
  64. foreach ( $changed_props as $prop ) { 
  65. if ( ! in_array( $prop, $core_props ) ) { 
  66. continue; 
  67. $updated_props[] = $prop; 
  68. $payment_token_data[ $prop ] = $token->{"get_" . $prop}( 'edit' ); 
  69.  
  70. if ( ! empty( $payment_token_data ) ) { 
  71. $wpdb->update( 
  72. $wpdb->prefix . 'woocommerce_payment_tokens',  
  73. $payment_token_data,  
  74. array( 'token_id' => $token->get_id( 'edit' ) ) 
  75. ); 
  76.  
  77. $updated_extra_props = $this->save_extra_data( $token ); 
  78. $updated_props = array_merge( $updated_props, $updated_extra_props ); 
  79. $token->save_meta_data(); 
  80. $token->apply_changes(); 
  81.  
  82. // Make sure all other tokens are not set to default 
  83. if ( $token->is_default() && $token->get_user_id() > 0 ) { 
  84. WC_Payment_Tokens::set_users_default( $token->get_user_id(), $token->get_id() ); 
  85.  
  86. do_action( 'woocommerce_payment_token_object_updated_props', $token, $updated_props ); 
  87. do_action( 'woocommerce_payment_token_updated', $token->get_id() ); 
  88.  
  89. /** 
  90. * Remove a payment token from the database. 
  91. * @since 3.0.0 
  92. * @param WC_Payment_Token $token 
  93. * @param bool $force_delete 
  94. */ 
  95. public function delete( &$token, $force_delete = false ) { 
  96. global $wpdb; 
  97. $wpdb->delete( $wpdb->prefix . 'woocommerce_payment_tokens', array( 'token_id' => $token->get_id() ), array( '%d' ) ); 
  98. $wpdb->delete( $wpdb->prefix . 'woocommerce_payment_tokenmeta', array( 'payment_token_id' => $token->get_id() ), array( '%d' ) ); 
  99. do_action( 'woocommerce_payment_token_deleted', $token->get_id(), $token ); 
  100.  
  101. /** 
  102. * Read a token from the database. 
  103. * @since 3.0.0 
  104. * @param WC_Payment_Token $token 
  105. */ 
  106. public function read( &$token ) { 
  107. global $wpdb; 
  108. if ( $data = $wpdb->get_row( $wpdb->prepare( "SELECT token, user_id, gateway_id, is_default FROM {$wpdb->prefix}woocommerce_payment_tokens WHERE token_id = %d LIMIT 1;", $token->get_id() ) ) ) { 
  109. $token->set_props( array( 
  110. 'token' => $data->token,  
  111. 'user_id' => $data->user_id,  
  112. 'gateway_id' => $data->gateway_id,  
  113. 'default' => $data->is_default,  
  114. ) ); 
  115. $this->read_extra_data( $token ); 
  116. $token->read_meta_data(); 
  117. $token->set_object_read( true ); 
  118. do_action( 'woocommerce_payment_token_loaded', $token ); 
  119. } else { 
  120. throw new Exception( __( 'Invalid payment token.', 'woocommerce' ) ); 
  121.  
  122. /** 
  123. * Read extra data associated with the token (like last4 digits of a card for expiry dates). 
  124. * @param WC_Payment_Token 
  125. * @since 3.0.0 
  126. */ 
  127. protected function read_extra_data( &$token ) { 
  128. foreach ( $token->get_extra_data_keys() as $key ) { 
  129. $function = 'set_' . $key; 
  130. if ( is_callable( array( $token, $function ) ) ) { 
  131. $token->{$function}( get_metadata( 'payment_token', $token->get_id(), $key, true ) ); 
  132.  
  133. /** 
  134. * Saves extra token data as meta. 
  135. * @since 3.0.0 
  136. * @param $token WC_Token 
  137. * @param $force bool 
  138. * @return array List of updated props. 
  139. */ 
  140. protected function save_extra_data( &$token, $force = false ) { 
  141. if ( $this->extra_data_saved ) { 
  142. return array(); 
  143.  
  144. $updated_props = array(); 
  145. $extra_data_keys = $token->get_extra_data_keys(); 
  146. $meta_key_to_props = ! empty( $extra_data_keys ) ? array_combine( $extra_data_keys, $extra_data_keys ) : array(); 
  147. $props_to_update = $force ? $meta_key_to_props : $this->get_props_to_update( $token, $meta_key_to_props ); 
  148.  
  149. foreach ( $extra_data_keys as $key ) { 
  150. if ( ! array_key_exists( $key, $props_to_update ) ) { 
  151. continue; 
  152. $function = 'get_' . $key; 
  153. if ( is_callable( array( $token, $function ) ) ) { 
  154. if ( update_metadata( 'payment_token', $token->get_id(), $key, $token->{$function}( 'edit' ) ) ) { 
  155. $updated_props[] = $key; 
  156.  
  157. return $updated_props; 
  158.  
  159. /** 
  160. * Returns an array of objects (stdObject) matching specific token critera. 
  161. * Accepts token_id, user_id, gateway_id, and type. 
  162. * Each object should contain the fields token_id, gateway_id, token, user_id, type, is_default. 
  163. * @since 3.0.0 
  164. * @param array $args 
  165. * @return array 
  166. */ 
  167. public function get_tokens( $args ) { 
  168. global $wpdb; 
  169. $args = wp_parse_args( $args, array( 
  170. 'token_id' => '',  
  171. 'user_id' => '',  
  172. 'gateway_id' => '',  
  173. 'type' => '',  
  174. ) ); 
  175.  
  176. $sql = "SELECT * FROM {$wpdb->prefix}woocommerce_payment_tokens"; 
  177. $where = array( '1=1' ); 
  178.  
  179. if ( $args['token_id'] ) { 
  180. $token_ids = array_map( 'absint', is_array( $args['token_id'] ) ? $args['token_id'] : array( $args['token_id'] ) ); 
  181. $where[] = "token_id IN ('" . implode( "', '", array_map( 'esc_sql', $token_ids ) ) . "')"; 
  182.  
  183. if ( $args['user_id'] ) { 
  184. $where[] = $wpdb->prepare( 'user_id = %d', absint( $args['user_id'] ) ); 
  185.  
  186. if ( $args['gateway_id'] ) { 
  187. $gateway_ids = array( $args['gateway_id'] ); 
  188. } else { 
  189. $gateways = WC_Payment_Gateways::instance(); 
  190. $gateway_ids = $gateways->get_payment_gateway_ids(); 
  191.  
  192. $gateway_ids[] = ''; 
  193. $where[] = "gateway_id IN ('" . implode( "', '", array_map( 'esc_sql', $gateway_ids ) ) . "')"; 
  194.  
  195. if ( $args['type'] ) { 
  196. $where[] = $wpdb->prepare( 'type = %s', $args['type'] ); 
  197.  
  198. $token_results = $wpdb->get_results( $sql . ' WHERE ' . implode( ' AND ', $where ) ); 
  199.  
  200. return $token_results; 
  201.  
  202. /** 
  203. * Returns an stdObject of a token for a user's default token. 
  204. * Should contain the fields token_id, gateway_id, token, user_id, type, is_default. 
  205. * @since 3.0.0 
  206. * @param id $user_id 
  207. * @return object 
  208. */ 
  209. public function get_users_default_token( $user_id ) { 
  210. global $wpdb; 
  211. return $wpdb->get_row( $wpdb->prepare( 
  212. "SELECT * FROM {$wpdb->prefix}woocommerce_payment_tokens WHERE user_id = %d AND is_default = 1",  
  213. $user_id 
  214. ) ); 
  215.  
  216. /** 
  217. * Returns an stdObject of a token. 
  218. * Should contain the fields token_id, gateway_id, token, user_id, type, is_default. 
  219. * @since 3.0.0 
  220. * @param id $token_id 
  221. * @return object 
  222. */ 
  223. public function get_token_by_id( $token_id ) { 
  224. global $wpdb; 
  225. return $wpdb->get_row( $wpdb->prepare( 
  226. "SELECT * FROM {$wpdb->prefix}woocommerce_payment_tokens WHERE token_id = %d",  
  227. $token_id 
  228. ) ); 
  229.  
  230. /** 
  231. * Returns metadata for a specific payment token. 
  232. * @since 3.0.0 
  233. * @param id $token_id 
  234. * @return array 
  235. */ 
  236. public function get_metadata( $token_id ) { 
  237. return get_metadata( 'payment_token', $token_id ); 
  238.  
  239. /** 
  240. * Get a token's type by ID. 
  241. * @since 3.0.0 
  242. * @param id $token_id 
  243. * @return string 
  244. */ 
  245. public function get_token_type_by_id( $token_id ) { 
  246. global $wpdb; 
  247. return $wpdb->get_var( $wpdb->prepare( 
  248. "SELECT type FROM {$wpdb->prefix}woocommerce_payment_tokens WHERE token_id = %d",  
  249. $token_id 
  250. ) ); 
  251.  
  252. /** 
  253. * Update's a tokens default status in the database. Used for quickly 
  254. * looping through tokens and setting their statuses instead of creating a bunch 
  255. * of objects. 
  256. * @since 3.0.0 
  257. * @param id $token_id 
  258. * @return string 
  259. */ 
  260. public function set_default_status( $token_id, $status = true ) { 
  261. global $wpdb; 
  262. $wpdb->update( 
  263. $wpdb->prefix . 'woocommerce_payment_tokens',  
  264. array( 'is_default' => $status ),  
  265. array( 
  266. 'token_id' => $token_id,  
  267. ); 
  268.