PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-beta
Jetpack – WP Security, Backup, Speed, & Growth v16.3-beta
16.3-beta 16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 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 All 507 releases
jetpack / jetpack_vendor / automattic / jetpack-masterbar / src / admin-menu / class-dashboard-switcher-tracking.php

class-dashboard-switcher-tracking.php in Jetpack – WP Security, Backup, Speed, & Growth 16.3-beta, at jetpack_vendor/automattic/jetpack-masterbar/src/admin-menu/class-dashboard-switcher-tracking.php

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