Completed
Push — update/sync-masterbar-with-dot... ( f62610 )
by
unknown
07:26
created

A8C_WPCOM_Masterbar::init()   B

Complexity

Conditions 9
Paths 11

Size

Total Lines 72

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
nc 11
nop 0
dl 0
loc 72
rs 7.0553
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
3
use Automattic\Jetpack\Assets;
4
5
require_once dirname( __FILE__ ) . '/rtl-admin-bar.php';
6
7
/**
8
 * Custom Admin bar displayed instead of the default WordPress admin bar.
9
 */
10
class A8C_WPCOM_Masterbar {
11
	/**
12
	 * Use for testing changes made to remotely enqueued scripts and styles on your sandbox.
13
	 * If not set it will default to loading the ones from WordPress.com.
14
	 *
15
	 * @var string $sandbox_url
16
	 */
17
	private $sandbox_url = '';
18
19
	/**
20
	 * Current locale.
21
	 *
22
	 * @var string
23
	 */
24
	private $locale;
25
26
	/**
27
	 * Current User ID.
28
	 *
29
	 * @var int
30
	 */
31
	private $user_id;
32
	/**
33
	 * WordPress.com user data of the connected user.
34
	 *
35
	 * @var array
36
	 */
37
	private $user_data;
38
	/**
39
	 * WordPress.com email address for the connected user.
40
	 *
41
	 * @var string
42
	 */
43
	private $user_email;
44
	/**
45
	 * Site URL sanitized for usage in WordPress.com slugs.
46
	 *
47
	 * @var string
48
	 */
49
	private $primary_site_slug;
50
	/**
51
	 * Text direction (ltr or rtl) based on connected WordPress.com user's interface settings.
52
	 *
53
	 * @var string
54
	 */
55
	private $user_text_direction;
56
	/**
57
	 * Number of sites owned by connected WordPress.com user.
58
	 *
59
	 * @var int
60
	 */
61
	private $user_site_count;
62
63
	/**
64
	 * Constructor
65
	 */
66
	public function __construct() {
67
		add_action( 'admin_bar_init', array( $this, 'init' ) );
68
	}
69
70
	/**
71
	 * Initialize our masterbar.
72
	 */
73
	public function init() {
74
		$this->locale  = $this->get_locale();
75
		$this->user_id = get_current_user_id();
76
77
		// Limit the masterbar to be shown only to connected Jetpack users.
78
		if ( ! Jetpack::is_user_connected( $this->user_id ) ) {
79
			return;
80
		}
81
82
		// Don't show the masterbar on WordPress mobile apps.
83
		if ( Jetpack_User_Agent_Info::is_mobile_app() ) {
84
			add_filter( 'show_admin_bar', '__return_false' );
85
			return;
86
		}
87
88
		// Disable the Masterbar on AMP views.
89
		if (
90
			class_exists( 'Jetpack_AMP_Support' )
91
			&& Jetpack_AMP_Support::is_amp_request()
92
		) {
93
			return;
94
		}
95
96
		Jetpack::dns_prefetch(
97
			array(
98
				'//s0.wp.com',
99
				'//s1.wp.com',
100
				'//s2.wp.com',
101
				'//0.gravatar.com',
102
				'//1.gravatar.com',
103
				'//2.gravatar.com',
104
			)
105
		);
106
107
		// Atomic only.
108
		if ( jetpack_is_atomic_site() ) {
109
			/*
110
			 * override user setting that hides masterbar from site's front.
111
			 * https://github.com/Automattic/jetpack/issues/7667
112
			 */
113
			add_filter( 'show_admin_bar', '__return_true' );
114
		}
115
116
		$this->user_data       = Jetpack::get_connected_user_data( $this->user_id );
117
		$this->user_email      = $this->user_data['email'];
118
		$this->user_site_count = $this->user_data['site_count'];
119
120
		// Used to build menu links that point directly to Calypso.
121
		$this->primary_site_slug = Jetpack::build_raw_urls( get_home_url() );
122
123
		// We need to use user's setting here, instead of relying on current blog's text direction.
124
		$this->user_text_direction = $this->user_data['text_direction'];
125
126
		if ( $this->is_rtl() ) {
127
			// Extend core WP_Admin_Bar class in order to add rtl styles.
128
			add_filter( 'wp_admin_bar_class', array( $this, 'get_rtl_admin_bar_class' ) );
129
		}
130
		add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) );
