|
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 ); |
|
|
|
|
|
|
112
|
|
|
$order_number = $_GET['key']; |
|
|
|
|
|
|
113
|
|
|
$tracks_identity = jetpack_tracks_get_identity( get_current_user_id() ); |
|
|
|
|
|
|
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
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.