| 1 |
<?php |
| 2 |
declare( strict_types=1 ); |
| 3 |
|
| 4 |
namespace GutSlider\Blocks; |
| 5 |
|
| 6 |
if ( ! defined( 'ABSPATH' ) ) { |
| 7 |
exit; |
| 8 |
} |
| 9 |
|
| 10 |
/** |
| 11 |
* Handles registration of all GutSlider Gutenberg blocks. |
| 12 |
* |
| 13 |
* Reads block definitions from the data file, checks whether each |
| 14 |
* block is enabled, and registers or unregisters them accordingly. |
| 15 |
* |
| 16 |
* @package GutSlider\Blocks |
| 17 |
* @since 3.0.0 |
| 18 |
*/ |
| 19 |
final class RegisterBlocks { |
| 20 |
|
| 21 |
/** |
| 22 |
* Constructor. |
| 23 |
* |
| 24 |
* Registers WordPress hooks for block registration. |
| 25 |
* |
| 26 |
* @since 3.0.0 |
| 27 |
*/ |
| 28 |
public function __construct() { |
| 29 |
add_action( 'init', array( $this, 'register_blocks' ) ); |
| 30 |
|
| 31 |
if ( ! wp_is_block_theme() ) { |
| 32 |
add_filter( 'should_load_separate_core_block_assets', '__return_true' ); |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Register all GutSlider blocks. |
| 38 |
* |
| 39 |
* Iterates through the block definitions, registers enabled blocks, |
| 40 |
* and unregisters disabled ones from the block type registry. |
| 41 |
* |
| 42 |
* @since 3.0.0 |
| 43 |
* |
| 44 |
* @return void |
| 45 |
*/ |
| 46 |
public function register_blocks(): void { |
| 47 |
$blocks = get_option( 'gutslider_blocks', $this->get_gutslider_blocks() ); |
| 48 |
|
| 49 |
if ( empty( $blocks ) || ! is_array( $blocks ) ) { |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
foreach ( $blocks as $block ) { |
| 54 |
$path = GUTSLIDER_DIR_PATH; |
| 55 |
|
| 56 |
if ( isset( $block['is_pro'] ) && true === $block['is_pro'] && defined( 'GUTSLIDER_PRO_PATH' ) ) { |
| 57 |
$path = GUTSLIDER_PRO_PATH; |
| 58 |
} |
| 59 |
|
| 60 |
$block_name = trailingslashit( $path ) . 'build/blocks/' . $block['name']; |
| 61 |
$block_key = $block['name']; |
| 62 |
$registry = \WP_Block_Type_Registry::get_instance(); |
| 63 |
|
| 64 |
if ( $this->is_block_enabled( $block_key ) ) { |
| 65 |
if ( file_exists( $block_name ) && ! $registry->is_registered( 'gutsliders/' . $block['name'] ) ) { |
| 66 |
register_block_type( $block_name ); |
| 67 |
} |
| 68 |
} elseif ( $registry->is_registered( 'gutsliders/' . $block['name'] ) ) { |
| 69 |
unregister_block_type( 'gutsliders/' . $block['name'] ); |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Get the default GutSlider block definitions. |
| 76 |
* |
| 77 |
* @since 3.0.0 |
| 78 |
* |
| 79 |
* @return array<int, array<string, mixed>> Array of block definition arrays. |
| 80 |
*/ |
| 81 |
public function get_gutslider_blocks(): array { |
| 82 |
return require GUTSLIDER_DIR_PATH . 'includes/Api/blocks.php'; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Check if a specific block is enabled. |
| 87 |
* |
| 88 |
* Reads the block's enabled status from a WordPress option. |
| 89 |
* Blocks are enabled by default. |
| 90 |
* |
| 91 |
* @since 3.0.0 |
| 92 |
* |
| 93 |
* @param string $block_name The block name identifier. |
| 94 |
* @return bool Whether the block is enabled. |
| 95 |
*/ |
| 96 |
private function is_block_enabled( string $block_name ): bool { |
| 97 |
$option_key = 'gut_' . str_replace( '-', '_', $block_name ); |
| 98 |
return (bool) get_option( $option_key, true ); |
| 99 |
} |
| 100 |
} |
| 101 |
|