Completed
Push — update/admin-menu-sso-disabled ( 0c5766...857a7b )
by
unknown
42:30 queued 29:49
created

Atomic_Admin_Menu::add_tools_menu()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 4
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
	 * Whether the SSO module is enabled.
21
	 *
22
	 * @var bool
23
	 */
24
	protected $is_sso_enabled = false;
25
26
	/**
27
	 * Atomic_Admin_Menu constructor.
28
	 */
29
	protected function __construct() {
30
		$this->is_sso_enabled = \Jetpack::is_module_active( 'sso' );
31
32
		parent::__construct();
33
34
		if ( ! $this->should_customize_nav() ) {
35
			return;
36
		}
37
38
		add_action( 'wp_enqueue_scripts', array( $this, 'dequeue_scripts' ), 20 );
39
		add_action( 'admin_enqueue_scripts', array( $this, 'dequeue_scripts' ), 20 );
40
		add_action( 'wp_ajax_sidebar_state', array( $this, 'ajax_sidebar_state' ) );
41
42
		if ( ! $this->is_api_request ) {
43
			add_filter( 'submenu_file', array( $this, 'override_the_theme_installer' ), 10, 2 );
44
		}
45
46
		add_action(
47
			'admin_menu',
48
			function () {
49
				remove_action( 'admin_menu', 'gutenberg_menu', 9 );
50
			},
51
			0
52
		);
53
	}
54
55
	/**
56
	 * Dequeues unnecessary scripts.
57
	 */
58
	public function dequeue_scripts() {
59
		wp_dequeue_script( 'a8c_wpcom_masterbar_overrides' ); // Initially loaded in modules/masterbar/masterbar/class-masterbar.php.
60
	}
61
62
	/**
63
	 * Determines whether the current locale is right-to-left (RTL).
64
	 *
65
	 * Performs the check against the current locale set on the WordPress.com's account settings.
66
	 * See `Masterbar::__construct` in `modules/masterbar/masterbar/class-masterbar.php`.
67
	 */
68
	public function is_rtl() {
69
		return get_user_option( 'jetpack_wpcom_is_rtl' );
70
	}
71
72
	/**
73
	 * Create the desired menu output.
74
	 */
75
	public function reregister_menu_items() {
76
		parent::reregister_menu_items();
77
78
		$this->add_my_home_menu();
79
80
		// Not needed outside of wp-admin.
81
		if ( ! $this->is_api_request ) {
82
			$this->add_browse_sites_link();
83
			$this->add_site_card_menu();
84
			$nudge = $this->get_upsell_nudge();
85
			if ( $nudge ) {
86
				parent::add_upsell_nudge( $nudge );
0 ignored issues
show
Documentation introduced by
$nudge is of type array<string,?,{"content...s_click_cta_name":"?"}>, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Comprehensibility Bug introduced by
It seems like you call parent on a different method (add_upsell_nudge() instead of reregister_menu_items()). Are you sure this is correct? If so, you might want to change this to $this->add_upsell_nudge().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
87
			}
88
			$this->add_new_site_link();
89
		}
90
91
		ksort( $GLOBALS['menu'] );
92
	}
93
94
	/**
95
	 * Whether to use wp-admin pages rather than Calypso.
96
	 *
97
	 * @return bool
98
	 */
99
	public function should_link_to_wp_admin() {
100
		if ( $this->should_force_calypso_links() ) {
101
			return false;
102
		}
103
104
		return parent::should_link_to_wp_admin();
105
	}
106
107
	/**
108
	 * Adds Posts menu.
109
	 *
110
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
111
	 */
112
	public function add_posts_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
113
		if ( $this->should_force_calypso_links() ) {
114
			parent::add_posts_menu( false );
115
		}
116
117
		return false; // return explicit `false` to force WPAdmin links.
118
	}
119
120
	/**
121
	 * Adds Page menu.
122
	 *
123
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
124
	 */
125
	public function add_page_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
126
		if ( $this->should_force_calypso_links() ) {
127
			parent::add_page_menu( false );
128
		}
129
130
		return false; // return explicit `false` to force WPAdmin links.
131
	}
132
133
	/**
134
	 * Adds Plugins menu.
135
	 *
136
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
137
	 */
138
	public function add_plugins_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
