Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
56.00% covered (warning)
56.00%
28 / 50
0.00% covered (danger)
0.00%
0 / 3
CRAP
n/a
0 / 0
Automattic\Jetpack\Masterbar\should_customize_nav
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
72
Automattic\Jetpack\Masterbar\get_admin_menu_class
93.33% covered (success)
93.33%
28 / 30
0.00% covered (danger)
0.00%
0 / 1
13.05
Automattic\Jetpack\Masterbar\dashboard_quick_switcher_record_usage
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Admin Menu loader.
4 *
5 * @package automattic/jetpack-masterbar
6 */
7
8namespace Automattic\Jetpack\Masterbar;
9
10use Automattic\Jetpack\Modules;
11use Automattic\Jetpack\Status\Host;
12use Automattic\Jetpack\Tracking;
13
14if ( ! defined( 'ABSPATH' ) ) {
15    exit( 0 );
16}
17
18/**
19 * Checks whether the navigation customizations should be performed for the given class.
20 *
21 * @param string $admin_menu_class Class name.
22 *
23 * @return bool
24 */
25function should_customize_nav( $admin_menu_class ) {
26    // Make sure the class extends the base admin menu class.
27    if ( ! is_subclass_of( $admin_menu_class, Base_Admin_Menu::class ) ) {
28        return false;
29    }
30
31    $is_api_request = defined( 'REST_REQUEST' ) && REST_REQUEST || isset( $_SERVER['REQUEST_URI'] ) && str_starts_with( filter_var( wp_unslash( $_SERVER['REQUEST_URI'] ) ), '/?rest_route=%2Fwpcom%2Fv2%2Fadmin-menu' );
32
33    // No nav customizations on WP Admin of Atomic sites when SSO is disabled.
34    if ( is_a( $admin_menu_class, Atomic_Admin_Menu::class, true ) && ! $is_api_request && ! ( new Modules() )->is_active( 'sso' ) ) {
35        return false;
36    }
37
38    return true;
39}
40
41/**
42 * Gets the name of the class that customizes the admin menu.
43 *
44 * @return string Class name.
45 */
46function get_admin_menu_class() {
47    // WordPress.com Atomic sites.
48    if ( ( new Host() )->is_woa_site() ) {
49
50        // DIFM Lite In Progress Atomic Sites. Uses the same menu used for domain-only sites.
51        // Ignore this check if we are in a support session.
52        $is_difm_lite_in_progress = wpcomsh_is_site_sticker_active( 'difm-lite-in-progress' );
53        $is_support_session       = defined( 'WPCOM_SUPPORT_SESSION' ) && WPCOM_SUPPORT_SESSION;
54        if ( $is_difm_lite_in_progress && ! $is_support_session ) {
55            require_once __DIR__ . '/class-domain-only-admin-menu.php';
56
57            // While the content form is still open the customer needs to read
58            // their own Posts, Media and Pages to fill it in. The sticker is
59            // absent for builds that predate it and whenever wpcom has not set
60            // it, so the fallback is the existing full lockout.
61            if ( wpcomsh_is_site_sticker_active( 'difm-lite-awaiting-content' ) ) {
62                require_once __DIR__ . '/class-difm-lite-admin-menu.php';
63                return DIFM_Lite_Admin_Menu::class;
64            }
65
66            return Domain_Only_Admin_Menu::class;
67        }
68
69        require_once __DIR__ . '/class-atomic-admin-menu.php';
70        return Atomic_Admin_Menu::class;
71    }
72
73    // WordPress.com Simple sites.
74    if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
75        $blog_id = get_current_blog_id();
76
77        // Domain-only sites.
78        $blog_options   = get_blog_option( $blog_id, 'options' );
79        $is_domain_only = ! empty( $blog_options['is_domain_only'] );
80        if ( $is_domain_only ) {
81            require_once __DIR__ . '/class-domain-only-admin-menu.php';
82            return Domain_Only_Admin_Menu::class;
83        }
84
85        // DIFM Lite In Progress Sites. Uses the same menu used for domain-only sites.
86        // Ignore this check if we are in a support session.
87        $is_difm_lite_in_progress = has_blog_sticker( 'difm-lite-in-progress' );
88        $is_support_session       = defined( 'WPCOM_SUPPORT_SESSION' ) && WPCOM_SUPPORT_SESSION;
89        if ( $is_difm_lite_in_progress && ! $is_support_session ) {
90            require_once __DIR__ . '/class-domain-only-admin-menu.php';
91
92            // See the Atomic branch above: pre-submit the customer keeps access
93            // to their own content, and an absent sticker falls back to the
94            // existing full lockout.
95            if ( has_blog_sticker( 'difm-lite-awaiting-content' ) ) {
96                require_once __DIR__ . '/class-difm-lite-admin-menu.php';
97                return DIFM_Lite_Admin_Menu::class;
98            }
99
100            return Domain_Only_Admin_Menu::class;
101        }
102
103        // Rest of simple sites.
104        require_once __DIR__ . '/class-wpcom-admin-menu.php';
105        return WPcom_Admin_Menu::class;
106    }
107
108    // Default menu class.
109    require_once __DIR__ . '/class-admin-menu.php';
110    return Admin_Menu::class;
111}
112
113/**
114 * Filters the name of the class that customizes the admin menu. It should extends the `Base_Admin_Menu` class.
115 *
116 * @module masterbar
117 *
118 * @since jetpack-9.6.0
119 *
120 * @param string $admin_menu_class Class name.
121 */
122$admin_menu_class = apply_filters( 'jetpack_admin_menu_class', get_admin_menu_class() );
123if ( should_customize_nav( $admin_menu_class ) ) {
124    /** The admin menu singleton instance. @var Base_Admin_Menu $instance */
125    $admin_menu_class::get_instance();
126
127    /**
128     * Trigger an event when the user uses the dashboard quick switcher.
129     *
130     * @param string $screen The current screen.
131     * @param string $view The view the user choosed to go to.
132     */
133    function dashboard_quick_switcher_record_usage( $screen, $view ) {
134        require_once __DIR__ . '/class-dashboard-switcher-tracking.php';
135
136        $tracking = new Dashboard_Switcher_Tracking(
137            new Tracking( Dashboard_Switcher_Tracking::get_jetpack_tracking_product() ),
138            array( Dashboard_Switcher_Tracking::class, 'wpcom_tracks_record_event' ),
139            Dashboard_Switcher_Tracking::get_plan()
140        );
141
142        $tracking->record_switch_event( $screen, $view );
143    }
144
145    \add_action( 'jetpack_dashboard_switcher_changed_view', __NAMESPACE__ . '\dashboard_quick_switcher_record_usage', 10, 2 );
146} else {
147    \add_filter( 'jetpack_load_admin_menu_class', '__return_false' );
148}