Skip to content

Commit 052d5ef

Browse files
committed
feat: migration from from disabled blocks array to block states object
1 parent c0848cf commit 052d5ef

File tree

4 files changed

+42
-16
lines changed

4 files changed

+42
-16
lines changed

src/disabled-blocks.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const getVariationsToRemove = ( disabledBlocks, blockName ) => {
4141
}
4242

4343
const applySettingsToMeta = metadata => {
44-
const disabledBlocks = settings.stackable_disabled_blocks || {} // eslint-disable-line camelcase
44+
const disabledBlocks = settings.stackable_block_states || {} // eslint-disable-line camelcase
4545
let inserter = true
4646

4747
// If the block is hidden, set the inserter to false.

src/editor-settings.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function __construct() {
3232
public function register_settings() {
3333
register_setting(
3434
'stackable_editor_settings',
35-
'stackable_disabled_blocks',
35+
'stackable_block_states',
3636
// Use an object to store the block names as keys and the value that represents if disabled or hidden.
3737
// Enabled blocks are not stored in the object to save memory.
3838
array(
@@ -255,7 +255,7 @@ public function sanitize_array_setting( $input ) {
255255
*/
256256
public function add_settings( $settings ) {
257257
$settings['stackable_google_maps_api_key'] = get_option( 'stackable_google_maps_api_key' );
258-
$settings['stackable_disabled_blocks'] = get_option( 'stackable_disabled_blocks' );
258+
$settings['stackable_block_states'] = get_option( 'stackable_block_states' );
259259
$settings['stackable_enable_design_library'] = get_option( 'stackable_enable_design_library' );
260260
$settings['stackable_optimize_inline_css'] = get_option( 'stackable_optimize_inline_css' );
261261
$settings['stackable_auto_collapse_panels'] = get_option( 'stackable_auto_collapse_panels' );
@@ -298,6 +298,32 @@ public function add_nested_block_width( $css ) {
298298

299299
return $css;
300300
}
301+
302+
/**
303+
* Migrate from disabled blocks array to block states object.
304+
*
305+
* @return void
306+
*/
307+
function migrate_block_states() {
308+
$old_setting_name = 'stackable_disabled_blocks';
309+
$new_setting_name = 'stackable_block_states';
310+
311+
// Check if the old setting exists and the new setting is empty.
312+
if ( get_option( $old_setting_name ) !== false && empty( get_option( $new_setting_name, [] ) ) ) {
313+
$old_disabled_blocks = get_option( $old_setting_name, [] );
314+
$new_block_states = [];
315+
316+
if ( is_array( $old_disabled_blocks ) ) {
317+
foreach ( $old_disabled_blocks as $block_name ) {
318+
// In the block_states, disabled is 1 and hidden is 2
319+
$new_block_states[ $block_name ] = 1;
320+
}
321+
}
322+
323+
update_option( $new_setting_name, $new_block_states );
324+
delete_option( $old_setting_name );
325+
}
326+
}
301327
}
302328

303329
new Stackable_Editor_Settings();

src/util/blocks.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -498,9 +498,9 @@ export const addStackableBlockCategory = () => {
498498
*/
499499
export const registerBlockType = ( name, _settings ) => {
500500
// Do not register the block if the block is disabled.
501-
if ( ( BLOCK_DEPENDENCIES[ name ] in stackableSettings.stackable_disabled_blocks &&
502-
stackableSettings.stackable_disabled_blocks[ BLOCK_DEPENDENCIES[ name ] ] === BLOCK_STATE.DISABLED ) ||
503-
stackableSettings.stackable_disabled_blocks[ name ] === BLOCK_STATE.DISABLED
501+
if ( ( BLOCK_DEPENDENCIES[ name ] in stackableSettings.stackable_block_states &&
502+
stackableSettings.stackable_block_states[ BLOCK_DEPENDENCIES[ name ] ] === BLOCK_STATE.DISABLED ) ||
503+
stackableSettings.stackable_block_states[ name ] === BLOCK_STATE.DISABLED
504504
) {
505505
return
506506
}
@@ -544,7 +544,7 @@ export const registerBlockType = ( name, _settings ) => {
544544
* @return {Array} The resulting block definition
545545
*/
546546
export const substituteCoreIfDisabled = ( blockName, blockAttributes, children ) => {
547-
const disabled_blocks = stackableSettings.stackable_disabled_blocks || {} // eslint-disable-line camelcase
547+
const disabled_blocks = stackableSettings.stackable_block_states || {} // eslint-disable-line camelcase
548548

549549
if ( blockName === 'stackable/text' ) {
550550
if ( blockName in disabled_blocks && disabled_blocks[ blockName ] === BLOCK_STATE.DISABLED ) { // eslint-disable-line camelcase
@@ -618,7 +618,7 @@ export const substituteCoreIfDisabled = ( blockName, blockAttributes, children )
618618
* @return {Array} The resulting block definition
619619
*/
620620
export const substituteIfDisabled = ( blockNames, originalBlockDefinition, substituteBlockDefinition ) => {
621-
const disabled_blocks = stackableSettings.stackable_disabled_blocks || {} // eslint-disable-line camelcase
621+
const disabled_blocks = stackableSettings.stackable_block_states || {} // eslint-disable-line camelcase
622622

623623
for ( const blockName of blockNames ) {
624624
if ( blockName in disabled_blocks && disabled_blocks[ blockName ] === BLOCK_STATE.DISABLED ) { // eslint-disable-line camelcase
@@ -638,7 +638,7 @@ export const substituteIfDisabled = ( blockNames, originalBlockDefinition, subst
638638
* @return {Array} The resulting block tree definition
639639
*/
640640
export const removeChildIfDisabled = ( blockName, blockTree ) => {
641-
const disabled_blocks = stackableSettings.stackable_disabled_blocks || {} // eslint-disable-line camelcase
641+
const disabled_blocks = stackableSettings.stackable_block_states || {} // eslint-disable-line camelcase
642642

643643
if ( blockName in disabled_blocks && disabled_blocks[ blockName ] === BLOCK_STATE.DISABLED ) { // eslint-disable-line camelcase
644644
return blockTree.filter( innerBlock => innerBlock[ 0 ] !== blockName )

src/welcome/admin.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -790,15 +790,15 @@ const Blocks = props => {
790790

791791
const DERIVED_BLOCKS = getAllBlocks()
792792
const groups = filteredSearchTree.find( tab => tab.id === 'blocks' ).groups
793-
const disabledBlocks = settings.stackable_disabled_blocks ?? {} // eslint-disable-line camelcase
793+
const disabledBlocks = settings.stackable_block_states ?? {} // eslint-disable-line camelcase
794794

795795
const [ isDisabledDialogOpen, setIsDisabledDialogOpen ] = useState( false )
796796
const [ isEnabledDialogOpen, setIsEnabledDialogOpen ] = useState( false )
797797
const [ currentToggleBlock, setCurrentToggleBlock ] = useState( '' )
798798
const [ currentToggleBlockList, setCurrentToggleBlockList ] = useState( [] )
799799

800800
const enableAllBlocks = () => {
801-
handleSettingsChange( { stackable_disabled_blocks: {} } ) // eslint-disable-line camelcase
801+
handleSettingsChange( { stackable_block_states: {} } ) // eslint-disable-line camelcase
802802
}
803803

804804
const disableAllBlocks = () => {
@@ -808,7 +808,7 @@ const Blocks = props => {
808808
newDisabledBlocks[ block.name ] = BLOCK_STATE.DISABLED
809809
} )
810810
} )
811-
handleSettingsChange( { stackable_disabled_blocks: newDisabledBlocks } ) // eslint-disable-line camelcase
811+
handleSettingsChange( { stackable_block_states: newDisabledBlocks } ) // eslint-disable-line camelcase
812812
}
813813

814814
const hideAllBlocks = () => {
@@ -818,7 +818,7 @@ const Blocks = props => {
818818
newDisabledBlocks[ block.name ] = BLOCK_STATE.HIDDEN
819819
} )
820820
} )
821-
handleSettingsChange( { stackable_disabled_blocks: newDisabledBlocks } ) // eslint-disable-line camelcase
821+
handleSettingsChange( { stackable_block_states: newDisabledBlocks } ) // eslint-disable-line camelcase
822822
}
823823

824824
const toggleBlock = ( name, value ) => {
@@ -849,7 +849,7 @@ const Blocks = props => {
849849
} else {
850850
newDisabledBlocks = { ...disabledBlocks, [ name ]: valueInt }
851851
}
852-
handleSettingsChange( { stackable_disabled_blocks: newDisabledBlocks } ) // eslint-disable-line camelcase
852+
handleSettingsChange( { stackable_block_states: newDisabledBlocks } ) // eslint-disable-line camelcase
853853
}
854854

855855
const handleDisableDialogConfirm = () => {
@@ -858,7 +858,7 @@ const Blocks = props => {
858858
currentToggleBlockList.forEach( block => {
859859
newDisabledBlocks[ block ] = BLOCK_STATE.DISABLED
860860
} )
861-
handleSettingsChange( { stackable_disabled_blocks: newDisabledBlocks } ) // eslint-disable-line camelcase
861+
handleSettingsChange( { stackable_block_states: newDisabledBlocks } ) // eslint-disable-line camelcase
862862
}
863863

864864
const handleEnableDialogConfirm = () => {
@@ -868,7 +868,7 @@ const Blocks = props => {
868868
currentToggleBlockList.forEach( block => {
869869
delete newDisabledBlocks[ block ]
870870
} )
871-
handleSettingsChange( { stackable_disabled_blocks: newDisabledBlocks } ) // eslint-disable-line camelcase
871+
handleSettingsChange( { stackable_block_states: newDisabledBlocks } ) // eslint-disable-line camelcase
872872
}
873873

874874
return (

0 commit comments

Comments
 (0)