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