Completed
Push — update/wpcom-block-editor-shor... ( 9897fe...f43ed6 )
by Jeremy
148:26 queued 138:20
created

WPcom_Admin_Menu::add_tools_menu()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 4
rs 10
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
	/**
20
	 * Sets up class properties for REST API requests.
21
	 *
22
	 * @param WP_REST_Response $response Response from the endpoint.
23
	 */
24
	public function rest_api_init( $response ) {
25
		parent::rest_api_init( $response );
26
27
		// Get domain for requested site.
28
		$this->domain = ( new Status() )->get_site_suffix();
29
30
		return $response;
31
	}
32
33
	/**
34
	 * Create the desired menu output.
35
	 */
36
	public function reregister_menu_items() {
37
		parent::reregister_menu_items();
38
39
		$wp_admin = $this->should_link_to_wp_admin();
40
41
		$this->add_my_home_menu();
42
43
		// Not needed outside of wp-admin.
44
		if ( ! $this->is_api_request ) {
45
			$this->add_browse_sites_link();
46
			$this->add_site_card_menu();
47
			$this->add_new_site_link();
48
		}
49
50
		$this->add_gutenberg_menus( $wp_admin );
51
52
		ksort( $GLOBALS['menu'] );
53
	}
54
55
	/**
56
	 * Adds the site switcher link if user has more than one site.
57
	 */
58
	public function add_browse_sites_link() {
59
		if ( count( get_blogs_of_user( get_current_user_id() ) ) < 2 ) {
60
			return;
61
		}
62
63
		// Add the menu item.
64
		add_menu_page( __( 'site-switcher', 'jetpack' ), __( 'Browse sites', 'jetpack' ), 'read', 'https://wordpress.com/home', null, 'dashicons-arrow-left-alt2', 0 );
65
		add_filter( 'add_menu_classes', array( $this, 'set_browse_sites_link_class' ) );
66
	}
67
68
	/**
69
	 * Adds a custom element class for Site Switcher menu item.
70
	 *
71
	 * @param array $menu Associative array of administration menu items.
72
	 * @return array
73
	 */
74 View Code Duplication
	public function set_browse_sites_link_class( array $menu ) {
75
		foreach ( $menu as $key => $menu_item ) {
76
			if ( 'site-switcher' !== $menu_item[3] ) {
77
				continue;
78
			}
79
80
			$menu[ $key ][4] = add_cssclass( 'site-switcher', $menu_item[4] );
81
			break;
82
		}
83
84
		return $menu;
85
	}
86
87
	/**
88
	 * Adds a link to the menu to create a new site.
89
	 */
90
	public function add_new_site_link() {
91
		global $menu;
92
93
		if ( count( get_blogs_of_user( get_current_user_id() ) ) > 1 ) {
94
			return;
95
		}
96
97
		// Attempt to get last position.
98
		$position = 1000;
99
		while ( isset( $menu[ $position ] ) ) {
100
			$position++;
101
		}
102
103
		$this->add_admin_menu_separator( ++$position );
104
		add_menu_page( __( 'Add new site', 'jetpack' ), __( 'Add new site', 'jetpack' ), 'read', 'https://wordpress.com/start?ref=calypso-sidebar', null, 'dashicons-plus-alt', ++$position );
105
	}
106
107
	/**
108
	 * Adds site card component.
109
	 */
110
	public function add_site_card_menu() {
111
		$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>' );
112
		$icon      = get_site_icon_url( 32, $default );
113
		$blog_name = get_option( 'blogname' ) !== '' ? get_option( 'blogname' ) : $this->domain;
114
115
		if ( $default === $icon && blavatar_exists( $this->domain ) ) {
116
			$icon = blavatar_url( $this->domain, 'img', 32 );
117
		}
118
119
		$badge = '';
120
		if ( is_private_blog() ) {
121
			$badge .= sprintf(
122
				'<span class="site__badge site__badge-private">%s</span>',
123
				wpcom_is_coming_soon() ? esc_html__( 'Coming Soon', 'jetpack' ) : esc_html__( 'Private', 'jetpack' )
124
			);
125
		}
126
127
		if ( is_redirected_domain( $this->domain ) ) {
128
			$badge .= '<span class="site__badge site__badge-redirect">' . esc_html__( 'Redirect', 'jetpack' ) . '</span>';
129
		}
130
131
		if ( ! empty( get_option( 'options' )['is_domain_only'] ) ) {
132
			$badge .= '<span class="site__badge site__badge-domain-only">' . esc_html__( 'Domain', 'jetpack' ) . '</span>';
133
		}
134
135
		$site_card = '
136
<div class="site__info">
137
	<div class="site__title">%1$s</div>
138
	<div class="site__domain">%2$s</div>
139
	%3$s
140
</div>';
141
142
		$site_card = sprintf(
143
			$site_card,
144
			$blog_name,
145
			$this->domain,
146
			$badge
147
		);
148
149
		add_menu_page( 'site-card', $site_card, 'read', get_home_url(), null, $icon, 1 );
150
		add_filter( 'add_menu_classes', array( $this, 'set_site_card_menu_class' ) );
151
	}
