Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 46
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_WooCommerce_Analytics
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 5
380
0.00% covered (danger)
0.00%
0 / 1
 should_track_store
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
42
 __construct
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 wp_head_top
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
42
 enqueue_tracking_script
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
2
 get_instance
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * Jetpack_WooCommerce_Analytics is ported from the Jetpack_Google_Analytics code.
4 *
5 * @package automattic/jetpack
6 */
7
8if ( ! defined( 'ABSPATH' ) ) {
9    exit( 0 );
10}
11
12require __DIR__ . '/classes/class-jetpack-woocommerce-analytics-trait.php';
13require_once __DIR__ . '/classes/class-jetpack-woocommerce-analytics-universal.php';
14require_once __DIR__ . '/classes/class-jetpack-woocommerce-analytics-my-account.php';
15require_once __DIR__ . '/classes/class-jetpack-woocommerce-analytics-checkout-flow.php';
16
17/**
18 * Class Jetpack_WooCommerce_Analytics
19 * Instantiate WooCommerce Analytics
20 */
21class Jetpack_WooCommerce_Analytics {
22
23    /**
24     * Instance of this class
25     *
26     * @var Jetpack_WooCommerce_Analytics - Static property to hold our singleton instance
27     */
28    private static $instance = false;
29
30    /**
31     * Instance of the Universal functions
32     *
33     * @var Static property to hold concrete analytics implementation that does the work (universal or legacy)
34     */
35    private static $analytics = false;
36
37    /**
38     * Instance of the My account functions
39     *
40     * @var Static property to hold concrete analytics implementation that does the work.
41     */
42    private static $myaccount = false;
43
44    /**
45     * Instance of the Checkout Flow functions
46     *
47     * @var Static property to hold concrete analytics implementation that does the work.
48     */
49    private static $views = false;
50
51    /**
52     * WooCommerce Analytics is only available to Jetpack connected WooCommerce stores with both plugins set to active
53     * and WooCommerce version 3.0 or higher
54     *
55     * @return bool
56     */
57    public static function should_track_store() {
58        /**
59         * Make sure WooCommerce is installed and active
60         *
61         * This action is documented in https://docs.woocommerce.com/document/create-a-plugin
62         */
63        if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', Jetpack::get_active_plugins() ), true ) ) {
64            return false;
65        }
66        // Tracking only Site pages.
67        if ( is_admin() ) {
68            return false;
69        }
70        // Make sure Jetpack is installed and connected.
71        if ( ! Jetpack::is_connection_ready() ) {
72            return false;
73        }
74        // Ensure the WooCommerce class exists and is a valid version.
75        $minimum_woocommerce_active = class_exists( 'WooCommerce' ) && version_compare( WC_VERSION, '3.0', '>=' );
76        if ( ! $minimum_woocommerce_active ) {
77            return false;
78        }
79        return true;
80    }
81
82    /**
83     * This is our constructor, which is private to force the use of get_instance()
84     *
85     * @return void
86     */
87    private function __construct() {
88        // loading _wca.
89        add_action( 'wp_head', array( $this, 'wp_head_top' ), 1 );
90
91        // loading s.js.
92        add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_tracking_script' ) );
93
94        self::$analytics = new Jetpack_WooCommerce_Analytics_Universal();
95        self::$myaccount = new Jetpack_WooCommerce_Analytics_My_Account();
96        if ( class_exists( 'Automattic\WooCommerce\Blocks\Package' ) && version_compare( Automattic\WooCommerce\Blocks\Package::get_version(), '11.6.2', '>=' ) ) {
97            self::$views = new Jetpack_WooCommerce_Analytics_Checkout_Flow();
98        }
99    }
100
101        /**
102         * Make _wca available to queue events
103         */
104    public function wp_head_top() {
105        if ( is_cart() || is_checkout() || is_checkout_pay_page() || is_order_received_page() || is_add_payment_method_page() ) {
106            echo '<script>window._wca_prevent_referrer = true;</script>' . "\r\n";
107        }
108        echo '<script>window._wca = window._wca || [];</script>' . "\r\n";
109    }
110
111    /**
112     * Place script to call s.js, Store Analytics.
113     */
114    public function enqueue_tracking_script() {
115        $url = sprintf(
116            'https://stats.wp.com/s-%d.js',
117            gmdate( 'YW' )
118        );
119
120        wp_enqueue_script(
121            'woocommerce-analytics',
122            $url,
123            array(),
124            null, // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion -- The version is set in the URL.
125            array(
126                'in_footer' => false,
127                'strategy'  => 'defer',
128            )
129        );
130    }
131
132    /**
133     * Function to instantiate our class and make it a singleton
134     */
135    public static function get_instance() {
136        if ( ! self::should_track_store() ) {
137            return;
138        }
139        if ( ! self::$instance ) {
140            self::$instance = new self();
141        }
142
143        return self::$instance;
144    }
145}
146
147global $jetpack_woocommerce_analytics;
148$jetpack_woocommerce_analytics = Jetpack_WooCommerce_Analytics::get_instance();