139
		// Plugins on Atomic sites are always managed on WP Admin.
140
		parent::add_plugins_menu( true );
141
	}
142
143
	/**
144
	 * Adds the site switcher link if user has more than one site.
145
	 */
146
	public function add_browse_sites_link() {
147
		$site_count = get_user_option( 'wpcom_site_count' );
148
		if ( ! $site_count || $site_count < 2 ) {
149
			return;
150
		}
151
152
		// Add the menu item.
153
		add_menu_page( __( 'site-switcher', 'jetpack' ), __( 'Browse sites', 'jetpack' ), 'read', 'https://wordpress.com/home', null, 'dashicons-arrow-left-alt2', 0 );
154
		add_filter( 'add_menu_classes', array( $this, 'set_browse_sites_link_class' ) );
155
	}
156
157
	/**
158
	 * Adds a custom element class for Site Switcher menu item.
159
	 *
160
	 * @param array $menu Associative array of administration menu items.
161
	 *
162
	 * @return array
163
	 */
164 View Code Duplication
	public function set_browse_sites_link_class( array $menu ) {
165
		foreach ( $menu as $key => $menu_item ) {
166
			if ( 'site-switcher' !== $menu_item[3] ) {
167
				continue;
168
			}
169
170
			$menu[ $key ][4] = add_cssclass( 'site-switcher', $menu_item[4] );
171
			break;
172
		}
173
174
		return $menu;
175
	}
176
177
	/**
178
	 * Adds a link to the menu to create a new site.
179
	 */
180
	public function add_new_site_link() {
181
		$site_count = get_user_option( 'wpcom_site_count' );
182
		if ( $site_count && $site_count > 1 ) {
183
			return;
184
		}
185
186
		$this->add_admin_menu_separator();
187
		add_menu_page( __( 'Add New Site', 'jetpack' ), __( 'Add New Site', 'jetpack' ), 'read', 'https://wordpress.com/start?ref=calypso-sidebar', null, 'dashicons-plus-alt' );
188
	}
189
190
	/**
191
	 * Adds site card component.
192
	 */
193
	public function add_site_card_menu() {
194
		$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>' );
195
		$icon      = get_site_icon_url( 32, $default );
196
		$blog_name = get_option( 'blogname' ) !== '' ? get_option( 'blogname' ) : $this->domain;
197
198
		$badge = '';
199
		if ( function_exists( 'site_is_private' ) && site_is_private() ) {
200
			$badge .= sprintf(
201
				'<span class="site__badge site__badge-private">%s</span>',
202
				site_is_coming_soon() ? esc_html__( 'Coming Soon', 'jetpack' ) : esc_html__( 'Private', 'jetpack' )
203
			);
204
		}
205
206
		$site_card = '
207
<div class="site__info">
208
	<div class="site__title">%1$s</div>
209
	<div class="site__domain">%2$s</div>
210
	%3$s
211
</div>';
212
213
		$site_card = sprintf(
214
			$site_card,
215
			$blog_name,
216
			$this->domain,
217
			$badge
218
		);
219
220
		add_menu_page( 'site-card', $site_card, 'read', get_home_url(), null, $icon, 1 );
221
		add_filter( 'add_menu_classes', array( $this, 'set_site_card_menu_class' ) );
222
	}
223
224
	/**
225
	 * Adds a custom element class and id for Site Card's menu item.
226
	 *
227
	 * @param array $menu Associative array of administration menu items.
228
	 *
229
	 * @return array
230
	 */
231
	public function set_site_card_menu_class( array $menu ) {
232
		foreach ( $menu as $key => $menu_item ) {
233
			if ( 'site-card' !== $menu_item[3] ) {
234
				continue;
235
			}
236
237
			$classes = ' toplevel_page_site-card';
238
239
			// webclip.png is the default on WoA sites. Anything other than that means we have a custom site icon.
240
			if ( has_site_icon() && 'https://s0.wp.com/i/webclip.png' !== get_site_icon_url( 512 ) ) {
241
				$classes .= ' has-site-icon';
242
			}
243
244
			$menu[ $key ][4] = $menu_item[4] . $classes;
245
			$menu[ $key ][5] = 'toplevel_page_site_card';
246
			break;
247
		}
248
249
		return $menu;
250
	}
