Completed
Push — update/admin-menu-sync-wpcom ( b0ac91...43248e )
by
unknown
124:17 queued 113:08
created

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