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

Jetpack_Admin_Menu::add_options_menu()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Jetpack Admin Menu file.
4
 *
5
 * @package Jetpack
6
 */
7
8
namespace Automattic\Jetpack;
9
10
/**
11
 * Class Jetpack_Admin_Menu.
12
 */
13
class Jetpack_Admin_Menu extends Admin_Menu {
14
15
	/**
16
	 * Jetpack_Admin_Menu constructor.
17
	 */
18
	public function __construct() {
19
		parent::__construct();
20
21
		add_action( 'wp_enqueue_scripts', array( $this, 'dequeue_scripts' ), 20 );
22
		add_action( 'admin_enqueue_scripts', array( $this, 'dequeue_scripts' ), 20 );
23
	}
24
25
	/**
26
	 * Dequeues unnecessary scripts.
27
	 */
28
	public function dequeue_scripts() {
29
		wp_dequeue_script( 'a8c_wpcom_masterbar_overrides' ); // Initially loaded in modules/masterbar/masterbar/class-masterbar.php.
30
	}
31
32
	/**
33
	 * Determines whether the current locale is right-to-left (RTL).
34
	 *
35
	 * Performs the check against the current locale set on the WordPress.com's account settings.
36
	 * See `Masterbar::__construct` in `modules/masterbar/masterbar/class-masterbar.php`.
37
	 */
38
	public function is_rtl() {
39
		return get_user_option( 'jetpack_wpcom_is_rtl' );
40
	}
41
42
	/**
43
	 * Create the desired menu output.
44
	 */
45
	public function reregister_menu_items() {
46
		global $menu, $submenu;
47
48
		// Change the menu only when rendered in Calypso.
49
		if ( $this->is_api_request || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) {
50
			// Reset menus so there are no third-party plugin items.
51
			$menu    = array(); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
52
			$submenu = array(); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
53
54
			parent::reregister_menu_items();
55
56
			$this->add_feedback_menu();
57
			$this->add_wp_admin_menu();
58
59
			ksort( $GLOBALS['menu'] );
60
		}
61
	}
62
63
	/**
64
	 * Whether to use wp-admin pages rather than Calypso.
65
	 *
66
	 * @return bool
67
	 */
68
	public function should_link_to_wp_admin() {
69
		// Force Calypso links on Jetpack sites since Nav Unification is disabled on WP Admin.
70
		return false;
71
	}
72
73
	/**
74
	 * Adds Posts menu.
75
	 *
76
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
77
	 */
78
	public function add_posts_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
79
		$post = get_post_type_object( 'post' );
80
		add_menu_page( esc_attr( $post->labels->menu_name ), $post->labels->menu_name, $post->cap->edit_posts, 'https://wordpress.com/posts/' . $this->domain, null, 'dashicons-admin-post' );
81
	}
82
83
	/**
84
	 * Adds Media menu.
85
	 *
86
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
87
	 */
88
	public function add_media_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
89
		add_menu_page( __( 'Media', 'jetpack' ), __( 'Media', 'jetpack' ), 'upload_files', 'https://wordpress.com/media/' . $this->domain, null, 'dashicons-admin-media' );
90
	}
91
92
	/**
93
	 * Adds Page menu.
94
	 *
95
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
96
	 */
97
	public function add_page_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
98
		$page = get_post_type_object( 'page' );
99
		add_menu_page( esc_attr( $page->labels->menu_name ), $page->labels->menu_name, $page->cap->edit_posts, 'https://wordpress.com/pages/' . $this->domain, null, 'dashicons-admin-page' );
100
	}
101
102
	/**
103
	 * Adds a custom post type menu.
104
	 *
105
	 * @param string $post_type Custom post type.
106
	 * @param bool   $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
107
	 */
