Completed
Push — renovate/vercel-ncc-0.x ( fdb380...917c9e )
by
unknown
149:32 queued 140:15
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\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. https://github.com/Automattic/wp-calypso/issues/51188.
66
		// @see https://core.trac.wordpress.org/ticket/21307#comment:73.
67
		if ( $this->should_disable_links_manager() ) {
68
			remove_menu_page( 'link-manager.php' );
69
		}
70
71
		ksort( $GLOBALS['menu'] );
72
	}
73
74
	/**
75
	 * Check if Links Manager is being used.
76
	 */
77
	public function should_disable_links_manager() {
78
		// The max ID number of the auto-generated links.
79
		// See /wp-content/mu-plugins/wpcom-wp-install-defaults.php in WP.com.
80
		$max_default_id = 10;
81
82
		// We are only checking the latest entry link_id so are limiting the query to 1.
83
		$link_manager_links = get_bookmarks(
84
			array(
85
				'orderby'        => 'link_id',
86
				'order'          => 'DESC',
87
				'limit'          => 1,
88
				'hide_invisible' => 0,
89
			)
90
		);
91
92
		// Ordered links by ID descending, check if the first ID is more than $max_default_id.
93
		if ( count( $link_manager_links ) > 0 && $link_manager_links[0]->link_id > $max_default_id ) {
94
			return false;
95
		}
96
97
		return true;
98
	}
99
100
	/**
101
	 * Adds My Home menu.
102
	 */
103
	public function add_my_home_menu() {
104
		$this->update_menu( 'index.php', 'https://wordpress.com/home/' . $this->domain, __( 'My Home', 'jetpack' ), 'manage_options', 'dashicons-admin-home' );
105
	}
106
107
	/**
108
	 * Adds Stats menu.
109
	 */
110
	public function add_stats_menu() {
111
		add_menu_page( __( 'Stats', 'jetpack' ), __( 'Stats', 'jetpack' ), 'view_stats', 'https://wordpress.com/stats/day/' . $this->domain, null, 'dashicons-chart-bar', 3 );
112
	}
113
114
	/**
115
	 * Adds Upgrades menu.
116
	 *
117
	 * @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...
118
	 */
119
	public function add_upgrades_menu( $plan = null ) {
120
		global $menu;
121
122
		$menu_exists = false;
123
		foreach ( $menu as $item ) {
124
			if ( 'paid-upgrades.php' === $item[2] ) {
125
				$menu_exists = true;
126
				break;
127
			}
128
		}
129
130
		if ( ! $menu_exists ) {
131
			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...
132
				// Add display:none as a default for cases when CSS is not loaded.
133
				$site_upgrades = '%1$s<span class="inline-text" style="display:none">%2$s</span>';
134
				$site_upgrades = sprintf(
135
					$site_upgrades,
136
					__( 'Upgrades', 'jetpack' ),
137
					$plan
138
				);
139
			} else {
140
				$site_upgrades = __( 'Upgrades', 'jetpack' );
141
			}
142
143
			add_menu_page( __( 'Upgrades', 'jetpack' ), $site_upgrades, 'manage_options', 'paid-upgrades.php', null, 'dashicons-cart', 4 );
144
		}
145
146
		add_submenu_page( 'paid-upgrades.php', __( 'Plans', 'jetpack' ), __( 'Plans', 'jetpack' ), 'manage_options', 'https://wordpress.com/plans/my-plan/' . $this->domain, null, 5 );
147
		add_submenu_page( 'paid-upgrades.php', __( 'Purchases', 'jetpack' ), __( 'Purchases', 'jetpack' ), 'manage_options', 'https://wordpress.com/purchases/subscriptions/' . $this->domain, null, 15 );
148
149
		if ( ! $menu_exists ) {
150
			// Remove the submenu auto-created by Core.
151
			$this->hide_submenu_page( 'paid-upgrades.php', 'paid-upgrades.php' );
152
		}
153
	}
154
155
	/**
156
	 * Adds Posts menu.
157
	 *
158
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
159
	 */
160 View Code Duplication
	public function add_posts_menu( $wp_admin = false ) {
161
		if ( $wp_admin ) {
162
			return;
163
		}
164
165
		$submenus_to_update = array(
166
			'edit.php'     => 'https://wordpress.com/posts/' . $this->domain,
167
			'post-new.php' => 'https://wordpress.com/post/' . $this->domain,
168
		);
169
		$this->update_submenus( 'edit.php', $submenus_to_update );
170
	}
171
172
	/**
173
	 * Adds Media menu.
174
	 *
175
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
176
	 */
