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
/
index.tsx
171 lines (150 loc) · 4.22 KB
/
index.tsx
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
/**
* External dependencies
*/
import { PluginTemplateSettingPanel } from '@wordpress/edit-site';
import { subscribe, select, useSelect, useDispatch } from '@wordpress/data';
import { BlockInstance, createBlock } from '@wordpress/blocks';
import { Button, PanelBody } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { createInterpolateElement, useMemo } from '@wordpress/element';
import { useEntityRecord } from '@wordpress/core-data';
import { store as blockEditorStore } from '@wordpress/block-editor';
// @ts-expect-error: @wordpress/plugin is typed in the newer versions
// eslint-disable-next-line @woocommerce/dependency-group
import {
registerPlugin,
unregisterPlugin,
getPlugin,
} from '@wordpress/plugins';
/**
* Internal dependencies
*/
import './style.scss';
const hasLegacyTemplateBlock = ( blocks: Array< BlockInstance > ): boolean => {
return blocks.some( ( block ) => {
return (
block.name === 'woocommerce/legacy-template' ||
hasLegacyTemplateBlock( block.innerBlocks )
);
} );
};
const pickBlockClientIds = ( blocks: Array< BlockInstance > ) =>
blocks.reduce< Array< string > >( ( acc, block ) => {
if ( block.name === 'core/template-part' ) {
return acc;
}
return [ ...acc, block.clientId ];
}, [] );
const RevertClassicTemplateButton = () => {
const { blocks, editedPostId } = useSelect( ( sel ) => {
return {
blocks: sel( blockEditorStore ).getBlocks(),
editedPostId: sel( 'core/edit-site' ).getEditedPostId(),
};
}, [] );
const { replaceBlocks } = useDispatch( blockEditorStore );
const template = useEntityRecord< {
slug: string;
title: {
rendered?: string;
row: string;
};
} >( 'postType', 'wp_template', editedPostId );
const isLegacyTemplateBlockAdded = useMemo(
() => hasLegacyTemplateBlock( blocks ),
[ blocks ]
);
const clientIds = useMemo( () => pickBlockClientIds( blocks ), [ blocks ] );
return (
<>
{ ! isLegacyTemplateBlockAdded && (
<PluginTemplateSettingPanel>
<PanelBody className="wc-block-editor-revert-button-container">
<Button
variant="secondary"
onClick={ () => {
replaceBlocks(
clientIds,
createBlock(
'core/group',
{
layout: {
inherit: true,
type: 'constrained',
},
},
[
createBlock(
'woocommerce/legacy-template',
{
template:
template?.record?.slug,
}
),
]
)
);
} }
>
{ __(
'Revert to Classic Product Template',
'woo-gutenberg-products-block'
) }
</Button>
<span>
{ createInterpolateElement(
__(
`The <strongText /> template doesn’t allow for reordering or customizing blocks, but might work better with your extensions`,
'woo-gutenberg-products-block'
),
{
strongText: (
<strong>
{ template?.record?.title
?.rendered ?? '' }
</strong>
),
}
) }
</span>
</PanelBody>
</PluginTemplateSettingPanel>
) }
</>
);
};
const templateSlugs = [
'single-product',
'archive-product',
'product-search-results',
'taxonomy-product_cat',
'taxonomy-product_tag',
'taxonomy-product_attribute',
];
const REVERT_BUTTON_PLUGIN_NAME = 'woocommerce-blocks-revert-button-templates';
let currentTemplateId: string | undefined;
subscribe( () => {
const previousTemplateId = currentTemplateId;
const store = select( 'core/edit-site' );
currentTemplateId = store.getEditedPostId();
if ( previousTemplateId === currentTemplateId ) {
return;
}
const isWooTemplate = templateSlugs.some( ( slug ) =>
currentTemplateId?.includes( slug )
);
const hasSupportForPluginTemplateSettingPanel =
PluginTemplateSettingPanel !== undefined;
if ( isWooTemplate && hasSupportForPluginTemplateSettingPanel ) {
if ( getPlugin( REVERT_BUTTON_PLUGIN_NAME ) ) {
return;
}
return registerPlugin( REVERT_BUTTON_PLUGIN_NAME, {
render: RevertClassicTemplateButton,
} );
}
if ( getPlugin( REVERT_BUTTON_PLUGIN_NAME ) === undefined ) {
return;
}
unregisterPlugin( REVERT_BUTTON_PLUGIN_NAME );
}, 'core/edit-site' );