Completed
Push — try/wpcom-nav-package ( c38648 )
by
unknown
12:50 queued 02:43
created

Admin_Menu::add_upgrades_menu()   B

Complexity

Conditions 6
Paths 18

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 18
nop 1
dl 0
loc 35
rs 8.7377
c 0
b 0
f 0
1
<?php
2
/**
3
 * Admin Menu file.
4
 *
5
 * @package automattic/jetpack
6
 */
7
8
namespace Automattic\Jetpack;
9
10
/**
11
 * Class Admin_Menu.
12
 */
13
class Admin_Menu extends Base_Admin_Menu {
14
	/**
15
	 * Create the desired menu output.
16
	 */
17
	public function reregister_menu_items() {
18
		// Constant is not defined until parse_request.
19
		if ( ! $this->is_api_request ) {
20
			$this->is_api_request = defined( 'REST_REQUEST' ) && REST_REQUEST;
21
		}
22
23
		/*
24
		 * Whether links should point to Calypso or wp-admin.
25
		 *
26
		 * Options:
27
		 * false - Calypso (Default).
28
		 * true  - wp-admin.
29
		 */
30
		$wp_admin = $this->should_link_to_wp_admin();
31
32
		// Remove separators.
33
		remove_menu_page( 'separator1' );
34
35
		$this->add_stats_menu();
36
		$this->add_upgrades_menu();
37
		$this->add_posts_menu( $wp_admin );
38
		$this->add_media_menu( $wp_admin );
39
		$this->add_page_menu( $wp_admin );
40
		$this->add_testimonials_menu( $wp_admin );
41
		$this->add_portfolio_menu( $wp_admin );
42
		$this->add_comments_menu( $wp_admin );
43
44
		// Whether Themes/Customize links should point to Calypso (false) or wp-admin (true).
45
		$wp_admin_themes    = $wp_admin;
46
		$wp_admin_customize = $wp_admin;
47
		$this->add_appearance_menu( $wp_admin_themes, $wp_admin_customize );
48
		$this->add_plugins_menu( $wp_admin );
49
		$this->add_users_menu( $wp_admin );
50
51
		// Whether Import/Export links should point to Calypso (false) or wp-admin (true).
52
		$wp_admin_import = $wp_admin;
53
		$wp_admin_export = $wp_admin;
54
		$this->add_tools_menu( $wp_admin_import, $wp_admin_export );
55
56
		$this->add_options_menu( $wp_admin );
57
		$this->add_jetpack_menu();
58
		$this->add_gutenberg_menus( $wp_admin );
59
60
		// Remove Links Manager menu since its usage is discouraged. https://github.com/Automattic/wp-calypso/issues/51188.
61
		// @see https://core.trac.wordpress.org/ticket/21307#comment:73.
62
		if ( $this->should_disable_links_manager() ) {
63
			remove_menu_page( 'link-manager.php' );
64
		}
65
66
		ksort( $GLOBALS['menu'] );
67
	}
68
69
	/**
70
	 * Check if Links Manager is being used.
71
	 */
72
	public function should_disable_links_manager() {
73
		// The max ID number of the auto-generated links.
74
		// See /wp-content/mu-plugins/wpcom-wp-install-defaults.php in WP.com.
75
		$max_default_id = 10;
76
77
		// We are only checking the latest entry link_id so are limiting the query to 1.
78
		$link_manager_links = get_bookmarks(
79
			array(
80
				'orderby'        => 'link_id',
81
				'order'          => 'DESC',
82
				'limit'          => 1,
83
				'hide_invisible' => 0,
84
			)
85
		);
86
87
		// Ordered links by ID descending, check if the first ID is more than $max_default_id.
88
		if ( count( $link_manager_links ) > 0 && $link_manager_links[0]->link_id > $max_default_id ) {
89
			return false;
90
		}
91
92
		return true;
93
	}
94
95
	/**
96
	 * Adds My Home menu.
97
	 */
98
	public function add_my_home_menu() {
99
		$this->update_menu( 'index.php', 'https://wordpress.com/home/' . $this->domain, __( 'My Home', 'jetpack' ), 'manage_options', 'dashicons-admin-home' );
100
	}
101
102
	/**
103
	 * Adds Stats menu.
104
	 */
105
	public function add_stats_menu() {
106
		add_menu_page( __( 'Stats', 'jetpack' ), __( 'Stats', 'jetpack' ), 'view_stats', 'https://wordpress.com/stats/day/' . $this->domain, null, 'dashicons-chart-bar', 3 );
107
	}
108
109
	/**
110
	 * Adds Upgrades menu.
111
	 *
112
	 * @param string $plan The current WPCOM plan of the blog.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $plan not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
113
	 */
114
	public function add_upgrades_menu( $plan = null ) {
115
		global $menu;
116
117
		$menu_exists = false;
118
		foreach ( $menu as $item ) {
119
			if ( 'paid-upgrades.php' === $item[2] ) {
120
				$menu_exists = true;
121
				break;
122
			}
123
		}
124
125
		if ( ! $menu_exists ) {
126
			if ( $plan ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $plan of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
127
				// Add display:none as a default for cases when CSS is not loaded.
128
				$site_upgrades = '%1$s<span class="inline-text" style="display:none">%2$s</span>';
129
				$site_upgrades = sprintf(
130
					$site_upgrades,
131
					__( 'Upgrades', 'jetpack' ),
132
					$plan
133
				);
134
			} else {
135
				$site_upgrades = __( 'Upgrades', 'jetpack' );
136
			}
137
138
			add_menu_page( __( 'Upgrades', 'jetpack' ), $site_upgrades, 'manage_options', 'paid-upgrades.php', null, 'dashicons-cart', 4 );
139
		}
140
141
		add_submenu_page( 'paid-upgrades.php', __( 'Plans', 'jetpack' ), __( 'Plans', 'jetpack' ), 'manage_options', 'https://wordpress.com/plans/my-plan/' . $this->domain, null, 1 );
142
		add_submenu_page( 'paid-upgrades.php', __( 'Purchases', 'jetpack' ), __( 'Purchases', 'jetpack' ), 'manage_options', 'https://wordpress.com/purchases/subscriptions/' . $this->domain, null, 2 );
143
144
		if ( ! $menu_exists ) {
145
			// Remove the submenu auto-created by Core.
146
			$this->hide_submenu_page( 'paid-upgrades.php', 'paid-upgrades.php' );
147
		}
148
	}
149
150
	/**
151
	 * Adds Posts menu.
152
	 *
153
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
154
	 */
155
	public function add_posts_menu( $wp_admin = false ) {
156
		if ( $wp_admin ) {
157
			return;
158
		}
159
160
		$submenus_to_update = array(
161
			'edit.php'                        => 'https://wordpress.com/posts/' . $this->domain,
162
			'post-new.php'                    => 'https://wordpress.com/post/' . $this->domain,
163
			'edit-tags.php?taxonomy=category' => 'https://wordpress.com/settings/taxonomies/category/' . $this->domain,
164
			'edit-tags.php?taxonomy=post_tag' => 'https://wordpress.com/settings/taxonomies/post_tag/' . $this->domain,
165
		);
166
		$this->update_submenus( 'edit.php', $submenus_to_update );
167
	}
168
169
	/**
170
	 * Adds Media menu.
171
	 *
172
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
173
	 */
174
	public function add_media_menu( $wp_admin = false ) {
175
		if ( $wp_admin ) {
176
			return;
177
		}
178
179
		$this->hide_submenu_page( 'upload.php', 'media-new.php' );
180
181
		$this->update_menu( 'upload.php', 'https://wordpress.com/media/' . $this->domain );
182
	}
183
184
	/**
185
	 * Adds Page menu.
186
	 *
187
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
188
	 */
189
	public function add_page_menu( $wp_admin = false ) {
190
		if ( $wp_admin ) {
191
			return;
192
		}
193
194
		$submenus_to_update = array(
195
			'edit.php?post_type=page'     => 'https://wordpress.com/pages/' . $this->domain,
196
			'post-new.php?post_type=page' => 'https://wordpress.com/page/' . $this->domain,
197
		);
198
		$this->update_submenus( 'edit.php?post_type=page', $submenus_to_update );
199
	}
200
201
	/**
202
	 * Adds Testimonials menu.
203
	 *
204
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
205
	 */
206
	public function add_testimonials_menu( $wp_admin = false ) {
207
		$this->add_custom_post_type_menu( 'jetpack-testimonial', $wp_admin );
208
	}
209
210
	/**
211
	 * Adds Portfolio menu.
212
	 *
213
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
214
	 */
215
	public function add_portfolio_menu( $wp_admin = false ) {
216
		$this->add_custom_post_type_menu( 'jetpack-portfolio', $wp_admin );
217
	}
218
219
	/**
220
	 * Adds a custom post type menu.
221
	 *
222
	 * @param string $post_type Custom post type.
223
	 * @param bool   $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
224
	 */
225
	public function add_custom_post_type_menu( $post_type, $wp_admin = false ) {
226
		if ( $wp_admin ) {
227
			return;
228
		}
229
230
		$submenus_to_update = array(
231
			'edit.php?post_type=' . $post_type     => 'https://wordpress.com/types/' . $post_type . '/' . $this->domain,
232
			'post-new.php?post_type=' . $post_type => 'https://wordpress.com/edit/' . $post_type . '/' . $this->domain,
233
		);
234
		$this->update_submenus( 'edit.php?post_type=' . $post_type, $submenus_to_update );
235
	}
236
237
	/**
238
	 * Adds Comments menu.
239
	 *
240
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
241
	 */
242
	public function add_comments_menu( $wp_admin = false ) {
243
		if ( $wp_admin ) {
244
			return;
245
		}
246
247
		$this->update_menu( 'edit-comments.php', 'https://wordpress.com/comments/all/' . $this->domain );
248
	}
249
250
	/**
251
	 * Adds Appearance menu.
252
	 *
253
	 * @param bool $wp_admin_themes Optional. Whether Themes link should point to Calypso or wp-admin. Default false (Calypso).
254
	 * @param bool $wp_admin_customize Optional. Whether Customize link should point to Calypso or wp-admin. Default false (Calypso).
255
	 * @return string The Customizer URL.
256
	 */
257
	public function add_appearance_menu( $wp_admin_themes = false, $wp_admin_customize = false ) {
258
		$request_uri                     = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
259
		$default_customize_slug          = add_query_arg( 'return', rawurlencode( remove_query_arg( wp_removable_query_args(), $request_uri ) ), 'customize.php' );
260
		$default_customize_header_slug_1 = add_query_arg( array( 'autofocus' => array( 'control' => 'header_image' ) ), $default_customize_slug );
261
		// TODO: Remove WPCom_Theme_Customizer::modify_header_menu_links() and WPcom_Custom_Header::modify_admin_menu_links().
262
		$default_customize_header_slug_2     = admin_url( 'themes.php?page=custom-header' );
263
		$default_customize_background_slug_1 = add_query_arg( array( 'autofocus' => array( 'control' => 'background_image' ) ), $default_customize_slug );
264
		// TODO: Remove Colors_Manager::modify_header_menu_links() and Colors_Manager_Common::modify_header_menu_links().
265
		$default_customize_background_slug_2 = add_query_arg( array( 'autofocus' => array( 'section' => 'colors_manager_tool' ) ), admin_url( 'customize.php' ) );
266
267
		if ( ! $wp_admin_customize ) {
268
			$customize_url = 'https://wordpress.com/customize/' . $this->domain;
269
		} elseif ( $this->is_api_request ) {
270
			// In case this is an api request we will have to add the 'return' querystring via JS.
271
			$customize_url = 'customize.php';
272
		} else {
273
			$customize_url = $default_customize_slug;
274
		}
275
276
		$submenus_to_update = array(
277
			$default_customize_slug              => $customize_url,
278
			$default_customize_header_slug_1     => add_query_arg( array( 'autofocus' => array( 'control' => 'header_image' ) ), $customize_url ),
279
			$default_customize_header_slug_2     => add_query_arg( array( 'autofocus' => array( 'control' => 'header_image' ) ), $customize_url ),
280
			$default_customize_background_slug_1 => add_query_arg( array( 'autofocus' => array( 'section' => 'colors_manager_tool' ) ), $customize_url ),
281
			$default_customize_background_slug_2 => add_query_arg( array( 'autofocus' => array( 'section' => 'colors_manager_tool' ) ), $customize_url ),
282
		);
283
284
		if ( ! $wp_admin_themes ) {
285
			$submenus_to_update['themes.php'] = 'https://wordpress.com/themes/' . $this->domain;
286
		}
287
288
		if ( ! $wp_admin_customize ) {
289
			$submenus_to_update['widgets.php']       = add_query_arg( array( 'autofocus' => array( 'panel' => 'widgets' ) ), $customize_url );
290
			$submenus_to_update['gutenberg-widgets'] = add_query_arg( array( 'autofocus' => array( 'panel' => 'widgets' ) ), $customize_url );
291
			$submenus_to_update['nav-menus.php']     = add_query_arg( array( 'autofocus' => array( 'panel' => 'nav_menus' ) ), $customize_url );
292
		}
293
294
		$this->update_submenus( 'themes.php', $submenus_to_update );
295
296
		$this->hide_submenu_page( 'themes.php', 'custom-header' );
297
		$this->hide_submenu_page( 'themes.php', 'custom-background' );
298
299
		return $customize_url;
300
	}
301
302
	/**
303
	 * Adds Plugins menu.
304
	 *
305
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
306
	 */
307
	public function add_plugins_menu( $wp_admin = false ) {
308
		if ( $wp_admin ) {
309
			return;
310
		}
311
312
		$this->hide_submenu_page( 'plugins.php', 'plugin-install.php' );
313
		$this->hide_submenu_page( 'plugins.php', 'plugin-editor.php' );
314
315
		$this->update_menu( 'plugins.php', 'https://wordpress.com/plugins/' . $this->domain );
316
	}
317
318
	/**
319
	 * Adds Users menu.
320
	 *
321
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
322
	 */
323
	public function add_users_menu( $wp_admin = false ) {
324
		if ( current_user_can( 'list_users' ) ) {
325
			// We shall add the Calypso user management & add new user screens at all cases ( Calypso & Atomic ).
326
			$submenus_to_update = array(
327
				'user-new.php' => 'https://wordpress.com/people/new/' . $this->domain,
328
				'users.php'    => 'https://wordpress.com/people/team/' . $this->domain,
329
			);
330
			if ( ! $wp_admin ) {
331
				$submenus_to_update['profile.php'] = 'https://wordpress.com/me';
332
			}
333
			$this->update_submenus( 'users.php', $submenus_to_update );
334
			add_submenu_page( 'users.php', esc_attr__( 'Account Settings', 'jetpack' ), __( 'Account Settings', 'jetpack' ), 'read', 'https://wordpress.com/me/account' );
335
		} else {
336
			if ( ! $wp_admin ) {
337
				$submenus_to_update = array(
338
					'user-new.php' => 'https://wordpress.com/people/new/' . $this->domain,
339
					'profile.php'  => 'https://wordpress.com/me',
340
				);
341
				$this->update_submenus( 'profile.php', $submenus_to_update );
342
			}
343
344
			add_submenu_page( 'profile.php', esc_attr__( 'Account Settings', 'jetpack' ), __( 'Account Settings', 'jetpack' ), 'read', 'https://wordpress.com/me/account' );
345
		}
346
	}
347
348
	/**
349
	 * Adds Tools menu.
350
	 *
351
	 * @param bool $wp_admin_import Optional. Whether Import link should point to Calypso or wp-admin. Default false (Calypso).
352
	 * @param bool $wp_admin_export Optional. Whether Export link should point to Calypso or wp-admin. Default false (Calypso).
353
	 */
354
	public function add_tools_menu( $wp_admin_import = false, $wp_admin_export = false ) {
355
		$submenus_to_update = array();
356
		if ( ! $wp_admin_import ) {
357
			$submenus_to_update['import.php'] = 'https://wordpress.com/import/' . $this->domain;
358
		}
359
		if ( ! $wp_admin_export ) {
360
			$submenus_to_update['export.php'] = 'https://wordpress.com/export/' . $this->domain;
361
		}
362
		$this->update_submenus( 'tools.php', $submenus_to_update );
363
364
		$this->hide_submenu_page( 'tools.php', 'tools.php' );
365
		$this->hide_submenu_page( 'tools.php', 'delete-blog' );
366
367
		add_submenu_page( 'tools.php', esc_attr__( 'Marketing', 'jetpack' ), __( 'Marketing', 'jetpack' ), 'publish_posts', 'https://wordpress.com/marketing/tools/' . $this->domain, null, 0 );
368
		add_submenu_page( 'tools.php', esc_attr__( 'Earn', 'jetpack' ), __( 'Earn', 'jetpack' ), 'manage_options', 'https://wordpress.com/earn/' . $this->domain, null, 1 );
369
	}
370
371
	/**
372
	 * Adds Settings menu.
373
	 *
374
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
375
	 */
376
	public function add_options_menu( $wp_admin = false ) {
377
		$this->hide_submenu_page( 'options-general.php', 'sharing' );
378
379
		if ( $wp_admin ) {
380
			return;
381
		}
382
383
		$this->update_submenus( 'options-general.php', array( 'options-general.php' => 'https://wordpress.com/settings/general/' . $this->domain ) );
384
385
		$this->hide_submenu_page( 'options-general.php', 'options-discussion.php' );
386
		$this->hide_submenu_page( 'options-general.php', 'options-writing.php' );
387
	}
388
389
	/**
390
	 * Adds Jetpack menu.
391
	 */
392
	public function add_jetpack_menu() {
393
		$this->add_admin_menu_separator( 50, 'manage_options' );
394
395
		// TODO: Replace with proper SVG data url.
396
		$icon = "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='20' height='20' viewBox='0 0 40 40' %3E%3Cpath fill='%23a0a5aa' d='M20 0c11.046 0 20 8.954 20 20s-8.954 20-20 20S0 31.046 0 20 8.954 0 20 0zm11 17H21v19l10-19zM19 4L9 23h10V4z'/%3E%3C/svg%3E";
397
398
		$is_menu_updated = $this->update_menu( 'jetpack', null, null, null, $icon, 51 );
399
		if ( ! $is_menu_updated ) {
400
			add_menu_page( esc_attr__( 'Jetpack', 'jetpack' ), __( 'Jetpack', 'jetpack' ), 'manage_options', 'jetpack', null, $icon, 51 );
401
		}
402
403
		add_submenu_page( 'jetpack', esc_attr__( 'Activity Log', 'jetpack' ), __( 'Activity Log', 'jetpack' ), 'manage_options', 'https://wordpress.com/activity-log/' . $this->domain, null, 2 );
404
		add_submenu_page( 'jetpack', esc_attr__( 'Backup', 'jetpack' ), __( 'Backup', 'jetpack' ), 'manage_options', 'https://wordpress.com/backup/' . $this->domain, null, 3 );
405
		/* translators: Jetpack sidebar menu item. */
406
		add_submenu_page( 'jetpack', esc_attr__( 'Search', 'jetpack' ), __( 'Search', 'jetpack' ), 'read', 'https://wordpress.com/jetpack-search/' . $this->domain, null, 4 );
407
408
		$this->hide_submenu_page( 'jetpack', 'stats' );
409
		$this->hide_submenu_page( 'jetpack', esc_url( Redirect::get_url( 'calypso-backups' ) ) );
410
		$this->hide_submenu_page( 'jetpack', esc_url( Redirect::get_url( 'calypso-scanner' ) ) );
411
412
		if ( ! $is_menu_updated ) {
413
			// Remove the submenu auto-created by Core just to be sure that there no issues on non-admin roles.
414
			remove_submenu_page( 'jetpack', 'jetpack' );
415
		}
416
	}
417
418
	/**
419
	 * Re-adds the Site Editor menu without the (beta) tag, and where we want it.
420
	 *
421
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
422
	 */
423
	public function add_gutenberg_menus( $wp_admin = false ) {
424
		// We can bail if we don't meet the conditions of the Site Editor.
425
		if ( ! ( function_exists( 'gutenberg_is_fse_theme' ) && gutenberg_is_fse_theme() ) ) {
426
			return;
427
		}
428
429
		// Core Gutenberg registers without an explicit position, and we don't want the (beta) tag.
430
		remove_menu_page( 'gutenberg-edit-site' );
431
		// Core Gutenberg tries to manage its position, foiling our best laid plans. Unfoil.
432
		remove_filter( 'menu_order', 'gutenberg_menu_order' );
433
434
		$link = $wp_admin ? 'gutenberg-edit-site' : 'https://wordpress.com/site-editor/' . $this->domain;
435
436
		add_menu_page(
437
			__( 'Site Editor', 'jetpack' ),
438
			__( 'Site Editor', 'jetpack' ),
439
			'edit_theme_options',
440
			$link,
441
			$wp_admin ? 'gutenberg_edit_site_page' : null,
442
			'dashicons-layout',
443
			61 // Just under Appearance.
444
		);
445
	}
446
447
	/**
448
	 * Whether to use wp-admin pages rather than Calypso.
449
	 *
450
	 * @return bool
451
	 */
452
	public function should_link_to_wp_admin() {
453
		return get_user_option( 'jetpack_admin_menu_link_destination' );
454
	}
455
}
456