251
252
	/**
253
	 * Returns the first available upsell nudge.
254
	 *
255
	 * @return array
256
	 */
257
	public function get_upsell_nudge() {
258
		$jitm         = \Automattic\Jetpack\JITMS\JITM::get_instance();
259
		$message_path = 'calypso:sites:sidebar_notice';
260
		$message      = $jitm->get_messages( $message_path, wp_json_encode( array( 'message_path' => $message_path ) ), false );
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode(array('me...ath' => $message_path)) targeting wp_json_encode() can also be of type false; however, Automattic\Jetpack\JITMS...on_JITM::get_messages() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
Security Bug introduced by
It seems like wp_json_encode(array('me...ath' => $message_path)) targeting wp_json_encode() can also be of type false; however, Automattic\Jetpack\JITMS...on_JITM::get_messages() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
261
262
		if ( isset( $message[0] ) ) {
263
			$message = $message[0];
264
			return array(
265
				'content'                      => $message->content->message,
266
				'cta'                          => $message->CTA->message, // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
267
				'link'                         => $message->CTA->link, // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
268
				'tracks_impression_event_name' => $message->tracks->display->name,
269
				'tracks_impression_cta_name'   => $message->tracks->display->props->cta_name,
270
				'tracks_click_event_name'      => $message->tracks->click->name,
271
				'tracks_click_cta_name'        => $message->tracks->click->props->cta_name,
272
			);
273
		}
274
	}
275
276
	/**
277
	 * Adds Upgrades menu.
278
	 *
279
	 * @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...
280
	 */
281
	public function add_upgrades_menu( $plan = null ) {
282
		$products = Jetpack_Plan::get();
283
		if ( array_key_exists( 'product_name_short', $products ) ) {
284
			$plan = $products['product_name_short'];
285
		}
286
		parent::add_upgrades_menu( $plan );
287
288
		$last_upgrade_submenu_position = $this->get_submenu_item_count( 'paid-upgrades.php' );
289
290
		add_submenu_page( 'paid-upgrades.php', __( 'Domains', 'jetpack' ), __( 'Domains', 'jetpack' ), 'manage_options', 'https://wordpress.com/domains/manage/' . $this->domain, null, $last_upgrade_submenu_position - 1 );
291
292
		/**
293
		 * Whether to show the WordPress.com Emails submenu under the main Upgrades menu.
294
		 *
295
		 * @use add_filter( 'jetpack_show_wpcom_upgrades_email_menu', '__return_true' );
296
		 * @module masterbar
297
		 *
298
		 * @since 9.7.0
299
		 *
300
		 * @param bool $show_wpcom_upgrades_email_menu Load the WordPress.com Emails submenu item. Default to false.
301
		 */
302 View Code Duplication
		if ( apply_filters( 'jetpack_show_wpcom_upgrades_email_menu', false ) ) {
303
			add_submenu_page( 'paid-upgrades.php', __( 'Emails', 'jetpack' ), __( 'Emails', 'jetpack' ), 'manage_options', 'https://wordpress.com/email/' . $this->domain, null, $last_upgrade_submenu_position );
304
		}
305
	}
306
307
	/**
308
	 * Adds Tools menu.
309
	 *
310
	 * @param bool $wp_admin_import Optional. Whether Import link should point to Calypso or wp-admin. Default false (Calypso).
311
	 * @param bool $wp_admin_export Optional. Whether Export link should point to Calypso or wp-admin. Default false (Calypso).
312
	 */
313
	public function add_tools_menu( $wp_admin_import = false, $wp_admin_export = false ) {  // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
314
		// Export on Atomic sites is always handled on WP Admin.
315
		parent::add_tools_menu( $wp_admin_import, true );
316
	}
317
318
	/**
319
	 * Adds Settings menu.
320
	 *
321
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
322
	 */
323
	public function add_options_menu( $wp_admin = false ) {
324
		parent::add_options_menu( $wp_admin );
325
326
		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 );
327
328
		// Do not add links to WP Admin when there is already one or if Calypso is forced.
329
		if ( $wp_admin || $this->should_force_calypso_links() ) {
330
			return;
331
		}
