Completed
Push — master ( 68076d...b8782e )
by Camilla
10:53
created

P2_Admin_Menu::__construct()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 2
nop 0
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
1
<?php
2
/**
3
 * P2 Admin Menu file.
4
 *
5
 * @package Jetpack
6
 */
7
8
namespace Automattic\Jetpack\Dashboard_Customizations;
9
10
require_once __DIR__ . '/class-wpcom-admin-menu.php';
11
12
/**
13
 * Class P2_Admin_Menu.
14
 */
15
class P2_Admin_Menu extends WPcom_Admin_Menu {
16
17
	/**
18
	 * Slug for the "Appearance" menu item.
19
	 *
20
	 * @var string
21
	 */
22
	private $appearance_slug = 'themes.php';
23
24
	/**
25
	 * Slug for the "Jetpack" menu item.
26
	 *
27
	 * @var string
28
	 */
29
	private $jetpack_slug = 'jetpack';
30
31
	/**
32
	 * Slug for the "Upgrades" menu item.
33
	 *
34
	 * @var string
35
	 */
36
	private $upgrades_slug = 'paid-upgrades.php';
37
38
	/**
39
	 * Slug for the "Plugins" menu item.
40
	 *
41
	 * @var string
42
	 */
43
	private $plugins_slug = 'plugins.php';
44
45
	/**
46
	 * Slug for the "Tools" menu item.
47
	 *
48
	 * @var string
49
	 */
50
	private $tools_slug = 'tools.php';
51
52
	/**
53
	 * Whether or not the P2 is a hub.
54
	 *
55
	 * @var bool
56
	 */
57
	private $is_hub = false;
58
59
	/**
60
	 * P2_Admin_Menu constructor.
61
	 */
62
	protected function __construct() {
63
		parent::__construct();
64
65
		if (
66
			defined( 'IS_WPCOM' ) && IS_WPCOM &&
67
			function_exists( 'require_lib' )
68
		) {
69
			require_lib( 'wpforteams' );
70
71
			$this->is_hub = \WPForTeams\Workspace\is_workspace_hub( get_current_blog_id() );
72
		}
73
		// Appearance -> AMP. This needs to be called here in the constructor.
74
		// Running it from reregister_menu_items is not early enough.
75
		remove_action( 'admin_menu', 'amp_add_customizer_link' );
76
	}
77
78
	/**
79
	 * Create the desired menu output.
80
	 */
81
	public function reregister_menu_items() {
82
		parent::reregister_menu_items();
83
84
		if ( ! $this->is_hub ) {
85
			$this->remove_menus_for_p2_space();
86
			// Only add the P2 Editor menu on non-hubs.
87
			$this->add_p2_editor_menu();
88
		} else {
89
			$this->remove_menus_for_hub();
90
		}
91
92
		$this->remove_menus_for_all_p2s();
93
	}
94
95
	/**
96
	 * Remove menu items that are not applicable for P2 workspace sites.
97
	 */
98
	private function remove_menus_for_p2_space() {
99
		// Non-hub P2s can't have plans at all.
100
		remove_menu_page( $this->upgrades_slug );
101
		// Jetpack -> Backup.
102
		remove_submenu_page( $this->jetpack_slug, 'https://wordpress.com/backup/' . $this->domain );
103
		// Appearance -> Themes.
104
		remove_submenu_page( $this->appearance_slug, 'https://wordpress.com/themes/' . $this->domain );
105
		// Appearance -> Additional CSS.
106
		$customize_custom_css_url = add_query_arg(
107
			array( 'autofocus' => array( 'section' => 'css_nudge' ) ),
108
			'https://wordpress.com/customize/' . $this->domain
109
		);
110
		remove_submenu_page( $this->appearance_slug, $customize_custom_css_url );
111
	}
112
113
	/**
114
	 * Remove menu items that are not applicable for P2 hubs.
115
	 */
116
	private function remove_menus_for_hub() {
117
		// Hubs can have plans, but not domain and email products.
118
		remove_submenu_page( $this->upgrades_slug, 'https://wordpress.com/domains/manage/' . $this->domain );
119
		remove_submenu_page( $this->upgrades_slug, 'https://wordpress.com/email/' . $this->domain );
120
		// Stats.
121
		remove_menu_page( 'https://wordpress.com/stats/day/' . $this->domain );
122
		// Hide all Jetpack for hubs.
123
		remove_menu_page( $this->jetpack_slug );
124
		// Hide posts.
125
		remove_menu_page( 'edit.php' );
126
		// Hide pages.
127
		remove_menu_page( 'edit.php?post_type=page' );
128
		// Hide media.
129
		remove_menu_page( 'https://wordpress.com/media/' . $this->domain );
130
		// Hide comments.
131
		remove_menu_page( 'https://wordpress.com/comments/all/' . $this->domain );
132
		// Hide appearance.
133
		remove_menu_page( $this->appearance_slug );
134
		// Tools.
135
		remove_menu_page( $this->tools_slug );
136
	}
137
138
	/**
139
	 * Remove menu items that are not applicable for all P2s.
140
	 */
141
	private function remove_menus_for_all_p2s() {
142
		// The following menu items are hidden for both hubs and P2 sites.
143
		remove_menu_page( 'link-manager.php' );
144
		remove_menu_page( 'feedback' );
145
		remove_menu_page( $this->plugins_slug );
146
		remove_menu_page( 'https://wordpress.com/plugins/' . $this->domain );
147
148
		remove_submenu_page( $this->tools_slug, 'https://wordpress.com/marketing/tools/' . $this->domain );
149
		remove_submenu_page( $this->tools_slug, 'https://wordpress.com/earn/' . $this->domain );
150
151
		remove_submenu_page( 'https://wordpress.com/settings/general/' . $this->domain, 'sharing' );
152
		remove_submenu_page( 'https://wordpress.com/settings/general/' . $this->domain, 'polls&action=options' );
153
		remove_submenu_page( 'https://wordpress.com/settings/general/' . $this->domain, 'ratings&action=options' );
154
		remove_submenu_page(
155
			'options-general.php',
156
			'https://wordpress.com/hosting-config/' . $this->domain
157
		);
158
		remove_submenu_page(
159
			'https://wordpress.com/settings/general/' . $this->domain,
160
			'https://wordpress.com/marketing/sharing-buttons/' . $this->domain
161
		);
162
	}
163
164
	/**
165
	 * Adds the P2 Editor menu.
166
	 */
167
	private function add_p2_editor_menu() {
168
		/** This action is documented in `wp-content/plugins/p2-editor/classes/p2-editor-admin.php` */
169
		if ( apply_filters( 'p2tenberg_admin_patterns', apply_filters( 'p2editor_admin_patterns', true ) ) !== true ) {
170
			return;
171
		}
172
173
		// Add the menu only in Calypso (it already exists in WP Admin).
174
		if ( $this->is_api_request ) {
175
			add_menu_page(
176
				esc_attr__( 'P2 Editor', 'jetpack' ),
177
				__( 'P2 Editor', 'jetpack' ),
178
				'manage_options',
179
				'p2editor',
180
				'',
181
				'dashicons-admin-multisite'
182
			);
183
		}
184
	}
185
186
}
187