Completed
Push — fix/parent-selector-for-premiu... ( cac9af...425486 )
by Jeremy
20:54 queued 09:58
created

WPcom_Admin_Menu::remove_submenus()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 29
rs 9.456
c 0
b 0
f 0
1
<?php
2
/**
3
 * WP.com Admin Menu file.
4
 *
5
 * @package automattic/jetpack
6
 */
7
8
namespace Automattic\Jetpack\Dashboard_Customizations;
9
10
use Automattic\Jetpack\Status;
11
12
require_once __DIR__ . '/class-admin-menu.php';
13
14
/**
15
 * Class WPcom_Admin_Menu.
16
 */
17
class WPcom_Admin_Menu extends Admin_Menu {
18
	/**
19
	 * WPcom_Admin_Menu constructor.
20
	 */
21
	protected function __construct() {
22
		parent::__construct();
23
24
		add_action( 'wp_ajax_sidebar_state', array( $this, 'ajax_sidebar_state' ) );
25
		add_action( 'admin_init', array( $this, 'sync_sidebar_collapsed_state' ) );
26
		add_action( 'admin_menu', array( $this, 'remove_submenus' ), 140 ); // After hookpress hook at 130.
27
	}
28
29
	/**
30
	 * Sets up class properties for REST API requests.
31
	 *
32
	 * @param WP_REST_Response $response Response from the endpoint.
33
	 */
34
	public function rest_api_init( $response ) {
35
		parent::rest_api_init( $response );
36
37
		// Get domain for requested site.
38
		$this->domain = ( new Status() )->get_site_suffix();
39
40
		return $response;
41
	}
42
43
	/**
44
	 * Create the desired menu output.
45
	 */
46 View Code Duplication
	public function reregister_menu_items() {
47
		parent::reregister_menu_items();
48
49
		$this->add_my_home_menu();
50
51
		// Not needed outside of wp-admin.
52
		if ( ! $this->is_api_request ) {
53
			$this->add_browse_sites_link();
54
			$this->add_site_card_menu();
55
			$this->add_new_site_link();
56
		}
57
58
		ksort( $GLOBALS['menu'] );
59
	}
60
61
	/**
62
	 * Adds the site switcher link if user has more than one site.
63
	 */
64
	public function add_browse_sites_link() {
65
		if ( count( get_blogs_of_user( get_current_user_id() ) ) < 2 ) {
66
			return;
67
		}
68
69
		// Add the menu item.
70
		add_menu_page( __( 'site-switcher', 'jetpack' ), __( 'Browse sites', 'jetpack' ), 'read', 'https://wordpress.com/home', null, 'dashicons-arrow-left-alt2', 0 );
71
		add_filter( 'add_menu_classes', array( $this, 'set_browse_sites_link_class' ) );
72
	}
73
74
	/**
75
	 * Adds a custom element class for Site Switcher menu item.
76
	 *
77
	 * @param array $menu Associative array of administration menu items.
78
	 * @return array
79
	 */
80 View Code Duplication
	public function set_browse_sites_link_class( array $menu ) {
81
		foreach ( $menu as $key => $menu_item ) {
82
			if ( 'site-switcher' !== $menu_item[3] ) {
83
				continue;
84
			}
85
86
			$menu[ $key ][4] = add_cssclass( 'site-switcher', $menu_item[4] );
87
			break;
88
		}
89
90
		return $menu;
91
	}
92
93
	/**
94
	 * Adds a link to the menu to create a new site.
95
	 */
96
	public function add_new_site_link() {
97
		if ( count( get_blogs_of_user( get_current_user_id() ) ) > 1 ) {
98
			return;
99
		}
100
101
		$this->add_admin_menu_separator();
102
		add_menu_page( __( 'Add New Site', 'jetpack' ), __( 'Add New Site', 'jetpack' ), 'read', 'https://wordpress.com/start?ref=calypso-sidebar', null, 'dashicons-plus-alt' );
103
	}
104
105
	/**
106
	 * Adds site card component.
107
	 */
108
	public function add_site_card_menu() {
109
		$default   = 'data:image/svg+xml,' . rawurlencode( '<svg class="gridicon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><title>Globe</title><rect fill-opacity="0" x="0" width="24" height="24"/><g><path fill="#fff" d="M12 2C6.477 2 2 6.477 2 12s4.477 10 10 10 10-4.477 10-10S17.523 2 12 2zm0 18l2-2 1-1v-2h-2v-1l-1-1H9v3l2 2v1.93c-3.94-.494-7-3.858-7-7.93l1 1h2v-2h2l3-3V6h-2L9 5v-.41C9.927 4.21 10.94 4 12 4s2.073.212 3 .59V6l-1 1v2l1 1 3.13-3.13c.752.897 1.304 1.964 1.606 3.13H18l-2 2v2l1 1h2l.286.286C18.03 18.06 15.24 20 12 20z"/></g></svg>' );
110
		$icon      = get_site_icon_url( 32, $default );
111
		$blog_name = get_option( 'blogname' ) !== '' ? get_option( 'blogname' ) : $this->domain;
112
113
		if ( $default === $icon && blavatar_exists( $this->domain ) ) {
114
			$icon = blavatar_url( $this->domain, 'img', 32 );
115
		}
116
117
		$badge = '';
118
		if ( is_private_blog() ) {
119
			$badge .= sprintf(
120
				'<span class="site__badge site__badge-private">%s</span>',
121
				wpcom_is_coming_soon() ? esc_html__( 'Coming Soon', 'jetpack' ) : esc_html__( 'Private', 'jetpack' )
122
			);
123
		}
124
125
		if ( is_redirected_domain( $this->domain ) ) {
126
			$badge .= '<span class="site__badge site__badge-redirect">' . esc_html__( 'Redirect', 'jetpack' ) . '</span>';
127
		}
128
129
		if ( ! empty( get_option( 'options' )['is_domain_only'] ) ) {
130
			$badge .= '<span class="site__badge site__badge-domain-only">' . esc_html__( 'Domain', 'jetpack' ) . '</span>';
131
		}
132
133
		$site_card = '
134
<div class="site__info">
135
	<div class="site__title">%1$s</div>
136
	<div class="site__domain">%2$s</div>
137
	%3$s
138
</div>';
139
140
		$site_card = sprintf(
141
			$site_card,
142
			$blog_name,
143
			$this->domain,
144
			$badge
145
		);
146
147
		add_menu_page( 'site-card', $site_card, 'read', get_home_url(), null, $icon, 1 );
148
		add_filter( 'add_menu_classes', array( $this, 'set_site_card_menu_class' ) );
149
	}
150
151
	/**
152
	 * Adds a custom element class and id for Site Card's menu item.
153
	 *
154
	 * @param array $menu Associative array of administration menu items.
155
	 * @return array
156
	 */
157
	public function set_site_card_menu_class( array $menu ) {
158
		foreach ( $menu as $key => $menu_item ) {
159
			if ( 'site-card' !== $menu_item[3] ) {
160
				continue;
161
			}
162
163
			$classes = ' toplevel_page_site-card';
164
			if ( blavatar_exists( $this->domain ) ) {
165
				$classes .= ' has-site-icon';
166
			}
167
168
			$menu[ $key ][4] = $menu_item[4] . $classes;
169
			$menu[ $key ][5] = 'toplevel_page_site_card';
170
			break;
171
		}
172
173
		return $menu;
174
	}
175
176
	/**
177
	 * Adds Stats menu.
178
	 */
179
	public function add_stats_menu() {
180
		$menu_title = __( 'Stats', 'jetpack' );
181
182
		if ( ! $this->is_api_request ) {
183
			$menu_title .= sprintf(
184
				'<img class="sidebar-unified__sparkline" width="80" height="20" src="%1$s" alt="%2$s">',
185
				esc_url( site_url( 'wp-includes/charts/admin-bar-hours-scale-2x.php?masterbar=1&s=' . get_current_blog_id() ) ),
186
				esc_attr__( 'Hourly views', 'jetpack' )
187
			);
188
		}
189
190
		add_menu_page( __( 'Stats', 'jetpack' ), $menu_title, 'edit_posts', 'https://wordpress.com/stats/day/' . $this->domain, null, 'dashicons-chart-bar', 3 );
191
	}
192
193
	/**
194
	 * Adds Upgrades menu.
195
	 *
196
	 * @param string $plan The current WPCOM plan of the blog.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $plan not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
197
	 */
198
	public function add_upgrades_menu( $plan = null ) {
199
		if ( class_exists( 'WPCOM_Store_API' ) ) {
200
			$products = \WPCOM_Store_API::get_current_plan( get_current_blog_id() );
201
			if ( array_key_exists( 'product_name_short', $products ) ) {
202
				$plan = $products['product_name_short'];
203
			}
204
		}
205
		parent::add_upgrades_menu( $plan );
206
207
		$last_upgrade_submenu_position = $this->get_submenu_item_count( 'paid-upgrades.php' );
208
209
		add_submenu_page( 'paid-upgrades.php', __( 'Domains', 'jetpack' ), __( 'Domains', 'jetpack' ), 'manage_options', 'https://wordpress.com/domains/manage/' . $this->domain, null, $last_upgrade_submenu_position - 1 );
210
211
		/** This filter is already documented in modules/masterbar/admin-menu/class-atomic-admin-menu.php */
212 View Code Duplication
		if ( apply_filters( 'jetpack_show_wpcom_upgrades_email_menu', false ) ) {
213
			add_submenu_page( 'paid-upgrades.php', __( 'Emails', 'jetpack' ), __( 'Emails', 'jetpack' ), 'manage_options', 'https://wordpress.com/email/' . $this->domain, null, $last_upgrade_submenu_position );
214
		}
215
	}
216
217
	/**
218
	 * Adds Appearance menu.
219
	 *
220
	 * @param bool $wp_admin_themes Optional. Whether Themes link should point to Calypso or wp-admin. Default false (Calypso).
221
	 * @param bool $wp_admin_customize Optional. Whether Customize link should point to Calypso or wp-admin. Default false (Calypso).
222
	 */
223
	public function add_appearance_menu( $wp_admin_themes = false, $wp_admin_customize = false ) {
224
		// $wp_admin_themes can have a `true` value here if the user has activated the "Show advanced dashboard pages" account setting.
225
		// We force $wp_admin_themes to `false` anyways, since Simple sites should always see the Calypso Theme showcase.
226
		$wp_admin_themes = false;
227
		$customize_url   = parent::add_appearance_menu( $wp_admin_themes, $wp_admin_customize );
228
229
		$this->hide_submenu_page( 'themes.php', 'theme-editor.php' );
230
231
		$user_can_customize = current_user_can( 'customize' );
232
233
		if ( $user_can_customize ) {
234
			// If the user does not have the custom CSS option then present them with the CSS nudge upsell section instead.
235
			$custom_css_section = '1' === get_option( 'custom-design-upgrade' ) ? 'jetpack_custom_css' : 'css_nudge'; //phpcs:ignore
236
			$customize_custom_css_url = add_query_arg( array( 'autofocus' => array( 'section' => $custom_css_section ) ), $customize_url );
237
			add_submenu_page( 'themes.php', esc_attr__( 'Additional CSS', 'jetpack' ), __( 'Additional CSS', 'jetpack' ), 'customize', esc_url( $customize_custom_css_url ), null, 20 );
238
		}
239
	}
240
241
	/**
242
	 * Adds Users menu.
243
	 *
244
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
245
	 */
246
	public function add_users_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
247
		if ( current_user_can( 'list_users' ) ) {
248
			$submenus_to_update = array(
249
				'users.php'              => 'https://wordpress.com/people/team/' . $this->domain,
250
				'grofiles-editor'        => 'https://wordpress.com/me',
251
				'grofiles-user-settings' => 'https://wordpress.com/me/account',
252
			);
253
			$this->update_submenus( 'users.php', $submenus_to_update );
254
		} else {
255
			$submenus_to_update = array(
256
				'grofiles-editor'        => 'https://wordpress.com/me',
257
				'grofiles-user-settings' => 'https://wordpress.com/me/account',
258
			);
259
			$this->update_submenus( 'profile.php', $submenus_to_update );
260
		}
261
		add_submenu_page( 'users.php', esc_attr__( 'Add New', 'jetpack' ), __( 'Add New', 'jetpack' ), 'promote_users', 'https://wordpress.com/people/new/' . $this->domain, null, 1 );
262
	}
