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