Completed
Push — add/admin-menu-domain-only ( 7cf836 )
by
unknown
24:40 queued 13:55
created

Atomic_Admin_Menu::add_new_site_link()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 0
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
1
<?php
2
/**
3
 * Atomic Admin Menu file.
4
 *
5
 * @package automattic/jetpack
6
 */
7
8
namespace Automattic\Jetpack\Dashboard_Customizations;
9
10
require_once __DIR__ . '/class-admin-menu.php';
11
12
/**
13
 * Class Atomic_Admin_Menu.
14
 */
15
class Atomic_Admin_Menu extends Admin_Menu {
16
17
	/**
18
	 * Atomic_Admin_Menu constructor.
19
	 */
20
	protected 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
		add_action(
27
			'admin_menu',
28
			function () {
29
				remove_action( 'admin_menu', 'gutenberg_menu', 9 );
30
			},
31
			0
32
		);
33
	}
34
35
	/**
36
	 * Dequeues unnecessary scripts.
37
	 */
38
	public function dequeue_scripts() {
39
		wp_dequeue_script( 'a8c_wpcom_masterbar_overrides' ); // Initially loaded in modules/masterbar/masterbar/class-masterbar.php.
40
	}
41
42
	/**
43
	 * Create the desired menu output.
44
	 */
45
	public function reregister_menu_items() {
46
		parent::reregister_menu_items();
47
48
		$this->add_my_home_menu();
49
50
		// Not needed outside of wp-admin.
51
		if ( ! $this->is_api_request ) {
52
			$this->add_browse_sites_link();
53
			$this->add_site_card_menu();
54
			$this->add_new_site_link();
55
		}
56
57
		ksort( $GLOBALS['menu'] );
58
	}
59
60
	/**
61
	 * Adds Plugins menu.
62
	 *
63
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
64
	 */
65
	public function add_plugins_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
66
		// Plugins on Atomic sites are always managed on WP Admin.
67
		parent::add_plugins_menu( true );
68
	}
69
70
	/**
71
	 * Adds the site switcher link if user has more than one site.
72
	 */
73
	public function add_browse_sites_link() {
74
		$site_count = get_user_option( 'wpcom_site_count' );
75
		if ( ! $site_count || $site_count < 2 ) {
76
			return;
77
		}
78
79
		// Add the menu item.
80
		add_menu_page( __( 'site-switcher', 'jetpack' ), __( 'Browse sites', 'jetpack' ), 'read', 'https://wordpress.com/home', null, 'dashicons-arrow-left-alt2', 0 );
81
		add_filter( 'add_menu_classes', array( $this, 'set_browse_sites_link_class' ) );
82
	}
83
84
	/**
85
	 * Adds a custom element class for Site Switcher menu item.
86
	 *
87
	 * @param array $menu Associative array of administration menu items.
88
	 *
89
	 * @return array
90
	 */
91 View Code Duplication
	public function set_browse_sites_link_class( array $menu ) {
92
		foreach ( $menu as $key => $menu_item ) {
93
			if ( 'site-switcher' !== $menu_item[3] ) {
94
				continue;
95
			}
96
97
			$menu[ $key ][4] = add_cssclass( 'site-switcher', $menu_item[4] );
98
			break;
99
		}
100
101
		return $menu;
102
	}
103
104
	/**
105
	 * Adds a link to the menu to create a new site.
106
	 */
107
	public function add_new_site_link() {
108
		global $menu;
109
110
		$site_count = get_user_option( 'wpcom_site_count' );
111
		if ( $site_count && $site_count > 1 ) {
112
			return;
113
		}
114
115
		// Attempt to get last position.
116
		$position = 1000;
117
		while ( isset( $menu[ $position ] ) ) {
118
			$position++;
119
		}
120
121
		$this->add_admin_menu_separator( ++$position );
122
		add_menu_page( __( 'Add new site', 'jetpack' ), __( 'Add new site', 'jetpack' ), 'read', 'https://wordpress.com/start?ref=calypso-sidebar', null, 'dashicons-plus-alt', ++$position );
123
	}
124
125
	/**
126
	 * Adds site card component.
127
	 */
128
	public function add_site_card_menu() {
129
		$default   = 'data:image/svg+xml,' . rawurlencode( '<svg class="gridicon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><title>Globe</title><rect fill-opacity="0" x="0" width="24" height="24"/><g><path fill="#fff" d="M12 2C6.477 2 2 6.477 2 12s4.477 10 10 10 10-4.477 10-10S17.523 2 12 2zm0 18l2-2 1-1v-2h-2v-1l-1-1H9v3l2 2v1.93c-3.94-.494-7-3.858-7-7.93l1 1h2v-2h2l3-3V6h-2L9 5v-.41C9.927 4.21 10.94 4 12 4s2.073.212 3 .59V6l-1 1v2l1 1 3.13-3.13c.752.897 1.304 1.964 1.606 3.13H18l-2 2v2l1 1h2l.286.286C18.03 18.06 15.24 20 12 20z"/></g></svg>' );
130
		$icon      = get_site_icon_url( 32, $default );
131
		$blog_name = get_option( 'blogname' ) !== '' ? get_option( 'blogname' ) : $this->domain;
132
133
		$badge = '';
134
		if ( function_exists( 'site_is_private' ) && site_is_private() ) {
135
			$badge .= sprintf(
136
				'<span class="site__badge site__badge-private">%s</span>',
137
				site_is_coming_soon() ? esc_html__( 'Coming Soon', 'jetpack' ) : esc_html__( 'Private', 'jetpack' )
138
			);
139
		}
140
141
		$site_card = '
142
<div class="site__info">
143
	<div class="site__title">%1$s</div>
144
	<div class="site__domain">%2$s</div>
145
	%3$s
146
</div>';
147
148
		$site_card = sprintf(
149
			$site_card,
150
			$blog_name,
151
			$this->domain,
152
			$badge
153
		);
154
155
		add_menu_page( 'site-card', $site_card, 'read', get_home_url(), null, $icon, 1 );
156
		add_filter( 'add_menu_classes', array( $this, 'set_site_card_menu_class' ) );
157
	}
