Completed
Push — renovate/cross-env-7.x ( 99ee17...78eac3 )
by
unknown
330:11 queued 315:29
created

Admin_Menu::add_users_menu()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 8
nop 1
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
1
<?php
2
/**
3
 * Admin Menu file.
4
 *
5
 * @package Jetpack
6
 */
7
8
namespace Automattic\Jetpack\Dashboard_Customizations;
9
10
use Automattic\Jetpack\Status;
11
12
/**
13
 * Class Admin_Menu.
14
 */
15
class Admin_Menu {
16
	/**
17
	 * Holds class instances.
18
	 *
19
	 * @var array
20
	 */
21
	protected static $instances;
22
23
	/**
24
	 * Whether the current request is a REST API request.
25
	 *
26
	 * @var bool
27
	 */
28
	protected $is_api_request = false;
29
30
	/**
31
	 * Domain of the current site.
32
	 *
33
	 * @var string
34
	 */
35
	protected $domain;
36
37
	/**
38
	 * Admin_Menu constructor.
39
	 */
40
	protected function __construct() {
41
		add_action( 'admin_menu', array( $this, 'reregister_menu_items' ), 99999 );
42
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
43
		add_action( 'wp_enqueue_scripts', array( $this, 'dequeue_scripts' ), 20 );
44
		add_action( 'admin_enqueue_scripts', array( $this, 'dequeue_scripts' ), 20 );
45
		add_action( 'rest_request_before_callbacks', array( $this, 'rest_api_init' ), 11 );
46
47
		$this->domain = ( new Status() )->get_site_suffix();
48
	}
49
50
	/**
51
	 * Returns class instance.
52
	 *
53
	 * @return Admin_Menu
54
	 */
55
	public static function get_instance() {
56
		$class = get_called_class();
57
58
		if ( empty( static::$instances[ $class ] ) ) {
59
			static::$instances[ $class ] = new $class();
60
		}
61
62
		return static::$instances[ $class ];
63
	}
64
65
	/**
66
	 * Sets up class properties for REST API requests.
67
	 */
68
	public function rest_api_init() {
69
		$this->is_api_request = true;
70
	}
71
72
	/**
73
	 * Create the desired menu output.
74
	 */
75
	public function reregister_menu_items() {
76
		// Constant is not defined until parse_request.
77
		if ( ! $this->is_api_request ) {
78
			$this->is_api_request = defined( 'REST_REQUEST' ) && REST_REQUEST;
79
		}
80
81
		/**
82
		 * Whether links should point to Calypso or wp-admin.
83
		 *
84
		 * Options:
85
		 * true  - Calypso.
86
		 * false - wp-admin.
87
		 *
88
		 * @module masterbar
89
		 * @since 9.3.0
90
		 *
91
		 * @param bool $calypso Whether menu item URLs should point to Calypso.
92
		 */
93
		$calypso = apply_filters( 'jetpack_admin_menu_use_calypso_links', true );
94
95
		// Remove separators.
96
		remove_menu_page( 'separator1' );
97
98
		$this->add_my_home_menu( $calypso );
99
		$this->add_stats_menu();
100
		$this->add_upgrades_menu();
101
		$this->add_posts_menu( $calypso );
102
		$this->add_media_menu( $calypso );
103
		$this->add_page_menu( $calypso );
104
		$this->add_comments_menu( $calypso );
105
		$this->add_appearance_menu( $calypso );
106
		$this->add_plugins_menu();
107
		$this->add_users_menu( $calypso );
108
		$this->add_tools_menu( $calypso );
109
		$this->add_options_menu();
110
111
		ksort( $GLOBALS['menu'] );
112
	}
113
114
	/**
115
	 * Adds My Home menu.
116
	 *
117
	 * @param bool $calypso Optional. Whether links should point to Calypso or wp-admin. Default true (Calypso).
118
	 */
119
	public function add_my_home_menu( $calypso = true ) {
120
		global $submenu;
121
122
		$menu_slug = $calypso ? 'https://wordpress.com/home/' . $this->domain : 'index.php';
123
124
		remove_menu_page( 'index.php' );
125
		remove_submenu_page( 'index.php', 'index.php' );
126
127
		add_menu_page( __( 'My Home', 'jetpack' ), __( 'My Home', 'jetpack' ), 'read', $menu_slug, null, 'dashicons-admin-home', 2 );
128
129
		// Only add submenu when there are other submenu items.
130
		if ( ! empty( $submenu['index.php'] ) ) {
131
			add_submenu_page( $menu_slug, __( 'My Home', 'jetpack' ), __( 'My Home', 'jetpack' ), 'read', $menu_slug, null, 1 );
132
		}
133
134
		$this->migrate_submenus( 'index.php', $menu_slug );
135
	}
136
137
	/**
138
	 * Adds Stats menu.
139
	 */
140
	public function add_stats_menu() {
141
		add_menu_page( __( 'Stats', 'jetpack' ), __( 'Stats', 'jetpack' ), 'edit_posts', 'https://wordpress.com/stats/day/' . $this->domain, null, 'dashicons-chart-bar', 3 );
142
	}
143
144
	/**
145
	 * Adds Upgrades menu.
146
	 */
147
	public function add_upgrades_menu() {
148
		remove_menu_page( 'paid-upgrades.php' );
149
150
		$menu_slug = 'https://wordpress.com/plans/' . $this->domain;
151
152
		add_menu_page( __( 'Upgrades', 'jetpack' ), __( 'Upgrades', 'jetpack' ), 'manage_options', $menu_slug, null, 'dashicons-cart', 4 );
153
		add_submenu_page( $menu_slug, __( 'Plans', 'jetpack' ), __( 'Plans', 'jetpack' ), 'manage_options', $menu_slug, null, 5 );
154
		add_submenu_page( $menu_slug, __( 'Purchases', 'jetpack' ), __( 'Purchases', 'jetpack' ), 'manage_options', 'https://wordpress.com/purchases/subscriptions/' . $this->domain, null, 15 );
155
156
		$this->migrate_submenus( 'paid-upgrades.php', $menu_slug );
157
	}
158
159
	/**
160
	 * Adds Posts menu.
161
	 *
162
	 * @param bool $calypso Optional. Whether links should point to Calypso or wp-admin. Default true (Calypso).
163
	 */
164 View Code Duplication
	public function add_posts_menu( $calypso = true ) {
165
		if ( ! $calypso ) {
166
			return;
167
		}
168
169
		$ptype_obj = get_post_type_object( 'post' );
170
		$menu_slug = 'https://wordpress.com/posts/' . $this->domain;
171
172
		remove_menu_page( 'edit.php' );
173
		remove_submenu_page( 'edit.php', 'edit.php' );
174
		remove_submenu_page( 'edit.php', 'post-new.php' );
175
176
		add_menu_page( esc_attr( $ptype_obj->labels->menu_name ), $ptype_obj->labels->menu_name, $ptype_obj->cap->edit_posts, $menu_slug, null, 'dashicons-admin-post', $ptype_obj->menu_position );
177
		add_submenu_page( $menu_slug, $ptype_obj->labels->all_items, $ptype_obj->labels->all_items, $ptype_obj->cap->edit_posts, $menu_slug, null, 5 );
178
		add_submenu_page( $menu_slug, $ptype_obj->labels->add_new, $ptype_obj->labels->add_new, $ptype_obj->cap->create_posts, 'https://wordpress.com/post/' . $this->domain, null, 10 );
179
180
		$this->migrate_submenus( 'edit.php', $menu_slug );
181
	}
182
183
	/**
184
	 * Adds Media menu.
185
	 *
186
	 * @param bool $calypso Optional. Whether links should point to Calypso or wp-admin. Default true (Calypso).
187
	 */
188
	public function add_media_menu( $calypso = true ) {
189
		remove_submenu_page( 'upload.php', 'upload.php' );
190
		remove_submenu_page( 'upload.php', 'media-new.php' );
191
192
		if ( $calypso ) {
193
			$menu_slug = 'https://wordpress.com/media/' . $this->domain;
194
195
			remove_menu_page( 'upload.php' );
196
			add_menu_page( __( 'Media', 'jetpack' ), __( 'Media', 'jetpack' ), 'upload_files', $menu_slug, null, 'dashicons-admin-media', 10 );
197
			$this->migrate_submenus( 'upload.php', $menu_slug );
198
		}
199
	}
200
201
	/**
202
	 * Adds Page menu.
203
	 *
204
	 * @param bool $calypso Optional. Whether links should point to Calypso or wp-admin. Default true (Calypso).
205
	 */
206 View Code Duplication
	public function add_page_menu( $calypso = true ) {
207
		if ( ! $calypso ) {
208
			return;
209
		}
210
211
		$ptype_obj = get_post_type_object( 'page' );
212
		$menu_slug = 'https://wordpress.com/pages/' . $this->domain;
213
214
		remove_menu_page( 'edit.php?post_type=page' );
215
		remove_submenu_page( 'edit.php?post_type=page', 'edit.php?post_type=page' );
216
		remove_submenu_page( 'edit.php?post_type=page', 'post-new.php?post_type=page' );
217
218
		add_menu_page( esc_attr( $ptype_obj->labels->menu_name ), $ptype_obj->labels->menu_name, $ptype_obj->cap->edit_posts, $menu_slug, null, 'dashicons-admin-page', $ptype_obj->menu_position );
219
		add_submenu_page( $menu_slug, $ptype_obj->labels->all_items, $ptype_obj->labels->all_items, $ptype_obj->cap->edit_posts, $menu_slug, null, 5 );
220
		add_submenu_page( $menu_slug, $ptype_obj->labels->add_new, $ptype_obj->labels->add_new, $ptype_obj->cap->create_posts, 'https://wordpress.com/page/' . $this->domain, null, 10 );
221
		$this->migrate_submenus( 'edit.php?post_type=page', $menu_slug );
222
	}
223
224
	/**
225
	 * Adds Comments menu.
226
	 *
227
	 * @param bool $calypso Optional. Whether links should point to Calypso or wp-admin. Default true (Calypso).
228
	 */
229
	public function add_comments_menu( $calypso = true ) {
230
		if ( ! $calypso || ! current_user_can( 'edit_posts' ) ) {
231
			return;
232
		}
233
234
		$awaiting_mod      = wp_count_comments();
235
		$awaiting_mod      = $awaiting_mod->moderated;
236
		$awaiting_mod_i18n = number_format_i18n( $awaiting_mod );
237
		/* translators: %s: Number of comments. */
238
		$awaiting_mod_text = sprintf( _n( '%s Comment in moderation', '%s Comments in moderation', $awaiting_mod, 'jetpack' ), $awaiting_mod_i18n );
239
240
		/* translators: %s: Number of comments. */
241
		$menu_title = sprintf( __( 'Comments %s', 'jetpack' ), '<span class="awaiting-mod count-' . absint( $awaiting_mod ) . '"><span class="pending-count" aria-hidden="true">' . $awaiting_mod_i18n . '</span><span class="comments-in-moderation-text screen-reader-text">' . $awaiting_mod_text . '</span></span>' );
242
		$menu_slug  = 'https://wordpress.com/comments/all/' . $this->domain;
243
244
		remove_menu_page( 'edit-comments.php' );
245
		remove_submenu_page( 'edit-comments.php', 'edit-comments.php' );
246
247
		add_menu_page( esc_attr__( 'Comments', 'jetpack' ), $menu_title, 'edit_posts', $menu_slug, null, 'dashicons-admin-comments', 25 );
248
		$this->migrate_submenus( 'edit-comments.php', $menu_slug );
249
	}
250
251
	/**
252
	 * Adds Appearance menu.
253
	 *
254
	 * @param bool $calypso Optional. Whether links should point to Calypso or wp-admin. Default true (Calypso).
255
	 */
256
	public function add_appearance_menu( $calypso = true ) {
257
		$user_can_customize = current_user_can( 'customize' );
258
		$appearance_cap     = current_user_can( 'switch_themes' ) ? 'switch_themes' : 'edit_theme_options';
259
		$customize_slug     = $calypso ? 'https://wordpress.com/customize/' . $this->domain : 'customize.php';
260
		$themes_slug        = $calypso ? 'https://wordpress.com/themes/' . $this->domain : 'themes.php';
261
		$customize_url      = add_query_arg( 'return', urlencode( remove_query_arg( wp_removable_query_args(), wp_unslash( $_SERVER['REQUEST_URI'] ) ) ), 'customize.php' ); // phpcs:ignore
262
263
		remove_menu_page( 'themes.php' );
264
		remove_submenu_page( 'themes.php', 'themes.php' );
265
		remove_submenu_page( 'themes.php', 'theme-editor.php' );
266
		remove_submenu_page( 'themes.php', $customize_url );
267
268
		add_menu_page( esc_attr__( 'Appearance', 'jetpack' ), __( 'Appearance', 'jetpack' ), $appearance_cap, $themes_slug, null, 'dashicons-admin-appearance', 60 );
269
		add_submenu_page( $themes_slug, esc_attr__( 'Themes', 'jetpack' ), __( 'Themes', 'jetpack' ), 'switch_themes', $themes_slug, null, 5 );
270
		add_submenu_page( $themes_slug, esc_attr__( 'Customize', 'jetpack' ), __( 'Customize', 'jetpack' ), 'customize', $customize_slug, null, 10 );
271
272 View Code Duplication
		if ( current_theme_supports( 'custom-header' ) && $user_can_customize ) {
273
			$customize_header_url = add_query_arg( array( 'autofocus' => array( 'control' => 'header_image' ) ), $customize_url );
274
			remove_submenu_page( 'themes.php', $customize_header_url );
275
276
			$customize_header_url = add_query_arg( array( 'autofocus' => array( 'control' => 'header_image' ) ), $customize_slug );
277
			add_submenu_page( $themes_slug, __( 'Header', 'jetpack' ), __( 'Header', 'jetpack' ), 'customize', esc_url( $customize_header_url ), null, 15 );
278
		}
279
280 View Code Duplication
		if ( current_theme_supports( 'custom-background' ) && $user_can_customize ) {
281
			$customize_background_url = add_query_arg( array( 'autofocus' => array( 'control' => 'background_image' ) ), $customize_url );
282
			remove_submenu_page( 'themes.php', $customize_background_url );
283
284
			$customize_background_url = add_query_arg( array( 'autofocus' => array( 'control' => 'background_image' ) ), $customize_slug );
285
			add_submenu_page( $themes_slug, esc_attr__( 'Background', 'jetpack' ), __( 'Background', 'jetpack' ), 'customize', esc_url( $customize_background_url ), null, 20 );
286
		}
287
288 View Code Duplication
		if ( current_theme_supports( 'widgets' ) ) {
289
			remove_submenu_page( 'themes.php', 'widgets.php' );
290
291
			$customize_menu_url = add_query_arg( array( 'autofocus' => array( 'panel' => 'widgets' ) ), $customize_slug );
292
			add_submenu_page( $themes_slug, esc_attr__( 'Widgets', 'jetpack' ), __( 'Widgets', 'jetpack' ), 'customize', esc_url( $customize_menu_url ), null, 20 );
293
		}
294
295 View Code Duplication
		if ( current_theme_supports( 'menus' ) || current_theme_supports( 'widgets' ) ) {
296
			remove_submenu_page( 'themes.php', 'nav-menus.php' );
297
298
			$customize_menu_url = add_query_arg( array( 'autofocus' => array( 'panel' => 'nav_menus' ) ), $customize_slug );
299
			add_submenu_page( $themes_slug, esc_attr__( 'Menus', 'jetpack' ), __( 'Menus', 'jetpack' ), 'customize', esc_url( $customize_menu_url ), null, 20 );
300
		}
301
302
		$this->migrate_submenus( 'themes.php', $themes_slug );
303
	}
304
305
	/**
306
	 * Adds Plugins menu.
307
	 */
308
	public function add_plugins_menu() {
309
		remove_submenu_page( 'plugins.php', 'plugin-editor.php' );
310
	}
311
312
	/**
313
	 * Adds Users menu.
314
	 *
315
	 * @param bool $calypso Optional. Whether links should point to Calypso or wp-admin. Default true (Calypso).
316
	 */
317
	public function add_users_menu( $calypso = true ) {
318
		$users_slug   = $calypso ? 'https://wordpress.com/people/team/' . $this->domain : 'users.php';
319
		$add_new_slug = 'https://wordpress.com/people/new/' . $this->domain;
320
		$profile_slug = $calypso ? 'https://wordpress.com/me' : 'profile.php';
321
322
		if ( current_user_can( 'list_users' ) ) {
323
			remove_menu_page( 'users.php' );
324
			remove_submenu_page( 'users.php', 'users.php' );
325
			remove_submenu_page( 'users.php', 'user-new.php' );
326
			remove_submenu_page( 'users.php', 'profile.php' );
327
			remove_submenu_page( 'users.php', 'grofiles-editor' );
328
			remove_submenu_page( 'users.php', 'grofiles-user-settings' );
329
330
			add_menu_page( esc_attr__( 'Users', 'jetpack' ), __( 'Users', 'jetpack' ), 'list_users', $users_slug, null, 'dashicons-admin-users', 70 );
331
			add_submenu_page( $users_slug, esc_attr__( 'All People', 'jetpack' ), __( 'All People', 'jetpack' ), 'list_users', $users_slug, null, 5 );
332
			add_submenu_page( $users_slug, esc_attr__( 'Add New', 'jetpack' ), __( 'Add New', 'jetpack' ), 'promote_users', $add_new_slug, null, 10 );
333
			add_submenu_page( $users_slug, esc_attr__( 'My Profile', 'jetpack' ), __( 'My Profile', 'jetpack' ), 'read', $profile_slug, null, 15 );
334
			add_submenu_page( $users_slug, esc_attr__( 'Account Settings', 'jetpack' ), __( 'Account Settings', 'jetpack' ), 'read', 'https://wordpress.com/me/account', null, 20 );
335
			$this->migrate_submenus( 'users.php', $users_slug );
336
		}
337
	}
338
339
	/**
340
	 * Adds Tools menu.
341
	 *
342
	 * @param bool $calypso Optional. Whether links should point to Calypso or wp-admin. Default true (Calypso).
343
	 */
344
	public function add_tools_menu( $calypso = true ) {
345
		if ( ! $calypso ) {
346
			return;
347
		}
348
349
		$admin_slug = 'tools.php';
350
		$menu_slug  = 'https://wordpress.com/marketing/tools/' . $this->domain;
351
352
		remove_menu_page( $admin_slug );
353
		remove_submenu_page( $admin_slug, $admin_slug );
354
		remove_submenu_page( $admin_slug, 'import.php' );
355
		remove_submenu_page( $admin_slug, 'export.php' );
356
		remove_submenu_page( $admin_slug, 'delete-blog' );
357
358
		add_menu_page( esc_attr__( 'Tools', 'jetpack' ), __( 'Tools', 'jetpack' ), 'manage_options', $menu_slug, null, 'dashicons-admin-tools', 75 );
359
		add_submenu_page( $menu_slug, esc_attr__( 'Import', 'jetpack' ), __( 'Import', 'jetpack' ), 'import', 'https://wordpress.com/import/' . $this->domain, null, 15 );
360
		add_submenu_page( $menu_slug, esc_attr__( 'Export', 'jetpack' ), __( 'Export', 'jetpack' ), 'export', 'https://wordpress.com/export/' . $this->domain, null, 20 );
361
362
		$this->migrate_submenus( $admin_slug, $menu_slug );
363
364
	}
365
366
	/**
367
	 * Adds Settings menu.
368
	 *
369
	 * @param bool $calypso Optional. Whether links should point to Calypso or wp-admin. Default true (Calypso).
370
	 */
371
	public function add_options_menu( $calypso = true ) {
372
		if ( $calypso ) {
373
			remove_submenu_page( 'options-general.php', 'options-discussion.php' );
374
			remove_submenu_page( 'options-general.php', 'options-writing.php' );
375
		}
376
	}
377
378
	/**
379
	 * Migrates submenu items from wp-admin menu slugs to Calypso menu slugs.
380
	 *
381
	 * @param string $old_slug WP-Admin menu slug.
382
	 * @param string $new_slug Calypso menu slug. (Calypso URL).
383
	 */
384
	public function migrate_submenus( $old_slug, $new_slug ) {
385
		global $submenu;
386
387
		if ( $old_slug !== $new_slug && ! empty( $submenu[ $old_slug ] ) ) {
388
			if ( ! empty( $submenu[ $new_slug ] ) ) {
389
				// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
390
				$submenu[ $new_slug ] = array_replace( $submenu[ $new_slug ], $submenu[ $old_slug ] );
391
			} else {
392
				// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
393
				$submenu[ $new_slug ] = $submenu[ $old_slug ];
394
			}
395
			unset( $submenu[ $old_slug ] );
396
		}
397
	}
398
399
	/**
400
	 * Adds a menu separator.
401
	 *
402
	 * @param int    $position The position in the menu order this item should appear.
403
	 * @param string $cap      Optional. The capability required for this menu to be displayed to the user.
404
	 *                         Default: 'read'.
405
	 */
406
	public function add_admin_menu_separator( $position, $cap = 'read' ) {
407
		global $menu;
408
		static $uid = 3;
409
410
		// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
411
		$menu[ $position ] = array(
412
			'',                               // Menu title (ignored).
413
			$cap,                             // Required capability.
414
			'separator-custom-' . ( ++$uid ), // URL or file (ignored, but must be unique).
415
			'',                               // Page title (ignored).
416
			'wp-menu-separator',              // CSS class. Identifies this item as a separator.
417
		);
418
		ksort( $menu );
419
	}
420
421
	/**
422
	 * Enqueues scripts and styles.
423
	 */
424
	public function enqueue_scripts() {
425
		$style_dependencies = array();
0 ignored issues
show
Unused Code introduced by
$style_dependencies is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
426
		$rtl                = is_rtl() ? '-rtl' : '';
427 View Code Duplication
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
428
			$style_dependencies = array( 'wpcom-admin-bar', 'wpcom-masterbar-css' );
429
		} else {
430
			$style_dependencies = array( 'a8c-wpcom-masterbar' . $rtl, 'a8c-wpcom-masterbar-overrides' . $rtl );
431
		}
432
		wp_enqueue_style(
433
			'jetpack-admin-menu',
434
			plugins_url( 'admin-menu.css', __FILE__ ),
435
			$style_dependencies,
436
			JETPACK__VERSION
437
		);
438
		wp_enqueue_script(
439
			'jetpack-admin-menu',
440
			plugins_url( 'admin-menu.js', __FILE__ ),
441
			array(),
442
			JETPACK__VERSION,
443
			true
444
		);
445
	}
446
447
	/**
448
	 * Dequeues unnecessary scripts.
449
	 */
450
	public function dequeue_scripts() {
451
		wp_dequeue_script( 'a8c_wpcom_masterbar_overrides' ); // Initially loaded in modules/masterbar/masterbar/class-masterbar.php.
452
	}
453
}
454