Completed
Push — renovate/jest-monorepo ( bbafd9...36469d )
by
unknown
33:09 queued 24:56
created

A8C_WPCOM_Masterbar::wpcom_static_url()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
1
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
3
require_once dirname( __FILE__ ) . '/rtl-admin-bar.php';
4
5
/**
6
 * Custom Admin bar displayed instead of the default WordPress admin bar.
7
 */
8
class A8C_WPCOM_Masterbar {
9
	/**
10
	 * Use for testing changes made to remotely enqueued scripts and styles on your sandbox.
11
	 * If not set it will default to loading the ones from WordPress.com.
12
	 *
13
	 * @var string $sandbox_url
14
	 */
15
	private $sandbox_url = '';
16
17
	/**
18
	 * Current locale.
19
	 *
20
	 * @var string
21
	 */
22
	private $locale;
23
24
	/**
25
	 * Current User ID.
26
	 *
27
	 * @var int
28
	 */
29
	private $user_id;
30
	/**
31
	 * WordPress.com user data of the connected user.
32
	 *
33
	 * @var array
34
	 */
35
	private $user_data;
36
	/**
37
	 * WordPress.com username for the connected user.
38
	 *
39
	 * @var string
40
	 */
41
	private $user_login;
42
	/**
43
	 * WordPress.com email address for the connected user.
44
	 *
45
	 * @var string
46
	 */
47
	private $user_email;
48
	/**
49
	 * WordPress.com display name for the connected user.
50
	 *
51
	 * @var string
52
	 */
53
	private $display_name;
54
	/**
55
	 * Site URL sanitized for usage in WordPress.com slugs.
56
	 *
57
	 * @var string
58
	 */
59
	private $primary_site_slug;
60
	/**
61
	 * Text direction (ltr or rtl) based on connected WordPress.com user's interface settings.
62
	 *
63
	 * @var string
64
	 */
65
	private $user_text_direction;
66
	/**
67
	 * Number of sites owned by connected WordPress.com user.
68
	 *
69
	 * @var int
70
	 */
71
	private $user_site_count;
72
73
	/**
74
	 * Constructor
75
	 */
76
	public function __construct() {
77
		$this->locale  = $this->get_locale();
78
		$this->user_id = get_current_user_id();
79
80
		// Limit the masterbar to be shown only to connected Jetpack users.
81
		if ( ! Jetpack::is_user_connected( $this->user_id ) ) {
82
			return;
83
		}
84
85
		// Don't show the masterbar on WordPress mobile apps.
86
		if ( Jetpack_User_Agent_Info::is_mobile_app() ) {
87
			add_filter( 'show_admin_bar', '__return_false' );
88
			return;
89
		}
90
91
		Jetpack::dns_prefetch(
92
			array(
93
				'//s0.wp.com',
94
				'//s1.wp.com',
95
				'//s2.wp.com',
96
				'//0.gravatar.com',
97
				'//1.gravatar.com',
98
				'//2.gravatar.com',
99
			)
100
		);
101
102
		// Atomic only.
103
		if ( jetpack_is_atomic_site() ) {
104
			/*
105
			 * override user setting that hides masterbar from site's front.
106
			 * https://github.com/Automattic/jetpack/issues/7667
107
			 */
108
			add_filter( 'show_admin_bar', '__return_true' );
109
		}
110
111
		$this->user_data       = Jetpack::get_connected_user_data( $this->user_id );
112
		$this->user_login      = $this->user_data['login'];
113
		$this->user_email      = $this->user_data['email'];
114
		$this->display_name    = $this->user_data['display_name'];
115
		$this->user_site_count = $this->user_data['site_count'];
116
117
		// Used to build menu links that point directly to Calypso.
118
		$this->primary_site_slug = Jetpack::build_raw_urls( get_home_url() );
119
120
		// Used for display purposes and for building WP Admin links.
121
		$this->primary_site_url = str_replace( '::', '/', $this->primary_site_slug );
0 ignored issues
show
Bug introduced by
The property primary_site_url does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
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
		// Local overrides.
194
		wp_enqueue_style( 'a8c_wpcom_css_override', plugins_url( 'overrides.css', __FILE__ ), array(), JETPACK__VERSION );
195
196
		if ( ! Jetpack::is_module_active( 'notes ' ) ) {
197
			// Masterbar is relying on some icons from noticons.css.
198
			wp_enqueue_style( 'noticons', $this->wpcom_static_url( '/i/noticons/noticons.css' ), array(), JETPACK__VERSION . '-' . gmdate( 'oW' ) );
199
		}
200
201
		wp_enqueue_script(
202
			'jetpack-accessible-focus',
203
			Jetpack::get_file_url_for_environment( '_inc/build/accessible-focus.min.js', '_inc/accessible-focus.js' ),
204
			array(),
205
			JETPACK__VERSION,
206
			false
207
		);
208
		wp_enqueue_script(
209
			'a8c_wpcom_masterbar_tracks_events',
210
			Jetpack::get_file_url_for_environment(
211
				'_inc/build/masterbar/tracks-events.min.js',
212
				'modules/masterbar/tracks-events.js'
213
			),
214
			array( 'jquery' ),
215
			JETPACK__VERSION,
216
			false
217
		);
218
219
		wp_enqueue_script(
220
			'a8c_wpcom_masterbar_overrides',
221
			$this->wpcom_static_url( '/wp-content/mu-plugins/admin-bar/masterbar-overrides/masterbar.js' ),
222
			array( 'jquery' ),
223
			JETPACK__VERSION,
224
			false
225
		);
226
	}
227
228
	/**
229
	 * Get base URL where our CSS and JS will come from.
230
	 *
231
	 * @param string $file File path for a static resource.
232
	 */
233
	private function wpcom_static_url( $file ) {
234
		if ( ! empty( $this->sandbox_url ) ) {
235
			// For testing undeployed changes to remotely enqueued scripts and styles.
236
			return set_url_scheme( $this->sandbox_url . $file, 'https' );
237
		}
238
239
		$i   = hexdec( substr( md5( $file ), - 1 ) ) % 2;
240
		$url = 'https://s' . $i . '.wp.com' . $file;
241
242
		return set_url_scheme( $url, 'https' );
243
	}
244
245
	/**
246
	 * Remove the default admin bar items and replace it with our own admin bar.
247
	 */
248
	public function replace_core_masterbar() {
249
		global $wp_admin_bar;
250
251
		if ( ! is_object( $wp_admin_bar ) ) {
252
			return false;
253
		}
254
255
		$this->clear_core_masterbar( $wp_admin_bar );
256
		$this->build_wpcom_masterbar( $wp_admin_bar );
257
	}
258
259
	/**
260
	 * Remove all existing toolbar entries from core Masterbar
261
	 *
262
	 * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance.
263
	 */
264
	public function clear_core_masterbar( $wp_admin_bar ) {
265
		foreach ( $wp_admin_bar->get_nodes() as $node ) {
266
			$wp_admin_bar->remove_node( $node->id );
267
		}
268
	}
269
270
	/**
271
	 * Add entries corresponding to WordPress.com Masterbar
272
	 *
273
	 * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance.
274
	 */
275
	public function build_wpcom_masterbar( $wp_admin_bar ) {
276
		// Menu groups.
277
		$this->wpcom_adminbar_add_secondary_groups( $wp_admin_bar );
278
279
		// Left part.
280
		$this->add_my_sites_submenu( $wp_admin_bar );
281
		$this->add_reader_submenu( $wp_admin_bar );
282
283
		// Right part.
284
		if ( Jetpack::is_module_active( 'notes' ) ) {
285
			$this->add_notifications( $wp_admin_bar );
286
		}
287
288
		$this->add_me_submenu( $wp_admin_bar );
289
		$this->add_write_button( $wp_admin_bar );
290
	}
291
292
	/**
293
	 * Get WordPress.com current locale name.
294
	 */
295
	public function get_locale() {
296
		$wpcom_locale = get_locale();
297
298
		if ( ! class_exists( 'GP_Locales' ) ) {
299
			if ( defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) && file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) {
300
				require JETPACK__GLOTPRESS_LOCALES_PATH;
301
			}
302
		}
303
304 View Code Duplication
		if ( class_exists( 'GP_Locales' ) ) {
305
			$wpcom_locale_object = GP_Locales::by_field( 'wp_locale', get_locale() );
306
			if ( $wpcom_locale_object instanceof GP_Locale ) {
307
				$wpcom_locale = $wpcom_locale_object->slug;
308
			}
309
		}
310
311
		return $wpcom_locale;
312
	}
