wp-woocommerce-analytics.php 2.4 KB

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