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