migrations
2 years ago
index.js
1 year ago
withButtonContainerLegacyMigration.js
2 years ago
withButtonLegacyMigration.js
2 years ago
withContainerLegacyMigration.js
2 years ago
withDeviceType.js
3 years ago
withDynamicTag.js
1 year ago
withGridLegacyMigration.js
2 years ago
withHeadlineLegacyMigration.js
2 years ago
withHtmlAttributes.js
1 year ago
withImageLegacyMigration.js
2 years ago
withSetAttributes.js
2 years ago
withStyles.js
1 year ago
withUniqueId.js
2 years ago
withButtonLegacyMigration.js
200 lines
| 1 | import { useEffect } from '@wordpress/element'; |
| 2 | import { getBlockType } from '@wordpress/blocks'; |
| 3 | import wasBlockJustInserted from '../utils/was-block-just-inserted'; |
| 4 | import isBlockVersionLessThan from '../utils/check-block-version'; |
| 5 | import hasNumericValue from '../utils/has-numeric-value'; |
| 6 | import migrateTypography from './migrations/migrateTypography'; |
| 7 | import migrateIconSizing from './migrations/migratingIconSizing'; |
| 8 | import migrateIconPadding from './migrations/migrateIconPadding'; |
| 9 | import { migrationPipe, updateBlockVersion } from './migrations/utils'; |
| 10 | import { isEmpty } from 'lodash'; |
| 11 | import migrateSpacing from './migrations/migrateSpacing'; |
| 12 | import migrateBorders from './migrations/migrateBorders'; |
| 13 | |
| 14 | function oldMigrations( attrs, existingAttrs, mode ) { |
| 15 | if ( 'css' === mode ) { |
| 16 | return attrs; |
| 17 | } |
| 18 | |
| 19 | if ( ! existingAttrs.hasIcon && !! existingAttrs.icon ) { |
| 20 | attrs.hasIcon = true; |
| 21 | } |
| 22 | |
| 23 | if ( ! existingAttrs.hasUrl && !! existingAttrs.url ) { |
| 24 | attrs.hasUrl = true; |
| 25 | } |
| 26 | |
| 27 | return attrs; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Set our old defaults as static values. |
| 32 | * |
| 33 | * @param {Object} Props Function props. |
| 34 | * @param {number} Props.blockVersionLessThan The version blocks should be less than for this to run. |
| 35 | * @param {Object} Props.oldDefaults Old defaults that were changed and need to be added to attributes. |
| 36 | * @return {Object} Updated attributes. |
| 37 | * @since 1.4.0 |
| 38 | */ |
| 39 | export function migrateOldButtonDefaults( { blockVersionLessThan, oldDefaults } ) { |
| 40 | return function( attrs, existingAttrs, mode ) { |
| 41 | if ( 'css' === mode ) { |
| 42 | return attrs; |
| 43 | } |
| 44 | |
| 45 | if ( ! wasBlockJustInserted( existingAttrs ) && isBlockVersionLessThan( existingAttrs.blockVersion, blockVersionLessThan ) ) { |
| 46 | const items = []; |
| 47 | |
| 48 | if ( existingAttrs.gradient ) { |
| 49 | items.push( |
| 50 | 'gradientDirection', |
| 51 | 'gradientColorOne', |
| 52 | 'gradientColorOneOpacity', |
| 53 | 'gradientColorTwo', |
| 54 | 'gradientColorTwoOpacity' |
| 55 | ); |
| 56 | } |
| 57 | |
| 58 | items.forEach( ( item ) => { |
| 59 | if ( ! hasNumericValue( existingAttrs[ item ] ) ) { |
| 60 | attrs[ item ] = oldDefaults[ item ]; |
| 61 | } |
| 62 | } ); |
| 63 | } |
| 64 | |
| 65 | return attrs; |
| 66 | }; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Set our layout attributes for old Button blocks. |
| 71 | * |
| 72 | * @param {Object} Props Function props. |
| 73 | * @param {number} Props.blockVersionLessThan The version blocks should be less than for this to run. |
| 74 | * @return {Object} Updated attributes. |
| 75 | * @since 1.7.0 |
| 76 | */ |
| 77 | export function migrateButtonLayout( { blockVersionLessThan } ) { |
| 78 | return function( attrs, existingAttrs, mode ) { |
| 79 | if ( 'css' === mode ) { |
| 80 | return attrs; |
| 81 | } |
| 82 | |
| 83 | if ( ! wasBlockJustInserted( existingAttrs ) && isBlockVersionLessThan( existingAttrs.blockVersion, blockVersionLessThan ) && ! existingAttrs.useGlobalStyle ) { |
| 84 | attrs = { |
| 85 | ...attrs, |
| 86 | display: 'inline-flex', |
| 87 | alignItems: 'center', |
| 88 | justifyContent: 'center', |
| 89 | alignment: 'center', |
| 90 | }; |
| 91 | } |
| 92 | |
| 93 | return attrs; |
| 94 | }; |
| 95 | } |
| 96 | |
| 97 | export const currentBlockVersion = 4; |
| 98 | |
| 99 | /** |
| 100 | * Migrate our Button attributes. |
| 101 | * |
| 102 | * @param {Object} Props Function props. |
| 103 | * @param {Object} Props.attributes The block attributes. |
| 104 | * @param {Object} Props.defaults The block defaults. |
| 105 | * @param {string} Props.mode The migration mode. |
| 106 | * @param {Object} Props.oldDefaults An object of old defaults keyed by version. |
| 107 | * @return {Object} Updated attributes. |
| 108 | * @since 1.8.0 |
| 109 | */ |
| 110 | export function migrateButtonAttributes( { attributes, defaults, mode = '', oldDefaults = {} } ) { |
| 111 | return migrationPipe( |
| 112 | attributes, |
| 113 | [ |
| 114 | oldMigrations, |
| 115 | migrateOldButtonDefaults( { |
| 116 | blockVersionLessThan: 2, |
| 117 | oldDefaults: oldDefaults.v1_4_0, |
| 118 | } ), |
| 119 | migrateButtonLayout( { |
| 120 | blockVersionLessThan: 3, |
| 121 | } ), |
| 122 | migrateSpacing( { |
| 123 | blockVersionLessThan: 4, |
| 124 | defaults, |
| 125 | attributesToMigrate: [ |
| 126 | 'paddingTop', |
| 127 | 'paddingRight', |
| 128 | 'paddingBottom', |
| 129 | 'paddingLeft', |
| 130 | 'marginTop', |
| 131 | 'marginRight', |
| 132 | 'marginBottom', |
| 133 | 'marginLeft', |
| 134 | ], |
| 135 | } ), |
| 136 | migrateBorders( { |
| 137 | blockVersionLessThan: 4, |
| 138 | defaults, |
| 139 | attributesToMigrate: [ |
| 140 | 'borderSizeTop', |
| 141 | 'borderSizeRight', |
| 142 | 'borderSizeBottom', |
| 143 | 'borderSizeLeft', |
| 144 | 'borderRadiusTopRight', |
| 145 | 'borderRadiusBottomRight', |
| 146 | 'borderRadiusBottomLeft', |
| 147 | 'borderRadiusTopLeft', |
| 148 | ], |
| 149 | } ), |
| 150 | migrateTypography( { |
| 151 | blockVersionLessThan: 4, |
| 152 | defaults, |
| 153 | attributesToMigrate: [ |
| 154 | 'fontFamily', |
| 155 | 'fontSize', |
| 156 | 'letterSpacing', |
| 157 | 'fontWeight', |
| 158 | 'textTransform', |
| 159 | 'alignment', |
| 160 | ], |
| 161 | } ), |
| 162 | migrateIconSizing( { |
| 163 | blockVersionLessThan: 4, |
| 164 | defaults, |
| 165 | } ), |
| 166 | migrateIconPadding( { |
| 167 | blockVersionLessThan: 4, |
| 168 | defaults, |
| 169 | } ), |
| 170 | updateBlockVersion( currentBlockVersion ), |
| 171 | ], |
| 172 | mode |
| 173 | ); |
| 174 | } |
| 175 | |
| 176 | export default ( WrappedComponent ) => { |
| 177 | return ( props ) => { |
| 178 | const { |
| 179 | attributes, |
| 180 | setAttributes, |
| 181 | } = props; |
| 182 | |
| 183 | useEffect( () => { |
| 184 | const newAttributes = migrateButtonAttributes( { |
| 185 | attributes, |
| 186 | defaults: getBlockType( 'generateblocks/button' )?.attributes, |
| 187 | oldDefaults: { |
| 188 | v1_4_0: generateBlocksLegacyDefaults.v_1_4_0.button, |
| 189 | }, |
| 190 | } ); |
| 191 | |
| 192 | if ( ! isEmpty( newAttributes ) ) { |
| 193 | setAttributes( newAttributes ); |
| 194 | } |
| 195 | }, [] ); |
| 196 | |
| 197 | return ( <WrappedComponent { ...props } /> ); |
| 198 | }; |
| 199 | }; |
| 200 |