Completed
Push — add/test-changelog-cli ( c2d6aa...b7621b )
by
unknown
255:12 queued 240:55
created

WPcom_Admin_Menu::add_new_site_link()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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