1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Automattic\Jetpack\Sync\Modules; |
4
|
|
|
|
5
|
|
|
class WooCommerce extends Module { |
6
|
|
|
|
7
|
|
|
private $order_item_meta_whitelist = array( |
8
|
|
|
// https://github.com/woocommerce/woocommerce/blob/master/includes/data-stores/class-wc-order-item-product-store.php#L20 |
9
|
|
|
'_product_id', |
10
|
|
|
'_variation_id', |
11
|
|
|
'_qty', |
12
|
|
|
// Tax ones also included in below class |
13
|
|
|
// https://github.com/woocommerce/woocommerce/blob/master/includes/data-stores/class-wc-order-item-fee-data-store.php#L20 |
14
|
|
|
'_tax_class', |
15
|
|
|
'_tax_status', |
16
|
|
|
'_line_subtotal', |
17
|
|
|
'_line_subtotal_tax', |
18
|
|
|
'_line_total', |
19
|
|
|
'_line_tax', |
20
|
|
|
'_line_tax_data', |
21
|
|
|
// https://github.com/woocommerce/woocommerce/blob/master/includes/data-stores/class-wc-order-item-shipping-data-store.php#L20 |
22
|
|
|
'method_id', |
23
|
|
|
'cost', |
24
|
|
|
'total_tax', |
25
|
|
|
'taxes', |
26
|
|
|
// https://github.com/woocommerce/woocommerce/blob/master/includes/data-stores/class-wc-order-item-tax-data-store.php#L20 |
27
|
|
|
'rate_id', |
28
|
|
|
'label', |
29
|
|
|
'compound', |
30
|
|
|
'tax_amount', |
31
|
|
|
'shipping_tax_amount', |
32
|
|
|
// https://github.com/woocommerce/woocommerce/blob/master/includes/data-stores/class-wc-order-item-coupon-data-store.php |
33
|
|
|
'discount_amount', |
34
|
|
|
'discount_amount_tax', |
35
|
|
|
); |
36
|
|
|
|
37
|
|
|
private $order_item_table_name; |
38
|
|
|
|
39
|
|
|
public function __construct() { |
40
|
|
|
global $wpdb; |
41
|
|
|
$this->order_item_table_name = $wpdb->prefix . 'woocommerce_order_items'; |
42
|
|
|
|
43
|
|
|
// options, constants and post meta whitelists |
44
|
|
|
add_filter( 'jetpack_sync_options_whitelist', array( $this, 'add_woocommerce_options_whitelist' ), 10 ); |
45
|
|
|
add_filter( 'jetpack_sync_constants_whitelist', array( $this, 'add_woocommerce_constants_whitelist' ), 10 ); |
46
|
|
|
add_filter( 'jetpack_sync_post_meta_whitelist', array( $this, 'add_woocommerce_post_meta_whitelist' ), 10 ); |
47
|
|
|
add_filter( 'jetpack_sync_comment_meta_whitelist', array( $this, 'add_woocommerce_comment_meta_whitelist' ), 10 ); |
48
|
|
|
|
49
|
|
|
add_filter( 'jetpack_sync_before_enqueue_woocommerce_new_order_item', array( $this, 'filter_order_item' ) ); |
50
|
|
|
add_filter( 'jetpack_sync_before_enqueue_woocommerce_update_order_item', array( $this, 'filter_order_item' ) ); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
function name() { |
54
|
|
|
return 'woocommerce'; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function init_listeners( $callable ) { |
58
|
|
|
// attributes |
59
|
|
|
add_action( 'woocommerce_attribute_added', $callable, 10, 2 ); |
60
|
|
|
add_action( 'woocommerce_attribute_updated', $callable, 10, 3 ); |
61
|
|
|
add_action( 'woocommerce_attribute_deleted', $callable, 10, 3 ); |
62
|
|
|
|
63
|
|
|
// orders |
64
|
|
|
add_action( 'woocommerce_new_order', $callable, 10, 1 ); |
65
|
|
|
add_action( 'woocommerce_order_status_changed', $callable, 10, 3 ); |
66
|
|
|
add_action( 'woocommerce_payment_complete', $callable, 10, 1 ); |
67
|
|
|
|
68
|
|
|
// order items |
69
|
|
|
add_action( 'woocommerce_new_order_item', $callable, 10, 4 ); |
70
|
|
|
add_action( 'woocommerce_update_order_item', $callable, 10, 4 ); |
71
|
|
|
add_action( 'woocommerce_delete_order_item', $callable, 10, 1 ); |
72
|
|
|
$this->init_listeners_for_meta_type( 'order_item', $callable ); |
73
|
|
|
|
74
|
|
|
// payment tokens |
75
|
|
|
add_action( 'woocommerce_new_payment_token', $callable, 10, 1 ); |
76
|
|
|
add_action( 'woocommerce_payment_token_deleted', $callable, 10, 2 ); |
77
|
|
|
add_action( 'woocommerce_payment_token_updated', $callable, 10, 1 ); |
78
|
|
|
$this->init_listeners_for_meta_type( 'payment_token', $callable ); |
79
|
|
|
|
80
|
|
|
// product downloads |
81
|
|
|
add_action( 'woocommerce_downloadable_product_download_log_insert', $callable, 10, 1 ); |
82
|
|
|
add_action( 'woocommerce_grant_product_download_access', $callable, 10, 1 ); |
83
|
|
|
|
84
|
|
|
// tax rates |
85
|
|
|
add_action( 'woocommerce_tax_rate_added', $callable, 10, 2 ); |
86
|
|
|
add_action( 'woocommerce_tax_rate_updated', $callable, 10, 2 ); |
87
|
|
|
add_action( 'woocommerce_tax_rate_deleted', $callable, 10, 1 ); |
88
|
|
|
|
89
|
|
|
// webhooks |
90
|
|
|
add_action( 'woocommerce_new_webhook', $callable, 10, 1 ); |
91
|
|
|
add_action( 'woocommerce_webhook_deleted', $callable, 10, 2 ); |
92
|
|
|
add_action( 'woocommerce_webhook_updated', $callable, 10, 1 ); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function init_full_sync_listeners( $callable ) { |
96
|
|
|
add_action( 'jetpack_full_sync_woocommerce_order_items', $callable ); // also sends post meta |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function get_full_sync_actions() { |
100
|
|
|
return array( 'jetpack_full_sync_woocommerce_order_items' ); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function init_before_send() { |
104
|
|
|
// full sync |
105
|
|
|
add_filter( 'jetpack_sync_before_send_jetpack_full_sync_woocommerce_order_items', array( $this, 'expand_order_item_ids' ) ); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function filter_order_item( $args ) { |
109
|
|
|
// Make sure we always have all the data - prior to WooCommerce 3.0 we only have the user supplied data in the second argument and not the full details |
110
|
|
|
$args[1] = $this->build_order_item( $args[0] ); |
111
|
|
|
return $args; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function expand_order_item_ids( $args ) { |
115
|
|
|
$order_item_ids = $args[0]; |
116
|
|
|
|
117
|
|
|
global $wpdb; |
118
|
|
|
|
119
|
|
|
$order_item_ids_sql = implode( ', ', array_map( 'intval', $order_item_ids ) ); |
120
|
|
|
|
121
|
|
|
$order_items = $wpdb->get_results( |
122
|
|
|
"SELECT * FROM $this->order_item_table_name WHERE order_item_id IN ( $order_item_ids_sql )" |
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
return array( |
126
|
|
|
$order_items, |
127
|
|
|
$this->get_metadata( $order_item_ids, 'order_item', $this->order_item_meta_whitelist ), |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function build_order_item( $order_item_id ) { |
132
|
|
|
global $wpdb; |
133
|
|
|
return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $this->order_item_table_name WHERE order_item_id = %d", $order_item_id ) ); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
137
|
|
|
global $wpdb; |
138
|
|
|
|
139
|
|
|
return $this->enqueue_all_ids_as_action( 'jetpack_full_sync_woocommerce_order_items', $this->order_item_table_name, 'order_item_id', $this->get_where_sql( $config ), $max_items_to_enqueue, $state ); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
View Code Duplication |
public function estimate_full_sync_actions( $config ) { |
143
|
|
|
global $wpdb; |
144
|
|
|
|
145
|
|
|
$query = "SELECT count(*) FROM $this->order_item_table_name WHERE " . $this->get_where_sql( $config ); |
146
|
|
|
$count = $wpdb->get_var( $query ); |
147
|
|
|
|
148
|
|
|
return (int) ceil( $count / self::ARRAY_CHUNK_SIZE ); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
private function get_where_sql( $config ) { |
152
|
|
|
return '1=1'; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function add_woocommerce_options_whitelist( $list ) { |
156
|
|
|
return array_merge( $list, self::$wc_options_whitelist ); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function add_woocommerce_constants_whitelist( $list ) { |
160
|
|
|
return array_merge( $list, self::$wc_constants_whitelist ); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function add_woocommerce_post_meta_whitelist( $list ) { |
164
|
|
|
return array_merge( $list, self::$wc_post_meta_whitelist ); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function add_woocommerce_comment_meta_whitelist( $list ) { |
168
|
|
|
return array_merge( $list, self::$wc_comment_meta_whitelist ); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
private static $wc_options_whitelist = array( |
172
|
|
|
'woocommerce_currency', |
173
|
|
|
'woocommerce_db_version', |
174
|
|
|
'woocommerce_weight_unit', |
175
|
|
|
'woocommerce_version', |
176
|
|
|
'woocommerce_unforce_ssl_checkout', |
177
|
|
|
'woocommerce_tax_total_display', |
178
|
|
|
'woocommerce_tax_round_at_subtotal', |
179
|
|
|
'woocommerce_tax_display_shop', |
180
|
|
|
'woocommerce_tax_display_cart', |
181
|
|
|
'woocommerce_prices_include_tax', |
182
|
|
|
'woocommerce_price_thousand_sep', |
183
|
|
|
'woocommerce_price_num_decimals', |
184
|
|
|
'woocommerce_price_decimal_sep', |
185
|
|
|
'woocommerce_notify_low_stock', |
186
|
|
|
'woocommerce_notify_low_stock_amount', |
187
|
|
|
'woocommerce_notify_no_stock', |
188
|
|
|
'woocommerce_notify_no_stock_amount', |
189
|
|
|
'woocommerce_manage_stock', |
190
|
|
|
'woocommerce_force_ssl_checkout', |
191
|
|
|
'woocommerce_hide_out_of_stock_items', |
192
|
|
|
'woocommerce_file_download_method', |
193
|
|
|
'woocommerce_enable_signup_and_login_from_checkout', |
194
|
|
|
'woocommerce_enable_shipping_calc', |
195
|
|
|
'woocommerce_enable_review_rating', |
196
|
|
|
'woocommerce_enable_guest_checkout', |
197
|
|
|
'woocommerce_enable_coupons', |
198
|
|
|
'woocommerce_enable_checkout_login_reminder', |
199
|
|
|
'woocommerce_enable_ajax_add_to_cart', |
200
|
|
|
'woocommerce_dimension_unit', |
201
|
|
|
'woocommerce_default_country', |
202
|
|
|
'woocommerce_default_customer_address', |
203
|
|
|
'woocommerce_currency_pos', |
204
|
|
|
'woocommerce_api_enabled', |
205
|
|
|
'woocommerce_allow_tracking', |
206
|
|
|
); |
207
|
|
|
|
208
|
|
|
private static $wc_constants_whitelist = array( |
209
|
|
|
// woocommerce options |
210
|
|
|
'WC_PLUGIN_FILE', |
211
|
|
|
'WC_ABSPATH', |
212
|
|
|
'WC_PLUGIN_BASENAME', |
213
|
|
|
'WC_VERSION', |
214
|
|
|
'WOOCOMMERCE_VERSION', |
215
|
|
|
'WC_ROUNDING_PRECISION', |
216
|
|
|
'WC_DISCOUNT_ROUNDING_MODE', |
217
|
|
|
'WC_TAX_ROUNDING_MODE', |
218
|
|
|
'WC_DELIMITER', |
219
|
|
|
'WC_LOG_DIR', |
220
|
|
|
'WC_SESSION_CACHE_GROUP', |
221
|
|
|
'WC_TEMPLATE_DEBUG_MODE', |
222
|
|
|
); |
223
|
|
|
|
224
|
|
|
private static $wc_post_meta_whitelist = array( |
225
|
|
|
// woocommerce products |
226
|
|
|
// https://github.com/woocommerce/woocommerce/blob/8ed6e7436ff87c2153ed30edd83c1ab8abbdd3e9/includes/data-stores/class-wc-product-data-store-cpt.php#L21 |
227
|
|
|
'_visibility', |
228
|
|
|
'_sku', |
229
|
|
|
'_price', |
230
|
|
|
'_regular_price', |
231
|
|
|
'_sale_price', |
232
|
|
|
'_sale_price_dates_from', |
233
|
|
|
'_sale_price_dates_to', |
234
|
|
|
'total_sales', |
235
|
|
|
'_tax_status', |
236
|
|
|
'_tax_class', |
237
|
|
|
'_manage_stock', |
238
|
|
|
'_backorders', |
239
|
|
|
'_sold_individually', |
240
|
|
|
'_weight', |
241
|
|
|
'_length', |
242
|
|
|
'_width', |
243
|
|
|
'_height', |
244
|
|
|
'_upsell_ids', |
245
|
|
|
'_crosssell_ids', |
246
|
|
|
'_purchase_note', |
247
|
|
|
'_default_attributes', |
248
|
|
|
'_product_attributes', |
249
|
|
|
'_virtual', |
250
|
|
|
'_downloadable', |
251
|
|
|
'_download_limit', |
252
|
|
|
'_download_expiry', |
253
|
|
|
'_featured', |
254
|
|
|
'_downloadable_files', |
255
|
|
|
'_wc_rating_count', |
256
|
|
|
'_wc_average_rating', |
257
|
|
|
'_wc_review_count', |
258
|
|
|
'_variation_description', |
259
|
|
|
'_thumbnail_id', |
260
|
|
|
'_file_paths', |
261
|
|
|
'_product_image_gallery', |
262
|
|
|
'_product_version', |
263
|
|
|
'_wp_old_slug', |
264
|
|
|
|
265
|
|
|
// woocommerce orders |
266
|
|
|
// https://github.com/woocommerce/woocommerce/blob/8ed6e7436ff87c2153ed30edd83c1ab8abbdd3e9/includes/data-stores/class-wc-order-data-store-cpt.php#L27 |
267
|
|
|
'_order_key', |
268
|
|
|
'_order_currency', |
269
|
|
|
// '_billing_first_name', do not sync these as they contain personal data |
270
|
|
|
// '_billing_last_name', |
271
|
|
|
// '_billing_company', |
272
|
|
|
// '_billing_address_1', |
273
|
|
|
// '_billing_address_2', |
274
|
|
|
'_billing_city', |
275
|
|
|
'_billing_state', |
276
|
|
|
'_billing_postcode', |
277
|
|
|
'_billing_country', |
278
|
|
|
// '_billing_email', do not sync these as they contain personal data |
279
|
|
|
// '_billing_phone', |
280
|
|
|
// '_shipping_first_name', |
281
|
|
|
// '_shipping_last_name', |
282
|
|
|
// '_shipping_company', |
283
|
|
|
// '_shipping_address_1', |
284
|
|
|
// '_shipping_address_2', |
285
|
|
|
'_shipping_city', |
286
|
|
|
'_shipping_state', |
287
|
|
|
'_shipping_postcode', |
288
|
|
|
'_shipping_country', |
289
|
|
|
'_completed_date', |
290
|
|
|
'_paid_date', |
291
|
|
|
'_cart_discount', |
292
|
|
|
'_cart_discount_tax', |
293
|
|
|
'_order_shipping', |
294
|
|
|
'_order_shipping_tax', |
295
|
|
|
'_order_tax', |
296
|
|
|
'_order_total', |
297
|
|
|
'_payment_method', |
298
|
|
|
'_payment_method_title', |
299
|
|
|
// '_transaction_id', do not sync these as they contain personal data |
300
|
|
|
// '_customer_ip_address', |
301
|
|
|
// '_customer_user_agent', |
302
|
|
|
'_created_via', |
303
|
|
|
'_order_version', |
304
|
|
|
'_prices_include_tax', |
305
|
|
|
'_date_completed', |
306
|
|
|
'_date_paid', |
307
|
|
|
'_payment_tokens', |
308
|
|
|
'_billing_address_index', |
309
|
|
|
'_shipping_address_index', |
310
|
|
|
'_recorded_sales', |
311
|
|
|
'_recorded_coupon_usage_counts', |
312
|
|
|
// https://github.com/woocommerce/woocommerce/blob/8ed6e7436ff87c2153ed30edd83c1ab8abbdd3e9/includes/data-stores/class-wc-order-data-store-cpt.php#L539 |
313
|
|
|
'_download_permissions_granted', |
314
|
|
|
// https://github.com/woocommerce/woocommerce/blob/8ed6e7436ff87c2153ed30edd83c1ab8abbdd3e9/includes/data-stores/class-wc-order-data-store-cpt.php#L594 |
315
|
|
|
'_order_stock_reduced', |
316
|
|
|
|
317
|
|
|
// woocommerce order refunds |
318
|
|
|
// https://github.com/woocommerce/woocommerce/blob/b8a2815ae546c836467008739e7ff5150cb08e93/includes/data-stores/class-wc-order-refund-data-store-cpt.php#L20 |
319
|
|
|
'_order_currency', |
320
|
|
|
'_refund_amount', |
321
|
|
|
'_refunded_by', |
322
|
|
|
'_refund_reason', |
323
|
|
|
'_order_shipping', |
324
|
|
|
'_order_shipping_tax', |
325
|
|
|
'_order_tax', |
326
|
|
|
'_order_total', |
327
|
|
|
'_order_version', |
328
|
|
|
'_prices_include_tax', |
329
|
|
|
'_payment_tokens', |
330
|
|
|
); |
331
|
|
|
|
332
|
|
|
private static $wc_comment_meta_whitelist = array( |
333
|
|
|
'rating', |
334
|
|
|
); |
335
|
|
|
} |
336
|
|
|
|