263
264
	/**
265
	 * Adds Settings menu.
266
	 *
267
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
268
	 */
269
	public function add_options_menu( $wp_admin = false ) {
270
		parent::add_options_menu( $wp_admin );
271
272
		add_submenu_page( 'options-general.php', esc_attr__( 'Hosting Configuration', 'jetpack' ), __( 'Hosting Configuration', 'jetpack' ), 'manage_options', 'https://wordpress.com/hosting-config/' . $this->domain, null, 6 );
273
	}
274
275
	/**
276
	 * Also remove the Gutenberg plugin menu.
277
	 *
278
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
279
	 */
280
	public function add_gutenberg_menus( $wp_admin = false ) {
281
		// Always remove the Gutenberg menu.
282
		remove_menu_page( 'gutenberg' );
283
		parent::add_gutenberg_menus( $wp_admin );
284
	}
285
286
	/**
287
	 * Whether to use wp-admin pages rather than Calypso.
288
	 *
289
	 * @return bool
290
	 */
291
	public function should_link_to_wp_admin() {
292
		$result = false; // Calypso.
293
294
		$user_attribute = get_user_attribute( get_current_user_id(), 'calypso_preferences' );
295
		if ( ! empty( $user_attribute['linkDestination'] ) ) {
296
			$result = $user_attribute['linkDestination'];
297
		}
298
299
		return $result;
300
	}
