Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
80.00% covered (warning)
80.00%
32 / 40
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
88.89% covered (warning)
88.89%
16 / 18
0.00% covered (danger)
0.00%
0 / 1
5.03
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.
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    $event_props = array(
51        'route'           => $current_screen->id,
52        'source'          => 'wp-admin',
53        'site_type'       => $site_props['site_type'],
54        'is_block_editor' => $current_screen->is_block_editor,
55        'blog_id'         => $site_props['blog_id'],
56        'user_type'       => implode( ',', $user_types ),
57    );
58    ?>
59    <script type="text/javascript">
60        var _admin_pv_props = <?php echo wp_json_encode( $event_props, JSON_HEX_TAG ); ?>;
61        _tkq = window._tkq || [];
62        _tkq.push( [ 'identifyUser', <?php echo (int) $current_user->ID; ?>, <?php echo wp_json_encode( $current_user->user_login, JSON_HEX_TAG ); ?> ] );
63        _tkq.push( [ 'recordEvent', 'wpcom_unified_admin_page_view', _admin_pv_props ] );
64        ( function ( props ) {
65            document.addEventListener( 'click', function ( event ) {
66                var button = event.target.closest && event.target.closest( '#show-settings-link' );
67                var panel = button && document.getElementById( button.getAttribute( 'aria-controls' ) );
68                // This listener captures the click before core toggles the panel, so a hidden panel is about to open.
69                if ( panel && ! panel.getClientRects().length ) {
70                    window._tkq.push( [ 'recordEvent', 'wpcom_admin_screen_options_open', props ] );
71                }
72            }, true );
73        } )( <?php echo wp_json_encode( array( 'route' => $current_screen->id ) + $site_props, JSON_HEX_TAG ); ?> );
74    </script>
75    <?php
76}
77add_action( 'admin_footer', __NAMESPACE__ . '\wpcom_unified_track_admin_page_views' );
78// Track customizer page views the same as any wp-admin page.
79add_action( 'customize_controls_print_footer_scripts', __NAMESPACE__ . '\wpcom_unified_track_admin_page_views' );
80
81/**
82 * Retrieves the user types for Atomic sites.
83 *
84 * @return array An array of user types.
85 */
86function wpcom_atomic_get_user_types() {
87    $user_types = array();
88
89    if ( get_account_age_in_days() <= 14 ) {
90        $user_types[] = 'New User';
91    }
92
93    $subscriptions = wpcomsh_get_wpcom_active_subscriptions();
94    if ( count( $subscriptions ) > 0 ) {
95        $user_types[] = 'Paid';
96    }
97
98    if ( wpcom_site_has_feature( \WPCOM_Features::PRIORITY_SUPPORT ) ) {
99        $user_types[] = 'Business';
100    }
101
102    return $user_types;
103}
104
105/**
106 * Calculate the number of days since the current user registered their account.
107 *
108 * @global \WP_User $current_user The current user object.
109 *
110 * @return float The age of the account in days.
111 */
112function get_account_age_in_days() {
113    global $current_user;
114
115    return ( time() - strtotime( $current_user->user_registered ) ) / DAY_IN_SECONDS;
116}