/wp-content/plugins/woocommerce-services/classes/class-wc-connect-tracks.php

https://gitlab.com/remyvianne/krowkaramel · PHP · 125 lines · 95 code · 24 blank · 6 comment · 14 complexity · 42c44e948e76d2e71e7c341234832d5a MD5 · raw file

  1. <?php
  2. // No direct access please
  3. if ( ! defined( 'ABSPATH' ) ) {
  4. exit;
  5. }
  6. if ( ! class_exists( 'WC_Connect_Tracks' ) ) {
  7. class WC_Connect_Tracks {
  8. static $product_name = 'woocommerceconnect';
  9. /**
  10. * @var WC_Connect_Logger
  11. */
  12. protected $logger;
  13. public function __construct( WC_Connect_Logger $logger, $plugin_file ) {
  14. $this->logger = $logger;
  15. $this->plugin_file = $plugin_file;
  16. }
  17. public function init() {
  18. add_action( 'wc_connect_shipping_zone_method_added', array( $this, 'shipping_zone_method_added' ), 10, 3 );
  19. add_action( 'wc_connect_shipping_zone_method_deleted', array( $this, 'shipping_zone_method_deleted' ), 10, 3 );
  20. add_action( 'wc_connect_shipping_zone_method_status_toggled', array( $this, 'shipping_zone_method_status_toggled' ), 10, 4 );
  21. add_action( 'wc_connect_saved_service_settings', array( $this, 'saved_service_settings' ), 10, 3 );
  22. register_deactivation_hook( $this->plugin_file, array( $this, 'opted_out' ) );
  23. }
  24. public function opted_in( $source = null ) {
  25. if ( is_null( $source ) ) {
  26. $this->record_user_event( 'opted_in' );
  27. } else {
  28. $this->record_user_event( 'opted_in', compact( 'source' ) );
  29. }
  30. }
  31. public function opted_out() {
  32. $this->record_user_event( 'opted_out' );
  33. }
  34. public function shipping_zone_method_added( $instance_id, $service_id ) {
  35. $this->record_user_event( 'shipping_zone_method_added' );
  36. $this->record_user_event( 'shipping_zone_' . $service_id . '_added' );
  37. }
  38. public function shipping_zone_method_deleted( $instance_id, $service_id ) {
  39. $this->record_user_event( 'shipping_zone_method_deleted' );
  40. $this->record_user_event( 'shipping_zone_' . $service_id . '_deleted' );
  41. }
  42. public function shipping_zone_method_status_toggled( $instance_id, $service_id, $zone_id, $enabled ) {
  43. if ( $enabled ) {
  44. $this->record_user_event( 'shipping_zone_method_enabled' );
  45. $this->record_user_event( 'shipping_zone_' . $service_id . '_enabled' );
  46. } else {
  47. $this->record_user_event( 'shipping_zone_method_disabled' );
  48. $this->record_user_event( 'shipping_zone_' . $service_id . '_disabled' );
  49. }
  50. }
  51. public function saved_service_settings( $service_id ) {
  52. $this->record_user_event( 'saved_service_settings' );
  53. $this->record_user_event( 'saved_' . $service_id . '_settings' );
  54. }
  55. public function record_user_event( $event_type, $data = array() ) {
  56. if ( ! function_exists( 'jetpack_tracks_record_event' ) && ! class_exists( 'Automattic\\Jetpack\\Tracking' ) ) {
  57. $this->debug( 'Error. jetpack_tracks_record_event is not defined.' );
  58. return;
  59. }
  60. $user = wp_get_current_user();
  61. $site_url = get_option( 'siteurl' );
  62. $wcs_version = WC_Connect_Loader::get_wcs_version();
  63. // Check for WooCommerce
  64. $wc_version = 'unavailable';
  65. if ( function_exists( 'WC' ) ) {
  66. $wc_version = WC()->version;
  67. }
  68. // Check for Jetpack
  69. $jp_version = 'unavailable';
  70. if ( defined( 'JETPACK__VERSION' ) ) {
  71. $jp_version = JETPACK__VERSION;
  72. }
  73. $is_atomic = WC_Connect_Jetpack::is_atomic_site();
  74. $jetpack_blog_id = -1;
  75. if ( class_exists( 'Jetpack_Options' ) && method_exists( 'Jetpack_Options', 'get_option' ) ) {
  76. $jetpack_blog_id = Jetpack_Options::get_option( 'id' );
  77. }
  78. if ( ! is_array( $data ) ) {
  79. $data = array();
  80. }
  81. $data['_via_ua'] = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : '';
  82. $data['_via_ip'] = isset( $_SERVER['REMOTE_ADDR'] ) ? $_SERVER['REMOTE_ADDR'] : '';
  83. $data['_lg'] = isset( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : '';
  84. $data['blog_url'] = $site_url;
  85. $data['blog_id'] = $jetpack_blog_id;
  86. $data['wcs_version'] = $wcs_version;
  87. $data['jetpack_version'] = $jp_version;
  88. $data['is_atomic'] = $is_atomic;
  89. $data['wc_version'] = $wc_version;
  90. $data['wp_version'] = get_bloginfo( 'version' );
  91. $event_type = self::$product_name . '_' . $event_type;
  92. $this->debug( 'Tracked the following event: ' . $event_type );
  93. WC_Connect_Jetpack::tracks_record_event( $user, $event_type, $data );
  94. }
  95. protected function debug( $message ) {
  96. if ( ! is_null( $this->logger ) ) {
  97. $this->logger->log( $message );
  98. }
  99. }
  100. }
  101. }