152
153
	/**
154
	 * Adds a custom element class and id for Site Card's menu item.
155
	 *
156
	 * @param array $menu Associative array of administration menu items.
157
	 * @return array
158
	 */
159
	public function set_site_card_menu_class( array $menu ) {
160
		foreach ( $menu as $key => $menu_item ) {
161
			if ( 'site-card' !== $menu_item[3] ) {
162
				continue;
163
			}
164
165
			$classes = ' toplevel_page_site-card';
166
			if ( blavatar_exists( $this->domain ) ) {
167
				$classes .= ' has-site-icon';
168
			}
169
170
			$menu[ $key ][4] = $menu_item[4] . $classes;
171
			$menu[ $key ][5] = 'toplevel_page_site_card';
172
			break;
173
		}
174
175
		return $menu;
176
	}
177
178
	/**
179
	 * Adds Stats menu.
180
	 */
181
	public function add_stats_menu() {
182
		$menu_title = __( 'Stats', 'jetpack' );
183
184
		if ( ! $this->is_api_request ) {
185
			$menu_title .= sprintf(
186
				'<img class="sidebar-unified__sparkline" width="80" height="20" src="%1$s" alt="%2$s">',
187
				esc_url( site_url( 'wp-includes/charts/admin-bar-hours-scale-2x.php?masterbar=1&s=' . get_current_blog_id() ) ),
188
				esc_attr__( 'Hourly views', 'jetpack' )
189
			);
190
		}
191
192
		add_menu_page( __( 'Stats', 'jetpack' ), $menu_title, 'edit_posts', 'https://wordpress.com/stats/day/' . $this->domain, null, 'dashicons-chart-bar', 3 );
193
	}
194
195
	/**
196
	 * Adds Upgrades menu.
197
	 */
198
	public function add_upgrades_menu() {
199
		parent::add_upgrades_menu();
200
201
		add_submenu_page( 'paid-upgrades.php', __( 'Domains', 'jetpack' ), __( 'Domains', 'jetpack' ), 'manage_options', 'https://wordpress.com/domains/manage/' . $this->domain, null, 10 );
202
	}
203
204
	/**
205
	 * Adds Appearance menu.
206
	 *
207
	 * @param bool $wp_admin_themes Optional. Whether Themes link should point to Calypso or wp-admin. Default false (Calypso).
208
	 * @param bool $wp_admin_customize Optional. Whether Customize link should point to Calypso or wp-admin. Default false (Calypso).
209
	 */
210
	public function add_appearance_menu( $wp_admin_themes = false, $wp_admin_customize = false ) {
211
		$customize_url = parent::add_appearance_menu( $wp_admin_themes, $wp_admin_customize );
212
213
		remove_submenu_page( 'themes.php', 'theme-editor.php' );
214
215
		$user_can_customize = current_user_can( 'customize' );
216
217
		if ( $user_can_customize ) {
218
			// If the user does not have the custom CSS option then present them with the CSS nudge upsell section instead.
219
			$custom_css_section = '1' === get_option( 'custom-design-upgrade' ) ? 'jetpack_custom_css' : 'css_nudge'; //phpcs:ignore
220
			$customize_custom_css_url = add_query_arg( array( 'autofocus' => array( 'section' => $custom_css_section ) ), $customize_url );
221
			add_submenu_page( 'themes.php', esc_attr__( 'Edit CSS', 'jetpack' ), __( 'Edit CSS', 'jetpack' ), 'customize', esc_url( $customize_custom_css_url ), null, 20 );
222
		}
223
	}
224
225
	/**
226
	 * Adds Users menu.
227
	 *
228
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
229
	 */
230
	public function add_users_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
231
		if ( current_user_can( 'list_users' ) ) {
232
			$submenus_to_update = array(
233
				'users.php'              => 'https://wordpress.com/people/team/' . $this->domain,
234
				'grofiles-editor'        => 'https://wordpress.com/me',
235
				'grofiles-user-settings' => 'https://wordpress.com/me/account',
236
			);
237
			$this->update_submenus( 'users.php', $submenus_to_update );
238
		} else {
239
			$submenus_to_update = array(
240
				'grofiles-editor'        => 'https://wordpress.com/me',
241
				'grofiles-user-settings' => 'https://wordpress.com/me/account',
242
			);
243
			$this->update_submenus( 'profile.php', $submenus_to_update );
244
		}
