abstracts
8 years ago
admin
8 years ago
api
8 years ago
cli
8 years ago
data-stores
8 years ago
emails
8 years ago
gateways
8 years ago
interfaces
9 years ago
legacy
9 years ago
libraries
9 years ago
log-handlers
9 years ago
payment-tokens
9 years ago
shipping
9 years ago
shortcodes
8 years ago
theme-support
9 years ago
vendor
9 years ago
walkers
9 years ago
widgets
8 years ago
class-wc-ajax.php
9 years ago
class-wc-api.php
9 years ago
class-wc-auth.php
9 years ago
class-wc-autoloader.php
9 years ago
class-wc-background-emailer.php
9 years ago
class-wc-background-updater.php
9 years ago
class-wc-breadcrumb.php
9 years ago
class-wc-cache-helper.php
9 years ago
class-wc-cart.php
8 years ago
class-wc-checkout.php
8 years ago
class-wc-cli.php
9 years ago
class-wc-comments.php
9 years ago
class-wc-countries.php
8 years ago
class-wc-coupon.php
8 years ago
class-wc-customer-download.php
9 years ago
class-wc-customer.php
9 years ago
class-wc-data-exception.php
9 years ago
class-wc-data-store.php
9 years ago
class-wc-datetime.php
9 years ago
class-wc-deprecated-action-hooks.php
9 years ago
class-wc-deprecated-filter-hooks.php
8 years ago
class-wc-download-handler.php
9 years ago
class-wc-emails.php
8 years ago
class-wc-embed.php
9 years ago
class-wc-form-handler.php
9 years ago
class-wc-frontend-scripts.php
9 years ago
class-wc-geo-ip.php
9 years ago
class-wc-geolocation.php
8 years ago
class-wc-https.php
9 years ago
class-wc-install.php
9 years ago
class-wc-integrations.php
9 years ago
class-wc-legacy-api.php
9 years ago
class-wc-log-levels.php
9 years ago
class-wc-logger.php
9 years ago
class-wc-order-factory.php
9 years ago
class-wc-order-item-coupon.php
9 years ago
class-wc-order-item-fee.php
9 years ago
class-wc-order-item-meta.php
8 years ago
class-wc-order-item-product.php
8 years ago
class-wc-order-item-shipping.php
8 years ago
class-wc-order-item-tax.php
9 years ago
class-wc-order-item.php
8 years ago
class-wc-order-refund.php
9 years ago
class-wc-order.php
9 years ago
class-wc-payment-gateways.php
9 years ago
class-wc-payment-tokens.php
9 years ago
class-wc-post-data.php
8 years ago
class-wc-post-types.php
9 years ago
class-wc-product-attribute.php
8 years ago
class-wc-product-download.php
9 years ago
class-wc-product-external.php
9 years ago
class-wc-product-factory.php
9 years ago
class-wc-product-grouped.php
8 years ago
class-wc-product-simple.php
9 years ago
class-wc-product-variable.php
8 years ago
class-wc-product-variation.php
8 years ago
class-wc-query.php
9 years ago
class-wc-register-wp-admin-settings.php
9 years ago
class-wc-session-handler.php
9 years ago
class-wc-shipping-rate.php
9 years ago
class-wc-shipping-zone.php
8 years ago
class-wc-shipping-zones.php
9 years ago
class-wc-shipping.php
9 years ago
class-wc-shortcodes.php
8 years ago
class-wc-structured-data.php
8 years ago
class-wc-tax.php
9 years ago
class-wc-template-loader.php
9 years ago
class-wc-tracker.php
9 years ago
class-wc-validation.php
9 years ago
class-wc-webhook.php
8 years ago
wc-account-functions.php
9 years ago
wc-attribute-functions.php
9 years ago
wc-cart-functions.php
9 years ago
wc-conditional-functions.php
9 years ago
wc-core-functions.php
8 years ago
wc-coupon-functions.php
9 years ago
wc-deprecated-functions.php
9 years ago
wc-formatting-functions.php
9 years ago
wc-notice-functions.php
9 years ago
wc-order-functions.php
8 years ago
wc-order-item-functions.php
8 years ago
wc-page-functions.php
9 years ago
wc-product-functions.php
8 years ago
wc-rest-functions.php
8 years ago
wc-stock-functions.php
9 years ago
wc-template-functions.php
8 years ago
wc-template-hooks.php
9 years ago
wc-term-functions.php
9 years ago
wc-update-functions.php
9 years ago
wc-user-functions.php
9 years ago
wc-webhook-functions.php
9 years ago
wc-widget-functions.php
9 years ago
class-wc-data-store.php
194 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; |
| 4 | } |
| 5 | |
| 6 | /** |
| 7 | * WC Data Store. |
| 8 | * |
| 9 | * @since 3.0.0 |
| 10 | * @version 3.0.0 |
| 11 | * @category Class |
| 12 | * @author WooThemes |
| 13 | */ |
| 14 | class WC_Data_Store { |
| 15 | |
| 16 | /** |
| 17 | * Contains an instance of the data store class that we are working with. |
| 18 | */ |
| 19 | private $instance = null; |
| 20 | |
| 21 | /** |
| 22 | * Contains an array of default WC supported data stores. |
| 23 | * Format of object name => class name. |
| 24 | * Example: 'product' => 'WC_Product_Data_Store_CPT' |
| 25 | * You can aso pass something like product_<type> for product stores and |
| 26 | * that type will be used first when avaiable, if a store is requested like |
| 27 | * this and doesn't exist, then the store would fall back to 'product'. |
| 28 | * Ran through `woocommerce_data_stores`. |
| 29 | */ |
| 30 | private $stores = array( |
| 31 | 'coupon' => 'WC_Coupon_Data_Store_CPT', |
| 32 | 'customer' => 'WC_Customer_Data_Store', |
| 33 | 'customer-download' => 'WC_Customer_Download_Data_Store', |
| 34 | 'customer-session' => 'WC_Customer_Data_Store_Session', |
| 35 | 'order' => 'WC_Order_Data_Store_CPT', |
| 36 | 'order-refund' => 'WC_Order_Refund_Data_Store_CPT', |
| 37 | 'order-item' => 'WC_Order_Item_Data_Store', |
| 38 | 'order-item-coupon' => 'WC_Order_Item_Coupon_Data_Store', |
| 39 | 'order-item-fee' => 'WC_Order_Item_Fee_Data_Store', |
| 40 | 'order-item-product' => 'WC_Order_Item_Product_Data_Store', |
| 41 | 'order-item-shipping' => 'WC_Order_Item_Shipping_Data_Store', |
| 42 | 'order-item-tax' => 'WC_Order_Item_Tax_Data_Store', |
| 43 | 'payment-token' => 'WC_Payment_Token_Data_Store', |
| 44 | 'product' => 'WC_Product_Data_Store_CPT', |
| 45 | 'product-grouped' => 'WC_Product_Grouped_Data_Store_CPT', |
| 46 | 'product-variable' => 'WC_Product_Variable_Data_Store_CPT', |
| 47 | 'product-variation' => 'WC_Product_Variation_Data_Store_CPT', |
| 48 | 'shipping-zone' => 'WC_Shipping_Zone_Data_Store', |
| 49 | ); |
| 50 | |
| 51 | /** |
| 52 | * Contains the name of the current data store's class name. |
| 53 | */ |
| 54 | private $current_class_name = ''; |
| 55 | |
| 56 | /** |
| 57 | * The object type this store works with. |
| 58 | * @var string |
| 59 | */ |
| 60 | private $object_type = ''; |
| 61 | |
| 62 | |
| 63 | /** |
| 64 | * Tells WC_Data_Store which object (coupon, product, order, etc) |
| 65 | * store we want to work with. |
| 66 | * |
| 67 | * @param string $object_type Name of object. |
| 68 | */ |
| 69 | public function __construct( $object_type ) { |
| 70 | $this->object_type = $object_type; |
| 71 | $this->stores = apply_filters( 'woocommerce_data_stores', $this->stores ); |
| 72 | |
| 73 | // If this object type can't be found, check to see if we can load one |
| 74 | // level up (so if product-type isn't found, we try product). |
| 75 | if ( ! array_key_exists( $object_type, $this->stores ) ) { |
| 76 | $pieces = explode( '-', $object_type ); |
| 77 | $object_type = $pieces[0]; |
| 78 | } |
| 79 | |
| 80 | if ( array_key_exists( $object_type, $this->stores ) ) { |
| 81 | $store = apply_filters( 'woocommerce_' . $object_type . '_data_store', $this->stores[ $object_type ] ); |
| 82 | if ( is_object( $store ) ) { |
| 83 | if ( ! $store instanceof WC_Object_Data_Store_Interface ) { |
| 84 | throw new Exception( __( 'Invalid data store.', 'woocommerce' ) ); |
| 85 | } |
| 86 | $this->current_class_name = get_class( $store ); |
| 87 | $this->instance = $store; |
| 88 | } else { |
| 89 | if ( ! class_exists( $store ) ) { |
| 90 | throw new Exception( __( 'Invalid data store.', 'woocommerce' ) ); |
| 91 | } |
| 92 | $this->current_class_name = $store; |
| 93 | $this->instance = new $store; |
| 94 | } |
| 95 | } else { |
| 96 | throw new Exception( __( 'Invalid data store.', 'woocommerce' ) ); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Only store the object type to avoid serializing the data store instance. |
| 102 | * |
| 103 | * @return array |
| 104 | */ |
| 105 | public function __sleep() { |
| 106 | return array( 'object_type' ); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Re-run the constructor with the object type. |
| 111 | */ |
| 112 | public function __wakeup() { |
| 113 | $this->__construct( $this->object_type ); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Loads a data store. |
| 118 | * |
| 119 | * @param string $object_type Name of object. |
| 120 | * @since 3.0.0 |
| 121 | */ |
| 122 | public static function load( $object_type ) { |
| 123 | return new WC_Data_Store( $object_type ); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Returns the class name of the current data store. |
| 128 | * |
| 129 | * @since 3.0.0 |
| 130 | * @return string |
| 131 | */ |
| 132 | public function get_current_class_name() { |
| 133 | return $this->current_class_name; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Reads an object from the data store. |
| 138 | * |
| 139 | * @since 3.0.0 |
| 140 | * @param WC_Data |
| 141 | */ |
| 142 | public function read( &$data ) { |
| 143 | $this->instance->read( $data ); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Create an object in the data store. |
| 148 | * |
| 149 | * @since 3.0.0 |
| 150 | * @param WC_Data |
| 151 | */ |
| 152 | public function create( &$data ) { |
| 153 | $this->instance->create( $data ); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Update an object in the data store. |
| 158 | * |
| 159 | * @since 3.0.0 |
| 160 | * @param WC_Data |
| 161 | */ |
| 162 | public function update( &$data ) { |
| 163 | $this->instance->update( $data ); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Delete an object from the data store. |
| 168 | * |
| 169 | * @since 3.0.0 |
| 170 | * @param WC_Data |
| 171 | * @param array $args Array of args to pass to the delete method. |
| 172 | */ |
| 173 | public function delete( &$data, $args = array() ) { |
| 174 | $this->instance->delete( $data, $args ); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Data stores can define additional functions (for example, coupons have |
| 179 | * some helper methods for increasing or decreasing usage). This passes |
| 180 | * through to the instance if that function exists. |
| 181 | * |
| 182 | * @since 3.0.0 |
| 183 | * @param $method |
| 184 | * @param $parameters |
| 185 | */ |
| 186 | public function __call( $method, $parameters ) { |
| 187 | if ( is_callable( array( $this->instance, $method ) ) ) { |
| 188 | $object = array_shift( $parameters ); |
| 189 | return call_user_func_array( array( $this->instance, $method ), array_merge( array( &$object ), $parameters ) ); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | } |
| 194 |