Add a category for product editor blocks (#37347)
* Add category for product editor blocks * Add changelog entry * Fix php lint errors
This commit is contained in:
parent
71cde2c856
commit
b1c9ced2a5
|
@ -0,0 +1,4 @@
|
||||||
|
Significance: minor
|
||||||
|
Type: add
|
||||||
|
|
||||||
|
Add a category for product editor blocks
|
|
@ -31,19 +31,45 @@ class BlockRegistry {
|
||||||
*
|
*
|
||||||
* @param string $path File path.
|
* @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;
|
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.
|
* Register all the product blocks.
|
||||||
*/
|
*/
|
||||||
public function register_product_blocks() {
|
private function register_product_blocks() {
|
||||||
foreach ( self::PRODUCT_BLOCKS as $block_name ) {
|
foreach ( self::PRODUCT_BLOCKS as $block_name ) {
|
||||||
$this->register_block( $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.
|
* Get the block name without the "woocommerce/" prefix.
|
||||||
*
|
*
|
||||||
|
@ -51,7 +77,7 @@ class BlockRegistry {
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function remove_block_prefix( $block_name ) {
|
private function remove_block_prefix( $block_name ) {
|
||||||
if ( 0 === strpos( $block_name, 'woocommerce/' ) ) {
|
if ( 0 === strpos( $block_name, 'woocommerce/' ) ) {
|
||||||
return substr_replace( $block_name, '', 0, strlen( '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.
|
* @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_name = $this->remove_block_prefix( $block_name );
|
||||||
$block_json_file = $this->get_file_path( $block_name . '/block.json' );
|
$block_json_file = $this->get_file_path( $block_name . '/block.json' );
|
||||||
|
|
||||||
|
|
|
@ -23,6 +23,11 @@ class Init {
|
||||||
*/
|
*/
|
||||||
const TOGGLE_OPTION_NAME = 'woocommerce_' . self::FEATURE_ID . '_enabled';
|
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
|
* Constructor
|
||||||
*/
|
*/
|
||||||
|
@ -34,7 +39,7 @@ class Init {
|
||||||
if ( Features::is_enabled( self::FEATURE_ID ) ) {
|
if ( Features::is_enabled( self::FEATURE_ID ) ) {
|
||||||
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
|
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
|
||||||
$block_registry = new BlockRegistry();
|
$block_registry = new BlockRegistry();
|
||||||
$block_registry->register_product_blocks();
|
$block_registry->init();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,7 +51,7 @@ class Init {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$post_type_object = get_post_type_object( 'product' );
|
$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();
|
$editor_settings = array();
|
||||||
if ( ! empty( $post_type_object->template ) ) {
|
if ( ! empty( $post_type_object->template ) ) {
|
||||||
|
@ -64,6 +69,11 @@ class Init {
|
||||||
'var productBlockEditorSettings = productBlockEditorSettings || ' . wp_json_encode( $editor_settings ) . ';',
|
'var productBlockEditorSettings = productBlockEditorSettings || ' . wp_json_encode( $editor_settings ) . ';',
|
||||||
'before'
|
'before'
|
||||||
);
|
);
|
||||||
|
wp_add_inline_script(
|
||||||
|
$script_handle,
|
||||||
|
sprintf( 'wp.blocks.setCategories( %s );', wp_json_encode( $editor_settings['blockCategories'] ) ),
|
||||||
|
'before'
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue