Completed
Push — psr4-jetpack-sync-module ( 862b11...910f64 )
by
unknown
277:16 queued 269:36
created

WooCommerce   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 331
Duplicated Lines 2.42 %

Coupling/Cohesion

Components 5
Dependencies 0

Importance

Changes 0
Metric Value
dl 8
loc 331
c 0
b 0
f 0
rs 10
wmc 16
lcom 5
cbo 0

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Automattic\Jetpack\Sync\Modules;
4
^
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

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