Skip to content

Commit 39fac01

Browse files
committed
hide global settings for non-admin
1 parent 9aaae60 commit 39fac01

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

src/plugins/global-settings/index.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@ import {
1818
isContentOnlyMode,
1919
settings,
2020
} from 'stackable'
21+
import { currentUserHasCapability } from '~stackable/util'
2122

2223
/** WordPress dependencies
2324
*/
2425
import { registerPlugin } from '@wordpress/plugins'
2526
import { __ } from '@wordpress/i18n'
2627
import { applyFilters, addAction } from '@wordpress/hooks'
27-
import { dispatch, select } from '@wordpress/data'
28+
import { useEffect, useState } from '@wordpress/element'
29+
import {
30+
dispatch, select, useSelect,
31+
} from '@wordpress/data'
2832
import { PanelBody } from '@wordpress/components'
2933

3034
// Action used to toggle the global settings panel.
@@ -40,13 +44,26 @@ addAction( 'stackable.global-settings.toggle-sidebar', 'toggle', () => {
4044
} )
4145

4246
const GlobalSettings = () => {
47+
const [ userCanManageOptions, setUserCanManageOptions ] = useState( false )
48+
const id = useSelect( select => select( 'core' ).getCurrentUser().id )
49+
50+
useEffect( () => {
51+
const checkCapabilities = async () => {
52+
const capabilities = await currentUserHasCapability( 'manage_options' )
53+
setUserCanManageOptions( capabilities )
54+
}
55+
56+
checkCapabilities()
57+
}, [ id ] )
4358
// For older WP versions (<6.6), wp.editor.PluginSidebar is undefined,
4459
// use wp.editSite.PluginSidebar and wp.editPost.PluginSidebar as fallback
4560
const PluginSidebar = window.wp.editor.PluginSidebar || window.wp.editSite?.PluginSidebar || window.wp.editPost?.PluginSidebar
4661

62+
const globalSettingsInspector = applyFilters( 'stackable.global-settings.inspector', null )
63+
4764
return (
4865
<>
49-
{ PluginSidebar &&
66+
{ PluginSidebar && userCanManageOptions &&
5067
<PluginSidebar
5168
name="sidebar"
5269
title={ __( 'Stackable Settings', i18n ) }
@@ -60,7 +77,7 @@ const GlobalSettings = () => {
6077
<a href="https://docs.wpstackable.com/article/465-how-to-style-the-different-block-hover-states?utm_source=wp-settings-global-settings&utm_campaign=learnmore&utm_medium=wp-dashboard" target="_docs">{ __( 'Learn more', i18n ) }</a> */ }
6178
</p>
6279
</PanelBody>
63-
{ applyFilters( 'stackable.global-settings.inspector', null ) }
80+
{ globalSettingsInspector }
6481
</PluginSidebar>
6582
}
6683
</>

0 commit comments

Comments
 (0)