Completed
Push — kraftbj-patch-2 ( f1a4bb...ba1272 )
by
unknown
25:04 queued 15:38
created

WPcom_Admin_Menu::add_plugins_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 1
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 View Code Duplication
	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( $wp_admin );
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
114
		if ( $default === $icon && blavatar_exists( $this->domain ) ) {
115
			$icon = blavatar_url( $this->domain, 'img', 32 );
116
		}
117
118
		$badge = '';
119
		if ( is_private_blog() ) {
120
			$badge .= sprintf(
121
				'<span class="site__badge site__badge-private">%s</span>',
122
				wpcom_is_coming_soon() ? esc_html__( 'Coming Soon', 'jetpack' ) : esc_html__( 'Private', 'jetpack' )
123
			);
124
		}
125
126
		if ( is_redirected_domain( $this->domain ) ) {
127
			$badge .= '<span class="site__badge site__badge-redirect">' . esc_html__( 'Redirect', 'jetpack' ) . '</span>';
128
		}
129
130
		if ( ! empty( get_option( 'options' )['is_domain_only'] ) ) {
131
			$badge .= '<span class="site__badge site__badge-domain-only">' . esc_html__( 'Domain', 'jetpack' ) . '</span>';
132
		}
133
134
		$site_card = '
135
<div class="site__info">
136
	<div class="site__title">%1$s</div>
137
	<div class="site__domain">%2$s</div>
138
	%3$s
139
</div>';
140
141
		$site_card = sprintf(
142
			$site_card,
143
			get_option( 'blogname' ),
144
			$this->domain,
145
			$badge
146
		);
147
148
		add_menu_page( 'site-card', $site_card, 'read', get_home_url(), null, $icon, 1 );
149
		add_filter( 'add_menu_classes', array( $this, 'set_site_card_menu_class' ) );
150
	}
151
152
	/**
153
	 * Adds a custom element class and id for Site Card's menu item.
154
	 *
155
	 * @param array $menu Associative array of administration menu items.
156
	 * @return array
157
	 */
158
	public function set_site_card_menu_class( array $menu ) {
159
		foreach ( $menu as $key => $menu_item ) {
160
			if ( 'site-card' !== $menu_item[3] ) {
161
				continue;
162
			}
163
164
			$classes = ' toplevel_page_site-card';
165
			if ( blavatar_exists( $this->domain ) ) {
166
				$classes .= ' has-site-icon';
167
			}
168
169
			$menu[ $key ][4] = $menu_item[4] . $classes;
170
			$menu[ $key ][5] = 'toplevel_page_site_card';
171
			break;
172
		}
173
174
		return $menu;
175
	}
176
177
	/**
178
	 * Adds Stats menu.
179
	 */
180
	public function add_stats_menu() {
181
		$menu_title = __( 'Stats', 'jetpack' );
182
183
		if ( ! $this->is_api_request ) {
184
			$menu_title .= sprintf(
185
				'<img class="sidebar-unified__sparkline" width="80" height="20" src="%1$s" alt="%2$s">',
186
				esc_url( site_url( 'wp-includes/charts/admin-bar-hours-scale-2x.php?masterbar=1&s=' . get_current_blog_id() ) ),
187
				esc_attr__( 'Hourly views', 'jetpack' )
188
			);
189
		}
190
191
		add_menu_page( __( 'Stats', 'jetpack' ), $menu_title, 'edit_posts', 'https://wordpress.com/stats/day/' . $this->domain, null, 'dashicons-chart-bar', 3 );
192
	}
193
194
	/**
195
	 * Adds Upgrades menu.
196
	 */
197
	public function add_upgrades_menu() {
198
		parent::add_upgrades_menu();
199
200
		add_submenu_page( 'https://wordpress.com/plans/' . $this->domain, __( 'Domains', 'jetpack' ), __( 'Domains', 'jetpack' ), 'manage_options', 'https://wordpress.com/domains/manage/' . $this->domain, null, 10 );
201
	}
