• Resolved orsacchiotto80

    (@orsacchiotto80)


    Dear Nikita,

    I’m a kind of young developer and I’ve been using lazyblock for a while. My approach is in a sense different: instead of storing or preparing each block on the db, I hard-code them as a php element:

    }class Isabeltheme_Lazyblock {
    
    public function __construct() {
    
    add_action( 'init', array( $this, 'load_my_lazy_blocks' ));
    
    add_action( 'lzb/handlebars/object', array($this, 'project_custom_lazyblock_handlebars_helper'));
    
    }
    
    public function load_my_lazy_blocks() {
    
    if (function_exists('lazyblocks')) {
    
    $blocks_dir = get_template_directory() . '/lazy-blocks/';
    
    if (!is_dir($blocks_dir)) {
    
    return;
    
    }
    
    $blocks = array_diff(scandir($blocks_dir), array('.', '..'));
    
    foreach ($blocks as $block) {
    
    if (!is_dir($blocks_dir . $block) || !file_exists($blocks_dir . $block . '/' . $block . '-block.php')) {
    
    continue;
    
    }
    
    add_filter('lazyblock/' . $block . '/allow_wrapper', function ($a, $b) {
    
    return false;
    
    }, 10, 2);
    
    require_once $blocks_dir . $block . '/' . $block . '-block.php';
    
    }
    
    }
    
    }
    
    public function project_custom_lazyblock_handlebars_helper(Handlebars\Handlebars $handlebars) {}

    After then, I only want to use them instead of the core ones or others, so with my lazyblock.js file I will unregister those ones that I don’t need:

    document.addEventListener('DOMContentLoaded', function() { 
    	wp.blocks.getBlockTypes().forEach( function( blockType ){ 
    		let match = "lazyblock";
    		if (!blockType.name.match(match)) {
    			wp.blocks.unregisterBlockType(blockType.name);
    		}
    	});
    });
    //or
    wp.domReady(function () {}

    I recently noticed that my approach is not valid anymore because I was somehow forced to also include the tadv and the core/paragraph item.

    wp.domReady(function () {
    	
    	wp.blocks.getBlockTypes().forEach( function( blockType ) {
    		let match1 = "lazyblock";
    		let match2 = "tadv";
    		let match3 = "core/paragraph";
    		if (!blockType.name.startsWith(match1) && !blockType.name.startsWith(match2) && blockType.name !== match3) {
    			wp.blocks.unregisterBlockType(blockType.name);
    		}
    	});
    });

    I was wondering if you have a better suggestion on how to approach this issue and continue my script in order to rely only on lazyblock (also, today I notice that the ghostkit were also added.

    Thank you in advance and I wish you a good rest of the day!

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Php approach to use lazyblock in place or’ is closed to new replies.