WC_Data_Store_WP

Shared logic for WP based data.

Defined (1)

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

/includes/data-stores/class-wc-data-store-wp.php  
  1. class WC_Data_Store_WP { 
  2.  
  3. /** 
  4. * Meta type. This should match up with 
  5. * the types available at https://codex.wordpress.org/Function_Reference/add_metadata. 
  6. * WP defines 'post', 'user', 'comment', and 'term'. 
  7. */ 
  8. protected $meta_type = 'post'; 
  9.  
  10. /** 
  11. * This only needs set if you are using a custom metadata type (for example payment tokens. 
  12. * This should be the name of the field your table uses for associating meta with objects. 
  13. * For example, in payment_tokenmeta, this would be payment_token_id. 
  14. * @var string 
  15. */ 
  16. protected $object_id_field_for_meta = ''; 
  17.  
  18. /** 
  19. * Data stored in meta keys, but not considered "meta" for an object. 
  20. * @since 3.0.0 
  21. * @var array 
  22. */ 
  23. protected $internal_meta_keys = array(); 
  24.  
  25. /** 
  26. * Get and store terms from a taxonomy. 
  27. * @since 3.0.0 
  28. * @param WC_Data|integer $object 
  29. * @param string $taxonomy Taxonomy name e.g. product_cat 
  30. * @return array of terms 
  31. */ 
  32. protected function get_term_ids( $object, $taxonomy ) { 
  33. if ( is_numeric( $object ) ) { 
  34. $object_id = $object; 
  35. } else { 
  36. $object_id = $object->get_id(); 
  37. $terms = get_the_terms( $object_id, $taxonomy ); 
  38. if ( false === $terms || is_wp_error( $terms ) ) { 
  39. return array(); 
  40. return wp_list_pluck( $terms, 'term_id' ); 
  41.  
  42. /** 
  43. * Returns an array of meta for an object. 
  44. * @since 3.0.0 
  45. * @param WC_Data 
  46. * @return array 
  47. */ 
  48. public function read_meta( &$object ) { 
  49. global $wpdb; 
  50. $db_info = $this->get_db_info(); 
  51. $raw_meta_data = $wpdb->get_results( $wpdb->prepare( " 
  52. SELECT {$db_info['meta_id_field']} as meta_id, meta_key, meta_value 
  53. FROM {$db_info['table']} 
  54. WHERE {$db_info['object_id_field']} = %d 
  55. ORDER BY {$db_info['meta_id_field']} 
  56. ", $object->get_id() ) ); 
  57.  
  58. $this->internal_meta_keys = array_merge( array_map( array( $this, 'prefix_key' ), $object->get_data_keys() ), $this->internal_meta_keys ); 
  59. return array_filter( $raw_meta_data, array( $this, 'exclude_internal_meta_keys' ) ); 
  60.  
  61. /** 
  62. * Deletes meta based on meta ID. 
  63. * @since 3.0.0 
  64. * @param WC_Data 
  65. * @param stdClass (containing at least ->id) 
  66. * @return array 
  67. */ 
  68. public function delete_meta( &$object, $meta ) { 
  69. delete_metadata_by_mid( $this->meta_type, $meta->id ); 
  70.  
  71. /** 
  72. * Add new piece of meta. 
  73. * @since 3.0.0 
  74. * @param WC_Data 
  75. * @param stdClass (containing ->key and ->value) 
  76. * @return int meta ID 
  77. */ 
  78. public function add_meta( &$object, $meta ) { 
  79. return add_metadata( $this->meta_type, $object->get_id(), $meta->key, $meta->value, false ); 
  80.  
  81. /** 
  82. * Update meta. 
  83. * @since 3.0.0 
  84. * @param WC_Data 
  85. * @param stdClass (containing ->id, ->key and ->value) 
  86. */ 
  87. public function update_meta( &$object, $meta ) { 
  88. update_metadata_by_mid( $this->meta_type, $meta->id, $meta->value, $meta->key ); 
  89.  
  90. /** 
  91. * Table structure is slightly different between meta types, this function will return what we need to know. 
  92. * @since 3.0.0 
  93. * @return array Array elements: table, object_id_field, meta_id_field 
  94. */ 
  95. protected function get_db_info() { 
  96. global $wpdb; 
  97.  
  98. $meta_id_field = 'meta_id'; // for some reason users calls this umeta_id so we need to track this as well. 
  99. $table = $wpdb->prefix; 
  100.  
  101. // If we are dealing with a type of metadata that is not a core type, the table should be prefixed. 
  102. if ( ! in_array( $this->meta_type, array( 'post', 'user', 'comment', 'term' ) ) ) { 
  103. $table .= 'woocommerce_'; 
  104.  
  105. $table .= $this->meta_type . 'meta'; 
  106. $object_id_field = $this->meta_type . '_id'; 
  107.  
  108. // Figure out our field names. 
  109. if ( 'user' === $this->meta_type ) { 
  110. $meta_id_field = 'umeta_id'; 
  111. $table = $wpdb->usermeta; 
  112.  
  113. if ( ! empty( $this->object_id_field_for_meta ) ) { 
  114. $object_id_field = $this->object_id_field_for_meta; 
  115.  
  116. return array( 
  117. 'table' => $table,  
  118. 'object_id_field' => $object_id_field,  
  119. 'meta_id_field' => $meta_id_field,  
  120. ); 
  121.  
  122. /** 
  123. * Internal meta keys we don't want exposed as part of meta_data. This is in 
  124. * addition to all data props with _ prefix. 
  125. * @since 2.6.0 
  126. * @return array 
  127. */ 
  128. protected function prefix_key( $key ) { 
  129. return '_' === substr( $key, 0, 1 ) ? $key : '_' . $key; 
  130.  
  131. /** 
  132. * Callback to remove unwanted meta data. 
  133. * @param object $meta 
  134. * @return bool 
  135. */ 
  136. protected function exclude_internal_meta_keys( $meta ) { 
  137. return ! in_array( $meta->meta_key, $this->internal_meta_keys ) && 0 !== stripos( $meta->meta_key, 'wp_' ); 
  138.  
  139. /** 
  140. * Gets a list of props and meta keys that need updated based on change state 
  141. * or if they are present in the database or not. 
  142. * @param WC_Data $object The WP_Data object (WC_Coupon for coupons, etc). 
  143. * @param array $meta_key_to_props A mapping of meta keys => prop names. 
  144. * @param string $meta_type The internal WP meta type (post, user, etc). 
  145. * @return array A mapping of meta keys => prop names, filtered by ones that should be updated. 
  146. */ 
  147. protected function get_props_to_update( $object, $meta_key_to_props, $meta_type = 'post' ) { 
  148. $props_to_update = array(); 
  149. $changed_props = $object->get_changes(); 
  150.  
  151. // Props should be updated if they are a part of the $changed array or don't exist yet. 
  152. foreach ( $meta_key_to_props as $meta_key => $prop ) { 
  153. if ( array_key_exists( $prop, $changed_props ) || ! metadata_exists( $meta_type, $object->get_id(), $meta_key ) ) { 
  154. $props_to_update[ $meta_key ] = $prop; 
  155.  
  156. return $props_to_update; 
  157.