313
314
	/**
315
	 * Add the Notifications menu item.
316
	 *
317
	 * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance.
318
	 */
319
	public function add_notifications( $wp_admin_bar ) {
320
		$wp_admin_bar->add_node(
321
			array(
322
				'id'     => 'notes',
323
				'title'  => '<span id="wpnt-notes-unread-count" class="wpnt-loading wpn-read"></span>
324
						 <span class="screen-reader-text">' . esc_html__( 'Notifications', 'jetpack' ) . '</span>
325
						 <span class="noticon noticon-bell"></span>',
326
				'meta'   => array(
327
					'html'  => '<div id="wpnt-notes-panel2" style="display:none" lang="' . esc_attr( $this->locale ) . '" dir="' . ( $this->is_rtl() ? 'rtl' : 'ltr' ) . '">' .
328
								'<div class="wpnt-notes-panel-header">' .
329
								'<span class="wpnt-notes-header">' .
330
								esc_html__( 'Notifications', 'jetpack' ) .
331
								'</span>' .
332
								'<span class="wpnt-notes-panel-link">' .
333
								'</span>' .
334
								'</div>' .
335
								'</div>',
336
					'class' => 'menupop mb-trackable',
337
				),
338
				'parent' => 'top-secondary',
339
			)
340
		);
341
	}
