Completed
Push — update/remove-jquery-from-caro... ( 612414...19534f )
by
unknown
31:56 queued 18:03
created

profile-edit.php ➔ jetpack_masterbar_hide_profile_fields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 49
rs 9.1127
c 0
b 0
f 0
1
<?php
2
/**
3
 * WP-Admin Profile edit.
4
 *
5
 * @package Jetpack
6
 */
7
8
use Automattic\Jetpack\Connection\Manager as Connection_Manager;
9
10
/**
11
 * Hides profile fields for WordPress.com connected users.
12
 *
13
 * @param WP_User $user The current WP_User object.
14
 */
15
function jetpack_masterbar_hide_profile_fields( $user ) {
16
	$connection_manager = new Connection_Manager( 'jetpack' );
17
	if ( ! $connection_manager->is_user_connected( $user->ID ) ) {
18
		// If this is a local user, show the default UX.
19
		return;
20
	}
21
	$wp_kses_rule = array(
22
		'a' => array(
23
			'href'   => array(),
24
			'rel'    => array(),
25
			'target' => array(),
26
		),
27
	);
28
	// Since there is no hook for altering profile fields, we will use CSS and JS.
29
	$name_info_wpcom_link_message = sprintf(
30
		/* translators: 1 link */
31
		__( 'WordPress.com users can change their profile’s basic details ( i.e., First Name, Last Name, Display Name, About ) in <a href="%1$s" target="_blank" rel="noopener noreferrer">WordPress.com Profile settings.</a>', 'jetpack' ),
32
		'https://wordpress.com/me'
33
	);
34
	$contact_info_wpcom_link_message = sprintf(
35
		/* translators: 1 link */
36
		__( 'WordPress.com users can change their profile’s email & website address in <a href="%1$s" target="_blank" rel="noopener noreferrer">WordPress.com Account settings.</a>', 'jetpack' ),
37
		'https://wordpress.com/me/account'
38
	);
39
	?>
40
	<script>
41
		document.addEventListener( 'DOMContentLoaded', function() {
42
			// Field to be hidden.
43
			var fieldsToHide = '.user-first-name-wrap, .user-last-name-wrap, .user-nickname-wrap, .user-display-name-wrap, .user-email-wrap, .user-url-wrap, .user-description-wrap';
44
			document.querySelectorAll( fieldsToHide ).forEach( element => element.classList.add( 'hidden' ) );
45
46
			// Name Info.
47
			var nameInfo                    = document.querySelector( '.user-first-name-wrap' ).closest( 'table' );
48
			var nameInfoWpcomLink           = document.createElement( 'div' );
49
				nameInfoWpcomLink.className = 'notice inline notice-large notice-warning';
50
				nameInfoWpcomLink.innerHTML = '<?php echo wp_kses( $name_info_wpcom_link_message, $wp_kses_rule ); ?>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
51
			nameInfo.parentNode.insertBefore( nameInfoWpcomLink, nameInfo.nextSibling );
52
53
			// Contact Info.
54
			var contactInfo                    = document.querySelector( '.user-email-wrap' ).closest( 'table' );
55
			var contactInfoWpcomLink           = document.createElement( 'div' );
56
				contactInfoWpcomLink.className = 'notice inline notice-large notice-warning';
57
				contactInfoWpcomLink.innerHTML = '<?php echo wp_kses( $contact_info_wpcom_link_message, $wp_kses_rule ); ?>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
58
			contactInfo.parentNode.insertBefore( contactInfoWpcomLink, contactInfo.nextSibling );
59
		});
60
61
	</script>
62
	<?php
63
}
64
65
add_action( 'personal_options', 'jetpack_masterbar_hide_profile_fields' );
66