Completed
Push — fix/markdown-parser-breaking-s... ( 6ae5e6...f00a12 )
by Jeremy
222:51 queued 212:00
created

WPcom_Admin_Menu::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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