PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 2.0.2
GenerateBlocks v2.0.2
2.4.1 2.4.0 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 / migrations / migrateGlobalStyleAttrs.js
generateblocks / src / hoc / migrations Last commit date
migrateBorders.js 3 years ago migrateGlobalStyleAttrs.js 3 years ago migrateIconPadding.js 3 years ago migrateSizing.js 3 years ago migrateSpacing.js 3 years ago migrateTypography.js 3 years ago migratingIconSizing.js 3 years ago utils.js 3 years ago
migrateGlobalStyleAttrs.js
92 lines
1 import { addFilter } from '@wordpress/hooks';
2 import { migrateButtonAttributes } from '../withButtonLegacyMigration';
3 import { migrateButtonContainerAttributes } from '../withButtonContainerLegacyMigration';
4 import { migrateContainerAttributes } from '../withContainerLegacyMigration';
5 import { migrateHeadlineAttributes } from '../withHeadlineLegacyMigration';
6 import { migrateGridAttributes } from '../withGridLegacyMigration';
7 import { migrateImageAttributes } from '../withImageLegacyMigration';
8 import { isEmpty, isObject } from 'lodash';
9
10 /**
11 * This ensures that Global Styles pass the correct attributes
12 * to the CSS generation even when attributes change names.
13 */
14 addFilter(
15 'generateblocks.editor.cssAttrs',
16 'generateblocks/migrateCssAttrs',
17 ( attributes, props ) => {
18 const {
19 name,
20 } = props;
21
22 if ( ! attributes.useGlobalStyle ) {
23 return attributes;
24 }
25
26 let migrateFunction = null;
27
28 switch ( name ) {
29 case 'generateblocks/button':
30 migrateFunction = migrateButtonAttributes;
31 break;
32
33 case 'generateblocks/button-container':
34 migrateFunction = migrateButtonContainerAttributes;
35 break;
36
37 case 'generateblocks/container':
38 migrateFunction = migrateContainerAttributes;
39 break;
40
41 case 'generateblocks/headline':
42 migrateFunction = migrateHeadlineAttributes;
43 break;
44
45 case 'generateblocks/grid':
46 migrateFunction = migrateGridAttributes;
47 break;
48
49 case 'generateblocks/image':
50 migrateFunction = migrateImageAttributes;
51 break;
52 }
53
54 if ( null !== migrateFunction ) {
55 // This ensures that old attributes (coming from Global Styles) are migrated
56 // to their new attribute names so styling continues to work in the editor.
57 const migratedAttributes = migrateFunction( {
58 attributes: {
59 ...attributes,
60 // Use the global style block version if possible.
61 blockVersion: attributes.globalBlockVersion || 1,
62 },
63 mode: 'css',
64 } );
65
66 const newAttributes = {};
67
68 // We only need these migrated attributes if a local attribute doesn't exist.
69 Object.entries( migratedAttributes ).forEach( ( [ attributeName, attributeValue ] ) => {
70 if ( isObject( attributeValue ) ) {
71 Object.entries( attributeValue ).forEach( ( [ objectProperty, objectValue ] ) => {
72 if ( isEmpty( attributes[ attributeName ]?.[ objectProperty ] ) ) {
73 newAttributes[ attributeName ] = {
74 ...attributes[ attributeName ],
75 ...newAttributes[ attributeName ],
76 [ objectProperty ]: objectValue,
77 };
78 }
79 } );
80 } else if ( isEmpty( attributes[ attributeName ] ) ) {
81 newAttributes[ attributeName ] = attributeValue;
82 }
83 } );
84
85 return { ...attributes, ...newAttributes };
86 }
87
88 return attributes;
89 },
90 1000
91 );
92