202
203
	/**
204
	 * Adds Users menu.
205
	 *
206
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
207
	 */
208
	public function add_users_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
209
		// Users on Simple sites are always managed on Calypso.
210
		parent::add_users_menu( false );
211
	}
212
213
	/**
214
	 * Adds Tools menu.
215
	 *
216
	 * @param bool $wp_admin_import Optional. Whether Import link should point to Calypso or wp-admin. Default false (Calypso).
217
	 * @param bool $wp_admin_export Optional. Whether Export link should point to Calypso or wp-admin. Default false (Calypso).
218
	 */
219
	public function add_tools_menu( $wp_admin_import = false, $wp_admin_export = false ) {  // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
220
		// Export on Simple sites is always handled on Calypso.
221
		parent::add_tools_menu( $wp_admin_import, false );
222
	}
223
224
	/**
225
	 * Adds Settings menu.
226
	 *
227
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
228
	 */
229
	public function add_options_menu( $wp_admin = false ) {
230
		add_options_page( esc_attr__( 'Hosting Configuration', 'jetpack' ), __( 'Hosting Configuration', 'jetpack' ), 'manage_options', 'https://wordpress.com/hosting-config/' . $this->domain, null, 6 );
231
232
		// Replace sharing menu if it exists. See Publicize_UI::sharing_menu.
233
		if ( remove_submenu_page( 'options-general.php', 'sharing' ) ) {
234
			add_options_page( esc_attr__( 'Sharing Settings', 'jetpack' ), __( 'Sharing', 'jetpack' ), 'publish_posts', 'https://wordpress.com/marketing/sharing-buttons/' . $this->domain, null, 30 );
235
		}
236
237
		parent::add_options_menu( $wp_admin );
238
	}
239
240
	/**
241
	 * 1. Remove the Gutenberg plugin menu
242
	 * 2. Re-add the Site Editor 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_gutenberg_menus( $wp_admin = false ) {
247
		// Always remove the Gutenberg menu.
248
		remove_menu_page( 'gutenberg' );
249
250
		// We can bail if we don't meet the conditions of the Site Editor.
251
		if ( ! ( function_exists( 'gutenberg_is_fse_theme' ) && gutenberg_is_fse_theme() ) ) {
252
			return;
253
		}
254
255
		// Core Gutenberg registers without an explicit position, and we don't want the (beta) tag.
256
		remove_menu_page( 'gutenberg-edit-site' );
257
		// Core Gutenberg tries to manage its position, foiling our best laid plans. Unfoil.
258
		remove_filter( 'menu_order', 'gutenberg_menu_order' );
259
260
		$link = $wp_admin ? 'gutenberg-edit-site' : 'https://wordpress.com/site-editor/' . $this->domain;
261
262
		add_menu_page(
263
			__( 'Site Editor', 'jetpack' ),
264
			__( 'Site Editor', 'jetpack' ),
265
			'edit_theme_options',
266
			$link,
267
			$wp_admin ? 'gutenberg_edit_site_page' : null,
268
			'dashicons-layout',
269
			61 // Just under Appearance.
270
		);
271
	}
272
273
	/**
274
	 * Whether to use wp-admin pages rather than Calypso.
275
	 *
276
	 * @return bool
277
	 */
278
	public function should_link_to_wp_admin() {
279
		$result = false; // Calypso.
280
281
		$user_attribute = get_user_attribute( get_current_user_id(), 'calypso_preferences' );
282
		if ( ! empty( $user_attribute['linkDestination'] ) ) {
283
			$result = $user_attribute['linkDestination'];
284
		}
285
286
		return $result;
287
	}
288
289
	/**
290
	 * Adds Plugins menu.
291
	 *
292
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
293
	 */
294
	public function add_plugins_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
295
		// Plugins on Simple sites are always managed on Calypso.
296
		parent::add_plugins_menu( false );
297
	}
298
}
299