158
159
	/**
160
	 * Adds a custom element class and id for Site Card's menu item.
161
	 *
162
	 * @param array $menu Associative array of administration menu items.
163
	 *
164
	 * @return array
165
	 */
166
	public function set_site_card_menu_class( array $menu ) {
167
		foreach ( $menu as $key => $menu_item ) {
168
			if ( 'site-card' !== $menu_item[3] ) {
169
				continue;
170
			}
171
172
			$classes = ' toplevel_page_site-card';
173
174
			// webclip.png is the default on WoA sites. Anything other than that means we have a custom site icon.
175
			if ( has_site_icon() && 'https://s0.wp.com/i/webclip.png' !== get_site_icon_url( 512 ) ) {
176
				$classes .= ' has-site-icon';
177
			}
178
179
			$menu[ $key ][4] = $menu_item[4] . $classes;
180
			$menu[ $key ][5] = 'toplevel_page_site_card';
181
			break;
182
		}
183
184
		return $menu;
185
	}
186
187
	/**
188
	 * Adds Upgrades menu.
189
	 */
190
	public function add_upgrades_menu() {
191
		parent::add_upgrades_menu();
192
193
		add_submenu_page( 'paid-upgrades.php', __( 'Domains', 'jetpack' ), __( 'Domains', 'jetpack' ), 'manage_options', 'https://wordpress.com/domains/manage/' . $this->domain, null, 10 );
194
	}
195
196
	/**
197
	 * Adds Tools menu.
198
	 *
199
	 * @param bool $wp_admin_import Optional. Whether Import link should point to Calypso or wp-admin. Default false (Calypso).
200
	 * @param bool $wp_admin_export Optional. Whether Export link should point to Calypso or wp-admin. Default false (Calypso).
201
	 */
202
	public function add_tools_menu( $wp_admin_import = false, $wp_admin_export = false ) {  // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
203
		// Export on Atomic sites is always handled on WP Admin.
204
		parent::add_tools_menu( $wp_admin_import, true );
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 ) {
213
		parent::add_options_menu( $wp_admin );
214
215
		add_submenu_page( 'options-general.php', esc_attr__( 'Hosting Configuration', 'jetpack' ), __( 'Hosting Configuration', 'jetpack' ), 'manage_options', 'https://wordpress.com/hosting-config/' . $this->domain, null, 6 );
216
217
		// No need to add a menu linking to WP Admin if there is already one.
218
		if ( ! $wp_admin ) {
219
			add_submenu_page( 'options-general.php', esc_attr__( 'Advanced General', 'jetpack' ), __( 'Advanced General', 'jetpack' ), 'manage_options', 'options-general.php' );
220
			add_submenu_page( 'options-general.php', esc_attr__( 'Advanced Writing', 'jetpack' ), __( 'Advanced Writing', 'jetpack' ), 'manage_options', 'options-writing.php' );
221
		}
222
	}
223
224
	/**
225
	 * Adds Appearance menu.
226
	 *
227
	 * @param bool $wp_admin_themes Optional. Whether Themes link should point to Calypso or wp-admin. Default false (Calypso).
228
	 * @param bool $wp_admin_customize Optional. Whether Customize link should point to Calypso or wp-admin. Default false (Calypso).
229
	 */
230
	public function add_appearance_menu( $wp_admin_themes = false, $wp_admin_customize = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
231
		// Customize on Atomic sites is always done on WP Admin.
232
		parent::add_appearance_menu( $wp_admin_themes, true );
233
234
		add_submenu_page( 'themes.php', esc_attr__( 'Add New Theme', 'jetpack' ), __( 'Add New Theme', 'jetpack' ), 'install_themes', 'theme-install.php', null, 1 );
235
	}
236
237
	/**
238
	 * Adds Users menu.
239
	 *
240
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
241
	 */
242
	public function add_users_menu( $wp_admin = false ) {
243
		parent::add_users_menu( $wp_admin );
244
245
		add_submenu_page( 'users.php', esc_attr__( 'Advanced Users Management', 'jetpack' ), __( 'Advanced Users Management', 'jetpack' ), 'list_users', 'users.php', null, 2 );
246
	}
247
248
	/**
249
	 * Also remove the Gutenberg plugin menu.
250
	 *
251
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
252
	 */
253
	public function add_gutenberg_menus( $wp_admin = false ) {
254
		// Always remove the Gutenberg menu.
255
		remove_menu_page( 'gutenberg' );
256
		parent::add_gutenberg_menus( $wp_admin );
257
	}
258
}
259