Completed
Push — try/tracks-store-stats ( cd41c9...75a9b5 )
by
unknown
07:52
created

WC_Stats::isActiveStore()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 0
dl 0
loc 21
rs 9.0534
c 0
b 0
f 0
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
	static function getScriptTag() {
30
		if ( WC_Stats::isActiveStore() ) {
31
			return "
32
				<script type='text/javascript' src='https://stats.wp.com/s.js'></script>
33
			";
34
		}
35
	}
36
37
	public function isActiveStore() {
38
		// Tracking only Site pages
39
		if ( is_admin() ) {
40
			return false;
41
		}
42
		// Make sure Jetpack is installed and active
43
		if ( ! Jetpack::is_active() ) {
44
			return false;
45
		}
46
47
		/**
48
		 * Make sure WooCommerce is installed and active
49
		 *
50
		 * This action is documented in https://docs.woocommerce.com/document/create-a-plugin
51
		 */
52
		if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
53
			return false;
54
		}
55
56
		return true;
57
	}
58
59
	public function __construct() {
60
		$this->jetpack = Jetpack::init();
61
62
		add_action( 'wp_enqueue_scripts', array( $this, 'writeDataToDom' ) );
63
	}
64
65
	public function get_cart_ids( $result, $item ) {
66
		$comma = strlen( $result ) > 0 ? ',' : '';
67
		return $result . $comma . $item['product_id'];
68
	}
69
70
	public function get_cart_quantities( $result, $item ) {
71
		$comma = strlen( $result ) > 0 ? ',' : '';
72
		return $result . $comma . $item['quantity'];
73
	}
74
75
	public function get_store_page( $post_type, $post_id ) {
76
		if ( 'product' === $post_type ) {
77
			return 'product';
78
		}
79
80
		switch ( $post_id ) {
81
			case get_option( 'woocommerce_cart_page_id' ):
82
				return 'cart_view';
83
			case get_option( 'woocommerce_checkout_page_id' ):
84
				global $wp;
85
				if ( false !== strpos( $wp->request, 'order-received' ) ) {
86
					return 'checkout_complete';
87
				}
88
				return 'checkout_view';
89
			case get_option( 'woocommerce_view_order_page_id' ):
90
				return 'view_order';
91
			default:
92
				return $post_type;
93
		}
94
	}
95
96
	public function writeDataToDom() {
97
		$cart = WC()->cart->get_cart();
98
		$cart_ids = array_reduce( $cart, array( $this, 'get_cart_ids' ), '' );
99
		$cart_quantities = array_reduce( $cart, array( $this, 'get_cart_quantities' ), '' );
100
		// Is this the right id? Is WooCommerce id different?
101
		$store_id = Jetpack::get_option( 'id' );
102
		$post = get_post();
103
		$post_id = $post->ID;
104
		$post_type = get_post_type( $post_id );
105
		$store_page = $this->get_store_page( $post_type, $post_id );
106
		$order_number = $_GET['key'];
107
108
		echo "
109
			<div 
110
				id='store_data'
111
				style='display: none;' 
112
				data-store_id='" . $store_id . "'
113
				data-post_type='" . $post_type . "'
114
				data-post_id='" . $post_id . "'
115
				data-store_page='" . $store_page . "'
116
				data-cart_ids='" . $cart_ids . "'
117
				data-cart_quantities='" . $cart_quantities . "'
118
				data-order_number='" . $order_number . "'>
119
			</div>
120
		";
121
	}
122
}
123
124
WC_Stats::init();
125