108
	public function add_custom_post_type_menu( $post_type, $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
109
		$ptype_obj = get_post_type_object( $post_type );
110
		if ( empty( $ptype_obj ) ) {
111
			return;
112
		}
113
114
		$menu_slug = 'https://wordpress.com/types/' . $post_type . '/' . $this->domain;
115
116
		// Menu icon.
117
		$menu_icon = 'dashicons-admin-post';
118
		if ( is_string( $ptype_obj->menu_icon ) ) {
119
			// Special handling for data:image/svg+xml and Dashicons.
120
			if ( 0 === strpos( $ptype_obj->menu_icon, 'data:image/svg+xml;base64,' ) || 0 === strpos( $ptype_obj->menu_icon, 'dashicons-' ) ) {
121
				$menu_icon = $ptype_obj->menu_icon;
122
			} else {
123
				$menu_icon = esc_url( $ptype_obj->menu_icon );
124
			}
125
		}
126
127
		add_menu_page( esc_attr( $ptype_obj->labels->menu_name ), $ptype_obj->labels->menu_name, $ptype_obj->cap->edit_posts, $menu_slug, null, $menu_icon );
128
	}
129
130
	/**
131
	 * Adds Comments 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_comments_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
136
		add_menu_page( esc_attr__( 'Comments', 'jetpack' ), __( 'Comments', 'jetpack' ), 'edit_posts', 'https://wordpress.com/comments/all/' . $this->domain, null, 'dashicons-admin-comments' );
137
	}
138
139
	/**
140
	 * Adds Feedback menu.
141
	 */
142
	public function add_feedback_menu() {
143
		$post_type = 'feedback';
144
145
		$ptype_obj = get_post_type_object( $post_type );
146
		if ( empty( $ptype_obj ) ) {
147
			return;
148
		}
149
150
		$slug       = 'edit.php?post_type=' . $post_type;
151
		$name       = $ptype_obj->labels->menu_name;
152
		$capability = $ptype_obj->cap->edit_posts;
153
		$icon       = $ptype_obj->menu_icon;
154
		$position   = 45; // Before Jetpack.
155
156
		add_menu_page( esc_attr( $name ), $name, $capability, $slug, null, $icon, $position );
157
	}
158
159
	/**
160
	 * Adds Jetpack menu.
161
	 */
162
	public function add_jetpack_menu() {
163
		parent::add_jetpack_menu();
164
165
		// Place "Scan" submenu after Backup.
166
		$position = 0;
167
		global $submenu;
168
		foreach ( $submenu['jetpack'] as $submenu_item ) {
169
			$position ++;
170
			if ( __( 'Backup', 'jetpack' ) === $submenu_item[3] ) {
171
				break;
172
			}
173
		}
174
		add_submenu_page( 'jetpack', esc_attr__( 'Scan', 'jetpack' ), __( 'Scan', 'jetpack' ), 'manage_options', 'https://wordpress.com/scan/' . $this->domain, null, $position );
175
	}
176
177
	/**
178
	 * Adds Appearance menu.
179
	 *
180
	 * @param bool $wp_admin_themes Optional. Whether Themes link should point to Calypso or wp-admin. Default false (Calypso).
181
	 * @param bool $wp_admin_customize Optional. Whether Customize link should point to Calypso or wp-admin. Default false (Calypso).
182
	 */
183
	public function add_appearance_menu( $wp_admin_themes = false, $wp_admin_customize = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
184
		add_menu_page( esc_attr__( 'Appearance', 'jetpack' ), __( 'Appearance', 'jetpack' ), 'switch_themes', 'https://wordpress.com/themes/' . $this->domain, null, 'dashicons-admin-appearance', 60 );
185
		add_submenu_page( 'themes.php', esc_attr__( 'Themes', 'jetpack' ), __( 'Themes', 'jetpack' ), 'switch_themes', 'https://wordpress.com/themes/' . $this->domain );
186
		// Customize on Jetpack sites is always done on WP Admin (unsupported by Calypso).
187
		add_submenu_page( 'themes.php', esc_attr__( 'Customize', 'jetpack' ), __( 'Customize', 'jetpack' ), 'customize', 'customize.php' );
188
	}