245
		add_submenu_page( 'users.php', esc_attr__( 'Add New', 'jetpack' ), __( 'Add New', 'jetpack' ), 'promote_users', 'https://wordpress.com/people/new/' . $this->domain, null, 1 );
246
	}
247
248
	/**
249
	 * Adds Tools menu.
250
	 *
251
	 * @param bool $wp_admin_import Optional. Whether Import link should point to Calypso or wp-admin. Default false (Calypso).
252
	 * @param bool $wp_admin_export Optional. Whether Export link should point to Calypso or wp-admin. Default false (Calypso).
253
	 */
254
	public function add_tools_menu( $wp_admin_import = false, $wp_admin_export = false ) {  // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
255
		// Export on Simple sites is always handled on Calypso.
256
		parent::add_tools_menu( $wp_admin_import, false );
257
	}
258
259
	/**
260
	 * Adds Settings menu.
261
	 *
262
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
263
	 */
264 View Code Duplication
	public function add_options_menu( $wp_admin = false ) {
265
		parent::add_options_menu( $wp_admin );
266
267
		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 );
268
269
		// Replace sharing menu if it exists. See Publicize_UI::sharing_menu.
270
		if ( remove_submenu_page( 'options-general.php', 'sharing' ) ) {
271
			add_submenu_page( 'options-general.php', esc_attr__( 'Sharing Settings', 'jetpack' ), __( 'Sharing', 'jetpack' ), 'publish_posts', 'https://wordpress.com/marketing/sharing-buttons/' . $this->domain, null, 30 );
272
		}
273
	}
274
275
	/**
276
	 * 1. Remove the Gutenberg plugin menu
277
	 * 2. Re-add the Site Editor menu
278
	 *
279
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
280
	 */
281
	public function add_gutenberg_menus( $wp_admin = false ) {
282
		// Always remove the Gutenberg menu.
283
		remove_menu_page( 'gutenberg' );
284
285
		// We can bail if we don't meet the conditions of the Site Editor.
286
		if ( ! ( function_exists( 'gutenberg_is_fse_theme' ) && gutenberg_is_fse_theme() ) ) {
287
			return;
288
		}
289
290
		// Core Gutenberg registers without an explicit position, and we don't want the (beta) tag.
291
		remove_menu_page( 'gutenberg-edit-site' );
292
		// Core Gutenberg tries to manage its position, foiling our best laid plans. Unfoil.
293
		remove_filter( 'menu_order', 'gutenberg_menu_order' );
294
295
		$link = $wp_admin ? 'gutenberg-edit-site' : 'https://wordpress.com/site-editor/' . $this->domain;
296
297
		add_menu_page(
298
			__( 'Site Editor', 'jetpack' ),
299
			__( 'Site Editor', 'jetpack' ),
300
			'edit_theme_options',
301
			$link,
302
			$wp_admin ? 'gutenberg_edit_site_page' : null,
303
			'dashicons-layout',
304
			61 // Just under Appearance.
305
		);
306
	}
307
308
	/**
309
	 * Whether to use wp-admin pages rather than Calypso.
310
	 *
311
	 * @return bool
312
	 */
313
	public function should_link_to_wp_admin() {
314
		$result = false; // Calypso.
315
316
		$user_attribute = get_user_attribute( get_current_user_id(), 'calypso_preferences' );
317
		if ( ! empty( $user_attribute['linkDestination'] ) ) {
318
			$result = $user_attribute['linkDestination'];
319
		}
320
321
		return $result;
322
	}
323
324
	/**
325
	 * Adds Plugins menu.
326
	 *
327
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
328
	 */
329
	public function add_plugins_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
330
		// TODO: Remove wpcom_menu (/wp-content/admin-plugins/wpcom-misc.php).
331
		$count = '';
332
		if ( ! is_multisite() && current_user_can( 'update_plugins' ) ) {
333
			$update_data = wp_get_update_data();
334
			$count       = sprintf(
335
				'<span class="update-plugins count-%s"><span class="plugin-count">%s</span></span>',
336
				$update_data['counts']['plugins'],
337
				number_format_i18n( $update_data['counts']['plugins'] )
338
			);
339
		}
340
		/* translators: %s: Number of pending plugin updates. */
341
		add_menu_page( esc_attr__( 'Plugins', 'jetpack' ), sprintf( __( 'Plugins %s', 'jetpack' ), $count ), 'activate_plugins', 'plugins.php', null, 'dashicons-admin-plugins', 65 );
342
343
		// Plugins on Simple sites are always managed on Calypso.
344
		parent::add_plugins_menu( false );
345
	}
346
}
347