177
	public function add_media_menu( $wp_admin = false ) {
178
		if ( $wp_admin ) {
179
			return;
180
		}
181
182
		$this->hide_submenu_page( 'upload.php', 'media-new.php' );
183
184
		$this->update_menu( 'upload.php', 'https://wordpress.com/media/' . $this->domain );
185
	}
186
187
	/**
188
	 * Adds Page menu.
189
	 *
190
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
191
	 */
192 View Code Duplication
	public function add_page_menu( $wp_admin = false ) {
193
		if ( $wp_admin ) {
194
			return;
195
		}
196
197
		$submenus_to_update = array(
198
			'edit.php?post_type=page'     => 'https://wordpress.com/pages/' . $this->domain,
199
			'post-new.php?post_type=page' => 'https://wordpress.com/page/' . $this->domain,
200
		);
201
		$this->update_submenus( 'edit.php?post_type=page', $submenus_to_update );
202
	}
203
204
	/**
205
	 * Adds Testimonials menu.
206
	 *
207
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
208
	 */
209
	public function add_testimonials_menu( $wp_admin = false ) {
210
		$this->add_custom_post_type_menu( 'jetpack-testimonial', $wp_admin );
211
	}
212
213
	/**
214
	 * Adds Portfolio menu.
215
	 *
216
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
217
	 */
218
	public function add_portfolio_menu( $wp_admin = false ) {
219
		$this->add_custom_post_type_menu( 'jetpack-portfolio', $wp_admin );
220
	}
221
222
	/**
223
	 * Adds a custom post type menu.
224
	 *
225
	 * @param string $post_type Custom post type.
226
	 * @param bool   $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
227
	 */
228
	public function add_custom_post_type_menu( $post_type, $wp_admin = false ) {
229
		if ( $wp_admin ) {
230
			return;
231
		}
232
233
		$submenus_to_update = array(
234
			'edit.php?post_type=' . $post_type     => 'https://wordpress.com/types/' . $post_type . '/' . $this->domain,
235
			'post-new.php?post_type=' . $post_type => 'https://wordpress.com/edit/' . $post_type . '/' . $this->domain,
236
		);
237
		$this->update_submenus( 'edit.php?post_type=' . $post_type, $submenus_to_update );
238
	}
239
240
	/**
241
	 * Adds Comments menu.
242
	 *
243
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
244
	 */
245
	public function add_comments_menu( $wp_admin = false ) {
246
		if ( $wp_admin ) {
247
			return;
248
		}
249
250
		$this->update_menu( 'edit-comments.php', 'https://wordpress.com/comments/all/' . $this->domain );
251
	}
252
253
	/**
254
	 * Adds Appearance menu.
255
	 *
256
	 * @param bool $wp_admin_themes Optional. Whether Themes link should point to Calypso or wp-admin. Default false (Calypso).
257
	 * @param bool $wp_admin_customize Optional. Whether Customize link should point to Calypso or wp-admin. Default false (Calypso).
258
	 * @return string The Customizer URL.
259
	 */
260
	public function add_appearance_menu( $wp_admin_themes = false, $wp_admin_customize = false ) {
261
		$request_uri                     = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
262
		$default_customize_slug          = add_query_arg( 'return', rawurlencode( remove_query_arg( wp_removable_query_args(), $request_uri ) ), 'customize.php' );
263
		$default_customize_header_slug_1 = add_query_arg( array( 'autofocus' => array( 'control' => 'header_image' ) ), $default_customize_slug );
264
		// TODO: Remove WPCom_Theme_Customizer::modify_header_menu_links() and WPcom_Custom_Header::modify_admin_menu_links().
265
		$default_customize_header_slug_2     = admin_url( 'themes.php?page=custom-header' );
266
		$default_customize_background_slug_1 = add_query_arg( array( 'autofocus' => array( 'control' => 'background_image' ) ), $default_customize_slug );
267
		// TODO: Remove Colors_Manager::modify_header_menu_links() and Colors_Manager_Common::modify_header_menu_links().
268
		$default_customize_background_slug_2 = add_query_arg( array( 'autofocus' => array( 'section' => 'colors_manager_tool' ) ), admin_url( 'customize.php' ) );
269
270
		if ( ! $wp_admin_customize ) {
271
			$customize_url = 'https://wordpress.com/customize/' . $this->domain;
272
		} elseif ( $this->is_api_request ) {
273
			// In case this is an api request we will have to add the 'return' querystring via JS.
274
			$customize_url = 'customize.php';
275
		} else {
276
			$customize_url = $default_customize_slug;
277
		}
278
279
		$submenus_to_update = array(
280
			$default_customize_slug              => $customize_url,
281
			$default_customize_header_slug_1     => add_query_arg( array( 'autofocus' => array( 'control' => 'header_image' ) ), $customize_url ),
282
			$default_customize_header_slug_2     => add_query_arg( array( 'autofocus' => array( 'control' => 'header_image' ) ), $customize_url ),
283
			$default_customize_background_slug_1 => add_query_arg( array( 'autofocus' => array( 'section' => 'colors_manager_tool' ) ), $customize_url ),
284
			$default_customize_background_slug_2 => add_query_arg( array( 'autofocus' => array( 'section' => 'colors_manager_tool' ) ), $customize_url ),
285
		);
286
287
		if ( ! $wp_admin_themes ) {
288
			$submenus_to_update['themes.php'] = 'https://wordpress.com/themes/' . $this->domain;
289
		}
290
291
		if ( ! $wp_admin_customize ) {
292
			$submenus_to_update['widgets.php']       = add_query_arg( array( 'autofocus' => array( 'panel' => 'widgets' ) ), $customize_url );
293
			$submenus_to_update['gutenberg-widgets'] = add_query_arg( array( 'autofocus' => array( 'panel' => 'widgets' ) ), $customize_url );
294
			$submenus_to_update['nav-menus.php']     = add_query_arg( array( 'autofocus' => array( 'panel' => 'nav_menus' ) ), $customize_url );
295
		}
296
297
		$this->update_submenus( 'themes.php', $submenus_to_update );
298
299
		$this->hide_submenu_page( 'themes.php', 'custom-header' );
300
		$this->hide_submenu_page( 'themes.php', 'custom-background' );
301
302
		return $customize_url;
303
	}
