Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 1
CRAP
n/a
0 / 0
Automattic\Jetpack\Masterbar\jetpack_masterbar_hide_profile_fields
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * WP-Admin Profile edit.
4 *
5 * @package automattic/jetpack-masterbar
6 */
7
8namespace Automattic\Jetpack\Masterbar;
9
10use Automattic\Jetpack\Connection\Manager as Connection_Manager;
11use WP_User;
12
13if ( ! defined( 'ABSPATH' ) ) {
14    exit( 0 );
15}
16
17/**
18 * Hides profile fields for WordPress.com connected users.
19 *
20 * @param WP_User $user The current WP_User object.
21 */
22function jetpack_masterbar_hide_profile_fields( $user ) {
23    $connection_manager = new Connection_Manager( 'jetpack' );
24    if ( ! $connection_manager->is_user_connected( $user->ID ) ) {
25        // If this is a local user, show the default UX.
26        return;
27    }
28    $wp_kses_rule = array(
29        'a' => array(
30            'href'   => array(),
31            'rel'    => array(),
32            'target' => array(),
33        ),
34    );
35    // Since there is no hook for altering profile fields, we will use CSS and JS.
36    $name_info_wpcom_link_message = sprintf(
37        /* translators: 1 link */
38        __( '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-masterbar' ),
39        'https://wordpress.com/me'
40    );
41    $contact_info_wpcom_link_message = sprintf(
42        /* translators: 1 link */
43        __( '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-masterbar' ),
44        'https://wordpress.com/me/account'
45    );
46    ?>
47    <script>
48        document.addEventListener( 'DOMContentLoaded', function() {
49            // Field to be hidden.
50            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';
51            document.querySelectorAll( fieldsToHide ).forEach( element => element.classList.add( 'hidden' ) );
52
53            // Name Info.
54            var nameInfo                    = document.querySelector( '.user-first-name-wrap' ).closest( 'table' );
55            var nameInfoWpcomLink           = document.createElement( 'div' );
56                nameInfoWpcomLink.className = 'notice inline notice-large notice-warning';
57                nameInfoWpcomLink.innerHTML = '<?php echo wp_kses( $name_info_wpcom_link_message, $wp_kses_rule ); ?>';
58            nameInfo.parentNode.insertBefore( nameInfoWpcomLink, nameInfo.nextSibling );
59
60            // Contact Info.
61            var contactInfo                    = document.querySelector( '.user-email-wrap' ).closest( 'table' );
62            var contactInfoWpcomLink           = document.createElement( 'div' );
63                contactInfoWpcomLink.className = 'notice inline notice-large notice-warning';
64                contactInfoWpcomLink.innerHTML = '<?php echo wp_kses( $contact_info_wpcom_link_message, $wp_kses_rule ); ?>';
65            contactInfo.parentNode.insertBefore( contactInfoWpcomLink, contactInfo.nextSibling );
66        });
67
68    </script>
69    <?php
70}
71
72add_action( 'personal_options', __NAMESPACE__ . '\jetpack_masterbar_hide_profile_fields' );