Completed
Push — fix/woocommerce-order-item-met... ( 50dbba...3f3f51 )
by
unknown
29:49
created

Jetpack_Sync_Module_WooCommerce::get_where_sql()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
require_once JETPACK__PLUGIN_DIR . '/sync/class.jetpack-sync-module.php';
4
5
class Jetpack_Sync_Module_WooCommerce extends Jetpack_Sync_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
48
		add_filter( 'jetpack_sync_before_enqueue_woocommerce_new_order_item', array( $this, 'filter_order_item' ) );
49
		add_filter( 'jetpack_sync_before_enqueue_woocommerce_update_order_item', array( $this, 'filter_order_item' ) );
50
	}
51
52
	function name() {
53
		return "woocommerce";
54
	}
55
56
	public function init_listeners( $callable ) {
57
		// orders
58
		add_action( 'woocommerce_new_order', $callable, 10, 1 );
59
		add_action( 'woocommerce_order_status_changed', $callable, 10, 3 );
60
		add_action( 'woocommerce_payment_complete', $callable, 10, 1 );
61
62
		// order items
63
		add_action( 'woocommerce_new_order_item', $callable, 10, 4 );
64
		add_action( 'woocommerce_update_order_item', $callable, 10, 4 );
65
66
		// order item meta
67
		$this->init_listeners_for_meta_type( 'order_item', $callable );
68
	}
69
70
	public function init_full_sync_listeners( $callable ) {
71
		add_action( 'jetpack_full_sync_woocommerce_order_items', $callable ); // also sends post meta
72
	}
73
74
	public function get_full_sync_actions() {
75
		return array( 'jetpack_full_sync_woocommerce_order_items' );
76
	}
77
78
	public function init_before_send() {
79
		// full sync
80
		add_filter( 'jetpack_sync_before_send_jetpack_full_sync_woocommerce_order_items', array( $this, 'expand_order_item_ids' ) );
81
	}
82
83
	public function filter_order_item( $args ) {
84
		$args[1] = $this->build_order_item( $args[1] );
85
		return $args;
86
	}
87
88
	public function expand_order_item_ids( $args ) {
89
		$order_item_ids = $args[0];
90
91
		global $wpdb;
92
93
		$order_item_ids_sql = implode( ', ', array_map( 'intval', $order_item_ids ) );
94
95
		$order_items = $wpdb->get_results(
96
			"SELECT * FROM $this->order_item_table_name WHERE order_item_id IN ( $order_item_ids_sql )"
97
		);
98
99
		return array(
100
			$order_items,
101
			$this->get_metadata( $order_item_ids, 'order_item', $this->order_item_meta_whitelist ),
102
		);
103
	}
104
105
	public function build_order_item( $order_item ) {
106
		if ( is_numeric( $order_item ) ) {
107
			global $wpdb;
108
			return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $this->order_item_table_name WHERE order_item_id = %d", $order_item ) );
109
		} elseif ( is_array( $order_item ) ) {
110
			return $order_item;
111
		} else {
112
			return (object)array(
113
				'order_item_id'   => $order_item->get_id(),
114
				'order_item_type' => $order_item->get_type(),
115
				'order_item_name' => $order_item->get_name(),
116
				'order_id'        => $order_item->get_order_id(),
117
			);
118
		}
119
	}
120
121
	public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
122
		global $wpdb;
123
124
		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 );
125
	}
126
127 View Code Duplication
	public function estimate_full_sync_actions( $config ) {
128
		global $wpdb;
129
130
		$query = "SELECT count(*) FROM $this->order_item_table_name WHERE " . $this->get_where_sql( $config );
131
		$count = $wpdb->get_var( $query );
132
133
		return (int) ceil( $count / self::ARRAY_CHUNK_SIZE );
134
	}
135
136
	private function get_where_sql( $config ) {
137
		return '1=1';
138
	}
139
140
	public function add_woocommerce_options_whitelist( $list ) {
141
		return array_merge( $list, self::$wc_options_whitelist );
142
	}
