Completed
Push — e2e/e2e-page-actions ( 1f4d85...20a111 )
by
unknown
10:03
created

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