index.js
4 years ago
withButtonContainerLegacyMigration.js
4 years ago
withButtonLegacyMigration.js
4 years ago
withContainerLegacyMigration.js
4 years ago
withDocumentation.js
4 years ago
withGridLegacyMigration.js
4 years ago
withResponsiveTabs.js
4 years ago
withUniqueId.js
4 years ago
withButtonLegacyMigration.js
69 lines
| 1 | import { useEffect } from '@wordpress/element'; |
| 2 | import wasBlockJustInserted from '../utils/was-block-just-inserted'; |
| 3 | import isBlockVersionLessThan from '../utils/check-block-version'; |
| 4 | import hasNumericValue from '../utils/has-numeric-value'; |
| 5 | |
| 6 | export default ( WrappedComponent ) => { |
| 7 | return ( props ) => { |
| 8 | const { |
| 9 | attributes, |
| 10 | setAttributes, |
| 11 | } = props; |
| 12 | |
| 13 | const { |
| 14 | hasIcon, |
| 15 | icon, |
| 16 | hasUrl, |
| 17 | url, |
| 18 | blockVersion, |
| 19 | gradient, |
| 20 | } = attributes; |
| 21 | |
| 22 | useEffect( () => { |
| 23 | if ( ! hasIcon && icon ) { |
| 24 | setAttributes( { hasIcon: true } ); |
| 25 | } |
| 26 | |
| 27 | if ( ! hasUrl ) { |
| 28 | setAttributes( { hasUrl: ( !! url ) } ); |
| 29 | } |
| 30 | |
| 31 | // Set our old defaults as static values. |
| 32 | // @since 1.4.0. |
| 33 | if ( ! wasBlockJustInserted( attributes ) && isBlockVersionLessThan( blockVersion, 2 ) ) { |
| 34 | const legacyDefaults = generateBlocksLegacyDefaults.v_1_4_0.button; |
| 35 | |
| 36 | const newAttrs = {}; |
| 37 | const items = []; |
| 38 | |
| 39 | if ( gradient ) { |
| 40 | items.push( |
| 41 | 'gradientDirection', |
| 42 | 'gradientColorOne', |
| 43 | 'gradientColorOneOpacity', |
| 44 | 'gradientColorTwo', |
| 45 | 'gradientColorTwoOpacity' |
| 46 | ); |
| 47 | } |
| 48 | |
| 49 | items.forEach( ( item ) => { |
| 50 | if ( ! hasNumericValue( attributes[ item ] ) ) { |
| 51 | newAttrs[ item ] = legacyDefaults[ item ]; |
| 52 | } |
| 53 | } ); |
| 54 | |
| 55 | if ( Object.keys( newAttrs ).length > 0 ) { |
| 56 | setAttributes( newAttrs ); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | // Update block version flag if it's out of date. |
| 61 | if ( isBlockVersionLessThan( blockVersion, 2 ) ) { |
| 62 | setAttributes( { blockVersion: 2 } ); |
| 63 | } |
| 64 | }, [] ); |
| 65 | |
| 66 | return ( <WrappedComponent { ...props } /> ); |
| 67 | }; |
| 68 | }; |
| 69 |