Skip to content

Commit aaae28b

Browse files
kaeizenbfintal
andauthored
Fix (global settings): Global Settings not showing for non-admin users (#3533)
* allow other roles to read stackable options * allow non-admin users to read stackable options for Global Settings * hide global settings for non-admin * Update src/plugins/global-settings/index.js * update request url for fetching settings * add debug logs * update request url for test * update urls * update path * update request url * update test url --------- Co-authored-by: Benjamin Intal <[email protected]>
1 parent 71d21f1 commit aaae28b

File tree

5 files changed

+94
-11
lines changed

5 files changed

+94
-11
lines changed

e2e/test-utils/stackable.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class StackableFixture {
1717
}
1818

1919
const finishedCallback = async ( request: Request ) => {
20-
if ( request.url().includes( 'wp/v2/settings' ) && request.method() === 'GET' ) {
20+
if ( decodeURIComponent( request.url() ).includes( 'stackable/v3/settings' ) && request.method() === 'GET' ) {
2121
try {
2222
let settings = null
2323
await test.step( 'Wait for Stackable settings to load', async () => {
@@ -38,7 +38,7 @@ export class StackableFixture {
3838
}
3939
}
4040
const failedCallback = async ( request: Request ) => {
41-
if ( request.url().includes( 'wp/v2/settings' ) && request.method() === 'GET' ) {
41+
if ( decodeURIComponent( request.url() ).includes( 'stackable/v3/settings' ) && request.method() === 'GET' ) {
4242
cleanup()
4343
throw Error( 'Failed to get Stackable settings' )
4444
}

plugin.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ function is_frontend() {
216216
* Block Initializer.
217217
*/
218218
require_once( plugin_dir_path( __FILE__ ) . 'src/editor-settings.php' );
219+
require_once( plugin_dir_path( __FILE__ ) . 'src/admin.php' );
219220
require_once( plugin_dir_path( __FILE__ ) . 'src/init.php' );
220221
require_once( plugin_dir_path( __FILE__ ) . 'src/stk-block-types.php' );
221222
require_once( plugin_dir_path( __FILE__ ) . 'src/blocks.php' );

src/admin.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* This allows non-admin users to read Stackable Options for Global Settings in the Editor
4+
*/
5+
6+
// Exit if accessed directly.
7+
if ( ! defined( 'ABSPATH' ) ) {
8+
exit;
9+
}
10+
11+
if ( ! class_exists( 'Stackable_Admin_Settings' ) ) {
12+
13+
class Stackable_Admin_Settings extends WP_REST_Settings_Controller {
14+
15+
/**
16+
* Constructor.
17+
*
18+
*/
19+
public function __construct() {
20+
$this->namespace = 'stackable/v3';
21+
$this->rest_base = 'settings';
22+
add_action( 'rest_api_init', array( $this, 'register_routes' ) );
23+
}
24+
25+
public function register_routes() {
26+
register_rest_route(
27+
$this->namespace,
28+
'/' . $this->rest_base,
29+
array(
30+
'methods' => WP_REST_Server::READABLE,
31+
'callback' => array( $this, 'get_item' ),
32+
'args' => array(),
33+
'permission_callback' => array( $this, 'retrieve_item_permissions_check' ),
34+
)
35+
);
36+
}
37+
38+
public function retrieve_item_permissions_check( $request ) {
39+
return current_user_can( 'edit_posts' );
40+
}
41+
42+
/**
43+
* Retrieves only the Stackable registered options
44+
*
45+
* @return array Array of registered options.
46+
*/
47+
protected function get_registered_options() {
48+
$rest_options = parent::get_registered_options();
49+
50+
$rest_options = array_filter(
51+
$rest_options,
52+
function( $key ) {
53+
return strpos( $key, 'stackable' ) === 0;
54+
},
55+
ARRAY_FILTER_USE_KEY
56+
);
57+
58+
return $rest_options;
59+
}
60+
}
61+
62+
new Stackable_Admin_Settings();
63+
}

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
</>

src/util/admin.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import { sortBy } from 'lodash'
66
/**
77
* WordPress dependencies
88
*/
9-
import { loadPromise, models } from '@wordpress/api'
9+
import { loadPromise } from '@wordpress/api'
10+
import apiFetch from '@wordpress/api-fetch'
1011

1112
// Collect all the blocks and their variations for enabling/disabling and sort
1213
// them by type.
@@ -52,12 +53,13 @@ let fetchingPromise = null
5253
*/
5354
export const fetchSettings = () => {
5455
if ( ! fetchingPromise ) {
55-
fetchingPromise = loadPromise.then( () => {
56-
const settings = new models.Settings()
57-
return settings.fetch().then( response => {
58-
fetchingPromise = null
59-
return response
56+
fetchingPromise = loadPromise.then( async () => {
57+
const response = await apiFetch( {
58+
path: '/stackable/v3/settings/',
59+
method: 'GET',
6060
} )
61+
fetchingPromise = null
62+
return response
6163
} )
6264
}
6365

0 commit comments

Comments
 (0)