Skip to content

Commit

Permalink
feat: migration from from disabled blocks array to block states object
Browse files Browse the repository at this point in the history
  • Loading branch information
Arukuen committed Nov 19, 2024
1 parent c0848cf commit 052d5ef
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/disabled-blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const getVariationsToRemove = ( disabledBlocks, blockName ) => {
}

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

// If the block is hidden, set the inserter to false.
Expand Down
30 changes: 28 additions & 2 deletions src/editor-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function __construct() {
public function register_settings() {
register_setting(
'stackable_editor_settings',
'stackable_disabled_blocks',
'stackable_block_states',
// Use an object to store the block names as keys and the value that represents if disabled or hidden.
// Enabled blocks are not stored in the object to save memory.
array(
Expand Down Expand Up @@ -255,7 +255,7 @@ public function sanitize_array_setting( $input ) {
*/
public function add_settings( $settings ) {
$settings['stackable_google_maps_api_key'] = get_option( 'stackable_google_maps_api_key' );
$settings['stackable_disabled_blocks'] = get_option( 'stackable_disabled_blocks' );
$settings['stackable_block_states'] = get_option( 'stackable_block_states' );
$settings['stackable_enable_design_library'] = get_option( 'stackable_enable_design_library' );
$settings['stackable_optimize_inline_css'] = get_option( 'stackable_optimize_inline_css' );
$settings['stackable_auto_collapse_panels'] = get_option( 'stackable_auto_collapse_panels' );
Expand Down Expand Up @@ -298,6 +298,32 @@ public function add_nested_block_width( $css ) {

return $css;
}

/**
* Migrate from disabled blocks array to block states object.
*
* @return void
*/
function migrate_block_states() {
$old_setting_name = 'stackable_disabled_blocks';
$new_setting_name = 'stackable_block_states';

// Check if the old setting exists and the new setting is empty.
if ( get_option( $old_setting_name ) !== false && empty( get_option( $new_setting_name, [] ) ) ) {
$old_disabled_blocks = get_option( $old_setting_name, [] );
$new_block_states = [];

if ( is_array( $old_disabled_blocks ) ) {
foreach ( $old_disabled_blocks as $block_name ) {
// In the block_states, disabled is 1 and hidden is 2
$new_block_states[ $block_name ] = 1;
}
}

update_option( $new_setting_name, $new_block_states );
delete_option( $old_setting_name );
}
}
}

new Stackable_Editor_Settings();
Expand Down
12 changes: 6 additions & 6 deletions src/util/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -498,9 +498,9 @@ export const addStackableBlockCategory = () => {
*/
export const registerBlockType = ( name, _settings ) => {
// Do not register the block if the block is disabled.
if ( ( BLOCK_DEPENDENCIES[ name ] in stackableSettings.stackable_disabled_blocks &&
stackableSettings.stackable_disabled_blocks[ BLOCK_DEPENDENCIES[ name ] ] === BLOCK_STATE.DISABLED ) ||
stackableSettings.stackable_disabled_blocks[ name ] === BLOCK_STATE.DISABLED
if ( ( BLOCK_DEPENDENCIES[ name ] in stackableSettings.stackable_block_states &&
stackableSettings.stackable_block_states[ BLOCK_DEPENDENCIES[ name ] ] === BLOCK_STATE.DISABLED ) ||
stackableSettings.stackable_block_states[ name ] === BLOCK_STATE.DISABLED
) {
return
}
Expand Down Expand Up @@ -544,7 +544,7 @@ export const registerBlockType = ( name, _settings ) => {
* @return {Array} The resulting block definition
*/
export const substituteCoreIfDisabled = ( blockName, blockAttributes, children ) => {
const disabled_blocks = stackableSettings.stackable_disabled_blocks || {} // eslint-disable-line camelcase
const disabled_blocks = stackableSettings.stackable_block_states || {} // eslint-disable-line camelcase

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

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

if ( blockName in disabled_blocks && disabled_blocks[ blockName ] === BLOCK_STATE.DISABLED ) { // eslint-disable-line camelcase
return blockTree.filter( innerBlock => innerBlock[ 0 ] !== blockName )
Expand Down
14 changes: 7 additions & 7 deletions src/welcome/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -790,15 +790,15 @@ const Blocks = props => {

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

const [ isDisabledDialogOpen, setIsDisabledDialogOpen ] = useState( false )
const [ isEnabledDialogOpen, setIsEnabledDialogOpen ] = useState( false )
const [ currentToggleBlock, setCurrentToggleBlock ] = useState( '' )
const [ currentToggleBlockList, setCurrentToggleBlockList ] = useState( [] )

const enableAllBlocks = () => {
handleSettingsChange( { stackable_disabled_blocks: {} } ) // eslint-disable-line camelcase
handleSettingsChange( { stackable_block_states: {} } ) // eslint-disable-line camelcase
}

const disableAllBlocks = () => {
Expand All @@ -808,7 +808,7 @@ const Blocks = props => {
newDisabledBlocks[ block.name ] = BLOCK_STATE.DISABLED
} )
} )
handleSettingsChange( { stackable_disabled_blocks: newDisabledBlocks } ) // eslint-disable-line camelcase
handleSettingsChange( { stackable_block_states: newDisabledBlocks } ) // eslint-disable-line camelcase
}

const hideAllBlocks = () => {
Expand All @@ -818,7 +818,7 @@ const Blocks = props => {
newDisabledBlocks[ block.name ] = BLOCK_STATE.HIDDEN
} )
} )
handleSettingsChange( { stackable_disabled_blocks: newDisabledBlocks } ) // eslint-disable-line camelcase
handleSettingsChange( { stackable_block_states: newDisabledBlocks } ) // eslint-disable-line camelcase
}

const toggleBlock = ( name, value ) => {
Expand Down Expand Up @@ -849,7 +849,7 @@ const Blocks = props => {
} else {
newDisabledBlocks = { ...disabledBlocks, [ name ]: valueInt }
}
handleSettingsChange( { stackable_disabled_blocks: newDisabledBlocks } ) // eslint-disable-line camelcase
handleSettingsChange( { stackable_block_states: newDisabledBlocks } ) // eslint-disable-line camelcase
}

const handleDisableDialogConfirm = () => {
Expand All @@ -858,7 +858,7 @@ const Blocks = props => {
currentToggleBlockList.forEach( block => {
newDisabledBlocks[ block ] = BLOCK_STATE.DISABLED
} )
handleSettingsChange( { stackable_disabled_blocks: newDisabledBlocks } ) // eslint-disable-line camelcase
handleSettingsChange( { stackable_block_states: newDisabledBlocks } ) // eslint-disable-line camelcase
}

const handleEnableDialogConfirm = () => {
Expand All @@ -868,7 +868,7 @@ const Blocks = props => {
currentToggleBlockList.forEach( block => {
delete newDisabledBlocks[ block ]
} )
handleSettingsChange( { stackable_disabled_blocks: newDisabledBlocks } ) // eslint-disable-line camelcase
handleSettingsChange( { stackable_block_states: newDisabledBlocks } ) // eslint-disable-line camelcase
}

return (
Expand Down

0 comments on commit 052d5ef

Please sign in to comment.