Make the BlockRegistry a singleton

This commit is contained in:
Matt Sherman 2023-12-06 20:07:52 -05:00
parent 8c6b414fe0
commit 1451626182
2 changed files with 28 additions and 10 deletions

View File

@ -64,6 +64,32 @@ class BlockRegistry {
'woocommerce/product-single-variation-notice',
);
/**
* Singleton instance.
*
* @var BlockRegistry
*/
private static $instance = null;
/**
* Get the singleton instance.
*/
public static function get_instance(): BlockRegistry {
if ( ! self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor
*/
protected function __construct() {
add_filter( 'block_categories_all', array( $this, 'register_categories' ), 10, 2 );
$this->register_product_blocks();
}
/**
* Get a file path for a given block file.
*
@ -74,14 +100,6 @@ class BlockRegistry {
return WC_ABSPATH . WCAdminAssets::get_path( 'js' ) . trailingslashit( $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.
*/

View File

@ -67,8 +67,8 @@ class Init {
add_action( 'current_screen', array( $this, 'set_current_screen_to_block_editor_if_wc_admin' ) );
$block_registry = new BlockRegistry();
$block_registry->init();
// Make sure the block registry is initialized so that core blocks are registered.
BlockRegistry::get_instance();
$tracks = new Tracks();
$tracks->init();