| 1 |
/** |
| 2 |
* Deprecated Extensions. |
| 3 |
* |
| 4 |
* @since v3.1.0 |
| 5 |
*/ |
| 6 |
import { merge } from 'lodash'; |
| 7 |
|
| 8 |
import { createHigherOrderComponent } from '@wordpress/compose'; |
| 9 |
import { useEffect } from '@wordpress/element'; |
| 10 |
import { addFilter } from '@wordpress/hooks'; |
| 11 |
|
| 12 |
import migrateSR from './scroll-reveal'; |
| 13 |
import migrateStyles from './styles'; |
| 14 |
|
| 15 |
function DeprecatedExtensions(props) { |
| 16 |
const { setAttributes } = props; |
| 17 |
|
| 18 |
useEffect(() => { |
| 19 |
// We have tp use timeout to prevent conflicts with block `customStylesCallback`. |
| 20 |
setTimeout(() => { |
| 21 |
// Migration to new attributes. |
| 22 |
let result = {}; |
| 23 |
|
| 24 |
const newSR = migrateSR(props); |
| 25 |
const newStyles = migrateStyles(props); |
| 26 |
|
| 27 |
if (newSR) { |
| 28 |
result = merge(result, newSR); |
| 29 |
} |
| 30 |
if (newStyles) { |
| 31 |
result = merge(result, newStyles); |
| 32 |
} |
| 33 |
|
| 34 |
if (Object.keys(result).length) { |
| 35 |
setAttributes(result); |
| 36 |
} |
| 37 |
}, 0); |
| 38 |
// eslint-disable-next-line react-hooks/exhaustive-deps |
| 39 |
}, []); |
| 40 |
|
| 41 |
return null; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Override the default edit UI to include a new block inspector control for |
| 46 |
* assigning the custom styles if needed. |
| 47 |
* |
| 48 |
* @param {Function | Component} BlockEdit Original component. |
| 49 |
* |
| 50 |
* @return {string} Wrapped component. |
| 51 |
*/ |
| 52 |
const withInspectorControl = createHigherOrderComponent( |
| 53 |
(BlockEdit) => |
| 54 |
function (props) { |
| 55 |
return ( |
| 56 |
<> |
| 57 |
<BlockEdit {...props} /> |
| 58 |
<DeprecatedExtensions {...props} /> |
| 59 |
</> |
| 60 |
); |
| 61 |
}, |
| 62 |
'withInspectorControl' |
| 63 |
); |
| 64 |
|
| 65 |
/** |
| 66 |
* Add old attributes to all blocks to allow migrations from old attributes to new one. |
| 67 |
* |
| 68 |
* @param {Object} blockSettings Original block settings. |
| 69 |
* @param {string} name Original block name. |
| 70 |
* |
| 71 |
* @return {Object} Filtered block settings. |
| 72 |
*/ |
| 73 |
function addAttribute(blockSettings) { |
| 74 |
if (!blockSettings.attributes.ghostkitId) { |
| 75 |
blockSettings.attributes.ghostkitId = { |
| 76 |
type: 'string', |
| 77 |
}; |
| 78 |
} |
| 79 |
if (!blockSettings.attributes.ghostkitClassname) { |
| 80 |
blockSettings.attributes.ghostkitClassname = { |
| 81 |
type: 'string', |
| 82 |
}; |
| 83 |
} |
| 84 |
if (!blockSettings.attributes.ghostkitStyles) { |
| 85 |
blockSettings.attributes.ghostkitStyles = { |
| 86 |
type: 'object', |
| 87 |
}; |
| 88 |
} |
| 89 |
if (!blockSettings.attributes.ghostkitSR) { |
| 90 |
blockSettings.attributes.ghostkitSR = { |
| 91 |
type: 'string', |
| 92 |
}; |
| 93 |
} |
| 94 |
if (!blockSettings.attributes.ghostkitCustomCSS) { |
| 95 |
blockSettings.attributes.ghostkitCustomCSS = { |
| 96 |
type: 'string', |
| 97 |
}; |
| 98 |
} |
| 99 |
if (!blockSettings.attributes.ghostkitPosition) { |
| 100 |
blockSettings.attributes.ghostkitPosition = { |
| 101 |
type: 'object', |
| 102 |
}; |
| 103 |
} |
| 104 |
if (!blockSettings.attributes.ghostkitSpacings) { |
| 105 |
blockSettings.attributes.ghostkitSpacings = { |
| 106 |
type: 'object', |
| 107 |
}; |
| 108 |
} |
| 109 |
if (!blockSettings.attributes.ghostkitFrame) { |
| 110 |
blockSettings.attributes.ghostkitFrame = { |
| 111 |
type: 'object', |
| 112 |
}; |
| 113 |
} |
| 114 |
|
| 115 |
return blockSettings; |
| 116 |
} |
| 117 |
|
| 118 |
addFilter( |
| 119 |
'editor.BlockEdit', |
| 120 |
'ghostkit/deprecated/extensions', |
| 121 |
withInspectorControl |
| 122 |
); |
| 123 |
|
| 124 |
addFilter( |
| 125 |
'blocks.registerBlockType', |
| 126 |
'ghostkit/deprecated/extensions/additional-attributes', |
| 127 |
addAttribute |
| 128 |
); |
| 129 |
|