Skip to content

Commit 3937748

Browse files
committed
allow other roles to read stackable options
1 parent 9890d19 commit 3937748

File tree

11 files changed

+147
-45
lines changed

11 files changed

+147
-45
lines changed

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: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
/**
3+
*
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 get_item_permissions_check( $request ) {
26+
return current_user_can( 'edit_posts' );
27+
}
28+
29+
/**
30+
* Updates settings for the settings object.
31+
*/
32+
public function update_item( $request ) {
33+
34+
if ( $request->has_param( '_locale' ) ) {
35+
unset( $request[ '_locale' ] );
36+
}
37+
38+
$params = $request->get_params();
39+
40+
return parent::update_item( $request );
41+
}
42+
43+
/**
44+
* Retrieves all of the registered stackable options.
45+
*
46+
* @return array Array of registered options.
47+
*/
48+
protected function get_registered_options() {
49+
$rest_options = array();
50+
51+
foreach ( get_registered_settings() as $name => $args ) {
52+
if ( empty( $args['show_in_rest'] ) ) {
53+
continue;
54+
}
55+
56+
$rest_args = array();
57+
58+
if ( is_array( $args['show_in_rest'] ) ) {
59+
$rest_args = $args['show_in_rest'];
60+
}
61+
62+
$defaults = array(
63+
'name' => ! empty( $rest_args['name'] ) ? $rest_args['name'] : $name,
64+
'schema' => array(),
65+
);
66+
67+
$rest_args = array_merge( $defaults, $rest_args );
68+
69+
// Skip over settings not from Stackable
70+
if ( strpos( $rest_args[ 'name' ], 'stackable' ) !== 0 ) {
71+
continue;
72+
}
73+
74+
$default_schema = array(
75+
'type' => empty( $args['type'] ) ? null : $args['type'],
76+
'title' => empty( $args['label'] ) ? '' : $args['label'],
77+
'description' => empty( $args['description'] ) ? '' : $args['description'],
78+
'default' => isset( $args['default'] ) ? $args['default'] : null,
79+
);
80+
81+
$rest_args['schema'] = array_merge( $default_schema, $rest_args['schema'] );
82+
$rest_args['option_name'] = $name;
83+
84+
// Skip over settings that don't have a defined type in the schema.
85+
if ( empty( $rest_args['schema']['type'] ) ) {
86+
continue;
87+
}
88+
89+
/*
90+
* Allow the supported types for settings, as we don't want invalid types
91+
* to be updated with arbitrary values that we can't do decent sanitizing for.
92+
*/
93+
if ( ! in_array( $rest_args['schema']['type'], array( 'number', 'integer', 'string', 'boolean', 'array', 'object' ), true ) ) {
94+
continue;
95+
}
96+
97+
$rest_args['schema'] = rest_default_additional_properties_to_false( $rest_args['schema'] );
98+
99+
$rest_options[ $rest_args['name'] ] = $rest_args;
100+
}
101+
102+
return $rest_options;
103+
}
104+
}
105+
106+
new Stackable_Admin_Settings();
107+
}

src/components/base-control2/label-tooltip.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@
77
* Internal dependencies
88
*/
99
import HelpTooltip from '../help-tooltip'
10-
import { fetchSettings } from '~stackable/util'
10+
import { fetchSettings, saveSettings } from '~stackable/util'
1111

1212
/**
1313
* WordPress dependencies
1414
*/
1515
import {
1616
useRef, useState, useEffect,
1717
} from '@wordpress/element'
18-
import { models } from '@wordpress/api'
1918
import domReady from '@wordpress/dom-ready'
2019

