Completed
Push — renovate/vercel-ncc-0.x ( fdb380...917c9e )
by
unknown
149:32 queued 140:15
created

Atomic_Admin_Menu::override_the_theme_installer()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 1
dl 0
loc 8
rs 10
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
use Automattic\Jetpack\Connection\Client;
11
use Jetpack_Plan;
12
13
require_once __DIR__ . '/class-admin-menu.php';
14
15
/**
16
 * Class Atomic_Admin_Menu.
17
 */
18
class Atomic_Admin_Menu extends Admin_Menu {
19
20
	/**
21
	 * Atomic_Admin_Menu constructor.
22
	 */
23
	protected function __construct() {
24
		parent::__construct();
25
26
		add_action( 'wp_enqueue_scripts', array( $this, 'dequeue_scripts' ), 20 );
27
		add_action( 'admin_enqueue_scripts', array( $this, 'dequeue_scripts' ), 20 );
28
		add_action( 'wp_ajax_sidebar_state', array( $this, 'ajax_sidebar_state' ) );
29
30
		if ( ! $this->is_api_request ) {
31
			add_filter( 'submenu_file', array( $this, 'override_the_theme_installer' ), 10, 2 );
32
		}
33
34
		add_action(
35
			'admin_menu',
36
			function () {
37
				remove_action( 'admin_menu', 'gutenberg_menu', 9 );
38
			},
39
			0
40
		);
41
	}
42
43
	/**
44
	 * Dequeues unnecessary scripts.
45
	 */
46
	public function dequeue_scripts() {
47
		wp_dequeue_script( 'a8c_wpcom_masterbar_overrides' ); // Initially loaded in modules/masterbar/masterbar/class-masterbar.php.
48
	}
49
50
	/**
51
	 * Determines whether the current locale is right-to-left (RTL).
52
	 *
53
	 * Performs the check against the current locale set on the WordPress.com's account settings.
54
	 * See `Masterbar::__construct` in `modules/masterbar/masterbar/class-masterbar.php`.
55
	 */
56
	public function is_rtl() {
57
		return get_user_option( 'jetpack_wpcom_is_rtl' );
58
	}
59
60
	/**
61
	 * Create the desired menu output.
62
	 */
63 View Code Duplication
	public function reregister_menu_items() {
64
		parent::reregister_menu_items();
65
66
		$this->add_my_home_menu();
67
68
		// Not needed outside of wp-admin.
69
		if ( ! $this->is_api_request ) {
70
			$this->add_browse_sites_link();
71
			$this->add_site_card_menu();
72
			$this->add_new_site_link();
73
		}
74
75
		ksort( $GLOBALS['menu'] );
76
	}
77
78
	/**
79
	 * Forces Posts menu to WPAdmin for Atomic sites only.
80
	 * Overloads `add_posts_menu` in parent class.
81
	 *
82
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
83
	 */
84
	public function add_posts_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
85
		return false; // return explicit `false` to force WPAdmin links.
86
	}
87
88
	/**
89
	 * Forces Pages menu to WPAdmin for Atomic sites only.
90
	 * Overloads `add_page_menu` in parent class.
91
	 *
92
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
93
	 */
94
	public function add_page_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
95
		return false; // return explicit `false` to force WPAdmin links.
96
	}
97
98
	/**
99
	 * Adds Plugins menu.
100
	 *
101
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
102
	 */
103
	public function add_plugins_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
104
		// Plugins on Atomic sites are always managed on WP Admin.
105
		parent::add_plugins_menu( true );
106
	}
107
108
	/**
109
	 * Adds the site switcher link if user has more than one site.
110
	 */
111
	public function add_browse_sites_link() {
112
		$site_count = get_user_option( 'wpcom_site_count' );
113
		if ( ! $site_count || $site_count < 2 ) {
114
			return;
115
		}
116
117
		// Add the menu item.
118
		add_menu_page( __( 'site-switcher', 'jetpack' ), __( 'Browse sites', 'jetpack' ), 'read', 'https://wordpress.com/home', null, 'dashicons-arrow-left-alt2', 0 );
119
		add_filter( 'add_menu_classes', array( $this, 'set_browse_sites_link_class' ) );
120
	}
