| 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( 0 ); |
| 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 |
require_once __DIR__ . '/classes/class-jetpack-woocommerce-analytics-checkout-flow.php'; |
| 16 |
|
| 17 |
/** |
| 18 |
* Class Jetpack_WooCommerce_Analytics |
| 19 |
* Instantiate WooCommerce Analytics |
| 20 |
*/ |
| 21 |
class 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 |
// No store activity to track on 404 pages. |
| 116 |
if ( is_404() ) { |
| 117 |
return; |
| 118 |
} |
| 119 |
|
| 120 |
$url = sprintf( |
| 121 |
'https://stats.wp.com/s-%d.js', |
| 122 |
gmdate( 'YW' ) |
| 123 |
); |
| 124 |
|
| 125 |
wp_enqueue_script( |
| 126 |
'woocommerce-analytics', |
| 127 |
$url, |
| 128 |
array(), |
| 129 |
null, // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion -- The version is set in the URL. |
| 130 |
array( |
| 131 |
'in_footer' => false, |
| 132 |
'strategy' => 'defer', |
| 133 |
) |
| 134 |
); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Function to instantiate our class and make it a singleton |
| 139 |
*/ |
| 140 |
public static function get_instance() { |
| 141 |
if ( ! self::should_track_store() ) { |
| 142 |
return; |
| 143 |
} |
| 144 |
if ( ! self::$instance ) { |
| 145 |
self::$instance = new self(); |
| 146 |
} |
| 147 |
|
| 148 |
return self::$instance; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
global $jetpack_woocommerce_analytics; |
| 153 |
$jetpack_woocommerce_analytics = Jetpack_WooCommerce_Analytics::get_instance(); |
| 154 |
|