PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 1.8.3
GenerateBlocks v1.8.3
trunk 1.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 2.0.0 2.0.1 2.0.2 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.3.0
generateblocks / src / hoc / withContainerLegacyMigration.js
generateblocks / src / hoc Last commit date
migrations 2 years ago index.js 2 years ago withButtonContainerLegacyMigration.js 2 years ago withButtonLegacyMigration.js 2 years ago withContainerLegacyMigration.js 2 years ago withDeviceType.js 3 years ago withDocumentation.js 4 years ago withGridLegacyMigration.js 2 years ago withHeadlineLegacyMigration.js 2 years ago withImageLegacyMigration.js 2 years ago withSetAttributes.js 2 years ago withUniqueId.js 4 years ago
withContainerLegacyMigration.js
301 lines
1 import { useEffect } from '@wordpress/element';
2 import { getBlockType } from '@wordpress/blocks';
3 import hasNumericValue from '../utils/has-numeric-value';
4 import wasBlockJustInserted from '../utils/was-block-just-inserted';
5 import isBlockVersionLessThan from '../utils/check-block-version';
6 import { migrationPipe, updateBlockVersion, setIsDynamic } from './migrations/utils';
7 import migrateSpacing from './migrations/migrateSpacing';
8 import migrateBorders from './migrations/migrateBorders';
9 import migrateTypography from './migrations/migrateTypography';
10 import migrateSizing from './migrations/migrateSizing';
11 import { isEmpty } from 'lodash';
12
13 /**
14 * Add late-added attributes to the bgOptions object.
15 * This was to prevent an error where `selector` was undefined.
16 *
17 * @param {Object} attrs New attributes from previous migrations.
18 * @param {Object} existingAttrs Pre-existing block attributes.
19 * @param {string} mode The migration mode.
20 * @return {Object} Updated attributes.
21 * @since 1.1.2
22 */
23 export function migrateBgSelectorOpacity( attrs, existingAttrs, mode ) {
24 if ( 'css' === mode ) {
25 return attrs;
26 }
27
28 if ( ! existingAttrs.bgOptions || ! Object.keys( existingAttrs.bgOptions ).length ) {
29 return attrs;
30 }
31
32 if ( 'undefined' === typeof existingAttrs.bgOptions.selector ) {
33 attrs = {
34 ...attrs,
35 bgOptions: {
36 ...existingAttrs.bgOptions,
37 ...attrs.bgOptions,
38 selector: 'element',
39 },
40 };
41 }
42
43 if ( 'undefined' === typeof existingAttrs.bgOptions.opacity ) {
44 attrs = {
45 ...attrs,
46 bgOptions: {
47 ...existingAttrs.bgOptions,
48 ...attrs.bgOptions,
49 opacity: 1,
50 },
51 };
52 }
53
54 return attrs;
55 }
56
57 /**
58 * Set our old defaults as static values.
59 *
60 * @param {Object} Props Function props.
61 * @param {number} Props.blockVersionLessThan The version blocks should be less than for this to run.
62 * @param {Object} Props.oldDefaults Old defaults that were changed and need to be added to attributes.
63 * @return {Object} Updated attributes.
64 * @since 1.4.0
65 */
66 export function migrateOldContainerDefaults( { blockVersionLessThan, oldDefaults } ) {
67 return function( attrs, existingAttrs, mode ) {
68 if ( 'css' === mode ) {
69 return attrs;
70 }
71
72 if ( ! wasBlockJustInserted( existingAttrs ) && isBlockVersionLessThan( existingAttrs.blockVersion, blockVersionLessThan ) ) {
73 const useGlobalStyle = 'undefined' !== typeof existingAttrs.useGlobalStyle && existingAttrs.useGlobalStyle;
74 const items = [];
75
76 if ( ! useGlobalStyle ) {
77 items.push(
78 'paddingTop',
79 'paddingRight',
80 'paddingBottom',
81 'paddingLeft',
82 );
83 }
84
85 if ( existingAttrs.isGrid ) {
86 items.push(
87 'width',
88 'widthMobile',
89 );
90 }
91
92 if ( existingAttrs.gradient ) {
93 items.push(
94 'gradientDirection',
95 'gradientColorOne',
96 'gradientColorOneOpacity',
97 'gradientColorTwo',
98 'gradientColorTwoOpacity'
99 );
100 }
101
102 items.forEach( ( item ) => {
103 if ( ! hasNumericValue( existingAttrs[ item ] ) ) {
104 attrs[ item ] = oldDefaults[ item ];
105 }
106 } );
107 }
108
109 return attrs;
110 };
111 }
112
113 /**
114 * Set our inner z-index if we're using a gradient overlay or pseudo background.
115 *
116 * @param {Object} Props Function props.
117 * @param {number} Props.blockVersionLessThan The version blocks should be less than for this to run.
118 * @return {Object} Updated attributes.
119 * @since 1.4.0
120 */
121 export function migrateContainerZIndex( { blockVersionLessThan } ) {
122 return function( attrs, existingAttrs, mode ) {
123 if ( 'css' === mode ) {
124 return attrs;
125 }
126
127 if ( ! wasBlockJustInserted( existingAttrs ) && isBlockVersionLessThan( existingAttrs.blockVersion, blockVersionLessThan ) ) {
128 let updateOldZindex =
129 existingAttrs.gradient && 'pseudo-element' === existingAttrs.gradientSelector &&
130 ! hasNumericValue( existingAttrs.innerZindex );
131
132 if ( ! updateOldZindex ) {
133 updateOldZindex = !! existingAttrs.bgImage && 'undefined' !== typeof existingAttrs.bgOptions.selector && 'pseudo-element' === existingAttrs.bgOptions.selector;
134 }
135
136 if ( ! updateOldZindex ) {
137 updateOldZindex = 'undefined' !== typeof existingAttrs.useAdvBackgrounds && existingAttrs.useAdvBackgrounds;
138 }
139
140 if ( updateOldZindex ) {
141 attrs.innerZindex = 1;
142 }
143 }
144
145 return attrs;
146 };
147 }
148
149 /**
150 * Set our useInnerContainer attribute on old Containers.
151 *
152 * @param {Object} Props Function props.
153 * @param {number} Props.blockVersionLessThan The version blocks should be less than for this to run.
154 * @return {Object} Updated attributes.
155 * @since 1.7.0
156 */
157 export function migrateInnerContainer( { blockVersionLessThan } ) {
158 return function( attrs, existingAttrs, mode ) {
159 if ( 'css' === mode ) {
160 return attrs;
161 }
162
163 if ( ! wasBlockJustInserted( existingAttrs ) && isBlockVersionLessThan( existingAttrs.blockVersion, blockVersionLessThan ) ) {
164 attrs.useInnerContainer = true;
165 }
166
167 return attrs;
168 };
169 }
170
171 /**
172 * Migrate our flexBasis attributes to include their unit.
173 *
174 * @param {Object} Props Function props.
175 * @param {number} Props.blockVersionLessThan The version blocks should be less than for this to run.
176 * @return {Object} Updated attributes.
177 * @since 1.7.0
178 */
179 export function migrateFlexBasis( { blockVersionLessThan } ) {
180 return function( attrs, existingAttrs, mode ) {
181 if ( 'css' === mode ) {
182 return attrs;
183 }
184
185 if ( ! wasBlockJustInserted( existingAttrs ) && isBlockVersionLessThan( existingAttrs.blockVersion, blockVersionLessThan ) ) {
186 [ '', 'Tablet', 'Mobile' ].forEach( ( device ) => {
187 if ( existingAttrs[ 'flexBasis' + device ] && ! isNaN( existingAttrs[ 'flexBasis' + device ] ) ) {
188 attrs[ 'flexBasis' + device ] = existingAttrs[ 'flexBasis' + device ] + existingAttrs.flexBasisUnit;
189 }
190 } );
191 }
192
193 return attrs;
194 };
195 }
196
197 export const currentBlockVersion = 4;
198
199 /**
200 * Migrate our Container attributes.
201 *
202 * @param {Object} Props Function props.
203 * @param {Object} Props.attributes The block attributes.
204 * @param {Object} Props.defaults The block defaults.
205 * @param {string} Props.mode The migration mode.
206 * @param {Object} Props.oldDefaults An object of old defaults keyed by version.
207 * @return {Object} Updated attributes.
208 * @since 1.8.0
209 */
210 export function migrateContainerAttributes( { attributes, defaults, mode = '', oldDefaults = {} } ) {
211 return migrationPipe(
212 attributes,
213 [
214 setIsDynamic,
215 migrateBgSelectorOpacity,
216 migrateContainerZIndex( {
217 blockVersionLessThan: 2,
218 } ),
219 migrateOldContainerDefaults( {
220 blockVersionLessThan: 2,
221 oldDefaults: oldDefaults.v1_4_0,
222 } ),
223 migrateInnerContainer( {
224 blockVersionLessThan: 3,
225 } ),
226 migrateFlexBasis( {
227 blockVersionLessThan: 3,
228 } ),
229 migrateSizing( {
230 blockVersionLessThan: 3,
231 } ),
232 migrateSpacing( {
233 blockVersionLessThan: 4,
234 defaults,
235 attributesToMigrate: [
236 'paddingTop',
237 'paddingRight',
238 'paddingBottom',
239 'paddingLeft',
240 'marginTop',
241 'marginRight',
242 'marginBottom',
243 'marginLeft',
244 ],
245 } ),
246 migrateBorders( {
247 blockVersionLessThan: 4,
248 defaults,
249 attributesToMigrate: [
250 'borderSizeTop',
251 'borderSizeRight',
252 'borderSizeBottom',
253 'borderSizeLeft',
254 'borderRadiusTopRight',
255 'borderRadiusBottomRight',
256 'borderRadiusBottomLeft',
257 'borderRadiusTopLeft',
258 ],
259 } ),
260 migrateTypography( {
261 blockVersionLessThan: 4,
262 defaults,
263 attributesToMigrate: [
264 'fontFamily',
265 'fontSize',
266 'fontWeight',
267 'textTransform',
268 'alignment',
269 ],
270 } ),
271 updateBlockVersion( currentBlockVersion ),
272 ],
273 mode
274 );
275 }
276
277 export default ( WrappedComponent ) => {
278 return ( props ) => {
279 const {
280 attributes,
281 setAttributes,
282 } = props;
283
284 useEffect( () => {
285 const newAttributes = migrateContainerAttributes( {
286 attributes,
287 defaults: getBlockType( 'generateblocks/container' )?.attributes,
288 oldDefaults: {
289 v1_4_0: generateBlocksLegacyDefaults.v_1_4_0.container,
290 },
291 } );
292
293 if ( ! isEmpty( newAttributes ) ) {
294 setAttributes( newAttributes );
295 }
296 }, [] );
297
298 return ( <WrappedComponent { ...props } /> );
299 };
300 };
301