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rc.2 3.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / src / Admin / Features / ProductBlockEditor / Init.php
woocommerce / src / Admin / Features / ProductBlockEditor Last commit date
ProductTemplates 4 weeks ago BlockRegistry.php 4 weeks ago BlockTemplateUtils.php 4 weeks ago Init.php 4 weeks ago ProductFormsController.php 4 weeks ago ProductTemplate.php 4 weeks ago RedirectionController.php 4 weeks ago Tracks.php 4 weeks ago
Init.php
528 lines
1 <?php
2 /**
3 * WooCommerce Product Block Editor
4 */
5
6 declare(strict_types = 1);
7
8 namespace Automattic\WooCommerce\Admin\Features\ProductBlockEditor;
9
10 use Automattic\WooCommerce\Admin\Features\ProductBlockEditor\ProductTemplate;
11 use Automattic\WooCommerce\Admin\PageController;
12 use Automattic\WooCommerce\Enums\ProductType;
13 use Automattic\WooCommerce\LayoutTemplates\LayoutTemplateRegistry;
14
15 use Automattic\WooCommerce\Internal\Features\ProductBlockEditor\ProductTemplates\SimpleProductTemplate;
16 use Automattic\WooCommerce\Internal\Features\ProductBlockEditor\ProductTemplates\ProductVariationTemplate;
17 use WC_Meta_Data;
18 use WP_Block_Editor_Context;
19
20 /**
21 * Loads assets related to the product block editor.
22 *
23 * @deprecated 10.9.0 Product editor extension APIs will be removed in WooCommerce 11.0.
24 */
25 class Init {
26 /**
27 * Version that product editor APIs were deprecated in.
28 */
29 const DEPRECATED_SINCE = '10.9.0';
30
31 /**
32 * The context name used to identify the editor.
33 */
34 const EDITOR_CONTEXT_NAME = 'woocommerce/edit-product';
35
36 /**
37 * Supported product types.
38 *
39 * @var array
40 */
41 private $supported_product_types = array( ProductType::SIMPLE );
42
43 /**
44 * Registered product templates.
45 *
46 * @var array
47 */
48 private $product_templates = array();
49
50 /**
51 * Redirection controller.
52 *
53 * @var RedirectionController
54 */
55 private $redirection_controller;
56
57 /**
58 * Constructor
59 */
60 public function __construct() {
61 if ( ! is_admin() && ! WC()->is_rest_api_request() ) {
62 return;
63 }
64
65 array_push( $this->supported_product_types, ProductType::VARIABLE );
66 array_push( $this->supported_product_types, ProductType::EXTERNAL );
67 array_push( $this->supported_product_types, ProductType::GROUPED );
68
69 $this->redirection_controller = new RedirectionController();
70
71 if ( \Automattic\WooCommerce\Utilities\FeaturesUtil::feature_is_enabled( 'product_block_editor' ) ) {
72 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles' ) );
73 add_action( 'admin_enqueue_scripts', array( $this, 'dequeue_conflicting_styles' ), 100 );
74 add_action( 'get_edit_post_link', array( $this, 'update_edit_product_link' ), 10, 2 );
75
76 add_filter( 'woocommerce_admin_get_user_data_fields', array( $this, 'add_user_data_fields' ) );
77 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
78 add_filter( 'woocommerce_register_post_type_product_variation', array( $this, 'enable_rest_api_for_product_variation' ) );
79
80 add_action( 'current_screen', array( $this, 'set_current_screen_to_block_editor_if_wc_admin' ) );
81
82 add_action( 'rest_api_init', array( $this, 'register_layout_templates' ) );
83 add_action( 'rest_api_init', array( $this, 'register_user_metas' ) );
84
85 add_filter( 'register_block_type_args', array( $this, 'register_metadata_attribute' ) );
86 add_filter( 'woocommerce_get_block_types', array( $this, 'get_block_types' ), 999, 1 );
87
88 add_filter( 'woocommerce_rest_prepare_product_object', array( $this, 'possibly_add_template_id' ), 10, 2 );
89 add_filter( 'woocommerce_rest_prepare_product_variation_object', array( $this, 'possibly_add_template_id' ), 10, 2 );
90
91 // Make sure the block registry is initialized so that core blocks are registered.
92 BlockRegistry::get_instance();
93
94 $tracks = new Tracks();
95 $tracks->init();
96
97 $this->register_product_templates();
98 }
99 }
100
101
102 /**
103 * Adds the product template ID to the product if it doesn't exist.
104 *
105 * @param WP_REST_Response $response The response object.
106 * @param WC_Product $product The product.
107 */
108 public function possibly_add_template_id( $response, $product ) {
109 if ( ! $product ) {
110 return $response;
111 }
112 if ( ! $product->meta_exists( '_product_template_id' ) ) {
113 if ( has_filter( 'experimental_woocommerce_product_editor_product_template_id_for_product' ) ) {
114 wc_deprecated_hook(
115 'experimental_woocommerce_product_editor_product_template_id_for_product',
116 $this::DEPRECATED_SINCE,
117 null,
118 'This product editor extension filter will be removed in WooCommerce 11.0.'
119 );
120 }
121
122 /**
123 * Experimental: Allows to determine a product template id based on the product data.
124 *
125 * @ignore
126 * @since 9.1.0
127 * @deprecated 10.9.0 Product editor extension APIs will be removed in WooCommerce 11.0.
128 */
129 $product_template_id = apply_filters( 'experimental_woocommerce_product_editor_product_template_id_for_product', '', $product );
130 if ( $product_template_id ) {
131 $response->data['meta_data'][] = new WC_Meta_Data(
132 array(
133 'key' => '_product_template_id',
134 'value' => $product_template_id,
135 )
136 );
137 }
138 }
139 return $response;
140 }
141
142 /**
143 * Enqueue scripts needed for the product form block editor.
144 */
145 public function enqueue_scripts() {
146 if ( ! PageController::is_admin_or_embed_page() ) {
147 return;
148 }
149
150 $editor_settings = $this->get_product_editor_settings();
151
152 $script_handle = 'wc-admin-edit-product';
153 wp_register_script( $script_handle, '', array( 'wp-blocks' ), '0.1.0', true );
154 wp_enqueue_script( $script_handle );
155 wp_add_inline_script(
156 $script_handle,
157 'var productBlockEditorSettings = productBlockEditorSettings || ' . wp_json_encode( $editor_settings, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ) . ';',
158 'before'
159 );
160 wp_add_inline_script(
161 $script_handle,
162 sprintf( 'wp.blocks.setCategories( %s );', wp_json_encode( $editor_settings['blockCategories'], JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ) ),
163 'before'
164 );
165 wp_tinymce_inline_scripts();
166 wp_enqueue_media();
167 wp_register_style( 'wc-global-presets', false ); // phpcs:ignore
168 wp_add_inline_style( 'wc-global-presets', wp_get_global_stylesheet( array( 'presets' ) ) );
169 wp_enqueue_style( 'wc-global-presets' );
170 }
171
172 /**
173 * Enqueue styles needed for the rich text editor.
174 */
175 public function enqueue_styles() {
176 if ( ! PageController::is_admin_page() ) {
177 return;
178 }
179 wp_enqueue_style( 'wc-product-editor' );
180 wp_enqueue_style( 'wp-editor' );
181 wp_enqueue_style( 'wp-format-library' );
182 wp_enqueue_editor();
183 /**
184 * Enqueue any block editor related assets.
185 *
186 * @since 7.1.0
187 */
188 do_action( 'enqueue_block_editor_assets' );
189 }
190
191 /**
192 * Dequeue conflicting styles.
193 */
194 public function dequeue_conflicting_styles() {
195 if ( ! PageController::is_admin_page() ) {
196 return;
197 }
198 // Dequeuing this to avoid conflicts, until we remove the 'woocommerce-page' class.
199 wp_dequeue_style( 'woocommerce-blocktheme' );
200 }
201
202 /**
203 * Update the edit product links when the new experience is enabled.
204 *
205 * @param string $link The edit link.
206 * @param int $post_id Post ID.
207 * @return string
208 */
209 public function update_edit_product_link( $link, $post_id ) {
210 $product = wc_get_product( $post_id );
211
212 if ( ! $product ) {
213 return $link;
214 }
215
216 if ( $product->get_type() === ProductType::SIMPLE ) {
217 return admin_url( 'admin.php?page=wc-admin&path=/product/' . $product->get_id() );
218 }
219
220 return $link;
221 }
222
223 /**
224 * Enables variation post type in REST API.
225 *
226 * @param array $args Array of post type arguments.
227 * @return array Array of post type arguments.
228 */
229 public function enable_rest_api_for_product_variation( $args ) {
230 $args['show_in_rest'] = true;
231
232 return $args;
233 }
234
235 /**
236 * Adds fields so that we can store user preferences for the variations block.
237 *
238 * @param array $user_data_fields User data fields.
239 * @return array
240 */
241 public function add_user_data_fields( $user_data_fields ) {
242 return array_merge(
243 $user_data_fields,
244 array(
245 'variable_product_block_tour_shown',
246 'local_attributes_notice_dismissed_ids',
247 'variable_items_without_price_notice_dismissed',
248 'product_advice_card_dismissed',
249 )
250 );
251 }
252
253 /**
254 * Sets the current screen to the block editor if a wc-admin page.
255 */
256 public function set_current_screen_to_block_editor_if_wc_admin() {
257 $screen = get_current_screen();
258
259 // phpcs:ignore Squiz.PHP.CommentedOutCode.Found
260 // (no idea why I need that phpcs:ignore above, but I'm tired trying to re-write this comment to get it to pass)
261 // we can't check the 'path' query param because client-side routing is used within wc-admin,
262 // so this action handler is only called on the initial page load from the server, which might
263 // not be the product edit page (it mostly likely isn't).
264 if ( PageController::is_admin_page() ) {
265 $screen->is_block_editor( true );
266
267 wp_add_inline_script(
268 'wp-blocks',
269 'wp.blocks && wp.blocks.unstable__bootstrapServerSideBlockDefinitions && wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings(), JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ) . ');'
270 );
271 }
272 }
273
274 /**
275 * Get the product editor settings.
276 */
277 private function get_product_editor_settings() {
278 $editor_settings['productTemplates'] = array_map(
279 function ( $product_template ) {
280 return $product_template->to_json();
281 },
282 $this->product_templates
283 );
284
285 $block_editor_context = new WP_Block_Editor_Context( array( 'name' => self::EDITOR_CONTEXT_NAME ) );
286
287 return get_block_editor_settings( $editor_settings, $block_editor_context );
288 }
289
290 /**
291 * Get default product templates.
292 *
293 * @return array The default templates.
294 */
295 private function get_default_product_templates() {
296 $templates = array();
297 $templates[] = new ProductTemplate(
298 array(
299 'id' => 'standard-product-template',
300 'title' => __( 'Standard product', 'woocommerce' ),
301 'description' => __( 'A single physical or virtual product, e.g. a t-shirt or an eBook.', 'woocommerce' ),
302 'order' => 10,
303 'icon' => 'shipping',
304 'layout_template_id' => 'simple-product',
305 'product_data' => array(
306 'type' => ProductType::SIMPLE,
307 ),
308 )
309 );
310 $templates[] = new ProductTemplate(
311 array(
312 'id' => 'grouped-product-template',
313 'title' => __( 'Grouped product', 'woocommerce' ),
314 'description' => __( 'A set of products that go well together, e.g. camera kit.', 'woocommerce' ),
315 'order' => 20,
316 'icon' => 'group',
317 'layout_template_id' => 'simple-product',
318 'product_data' => array(
319 'type' => ProductType::GROUPED,
320 ),
321 )
322 );
323 $templates[] = new ProductTemplate(
324 array(
325 'id' => 'affiliate-product-template',
326 'title' => __( 'Affiliate product', 'woocommerce' ),
327 'description' => __( 'A link to a product sold on a different website, e.g. brand collab.', 'woocommerce' ),
328 'order' => 30,
329 'icon' => 'link',
330 'layout_template_id' => 'simple-product',
331 'product_data' => array(
332 'type' => ProductType::EXTERNAL,
333 ),
334 )
335 );
336
337 return $templates;
338 }
339
340 /**
341 * Create default product template by custom product type if it does not have a
342 * template associated yet.
343 *
344 * @param array $templates The registered product templates.
345 * @return array The new templates.
346 */
347 private function create_default_product_template_by_custom_product_type( array $templates ) {
348 // Getting the product types registered via the classic editor.
349 $registered_product_types = wc_get_product_types();
350
351 $custom_product_types = array_filter(
352 $registered_product_types,
353 function ( $product_type ) {
354 return ! in_array( $product_type, $this->supported_product_types, true );
355 },
356 ARRAY_FILTER_USE_KEY
357 );
358
359 $templates_with_product_type = array_filter(
360 $templates,
361 function ( $template ) {
362 $product_data = $template->get_product_data();
363 return ! is_null( $product_data ) && array_key_exists( 'type', $product_data );
364 }
365 );
366
367 $custom_product_types_on_templates = array_map(
368 function ( $template ) {
369 $product_data = $template->get_product_data();
370 return $product_data['type'];
371 },
372 $templates_with_product_type
373 );
374
375 foreach ( $custom_product_types as $product_type => $title ) {
376 if ( in_array( $product_type, $custom_product_types_on_templates, true ) ) {
377 continue;
378 }
379
380 $templates[] = new ProductTemplate(
381 array(
382 'id' => $product_type . '-product-template',
383 'title' => $title,
384 'product_data' => array(
385 'type' => $product_type,
386 ),
387 )
388 );
389 }
390
391 return $templates;
392 }
393
394 /**
395 * Register layout templates.
396 */
397 public function register_layout_templates() {
398 $layout_template_registry = wc_get_container()->get( LayoutTemplateRegistry::class );
399
400 if ( ! $layout_template_registry->is_registered( 'simple-product' ) ) {
401 $layout_template_registry->register(
402 'simple-product',
403 'product-form',
404 SimpleProductTemplate::class
405 );
406 }
407
408 if ( ! $layout_template_registry->is_registered( 'product-variation' ) ) {
409 $layout_template_registry->register(
410 'product-variation',
411 'product-form',
412 ProductVariationTemplate::class
413 );
414 }
415 }
416
417 /**
418 * Register product templates.
419 */
420 public function register_product_templates() {
421 if ( has_filter( 'woocommerce_product_editor_product_templates' ) ) {
422 wc_deprecated_hook(
423 'woocommerce_product_editor_product_templates',
424 self::DEPRECATED_SINCE,
425 null,
426 'This product editor extension filter will be removed in WooCommerce 11.0.'
427 );
428 }
429
430 /**
431 * Allows for new product template registration.
432 *
433 * @since 8.5.0
434 * @deprecated 10.9.0 Product editor extension APIs will be removed in WooCommerce 11.0.
435 */
436 $this->product_templates = apply_filters( 'woocommerce_product_editor_product_templates', $this->get_default_product_templates() );
437 $this->product_templates = $this->create_default_product_template_by_custom_product_type( $this->product_templates );
438
439 usort(
440 $this->product_templates,
441 function ( $a, $b ) {
442 return $a->get_order() - $b->get_order();
443 }
444 );
445
446 $this->redirection_controller->set_product_templates( $this->product_templates );
447 }
448
449 /**
450 * Register user metas.
451 */
452 public function register_user_metas() {
453 register_rest_field(
454 'user',
455 'metaboxhidden_product',
456 array(
457 'get_callback' => function ( $object, $attr ) {
458 $hidden = get_user_meta( $object['id'], $attr, true );
459
460 if ( is_array( $hidden ) ) {
461 // Ensures to always return a string array.
462 return array_values( $hidden );
463 }
464
465 return array( 'postcustom' );
466 },
467 'update_callback' => function ( $value, $object, $attr ) {
468 // Update the field/meta value.
469 update_user_meta( $object->ID, $attr, $value );
470 },
471 'schema' => array(
472 'type' => 'array',
473 'description' => __( 'The metaboxhidden_product meta from the user metas.', 'woocommerce' ),
474 'items' => array(
475 'type' => 'string',
476 ),
477 'arg_options' => array(
478 'sanitize_callback' => 'wp_parse_list',
479 'validate_callback' => 'rest_validate_request_arg',
480 ),
481 ),
482 )
483 );
484 }
485
486 /**
487 * Registers the metadata block attribute for all block types.
488 * This is a fallback/temporary solution until
489 * the Gutenberg core version registers the metadata attribute.
490 *
491 * @see https://github.com/WordPress/gutenberg/blob/6aaa3686ae67adc1a6a6b08096d3312859733e1b/lib/compat/wordpress-6.5/blocks.php#L27-L47
492 * To do: Remove this method once the Gutenberg core version registers the metadata attribute.
493 *
494 * @param array $args Array of arguments for registering a block type.
495 * @return array $args
496 */
497 public function register_metadata_attribute( $args ) {
498 // Setup attributes if needed.
499 if ( ! isset( $args['attributes'] ) || ! is_array( $args['attributes'] ) ) {
500 $args['attributes'] = array();
501 }
502
503 // Add metadata attribute if it doesn't exist.
504 if ( ! array_key_exists( 'metadata', $args['attributes'] ) ) {
505 $args['attributes']['metadata'] = array(
506 'type' => 'object',
507 );
508 }
509
510 return $args;
511 }
512
513 /**
514 * Filters woocommerce block types.
515 *
516 * @param string[] $block_types Array of woocommerce block types.
517 * @return array
518 */
519 public function get_block_types( $block_types ) {
520 if ( PageController::is_admin_page() ) {
521 // Ignore all woocommerce blocks.
522 return array();
523 }
524
525 return $block_types;
526 }
527 }
528