Completed
Push — try/tracks-store-stats ( 85ef57...4d4c21 )
by
unknown
08:54
created

WC_Stats   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 121
rs 10
c 0
b 0
f 0
wmc 27
lcom 1
cbo 1

10 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 10 3
A isActiveStore() 0 21 4
A __construct() 0 8 1
A get_cart_ids() 0 4 2
A get_cart_quantities() 0 4 2
B get_store_page() 0 20 6
A get_session_id() 0 5 2
A register_s_script() 0 6 2
A register_params_scripts() 0 8 2
A collect_params() 0 14 3
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
		if ( ! WC_Stats::isActiveStore() ) {
20
			return;
21
		}
22
23
		if ( is_null( self::$instance ) ) {
24
			self::$instance = new WC_Stats();
25
		}
26
		return self::$instance;
27
	}
28
29
	public function isActiveStore() {
30
		// Tracking only Site pages
31
		if ( is_admin() ) {
32
			return false;
33
		}
34
		// Make sure Jetpack is installed and active
35
		if ( ! Jetpack::is_active() ) {
36
			return false;
37
		}
38
39
		/**
40
		 * Make sure WooCommerce is installed and active
41
		 *
42
		 * This action is documented in https://docs.woocommerce.com/document/create-a-plugin
43
		 */
44
		if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
45
			return false;
46
		}
47
48
		return true;
49
	}
50
51
	public function __construct() {
52
		$this->jetpack = Jetpack::init();
53
		// add store analytics parameters
54
		add_action( 'wp_enqueue_scripts', array( $this, 'register_params_scripts' ), 1 );
55
56
		// add s.js at the end
57
		add_action( 'wp_enqueue_scripts', array( $this, 'register_s_script' ), 10 );
58
	}
59
60
	public function get_cart_ids( $result, $item ) {
61
		$comma = strlen( $result ) > 0 ? ',' : '';
62
		return $result . $comma . $item['product_id'];
63
	}
64
65
	public function get_cart_quantities( $result, $item ) {
66
		$comma = strlen( $result ) > 0 ? ',' : '';
67
		return $result . $comma . $item['quantity'];
68
	}
69
70
	public function get_store_page( $post_type, $post_id ) {
71
		if ( 'product' === $post_type ) {
72
			return 'product';
73
		}
74
75
		switch ( $post_id ) {
76
			case get_option( 'woocommerce_cart_page_id' ):
77
				return 'cart_view';
78
			case get_option( 'woocommerce_checkout_page_id' ):
79
				global $wp;
80
				if ( false !== strpos( $wp->request, 'order-received' ) ) {
81
					return 'checkout_complete';
82
				}
83
				return 'checkout_view';
84
			case get_option( 'woocommerce_view_order_page_id' ):
85
				return 'view_order';
86
			default:
87
				return $post_type;
88
		}
89
	}
90
91
	public function get_session_id() {
92
		$session_handler = new WC_Session_Handler();
93
		$session = $session_handler->get_session_cookie();
94
		return $session ? $session[ 0 ] : null;
95
	}
96
97
	public function register_s_script() {
98
		if ( ! is_admin() ) {
99
			wp_register_script( 'wc_tk_s', 'https://stats.wp.com/s.js', null, null, true );
100
			wp_enqueue_script( 'wc_tk_s' );
101
		}
102
	}
103
104
	public function register_params_scripts() {
105
		if ( ! is_admin() ) {
106
			$event_params = $this->collect_params();
107
			wp_register_script( 'wc_tk', null, null, '1.0', true );
108
			wp_localize_script( 'wc_tk', 'tk_params', $event_params );
109
			wp_enqueue_script( 'wc_tk' );
110
		}
111
	}
112
113
	public function collect_params( $params = array() ) {
114
		if ( is_product() ) {
115
			$params[ 'blog_id' ] = Jetpack::get_option( 'id' );
116
			$post = get_post();
117
			$params[ 'product_id' ] = $post->ID;
118
		}
119
		if ( is_cart() ) {
120
			$cart = WC()->cart->get_cart();
121
			$params[ 'cart_id' ] = $this->get_session_id();
122
			$params[ 'cart_products' ] = array_reduce( $cart, array( $this, 'get_cart_ids' ), '' );
123
			$params[ 'cart_quantities' ] = array_reduce( $cart, array( $this, 'get_cart_quantities' ), '' );
124
		}
125
		return $params;
126
	}
127
}
128
129
WC_Stats::init();
130