Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
86.67% covered (warning)
86.67%
52 / 60
25.00% covered (danger)
25.00%
1 / 4
CRAP
n/a
0 / 0
Automattic\Jetpack\Jetpack_Mu_Wpcom\Wpcom_Unified_Admin_Page_View\wpcom_unified_admin_tracks_props
90.00% covered (success)
90.00%
9 / 10
0.00% covered (danger)
0.00%
0 / 1
5.03
Automattic\Jetpack\Jetpack_Mu_Wpcom\Wpcom_Unified_Admin_Page_View\wpcom_unified_track_admin_page_views
94.74% covered (success)
94.74%
36 / 38
0.00% covered (danger)
0.00%
0 / 1
10.01
Automattic\Jetpack\Jetpack_Mu_Wpcom\Wpcom_Unified_Admin_Page_View\wpcom_atomic_get_user_types
66.67% covered (warning)
66.67%
6 / 9
0.00% covered (danger)
0.00%
0 / 1
4.59
Automattic\Jetpack\Jetpack_Mu_Wpcom\Wpcom_Unified_Admin_Page_View\get_account_age_in_days
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * WPCom Unified Tracking for WP Admin Page Views.
4 * Similar to what we're doing in wpcom-wpadmin-page-view, but using the same page view definition across all WPCom.
5 *
6 * @package automattic/jetpack-mu-wpcom
7 */
8
9namespace Automattic\Jetpack\Jetpack_Mu_Wpcom\Wpcom_Unified_Admin_Page_View;
10
11/**
12 * The site properties shared by the wp-admin Tracks events.
13 *
14 * @return array|null The properties, or null when there is no site to describe.
15 */
16function wpcom_unified_admin_tracks_props() {
17    $is_simple_site = defined( 'IS_WPCOM' ) && IS_WPCOM;
18
19    if ( $is_simple_site ) {
20        global $current_blog;
21        if ( ! $current_blog instanceof \WP_Site ) {
22            return null;
23        }
24        $blog_id = $current_blog->blog_id;
25    } else {
26        $blog_id = _wpcom_get_current_blog_id();
27    }
28
29    return array(
30        'site_type' => $is_simple_site ? 'simple' : 'atomic',
31        'blog_id'   => $blog_id,
32    );
33}
34
35/**
36 * Bump a unified Tracks stat for every wp-admin page view, and for every time Screen Options is opened or closed.
37 */
38function wpcom_unified_track_admin_page_views() {
39    global $current_user, $current_screen;
40    if ( ! $current_user instanceof \WP_User || ! $current_screen instanceof \WP_Screen ) {
41        return;
42    }
43
44    $site_props = wpcom_unified_admin_tracks_props();
45    if ( null === $site_props ) {
46        return;
47    }
48
49    $user_types      = 'simple' === $site_props['site_type'] ? \WPCOM_User::get_types() : wpcom_atomic_get_user_types();
50    $wpcom_user_id   = 'simple' === $site_props['site_type']
51        ? absint( $current_user->ID )
52        : (int) get_user_meta( absint( $current_user->ID ), 'wpcom_user_id', true );
53    $wpcom_user_data = 'simple' === $site_props['site_type'] ? null : get_user_meta( absint( $current_user->ID ), 'wpcom_user_data', true );
54    $user_login      = (int) ( $wpcom_user_data->ID ?? 0 ) === $wpcom_user_id && ! empty( $wpcom_user_data->login )
55        ? $wpcom_user_data->login
56        : $current_user->user_login;
57
58    $event_props = array(
59        'route'              => $current_screen->id,
60        'source'             => 'wp-admin',
61        'site_type'          => $site_props['site_type'],
62        'is_block_editor'    => $current_screen->is_block_editor,
63        'blog_id'            => $site_props['blog_id'],
64        'user_type'          => implode( ',', $user_types ),
65        'is_local_only_user' => 0 === $wpcom_user_id,
66    );
67    ?>
68    <script type="text/javascript">
69        var _admin_pv_props = <?php echo wp_json_encode( $event_props, JSON_HEX_TAG ); ?>;
70        _tkq = window._tkq || [];
71        <?php if ( $wpcom_user_id > 0 ) : ?>
72        _tkq.push( [ 'identifyUser', <?php echo (int) $wpcom_user_id; ?>, <?php echo wp_json_encode( $user_login, JSON_HEX_TAG ); ?> ] );
73        <?php endif; ?>
74        _tkq.push( [ 'recordEvent', 'wpcom_unified_admin_page_view', _admin_pv_props ] );
75        ( function ( props ) {
76            document.addEventListener( 'click', function ( event ) {
77                var button = event.target.closest && event.target.closest( '#show-settings-link' );
78                var panel = button && document.getElementById( button.getAttribute( 'aria-controls' ) );
79                if ( panel ) {
80                    // This listener captures the click before core toggles the panel, so a hidden panel is about to open.
81                    var action = panel.getClientRects().length ? 'close' : 'open';
82                    window._tkq.push( [ 'recordEvent', 'wpcom_admin_screen_options_toggle', Object.assign( { action: action }, props ) ] );
83                }
84            }, true );
85        } )( <?php echo wp_json_encode( array( 'route' => $current_screen->id ) + $site_props, JSON_HEX_TAG ); ?> );
86    </script>
87    <?php
88}
89add_action( 'admin_footer', __NAMESPACE__ . '\wpcom_unified_track_admin_page_views' );
90// Track customizer page views the same as any wp-admin page.
91add_action( 'customize_controls_print_footer_scripts', __NAMESPACE__ . '\wpcom_unified_track_admin_page_views' );
92
93/**
94 * Retrieves the user types for Atomic sites.
95 *
96 * @return array An array of user types.
97 */
98function wpcom_atomic_get_user_types() {
99    $user_types = array();
100
101    if ( get_account_age_in_days() <= 14 ) {
102        $user_types[] = 'New User';
103    }
104
105    $subscriptions = wpcomsh_get_wpcom_active_subscriptions();
106    if ( count( $subscriptions ) > 0 ) {
107        $user_types[] = 'Paid';
108    }
109
110    if ( wpcom_site_has_feature( \WPCOM_Features::PRIORITY_SUPPORT ) ) {
111        $user_types[] = 'Business';
112    }
113
114    return $user_types;
115}
116
117/**
118 * Calculate the number of days since the current user registered their account.
119 *
120 * @global \WP_User $current_user The current user object.
121 *
122 * @return float The age of the account in days.
123 */
124function get_account_age_in_days() {
125    global $current_user;
126
127    return ( time() - strtotime( $current_user->user_registered ) ) / DAY_IN_SECONDS;
128}