131
132
		add_action( 'wp_before_admin_bar_render', array( $this, 'replace_core_masterbar' ), 99999 );
133
134
		add_action( 'wp_enqueue_scripts', array( $this, 'add_styles_and_scripts' ) );
135
		add_action( 'admin_enqueue_scripts', array( $this, 'add_styles_and_scripts' ) );
136
137
		add_action( 'wp_enqueue_scripts', array( $this, 'remove_core_styles' ) );
138
		add_action( 'admin_enqueue_scripts', array( $this, 'remove_core_styles' ) );
139
140
		if ( Jetpack::is_module_active( 'notes' ) && $this->is_rtl() ) {
141
			// Override Notification module to include RTL styles.
142
			add_action( 'a8c_wpcom_masterbar_enqueue_rtl_notification_styles', '__return_true' );
143
		}
144
	}
145
146
	/**
147
	 * Get class name for RTL sites.
148
	 */
149
	public function get_rtl_admin_bar_class() {
150
		return 'RTL_Admin_Bar';
151
	}
152
153
	/**
154
	 * Adds CSS classes to admin body tag.
155
	 *
156
	 * @since 5.1
157
	 *
158
	 * @param string $admin_body_classes CSS classes that will be added.
159
	 *
160
	 * @return string
161
	 */
162
	public function admin_body_class( $admin_body_classes ) {
163
		return "$admin_body_classes jetpack-masterbar";
164
	}
165
166
	/**
167
	 * Remove the default Admin Bar CSS.
168
	 */
169
	public function remove_core_styles() {
170
		wp_dequeue_style( 'admin-bar' );
171
	}
172
173
	/**
174
	 * Check if the user settings are for an RTL language or not.
175
	 */
176
	public function is_rtl() {
177
		return 'rtl' === $this->user_text_direction ? true : false;
178
	}
179
180
	/**
181
	 * Enqueue our own CSS and JS to display our custom admin bar.
182
	 */
183
	public function add_styles_and_scripts() {
184
185
		if ( $this->is_rtl() ) {
186
			wp_enqueue_style( 'a8c-wpcom-masterbar-rtl', $this->wpcom_static_url( '/wp-content/mu-plugins/admin-bar/rtl/wpcom-admin-bar-rtl.css' ), array(), JETPACK__VERSION );
187
			wp_enqueue_style( 'a8c-wpcom-masterbar-overrides-rtl', $this->wpcom_static_url( '/wp-content/mu-plugins/admin-bar/masterbar-overrides/rtl/masterbar-rtl.css' ), array(), JETPACK__VERSION );
188
		} else {
189
			wp_enqueue_style( 'a8c-wpcom-masterbar', $this->wpcom_static_url( '/wp-content/mu-plugins/admin-bar/wpcom-admin-bar.css' ), array(), JETPACK__VERSION );
190
			wp_enqueue_style( 'a8c-wpcom-masterbar-overrides', $this->wpcom_static_url( '/wp-content/mu-plugins/admin-bar/masterbar-overrides/masterbar.css' ), array(), JETPACK__VERSION );
191
		}
192
193
		if ( ! Jetpack::is_module_active( 'notes ' ) ) {
194
			// Masterbar is relying on some icons from noticons.css.
195
			wp_enqueue_style( 'noticons', $this->wpcom_static_url( '/i/noticons/noticons.css' ), array(), JETPACK__VERSION . '-' . gmdate( 'oW' ) );
196
		}
197
198
		wp_enqueue_script(
199
			'jetpack-accessible-focus',
200
			Assets::get_file_url_for_environment( '_inc/build/accessible-focus.min.js', '_inc/accessible-focus.js' ),
201
			array(),
202
			JETPACK__VERSION,
203
			false
204
		);
205
		wp_enqueue_script(
206
			'a8c_wpcom_masterbar_tracks_events',
207
			Assets::get_file_url_for_environment(
208
				'_inc/build/masterbar/tracks-events.min.js',
209
				'modules/masterbar/tracks-events.js'
210
			),
211
			array( 'jquery' ),
212
			JETPACK__VERSION,
213
			false
214
		);
215
	}
216
217
	/**
218
	 * Get base URL where our CSS and JS will come from.
219
	 *
220
	 * @param string $file File path for a static resource.
221
	 */
