From b1c9ced2a57290c3151bc8f4f557206e73cbc63c Mon Sep 17 00:00:00 2001 From: Joshua T Flowers Date: Thu, 13 Apr 2023 17:55:01 -0700 Subject: [PATCH] Add a category for product editor blocks (#37347) * Add category for product editor blocks * Add changelog entry * Fix php lint errors --- plugins/woocommerce/changelog/fix-37200 | 4 +++ .../ProductBlockEditor/BlockRegistry.php | 34 ++++++++++++++++--- .../Features/ProductBlockEditor/Init.php | 14 ++++++-- 3 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 plugins/woocommerce/changelog/fix-37200 diff --git a/plugins/woocommerce/changelog/fix-37200 b/plugins/woocommerce/changelog/fix-37200 new file mode 100644 index 00000000000..ecce3e178be --- /dev/null +++ b/plugins/woocommerce/changelog/fix-37200 @@ -0,0 +1,4 @@ +Significance: minor +Type: add + +Add a category for product editor blocks diff --git a/plugins/woocommerce/src/Admin/Features/ProductBlockEditor/BlockRegistry.php b/plugins/woocommerce/src/Admin/Features/ProductBlockEditor/BlockRegistry.php index 0271e4cf7ac..473a476982b 100644 --- a/plugins/woocommerce/src/Admin/Features/ProductBlockEditor/BlockRegistry.php +++ b/plugins/woocommerce/src/Admin/Features/ProductBlockEditor/BlockRegistry.php @@ -31,19 +31,45 @@ class BlockRegistry { * * @param string $path File path. */ - public function get_file_path( $path ) { + private function get_file_path( $path ) { return WC_ABSPATH . WCAdminAssets::get_path( 'js' ) . trailingslashit( self::BLOCKS_DIR ) . $path; } + /** + * Initialize all blocks. + */ + public function init() { + add_filter( 'block_categories_all', array( $this, 'register_categories' ), 10, 2 ); + $this->register_product_blocks(); + } + /** * Register all the product blocks. */ - public function register_product_blocks() { + private function register_product_blocks() { foreach ( self::PRODUCT_BLOCKS as $block_name ) { $this->register_block( $block_name ); } } + /** + * Register product related block categories. + * + * @param array[] $block_categories Array of categories for block types. + * @param WP_Block_Editor_Context $editor_context The current block editor context. + */ + public function register_categories( $block_categories, $editor_context ) { + if ( INIT::EDITOR_CONTEXT_NAME === $editor_context->name ) { + $block_categories[] = array( + 'slug' => 'woocommerce', + 'title' => __( 'WooCommerce', 'woocommerce' ), + 'icon' => null, + ); + } + + return $block_categories; + } + /** * Get the block name without the "woocommerce/" prefix. * @@ -51,7 +77,7 @@ class BlockRegistry { * * @return string */ - public function remove_block_prefix( $block_name ) { + private function remove_block_prefix( $block_name ) { if ( 0 === strpos( $block_name, 'woocommerce/' ) ) { return substr_replace( $block_name, '', 0, strlen( 'woocommerce/' ) ); } @@ -66,7 +92,7 @@ class BlockRegistry { * * @return WP_Block_Type|false The registered block type on success, or false on failure. */ - public function register_block( $block_name ) { + private function register_block( $block_name ) { $block_name = $this->remove_block_prefix( $block_name ); $block_json_file = $this->get_file_path( $block_name . '/block.json' ); diff --git a/plugins/woocommerce/src/Admin/Features/ProductBlockEditor/Init.php b/plugins/woocommerce/src/Admin/Features/ProductBlockEditor/Init.php index 3f81ba822d7..eccdc352acb 100644 --- a/plugins/woocommerce/src/Admin/Features/ProductBlockEditor/Init.php +++ b/plugins/woocommerce/src/Admin/Features/ProductBlockEditor/Init.php @@ -23,6 +23,11 @@ class Init { */ const TOGGLE_OPTION_NAME = 'woocommerce_' . self::FEATURE_ID . '_enabled'; + /** + * The context name used to identify the editor. + */ + const EDITOR_CONTEXT_NAME = 'woocommerce/edit-product'; + /** * Constructor */ @@ -34,7 +39,7 @@ class Init { if ( Features::is_enabled( self::FEATURE_ID ) ) { add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); $block_registry = new BlockRegistry(); - $block_registry->register_product_blocks(); + $block_registry->init(); } } @@ -46,7 +51,7 @@ class Init { return; } $post_type_object = get_post_type_object( 'product' ); - $block_editor_context = new WP_Block_Editor_Context( array( 'name' => 'core/edit-post' ) ); + $block_editor_context = new WP_Block_Editor_Context( array( 'name' => self::EDITOR_CONTEXT_NAME ) ); $editor_settings = array(); if ( ! empty( $post_type_object->template ) ) { @@ -64,6 +69,11 @@ class Init { 'var productBlockEditorSettings = productBlockEditorSettings || ' . wp_json_encode( $editor_settings ) . ';', 'before' ); + wp_add_inline_script( + $script_handle, + sprintf( 'wp.blocks.setCategories( %s );', wp_json_encode( $editor_settings['blockCategories'] ) ), + 'before' + ); } /**