Skip to content

Commit ff7e7b7

Browse files
committed
Hide DevTools toggles when dependency_support is absent
1 parent 779104f commit ff7e7b7

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

assets/src/settings-page/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,9 @@ function Root( { appRoot } ) {
277277
initialOpen={ 'other-settings' === focusedSection }
278278
>
279279
<MobileRedirection />
280-
<DeveloperTools />
280+
{ HAS_DEPENDENCY_SUPPORT && (
281+
<DeveloperTools />
282+
) }
281283
<DeleteDataAtUninstall />
282284
</AMPDrawer>
283285
<SettingsFooter />

src/DevTools/UserAccess.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,29 @@ public function register_rest_field() {
146146
);
147147
}
148148

149+
/**
150+
* Determine whether the option can be modified.
151+
*
152+
* @param int $user_id User ID.
153+
* @return bool Whether the option can be modified.
154+
*/
155+
private function can_modify_option( $user_id ) {
156+
return (
157+
$this->dependency_support->has_support()
158+
&&
159+
current_user_can( 'edit_user', $user_id )
160+
&&
161+
AMP_Validation_Manager::has_cap( $user_id )
162+
);
163+
}
164+
149165
/**
150166
* Add the developer tools checkbox to the user edit screen.
151167
*
152168
* @param WP_User $profile_user Current user being edited.
153169
*/
154170
public function print_personal_options( $profile_user ) {
155-
if ( ! current_user_can( 'edit_user', $profile_user->ID ) || ! AMP_Validation_Manager::has_cap( $profile_user ) ) {
171+
if ( ! $this->can_modify_option( $profile_user->ID ) ) {
156172
return;
157173
}
158174
?>
@@ -177,7 +193,7 @@ public function print_personal_options( $profile_user ) {
177193
* @return bool Whether update was successful.
178194
*/
179195
public function update_user_setting( $user_id ) {
180-
if ( ! current_user_can( 'edit_user', $user_id ) || ! AMP_Validation_Manager::has_cap( $user_id ) ) {
196+
if ( ! $this->can_modify_option( $user_id ) ) {
181197
return false;
182198
}
183199
$enabled = isset( $_POST[ self::USER_FIELD_DEVELOPER_TOOLS_ENABLED ] ) && rest_sanitize_boolean( wp_unslash( $_POST[ self::USER_FIELD_DEVELOPER_TOOLS_ENABLED ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing, phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Nonce handled by user-edit.php; sanitization used is sanitized.

tests/php/src/DevTools/UserAccessTest.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ public function test_register_rest_field() {
165165
/**
166166
* Tests UserAccess::print_personal_options
167167
*
168+
* @covers ::can_modify_option
168169
* @covers ::print_personal_options
169170
*/
170171
public function test_print_personal_options() {
@@ -182,12 +183,18 @@ public function test_print_personal_options() {
182183

183184
ob_start();
184185
$this->dev_tools_user_access->print_personal_options( $admin_user );
185-
$this->assertStringContainsString( 'checkbox', ob_get_clean() );
186+
$output = ob_get_clean();
187+
if ( ( new DependencySupport() )->has_support() ) {
188+
$this->assertStringContainsString( 'checkbox', $output );
189+
} else {
190+
$this->assertStringNotContainsString( 'checkbox', $output );
191+
}
186192
}
187193

188194
/**
189195
* Tests UserAccess::update_user_setting
190196
*
197+
* @covers ::can_modify_option
191198
* @covers ::update_user_setting
192199
*/
193200
public function test_update_user_setting() {
@@ -201,10 +208,10 @@ public function test_update_user_setting() {
201208
wp_set_current_user( $admin_user->ID );
202209
$this->assertFalse( $this->dev_tools_user_access->update_user_setting( $editor_user->ID ) );
203210

204-
$this->assertTrue( $this->dev_tools_user_access->update_user_setting( $admin_user->ID ) );
205-
$this->assertTrue( $this->dev_tools_user_access->get_user_enabled( $admin_user ) );
211+
$this->assertEquals( ( new DependencySupport() )->has_support(), $this->dev_tools_user_access->update_user_setting( $admin_user->ID ) );
212+
$this->assertEquals( ( new DependencySupport() )->has_support(), $this->dev_tools_user_access->get_user_enabled( $admin_user ) );
206213
$_POST[ UserAccess::USER_FIELD_DEVELOPER_TOOLS_ENABLED ] = null;
207-
$this->assertTrue( $this->dev_tools_user_access->update_user_setting( $admin_user->ID ) );
214+
$this->assertEquals( ( new DependencySupport() )->has_support(), $this->dev_tools_user_access->update_user_setting( $admin_user->ID ) );
208215
$this->assertFalse( $this->dev_tools_user_access->get_user_enabled( $admin_user ) );
209216
}
210217

0 commit comments

Comments
 (0)