222
	private function wpcom_static_url( $file ) {
223
		if ( ! empty( $this->sandbox_url ) ) {
224
			// For testing undeployed changes to remotely enqueued scripts and styles.
225
			return set_url_scheme( $this->sandbox_url . $file, 'https' );
226
		}
227
228
		$i   = hexdec( substr( md5( $file ), - 1 ) ) % 2;
229
		$url = 'https://s' . $i . '.wp.com' . $file;
230
231
		return set_url_scheme( $url, 'https' );
232
	}
233
234
	/**
235
	 * Remove the default admin bar items and replace it with our own admin bar.
236
	 */
237
	public function replace_core_masterbar() {
238
		global $wp_admin_bar;
239
240
		if ( ! is_object( $wp_admin_bar ) ) {
241
			return false;
242
		}
243
244
		$this->clear_core_masterbar( $wp_admin_bar );
245
		$this->build_wpcom_masterbar( $wp_admin_bar );
246
	}
247
248
	/**
249
	 * Remove all existing toolbar entries from core Masterbar
250
	 *
251
	 * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance.
252
	 */
253
	public function clear_core_masterbar( $wp_admin_bar ) {
254
		foreach ( $wp_admin_bar->get_nodes() as $node ) {
255
			$wp_admin_bar->remove_node( $node->id );
256
		}
257
	}
258
259
	/**
260
	 * Add entries corresponding to WordPress.com Masterbar
261
	 *
262
	 * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance.
263
	 */
264
	public function build_wpcom_masterbar( $wp_admin_bar ) {
265
		// Menu groups.
266
		$this->wpcom_adminbar_add_secondary_groups( $wp_admin_bar );
267
268
		// Left part.
269
		$this->add_my_sites_submenu( $wp_admin_bar );
270
		$this->add_reader_submenu( $wp_admin_bar );
271
272
		// Right part.
273
		if ( Jetpack::is_module_active( 'notes' ) ) {
274
			$this->add_notifications( $wp_admin_bar );
275
		}
276
277
		$this->add_me_submenu( $wp_admin_bar );
278
		$this->add_write_button( $wp_admin_bar );
279
280
		// Recovery mode exit.
281
		if ( function_exists( 'wp_admin_bar_recovery_mode_menu' ) ) {
282
			wp_admin_bar_recovery_mode_menu( $wp_admin_bar );
283
		}
284
	}
285
286
	/**
287
	 * Get WordPress.com current locale name.
288
	 */
289
	public function get_locale() {
290
		$wpcom_locale = get_locale();
291
292
		if ( ! class_exists( 'GP_Locales' ) ) {
293
			if ( defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) && file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) {
294
				require JETPACK__GLOTPRESS_LOCALES_PATH;
295
			}
296
		}
297
298 View Code Duplication
		if ( class_exists( 'GP_Locales' ) ) {
299
			$wpcom_locale_object = GP_Locales::by_field( 'wp_locale', get_locale() );
300
			if ( $wpcom_locale_object instanceof GP_Locale ) {
301
				$wpcom_locale = $wpcom_locale_object->slug;
302
			}
303
		}
304
305
		return $wpcom_locale;
306
	}
307
308
	/**
309
	 * Add the Notifications menu item.
310
	 *
311
	 * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance.
312
	 */
313
	public function add_notifications( $wp_admin_bar ) {
314
		$wp_admin_bar->add_node(
315
			array(
316
				'id'     => 'notes',
317
				'title'  => '<span id="wpnt-notes-unread-count" class="wpnt-loading wpn-read"></span>
318
						 <span class="screen-reader-text">' . esc_html__( 'Notifications', 'jetpack' ) . '</span>
319
						 <span class="noticon noticon-bell"></span>',
320
				'meta'   => array(
321
					'html'  => '<div id="wpnt-notes-panel2" style="display:none" lang="' . esc_attr( $this->locale ) . '" dir="' . ( $this->is_rtl() ? 'rtl' : 'ltr' ) . '">' .
322
								'<div class="wpnt-notes-panel-header">' .
323
								'<span class="wpnt-notes-header">' .
324
								esc_html__( 'Notifications', 'jetpack' ) .
325
								'</span>' .
326
								'<span class="wpnt-notes-panel-link">' .
327
								'</span>' .
328
								'</div>' .
329
								'</div>',
330
					'class' => 'menupop mb-trackable',
331
				),
332
				'parent' => 'top-secondary',
333
			)
334
		);
335
	}
336
337
	/**
338
	 * Add the "Reader" menu item in the root default group.
339
	 *
340
	 * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance.
341
	 */
