Skip to content

Commit

Permalink
Register & require_once block variations
Browse files Browse the repository at this point in the history
  • Loading branch information
carstingaxion committed Sep 21, 2024
1 parent d3a1dfe commit f749adb
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions includes/core/classes/class-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ protected function __construct() {
*/
protected function setup_hooks(): void {
add_action( 'init', array( $this, 'register_block_patterns' ) );
add_action( 'init', array( $this, 'register_block_variations' ) );
// Priority 11 needed for block.json translations of title and description.
add_action( 'init', array( $this, 'register_blocks' ), 11 );
}
Expand All @@ -74,6 +75,35 @@ public function register_blocks(): void {
}
}

public function register_block_variations(): void {
foreach ( $this->get_block_variations() as $block ) {
$name = $this->get_classname_from_foldername( $block );
require_once sprintf( '%1$s/build/variations/%2$s/class-%2$s.php', GATHERPRESS_CORE_PATH, $block );
$name::get_instance();
}
}

protected static function get_block_variations(): array {
$blocks_directory = sprintf( '%1$s/build/variations/', GATHERPRESS_CORE_PATH );
$blocks = array_diff( scandir( $blocks_directory ), array( '..', '.' ) );

return $blocks; // maybe cache in var.
}

/**
* Get class name from folder name.
*
* @param string $foldername
*
* @return string
*/
protected static function get_classname_from_foldername( string $foldername ) : string {
return join( '\\', array(
__CLASS__,
ucwords( str_replace( '-', '_', $foldername ), '_' )
));
}


/**
* Register block patterns.
Expand Down

0 comments on commit f749adb

Please sign in to comment.