Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
40.00% covered (danger)
40.00%
14 / 35
14.29% covered (danger)
14.29%
1 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
Dashboard_Switcher_Tracking
40.00% covered (danger)
40.00%
14 / 35
14.29% covered (danger)
14.29%
1 / 7
87.98
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 record_switch_event
70.00% covered (warning)
70.00%
7 / 10
0.00% covered (danger)
0.00%
0 / 1
3.24
 get_plan
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
42
 record_jetpack_event
66.67% covered (warning)
66.67%
4 / 6
0.00% covered (danger)
0.00%
0 / 1
3.33
 wpcom_tracks_record_event
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 get_jetpack_tracking_product
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 mark_jetpack_tos_as_read
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * Quick switcher tracking file.
4 *
5 * @package automattic/jetpack-masterbar
6 */
7
8namespace Automattic\Jetpack\Masterbar;
9
10use Automattic\Jetpack\Current_Plan as Jetpack_Plan;
11use Automattic\Jetpack\Status\Host;
12use Automattic\Jetpack\Terms_Of_Service;
13use Automattic\Jetpack\Tracking;
14
15/**
16 * Class Dashboard_Switcher_Tracking
17 */
18class 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}