-
Notifications
You must be signed in to change notification settings - Fork 1
/
BlockTypesController.php
297 lines (263 loc) · 9.05 KB
/
BlockTypesController.php
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php
namespace Automattic\WooCommerce\Blocks;
use Automattic\WooCommerce\Blocks\BlockTypes\AtomicBlock;
use Automattic\WooCommerce\Blocks\Package;
use Automattic\WooCommerce\Blocks\Assets\AssetDataRegistry;
use Automattic\WooCommerce\Blocks\Assets\Api as AssetApi;
use Automattic\WooCommerce\Blocks\Integrations\IntegrationRegistry;
/**
* BlockTypesController class.
*
* @since 5.0.0
* @internal
*/
final class BlockTypesController {
/**
* Instance of the asset API.
*
* @var AssetApi
*/
protected $asset_api;
/**
* Instance of the asset data registry.
*
* @var AssetDataRegistry
*/
protected $asset_data_registry;
/**
* Constructor.
*
* @param AssetApi $asset_api Instance of the asset API.
* @param AssetDataRegistry $asset_data_registry Instance of the asset data registry.
*/
public function __construct( AssetApi $asset_api, AssetDataRegistry $asset_data_registry ) {
$this->asset_api = $asset_api;
$this->asset_data_registry = $asset_data_registry;
$this->init();
}
/**
* Initialize class features.
*/
protected function init() {
add_action( 'init', array( $this, 'register_blocks' ) );
add_filter( 'render_block', array( $this, 'add_data_attributes' ), 10, 2 );
add_filter( 'render_block', array( $this, 'hydration_block_wrapper' ), 10, 3 );
add_action( 'woocommerce_login_form_end', array( $this, 'redirect_to_field' ) );
add_filter( 'widget_types_to_hide_from_legacy_widget_block', array( $this, 'hide_legacy_widgets_with_block_equivalent' ) );
}
/**
* Register blocks, hooking up assets and render functions as needed.
*/
public function register_blocks() {
$block_types = $this->get_block_types();
foreach ( $block_types as $block_type ) {
$block_type_class = __NAMESPACE__ . '\\BlockTypes\\' . $block_type;
$block_type_instance = new $block_type_class( $this->asset_api, $this->asset_data_registry, new IntegrationRegistry() );
}
}
/**
* Add data- attributes to blocks when rendered if the block is under the woocommerce/ namespace.
*
* @param string $content Block content.
* @param array $block Parsed block data.
* @return string
*/
public function add_data_attributes( $content, $block ) {
$block_name = $block['blockName'];
$block_namespace = strtok( $block_name ?? '', '/' );
/**
* Filters the list of allowed block namespaces.
*
* This hook defines which block namespaces should have block name and attribute `data-` attributes appended on render.
*
* @param array $allowed_namespaces List of namespaces.
*/
$allowed_namespaces = array_merge( [ 'woocommerce', 'woocommerce-checkout' ], (array) apply_filters( '__experimental_woocommerce_blocks_add_data_attributes_to_namespace', [] ) );
/**
* Filters the list of allowed Block Names
*
* This hook defines which block names should have block name and attribute data- attributes appended on render.
*
* @param array $allowed_namespaces List of namespaces.
*/
$allowed_blocks = (array) apply_filters( '__experimental_woocommerce_blocks_add_data_attributes_to_block', [] );
if ( ! in_array( $block_namespace, $allowed_namespaces, true ) && ! in_array( $block_name, $allowed_blocks, true ) ) {
return $content;
}
if ( 'woocommerce/all-products' === $block_name ) {
return $content;
}
$attributes = (array) $block['attrs'];
$exclude_attributes = [ 'className', 'align' ];
$escaped_data_attributes = [
'data-block-name="' . esc_attr( $block['blockName'] ) . '"',
];
foreach ( $attributes as $key => $value ) {
if ( in_array( $key, $exclude_attributes, true ) ) {
continue;
}
if ( is_bool( $value ) ) {
$value = $value ? 'true' : 'false';
}
if ( ! is_scalar( $value ) ) {
$value = wp_json_encode( $value );
}
$escaped_data_attributes[] = 'data-' . esc_attr( strtolower( preg_replace( '/(?<!\ )[A-Z]/', '-$0', $key ) ) ) . '="' . esc_attr( $value ) . '"';
}
return preg_replace( '/^<div /', '<div ' . implode( ' ', $escaped_data_attributes ) . ' ', trim( $content ) );
}
function hydration_block_wrapper( $block_content, $block, $instance ) {
// We might want to use a flag from block.json as the criterion here.
if ( ! in_array(
$block['blockName'],
array(
'woocommerce/all-products',
),
true
) ) {
return $block_content;
}
$block_type = $instance->block_type;
$attr_definitions = $block_type->attributes;
$attributes = array();
$sourced_attributes = array();
foreach( $attr_definitions as $attr => $definition ) {
if ( ! empty( $definition['frontend'] ) ) {
if ( ! empty( $definition['source'] ) ) {
$sourced_attributes[ $attr ] = array(
"selector" => $definition['selector'], // TODO: Guard against unset.
"source" => $definition['source']
);
} else {
if ( array_key_exists ( $attr, $block['attrs'] ) ) {
$attributes[ $attr ] = $block['attrs'][$attr];
} else if ( isset( $definition['default'] ) ) {
$attributes[ $attr ] = $definition['default'];
}
}
}
}
$previous_block_to_render = \WP_Block_Supports::$block_to_render;
// TODO: The following is a bit hacky. If we stick with this technique, we might
// want to change apply_block_supports() to accepts a block as its argument.
\WP_Block_Supports::$block_to_render = $block;
$block_supports_attributes = \WP_Block_Supports::get_instance()->apply_block_supports();
\WP_Block_Supports::$block_to_render = $previous_block_to_render;
$block_wrapper = sprintf(
'<gutenberg-interactive-block ' .
'data-gutenberg-block-type="%1$s" ' .
'data-gutenberg-uses-block-context="%2$s" ' .
'data-gutenberg-provides-block-context="%3$s" ' .
'data-gutenberg-attributes="%4$s" ' .
'data-gutenberg-sourced-attributes="%5$s" ' .
'data-gutenberg-block-props="%6$s" ' .
'data-gutenberg-hydrate="idle">',
esc_attr( $block['blockName'] ),
esc_attr( json_encode( $block_type->uses_context ) ),
esc_attr( json_encode( $block_type->provides_context ) ),
esc_attr( json_encode( $attributes ) ),
esc_attr( json_encode( $sourced_attributes ) ),
esc_attr( json_encode( $block_supports_attributes ) )
) . '%1$s</gutenberg-interactive-block>';
$template_wrapper = '<template class="gutenberg-inner-blocks">%1$s</template>';
$empty_template = sprintf( $template_wrapper, '' );
$template = sprintf( $template_wrapper, sprintf( $block_wrapper, $block_content . $empty_template ) );
return sprintf( $block_wrapper, $block_content . $template );
}
/**
* Adds a redirect field to the login form so blocks can redirect users after login.
*/
public function redirect_to_field() {
// phpcs:ignore WordPress.Security.NonceVerification
if ( empty( $_GET['redirect_to'] ) ) {
return;
}
echo '<input type="hidden" name="redirect" value="' . esc_attr( esc_url_raw( wp_unslash( $_GET['redirect_to'] ) ) ) . '" />'; // phpcs:ignore WordPress.Security.NonceVerification
}
/**
* Hide legacy widgets with a feature complete block equivalent in the inserter
* and prevent them from showing as an option in the Legacy Widget block.
*
* @param array $widget_types An array of widgets hidden in core.
* @return array $widget_types An array inluding the WooCommerce widgets to hide.
*/
public function hide_legacy_widgets_with_block_equivalent( $widget_types ) {
array_push(
$widget_types,
'woocommerce_product_search',
'woocommerce_product_categories',
'woocommerce_recent_reviews',
'woocommerce_product_tag_cloud',
'woocommerce_price_filter',
'woocommerce_layered_nav',
'woocommerce_layered_nav_filters'
);
return $widget_types;
}
/**
* Get list of block types.
*
* @return array
*/
protected function get_block_types() {
global $pagenow;
$block_types = [
'AllReviews',
'FeaturedCategory',
'FeaturedProduct',
'HandpickedProducts',
'ProductBestSellers',
'ProductCategories',
'ProductCategory',
'ProductNew',
'ProductOnSale',
'ProductsByAttribute',
'ProductTopRated',
'ReviewsByProduct',
'ReviewsByCategory',
'ProductSearch',
'ProductTag',
'AllProducts',
'PriceFilter',
'AttributeFilter',
'StockFilter',
'ActiveFilters',
'ClassicTemplate',
'ProductAddToCart',
'ProductButton',
'ProductCategoryList',
'ProductImage',
'ProductPrice',
'ProductRating',
'ProductSaleBadge',
'ProductSKU',
'ProductStockIndicator',
'ProductSummary',
'ProductTagList',
'ProductTitle',
'MiniCart',
'MiniCartContents',
];
if ( Package::feature()->is_feature_plugin_build() ) {
$block_types[] = 'Checkout';
$block_types[] = 'Cart';
}
if ( Package::feature()->is_experimental_build() ) {
$block_types[] = 'SingleProduct';
}
/**
* This disables specific blocks in Widget Areas by not registering them.
*/
if ( in_array( $pagenow, [ 'widgets.php', 'themes.php', 'customize.php' ], true ) && ( empty( $_GET['page'] ) || 'gutenberg-edit-site' !== $_GET['page'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
$block_types = array_diff(
$block_types,
[
'AllProducts',
'Cart',
'Checkout',
]
);
}
return $block_types;
}
}