This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 219
/
migration-from-products-to-product-collection.ts
234 lines (203 loc) · 6.35 KB
/
migration-from-products-to-product-collection.ts
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/**
* External dependencies
*/
import { createBlock, BlockInstance } from '@wordpress/blocks';
import { select, dispatch, subscribe } from '@wordpress/data';
import { isWpVersion } from '@woocommerce/settings';
/**
* Internal dependencies
*/
import {
getProductsBlockClientIds,
checkIfBlockCanBeInserted,
postTemplateHasSupportForGridView,
getUpgradeStatus,
type TransformBlock,
type IsBlockType,
type ProductGridLayout,
type ProductGridLayoutTypes,
type PostTemplateLayout,
type PostTemplateLayoutTypes,
} from './migration-utils';
import { REPLACE_PRODUCTS_WITH_PRODUCT_COLLECTION } from './constants';
const mapAttributes = ( attributes: Record< string, unknown > ) => {
const { query, namespace, ...restAttributes } = attributes;
const {
__woocommerceAttributes,
__woocommerceStockStatus,
__woocommerceOnSale,
include,
...restQuery
} = query;
return {
...restAttributes,
query: {
woocommerceAttributes: __woocommerceAttributes,
woocommerceStockStatus: __woocommerceStockStatus,
woocommerceOnSale: __woocommerceOnSale,
woocommerceHandPickedProducts: include,
taxQuery: {},
parents: [],
isProductCollectionBlock: true,
...restQuery,
},
displayUpgradeNotice: true,
};
};
const isPostTemplate: IsBlockType = ( { name, attributes } ) =>
name === 'core/post-template' &&
attributes.__woocommerceNamespace ===
'woocommerce/product-query/product-template';
const isPostTitle: IsBlockType = ( { name, attributes } ) =>
name === 'core/post-title' &&
attributes.__woocommerceNamespace ===
'woocommerce/product-query/product-title';
const isPostSummary: IsBlockType = ( { name, attributes } ) =>
name === 'core/post-excerpt' &&
attributes.__woocommerceNamespace ===
'woocommerce/product-query/product-summary';
const transformPostTemplate: TransformBlock = ( block, innerBlocks ) => {
const { __woocommerceNamespace, className, layout, ...restAttrributes } =
block.attributes;
return createBlock(
'woocommerce/product-template',
restAttrributes,
innerBlocks
);
};
const transformPostTitle: TransformBlock = ( block, innerBlocks ) => {
const { __woocommerceNamespace, ...restAttrributes } = block.attributes;
return createBlock(
'core/post-title',
{
__woocommerceNamespace:
'woocommerce/product-collection/product-title',
...restAttrributes,
},
innerBlocks
);
};
const transformPostSummary: TransformBlock = ( block, innerBlocks ) => {
const { __woocommerceNamespace, ...restAttrributes } = block.attributes;
return createBlock(
'core/post-excerpt',
{
__woocommerceNamespace:
'woocommerce/product-collection/product-summary',
...restAttrributes,
},
innerBlocks
);
};
const mapLayoutType = (
type: PostTemplateLayoutTypes
): ProductGridLayoutTypes => {
if ( type === 'grid' ) {
return 'flex';
}
if ( type === 'default' ) {
return 'list';
}
return 'flex';
};
const mapLayoutPropertiesFromPostTemplateToProductCollection = (
layout: PostTemplateLayout
): ProductGridLayout => {
const { type, columnCount } = layout;
return {
type: mapLayoutType( type ),
columns: columnCount,
};
};
const getLayoutAttribute = (
attributes,
innerBlocks: BlockInstance[]
): ProductGridLayout => {
// Starting from GB 16, it's not Query Loop that keeps the layout, but the Post Template block.
// We need to account for that and in that case, move the layout properties
// from Post Template to Product Collection.
const postTemplate = innerBlocks.find( isPostTemplate );
const { layout: postTemplateLayout } = postTemplate?.attributes || {};
return postTemplateHasSupportForGridView
? mapLayoutPropertiesFromPostTemplateToProductCollection(
postTemplateLayout
)
: attributes.displayLayout;
};
const mapInnerBlocks = ( innerBlocks: BlockInstance[] ): BlockInstance[] => {
const mappedInnerBlocks = innerBlocks.map( ( innerBlock ) => {
const { name, attributes } = innerBlock;
const mappedInnerInnerBlocks = mapInnerBlocks( innerBlock.innerBlocks );
if ( isPostTemplate( innerBlock ) ) {
return transformPostTemplate( innerBlock, mappedInnerInnerBlocks );
}
if ( isPostTitle( innerBlock ) ) {
return transformPostTitle( innerBlock, mappedInnerInnerBlocks );
}
if ( isPostSummary( innerBlock ) ) {
return transformPostSummary( innerBlock, mappedInnerInnerBlocks );
}
return createBlock( name, attributes, mappedInnerInnerBlocks );
} );
return mappedInnerBlocks;
};
const replaceProductsBlock = ( clientId: string ) => {
const productsBlock = select( 'core/block-editor' ).getBlock( clientId );
const canBeInserted = checkIfBlockCanBeInserted(
clientId,
'woocommerce/product-collection'
);
if ( productsBlock && canBeInserted ) {
const { attributes = {}, innerBlocks = [] } = productsBlock;
const displayLayout = getLayoutAttribute( attributes, innerBlocks );
const adjustedAttributes = mapAttributes( {
...attributes,
displayLayout,
} );
const adjustedInnerBlocks = mapInnerBlocks( innerBlocks );
const productCollectionBlock = createBlock(
'woocommerce/product-collection',
adjustedAttributes,
adjustedInnerBlocks
);
dispatch( 'core/block-editor' ).replaceBlock(
clientId,
productCollectionBlock
);
return true;
}
return false;
};
const replaceProductsBlocks = ( productsBlockClientIds: string[] ) => {
const results = productsBlockClientIds.map( replaceProductsBlock );
return !! results.length && results.every( ( result ) => !! result );
};
export const replaceProductsWithProductCollection = () => {
const queryBlocksCount =
select( 'core/block-editor' ).getGlobalBlockCount( 'core/query' );
if ( queryBlocksCount === 0 ) {
return;
}
const blocks = select( 'core/block-editor' ).getBlocks();
const productsBlockClientIds = getProductsBlockClientIds( blocks );
const productsBlocksCount = productsBlockClientIds.length;
if ( productsBlocksCount === 0 ) {
return;
}
replaceProductsBlocks( productsBlockClientIds );
};
export let productsReplacementUnsubscribe: ( () => void ) | undefined;
const { status } = getUpgradeStatus();
if ( isWpVersion( '6.1', '>=' ) ) {
// @todo Read from local storage. If reverted - don't subscribe
if (
REPLACE_PRODUCTS_WITH_PRODUCT_COLLECTION &&
status !== 'reverted' &&
! productsReplacementUnsubscribe
) {
console.info( 'Subscribed to allow Products block migration' );
productsReplacementUnsubscribe = subscribe( () => {
replaceProductsWithProductCollection();
}, 'core/block-editor' );
}
}