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