Skip to content

Commit 426cd02

Browse files
authored
MU WPCOM: Prevent site owner from editing user's account-level fields (2nd try) (#42177)
* MU WPCOM: Prevent site owner from editing user's account-level fields * changelog * Use php to inject script * Rename the function to is_user_connected * Ignore super admin * Change to use is_network_admin
1 parent 2c22f69 commit 426cd02

File tree

7 files changed

+118
-12
lines changed

7 files changed

+118
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: minor
2+
Type: changed
3+
4+
MU WPCOM: Prevent site owner from editing user's account-level fields

projects/packages/jetpack-mu-wpcom/src/class-jetpack-mu-wpcom.php

+1
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ public static function load_wpcom_user_features() {
171171
require_once __DIR__ . '/features/wpcom-profile-settings/profile-settings-notices.php';
172172
require_once __DIR__ . '/features/wpcom-sidebar-notice/wpcom-sidebar-notice.php';
173173
require_once __DIR__ . '/features/wpcom-themes/wpcom-themes.php';
174+
require_once __DIR__ . '/features/wpcom-user-edit/wpcom-user-edit.php';
174175

175176
// Only load the Calypsoify and Masterbar features on WoA sites.
176177
if ( class_exists( '\Automattic\Jetpack\Status\Host' ) && ( new \Automattic\Jetpack\Status\Host() )->is_woa_site() ) {

projects/packages/jetpack-mu-wpcom/src/features/wpcom-profile-settings/profile-settings-link-to-wpcom.php

+1-12
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,7 @@
77

88
use Automattic\Jetpack\Jetpack_Mu_Wpcom;
99

10-
/**
11-
* Check if the site is a WordPress.com Atomic site.
12-
*
13-
* @return bool
14-
*/
15-
function is_woa_site() {
16-
if ( ! class_exists( 'Automattic\Jetpack\Status\Host' ) ) {
17-
return false;
18-
}
19-
$host = new Automattic\Jetpack\Status\Host();
20-
return $host->is_woa_site();
21-
}
10+
require_once __DIR__ . '/../../utils.php';
2211

2312
/**
2413
* Adds a link to the WordPress.com profile settings page.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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' );

projects/packages/jetpack-mu-wpcom/src/utils.php

+27
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,30 @@ function get_wpcom_blog_id() {
154154

155155
return false;
156156
}
157+
158+
/**
159+
* Check if the site is a WordPress.com Atomic site.
160+
*
161+
* @return bool
162+
*/
163+
function is_woa_site() {
164+
if ( ! class_exists( 'Automattic\Jetpack\Status\Host' ) ) {
165+
return false;
166+
}
167+
$host = new Automattic\Jetpack\Status\Host();
168+
return $host->is_woa_site();
169+
}
170+
171+
/**
172+
* Whether the current user is connected to WordPress.com.
173+
*
174+
* @param int $user_id the user identifier. Default is the current user.
175+
* @return bool Boolean is the user connected?
176+
*/
177+
function is_user_connected( $user_id ) {
178+
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
179+
return true;
180+
}
181+
182+
return ( new Connection_Manager( 'jetpack' ) )->is_user_connected( $user_id );
183+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: minor
2+
Type: changed
3+
4+
MU WPCOM: Prevent site owner from editing user's account-level fields
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: minor
2+
Type: changed
3+
4+
MU WPCOM: Prevent site owner from editing user's account-level fields

0 commit comments

Comments
 (0)