WC_Data_Store

WC Data Store.

Defined (1)

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

/includes/class-wc-data-store.php  
  1. class WC_Data_Store { 
  2.  
  3. /** 
  4. * Contains an instance of the data store class that we are working with. 
  5. */ 
  6. private $instance = null; 
  7.  
  8. /** 
  9. * Contains an array of default WC supported data stores. 
  10. * Format of object name => class name. 
  11. * Example: 'product' => 'WC_Product_Data_Store_CPT' 
  12. * You can aso pass something like product_<type> for product stores and 
  13. * that type will be used first when avaiable, if a store is requested like 
  14. * this and doesn't exist, then the store would fall back to 'product'. 
  15. * Ran through `woocommerce_data_stores`. 
  16. */ 
  17. private $stores = array( 
  18. 'coupon' => 'WC_Coupon_Data_Store_CPT',  
  19. 'customer' => 'WC_Customer_Data_Store',  
  20. 'customer-download' => 'WC_Customer_Download_Data_Store',  
  21. 'customer-session' => 'WC_Customer_Data_Store_Session',  
  22. 'order' => 'WC_Order_Data_Store_CPT',  
  23. 'order-refund' => 'WC_Order_Refund_Data_Store_CPT',  
  24. 'order-item' => 'WC_Order_Item_Data_Store',  
  25. 'order-item-coupon' => 'WC_Order_Item_Coupon_Data_Store',  
  26. 'order-item-fee' => 'WC_Order_Item_Fee_Data_Store',  
  27. 'order-item-product' => 'WC_Order_Item_Product_Data_Store',  
  28. 'order-item-shipping' => 'WC_Order_Item_Shipping_Data_Store',  
  29. 'order-item-tax' => 'WC_Order_Item_Tax_Data_Store',  
  30. 'payment-token' => 'WC_Payment_Token_Data_Store',  
  31. 'product' => 'WC_Product_Data_Store_CPT',  
  32. 'product-grouped' => 'WC_Product_Grouped_Data_Store_CPT',  
  33. 'product-variable' => 'WC_Product_Variable_Data_Store_CPT',  
  34. 'product-variation' => 'WC_Product_Variation_Data_Store_CPT',  
  35. 'shipping-zone' => 'WC_Shipping_Zone_Data_Store',  
  36. ); 
  37.  
  38. /** 
  39. * Contains the name of the current data store's class name. 
  40. */ 
  41. private $current_class_name = ''; 
  42.  
  43. /** 
  44. * The object type this store works with. 
  45. * @var string 
  46. */ 
  47. private $object_type = ''; 
  48.  
  49.  
  50. /** 
  51. * Tells WC_Data_Store which object (coupon, product, order, etc) 
  52. * store we want to work with. 
  53. * @param string $object_type Name of object. 
  54. */ 
  55. public function __construct( $object_type ) { 
  56. $this->object_type = $object_type; 
  57. $this->stores = apply_filters( 'woocommerce_data_stores', $this->stores ); 
  58.  
  59. // If this object type can't be found, check to see if we can load one 
  60. // level up (so if product-type isn't found, we try product). 
  61. if ( ! array_key_exists( $object_type, $this->stores ) ) { 
  62. $pieces = explode( '-', $object_type ); 
  63. $object_type = $pieces[0]; 
  64.  
  65. if ( array_key_exists( $object_type, $this->stores ) ) { 
  66. $store = apply_filters( 'woocommerce_' . $object_type . '_data_store', $this->stores[ $object_type ] ); 
  67. if ( is_object( $store ) ) { 
  68. if ( ! $store instanceof WC_Object_Data_Store_Interface ) { 
  69. throw new Exception( __( 'Invalid data store.', 'woocommerce' ) ); 
  70. $this->current_class_name = get_class( $store ); 
  71. $this->instance = $store; 
  72. } else { 
  73. if ( ! class_exists( $store ) ) { 
  74. throw new Exception( __( 'Invalid data store.', 'woocommerce' ) ); 
  75. $this->current_class_name = $store; 
  76. $this->instance = new $store; 
  77. } else { 
  78. throw new Exception( __( 'Invalid data store.', 'woocommerce' ) ); 
  79.  
  80. /** 
  81. * Only store the object type to avoid serializing the data store instance. 
  82. * @return array 
  83. */ 
  84. public function __sleep() { 
  85. return array( 'object_type' ); 
  86.  
  87. /** 
  88. * Re-run the constructor with the object type. 
  89. */ 
  90. public function __wakeup() { 
  91. $this->__construct( $this->object_type ); 
  92.  
  93. /** 
  94. * Loads a data store. 
  95. * @param string $object_type Name of object. 
  96. * @since 3.0.0 
  97. */ 
  98. public static function load( $object_type ) { 
  99. return new WC_Data_Store( $object_type ); 
  100.  
  101. /** 
  102. * Returns the class name of the current data store. 
  103. * @since 3.0.0 
  104. * @return string 
  105. */ 
  106. public function get_current_class_name() { 
  107. return $this->current_class_name; 
  108.  
  109. /** 
  110. * Reads an object from the data store. 
  111. * @since 3.0.0 
  112. * @param WC_Data 
  113. */ 
  114. public function read( &$data ) { 
  115. $this->instance->read( $data ); 
  116.  
  117. /** 
  118. * Create an object in the data store. 
  119. * @since 3.0.0 
  120. * @param WC_Data 
  121. */ 
  122. public function create( &$data ) { 
  123. $this->instance->create( $data ); 
  124.  
  125. /** 
  126. * Update an object in the data store. 
  127. * @since 3.0.0 
  128. * @param WC_Data 
  129. */ 
  130. public function update( &$data ) { 
  131. $this->instance->update( $data ); 
  132.  
  133. /** 
  134. * Delete an object from the data store. 
  135. * @since 3.0.0 
  136. * @param WC_Data 
  137. * @param array $args Array of args to pass to the delete method. 
  138. */ 
  139. public function delete( &$data, $args = array() ) { 
  140. $this->instance->delete( $data, $args ); 
  141.  
  142. /** 
  143. * Data stores can define additional functions (for example, coupons have 
  144. * some helper methods for increasing or decreasing usage). This passes 
  145. * through to the instance if that function exists. 
  146. * @since 3.0.0 
  147. * @param $method 
  148. * @param $parameters 
  149. */ 
  150. public function __call( $method, $parameters ) { 
  151. if ( is_callable( array( $this->instance, $method ) ) ) { 
  152. $object = array_shift( $parameters ); 
  153. return call_user_func_array( array( $this->instance, $method ), array_merge( array( &$object ), $parameters ) ); 
  154.