Skip to content

Commit 4a21313

Browse files
authored
Merge pull request #395 from costdev/link_admin_bar_status_to_api_host_field
Branding: Link admin bar status text to `api_host` field.
2 parents ca0b079 + fe27cce commit 4a21313

File tree

2 files changed

+48
-35
lines changed

2 files changed

+48
-35
lines changed

includes/class-branding.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@ public function add_admin_bar_menu( $wp_admin_bar ) {
137137
return;
138138
}
139139

140+
$capability = is_multisite() ? 'manage_network' : 'manage_options';
141+
if ( ! current_user_can( $capability ) ) {
142+
return;
143+
}
144+
140145
$admin_settings = Admin_Settings::get_instance();
141146
$options_base = is_multisite() ? 'settings.php' : 'options-general.php';
142147
$settings_page = network_admin_url( $options_base . '?page=aspireupdate-settings' );
@@ -170,21 +175,18 @@ public function add_admin_bar_menu( $wp_admin_bar ) {
170175
[
171176
'id' => 'aspireupdate-admin-bar-menu-status',
172177
'parent' => $menu_id,
173-
'href' => false,
178+
'href' => $settings_page . '#aspireupdate-settings-field-api_host',
174179
'title' => sprintf( $status_message, $api_host_name ),
175180
]
176181
);
177182

178-
$capability = is_multisite() ? 'manage_network' : 'manage_options';
179-
if ( current_user_can( $capability ) ) {
180-
$wp_admin_bar->add_menu(
181-
[
182-
'id' => 'aspireupdate-admin-bar-menu-settings',
183-
'parent' => $menu_id,
184-
'href' => $settings_page,
185-
'title' => __( 'AspireUpdate Settings', 'aspireupdate' ),
186-
]
187-
);
188-
}
183+
$wp_admin_bar->add_menu(
184+
[
185+
'id' => 'aspireupdate-admin-bar-menu-settings',
186+
'parent' => $menu_id,
187+
'href' => $settings_page,
188+
'title' => __( 'AspireUpdate Settings', 'aspireupdate' ),
189+
]
190+
);
189191
}
190192
}

tests/phpunit/tests/Branding/Branding_AddAdminBarMenuTest.php

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class Branding_AddAdminBarMenuTest extends WP_UnitTestCase {
2323
* Test that the main admin bar menu is added.
2424
*/
2525
public function test_should_add_main_admin_bar_menu() {
26+
$this->set_user_to_administrator();
27+
2628
$wp_admin_bar = new WP_Admin_Bar();
2729
$branding = new AspireUpdate\Branding();
2830

@@ -43,6 +45,8 @@ public function test_should_add_main_admin_bar_menu() {
4345
* Test that the status menu item is added.
4446
*/
4547
public function test_should_add_status_menu_item() {
48+
$this->set_user_to_administrator();
49+
4650
$wp_admin_bar = new WP_Admin_Bar();
4751
$branding = new AspireUpdate\Branding();
4852

@@ -72,9 +76,11 @@ public function test_should_add_status_menu_item() {
7276
}
7377

7478
/**
75-
* Test that the settings link is not added when the user lacks appropriate capabilities.
79+
* Test that the settings link is added.
7680
*/
77-
public function test_should_not_add_settings_link_when_the_user_lacks_appropriate_capabilities() {
81+
public function test_should_add_settings_link() {
82+
$this->set_user_to_administrator();
83+
7884
$wp_admin_bar = new WP_Admin_Bar();
7985
$branding = new AspireUpdate\Branding();
8086

@@ -84,24 +90,17 @@ public function test_should_not_add_settings_link_when_the_user_lacks_appropriat
8490
$actual = $wp_admin_bar->get_nodes();
8591

8692
$this->assertIsArray( $actual, 'There are no admin bar nodes.' );
87-
$this->assertArrayNotHasKey(
93+
$this->assertArrayHasKey(
8894
'aspireupdate-admin-bar-menu-settings',
8995
$actual,
90-
'The settings link is present.'
96+
'The settings link is not present.'
9197
);
9298
}
9399

94100
/**
95-
* Test that the settings link is added when the user has appropriate capabilities.
101+
* Test that the main admin bar menu is not added when the user lacks appropriate capabilities.
96102
*/
97-
public function test_should_add_settings_link_when_the_user_has_appropriate_capabilities() {
98-
$user = $this->factory->user->create( [ 'role' => 'administrator' ] );
99-
wp_set_current_user( $user );
100-
101-
if ( is_multisite() ) {
102-
grant_super_admin( $user );
103-
}
104-
103+
public function test_should_not_add_main_admin_bar_menu_when_the_user_lacks_appropriate_capabilities() {
105104
$wp_admin_bar = new WP_Admin_Bar();
106105
$branding = new AspireUpdate\Branding();
107106

@@ -110,23 +109,17 @@ public function test_should_add_settings_link_when_the_user_has_appropriate_capa
110109

111110
$actual = $wp_admin_bar->get_nodes();
112111

113-
$this->assertIsArray( $actual, 'There are no admin bar nodes.' );
114-
$this->assertArrayHasKey(
115-
'aspireupdate-admin-bar-menu-settings',
116-
$actual,
117-
'The settings link is not present.'
118-
);
112+
$this->assertIsNotArray( $actual, 'There are admin bar nodes.' );
119113
}
120114

121115
/**
122-
* Test that the menu is not added when AP_REMOVE_UI is set to true.
116+
* Test that the main admin bar menu is not added when AP_REMOVE_UI is set to true.
123117
*/
124-
public function test_should_not_enqueue_style_when_ap_remove_ui_is_true() {
118+
public function test_should_not_add_main_admin_bar_menu_when_ap_remove_ui_is_true() {
125119
// Prevent the menu from being added.
126120
define( 'AP_REMOVE_UI', true );
127121

128-
$user = $this->factory->user->create( [ 'role' => 'administrator' ] );
129-
wp_set_current_user( $user );
122+
$this->set_user_to_administrator();
130123

131124
$wp_admin_bar = new WP_Admin_Bar();
132125
$branding = new AspireUpdate\Branding();
@@ -150,6 +143,8 @@ public function test_should_not_enqueue_style_when_ap_remove_ui_is_true() {
150143
public function test_should_set_status_menu_item_content_depending_on_the_api_host_option( $api_host, $expected_name ) {
151144
define( 'AP_HOST', $api_host );
152145

146+
$this->set_user_to_administrator();
147+
153148
$wp_admin_bar = new WP_Admin_Bar();
154149
$branding = new AspireUpdate\Branding();
155150

@@ -205,4 +200,20 @@ public function data_api_hosts_and_expected_names() {
205200
],
206201
];
207202
}
203+
204+
/**
205+
* Set the current user to an administrator.
206+
*
207+
* Grants super admin privileges when running multisite.
208+
*
209+
* @return void
210+
*/
211+
private function set_user_to_administrator() {
212+
$user = $this->factory->user->create( [ 'role' => 'administrator' ] );
213+
wp_set_current_user( $user );
214+
215+
if ( is_multisite() ) {
216+
grant_super_admin( $user );
217+
}
218+
}
208219
}

0 commit comments

Comments
 (0)