121
122
	/**
123
	 * Adds a custom element class for Site Switcher menu item.
124
	 *
125
	 * @param array $menu Associative array of administration menu items.
126
	 *
127
	 * @return array
128
	 */
129 View Code Duplication
	public function set_browse_sites_link_class( array $menu ) {
130
		foreach ( $menu as $key => $menu_item ) {
131
			if ( 'site-switcher' !== $menu_item[3] ) {
132
				continue;
133
			}
134
135
			$menu[ $key ][4] = add_cssclass( 'site-switcher', $menu_item[4] );
136
			break;
137
		}
138
139
		return $menu;
140
	}
141
142
	/**
143
	 * Adds a link to the menu to create a new site.
144
	 */
145
	public function add_new_site_link() {
146
		$site_count = get_user_option( 'wpcom_site_count' );
147
		if ( $site_count && $site_count > 1 ) {
148
			return;
149
		}
150
151
		$this->add_admin_menu_separator();
152
		add_menu_page( __( 'Add New Site', 'jetpack' ), __( 'Add New Site', 'jetpack' ), 'read', 'https://wordpress.com/start?ref=calypso-sidebar', null, 'dashicons-plus-alt' );
153
	}
154
155
	/**
156
	 * Adds site card component.
157
	 */
158
	public function add_site_card_menu() {
159
		$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>' );
160
		$icon      = get_site_icon_url( 32, $default );
161
		$blog_name = get_option( 'blogname' ) !== '' ? get_option( 'blogname' ) : $this->domain;
162
163
		$badge = '';
164
		if ( function_exists( 'site_is_private' ) && site_is_private() ) {
165
			$badge .= sprintf(
166
				'<span class="site__badge site__badge-private">%s</span>',
167
				site_is_coming_soon() ? esc_html__( 'Coming Soon', 'jetpack' ) : esc_html__( 'Private', 'jetpack' )
168
			);
169
		}
170
171
		$site_card = '
172
<div class="site__info">
173
	<div class="site__title">%1$s</div>
174
	<div class="site__domain">%2$s</div>
175
	%3$s
176
</div>';
177
178
		$site_card = sprintf(
179
			$site_card,
180
			$blog_name,
181
			$this->domain,
182
			$badge
183
		);
184
185
		add_menu_page( 'site-card', $site_card, 'read', get_home_url(), null, $icon, 1 );
186
		add_filter( 'add_menu_classes', array( $this, 'set_site_card_menu_class' ) );
187
	}
188
189
	/**
190
	 * Adds a custom element class and id for Site Card's menu item.
191
	 *
192
	 * @param array $menu Associative array of administration menu items.
193
	 *
194
	 * @return array
195
	 */
196
	public function set_site_card_menu_class( array $menu ) {
197
		foreach ( $menu as $key => $menu_item ) {
198
			if ( 'site-card' !== $menu_item[3] ) {
199
				continue;
200
			}
201
202
			$classes = ' toplevel_page_site-card';
203
204
			// webclip.png is the default on WoA sites. Anything other than that means we have a custom site icon.
205
			if ( has_site_icon() && 'https://s0.wp.com/i/webclip.png' !== get_site_icon_url( 512 ) ) {
206
				$classes .= ' has-site-icon';
207
			}
208
209
			$menu[ $key ][4] = $menu_item[4] . $classes;
210
			$menu[ $key ][5] = 'toplevel_page_site_card';
211
			break;
212
		}
213
214
		return $menu;
215
	}
216
217
	/**
218
	 * Adds Upgrades menu.
219
	 *
220
	 * @param string $plan The current WPCOM plan of the blog.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $plan not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
221
	 */
222
	public function add_upgrades_menu( $plan = null ) {
223
		$products = Jetpack_Plan::get();
224
		if ( array_key_exists( 'product_name_short', $products ) ) {
225
			$plan = $products['product_name_short'];
226
		}
227
		parent::add_upgrades_menu( $plan );
228
229
		add_submenu_page( 'paid-upgrades.php', __( 'Domains', 'jetpack' ), __( 'Domains', 'jetpack' ), 'manage_options', 'https://wordpress.com/domains/manage/' . $this->domain, null, 10 );
230
	}
