Skip to content

Commit 5f5a37f

Browse files
committed
Restrict settings page to admins with manage_options
Updated Settings_Page::init() to require both is_admin() and current_user_can('manage_options'). Added and updated unit tests to verify correct behavior for users with and without the required capability.
1 parent 23da236 commit 5f5a37f

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

plugins/hwp-previews/src/Admin/Settings_Page.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ protected function __construct() {
5858
* Initializes the settings page.
5959
*/
6060
public static function init(): ?Settings_Page {
61-
if ( ! is_admin() ) {
61+
if ( ! is_admin() || ! current_user_can( 'manage_options' ) ) {
6262
return null;
6363
}
6464
if ( ! isset( self::$instance ) || ! ( is_a( self::$instance, self::class ) ) ) {

plugins/hwp-previews/tests/wpunit/Admin/SettingsPageTest.php

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ public function in_admin( $context = null ) {
2121
return $context === 'user';
2222
}
2323
};
24+
25+
// Reset the instance before each test
26+
$reflection = new ReflectionClass( Settings_Page::class );
27+
$instanceProperty = $reflection->getProperty( 'instance' );
28+
$instanceProperty->setAccessible( true );
29+
$instanceProperty->setValue( null );
2430
}
2531

2632
public function tearDown() : void {
@@ -29,13 +35,40 @@ public function tearDown() : void {
2935
parent::tearDown();
3036
}
3137

38+
public function test_init_returns_null_if_not_admin() {
39+
// Unset the admin screen mock
40+
unset( $GLOBALS['current_screen'] );
41+
42+
$instance = Settings_Page::init();
43+
$this->assertNull( $instance, 'Settings_Page::init() should return null if not in admin.' );
44+
}
45+
46+
public function test_init_returns_null_for_user_without_manage_options_cap() {
47+
$user_id = self::factory()->user->create( [ 'role' => 'subscriber' ] );
48+
wp_set_current_user( $user_id );
49+
50+
$instance = Settings_Page::init();
51+
$this->assertNull( $instance, 'Settings_Page::init() should return null for users without manage_options capability.' );
52+
}
53+
54+
public function test_init_returns_instance_for_user_with_manage_options_cap() {
55+
$user_id = self::factory()->user->create( [ 'role' => 'administrator' ] );
56+
wp_set_current_user( $user_id );
57+
58+
$instance = Settings_Page::init();
59+
$this->assertInstanceOf( Settings_Page::class, $instance, 'Settings_Page::init() should return an instance for users with manage_options capability.' );
60+
}
61+
3262
public function test_settings_page_instance() {
3363
$reflection = new ReflectionClass( Settings_Page::class );
3464
$instanceProperty = $reflection->getProperty( 'instance' );
3565
$instanceProperty->setAccessible( true );
36-
$instanceProperty->setValue( null );
3766

3867
$this->assertNull( $instanceProperty->getValue() );
68+
69+
// To pass the capability check
70+
$user_id = self::factory()->user->create( [ 'role' => 'administrator' ] );
71+
wp_set_current_user( $user_id );
3972
$instance = Settings_Page::init();
4073

4174
$this->assertInstanceOf( Settings_Page::class, $instanceProperty->getValue() );
@@ -44,7 +77,11 @@ public function test_settings_page_instance() {
4477

4578
public function test_get_current_tab() {
4679
$_GET['attachment'] = 'attachment';
47-
$settings_page = Settings_Page::init();
80+
81+
// To pass the capability check
82+
$user_id = self::factory()->user->create( [ 'role' => 'administrator' ] );
83+
wp_set_current_user( $user_id );
84+
$settings_page = Settings_Page::init();
4885

4986
$post_preview_service = new Post_Preview_Service();
5087
$post_types = $post_preview_service->get_post_types();
@@ -61,6 +98,9 @@ public function test_get_current_tab() {
6198
}
6299

63100
public function test_register_hooks() {
101+
// To pass the capability check
102+
$user_id = self::factory()->user->create( [ 'role' => 'administrator' ] );
103+
wp_set_current_user( $user_id );
64104
$settings_page = Settings_Page::init();
65105
$this->assertNull( $settings_page->register_settings_page() );
66106
$this->assertNull( $settings_page->register_settings_fields() );

0 commit comments

Comments
 (0)