Completed
Push — update/node-version ( 857559...c25464 )
by Jeremy
313:10 queued 285:45
created

Admin_Menu::enqueue_scripts()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 22

Duplication

Lines 5
Ratio 22.73 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 0
dl 5
loc 22
rs 9.568
c 0
b 0
f 0
1
<?php
2
/**
3
 * Admin Menu file.
4
 *
5
 * @package automattic/jetpack
6
 */
7
8
namespace Automattic\Jetpack\Dashboard_Customizations;
9
10
use Automattic\Jetpack\Redirect;
11
12
require_once __DIR__ . '/class-base-admin-menu.php';
13
14
/**
15
 * Class Admin_Menu.
16
 */
17
class Admin_Menu extends Base_Admin_Menu {
18
19
	/**
20
	 * Create the desired menu output.
21
	 */
22
	public function reregister_menu_items() {
23
		// Constant is not defined until parse_request.
24
		if ( ! $this->is_api_request ) {
25
			$this->is_api_request = defined( 'REST_REQUEST' ) && REST_REQUEST;
26
		}
27
28
		/*
29
		 * Whether links should point to Calypso or wp-admin.
30
		 *
31
		 * Options:
32
		 * false - Calypso (Default).
33
		 * true  - wp-admin.
34
		 */
35
		$wp_admin = $this->should_link_to_wp_admin();
36
37
		// Remove separators.
38
		remove_menu_page( 'separator1' );
39
40
		$this->add_stats_menu();
41
		$this->add_upgrades_menu();
42
		$this->add_posts_menu( $wp_admin );
43
		$this->add_media_menu( $wp_admin );
44
		$this->add_page_menu( $wp_admin );
45
		$this->add_testimonials_menu( $wp_admin );
46
		$this->add_portfolio_menu( $wp_admin );
47
		$this->add_comments_menu( $wp_admin );
48
49
		// Whether Themes/Customize links should point to Calypso (false) or wp-admin (true).
50
		$wp_admin_themes    = $wp_admin;
51
		$wp_admin_customize = $wp_admin;
52
		$this->add_appearance_menu( $wp_admin_themes, $wp_admin_customize );
53
		$this->add_plugins_menu( $wp_admin );
54
		$this->add_users_menu( $wp_admin );
55
56
		// Whether Import/Export links should point to Calypso (false) or wp-admin (true).
57
		$wp_admin_import = $wp_admin;
58
		$wp_admin_export = $wp_admin;
59
		$this->add_tools_menu( $wp_admin_import, $wp_admin_export );
60
61
		$this->add_options_menu( $wp_admin );
62
		$this->add_jetpack_menu();
63
		$this->add_gutenberg_menus( $wp_admin );
64
65
		// Remove Links Manager menu since its usage is discouraged.
66
		// @see https://core.trac.wordpress.org/ticket/21307#comment:73.
67
		remove_menu_page( 'link-manager.php' );
68
69
		ksort( $GLOBALS['menu'] );
70
	}
71
72
	/**
73
	 * Adds My Home menu.
74
	 */
75
	public function add_my_home_menu() {
76
		$this->update_menu( 'index.php', 'https://wordpress.com/home/' . $this->domain, __( 'My Home', 'jetpack' ), 'manage_options', 'dashicons-admin-home' );
77
	}
78
79
	/**
80
	 * Adds Stats menu.
81
	 */
82
	public function add_stats_menu() {
83
		add_menu_page( __( 'Stats', 'jetpack' ), __( 'Stats', 'jetpack' ), 'view_stats', 'https://wordpress.com/stats/day/' . $this->domain, null, 'dashicons-chart-bar', 3 );
84
	}
85
86
	/**
87
	 * Adds Upgrades menu.
88
	 */
89
	public function add_upgrades_menu() {
90
		global $menu;
91
92
		$menu_exists = false;
93
		foreach ( $menu as $item ) {
94
			if ( 'paid-upgrades.php' === $item[2] ) {
95
				$menu_exists = true;
96
				break;
97
			}
98
		}
99
100
		if ( ! $menu_exists ) {
101
			add_menu_page( __( 'Upgrades', 'jetpack' ), __( 'Upgrades', 'jetpack' ), 'manage_options', 'paid-upgrades.php', null, 'dashicons-cart', 4 );
102
		}
103
104
		add_submenu_page( 'paid-upgrades.php', __( 'Plans', 'jetpack' ), __( 'Plans', 'jetpack' ), 'manage_options', 'https://wordpress.com/plans/' . $this->domain, null, 5 );
105
		add_submenu_page( 'paid-upgrades.php', __( 'Purchases', 'jetpack' ), __( 'Purchases', 'jetpack' ), 'manage_options', 'https://wordpress.com/purchases/subscriptions/' . $this->domain, null, 15 );
106
107
		if ( ! $menu_exists ) {
108
			// Remove the submenu auto-created by Core.
109
			remove_submenu_page( 'paid-upgrades.php', 'paid-upgrades.php' );
110
		}
111
	}
112
113
	/**
114
	 * Adds Posts menu.
115
	 *
116
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
117
	 */
118 View Code Duplication
	public function add_posts_menu( $wp_admin = false ) {
119
		if ( $wp_admin ) {
120
			return;
121
		}
122
123
		$submenus_to_update = array(
124
			'edit.php'     => 'https://wordpress.com/posts/' . $this->domain,
125
			'post-new.php' => 'https://wordpress.com/post/' . $this->domain,
126
		);
127
		$this->update_submenus( 'edit.php', $submenus_to_update );
128
	}
129
130
	/**
131
	 * Adds Media menu.
132
	 *
133
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
134
	 */
135
	public function add_media_menu( $wp_admin = false ) {
136
		if ( $wp_admin ) {
137
			return;
138
		}
139
140
		remove_submenu_page( 'upload.php', 'media-new.php' );
141
142
		$this->update_menu( 'upload.php', 'https://wordpress.com/media/' . $this->domain );
143
	}
144
145
	/**
146
	 * Adds Page menu.
147
	 *
148
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
149
	 */
150 View Code Duplication
	public function add_page_menu( $wp_admin = false ) {
151
		if ( $wp_admin ) {
152
			return;
153
		}
154
155
		$submenus_to_update = array(
156
			'edit.php?post_type=page'     => 'https://wordpress.com/pages/' . $this->domain,
157
			'post-new.php?post_type=page' => 'https://wordpress.com/page/' . $this->domain,
158
		);
159
		$this->update_submenus( 'edit.php?post_type=page', $submenus_to_update );
160
	}
161
162
	/**
163
	 * Adds Testimonials menu.
164
	 *
165
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
166
	 */
167
	public function add_testimonials_menu( $wp_admin = false ) {
168
		$this->add_custom_post_type_menu( 'jetpack-testimonial', $wp_admin );
169
	}
170
171
	/**
172
	 * Adds Portfolio menu.
173
	 *
174
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
175
	 */
176
	public function add_portfolio_menu( $wp_admin = false ) {
177
		$this->add_custom_post_type_menu( 'jetpack-portfolio', $wp_admin );
178
	}
179
180
	/**
181
	 * Adds a custom post type menu.
182
	 *
183
	 * @param string $post_type Custom post type.
184
	 * @param bool   $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
185
	 */
186
	public function add_custom_post_type_menu( $post_type, $wp_admin = false ) {
187
		if ( $wp_admin ) {
188
			return;
189
		}
190
191
		$submenus_to_update = array(
192
			'edit.php?post_type=' . $post_type     => 'https://wordpress.com/types/' . $post_type . '/' . $this->domain,
193
			'post-new.php?post_type=' . $post_type => 'https://wordpress.com/edit/' . $post_type . '/' . $this->domain,
194
		);
195
		$this->update_submenus( 'edit.php?post_type=' . $post_type, $submenus_to_update );
196
	}
197
198
	/**
199
	 * Adds Comments menu.
200
	 *
201
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
202
	 */
203
	public function add_comments_menu( $wp_admin = false ) {
204
		if ( $wp_admin ) {
205
			return;
206
		}
207
208
		$this->update_menu( 'edit-comments.php', 'https://wordpress.com/comments/all/' . $this->domain );
209
	}
210
211
	/**
212
	 * Adds Appearance menu.
213
	 *
214
	 * @param bool $wp_admin_themes Optional. Whether Themes link should point to Calypso or wp-admin. Default false (Calypso).
215
	 * @param bool $wp_admin_customize Optional. Whether Customize link should point to Calypso or wp-admin. Default false (Calypso).
216
	 * @return string The Customizer URL.
217
	 */
218
	public function add_appearance_menu( $wp_admin_themes = false, $wp_admin_customize = false ) {
219
		$request_uri                     = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
220
		$default_customize_slug          = add_query_arg( 'return', rawurlencode( remove_query_arg( wp_removable_query_args(), $request_uri ) ), 'customize.php' );
221
		$default_customize_header_slug_1 = add_query_arg( array( 'autofocus' => array( 'control' => 'header_image' ) ), $default_customize_slug );
222
		// TODO: Remove WPCom_Theme_Customizer::modify_header_menu_links() and WPcom_Custom_Header::modify_admin_menu_links().
223
		$default_customize_header_slug_2     = admin_url( 'themes.php?page=custom-header' );
224
		$default_customize_background_slug_1 = add_query_arg( array( 'autofocus' => array( 'control' => 'background_image' ) ), $default_customize_slug );
225
		// TODO: Remove Colors_Manager::modify_header_menu_links() and Colors_Manager_Common::modify_header_menu_links().
226
		$default_customize_background_slug_2 = add_query_arg( array( 'autofocus' => array( 'section' => 'colors_manager_tool' ) ), admin_url( 'customize.php' ) );
227
228
		if ( ! $wp_admin_customize ) {
229
			$customize_url = 'https://wordpress.com/customize/' . $this->domain;
230
		} elseif ( $this->is_api_request ) {
231
			// In case this is an api request we will have to add the 'return' querystring via JS.
232
			$customize_url = 'customize.php';
233
		} else {
234
			$customize_url = $default_customize_slug;
235
		}
236
237
		$submenus_to_update = array(
238
			$default_customize_slug              => $customize_url,
239
			$default_customize_header_slug_1     => add_query_arg( array( 'autofocus' => array( 'control' => 'header_image' ) ), $customize_url ),
240
			$default_customize_header_slug_2     => add_query_arg( array( 'autofocus' => array( 'control' => 'header_image' ) ), $customize_url ),
241
			$default_customize_background_slug_1 => add_query_arg( array( 'autofocus' => array( 'section' => 'colors_manager_tool' ) ), $customize_url ),
242
			$default_customize_background_slug_2 => add_query_arg( array( 'autofocus' => array( 'section' => 'colors_manager_tool' ) ), $customize_url ),
243
		);
244
245
		if ( ! $wp_admin_themes ) {
246
			$submenus_to_update['themes.php'] = 'https://wordpress.com/themes/' . $this->domain;
247
		}
248
249
		if ( ! $wp_admin_customize ) {
250
			$submenus_to_update['widgets.php']       = add_query_arg( array( 'autofocus' => array( 'panel' => 'widgets' ) ), $customize_url );
251
			$submenus_to_update['gutenberg-widgets'] = add_query_arg( array( 'autofocus' => array( 'panel' => 'widgets' ) ), $customize_url );
252
			$submenus_to_update['nav-menus.php']     = add_query_arg( array( 'autofocus' => array( 'panel' => 'nav_menus' ) ), $customize_url );
253
		}
254
255
		$this->update_submenus( 'themes.php', $submenus_to_update );
256
257
		remove_submenu_page( 'themes.php', 'custom-header' );
258
		remove_submenu_page( 'themes.php', 'custom-background' );
259
260
		return $customize_url;
261
	}
262
263
	/**
264
	 * Adds Plugins menu.
265
	 *
266
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
267
	 */
268
	public function add_plugins_menu( $wp_admin = false ) {
269
		if ( $wp_admin ) {
270
			return;
271
		}
272
273
		remove_submenu_page( 'plugins.php', 'plugin-install.php' );
274
		remove_submenu_page( 'plugins.php', 'plugin-editor.php' );
275
276
		$this->update_menu( 'plugins.php', 'https://wordpress.com/plugins/' . $this->domain );
277
	}
278
279
	/**
280
	 * Adds Users menu.
281
	 *
282
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
283
	 */
284
	public function add_users_menu( $wp_admin = false ) {
285
		if ( current_user_can( 'list_users' ) ) {
286
			// We shall add the Calypso user management & add new user screens at all cases ( Calypso & Atomic ).
287
			$submenus_to_update = array(
288
				'user-new.php' => 'https://wordpress.com/people/new/' . $this->domain,
289
				'users.php'    => 'https://wordpress.com/people/team/' . $this->domain,
290
			);
291
			if ( ! $wp_admin ) {
292
				$submenus_to_update['profile.php'] = 'https://wordpress.com/me';
293
			}
294
			$this->update_submenus( 'users.php', $submenus_to_update );
295
			add_submenu_page( 'users.php', esc_attr__( 'Account Settings', 'jetpack' ), __( 'Account Settings', 'jetpack' ), 'read', 'https://wordpress.com/me/account' );
296
		} else {
297
			if ( ! $wp_admin ) {
298
				$submenus_to_update = array(
299
					'user-new.php' => 'https://wordpress.com/people/new/' . $this->domain,
300
					'profile.php'  => 'https://wordpress.com/me',
301
				);
302
				$this->update_submenus( 'profile.php', $submenus_to_update );
303
			}
304
305
			add_submenu_page( 'profile.php', esc_attr__( 'Account Settings', 'jetpack' ), __( 'Account Settings', 'jetpack' ), 'read', 'https://wordpress.com/me/account' );
306
		}
307
	}
308
309
	/**
310
	 * Adds Tools menu.
311
	 *
312
	 * @param bool $wp_admin_import Optional. Whether Import link should point to Calypso or wp-admin. Default false (Calypso).
313
	 * @param bool $wp_admin_export Optional. Whether Export link should point to Calypso or wp-admin. Default false (Calypso).
314
	 */
315
	public function add_tools_menu( $wp_admin_import = false, $wp_admin_export = false ) {
316
		$submenus_to_update = array();
317
		if ( ! $wp_admin_import ) {
318
			$submenus_to_update['import.php'] = 'https://wordpress.com/import/' . $this->domain;
319
		}
320
		if ( ! $wp_admin_export ) {
321
			$submenus_to_update['export.php'] = 'https://wordpress.com/export/' . $this->domain;
322
		}
323
		$this->update_submenus( 'tools.php', $submenus_to_update );
324
325
		remove_submenu_page( 'tools.php', 'tools.php' );
326
		remove_submenu_page( 'tools.php', 'delete-blog' );
327
328
		add_submenu_page( 'tools.php', esc_attr__( 'Marketing', 'jetpack' ), __( 'Marketing', 'jetpack' ), 'publish_posts', 'https://wordpress.com/marketing/tools/' . $this->domain, null, 0 );
329
		add_submenu_page( 'tools.php', esc_attr__( 'Earn', 'jetpack' ), __( 'Earn', 'jetpack' ), 'manage_options', 'https://wordpress.com/earn/' . $this->domain, null, 1 );
330
	}
331
332
	/**
333
	 * Adds Settings menu.
334
	 *
335
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
336
	 */
337
	public function add_options_menu( $wp_admin = false ) {
338
		if ( $wp_admin ) {
339
			return;
340
		}
341
342
		$this->update_submenus( 'options-general.php', array( 'options-general.php' => 'https://wordpress.com/settings/general/' . $this->domain ) );
343
344
		remove_submenu_page( 'options-general.php', 'options-discussion.php' );
345
		remove_submenu_page( 'options-general.php', 'options-writing.php' );
346
	}
347
348
	/**
349
	 * Adds Jetpack menu.
350
	 */
351
	public function add_jetpack_menu() {
352
		global $menu;
353
354
		$position = 50;
355
		while ( isset( $menu[ $position ] ) ) {
356
			$position++;
357
		}
358
		$this->add_admin_menu_separator( $position++, 'manage_options' );
359
360
		// TODO: Replace with proper SVG data url.
361
		$icon = "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='20' height='20' viewBox='0 0 32 32' %3E%3Cpath fill='%23a0a5aa' d='M16,0C7.2,0,0,7.2,0,16s7.2,16,16,16s16-7.2,16-16S24.8,0,16,0z'%3E%3C/path%3E%3Cpolygon fill='%23fff' points='15,19 7,19 15,3 '%3E%3C/polygon%3E%3Cpolygon fill='%23fff' points='17,29 17,13 25,13 '%3E%3C/polygon%3E%3C/svg%3E";
362
363
		$is_menu_updated = $this->update_menu( 'jetpack', null, null, null, $icon, $position );
364
		if ( ! $is_menu_updated ) {
365
			add_menu_page( esc_attr__( 'Jetpack', 'jetpack' ), __( 'Jetpack', 'jetpack' ), 'manage_options', 'jetpack', null, $icon, $position );
366
		}
367
368
		add_submenu_page( 'jetpack', esc_attr__( 'Activity Log', 'jetpack' ), __( 'Activity Log', 'jetpack' ), 'manage_options', 'https://wordpress.com/activity-log/' . $this->domain, null, 2 );
369
		add_submenu_page( 'jetpack', esc_attr__( 'Backup', 'jetpack' ), __( 'Backup', 'jetpack' ), 'manage_options', 'https://wordpress.com/backup/' . $this->domain, null, 3 );
370
		/* translators: Jetpack sidebar menu item. */
371
		add_submenu_page( 'jetpack', esc_attr__( 'Search', 'jetpack' ), __( 'Search', 'jetpack' ), 'read', 'https://wordpress.com/jetpack-search/' . $this->domain, null, 4 );
372
373
		remove_submenu_page( 'jetpack', 'stats' );
374
		remove_submenu_page( 'jetpack', esc_url( Redirect::get_url( 'calypso-backups' ) ) );
375
		remove_submenu_page( 'jetpack', esc_url( Redirect::get_url( 'calypso-scanner' ) ) );
376
377
		if ( ! $is_menu_updated ) {
378
			// Remove the submenu auto-created by Core.
379
			remove_submenu_page( 'jetpack', 'jetpack' );
380
		}
381
	}
382
383
	/**
384
	 * Re-adds the Site Editor menu without the (beta) tag, and where we want it.
385
	 *
386
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
387
	 */
388
	public function add_gutenberg_menus( $wp_admin = false ) {
389
		// We can bail if we don't meet the conditions of the Site Editor.
390
		if ( ! ( function_exists( 'gutenberg_is_fse_theme' ) && gutenberg_is_fse_theme() ) ) {
391
			return;
392
		}
393
394
		// Core Gutenberg registers without an explicit position, and we don't want the (beta) tag.
395
		remove_menu_page( 'gutenberg-edit-site' );
396
		// Core Gutenberg tries to manage its position, foiling our best laid plans. Unfoil.
397
		remove_filter( 'menu_order', 'gutenberg_menu_order' );
398
399
		$link = $wp_admin ? 'gutenberg-edit-site' : 'https://wordpress.com/site-editor/' . $this->domain;
400
401
		add_menu_page(
402
			__( 'Site Editor', 'jetpack' ),
403
			__( 'Site Editor', 'jetpack' ),
404
			'edit_theme_options',
405
			$link,
406
			$wp_admin ? 'gutenberg_edit_site_page' : null,
407
			'dashicons-layout',
408
			61 // Just under Appearance.
409
		);
410
	}
411
412
	/**
413
	 * Whether to use wp-admin pages rather than Calypso.
414
	 *
415
	 * @return bool
416
	 */
417
	public function should_link_to_wp_admin() {
418
		return get_user_option( 'jetpack_admin_menu_link_destination' );
419
	}
420
}
421