Completed
Push — try/tracks-store-stats ( 87fce4...010ef0 )
by
unknown
11:05
created

WC_Stats::track()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 58
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 48
nc 3
nop 0
dl 0
loc 58
rs 9.639
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
		// Make sure WooCommerce is installed and active
28
		if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
29
			return;
30
		}
31
		if ( is_null( self::$instance ) ) {
32
			self::$instance = new WC_Stats();
33
		}
34
		return self::$instance;
35
	}
36
37
	public function __construct() {
38
		$this->jetpack = Jetpack::init();
39
40
		add_action( 'wp_enqueue_scripts', array( $this, 'track' ) );
41
		add_action( 'rest_api_init', function() {
42
			register_rest_route( 'jetpack/v4', '/tracks', array(
43
				'methods' => 'POST',
44
				'callback' => array( $this, 'handle_client_tracks' ),
45
			) );
46
		} );
47
	}
48
49
	public function handle_client_tracks( $request ) {
50
		return json_encode( $request->get_params() );
51
	}
52
53
	public function get_cart_ids( $item ) {
54
		return $item[ 'product_id' ];
55
	}
56
57
	public function get_cart_quantities( $item ) {
58
		return $item[ 'quantity' ];
59
	}
60
61
	public function get_uncachable_page_type( $post_id ) {
62
63
		switch ( $post_id ) {
64
			case get_option( 'woocommerce_cart_page_id' ):
65
				return 'cart_view';
66
			case get_option( 'woocommerce_checkout_page_id' ):
67
				global $wp;
68
				if ( strpos( $wp->request, 'order-received' ) !== false ) {
69
					return 'checkout_complete';
70
				}
71
				return 'checkout_view';
72
			case get_option( 'woocommerce_view_order_page_id' ):
73
				return 'view_order';
74
			default:
75
				null;
76
		}
77
	}
78
79
	public function track() {
80
		$cart = WC()->cart->get_cart();
81
		$cart_ids = array_map( array( $this, 'get_cart_ids' ), $cart );
82
		$cart_quantities = array_map( array( $this, 'get_cart_quantities' ), $cart );
83
		// Is this the right id? Is WooCommerce id different?
84
		$store_id = Jetpack::get_option( 'id' );
85
		$post = get_post();
86
		$post_id = $post->ID;
87
		$post_type = get_post_type( $post_id );
88
		$post_name = $post->post_name;
89
		$uncachable_page_type = $this->get_uncachable_page_type( $post_id );
90
		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...
91
			echo "<pre style=\"font-size: 12px;\">";
92
			echo "Tracks properties sent by PHP:";
93
			echo "<br/>";
94
			echo "post type: ";
95
			print_r( $post_type );
96
			echo "<br/>";
97
			echo "post name: ";
98
			print_r( $post_name );
99
			echo "<br/>";
100
			echo "post id: ";
101
			print_r( $post_id );
102
			echo "<br/>";
103
			echo "store id: ";
104
			print_r( $store_id );
105
			echo "<br/>";
106
			echo "page type: ";
107
			print_r( $uncachable_page_type );
108
			echo "<br/>";
109
			echo "cart ids: ";
110
			print_r( $cart_ids );
111
			echo "<br/>";
112
			echo "cart quantities: ";
113
			print_r( $cart_quantities );
114
			if ( isset( $_GET[ 'key' ] ) ) {
115
				echo "<br/>";
116
				echo "order number: ";
117
				print_r( $_GET[ 'key' ] );
118
			}
119
			echo "</pre>";
120
		} else {
121
			wc_enqueue_js(
122
				"var data = {
123
					'post_type': '" . $post_type . "',
124
					'post_id': " . $post_id . ",
125
					'post_name': '" . $post_name . "',
126
					'store_id': " . $store_id . ",
127
					'cart_ids': " . json_encode( $cart_ids ) . ",
128
					'cart_quantities': " . json_encode( $cart_quantities ) . "
129
				};
130
				jQuery.post( 'wp-json/jetpack/v4/tracks', data, function(response) {
131
					console.log( 'Tracks properties sent by PHP:' );
132
					console.log( JSON.parse( response ) );
133
				});"
134
			);
135
		}
136
	}
137
}
138
139
WC_Stats::init();
140