|
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 $meta_whitelist = array( |
|
8
|
|
|
'_product_id', |
|
9
|
|
|
'_variation_id', |
|
10
|
|
|
'_qty', |
|
11
|
|
|
'_tax_class', |
|
12
|
|
|
'_line_subtotal', |
|
13
|
|
|
'_line_subtotal_tax', |
|
14
|
|
|
'_line_total', |
|
15
|
|
|
'_line_tax', |
|
16
|
|
|
'_line_tax_data', |
|
17
|
|
|
); |
|
18
|
|
|
|
|
19
|
|
|
private $order_item_table_name; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct() { |
|
22
|
|
|
global $wpdb; |
|
23
|
|
|
$this->order_item_table_name = $wpdb->prefix . 'woocommerce_order_items'; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
function name() { |
|
27
|
|
|
return "woocommerce"; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function init_listeners( $callable ) { |
|
31
|
|
|
// orders |
|
32
|
|
|
add_action( 'woocommerce_new_order', $callable, 10, 1 ); |
|
33
|
|
|
add_action( 'woocommerce_order_status_changed', $callable, 10, 3 ); |
|
34
|
|
|
add_action( 'woocommerce_payment_complete', $callable, 10, 1 ); |
|
35
|
|
|
|
|
36
|
|
|
// order items |
|
37
|
|
|
add_action( 'woocommerce_new_order_item', $callable, 10, 4 ); |
|
38
|
|
|
add_action( 'woocommerce_update_order_item', $callable, 10, 4 ); |
|
39
|
|
|
add_filter( 'jetpack_sync_before_enqueue_woocommerce_new_order_item', array( $this, 'filter_order_item' ) ); |
|
40
|
|
|
add_filter( 'jetpack_sync_before_enqueue_woocommerce_update_order_item', array( $this, 'filter_order_item' ) ); |
|
41
|
|
|
|
|
42
|
|
|
// order item meta |
|
43
|
|
|
$this->init_listeners_for_meta_type( 'order_item', $callable ); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function init_full_sync_listeners( $callable ) { |
|
47
|
|
|
add_action( 'jetpack_full_sync_woocommerce_order_items', $callable ); // also sends post meta |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function get_full_sync_actions() { |
|
51
|
|
|
return array( 'jetpack_full_sync_woocommerce_order_items' ); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function init_before_send() { |
|
55
|
|
|
// full sync |
|
56
|
|
|
add_filter( 'jetpack_sync_before_send_jetpack_full_sync_woocommerce_order_items', array( $this, 'expand_order_item_ids' ) ); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function filter_order_item( $args ) { |
|
60
|
|
|
$args[1] = $this->build_order_item( $args[1] ); |
|
61
|
|
|
return $args; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function expand_order_item_ids( $args ) { |
|
65
|
|
|
$order_item_ids = $args[0]; |
|
66
|
|
|
|
|
67
|
|
|
global $wpdb; |
|
68
|
|
|
|
|
69
|
|
|
$order_item_ids_sql = implode( ', ', array_map( 'intval', $order_item_ids ) ); |
|
70
|
|
|
|
|
71
|
|
|
$order_items = $wpdb->get_results( |
|
72
|
|
|
"SELECT * FROM $this->order_item_table_name WHERE order_item_id IN ( $order_item_ids_sql )" |
|
73
|
|
|
); |
|
74
|
|
|
|
|
75
|
|
|
return array( |
|
76
|
|
|
$order_items, |
|
77
|
|
|
$this->get_metadata( $order_item_ids, 'order_item', $this->meta_whitelist ) |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function build_order_item( $order_item ) { |
|
82
|
|
|
if ( is_numeric( $order_item ) ) { |
|
83
|
|
|
global $wpdb; |
|
84
|
|
|
return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $this->order_item_table_name WHERE order_item_id = %d", $order_item ) ); |
|
85
|
|
|
} elseif ( is_array( $order_item ) ) { |
|
86
|
|
|
return $order_item; |
|
87
|
|
|
} else { |
|
88
|
|
|
return (object)array( |
|
89
|
|
|
'order_item_id' => $order_item->get_id(), |
|
90
|
|
|
'order_item_type' => $order_item->get_type(), |
|
91
|
|
|
'order_item_name' => $order_item->get_name(), |
|
92
|
|
|
'order_id' => $order_item->get_order_id(), |
|
93
|
|
|
); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
|
98
|
|
|
global $wpdb; |
|
99
|
|
|
|
|
100
|
|
|
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 ); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
View Code Duplication |
public function estimate_full_sync_actions( $config ) { |
|
104
|
|
|
global $wpdb; |
|
105
|
|
|
|
|
106
|
|
|
$query = "SELECT count(*) FROM $this->order_item_table_name WHERE " . $this->get_where_sql( $config ); |
|
107
|
|
|
$count = $wpdb->get_var( $query ); |
|
108
|
|
|
|
|
109
|
|
|
return (int) ceil( $count / self::ARRAY_CHUNK_SIZE ); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
private function get_where_sql( $config ) { |
|
|
|
|
|
|
113
|
|
|
return '1=1'; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.