Completed
Push — try/tracks-store-stats ( a40d45...85ef57 )
by
unknown
08:41
created

WC_Stats::get_session_id()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 5
rs 9.4285
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 get_session_id() {
97
		$session_handler = new WC_Session_Handler();
98
		$session = $session_handler->get_session_cookie();
99
		return $session ? $session[ 0 ] : null;
100
	}
101
102
	public function writeDataToDom() {
103
		$cart = WC()->cart->get_cart();
104
		$cart_ids = array_reduce( $cart, array( $this, 'get_cart_ids' ), '' );
105
		$cart_quantities = array_reduce( $cart, array( $this, 'get_cart_quantities' ), '' );
106
		// Is this the right id? Is WooCommerce id different?
107
		$blog_id = Jetpack::get_option( 'id' );
108
		$post = get_post();
109
		$post_id = $post->ID;
110
		$post_type = get_post_type( $post_id );
111
		$store_page = $this->get_store_page( $post_type, $post_id );
0 ignored issues
show
Unused Code introduced by
$store_page is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
112
		$order_number = $_GET['key'];
0 ignored issues
show
Unused Code introduced by
$order_number is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
113
		$tracks_identity = jetpack_tracks_get_identity( get_current_user_id() );
0 ignored issues
show
Unused Code introduced by
$tracks_identity is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
114
//		data-ut='" . $tracks_identity[ '_ut' ] . "'
115
//		data-ui='" . $tracks_identity[ '_ui' ] . "'>
116
		$session_id = $this->get_session_id();
117
118
		echo "
119
			<div 
120
				id='store_data'
121
				style='display: none;' 
122
				data-blog_id='" . $blog_id . "'
123
				data-post='" . $post_id . "'
124
				data-cart='" . $session_id . "'
125
				data-cart_i='" . $cart_ids . "'
126
				data-cart_q='" . $cart_quantities . "'>
127
				
128
			</div>
129
		";
130
	}
131
}
132
133
WC_Stats::init();
134