Completed
Push — update/admin-menu-sso-disabled ( 39bf4b...eefb5e )
by
unknown
10:52
created

Atomic_Admin_Menu::add_site_card_menu()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

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