Completed
Push — update/remove-disconnect-link ( 4b6a2c )
by
unknown
73:18 queued 63:47
created

Jetpack_Sync_Module_WooCommerce   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 111
Duplicated Lines 7.21 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 8
loc 111
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 1

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A name() 0 3 1
A init_listeners() 0 15 1
A init_full_sync_listeners() 0 3 1
A get_full_sync_actions() 0 3 1
A init_before_send() 0 4 1
A filter_order_item() 0 4 1
A expand_order_item_ids() 0 16 1
A build_order_item() 0 15 3
A enqueue_full_sync_actions() 0 5 1
A estimate_full_sync_actions() 8 8 1
A get_where_sql() 0 3 1

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
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 ) {
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
113
		return '1=1';
114
	}
115
}
116