Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
81.08% covered (warning)
81.08%
30 / 37
33.33% covered (danger)
33.33%
1 / 3
CRAP
n/a
0 / 0
Automattic\Jetpack\Jetpack_Mu_Wpcom\Wpcom_Unified_Admin_Page_View\wpcom_unified_track_admin_page_views
92.00% covered (success)
92.00%
23 / 25
0.00% covered (danger)
0.00%
0 / 1
8.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 * Bump a unified Tracks stat for every wp-admin page view.
13 */
14function wpcom_unified_track_admin_page_views() {
15    $is_simple_site = defined( 'IS_WPCOM' ) && IS_WPCOM;
16    $is_atomic_site = ! $is_simple_site;
17
18    global $current_user, $current_blog, $current_screen;
19    if ( ! $current_user instanceof \WP_User || ! $current_screen instanceof \WP_Screen ) {
20        return;
21    }
22    $blog_id    = null;
23    $user_types = array();
24
25    if ( $is_simple_site ) {
26        if ( ! $current_blog instanceof \WP_Site ) {
27            return;
28        }
29        $blog_id    = $current_blog->blog_id;
30        $user_types = \WPCOM_User::get_types();
31    }
32
33    if ( $is_atomic_site ) {
34        $blog_id    = _wpcom_get_current_blog_id();
35        $user_types = wpcom_atomic_get_user_types();
36    }
37    $event_props = array(
38        'route'           => $current_screen->id,
39        'source'          => 'wp-admin',
40        'site_type'       => $is_simple_site ? 'simple' : 'atomic',
41        'is_block_editor' => $current_screen->is_block_editor,
42        'blog_id'         => $blog_id,
43        'user_type'       => implode( ',', $user_types ),
44    );
45    ?>
46    <script type="text/javascript">
47        var _admin_pv_props = <?php echo wp_json_encode( $event_props, JSON_HEX_TAG ); ?>;
48        _tkq = window._tkq || [];
49        _tkq.push( [ 'identifyUser', <?php echo (int) $current_user->ID; ?>, <?php echo wp_json_encode( $current_user->user_login, JSON_HEX_TAG ); ?> ] );
50        _tkq.push( [ 'recordEvent', 'wpcom_unified_admin_page_view', _admin_pv_props ] );
51    </script>
52    <?php
53}
54add_action( 'admin_footer', __NAMESPACE__ . '\wpcom_unified_track_admin_page_views' );
55// Track customizer page views the same as any wp-admin page.
56add_action( 'customize_controls_print_footer_scripts', __NAMESPACE__ . '\wpcom_unified_track_admin_page_views' );
57
58/**
59 * Retrieves the user types for Atomic sites.
60 *
61 * @return array An array of user types.
62 */
63function wpcom_atomic_get_user_types() {
64    $user_types = array();
65
66    if ( get_account_age_in_days() <= 14 ) {
67        $user_types[] = 'New User';
68    }
69
70    $subscriptions = wpcomsh_get_wpcom_active_subscriptions();
71    if ( count( $subscriptions ) > 0 ) {
72        $user_types[] = 'Paid';
73    }
74
75    if ( wpcom_site_has_feature( \WPCOM_Features::PRIORITY_SUPPORT ) ) {
76        $user_types[] = 'Business';
77    }
78
79    return $user_types;
80}
81
82/**
83 * Calculate the number of days since the current user registered their account.
84 *
85 * @global \WP_User $current_user The current user object.
86 *
87 * @return float The age of the account in days.
88 */
89function get_account_age_in_days() {
90    global $current_user;
91
92    return ( time() - strtotime( $current_user->user_registered ) ) / DAY_IN_SECONDS;
93}