diff --git a/src/Actions.php b/src/Actions.php index 10fafb8d..257f1ffe 100644 --- a/src/Actions.php +++ b/src/Actions.php @@ -131,13 +131,32 @@ public function admin_bar_node( $admin_bar ) { return; // @codeCoverageIgnore } + $settings = Helpers::get_settings(); + $current_user = wp_get_current_user(); + + $has_access = false; + $user_roles_have_access = array_merge( + [ 'administrator' ], + $settings['expand_dashboard_access'] ?? [] + ); + + foreach ( $current_user->roles as $role ) { + if ( in_array( $role, $user_roles_have_access, true ) ) { + $has_access = true; + break; + } + } + + if ( ! $has_access ) { + return; + } + // Add main admin bar node. $args[] = [ 'id' => 'plausible-analytics', 'title' => 'Plausible Analytics', ]; - $settings = Helpers::get_settings(); if ( ! empty( $settings[ 'enable_analytics_dashboard' ] ) || ( ! empty( $settings[ 'self_hosted_domain' ] ) && ! empty( $settings[ 'self_hosted_shared_link' ] ) ) ) { @@ -167,12 +186,14 @@ public function admin_bar_node( $admin_bar ) { } // Add link to Plausible Settings page. - $args[] = [ - 'id' => 'settings', - 'title' => esc_html__( 'Settings', 'plausible-analytics' ), - 'href' => admin_url( 'options-general.php?page=plausible_analytics' ), - 'parent' => 'plausible-analytics', - ]; + if ( current_user_can( 'manage_options' ) ) { + $args[] = [ + 'id' => 'settings', + 'title' => esc_html__( 'Settings', 'plausible-analytics' ), + 'href' => admin_url( 'options-general.php?page=plausible_analytics' ), + 'parent' => 'plausible-analytics', + ]; + } foreach ( $args as $arg ) { $admin_bar->add_node( $arg ); diff --git a/tests/integration/ActionsTest.php b/tests/integration/ActionsTest.php index 160e0a82..484bf8cd 100644 --- a/tests/integration/ActionsTest.php +++ b/tests/integration/ActionsTest.php @@ -66,10 +66,14 @@ public function testAdminBarNode() { require_once( ABSPATH . 'wp-includes/class-wp-admin-bar.php' ); } + wp_set_current_user( 1 ); $admin_bar = new WP_Admin_Bar(); - $class->admin_bar_node( $admin_bar ); - $this->assertNotEmpty( $admin_bar->get_node( 'plausible-analytics' ) ); + + wp_set_current_user( 0 ); + $admin_bar = new WP_Admin_Bar(); + $class->admin_bar_node( $admin_bar ); + $this->assertEmpty( $admin_bar->get_node( 'plausible-analytics' ) ); } }