342
343
	/**
344
	 * Add the "My Site" menu item in the root default group.
345
	 *
346
	 * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance.
347
	 */
348
	public function add_my_sites_submenu( $wp_admin_bar ) {
349
		$wp_admin_bar->add_menu(
350
			array(
351
				'parent' => 'root-default',
352
				'id'     => 'blog',
353
				'title'  => _n( 'My Site', 'My Sites', $this->user_site_count, 'jetpack' ),
354
				'href'   => 'https://wordpress.com/stats/' . esc_attr( $this->primary_site_slug ),
355
				'meta'   => array(
356
					'class' => 'my-sites mb-trackable',
357
				),
358
			)
359
		);
360
	}
361
362
	/**
363
	 * Add the "Reader" menu item in the root default group.
364
	 *
365
	 * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance.
366
	 */
367
	public function add_reader_submenu( $wp_admin_bar ) {
368
		$wp_admin_bar->add_menu(
369
			array(
370
				'parent' => 'root-default',
371
				'id'     => 'newdash',
372
				'title'  => esc_html__( 'Reader', 'jetpack' ),
373
				'href'   => 'https://wordpress.com/',
374
				'meta'   => array(
375
					'class' => 'mb-trackable',
376
				),
377
			)
378
		);
379
	}
380
381
	/**
382
	 * Define main groups used in our admin bar.
383
	 *
384
	 * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance.
385
	 */
386
	public function wpcom_adminbar_add_secondary_groups( $wp_admin_bar ) {
387
		$wp_admin_bar->add_group(
388
			array(
389
				'id'   => 'root-default',
390
				'meta' => array(
391
					'class' => 'ab-top-menu',
392
				),
393
			)
394
		);
395
396
		$wp_admin_bar->add_group(
397
			array(
398
				'id'   => 'top-secondary',
399
				'meta' => array(
400
					'class' => 'ab-top-secondary',
401
				),
402
			)
403
		);
404
	}
405
406
	/**
407
	 * Add User info menu item.
408
	 *
409
	 * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance.
410
	 */
411
	public function add_me_submenu( $wp_admin_bar ) {
412
		$user_id = get_current_user_id();
413
		if ( empty( $user_id ) ) {
414
			return;
415
		}
416
417
		$avatar = get_avatar( $this->user_email, 32, 'mm', '', array( 'force_display' => true ) );
418
		$class  = empty( $avatar ) ? 'mb-trackable' : 'with-avatar mb-trackable';
419
420
		// Add the 'Me' menu.
421
		$wp_admin_bar->add_menu(
422
			array(
423
				'id'     => 'my-account',
424
				'parent' => 'top-secondary',
425
				'title'  => $avatar . '<span class="ab-text">' . esc_html__( 'Me', 'jetpack' ) . '</span>',
426
				'href'   => 'https://wordpress.com/me/account',
427
				'meta'   => array(
428
					'class' => $class,
429
				),
430
			)
431
		);
432
	}
433
434
	/**
435
	 * Add Write Menu item.
436
	 *
437
	 * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance.
438
	 */
439
	public function add_write_button( $wp_admin_bar ) {
440
		$current_user = wp_get_current_user();
441
442
		$posting_blog_id = get_current_blog_id();
443
		if ( ! is_user_member_of_blog( get_current_user_id(), get_current_blog_id() ) ) {
444
			$posting_blog_id = $current_user->primary_blog;
445
		}
446
447
		$user_can_post = current_user_can_for_blog( $posting_blog_id, 'publish_posts' );
448
449
		if ( ! $posting_blog_id || ! $user_can_post ) {
450
			return;
451
		}
452
453
		$blog_post_page = 'https://wordpress.com/post/' . esc_attr( $this->primary_site_slug );
454
455
		$wp_admin_bar->add_menu(
456
			array(
457
				'parent' => 'top-secondary',
458
				'id'     => 'ab-new-post',
459
				'href'   => $blog_post_page,
460
				'title'  => '<span>' . esc_html__( 'Write', 'jetpack' ) . '</span>',
461
				'meta'   => array(
462
					'class' => 'mb-trackable',
463
				),
464
			)
465
		);
466
	}
467
}
468