-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathconvert-legacy-block.js
167 lines (160 loc) · 5.26 KB
/
convert-legacy-block.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/**
* Convert legacy blocks to their canonical form. This function is used
* both in the parser level for previous content and to convert such blocks
* used in Custom Post Types templates.
*
* @param {string} name The block's name
* @param {Object} attributes The block's attributes
*
* @return {[string, Object]} The block's name and attributes, changed accordingly if a match was found
*/
export function convertLegacyBlockNameAndAttributes( name, attributes ) {
const newAttributes = { ...attributes };
// Convert 'core/cover-image' block in existing content to 'core/cover'.
if ( 'core/cover-image' === name ) {
name = 'core/cover';
}
// Convert 'core/text' blocks in existing content to 'core/paragraph'.
if ( 'core/text' === name || 'core/cover-text' === name ) {
name = 'core/paragraph';
}
// Convert derivative blocks such as 'core/social-link-wordpress' to the
// canonical form 'core/social-link'.
if ( name && name.indexOf( 'core/social-link-' ) === 0 ) {
// Capture `social-link-wordpress` into `{"service":"wordpress"}`
newAttributes.service = name.substring( 17 );
name = 'core/social-link';
}
// Convert derivative blocks such as 'core-embed/instagram' to the
// canonical form 'core/embed'.
if ( name && name.indexOf( 'core-embed/' ) === 0 ) {
// Capture `core-embed/instagram` into `{"providerNameSlug":"instagram"}`
const providerSlug = name.substring( 11 );
const deprecated = {
speaker: 'speaker-deck',
polldaddy: 'crowdsignal',
};
newAttributes.providerNameSlug =
providerSlug in deprecated
? deprecated[ providerSlug ]
: providerSlug;
// This is needed as the `responsive` attribute was passed
// in a different way before the refactoring to block variations.
if ( ! [ 'amazon-kindle', 'wordpress' ].includes( providerSlug ) ) {
newAttributes.responsive = true;
}
name = 'core/embed';
}
// Convert Post Comment blocks in existing content to Comment blocks.
// TODO: Remove these checks when WordPress 6.0 is released.
if ( name === 'core/post-comment-author' ) {
name = 'core/comment-author-name';
}
if ( name === 'core/post-comment-content' ) {
name = 'core/comment-content';
}
if ( name === 'core/post-comment-date' ) {
name = 'core/comment-date';
}
if ( name === 'core/comments-query-loop' ) {
name = 'core/comments';
const { className = '' } = newAttributes;
if ( ! className.includes( 'wp-block-comments-query-loop' ) ) {
newAttributes.className = [
'wp-block-comments-query-loop',
className,
].join( ' ' );
}
// Note that we also had to add a deprecation to the block in order
// for the ID change to work.
}
if ( name === 'core/post-comments' ) {
name = 'core/comments';
newAttributes.legacy = true;
}
// Column count was stored as a string from WP 6.3-6.6. Convert it to a number.
if (
attributes.layout?.type === 'grid' &&
typeof attributes.layout?.columnCount === 'string'
) {
newAttributes.layout = {
...newAttributes.layout,
columnCount: parseInt( attributes.layout.columnCount, 10 ),
};
}
// Column span and row span were stored as strings in WP 6.6. Convert them to numbers.
if ( typeof attributes.style?.layout?.columnSpan === 'string' ) {
const columnSpanNumber = parseInt(
attributes.style.layout.columnSpan,
10
);
newAttributes.style = {
...newAttributes.style,
layout: {
...newAttributes.style.layout,
columnSpan: isNaN( columnSpanNumber )
? undefined
: columnSpanNumber,
},
};
}
if ( typeof attributes.style?.layout?.rowSpan === 'string' ) {
const rowSpanNumber = parseInt( attributes.style.layout.rowSpan, 10 );
newAttributes.style = {
...newAttributes.style,
layout: {
...newAttributes.style.layout,
rowSpan: isNaN( rowSpanNumber ) ? undefined : rowSpanNumber,
},
};
}
// The following code is only relevant for the Gutenberg plugin.
// It's a stand-alone if statement for dead-code elimination.
if ( globalThis.IS_GUTENBERG_PLUGIN ) {
// Convert pattern overrides added during experimental phase.
// Only four blocks were supported initially.
// These checks can be removed in WordPress 6.6.
if (
newAttributes.metadata?.bindings &&
( name === 'core/paragraph' ||
name === 'core/heading' ||
name === 'core/image' ||
name === 'core/button' ) &&
newAttributes.metadata.bindings.__default?.source !==
'core/pattern-overrides'
) {
const bindings = [
'content',
'url',
'title',
'id',
'alt',
'text',
'linkTarget',
];
// Delete any existing individual bindings and add a default binding.
// It was only possible to add all the default attributes through the UI,
// So as soon as we find an attribute, we can assume all default attributes are overridable.
let hasPatternOverrides = false;
bindings.forEach( ( binding ) => {
if (
newAttributes.metadata.bindings[ binding ]?.source ===
'core/pattern-overrides'
) {
hasPatternOverrides = true;
newAttributes.metadata = {
...newAttributes.metadata,
bindings: { ...newAttributes.metadata.bindings },
};
delete newAttributes.metadata.bindings[ binding ];
}
} );
if ( hasPatternOverrides ) {
newAttributes.metadata.bindings.__default = {
source: 'core/pattern-overrides',
};
}
}
}
return [ name, newAttributes ];
}