| @@ -3,64 +3,87 @@ | ||
| 3 | 3 | * Register Blocks |
| 4 | 4 | * @package GutSliderBlocks |
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | - if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly. | |
| 8 | - | |
| 9 | - if( ! class_exists( 'GutSlider_Register_Blocks' ) ) { | |
| 7 | + if( ! defined( 'ABSPATH' ) ) { | |
| 8 | + exit; | |
| 9 | + } | |
| 10 | 10 | |
| 11 | - class GutSlider_Register_Blocks { | |
| 11 | + if( ! class_exists( 'GutSliderBlocks_Registration' ) ) { | |
| 12 | 12 | |
| 13 | + /** | |
| 14 | + * @since 1.0.0 | |
| 15 | + * @package GutSlider Blocks | |
| 16 | + */ | |
| 17 | + class GutSliderBlocks_Registration { | |
| 18 | + | |
| 13 | 19 | /** |
| 14 | 20 | * Constructor |
| 15 | - * return void | |
| 21 | + * | |
| 22 | + * @since 1.0.0 | |
| 23 | + * @return void | |
| 16 | 24 | */ |
| 17 | 25 | public function __construct() { |
| 18 | - add_action( 'init', [ $this, 'register_blocks' ] ); | |
| 26 | + $this->init(); | |
| 19 | 27 | } |
| 20 | 28 | |
| 21 | 29 | /** |
| 30 | + * Initialize the Class | |
| 31 | + * | |
| 32 | + * @since 1.0.0 | |
| 33 | + * @return void | |
| 34 | + */ | |
| 35 | + private function init() { | |
| 36 | + add_action( 'init', array ( $this, 'register_blocks' ) ); | |
| 37 | + } | |
| 38 | + | |
| 39 | + /** | |
| 22 | 40 | * Register Blocks |
| 23 | - * return void | |
| 41 | + * | |
| 42 | + * @since 1.0.0 | |
| 43 | + * @return void | |
| 24 | 44 | */ |
| 25 | 45 | public function register_blocks() { |
| 26 | - $blocksFolder = trailingslashit( GUTSLIDER_DIR ) . '/build/blocks'; | |
| 46 | + | |
| 47 | + $blocks = get_option( 'gutslider_blocks', $this->get_gutslider_blocks() ); | |
| 27 | 48 | |
| 28 | - if ( ! is_dir( $blocksFolder ) ) { | |
| 29 | - return; | |
| 30 | - } | |
| 49 | + if ( ! empty( $blocks ) && is_array( $blocks ) ) { | |
| 50 | + foreach ( $blocks as $block ) { | |
| 51 | + $path = GUTSLIDER_DIR_PATH; | |
| 31 | 52 | |
| 32 | - $active_blocks = []; | |
| 53 | + if( isset( $block['is_pro'] ) && $block['is_pro'] === true && defined( 'GUTSLIDER_PRO_PATH' ) ) { | |
| 54 | + $path = GUTSLIDER_PRO_PATH; | |
| 55 | + } | |
| 33 | 56 | |
| 34 | - $content_slider = get_option('gut_fixed_content_slider', true); | |
| 35 | - $any_content_slider = get_option('gut_any_content_slider', true); | |
| 36 | - $testimonial_slider = get_option('gut_testimonial_slider', true); | |
| 57 | + $block_name = trailingslashit( $path ) . 'build/blocks/' . $block['name']; | |
| 58 | + $full_block_name = 'gutsliders/' . $block['name']; | |
| 37 | 59 | |
| 38 | - $contents = scandir( $blocksFolder ); | |
| 39 | - | |
| 40 | - foreach ( $contents as $item ) { | |
| 41 | - if ( in_array( $item, ['.', '..'] ) ) { | |
| 42 | - continue; | |
| 43 | - } | |
| 44 | - | |
| 45 | - $itemPath = $blocksFolder . DIRECTORY_SEPARATOR . $item; | |
| 46 | - | |
| 47 | - if ( is_dir( $itemPath ) ) { | |
| 48 | - if ( ( ! $content_slider && $item === 'content-slider' ) || ( ! $any_content_slider && $item === 'any-content' ) || ( ! $testimonial_slider && $item === 'testimonial-slider' ) ) { | |
| 49 | - continue; | |
| 60 | + if ( isset( $block['active'] ) && $block['active'] === false ) { | |
| 61 | + // Check if the block type is registered before trying to unregister it | |
| 62 | + if( WP_Block_Type_Registry::get_instance()->is_registered( $full_block_name ) ) { | |
| 63 | + unregister_block_type( $full_block_name ); | |
| 64 | + } | |
| 65 | + } else { | |
| 66 | + if( file_exists( $block_name ) ) { | |
| 67 | + // Check if the block is not already registered | |
| 68 | + if ( ! WP_Block_Type_Registry::get_instance()->is_registered( $full_block_name ) ) { | |
| 69 | + register_block_type( $block_name ); | |
| 70 | + } | |
| 71 | + } | |
| 50 | 72 | } |
| 51 | - | |
| 52 | - $active_blocks[] = $item; | |
| 53 | 73 | } |
| 54 | 74 | } |
| 55 | - | |
| 56 | - if ( ! empty( $active_blocks ) ) { | |
| 57 | - foreach ( $active_blocks as $block ) { | |
| 58 | - register_block_type( trailingslashit( GUTSLIDER_DIR ) . '/build/blocks/' . $block); | |
| 59 | - } | |
| 60 | - } | |
| 61 | - } | |
| 75 | + } | |
| 76 | + | |
| 77 | + /** | |
| 78 | + * Get GutSlider Blocks | |
| 79 | + */ | |
| 80 | + public function get_gutslider_blocks() { | |
| 81 | + return require GUTSLIDER_DIR_PATH . 'includes/api/blocks.php'; | |
| 82 | + } | |
| 83 | + | |
| 84 | + | |
| 62 | 85 | } |
| 63 | 86 | |
| 64 | 87 | } |
| 65 | 88 | |
| 66 | - new GutSlider_Register_Blocks(); // Initialize the class | |
| 89 | + new GutSliderBlocks_Registration(); // Initialize the class. | |