143
144
	public function add_woocommerce_constants_whitelist( $list ) {
145
		return array_merge( $list, self::$wc_constants_whitelist );
146
	}
147
148
	public function add_woocommerce_post_meta_whitelist( $list ) {
149
		return array_merge( $list, self::$wc_post_meta_whitelist );
150
	}
151
152
	private static $wc_options_whitelist = array(
153
		'woocommerce_currency',
154
		'woocommerce_db_version',
155
		'woocommerce_weight_unit',
156
		'woocommerce_version',
157
		'woocommerce_unforce_ssl_checkout',
158
		'woocommerce_tax_total_display',
159
		'woocommerce_tax_round_at_subtotal',
160
		'woocommerce_tax_display_shop',
161
		'woocommerce_tax_display_cart',
162
		'woocommerce_prices_include_tax',
163
		'woocommerce_price_thousand_sep',
164
		'woocommerce_price_num_decimals',
165
		'woocommerce_price_decimal_sep',
166
		'woocommerce_notify_low_stock',
167
		'woocommerce_notify_low_stock_amount',
168
		'woocommerce_notify_no_stock',
169
		'woocommerce_notify_no_stock_amount',
170
		'woocommerce_manage_stock',
171
		'woocommerce_force_ssl_checkout',
172
		'woocommerce_hide_out_of_stock_items',
173
		'woocommerce_file_download_method',
174
		'woocommerce_enable_signup_and_login_from_checkout',
175
		'woocommerce_enable_shipping_calc',
176
		'woocommerce_enable_review_rating',
177
		'woocommerce_enable_guest_checkout',
178
		'woocommerce_enable_coupons',
179
		'woocommerce_enable_checkout_login_reminder',
180
		'woocommerce_enable_ajax_add_to_cart',
181
		'woocommerce_dimension_unit',
182
		'woocommerce_default_country',
183
		'woocommerce_default_customer_address',
184
		'woocommerce_currency_pos',
185
		'woocommerce_api_enabled',
186
		'woocommerce_allow_tracking',
187
	);
188
189
	private static $wc_constants_whitelist = array(
190
		//woocommerce options
191
		'WC_PLUGIN_FILE',
192
		'WC_ABSPATH',
193
		'WC_PLUGIN_BASENAME',
194
		'WC_VERSION',
195
		'WOOCOMMERCE_VERSION',
196
		'WC_ROUNDING_PRECISION',
197
		'WC_DISCOUNT_ROUNDING_MODE',
198
		'WC_TAX_ROUNDING_MODE',
199
		'WC_DELIMITER',
200
		'WC_LOG_DIR',
201
		'WC_SESSION_CACHE_GROUP',
202
		'WC_TEMPLATE_DEBUG_MODE',
203
	);
204
205
	private static $wc_post_meta_whitelist = array(
206
		//woocommerce products
207
		'_stock_status',
208
		'_visibility',
209
		'total_sales',
210
		'_downloadable',
211
		'_virtual',
212
		'_regular_price',
213
		'_sale_price',
214
		'_tax_status',
215
		'_tax_class',
216
		'_featured',
217
		'_price',
218
		'_stock',
219
		'_backorders',
220
		'_manage_stock',
221
222
		//woocommerce orders
223
		'_order_currency',
224
		'_prices_include_tax',
225
		'_created_via',
226
		'_billing_country',
227
		'_billing_city',
228
		'_billing_state',
229
		'_billing_postcode',
230
		'_shipping_country',
231
		'_shipping_city',
232
		'_shipping_state',
233
		'_shipping_postcode',
234
		'_payment_method',
235
		'_payment_method_title',
236
		'_order_shipping',
237
		'_cart_discount',
238
		'_cart_discount_tax',
239
		'_order_tax',
240
		'_order_shipping_tax',
241
		'_order_total',
242
		'_download_permissions_granted',
243
		'_recorded_sales',
244
		'_order_stock_reduced',
245
	);
246
}
247