332
333
		add_submenu_page( 'options-general.php', esc_attr__( 'Advanced Writing', 'jetpack' ), __( 'Advanced Writing', 'jetpack' ), 'manage_options', 'options-writing.php' );
334
	}
335
336
	/**
337
	 * Adds Appearance menu.
338
	 *
339
	 * @param bool $wp_admin_themes Optional. Whether Themes link should point to Calypso or wp-admin. Default false (Calypso).
340
	 * @param bool $wp_admin_customize Optional. Whether Customize link should point to Calypso or wp-admin. Default false (Calypso).
341
	 */
342 View Code Duplication
	public function add_appearance_menu( $wp_admin_themes = false, $wp_admin_customize = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
343
		// Customize on Atomic sites is always done on WP Admin.
344
		parent::add_appearance_menu( $wp_admin_themes, true );
345
346
		// Do not add links to WP Admin when Calypso is forced.
347
		if ( $this->should_force_calypso_links() ) {
348
			return;
349
		}
350
351
		add_submenu_page( 'themes.php', esc_attr__( 'Add New Theme', 'jetpack' ), __( 'Add New Theme', 'jetpack' ), 'install_themes', 'theme-install.php', null, 1 );
352
	}
353
354
	/**
355
	 * Override the global submenu_file for theme-install.php page so the WP Admin menu item gets highlighted correctly.
356
	 *
357
	 * @param string $submenu_file The current pages $submenu_file global variable value.
358
	 * @return string | null
359
	 */
360
	public function override_the_theme_installer( $submenu_file ) {
361
		global $pagenow;
362
363
		if ( 'themes.php' === $submenu_file && 'theme-install.php' === $pagenow ) {
364
			return null;
365
		}
366
		return $submenu_file;
367
	}
368
369
	/**
370
	 * Adds Users menu.
371
	 *
372
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
373
	 */
374 View Code Duplication
	public function add_users_menu( $wp_admin = false ) {
375
		parent::add_users_menu( $wp_admin );
376
377
		// Do not add links to WP Admin when Calypso is forced.
378
		if ( $this->should_force_calypso_links() ) {
379
			return;
380
		}
381
382
		add_submenu_page( 'users.php', esc_attr__( 'Advanced Users Management', 'jetpack' ), __( 'Advanced Users Management', 'jetpack' ), 'list_users', 'users.php', null, 2 );
383
	}
384
385
	/**
386
	 * Also remove the Gutenberg plugin menu.
387
	 *
388
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
389
	 */
390
	public function add_gutenberg_menus( $wp_admin = false ) {
391
		// Always remove the Gutenberg menu.
392
		remove_menu_page( 'gutenberg' );
393
		parent::add_gutenberg_menus( $wp_admin );
394
	}
395
396
	/**
397
	 * Adds Comments menu.
398
	 *
399
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
400
	 */
401
	public function add_comments_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
402
		// If Calypso is not forced, then always use WP Admin.
403
		parent::add_comments_menu( ! $this->should_force_calypso_links() );
404
	}
405
406
	/**
407
	 * Saves the sidebar state ( expanded / collapsed ) via an ajax request.
408
	 */
409
	public function ajax_sidebar_state() {
410
		$expanded = filter_var( $_REQUEST['expanded'], FILTER_VALIDATE_BOOLEAN ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
411
		Client::wpcom_json_api_request_as_user(
412
			'/me/preferences',
413
			'2',
414
			array(
415
				'method' => 'POST',
416
			),
417
			(object) array( 'calypso_preferences' => (object) array( 'sidebarCollapsed' => ! $expanded ) ),
418
			'wpcom'
419
		);
420
421
		wp_die();
422
	}
423
424
	/**
425
	 * Checks whether Calypso links should be enforced.
426
	 *
427
	 * @return bool
428
	 */
429
	public function should_force_calypso_links() {
430
		// Force Calypso links on API requests when SSO is disabled.
431
		return $this->is_api_request && ! $this->is_sso_enabled;
432
	}
433
434
	/**
435
	 * Checks whether the navigation customizations should be performed.
436
	 *
437
	 * @return bool
438
	 */
439
	public function should_customize_nav() {
440
		// No nav overrides on WP Admin when the SSO module is disabled.
441
		return $this->is_api_request || $this->is_sso_enabled;
442
	}
443
}
444