304
305
	/**
306
	 * Adds Plugins menu.
307
	 *
308
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
309
	 */
310
	public function add_plugins_menu( $wp_admin = false ) {
311
		if ( $wp_admin ) {
312
			return;
313
		}
314
315
		$this->hide_submenu_page( 'plugins.php', 'plugin-install.php' );
316
		$this->hide_submenu_page( 'plugins.php', 'plugin-editor.php' );
317
318
		$this->update_menu( 'plugins.php', 'https://wordpress.com/plugins/' . $this->domain );
319
	}
320
321
	/**
322
	 * Adds Users menu.
323
	 *
324
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
325
	 */
326
	public function add_users_menu( $wp_admin = false ) {
327
		if ( current_user_can( 'list_users' ) ) {
328
			// We shall add the Calypso user management & add new user screens at all cases ( Calypso & Atomic ).
329
			$submenus_to_update = array(
330
				'user-new.php' => 'https://wordpress.com/people/new/' . $this->domain,
331
				'users.php'    => 'https://wordpress.com/people/team/' . $this->domain,
332
			);
333
			if ( ! $wp_admin ) {
334
				$submenus_to_update['profile.php'] = 'https://wordpress.com/me';
335
			}
336
			$this->update_submenus( 'users.php', $submenus_to_update );
337
			add_submenu_page( 'users.php', esc_attr__( 'Account Settings', 'jetpack' ), __( 'Account Settings', 'jetpack' ), 'read', 'https://wordpress.com/me/account' );
338
		} else {
339
			if ( ! $wp_admin ) {
340
				$submenus_to_update = array(
341
					'user-new.php' => 'https://wordpress.com/people/new/' . $this->domain,
342
					'profile.php'  => 'https://wordpress.com/me',
343
				);
344
				$this->update_submenus( 'profile.php', $submenus_to_update );
345
			}
346
347
			add_submenu_page( 'profile.php', esc_attr__( 'Account Settings', 'jetpack' ), __( 'Account Settings', 'jetpack' ), 'read', 'https://wordpress.com/me/account' );
348
		}
349
	}
350
351
	/**
352
	 * Adds Tools menu.
353
	 *
354
	 * @param bool $wp_admin_import Optional. Whether Import link should point to Calypso or wp-admin. Default false (Calypso).
355
	 * @param bool $wp_admin_export Optional. Whether Export link should point to Calypso or wp-admin. Default false (Calypso).
356
	 */
357
	public function add_tools_menu( $wp_admin_import = false, $wp_admin_export = false ) {
358
		$submenus_to_update = array();
359
		if ( ! $wp_admin_import ) {
360
			$submenus_to_update['import.php'] = 'https://wordpress.com/import/' . $this->domain;
361
		}
362
		if ( ! $wp_admin_export ) {
363
			$submenus_to_update['export.php'] = 'https://wordpress.com/export/' . $this->domain;
364
		}
365
		$this->update_submenus( 'tools.php', $submenus_to_update );
366
367
		$this->hide_submenu_page( 'tools.php', 'tools.php' );
368
		$this->hide_submenu_page( 'tools.php', 'delete-blog' );
369
370
		add_submenu_page( 'tools.php', esc_attr__( 'Marketing', 'jetpack' ), __( 'Marketing', 'jetpack' ), 'publish_posts', 'https://wordpress.com/marketing/tools/' . $this->domain, null, 0 );
371
		add_submenu_page( 'tools.php', esc_attr__( 'Earn', 'jetpack' ), __( 'Earn', 'jetpack' ), 'manage_options', 'https://wordpress.com/earn/' . $this->domain, null, 1 );
372
	}
