Completed
Push — try/tracks-store-stats ( a96bfe...cd41c9 )
by
unknown
08:11
created

WC_Stats::writeDataToDom()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 18
nc 1
nop 0
dl 0
loc 26
rs 8.8571
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
		// 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
		/**
28
		 * Make sure WooCommerce is installed and active
29
		 *
30
		 * This action is documented in https://docs.woocommerce.com/document/create-a-plugin
31
		 */
32
		if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
33
			return;
34
		}
35
		if ( is_null( self::$instance ) ) {
36
			self::$instance = new WC_Stats();
37
		}
38
		return self::$instance;
39
	}
40
41
	public function __construct() {
42
		$this->jetpack = Jetpack::init();
43
44
		add_action( 'wp_enqueue_scripts', array( $this, 'writeDataToDom' ) );
45
	}
46
47
	public function get_cart_ids( $result, $item ) {
48
		$comma = strlen( $result ) > 0 ? ',' : '';
49
		return $result . $comma . $item['product_id'];
50
	}
51
52
	public function get_cart_quantities( $result, $item ) {
53
		$comma = strlen( $result ) > 0 ? ',' : '';
54
		return $result . $comma . $item['quantity'];
55
	}
56
57
	public function get_store_page( $post_type, $post_id ) {
58
		if ( 'product' === $post_type ) {
59
			return 'product';
60
		}
61
62
		switch ( $post_id ) {
63
			case get_option( 'woocommerce_cart_page_id' ):
64
				return 'cart_view';
65
			case get_option( 'woocommerce_checkout_page_id' ):
66
				global $wp;
67
				if ( false !== strpos( $wp->request, 'order-received' ) ) {
68
					return 'checkout_complete';
69
				}
70
				return 'checkout_view';
71
			case get_option( 'woocommerce_view_order_page_id' ):
72
				return 'view_order';
73
			default:
74
				return $post_type;
75
		}
76
	}
77
78
	public function writeDataToDom() {
79
		$cart = WC()->cart->get_cart();
80
		$cart_ids = array_reduce( $cart, array( $this, 'get_cart_ids' ), '' );
81
		$cart_quantities = array_reduce( $cart, array( $this, 'get_cart_quantities' ), '' );
82
		// Is this the right id? Is WooCommerce id different?
83
		$store_id = Jetpack::get_option( 'id' );
84
		$post = get_post();
85
		$post_id = $post->ID;
86
		$post_type = get_post_type( $post_id );
87
		$store_page = $this->get_store_page( $post_type, $post_id );
88
		$order_number = $_GET['key'];
89
90
		echo "
91
			<div 
92
				id='store_data'
93
				style='display: none;' 
94
				data-store_id='" . $store_id . "'
95
				data-post_type='" . $post_type . "'
96
				data-post_id='" . $post_id . "'
97
				data-store_page='" . $store_page . "'
98
				data-cart_ids='" . $cart_ids . "'
99
				data-cart_quantities='" . $cart_quantities . "'
100
				data-order_number='" . $order_number . "'>
101
			</div>
102
		";
103
	}
104
}
105
106
WC_Stats::init();
107