|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Prevent the site owner from editing user's account-level fields. |
| 4 | + * |
| 5 | + * @package automattic/jetpack-mu-wpcom |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Automattic\Jetpack\Jetpack_Mu_Wpcom; |
| 9 | + |
| 10 | +require_once __DIR__ . '/../../utils.php'; |
| 11 | + |
| 12 | +/** |
| 13 | + * Disable the account-level fields of the connected users to prevent the site owner from editing them. |
| 14 | + */ |
| 15 | +function wpcom_disable_account_level_fields_if_needed() { |
| 16 | + // Bail if editing from network. |
| 17 | + if ( is_network_admin() ) { |
| 18 | + return; |
| 19 | + } |
| 20 | + |
| 21 | + $user_id = ! empty( $_REQUEST['user_id'] ) ? absint( sanitize_text_field( wp_unslash( $_REQUEST['user_id'] ) ) ) : 0; // // phpcs:ignore WordPress.Security.NonceVerification |
| 22 | + |
| 23 | + // Do nothing if the user is not connected to WordPress.com. |
| 24 | + if ( ! $user_id || ! is_user_connected( $user_id ) ) { |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + ?> |
| 29 | + <script type="text/javascript"> |
| 30 | + document.addEventListener( 'DOMContentLoaded', function() { |
| 31 | + const fields = [ |
| 32 | + /** Language */ |
| 33 | + { selector: '#locale' }, |
| 34 | + /** First Name */ |
| 35 | + { selector: '#first_name' }, |
| 36 | + /** Last Name */ |
| 37 | + { selector: '#last_name' }, |
| 38 | + /** Nickname */ |
| 39 | + { selector: '#nickname' }, |
| 40 | + /** Display name */ |
| 41 | + { selector: '#display_name' }, |
| 42 | + /** Website */ |
| 43 | + { selector: '#url' }, |
| 44 | + /** Biographical Info */ |
| 45 | + { selector: '#description', tagName: 'p' }, |
| 46 | + /** Email */ |
| 47 | + { selector: '#email' }, |
| 48 | + ]; |
| 49 | + |
| 50 | + for ( let i = 0; i < fields.length; i++ ) { |
| 51 | + const field = fields[i]; |
| 52 | + const element = document.querySelector( field.selector ); |
| 53 | + if ( ! element ) { |
| 54 | + continue; |
| 55 | + } |
| 56 | + |
| 57 | + if ( element.tagName === 'INPUT' ) { |
| 58 | + element.readOnly = true; |
| 59 | + } else { |
| 60 | + element.disabled = true; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Append the description to indicate the field cannot be changed. |
| 65 | + */ |
| 66 | + const tagName = field.tagName ? field.tagName : 'span'; |
| 67 | + const description = document.createElement( tagName ); |
| 68 | + description.className = 'description'; |
| 69 | + // Use the `Tab` for spacing to align with other fields. |
| 70 | + description.innerHTML = "\t<?php echo esc_html__( 'It cannot be changed.', 'jetpack-mu-wpcom' ); ?>"; |
| 71 | + element.parentNode.appendChild( description ); |
| 72 | + } |
| 73 | + } ); |
| 74 | + </script> |
| 75 | + <?php |
| 76 | +} |
| 77 | +add_action( 'admin_print_footer_scripts-user-edit.php', __NAMESPACE__ . '\wpcom_disable_account_level_fields_if_needed' ); |
0 commit comments