Completed
Push — fix/site-editor-menu ( 5f4451...4b2c49 )
by
unknown
498:39 queued 488:16
created

WPcom_Admin_Menu::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 5
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
	 * Test_Admin_Menu.
80
	 *
81
	 * @covers ::reregister_menu_items
82
	 */
83
	public function test_admin_menu_output() {
84
		global $menu;
85
86
		static::$admin_menu->reregister_menu_items();
87
88
		$this->assertSame(
0 ignored issues
show
Bug introduced by
The method assertSame() does not seem to exist on object<Automattic\Jetpac...tions\WPcom_Admin_Menu>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

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

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
347
		if ( ! empty( $user_attribute['linkDestination'] ) ) {
348
			$result = $user_attribute['linkDestination'];
349
		}
350
351
		return $result;
352
	}
353
}
354