PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 13.0.2
Jetpack – WP Security, Backup, Speed, & Growth v13.0.2
12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 14.4.2 All 500 releases
jetpack / modules / woocommerce-analytics / class-jetpack-woocommerce-analytics.php
class-jetpack-woocommerce-analytics.php
142 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Jetpack_WooCommerce_Analytics is ported from the Jetpack_Google_Analytics code.
4 *
5 * @package automattic/jetpack
6 */
7
8 use Automattic\Jetpack\Assets;
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit;
12 }
13
14 require __DIR__ . '/classes/class-jetpack-woocommerce-analytics-trait.php';
15 require_once __DIR__ . '/classes/class-jetpack-woocommerce-analytics-universal.php';
16 require_once __DIR__ . '/classes/class-jetpack-woocommerce-analytics-my-account.php';
17 require_once __DIR__ . '/classes/class-jetpack-woocommerce-analytics-checkout-flow.php';
18
19 /**
20 * Class Jetpack_WooCommerce_Analytics
21 * Instantiate WooCommerce Analytics
22 */
23 class Jetpack_WooCommerce_Analytics {
24
25 /**
26 * Instance of this class
27 *
28 * @var Jetpack_WooCommerce_Analytics - Static property to hold our singleton instance
29 */
30 private static $instance = false;
31
32 /**
33 * Instance of the Universal functions
34 *
35 * @var Static property to hold concrete analytics implementation that does the work (universal or legacy)
36 */
37 private static $analytics = false;
38
39 /**
40 * Instance of the My account functions
41 *
42 * @var Static property to hold concrete analytics implementation that does the work.
43 */
44 private static $myaccount = false;
45
46 /**
47 * Instance of the Checkout Flow functions
48 *
49 * @var Static property to hold concrete analytics implementation that does the work.
50 */
51 private static $views = false;
52
53 /**
54 * WooCommerce Analytics is only available to Jetpack connected WooCommerce stores with both plugins set to active
55 * and WooCommerce version 3.0 or higher
56 *
57 * @return bool
58 */
59 public static function should_track_store() {
60 /**
61 * Make sure WooCommerce is installed and active
62 *
63 * This action is documented in https://docs.woocommerce.com/document/create-a-plugin
64 */
65 if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', Jetpack::get_active_plugins() ), true ) ) {
66 return false;
67 }
68 // Tracking only Site pages.
69 if ( is_admin() ) {
70 return false;
71 }
72 // Make sure Jetpack is installed and connected.
73 if ( ! Jetpack::is_connection_ready() ) {
74 return false;
75 }
76 // Ensure the WooCommerce class exists and is a valid version.
77 $minimum_woocommerce_active = class_exists( 'WooCommerce' ) && version_compare( WC_VERSION, '3.0', '>=' );
78 if ( ! $minimum_woocommerce_active ) {
79 return false;
80 }
81 return true;
82 }
83
84 /**
85 * This is our constructor, which is private to force the use of get_instance()
86 *
87 * @return void
88 */
89 private function __construct() {
90 // loading _wca.
91 add_action( 'wp_head', array( $this, 'wp_head_top' ), 1 );
92
93 // loading s.js.
94 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_tracking_script' ) );
95
96 self::$analytics = new Jetpack_WooCommerce_Analytics_Universal();
97 self::$myaccount = new Jetpack_WooCommerce_Analytics_My_Account();
98 if ( class_exists( 'Automattic\WooCommerce\Blocks\Package' ) && version_compare( Automattic\WooCommerce\Blocks\Package::get_version(), '11.6.2', '>=' ) ) {
99 self::$views = new Jetpack_WooCommerce_Analytics_Checkout_Flow();
100 }
101 }
102
103 /**
104 * Make _wca available to queue events
105 */
106 public function wp_head_top() {
107 if ( is_cart() || is_checkout() || is_checkout_pay_page() || is_order_received_page() || is_add_payment_method_page() ) {
108 echo '<script>window._wca_prevent_referrer = true;</script>' . "\r\n";
109 }
110 echo '<script>window._wca = window._wca || [];</script>' . "\r\n";
111 }
112
113 /**
114 * Place script to call s.js, Store Analytics.
115 */
116 public function enqueue_tracking_script() {
117 $filename = sprintf(
118 'https://stats.wp.com/s-%d.js',
119 gmdate( 'YW' )
120 );
121
122 Assets::enqueue_async_script( 'woocommerce-analytics', esc_url( $filename ), esc_url( $filename ), array(), null, false );
123 }
124
125 /**
126 * Function to instantiate our class and make it a singleton
127 */
128 public static function get_instance() {
129 if ( ! self::should_track_store() ) {
130 return;
131 }
132 if ( ! self::$instance ) {
133 self::$instance = new self();
134 }
135
136 return self::$instance;
137 }
138 }
139
140 global $jetpack_woocommerce_analytics;
141 $jetpack_woocommerce_analytics = Jetpack_WooCommerce_Analytics::get_instance();
142