Nessuna descrizione

class-dashboard-switcher-tracking.php 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * Quick switcher tracking file.
  4. *
  5. * @package automattic/jetpack
  6. */
  7. namespace Automattic\Jetpack\Dashboard_Customizations;
  8. use Automattic\Jetpack\Terms_Of_Service;
  9. use Automattic\Jetpack\Tracking;
  10. use Jetpack_Plan;
  11. /**
  12. * Class Dashboard_Switcher_Tracking
  13. */
  14. class Dashboard_Switcher_Tracking {
  15. /**
  16. * Jetpack Tracking library will prefix the event name with "jetpack_*" automatically.
  17. */
  18. const JETPACK_EVENT_NAME = 'dashboard_quick_switch_link_clicked';
  19. const WPCOM_EVENT_NAME = 'wpcom_dashboard_quick_switch_link_clicked';
  20. /**
  21. * Jetpack tracking object.
  22. *
  23. * @var Tracking
  24. */
  25. private $tracking;
  26. /**
  27. * Current site plan.
  28. *
  29. * @var string
  30. */
  31. private $plan;
  32. /**
  33. * The wpcom_tracks wrapper function.
  34. *
  35. * @var callable
  36. */
  37. private $wpcom_tracking;
  38. /**
  39. * Dashboard_Switcher_Tracking constructor.
  40. *
  41. * @param Tracking $tracking Jetpack tracking object.
  42. * @param callable $wpcom_tracking A wrapper over wpcom event record.
  43. * @param string $plan The current site plan.
  44. */
  45. public function __construct( Tracking $tracking, callable $wpcom_tracking, $plan ) {
  46. $this->tracking = $tracking;
  47. $this->plan = $plan;
  48. $this->wpcom_tracking = $wpcom_tracking;
  49. }
  50. /**
  51. * Create an event for the Quick switcher when the user changes it's preferred view.
  52. *
  53. * @param string $screen The screen page.
  54. * @param string $view The new preferred view.
  55. */
  56. public function record_switch_event( $screen, $view ) {
  57. $event_props = array(
  58. 'current_page' => $screen,
  59. 'destination' => $view,
  60. 'plan' => $this->plan,
  61. );
  62. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  63. $event_props['blog_id'] = get_current_blog_id();
  64. /**
  65. * Callable injected in the constructor with the static::wpcom_tracks_record_event() static method.
  66. *
  67. * @see wpcom_tracks_record_event A static method from this class that executes the actual WPCOM event record.
  68. */
  69. $wpcom_tracking = $this->wpcom_tracking;
  70. $wpcom_tracking( $event_props );
  71. } else {
  72. $this->record_jetpack_event( $event_props );
  73. }
  74. }
  75. /**
  76. * Get the current site plan or 'N/A' when we cannot determine site's plan.
  77. *
  78. * @todo: This method can be reused as a wrapper over WPCOM and Atomic as way to get site's current plan (display name).
  79. *
  80. * @return string
  81. */
  82. public static function get_plan() {
  83. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  84. if ( class_exists( '\WPCOM_Store_API' ) ) {
  85. // @todo: Maybe introduce a wrapper for this since we are duplicating it from WPCOM_Admin_Menu:253
  86. $products = \WPCOM_Store_API::get_current_plan( \get_current_blog_id() );
  87. if ( array_key_exists( 'product_name_short', $products ) ) {
  88. return $products['product_name_short'];
  89. }
  90. }
  91. return 'N/A'; // maybe we should return free or null? At the moment it's safe to return 'N/A' since we use it only for passing it to the event.
  92. }
  93. // @todo: Maybe introduce a helper for this since we are duplicating it from Atomic_Admin_Menu:240
  94. $products = Jetpack_Plan::get();
  95. if ( array_key_exists( 'product_name_short', $products ) ) {
  96. return $products['product_name_short'];
  97. }
  98. return 'N/A'; // maybe we should return free or null? At the moment we use it for passing it to the event.
  99. }
  100. /**
  101. * Record the event with Jetpack implementation.
  102. *
  103. * For Atomic sites we mark the Jetpack ToS option temporary as read.
  104. *
  105. * @todo Remove the jetpack_options_tos_agreed filter for Atomic sites after the Tracking is properly working for AT sites.
  106. *
  107. * @param array $event_properties The event properties.
  108. */
  109. private function record_jetpack_event( $event_properties ) {
  110. if ( \jetpack_is_atomic_site() ) {
  111. add_filter( 'jetpack_options', array( __CLASS__, 'mark_jetpack_tos_as_read' ), 10, 2 );
  112. }
  113. $this->tracking->record_user_event( self::JETPACK_EVENT_NAME, $event_properties );
  114. if ( \jetpack_is_atomic_site() ) {
  115. \remove_filter( 'jetpack_options', array( __CLASS__, 'mark_jetpack_tos_as_read' ) );
  116. }
  117. }
  118. /**
  119. * Trigger the WPCOM tracks_record_event.
  120. *
  121. * @param array $event_props Event props.
  122. */
  123. public static function wpcom_tracks_record_event( $event_props ) {
  124. jetpack_require_lib( 'tracks/client' );
  125. \tracks_record_event( \wp_get_current_user(), self::WPCOM_EVENT_NAME, $event_props );
  126. }
  127. /**
  128. * Get the tracking product name for the Tracking library.
  129. *
  130. * The tracking product name is used by the Tracking as a prefix for the event name.
  131. *
  132. * @return string
  133. */
  134. public static function get_jetpack_tracking_product() {
  135. return \jetpack_is_atomic_site() ? 'atomic' : 'jetpack';
  136. }
  137. /**
  138. * Mark the Jetpack ToS as read for Atomic Sites.
  139. *
  140. * @param mixed $option_value The value of the Jetpack option.
  141. * @param string $option_name The name of the Jetpack option.
  142. *
  143. * @return bool
  144. */
  145. public static function mark_jetpack_tos_as_read( $option_value, $option_name ) {
  146. if ( Terms_Of_Service::OPTION_NAME === $option_name ) {
  147. return true;
  148. }
  149. return $option_value;
  150. }
  151. }