Skip to content

Commit 9aaae60

Browse files
committed
allow non-admin users to read stackable options for Global Settings
1 parent 3937748 commit 9aaae60

File tree

10 files changed

+63
-99
lines changed

10 files changed

+63
-99
lines changed

src/admin.php

Lines changed: 24 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
*
3+
* This allows non-admin users to read Stackable Options for Global Settings in the Editor
44
*/
55

66
// Exit if accessed directly.
@@ -22,82 +22,38 @@ public function __construct() {
2222
add_action( 'rest_api_init', array( $this, 'register_routes' ) );
2323
}
2424

25-
public function get_item_permissions_check( $request ) {
26-
return current_user_can( 'edit_posts' );
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+
);
2736
}
2837

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 );
38+
public function retrieve_item_permissions_check( $request ) {
39+
return current_user_can( 'edit_posts' );
4140
}
4241

4342
/**
44-
* Retrieves all of the registered stackable options.
43+
* Retrieves only the Stackable registered options
4544
*
4645
* @return array Array of registered options.
4746
*/
4847
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-
}
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+
);
10157

10258
return $rest_options;
10359
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
* Internal dependencies
88
*/
99
import HelpTooltip from '../help-tooltip'
10-
import { fetchSettings, saveSettings } from '~stackable/util'
10+
import { fetchSettings } 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'
1819
import domReady from '@wordpress/dom-ready'
1920

2021
const LabelTooltip = props => {
@@ -137,9 +138,10 @@ function useHelpTooltipEnabled() {
137138
const updateHelpTooltipEnabled = newValue => {
138139
helpTooltipEnabled = newValue
139140

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

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

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@ import {
2323
ControlSeparator,
2424
} from '~stackable/components'
2525
import { useBlockHoverState } from '~stackable/hooks'
26-
import { extractColor, saveSettings } from '~stackable/util'
26+
import { extractColor } 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'
3334
import { __, sprintf } from '@wordpress/i18n'
3435
import { applyFilters, doAction } from '@wordpress/hooks'
3536

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

150151
saveTimeout = setTimeout( () => {
151-
saveSettings( {
152+
const settings = new models.Settings( {
152153
stackable_global_color_schemes: updatedColorSchemes, // eslint-disable-line camelcase
153154
// Clear the cached CSS when the color scheme is updated
154155
stackable_global_color_scheme_generated_css: '', // eslint-disable-line camelcase
155156
} )
157+
settings.save()
156158
}, 300 )
157159

158160
// Update our store.

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

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

1918
/**
2019
* WordPress dependencies
@@ -24,6 +23,7 @@ import { Fragment, useState } from '@wordpress/element'
2423
import { __ } from '@wordpress/i18n'
2524
import { ToggleControl } from '@wordpress/components'
2625
import { useSelect, dispatch } from '@wordpress/data'
26+
import { models } from '@wordpress/api'
2727

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

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

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

8889
return <>

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

Lines changed: 3 additions & 2 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'
1716

1817
import { useRef } from '@wordpress/element'
1918
import {
2019
select, dispatch, useSelect,
2120
} from '@wordpress/data'
21+
import { models } from '@wordpress/api'
2222
import { __, sprintf } from '@wordpress/i18n'
2323
import { ColorIndicator, ColorPicker } from '@wordpress/components'
2424

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

5455
// Update our store.

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

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

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

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

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

8687
const onChangeHideDefaultColors = value => {
8788
dispatch( 'stackable/global-colors' ).updateSettings( {
8889
hideDefaultColors: value,
8990
} )
9091

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

9496
const onChangeHideSiteEditorColors = value => {
9597
dispatch( 'stackable/global-colors' ).updateSettings( {
9698
hideSiteEditorColors: value,
9799
} )
98100

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

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

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

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

3434
let saveTypographyAsPresetsThrottle = null
3535

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

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

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

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

Lines changed: 12 additions & 6 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, saveSettings } from '~stackable/util'
26+
import { fetchSettings } from '~stackable/util'
2727
import { useDeviceType } from '~stackable/hooks'
2828
import {
2929
i18n, isPro, showProNotice,
@@ -38,6 +38,7 @@ import {
3838
import {
3939
Fragment, useEffect, useRef, useState,
4040
} from '@wordpress/element'
41+
import { models } from '@wordpress/api'
4142
import {
4243
addFilter, applyFilters, doAction,
4344
} from '@wordpress/hooks'
@@ -235,9 +236,10 @@ addFilter( 'stackable.global-settings.inspector', 'stackable/global-typography',
235236

236237
clearTimeout( saveTypographyThrottle )
237238
saveTypographyThrottle = setTimeout( () => {
238-
saveSettings( {
239+
const model = new models.Settings( {
239240
stackable_global_typography: [ newSettings ], // eslint-disable-line
240241
} )
242+
model.save()
241243
}, 500 )
242244
}
243245

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

247249
clearTimeout( saveSelectedFontPairThrottle )
248250
saveSelectedFontPairThrottle = setTimeout( () => {
249-
saveSettings( {
251+
const model = new models.Settings( {
250252
stackable_selected_font_pair: name, // eslint-disable-line
251253
} )
254+
model.save()
252255
}, 500 )
253256
}
254257

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

258261
clearTimeout( saveCustomFontPairsThrottle )
259262
saveCustomFontPairsThrottle = setTimeout( () => {
260-
saveSettings( {
263+
const model = new models.Settings( {
261264
stackable_custom_font_pairs: [ ...fontPairs ] , // eslint-disable-line
262265
} )
266+
model.save()
263267
}, 500 )
264268
}
265269

266270
const changeApplySettingsTo = value => {
267271
setApplySettingsTo( value )
268-
saveSettings( {
272+
const model = new models.Settings( {
269273
stackable_global_typography_apply_to: value, // eslint-disable-line
270274
} )
275+
model.save()
271276
}
272277

273278
const changeIsApplyBodyToHTML = value => {
274279
setIsApplyBodyToHTML( value )
275-
saveSettings( {
280+
const model = new models.Settings( {
276281
stackable_is_apply_body_to_html: value, // eslint-disable-line
277282
} )
283+
model.save()
278284
}
279285

280286
const updateTypeScale = value => {

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

Lines changed: 3 additions & 2 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'
1312

1413
/**
1514
* WordPress dependencies
1615
*/
1716
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,7 +52,8 @@ export const useBlockLayoutInspectorUtils = ( storeName, optionName, setDisplayH
5252
const saveSettings = newSettings => {
5353
clearTimeout( saveTimeout )
5454
saveTimeout = setTimeout( () => {
55-
saveAdminSettings( { [ optionName ]: newSettings } ) // eslint-disable-line camelcase
55+
const settings = new models.Settings( { [ optionName ]: newSettings } ) // eslint-disable-line camelcase
56+
settings.save()
5657
}, 300 )
5758

5859
// Update our store.

0 commit comments

Comments
 (0)