231
232
	/**
233
	 * Adds Tools menu.
234
	 *
235
	 * @param bool $wp_admin_import Optional. Whether Import link should point to Calypso or wp-admin. Default false (Calypso).
236
	 * @param bool $wp_admin_export Optional. Whether Export link should point to Calypso or wp-admin. Default false (Calypso).
237
	 */
238
	public function add_tools_menu( $wp_admin_import = false, $wp_admin_export = false ) {  // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
239
		// Export on Atomic sites is always handled on WP Admin.
240
		parent::add_tools_menu( $wp_admin_import, true );
241
	}
242
243
	/**
244
	 * Adds Settings menu.
245
	 *
246
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
247
	 */
248
	public function add_options_menu( $wp_admin = false ) {
249
		parent::add_options_menu( $wp_admin );
250
251
		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 );
252
253
		// No need to add a menu linking to WP Admin if there is already one.
254
		if ( ! $wp_admin ) {
255
			add_submenu_page( 'options-general.php', esc_attr__( 'Advanced General', 'jetpack' ), __( 'Advanced General', 'jetpack' ), 'manage_options', 'options-general.php' );
256
			add_submenu_page( 'options-general.php', esc_attr__( 'Advanced Writing', 'jetpack' ), __( 'Advanced Writing', 'jetpack' ), 'manage_options', 'options-writing.php' );
257
		}
258
	}
259
260
	/**
261
	 * Adds Appearance menu.
262
	 *
263
	 * @param bool $wp_admin_themes Optional. Whether Themes link should point to Calypso or wp-admin. Default false (Calypso).
264
	 * @param bool $wp_admin_customize Optional. Whether Customize link should point to Calypso or wp-admin. Default false (Calypso).
265
	 */
266
	public function add_appearance_menu( $wp_admin_themes = false, $wp_admin_customize = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
267
		// Customize on Atomic sites is always done on WP Admin.
268
		parent::add_appearance_menu( $wp_admin_themes, true );
269
270
		add_submenu_page( 'themes.php', esc_attr__( 'Add New Theme', 'jetpack' ), __( 'Add New Theme', 'jetpack' ), 'install_themes', 'theme-install.php', null, 1 );
271
	}
272
273
	/**
274
	 * Override the global submenu_file for theme-install.php page so the WP Admin menu item gets highlighted correctly.
275
	 *
276
	 * @param string $submenu_file The current pages $submenu_file global variable value.
277
	 * @return string | null
278
	 */
279
	public function override_the_theme_installer( $submenu_file ) {
280
		global $pagenow;
281
282
		if ( 'themes.php' === $submenu_file && 'theme-install.php' === $pagenow ) {
283
			return null;
284
		}
285
		return $submenu_file;
286
	}
287
288
	/**
289
	 * Adds Users menu.
290
	 *
291
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
292
	 */
293
	public function add_users_menu( $wp_admin = false ) {
294
		parent::add_users_menu( $wp_admin );
295
296
		add_submenu_page( 'users.php', esc_attr__( 'Advanced Users Management', 'jetpack' ), __( 'Advanced Users Management', 'jetpack' ), 'list_users', 'users.php', null, 2 );
297
	}
298
299
	/**
300
	 * Also remove the Gutenberg plugin menu.
301
	 *
302
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
303
	 */
304
	public function add_gutenberg_menus( $wp_admin = false ) {
305
		// Always remove the Gutenberg menu.
306
		remove_menu_page( 'gutenberg' );
307
		parent::add_gutenberg_menus( $wp_admin );
308
	}
309
310
	/**
311
	 * Saves the sidebar state ( expanded / collapsed ) via an ajax request.
312
	 */
313
	public function ajax_sidebar_state() {
314
		$expanded = filter_var( $_REQUEST['expanded'], FILTER_VALIDATE_BOOLEAN ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
315
		Client::wpcom_json_api_request_as_user(
316
			'/me/preferences',
317
			'2',
318
			array(
319
				'method' => 'POST',
320
			),
321
			(object) array( 'calypso_preferences' => (object) array( 'sidebarCollapsed' => ! $expanded ) ),
322
			'wpcom'
323
		);
324
325
		wp_die();
326
	}
327
}
328
329