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
withContainerLegacyMigration.js
114 lines
| 1 | import { useEffect } from '@wordpress/element'; |
| 2 | import hasNumericValue from '../utils/has-numeric-value'; |
| 3 | import wasBlockJustInserted from '../utils/was-block-just-inserted'; |
| 4 | import isBlockVersionLessThan from '../utils/check-block-version'; |
| 5 | |
| 6 | export default ( WrappedComponent ) => { |
| 7 | return ( props ) => { |
| 8 | const { |
| 9 | attributes, |
| 10 | setAttributes, |
| 11 | } = props; |
| 12 | |
| 13 | useEffect( () => { |
| 14 | // This block used to be static. Set it to dynamic by default from now on. |
| 15 | if ( 'undefined' === typeof attributes.isDynamic || ! attributes.isDynamic ) { |
| 16 | setAttributes( { isDynamic: true } ); |
| 17 | } |
| 18 | |
| 19 | // Set our inner z-index if we're using a gradient overlay or pseudo background. |
| 20 | // @since 1.4.0. |
| 21 | if ( 'undefined' === typeof attributes.blockVersion || attributes.blockVersion < 2 ) { |
| 22 | let updateOldZindex = |
| 23 | attributes.gradient && 'pseudo-element' === attributes.gradientSelector && |
| 24 | ! hasNumericValue( attributes.innerZindex ); |
| 25 | |
| 26 | if ( ! updateOldZindex ) { |
| 27 | updateOldZindex = !! attributes.bgImage && 'undefined' !== typeof attributes.bgOptions.selector && 'pseudo-element' === attributes.bgOptions.selector; |
| 28 | } |
| 29 | |
| 30 | if ( ! updateOldZindex ) { |
| 31 | updateOldZindex = 'undefined' !== typeof attributes.useAdvBackgrounds && attributes.useAdvBackgrounds; |
| 32 | } |
| 33 | |
| 34 | if ( updateOldZindex ) { |
| 35 | setAttributes( { innerZindex: 1 } ); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | // Set our old defaults as static values. |
| 40 | // @since 1.4.0. |
| 41 | if ( ! wasBlockJustInserted( attributes ) && isBlockVersionLessThan( attributes.blockVersion, 2 ) ) { |
| 42 | const legacyDefaults = generateBlocksLegacyDefaults.v_1_4_0.container; |
| 43 | const useGlobalStyle = 'undefined' !== typeof attributes.useGlobalStyle && attributes.useGlobalStyle; |
| 44 | |
| 45 | const newAttrs = {}; |
| 46 | const items = []; |
| 47 | |
| 48 | if ( ! useGlobalStyle ) { |
| 49 | items.push( |
| 50 | 'paddingTop', |
| 51 | 'paddingRight', |
| 52 | 'paddingBottom', |
| 53 | 'paddingLeft', |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | if ( attributes.isGrid ) { |
| 58 | items.push( |
| 59 | 'width', |
| 60 | 'widthMobile', |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | if ( attributes.gradient ) { |
| 65 | items.push( |
| 66 | 'gradientDirection', |
| 67 | 'gradientColorOne', |
| 68 | 'gradientColorOneOpacity', |
| 69 | 'gradientColorTwo', |
| 70 | 'gradientColorTwoOpacity' |
| 71 | ); |
| 72 | } |
| 73 | |
| 74 | items.forEach( ( item ) => { |
| 75 | if ( ! hasNumericValue( attributes[ item ] ) ) { |
| 76 | newAttrs[ item ] = legacyDefaults[ item ]; |
| 77 | } |
| 78 | } ); |
| 79 | |
| 80 | if ( Object.keys( newAttrs ).length > 0 ) { |
| 81 | setAttributes( newAttrs ); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | // Attribute defaults added to an object late don't get defaults. |
| 86 | // @since 1.1.2 |
| 87 | if ( 'undefined' === typeof attributes.bgOptions.selector ) { |
| 88 | setAttributes( { |
| 89 | bgOptions: { |
| 90 | ...attributes.bgOptions, |
| 91 | selector: 'element', |
| 92 | }, |
| 93 | } ); |
| 94 | } |
| 95 | |
| 96 | if ( 'undefined' === typeof attributes.bgOptions.opacity ) { |
| 97 | setAttributes( { |
| 98 | bgOptions: { |
| 99 | ...attributes.bgOptions, |
| 100 | opacity: 1, |
| 101 | }, |
| 102 | } ); |
| 103 | } |
| 104 | |
| 105 | // Update block version flag if it's out of date. |
| 106 | if ( isBlockVersionLessThan( attributes.blockVersion, 2 ) ) { |
| 107 | setAttributes( { blockVersion: 2 } ); |
| 108 | } |
| 109 | }, [] ); |
| 110 | |
| 111 | return ( <WrappedComponent { ...props } /> ); |
| 112 | }; |
| 113 | }; |
| 114 |