Completed
Push — try/tracks-store-stats ( 010ef0...d817f1 )
by
unknown
39:08 queued 30:43
created

WC_Stats::handle_client_tracks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
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, 'track' ) );
45
	}
46
47
	public function handle_client_tracks( $request ) {
48
		return json_encode( $request->get_params() );
49
	}
50
51
	public function get_cart_ids( $item ) {
52
		return $item['product_id'];
53
	}
54
55
	public function get_cart_quantities( $item ) {
56
		return $item['quantity'];
57
	}
58
59
	public function get_uncachable_page_type( $post_id ) {
60
61
		switch ( $post_id ) {
62
			case get_option( 'woocommerce_cart_page_id' ):
63
				return 'cart_view';
64
			case get_option( 'woocommerce_checkout_page_id' ):
65
				global $wp;
66
				if ( false !== strpos( $wp->request, 'order-received' ) ) {
67
					return 'checkout_complete';
68
				}
69
				return 'checkout_view';
70
			case get_option( 'woocommerce_view_order_page_id' ):
71
				return 'view_order';
72
			default:
73
				null;
74
		}
75
	}
76
77
	public function track() {
78
		$cart = WC()->cart->get_cart();
79
		$cart_ids = array_map( array( $this, 'get_cart_ids' ), $cart );
80
		$cart_quantities = array_map( array( $this, 'get_cart_quantities' ), $cart );
81
		// Is this the right id? Is WooCommerce id different?
82
		$store_id = Jetpack::get_option( 'id' );
83
		$post = get_post();
84
		$post_id = $post->ID;
85
		$post_type = get_post_type( $post_id );
86
		$post_name = $post->post_name;
87
		$uncachable_page_type = $this->get_uncachable_page_type( $post_id );
88
		if ( $uncachable_page_type ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $uncachable_page_type of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
89
			echo "<pre style=\"font-size: 12px;\">";
90
			echo "Tracks properties sent by PHP:";
91
			echo "<br/>";
92
			echo "post type: ";
93
			print_r( $post_type );
94
			echo "<br/>";
95
			echo "post name: ";
96
			print_r( $post_name );
97
			echo "<br/>";
98
			echo "post id: ";
99
			print_r( $post_id );
100
			echo "<br/>";
101
			echo "store id: ";
102
			print_r( $store_id );
103
			echo "<br/>";
104
			echo "page type: ";
105
			print_r( $uncachable_page_type );
106
			echo "<br/>";
107
			echo "cart ids: ";
108
			print_r( $cart_ids );
109
			echo "<br/>";
110
			echo "cart quantities: ";
111
			print_r( $cart_quantities );
112
			if ( isset( $_GET['key'] ) ) {
113
				echo "<br/>";
114
				echo "order number: ";
115
				print_r( $_GET['key'] );
116
			}
117
			echo "</pre>";
118
		}
119
	}
120
}
121
122
WC_Stats::init();
123