class-jetpack-woocommerce-analytics.php
101 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Jetpack_WooCommerce_Analytics is ported from the Jetpack_Google_Analytics code. |
| 4 | * |
| 5 | * @package automattic/jetpack |
| 6 | */ |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit; |
| 10 | } |
| 11 | |
| 12 | require __DIR__ . '/classes/class-jetpack-woocommerce-analytics-trait.php'; |
| 13 | require_once __DIR__ . '/classes/class-jetpack-woocommerce-analytics-universal.php'; |
| 14 | require_once __DIR__ . '/classes/class-jetpack-woocommerce-analytics-my-account.php'; |
| 15 | |
| 16 | /** |
| 17 | * Class Jetpack_WooCommerce_Analytics |
| 18 | * Instantiate WooCommerce Analytics |
| 19 | */ |
| 20 | class Jetpack_WooCommerce_Analytics { |
| 21 | |
| 22 | /** |
| 23 | * Instance of this class |
| 24 | * |
| 25 | * @var Jetpack_WooCommerce_Analytics - Static property to hold our singleton instance |
| 26 | */ |
| 27 | private static $instance = false; |
| 28 | |
| 29 | /** |
| 30 | * Instance of the Universal functions |
| 31 | * |
| 32 | * @var Static property to hold concrete analytics implementation that does the work (universal or legacy) |
| 33 | */ |
| 34 | private static $analytics = false; |
| 35 | |
| 36 | /** |
| 37 | * Instance of the My account functions |
| 38 | * |
| 39 | * @var Static property to hold concrete analytics implementation that does the work. |
| 40 | */ |
| 41 | private static $myaccount = false; |
| 42 | |
| 43 | /** |
| 44 | * WooCommerce Analytics is only available to Jetpack connected WooCommerce stores with both plugins set to active |
| 45 | * and WooCommerce version 3.0 or higher |
| 46 | * |
| 47 | * @return bool |
| 48 | */ |
| 49 | public static function should_track_store() { |
| 50 | /** |
| 51 | * Make sure WooCommerce is installed and active |
| 52 | * |
| 53 | * This action is documented in https://docs.woocommerce.com/document/create-a-plugin |
| 54 | */ |
| 55 | if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', Jetpack::get_active_plugins() ), true ) ) { |
| 56 | return false; |
| 57 | } |
| 58 | // Tracking only Site pages. |
| 59 | if ( is_admin() ) { |
| 60 | return false; |
| 61 | } |
| 62 | // Make sure Jetpack is installed and connected. |
| 63 | if ( ! Jetpack::is_connection_ready() ) { |
| 64 | return false; |
| 65 | } |
| 66 | // Ensure the WooCommerce class exists and is a valid version. |
| 67 | $minimum_woocommerce_active = class_exists( 'WooCommerce' ) && version_compare( WC_VERSION, '3.0', '>=' ); |
| 68 | if ( ! $minimum_woocommerce_active ) { |
| 69 | return false; |
| 70 | } |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * This is our constructor, which is private to force the use of get_instance() |
| 76 | * |
| 77 | * @return void |
| 78 | */ |
| 79 | private function __construct() { |
| 80 | self::$analytics = new Jetpack_WooCommerce_Analytics_Universal(); |
| 81 | self::$myaccount = new Jetpack_WooCommerce_Analytics_My_Account(); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Function to instantiate our class and make it a singleton |
| 86 | */ |
| 87 | public static function get_instance() { |
| 88 | if ( ! self::should_track_store() ) { |
| 89 | return; |
| 90 | } |
| 91 | if ( ! self::$instance ) { |
| 92 | self::$instance = new self(); |
| 93 | } |
| 94 | |
| 95 | return self::$instance; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | global $jetpack_woocommerce_analytics; |
| 100 | $jetpack_woocommerce_analytics = Jetpack_WooCommerce_Analytics::get_instance(); |
| 101 |