Completed
Push — update/add-csstidy-config-sett... ( 30eb4b...0a5859 )
by
unknown
19:15 queued 09:40
created

Jetpack_Admin_Menu::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0

1 Method

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