Completed
Push — fix/tiled-gallery-amp-mosaic-c... ( b708e7...398dc2 )
by Yaroslav
08:06
created

Jetpack_WooCommerce_Analytics::get_instance()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 0
dl 0
loc 10
rs 9.9332
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/class-jetpack-woocommerce-analytics-universal.php' );
13
14
/**
15
 * Class Jetpack_WooCommerce_Analytics
16
 * Instantiate WooCommerce Analytics
17
 */
18
class Jetpack_WooCommerce_Analytics {
19
20
	/**
21
	 * Instance of this class
22
	 *
23
	 * @var Jetpack_WooCommerce_Analytics - Static property to hold our singleton instance
24
	 */
25
	private static $instance = false;
26
27
	/**
28
	 * Instance of the Universal functions
29
	 *
30
	 * @var Static property to hold concrete analytics impl that does the work (universal or legacy)
31
	 */
32
	private static $analytics = false;
33
34
	/**
35
	 * WooCommerce Analytics is only available to Jetpack connected WooCommerce stores with both plugins set to active
36
	 * and WooCommerce version 3.0 or higher
37
	 *
38
	 * @return bool
39
	 */
40
	public static function should_track_store() {
41
		/**
42
		 * Make sure WooCommerce is installed and active
43
		 *
44
		 * This action is documented in https://docs.woocommerce.com/document/create-a-plugin
45
		 */
46
		if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', Jetpack::get_active_plugins() ), true ) ) {
47
			return false;
48
		}
49
		// Tracking only Site pages.
50
		if ( is_admin() ) {
51
			return false;
52
		}
53
		// Don't track site admins.
54
		if ( is_user_logged_in() && in_array( 'administrator', wp_get_current_user()->roles, true ) ) {
55
			return false;
56
		}
57
		// Make sure Jetpack is installed and active.
58
		if ( ! Jetpack::is_active() ) {
59
			return false;
60
		}
61
		// Ensure the WooCommerce class exists and is a valid version.
62
		$minimum_woocommerce_active = class_exists( 'WooCommerce' ) && version_compare( WC_VERSION, '3.0', '>=' );
63
		if ( ! $minimum_woocommerce_active ) {
64
			return false;
65
		}
66
		return true;
67
	}
68
69
	/**
70
	 * This is our constructor, which is private to force the use of get_instance()
71
	 *
72
	 * @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...
73
	 */
74
	private function __construct() {
75
		self::$analytics = new Jetpack_WooCommerce_Analytics_Universal();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Jetpack_WooCommerce_Analytics_Universal() of type object<Jetpack_WooCommerce_Analytics_Universal> is incompatible with the declared type object<Static> of property $analytics.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
76
	}
77
78
	/**
79
	 * Function to instantiate our class and make it a singleton
80
	 */
81
	public static function get_instance() {
82
		if ( ! self::should_track_store() ) {
83
			return;
84
		}
85
		if ( ! self::$instance ) {
86
			self::$instance = new self();
87
		}
88
89
		return self::$instance;
90
	}
91
}
92
93
global $jetpack_woocommerce_analytics;
94
$jetpack_woocommerce_analytics = Jetpack_WooCommerce_Analytics::get_instance();
95