2120
const LabelTooltip = props => {
@@ -138,10 +137,9 @@ function useHelpTooltipEnabled() {
138137
const updateHelpTooltipEnabled = newValue => {
139138
helpTooltipEnabled = newValue
140139

141-
const model = new models.Settings( {
140+
saveSettings( {
142141
stackable_help_tooltip_disabled: newValue ? '' : '1', // eslint-disable-line
143142
} )
144-
model.save()
145143

146144
// Let the other components know that the value has changed.
147145
window.dispatchEvent( new CustomEvent( '_stkHelpTooltipEnabledChanged', { detail: newValue } ) )

src/plugins/global-settings/color-schemes/color-scheme-picker.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,13 @@ import {
2323
ControlSeparator,
2424
} from '~stackable/components'
2525
import { useBlockHoverState } from '~stackable/hooks'
26-
import { extractColor } from '~stackable/util'
26+
import { extractColor, saveSettings } from '~stackable/util'
2727
import { cloneDeep, isEqual } from 'lodash'
2828

2929
import {
3030
useRef, useState, useEffect,
3131
} from '@wordpress/element'
3232
import { useSelect, dispatch } from '@wordpress/data'
33-
import { models } from '@wordpress/api'
3433
import { __, sprintf } from '@wordpress/i18n'
3534
import { applyFilters, doAction } from '@wordpress/hooks'
3635

@@ -149,12 +148,11 @@ const ColorSchemePicker = props => {
149148
updatedColorSchemes[ currentIndex ] = currentItem
150149

151150
saveTimeout = setTimeout( () => {
152-
const settings = new models.Settings( {
151+
saveSettings( {
153152
stackable_global_color_schemes: updatedColorSchemes, // eslint-disable-line camelcase
154153
// Clear the cached CSS when the color scheme is updated
155154
stackable_global_color_scheme_generated_css: '', // eslint-disable-line camelcase
156155
} )
157-
settings.save()
158156
}, 300 )
159157

160158
// Update our store.

src/plugins/global-settings/color-schemes/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
import {
1515
PanelAdvancedSettings, ProControlButton, HelpTooltip,
1616
} from '~stackable/components'
17+
import { saveSettings } from '~stackable/util'
1718

1819
/**
1920
* WordPress dependencies
@@ -23,7 +24,6 @@ import { Fragment, useState } from '@wordpress/element'
2324
import { __ } from '@wordpress/i18n'
2425
import { ToggleControl } from '@wordpress/components'
2526
import { useSelect, dispatch } from '@wordpress/data'
26-
import { models } from '@wordpress/api'
2727

2828
export { GlobalColorSchemeStyles } from './editor-loader'
2929

@@ -82,8 +82,7 @@ addFilter( 'stackable.global-settings.inspector.global-colors.toggle-controls',
8282
hideColorSchemeColors: value,
8383
} )
8484

85-
const settings = new models.Settings( { stackable_global_hide_color_scheme_colors: value } ) // eslint-disable-line camelcase
86-
settings.save()
85+
saveSettings( { stackable_global_hide_color_scheme_colors: value } ) // eslint-disable-line camelcase
8786
}
8887

8988
return <>

src/plugins/global-settings/colors/color-picker.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ import {
1313
import { cloneDeep } from 'lodash'
1414
import { i18n } from 'stackable'
1515
import { SortablePicker } from '~stackable/components'
16+
import { saveSettings } from '~stackable/util'
1617

1718
import { useRef } from '@wordpress/element'
1819
import {
1920
select, dispatch, useSelect,
2021
} from '@wordpress/data'
21-
import { models } from '@wordpress/api'
2222
import { __, sprintf } from '@wordpress/i18n'
2323
import { ColorIndicator, ColorPicker } from '@wordpress/components'
2424

@@ -48,8 +48,7 @@ const ColorPickers = props => {
4848
// Save settings.
4949
clearTimeout( saveTimeout )
5050
saveTimeout = setTimeout( () => {
51-
const settings = new models.Settings( { stackable_global_colors: [ newColors ] } ) // eslint-disable-line camelcase
52-
settings.save()
51+
saveSettings( { stackable_global_colors: [ newColors ] } ) // eslint-disable-line camelcase
5352
}, 300 )
5453

5554
// Update our store.

src/plugins/global-settings/colors/index.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { GlobalColorStyles } from './editor-loader'
1111
*/
1212
import { i18n } from 'stackable'
1313
import { PanelAdvancedSettings } from '~stackable/components'
14+
import { saveSettings } from '~stackable/util'
1415
import rgba from 'color-rgba'
1516

1617
/**
@@ -20,7 +21,6 @@ import { addFilter, applyFilters } from '@wordpress/hooks'
2021
import { Fragment } from '@wordpress/element'
2122
import { __ } from '@wordpress/i18n'
2223
import { dispatch, useSelect } from '@wordpress/data'
23-
import { models } from '@wordpress/api'
2424
import { ToggleControl } from '@wordpress/components'
2525

2626
export { GlobalColorStyles }
@@ -80,26 +80,23 @@ addFilter( 'stackable.global-settings.inspector', 'stackable/global-colors', out
8080
hideThemeColors: value,
8181
} )
8282

83-
const settings = new models.Settings( { stackable_global_hide_theme_colors: value } ) // eslint-disable-line camelcase
84-
settings.save()
83+
saveSettings( { stackable_global_hide_theme_colors: value } ) // eslint-disable-line camelcase
8584
}
8685

8786
const onChangeHideDefaultColors = value => {
8887
dispatch( 'stackable/global-colors' ).updateSettings( {
8988
hideDefaultColors: value,
9089
} )
9190

92-
const settings = new models.Settings( { stackable_global_hide_default_colors: value } ) // eslint-disable-line camelcase
93-
settings.save()
91+
saveSettings( { stackable_global_hide_default_colors: value } ) // eslint-disable-line camelcase
9492
}
9593

9694
const onChangeHideSiteEditorColors = value => {
9795
dispatch( 'stackable/global-colors' ).updateSettings( {
9896
hideSiteEditorColors: value,
9997
} )
10098

101-
const settings = new models.Settings( { stackable_global_hide_site_editor_colors: value } ) // eslint-disable-line camelcase
102-
settings.save()
99+
saveSettings( { stackable_global_hide_site_editor_colors: value } ) // eslint-disable-line camelcase
103100
}
104101

105102
const ColorToggleControls = applyFilters( 'stackable.global-settings.inspector.global-colors.toggle-controls', Fragment )

src/plugins/global-settings/typography/editor-loader.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
fetchSettings,
1414
loadGoogleFont,
1515
getDefaultFontSize,
16+
saveSettings,
1617
} from '~stackable/util'
1718
import { generateStyles } from '~stackable/block-components'
1819
import deepmerge from 'deepmerge'
@@ -29,7 +30,6 @@ import {
2930
addAction, removeAction, applyFilters, doAction, addFilter,
3031
} from '@wordpress/hooks'
3132
import { useSelect, dispatch } from '@wordpress/data'
32-
import { models } from '@wordpress/api'
3333

3434
let saveTypographyAsPresetsThrottle = null
3535

@@ -101,8 +101,7 @@ export const GlobalTypographyStyles = () => {
101101

102102
clearTimeout( saveTypographyAsPresetsThrottle )
103103
saveTypographyAsPresetsThrottle = setTimeout( () => {
104-
const settings = new models.Settings( { stackable_global_custom_preset_controls: newSettings } ) // eslint-disable-line camelcase
105-
settings.save()
104+
saveSettings( { stackable_global_custom_preset_controls: newSettings } ) // eslint-disable-line camelcase
106105
}, 300 )
107106

108107
dispatch( 'stackable/global-preset-controls.custom' ).updateCustomPresetControls( newSettings )

src/plugins/global-settings/typography/index.js

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
ProControlButton,
2424
AdvancedToggleControl,
2525
} from '~stackable/components'
26-
import { fetchSettings } from '~stackable/util'
26+
import { fetchSettings, saveSettings } from '~stackable/util'
2727
import { useDeviceType } from '~stackable/hooks'
2828
import {
2929
i18n, isPro, showProNotice,
@@ -38,7 +38,6 @@ import {
3838
import {
3939
Fragment, useEffect, useRef, useState,
4040
} from '@wordpress/element'
41-
import { models } from '@wordpress/api'
4241
import {
4342
addFilter, applyFilters, doAction,
4443
} from '@wordpress/hooks'
@@ -236,10 +235,9 @@ addFilter( 'stackable.global-settings.inspector', 'stackable/global-typography',
236235

237236
clearTimeout( saveTypographyThrottle )
238237
saveTypographyThrottle = setTimeout( () => {
239-
const model = new models.Settings( {
238+
saveSettings( {
240239
stackable_global_typography: [ newSettings ], // eslint-disable-line
241240
} )
242-
model.save()
243241
}, 500 )
244242
}
245243

@@ -248,10 +246,9 @@ addFilter( 'stackable.global-settings.inspector', 'stackable/global-typography',
248246

249247
clearTimeout( saveSelectedFontPairThrottle )
250248
saveSelectedFontPairThrottle = setTimeout( () => {
251-
const model = new models.Settings( {
249+
saveSettings( {
252250
stackable_selected_font_pair: name, // eslint-disable-line
253251
} )
254-
model.save()
255252
}, 500 )
256253
}
257254

@@ -260,27 +257,24 @@ addFilter( 'stackable.global-settings.inspector', 'stackable/global-typography',
260257

261258
clearTimeout( saveCustomFontPairsThrottle )
262259
saveCustomFontPairsThrottle = setTimeout( () => {
263-
const model = new models.Settings( {
260+
saveSettings( {
264261
stackable_custom_font_pairs: [ ...fontPairs ] , // eslint-disable-line
265262
} )
266-
model.save()
267263
}, 500 )
268264
}
269265

270266
const changeApplySettingsTo = value => {
271267
setApplySettingsTo( value )
272-
const model = new models.Settings( {
268+
saveSettings( {
273269
stackable_global_typography_apply_to: value, // eslint-disable-line
274270
} )
275-
model.save()
276271
}
277272

278273
const changeIsApplyBodyToHTML = value => {
279274
setIsApplyBodyToHTML( value )
280-
const model = new models.Settings( {
275+
saveSettings( {
281276
stackable_is_apply_body_to_html: value, // eslint-disable-line
282277
} )
283-
model.save()
284278
}
285279

286280
const updateTypeScale = value => {

src/plugins/global-settings/utils/use-block-layout-inspector-utils.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import { hoverState } from './block-layout-utils'
99
import { getShadows } from '~stackable/components'
1010
import { IMAGE_SHADOWS } from '~stackable/block-components'
1111
import { useDeviceType, useBlockHoverState } from '~stackable/hooks'
12+
import { saveSettings as saveAdminSettings } from '~stackable/util'
1213

1314
/**
1415
* WordPress dependencies
1516
*/
1617
import { __ } from '@wordpress/i18n'
17-
import { models } from '@wordpress/api'
1818
import { useSelect, dispatch } from '@wordpress/data'
1919

2020
export const useBlockLayoutInspectorUtils = ( storeName, optionName, setDisplayHoverNotice, saveTimeout ) => {
@@ -52,8 +52,7 @@ export const useBlockLayoutInspectorUtils = ( storeName, optionName, setDisplayH
5252
const saveSettings = newSettings => {
5353
clearTimeout( saveTimeout )
5454
saveTimeout = setTimeout( () => {
55-
const settings = new models.Settings( { [ optionName ]: newSettings } ) // eslint-disable-line camelcase
56-
settings.save()
55+
saveAdminSettings( { [ optionName ]: newSettings } ) // eslint-disable-line camelcase
5756
}, 300 )
5857

5958
// Update our store.

0 commit comments

Comments
 (0)