Skip to content

Commit ce117fb

Browse files
Merge branch 'feat-granular-plugin-setting' of https://github.com/gambitph/Stackable into feat-granular-plugin-setting
2 parents bf63341 + ab38887 commit ce117fb

File tree

4 files changed

+47
-15
lines changed

4 files changed

+47
-15
lines changed

src/block/column/block.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@
2020
],
2121
"textdomain": "stackable-ultimate-gutenberg-blocks",
2222
"stk-type": "hidden",
23-
"stk-cannot-be-disabled": true
23+
"stk-available-states": [ "enabled", "hidden" ]
2424
}

src/block/columns/block.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@
2020
"textdomain": "stackable-ultimate-gutenberg-blocks",
2121
"stk-type": "essential",
2222
"stk-demo": "https://wpstackable.com/columns-block/?utm_source=welcome&utm_medium=settings&utm_campaign=view_demo&utm_content=demolink",
23-
"stk-cannot-be-disabled": true
23+
"stk-available-states": [ "enabled", "hidden" ]
2424
}

src/components/admin-toolbar-setting/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ const AdminToolbarSetting = props => {
2626
props.controls.map( option => {
2727
const isSelected = props.value ? props.value === option.value : props.placeholder === option.value
2828
const tabindex = isSelected ? '0' : '-1'
29-
const isDisabled = props.disabledValues ? props.disabledValues.includes( option.value ) : false
29+
// If no available states provided, all states are allowed.
30+
const isAvailable = props.availableStates ? props.availableStates.includes( option.value ) : true
3031

31-
// Do not show the button if disabled.
32-
if ( isDisabled ) {
32+
// Do not show the button if not available.
33+
if ( ! isAvailable ) {
3334
return null
3435
}
3536

@@ -40,7 +41,6 @@ const AdminToolbarSetting = props => {
4041
label={ option.title || props.label }
4142
tabIndex={ tabindex }
4243
aria-pressed={ isSelected }
43-
disabled={ isDisabled }
4444
onClick={ () => {
4545
if ( option.value === props.value ) {
4646
return

src/welcome/admin.js

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,11 @@ const Blocks = props => {
872872
filteredSearchTree,
873873
} = props
874874

875+
const BLOCK_STATE_MAP = Object.freeze( {
876+
enabled: BLOCK_STATE.ENABLED,
877+
hidden: BLOCK_STATE.HIDDEN,
878+
disabled: BLOCK_STATE.DISABLED,
879+
} )
875880
const DERIVED_BLOCKS = getAllBlocks()
876881
const groups = filteredSearchTree.find( tab => tab.id === 'blocks' ).groups
877882
const groupLength = groups.reduce( ( acc, curr ) => acc + curr.children.length, 0 )
@@ -882,19 +887,38 @@ const Blocks = props => {
882887
const [ currentToggleBlock, setCurrentToggleBlock ] = useState( '' )
883888
const [ currentToggleBlockList, setCurrentToggleBlockList ] = useState( [] )
884889

890+
// Map string states to integer block states
891+
const mapStringStates = states => {
892+
return states?.map(
893+
state => BLOCK_STATE_MAP[ state.toLowerCase() ]
894+
)
895+
}
896+
885897
const enableAllBlocks = () => {
886-
handleSettingsChange( { stackable_block_states: {} } ) // eslint-disable-line camelcase
898+
const newDisabledBlocks = {}
899+
BLOCK_CATEROGIES.forEach( ( { id } ) => {
900+
DERIVED_BLOCKS[ id ].forEach( block => {
901+
const availableStates = mapStringStates( block[ 'stk-available-states' ] )
902+
// Retain previous state if cannot be enabled
903+
if ( availableStates && ! availableStates.includes( BLOCK_STATE.ENABLED ) && disabledBlocks[ block.name ] ) {
904+
newDisabledBlocks[ block.name ] = disabledBlocks[ block.name ]
905+
}
906+
} )
907+
} )
908+
handleSettingsChange( { stackable_block_states: newDisabledBlocks } ) // eslint-disable-line camelcase
887909
}
888910

889911
const disableAllBlocks = () => {
890912
const newDisabledBlocks = {}
891913
BLOCK_CATEROGIES.forEach( ( { id } ) => {
892914
DERIVED_BLOCKS[ id ].forEach( block => {
893-
// If the block cannot be disabled, default to hidden.
894-
if ( block[ 'stk-cannot-be-disabled' ] ) {
895-
newDisabledBlocks[ block.name ] = BLOCK_STATE.HIDDEN
896-
} else {
915+
const availableStates = mapStringStates( block[ 'stk-available-states' ] )
916+
if ( ! availableStates || availableStates.includes( BLOCK_STATE.DISABLED ) ) {
897917
newDisabledBlocks[ block.name ] = BLOCK_STATE.DISABLED
918+
} else if ( availableStates.includes( BLOCK_STATE.HIDDEN ) ) { // If the block cannot be disabled, default to hidden.
919+
newDisabledBlocks[ block.name ] = BLOCK_STATE.HIDDEN
920+
} else if ( disabledBlocks[ block.name ] ) { // Retain previous state if cannot be disabled or hidden
921+
newDisabledBlocks[ block.name ] = disabledBlocks[ block.name ]
898922
}
899923
} )
900924
} )
@@ -905,7 +929,12 @@ const Blocks = props => {
905929
const newDisabledBlocks = {}
906930
BLOCK_CATEROGIES.forEach( ( { id } ) => {
907931
DERIVED_BLOCKS[ id ].forEach( block => {
908-
newDisabledBlocks[ block.name ] = BLOCK_STATE.HIDDEN
932+
const availableStates = mapStringStates( block[ 'stk-available-states' ] )
933+
if ( ! availableStates || availableStates.includes( BLOCK_STATE.HIDDEN ) ) {
934+
newDisabledBlocks[ block.name ] = BLOCK_STATE.HIDDEN
935+
} else if ( disabledBlocks[ block.name ] ) { // Retain previous state if cannot be hidden
936+
newDisabledBlocks[ block.name ] = disabledBlocks[ block.name ]
937+
}
909938
} )
910939
} )
911940
handleSettingsChange( { stackable_block_states: newDisabledBlocks } ) // eslint-disable-line camelcase
@@ -1016,8 +1045,11 @@ const Blocks = props => {
10161045
<div className="s-settings-grid">
10171046
{ DERIVED_BLOCKS[ id ].map( ( block, i ) => {
10181047
const blockState = disabledBlocks[ block.name ] ?? BLOCK_STATE.ENABLED
1019-
const disabledValues = block[ 'stk-cannot-be-disabled' ] ? [ BLOCK_STATE.DISABLED ] : null
1020-
1048+
const availableStates = mapStringStates( block[ 'stk-available-states' ] )
1049+
// If there is only one state, do not render the block
1050+
if ( availableStates && availableStates.length <= 1 ) {
1051+
return null
1052+
}
10211053
return (
10221054
<AdminToolbarSetting
10231055
key={ i }
@@ -1043,7 +1075,7 @@ const Blocks = props => {
10431075
selectedColor: '#de0000',
10441076
},
10451077
] }
1046-
disabledValues={ disabledValues }
1078+
availableStates={ availableStates }
10471079
onChange={ value => {
10481080
toggleBlock( block.name, value )
10491081
} }

0 commit comments

Comments
 (0)