342
	public function add_reader_submenu( $wp_admin_bar ) {
343
		$wp_admin_bar->add_menu(
344
			array(
345
				'parent' => 'root-default',
346
				'id'     => 'newdash',
347
				'title'  => esc_html__( 'Reader', 'jetpack' ),
348
				'href'   => 'https://wordpress.com/',
349
				'meta'   => array(
350
					'class' => 'mb-trackable',
351
				),
352
			)
353
		);
354
	}
355
356
	/**
357
	 * Add Secondary groups for submenu items.
358
	 *
359
	 * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance.
360
	 */
361
	public function wpcom_adminbar_add_secondary_groups( $wp_admin_bar ) {
362
		$wp_admin_bar->add_group(
363
			array(
364
				'id'   => 'root-default',
365
				'meta' => array(
366
					'class' => 'ab-top-menu',
367
				),
368
			)
369
		);
370
371
		$wp_admin_bar->add_group(
372
			array(
373
				'parent' => 'blog',
374
				'id'     => 'blog-secondary',
375
				'meta'   => array(
376
					'class' => 'ab-sub-secondary',
377
				),
378
			)
379
		);
380
381
		$wp_admin_bar->add_group(
382
			array(
383
				'id'   => 'top-secondary',
384
				'meta' => array(
385
					'class' => 'ab-top-secondary',
386
				),
387
			)
388
		);
389
	}
390
391
	/**
392
	 * Add User info menu item.
393
	 *
394
	 * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance.
395
	 */
396
	public function add_me_submenu( $wp_admin_bar ) {
397
		$user_id = get_current_user_id();
398
		if ( empty( $user_id ) ) {
399
			return;
400
		}
401
402
		$avatar = get_avatar( $this->user_email, 32, 'mm', '', array( 'force_display' => true ) );
403
		$class  = empty( $avatar ) ? 'mb-trackable' : 'with-avatar mb-trackable';
404
405
		// Add the 'Me' menu.
406
		$wp_admin_bar->add_menu(
407
			array(
408
				'id'     => 'my-account',
409
				'parent' => 'top-secondary',
410
				'title'  => $avatar . '<span class="ab-text">' . esc_html__( 'Me', 'jetpack' ) . '</span>',
411
				'href'   => 'https://wordpress.com/me',
412
				'meta'   => array(
413
					'class' => $class,
414
				),
415
			)
416
		);
417
	}
418
419
	/**
420
	 * Add Write Menu item.
421
	 *
422
	 * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance.
423
	 */
424
	public function add_write_button( $wp_admin_bar ) {
425
		$current_user = wp_get_current_user();
426
427
		$posting_blog_id = get_current_blog_id();
428
		if ( ! is_user_member_of_blog( get_current_user_id(), get_current_blog_id() ) ) {
429
			$posting_blog_id = $current_user->primary_blog;
430
		}
431
432
		$user_can_post = current_user_can_for_blog( $posting_blog_id, 'publish_posts' );
433
434
		if ( ! $posting_blog_id || ! $user_can_post ) {
435
			return;
436
		}
437
438
		$wp_admin_bar->add_menu(
439
			array(
440
				'parent' => 'top-secondary',
441
				'id'     => 'ab-new-post',
442
				'href'   => 'https://wordpress.com/post/' . esc_attr( $this->primary_site_slug ),
443
				'title'  => '<span>' . esc_html__( 'Write', 'jetpack' ) . '</span>',
444
				'meta'   => array(
445
					'class' => 'mb-trackable',
446
				),
447
			)
448
		);
449
	}
450
451
	/**
452
	 * Add the "My Site" menu item in the root default group.
453
	 *
454
	 * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance.
455
	 */
456
	public function add_my_sites_submenu( $wp_admin_bar ) {
457
		$stats_page = 'https://wordpress.com/stats/day/' . esc_attr( $this->primary_site_slug );
458
459
		$wp_admin_bar->add_menu(
460
			array(
461
				'parent' => 'root-default',
462
				'id'     => 'blog',
463
				'title'  => _n( 'My Site', 'My Sites', $this->user_site_count, 'jetpack' ),
464
				'href'   => $stats_page,
465
				'meta'   => array(
466
					'class' => 'my-sites mb-trackable',
467
				),
468
			)
469
		);
470
471
		/**
472
		 * Fires when menu items are added to the masterbar "My Sites" menu.
473
		 *
474
		 * @since 5.4.0
475
		 */
476
		do_action( 'jetpack_masterbar' );
477
	}
478
}
479