Completed
Push — fix/search/unconfigured-widget ( d944f9...67a1b8 )
by Alex
35:00 queued 25:31
created

Jetpack_WooCommerce_Analytics::get_instance()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 0
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Jetpack_WooCommerce_Analytics is ported from the Jetpack_Google_Analytics code.
4
 *
5
 * @package Jetpack
6
 */
7
8
if ( ! defined( 'ABSPATH' ) ) {
9
	exit;
10
}
11
12
require_once plugin_basename( 'classes/wp-woocommerce-analytics-utils.php' );
13
require_once plugin_basename( 'classes/wp-woocommerce-analytics-universal.php' );
14
15
/**
16
 * Class Jetpack_WooCommerce_Analytics
17
 * Instantiate WooCommerce Analytics
18
 */
19
class Jetpack_WooCommerce_Analytics {
20
21
	/**
22
	 * Instance of this class
23
	 *
24
	 * @var Jetpack_WooCommerce_Analytics - Static property to hold our singleton instance
25
	 */
26
	private static $instance = false;
27
28
	/**
29
	 * Instance of the Universal functions
30
	 *
31
	 * @var Static property to hold concrete analytics impl that does the work (universal or legacy)
32
	 */
33
	private static $analytics = false;
0 ignored issues
show
Unused Code introduced by
The property $analytics is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
34
35
	/**
36
	 * WooCommerce Analytics is only available to Jetpack connected WooCommerce stores with both plugins set to active
37
	 * and WooCommerce version 3.0 or higher
38
	 *
39
	 * @return bool
40
	 */
41
	public static function shouldTrackStore() {
42
		// Tracking only Site pages
43
		if ( is_admin() ) {
44
			return false;
45
		}
46
		// Don't track site admins
47
		if ( is_user_logged_in() && in_array( 'administrator',  wp_get_current_user()->roles ) ) {
48
			return false;
49
		}
50
		// Make sure Jetpack is installed and active
51
		if ( ! Jetpack::is_active() ) {
52
			return false;
53
		}
54
		/**
55
		 * Make sure WooCommerce is installed and active
56
		 *
57
		 * This action is documented in https://docs.woocommerce.com/document/create-a-plugin
58
		 */
59
		if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
60
			return false;
61
		}
62
63
		// Ensure the WooCommerce class exists and is a valid version
64
		$minimum_woocommerce_active = class_exists( 'WooCommerce' ) && version_compare( WC_VERSION, '3.0', '>=' );
65
		if ( ! $minimum_woocommerce_active ) {
66
			return false;
67
		}
68
		return true;
69
	}
70
71
	/**
72
	 * This is our constructor, which is private to force the use of get_instance()
73
	 *
74
	 * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
75
	 */
76
	private function __construct() {
77
		$analytics = new Jetpack_WooCommerce_Analytics_Universal();
0 ignored issues
show
Unused Code introduced by
$analytics 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...
78
	}
79
80
	/**
81
	 * Function to instantiate our class and make it a singleton
82
	 */
83
	public static function get_instance() {
84
		if ( ! Jetpack_WooCommerce_Analytics::shouldTrackStore() ) {
85
			return;
86
		}
87
		if ( ! self::$instance ) {
88
			self::$instance = new self();
89
		}
90
91
		return self::$instance;
92
	}
93
}
94
95
global $jetpack_woocommerce_analytics;
96
$jetpack_woocommerce_analytics = Jetpack_WooCommerce_Analytics::get_instance();
97