373
374
	/**
375
	 * Adds Settings menu.
376
	 *
377
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
378
	 */
379
	public function add_options_menu( $wp_admin = false ) {
380
		if ( $wp_admin ) {
381
			return;
382
		}
383
384
		$this->update_submenus( 'options-general.php', array( 'options-general.php' => 'https://wordpress.com/settings/general/' . $this->domain ) );
385
386
		$this->hide_submenu_page( 'options-general.php', 'options-discussion.php' );
387
		$this->hide_submenu_page( 'options-general.php', 'options-writing.php' );
388
	}
389
390
	/**
391
	 * Adds Jetpack menu.
392
	 */
393
	public function add_jetpack_menu() {
394
		$this->add_admin_menu_separator( 50, 'manage_options' );
395
396
		// TODO: Replace with proper SVG data url.
397
		$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";
398
399
		$is_menu_updated = $this->update_menu( 'jetpack', null, null, null, $icon, 51 );
400
		if ( ! $is_menu_updated ) {
401
			add_menu_page( esc_attr__( 'Jetpack', 'jetpack' ), __( 'Jetpack', 'jetpack' ), 'manage_options', 'jetpack', null, $icon, 51 );
402
		}
403
404
		add_submenu_page( 'jetpack', esc_attr__( 'Activity Log', 'jetpack' ), __( 'Activity Log', 'jetpack' ), 'manage_options', 'https://wordpress.com/activity-log/' . $this->domain, null, 2 );
405
		add_submenu_page( 'jetpack', esc_attr__( 'Backup', 'jetpack' ), __( 'Backup', 'jetpack' ), 'manage_options', 'https://wordpress.com/backup/' . $this->domain, null, 3 );
406
		/* translators: Jetpack sidebar menu item. */
407
		add_submenu_page( 'jetpack', esc_attr__( 'Search', 'jetpack' ), __( 'Search', 'jetpack' ), 'read', 'https://wordpress.com/jetpack-search/' . $this->domain, null, 4 );
408
409
		$this->hide_submenu_page( 'jetpack', 'stats' );
410
		$this->hide_submenu_page( 'jetpack', esc_url( Redirect::get_url( 'calypso-backups' ) ) );
411
		$this->hide_submenu_page( 'jetpack', esc_url( Redirect::get_url( 'calypso-scanner' ) ) );
412
413
		if ( ! $is_menu_updated ) {
414
			// Remove the submenu auto-created by Core.
415
			$this->hide_submenu_page( 'jetpack', 'jetpack' );
416
		}
417
	}
418
419
	/**
420
	 * Re-adds the Site Editor menu without the (beta) tag, and where we want it.
421
	 *
422
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
423
	 */
424
	public function add_gutenberg_menus( $wp_admin = false ) {
425
		// We can bail if we don't meet the conditions of the Site Editor.
426
		if ( ! ( function_exists( 'gutenberg_is_fse_theme' ) && gutenberg_is_fse_theme() ) ) {
427
			return;
428
		}
429
430
		// Core Gutenberg registers without an explicit position, and we don't want the (beta) tag.
431
		remove_menu_page( 'gutenberg-edit-site' );
432
		// Core Gutenberg tries to manage its position, foiling our best laid plans. Unfoil.
433
		remove_filter( 'menu_order', 'gutenberg_menu_order' );
434
435
		$link = $wp_admin ? 'gutenberg-edit-site' : 'https://wordpress.com/site-editor/' . $this->domain;
436
437
		add_menu_page(
438
			__( 'Site Editor', 'jetpack' ),
439
			__( 'Site Editor', 'jetpack' ),
440
			'edit_theme_options',
441
			$link,
442
			$wp_admin ? 'gutenberg_edit_site_page' : null,
443
			'dashicons-layout',
444
			61 // Just under Appearance.
445
		);
446
	}
447
448
	/**
449
	 * Whether to use wp-admin pages rather than Calypso.
450
	 *
451
	 * @return bool
452
	 */
453
	public function should_link_to_wp_admin() {
454
		return get_user_option( 'jetpack_admin_menu_link_destination' );
455
	}
456
}
457