189
190
	/**
191
	 * Adds Plugins menu.
192
	 *
193
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
194
	 */
195
	public function add_plugins_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
196
		add_menu_page( esc_attr__( 'Plugins', 'jetpack' ), __( 'Plugins', 'jetpack' ), 'activate_plugins', 'https://wordpress.com/plugins/' . $this->domain, null, 'dashicons-admin-plugins', 65 );
197
	}
198
199
	/**
200
	 * Adds Users menu.
201
	 *
202
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
203
	 */
204
	public function add_users_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
205
		if ( current_user_can( 'list_users' ) ) {
206
			add_menu_page( esc_attr__( 'Users', 'jetpack' ), __( 'Users', 'jetpack' ), 'list_users', 'https://wordpress.com/people/team/' . $this->domain, null, 'dashicons-admin-users', 70 );
207
		} else {
208
			add_menu_page( esc_attr__( 'My Profile', 'jetpack' ), __( 'Profile', 'jetpack' ), 'read', 'https://wordpress.com/me', null, 'dashicons-admin-users', 70 );
209
		}
210
	}
211
212
	/**
213
	 * Adds Tools menu.
214
	 *
215
	 * @param bool $wp_admin_import Optional. Whether Import link should point to Calypso or wp-admin. Default false (Calypso).
216
	 * @param bool $wp_admin_export Optional. Whether Export link should point to Calypso or wp-admin. Default false (Calypso).
217
	 */
218
	public function add_tools_menu( $wp_admin_import = false, $wp_admin_export = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
219
		add_menu_page( esc_attr__( 'Tools', 'jetpack' ), __( 'Tools', 'jetpack' ), 'publish_posts', 'tools.php', null, 'dashicons-admin-tools', 75 );
220
221
		add_submenu_page( 'tools.php', esc_attr__( 'Marketing', 'jetpack' ), __( 'Marketing', 'jetpack' ), 'publish_posts', 'https://wordpress.com/marketing/tools/' . $this->domain );
222
		add_submenu_page( 'tools.php', esc_attr__( 'Earn', 'jetpack' ), __( 'Earn', 'jetpack' ), 'manage_options', 'https://wordpress.com/earn/' . $this->domain );
223
224
		// Import/Export on Jetpack sites is always handled on WP Admin.
225
		add_submenu_page( 'tools.php', esc_attr__( 'Import', 'jetpack' ), __( 'Import', 'jetpack' ), 'import', 'import.php' );
226
		add_submenu_page( 'tools.php', esc_attr__( 'Export', 'jetpack' ), __( 'Export', 'jetpack' ), 'export', 'export.php' );
227
228
		// Remove the submenu auto-created by Core.
229
		$this->hide_submenu_page( 'tools.php', 'tools.php' );
230
	}
231
232
	/**
233
	 * Adds Settings menu.
234
	 *
235
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
236
	 */
237
	public function add_options_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
238
		add_menu_page( esc_attr__( 'Settings', 'jetpack' ), __( 'Settings', 'jetpack' ), 'manage_options', 'https://wordpress.com/settings/general/' . $this->domain, null, 'dashicons-admin-settings', 80 );
239
	}
240
241
	/**
242
	 * Adds WP Admin menu.
243
	 */
244
	public function add_wp_admin_menu() {
245
		global $menu;
246
247
		// Attempt to get last position.
248
		ksort( $menu );
249
		end( $menu );
250
		$position = key( $menu );
251
252
		$this->add_admin_menu_separator( ++ $position );
253
		add_menu_page( __( 'WP Admin', 'jetpack' ), __( 'WP Admin', 'jetpack' ), 'read', 'index.php', null, 'dashicons-wordpress-alt', $position );
254
	}
255
}
256