301
302
	/**
303
	 * Adds Plugins menu.
304
	 *
305
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
306
	 */
307
	public function add_plugins_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
308
		// TODO: Remove wpcom_menu (/wp-content/admin-plugins/wpcom-misc.php).
309
		$count = '';
310
		if ( ! is_multisite() && current_user_can( 'update_plugins' ) ) {
311
			$update_data = wp_get_update_data();
312
			$count       = sprintf(
313
				'<span class="update-plugins count-%s"><span class="plugin-count">%s</span></span>',
314
				$update_data['counts']['plugins'],
315
				number_format_i18n( $update_data['counts']['plugins'] )
316
			);
317
		}
318
		/* translators: %s: Number of pending plugin updates. */
319
		add_menu_page( esc_attr__( 'Plugins', 'jetpack' ), sprintf( __( 'Plugins %s', 'jetpack' ), $count ), 'activate_plugins', 'plugins.php', null, 'dashicons-admin-plugins', 65 );
320
321
		// Plugins on Simple sites are always managed on Calypso.
322
		parent::add_plugins_menu( false );
323
	}
324
325
	/**
326
	 * Saves the sidebar state ( expanded / collapsed ) via an ajax request.
327
	 */
328
	public function ajax_sidebar_state() {
329
		$expanded    = filter_var( $_REQUEST['expanded'], FILTER_VALIDATE_BOOLEAN ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
330
		$user_id     = get_current_user_id();
331
		$preferences = get_user_attribute( $user_id, 'calypso_preferences' );
332
		if ( empty( $preferences ) ) {
333
			$preferences = array();
334
		}
335
336
		$value = array_merge( (array) $preferences, array( 'sidebarCollapsed' => ! $expanded ) );
337
		$value = array_filter(
338
			$value,
339
			function ( $preference ) {
340
				return null !== $preference;
341
			}
342
		);
343
344
		update_user_attribute( $user_id, 'calypso_preferences', $value );
345
346
		die();
347
	}
348
349
	/**
350
	 * Syncs the sidebar collapsed state from Calypso Preferences.
351
	 */
352
	public function sync_sidebar_collapsed_state() {
353
		$calypso_preferences = get_user_attribute( get_current_user_id(), 'calypso_preferences' );
354
355
		$sidebar_collapsed = isset( $calypso_preferences['sidebarCollapsed'] ) ? $calypso_preferences['sidebarCollapsed'] : false;
356
		set_user_setting( 'mfold', $sidebar_collapsed ? 'f' : 'o' );
357
	}
358
359
	/**
360
	 * Removes unwanted submenu items.
361
	 *
362
	 * These submenus are added across wp-content and should be removed together with these function calls.
363
	 */
364
	public function remove_submenus() {
365
		global $_registered_pages;
366
367
		remove_submenu_page( 'index.php', 'akismet-stats' );
368
		remove_submenu_page( 'index.php', 'my-comments' );
369
		remove_submenu_page( 'index.php', 'stats' );
370
		remove_submenu_page( 'index.php', 'subscriptions' );
371
372
		/* @see https://github.com/Automattic/wp-calypso/issues/49210 */
373
		remove_submenu_page( 'index.php', 'my-blogs' );
374
		$_registered_pages['admin_page_my-blogs'] = true; // phpcs:ignore
375
376
		remove_submenu_page( 'paid-upgrades.php', 'premium-themes' );
377
		remove_submenu_page( 'paid-upgrades.php', 'domains' );
378
		remove_submenu_page( 'paid-upgrades.php', 'my-upgrades' );
379
		remove_submenu_page( 'paid-upgrades.php', 'billing-history' );
380
381
		remove_submenu_page( 'themes.php', 'customize.php?autofocus[panel]=amp_panel&return=' . rawurlencode( admin_url() ) );
382
383
		remove_submenu_page( 'users.php', 'wpcom-invite-users' ); // Wpcom_Invite_Users::action_admin_menu.
384
385
		remove_submenu_page( 'options-general.php', 'adcontrol' );
386
387
		// Remove menu item but continue allowing access.
388
		foreach ( array( 'openidserver', 'webhooks' ) as $page_slug ) {
389
			remove_submenu_page( 'options-general.php', $page_slug );
390
			$_registered_pages[ 'admin_page_' . $page_slug ] = true; // phpcs:ignore
391
		}
392
	}
393
}
394