Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
37.14% covered (danger)
37.14%
39 / 105
13.33% covered (danger)
13.33%
2 / 15
CRAP
0.00% covered (danger)
0.00%
0 / 1
Atomic_Admin_Menu
37.50% covered (danger)
37.50%
39 / 104
13.33% covered (danger)
13.33%
2 / 15
410.34
0.00% covered (danger)
0.00%
0 / 1
 __construct
93.33% covered (success)
93.33%
14 / 15
0.00% covered (danger)
0.00%
0 / 1
2.00
 dequeue_scripts
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 is_rtl
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 reregister_menu_items
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 get_preferred_view
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
3.07
 add_appearance_menu
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 add_users_menu
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 add_plugins_menu
64.71% covered (warning)
64.71%
11 / 17
0.00% covered (danger)
0.00%
0 / 1
9.15
 set_browse_sites_link_class
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 add_new_site_link
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
3.14
 get_upsell_nudge
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
6
 override_the_theme_installer
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
 remove_gutenberg_menu
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 ajax_sidebar_state
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
6
 wp_ajax_jitm_dismiss
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * Atomic Admin Menu file.
4 *
5 * @package automattic/jetpack-masterbar
6 */
7
8namespace Automattic\Jetpack\Masterbar;
9
10use Automattic\Jetpack\Connection\Client;
11use Automattic\Jetpack\JITMS\JITM;
12use Automattic\Jetpack\Modules;
13
14require_once __DIR__ . '/class-admin-menu.php';
15
16/**
17 * Class Atomic_Admin_Menu.
18 */
19class Atomic_Admin_Menu extends Admin_Menu {
20
21    /**
22     * Atomic_Admin_Menu constructor.
23     */
24    protected function __construct() {
25        parent::__construct();
26
27        add_action( 'wp_enqueue_scripts', array( $this, 'dequeue_scripts' ), 20 );
28        add_action( 'admin_enqueue_scripts', array( $this, 'dequeue_scripts' ), 20 );
29        add_action( 'wp_ajax_sidebar_state', array( $this, 'ajax_sidebar_state' ) );
30        add_action( 'wp_ajax_jitm_dismiss', array( $this, 'wp_ajax_jitm_dismiss' ) );
31        add_action( 'adminmenu', array( $this, 'render_upsell_nudge' ), 100 );
32
33        if ( ! $this->is_api_request ) {
34            add_filter( 'submenu_file', array( $this, 'override_the_theme_installer' ), 10, 2 );
35        }
36
37        add_action(
38            'admin_menu',
39            function () {
40                remove_action( 'admin_menu', 'gutenberg_menu', 9 );
41            },
42            0
43        );
44    }
45
46    /**
47     * Dequeues unnecessary scripts.
48     */
49    public function dequeue_scripts() {
50        wp_dequeue_script( 'a8c_wpcom_masterbar_overrides' ); // Initially loaded in modules/masterbar/masterbar/class-masterbar.php.
51    }
52
53    /**
54     * Determines whether the current locale is right-to-left (RTL).
55     *
56     * Performs the check against the current locale set on the WordPress.com's account settings.
57     * See `Masterbar::__construct` in `modules/masterbar/masterbar/class-masterbar.php`.
58     */
59    public function is_rtl() {
60        return get_user_option( 'jetpack_wpcom_is_rtl' );
61    }
62
63    /**
64     * Create the desired menu output.
65     */
66    public function reregister_menu_items() {
67        parent::reregister_menu_items();
68
69        $this->remove_gutenberg_menu();
70
71        // Not needed outside of wp-admin.
72        if ( ! $this->is_api_request ) {
73            $this->add_new_site_link();
74        }
75
76        ksort( $GLOBALS['menu'] );
77    }
78
79    /**
80     * Get the preferred view for the given screen.
81     *
82     * @param string $screen Screen identifier.
83     * @param bool   $fallback_global_preference (Optional) Whether the global preference for all screens should be used
84     *                                           as fallback if there is no specific preference for the given screen.
85     *                                           Default: true.
86     * @return string
87     */
88    public function get_preferred_view( $screen, $fallback_global_preference = true ) {
89
90        // Export on Atomic sites are always managed on WP Admin.
91        if ( in_array( $screen, array( 'export.php' ), true ) ) {
92            return self::CLASSIC_VIEW;
93        }
94
95        /**
96         * When Jetpack SSO is disabled, we need to force Calypso because it might create confusion to be redirected to WP-Admin.
97         * Furthermore, because we don't display the quick switcher, users having an WP-Admin interface by default won't be able to go back to the Calyso version.
98         */
99        if ( ! ( new Modules() )->is_active( 'sso' ) ) {
100            return self::DEFAULT_VIEW;
101        }
102
103        return parent::get_preferred_view( $screen, $fallback_global_preference );
104    }
105
106    /**
107     * Add the appearance menu.
108     *
109     * @return string
110     */
111    public function add_appearance_menu() {
112        $customize_url                 = parent::add_appearance_menu();
113        $should_display_additional_css = current_user_can( 'customize' ) && ! wp_is_block_theme();
114
115        if ( ! $should_display_additional_css ) {
116            return $customize_url;
117        }
118
119        $customize_custom_css_url = add_query_arg( array( 'autofocus' => array( 'section' => 'custom_css' ) ), $customize_url );
120        // @phan-suppress-next-line PhanTypeMismatchArgumentProbablyReal -- Core should ideally document null for no-callback arg. https://core.trac.wordpress.org/ticket/52539.
121        add_submenu_page( 'themes.php', esc_attr__( 'Additional CSS', 'jetpack-masterbar' ), __( 'Additional CSS', 'jetpack-masterbar' ), 'customize', esc_url( $customize_custom_css_url ), null, 20 );
122
123        return $customize_url;
124    }
125
126    /**
127     * Adds Users menu.
128     */
129    public function add_users_menu() {
130        $slug = current_user_can( 'list_users' ) ? 'users.php' : 'profile.php';
131        if ( self::DEFAULT_VIEW === $this->get_preferred_view( 'users.php' ) ) {
132            $submenus_to_update = array(
133                'users.php' => 'https://wordpress.com/people/team/' . $this->domain,
134            );
135            $this->update_submenus( $slug, $submenus_to_update );
136        }
137    }
138
139    /**
140     * Adds Plugins menu.
141     */
142    public function add_plugins_menu() {
143
144        global $submenu;
145
146        // Calypso plugins screens link.
147        $plugins_slug = 'https://wordpress.com/plugins/' . $this->domain;
148
149        // Link to the Marketplace from Plugins > Add New on Atomic sites where the wpcom_admin_interface option is set to wp-admin.
150        if ( self::CLASSIC_VIEW === $this->get_preferred_view( 'plugins.php' ) ) {
151            $submenus_to_update = array( 'plugin-install.php' => $plugins_slug );
152            $this->update_submenus( 'plugins.php', $submenus_to_update );
153            return;
154        }
155
156        // Link to the Marketplace on sites that can't manage plugins.
157        if (
158            function_exists( 'wpcom_site_has_feature' ) &&
159            ! wpcom_site_has_feature( \WPCOM_Features::MANAGE_PLUGINS )
160        ) {
161            // @phan-suppress-next-line PhanTypeMismatchArgumentProbablyReal -- Core should ideally document null for no-callback arg. https://core.trac.wordpress.org/ticket/52539.
162            add_menu_page( __( 'Plugins', 'jetpack-masterbar' ), __( 'Plugins', 'jetpack-masterbar' ), 'manage_options', $plugins_slug, null, 'dashicons-admin-plugins', 65 );
163            return;
164        }
165
166        if ( ! isset( $submenu['plugins.php'] ) ) {
167            return;
168        }
169
170        $plugins_submenu = $submenu['plugins.php'];
171
172        // Move "Add New" plugin submenu to the top position.
173        foreach ( $plugins_submenu as $submenu_key => $submenu_keys ) {
174            if ( 'plugin-install.php' === $submenu_keys[2] ) {
175                // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
176                $submenu['plugins.php'] = array( $submenu_key => $plugins_submenu[ $submenu_key ] ) + $plugins_submenu;
177            }
178        }
179
180        $submenus_to_update = array( 'plugin-install.php' => $plugins_slug );
181
182        $this->update_submenus( 'plugins.php', $submenus_to_update );
183    }
184
185    /**
186     * Adds a custom element class for Site Switcher menu item.
187     *
188     * @param array $menu Associative array of administration menu items.
189     *
190     * @return array
191     */
192    public function set_browse_sites_link_class( array $menu ) {
193        foreach ( $menu as $key => $menu_item ) {
194            if ( 'site-switcher' !== $menu_item[3] ) {
195                continue;
196            }
197
198            $menu[ $key ][4] = add_cssclass( 'site-switcher', $menu_item[4] );
199            break;
200        }
201
202        return $menu;
203    }
204
205    /**
206     * Adds a link to the menu to create a new site.
207     */
208    public function add_new_site_link() {
209        $site_count = get_user_option( 'wpcom_site_count' );
210        if ( $site_count && $site_count > 1 ) {
211            return;
212        }
213
214        // @phan-suppress-next-line PhanTypeMismatchArgumentProbablyReal -- Core should ideally document null for no-callback arg. https://core.trac.wordpress.org/ticket/52539.
215        add_menu_page( __( 'Add New Site', 'jetpack-masterbar' ), __( 'Add New Site', 'jetpack-masterbar' ), 'read', 'https://wordpress.com/start?ref=calypso-sidebar', null, 'dashicons-plus-alt' );
216    }
217
218    /**
219     * Returns the first available upsell nudge.
220     *
221     * @return array
222     */
223    public function get_upsell_nudge() {
224        $jitm         = JITM::get_instance();
225        $message_path = 'calypso:sites:sidebar_notice';
226        $message      = $jitm->get_messages( $message_path, array( 'message_path' => $message_path ), false );
227
228        if ( isset( $message[0] ) ) {
229            $message = $message[0];
230            return array(
231                'content'                      => $message->content->message,
232                'cta'                          => $message->CTA->message, // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
233                'link'                         => $message->CTA->link, // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
234                'tracks_impression_event_name' => $message->tracks->display->name,
235                'tracks_impression_cta_name'   => $message->tracks->display->props->cta_name,
236                'tracks_click_event_name'      => $message->tracks->click->name,
237                'tracks_click_cta_name'        => $message->tracks->click->props->cta_name,
238                'dismissible'                  => $message->is_dismissible,
239                'feature_class'                => $message->feature_class,
240                'id'                           => $message->id,
241            );
242        }
243    }
244
245    /**
246     * Override the global submenu_file for theme-install.php page so the WP Admin menu item gets highlighted correctly.
247     *
248     * @param string $submenu_file The current pages $submenu_file global variable value.
249     * @return string | null
250     */
251    public function override_the_theme_installer( $submenu_file ) {
252        global $pagenow;
253
254        if ( 'themes.php' === $submenu_file && 'theme-install.php' === $pagenow ) {
255            return null;
256        }
257        return $submenu_file;
258    }
259
260    /**
261     * Also remove the Gutenberg plugin menu.
262     */
263    public function remove_gutenberg_menu() {
264        // Always remove the Gutenberg menu.
265        remove_menu_page( 'gutenberg' );
266    }
267
268    /**
269     * Saves the sidebar state ( expanded / collapsed ) via an ajax request.
270     */
271    public function ajax_sidebar_state() {
272        $expanded = isset( $_REQUEST['expanded'] ) ? filter_var( wp_unslash( $_REQUEST['expanded'] ), FILTER_VALIDATE_BOOLEAN ) : false; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
273        Client::wpcom_json_api_request_as_user(
274            '/me/preferences',
275            '2',
276            array(
277                'method' => 'POST',
278            ),
279            array( 'calypso_preferences' => (object) array( 'sidebarCollapsed' => ! $expanded ) ),
280            'wpcom'
281        );
282
283        wp_die();
284    }
285
286    /**
287     * Handle ajax requests to dismiss a just-in-time-message
288     */
289    public function wp_ajax_jitm_dismiss() {
290        check_ajax_referer( 'jitm_dismiss' );
291        $jitm = \Automattic\Jetpack\JITMS\JITM::get_instance();
292        if ( isset( $_REQUEST['id'] ) && isset( $_REQUEST['feature_class'] ) ) {
293            $jitm->dismiss( sanitize_text_field( wp_unslash( $_REQUEST['id'] ) ), sanitize_text_field( wp_unslash( $_REQUEST['feature_class'] ) ) );
294        }
295        wp_die();
296    }
297}