Completed
Push — try/tracks-store-stats ( 6e0574...62793e )
by
unknown
08:40
created

WC_Stats   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 71
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
B init() 0 18 5
A __construct() 0 6 1
A get_cart_ids() 0 3 1
A get_cart_quantities() 0 3 1
B track() 0 25 1
1
<?php
2
3
if ( ! defined( 'ABSPATH' ) ) {
4
	exit;
5
}
6
7
class WC_Stats {
8
	/**
9
	 * @var Jetpack
10
	 **/
11
	private $jetpack;
12
13
	/**
14
	 * @var WC_Stats
15
	 **/
16
	private static $instance = null;
17
18
	static function init() {
19
		// Tracking only Site pages
20
		if ( is_admin() ) {
21
			return;
22
		}
23
		// Make sure Jetpack is installed and active
24
		if ( ! Jetpack::is_active() ) {
25
			return;
26
		}
27
		// Make sure WooCommerce is installed and active
28
		if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
29
			return;
30
		}
31
		if ( is_null( self::$instance ) ) {
32
			self::$instance = new WC_Stats();
33
		}
34
		return self::$instance;
35
	}
36
37
	public function __construct() {
38
		$this->jetpack = Jetpack::init();
39
40
		// event woocommerce_init ??
41
		add_action( 'wp_enqueue_scripts', array( $this, 'track' ) );
42
	}
43
44
	public function get_cart_ids( $item ) {
45
		return $item[ 'product_id' ];
46
	}
47
48
	public function get_cart_quantities( $item ) {
49
		return $item[ 'quantity' ];
50
	}
51
52
	public function track() {
53
		$cart = WC()->cart->get_cart();
54
		$cart_ids = array_map( array( $this, 'get_cart_ids' ), $cart );
55
		$cart_quantities = array_map( array( $this, 'get_cart_quantities' ), $cart );
56
		$store_id = Jetpack::get_option( 'id' );
57
		$post_id = get_the_ID();
58
		$post_type = get_post_type( $post_id );
59
60
		echo "<pre>";
61
		echo "post type: ";
62
		print_r( $post_type );
63
		echo "<br/>";
64
		echo "post id: ";
65
		print_r( $post_id );
66
		echo "<br/>";
67
		echo "store id: ";
68
		print_r( $store_id );
69
		echo "<br/>";
70
		echo "cart ids: ";
71
		print_r( $cart_ids );
72
		echo "<br/>";
73
		echo "cart quantities: ";
74
		print_r( $cart_quantities );
75
		echo "</pre>";
76
	}
77
}
78
79
WC_Stats::init();
80