'method_name', 'after_setup_theme' => array( 'function_to_add' => 'register', ), 'enqueue_block_editor_assets' => 'enqueue', ); } /** * The plugin's options * * @var string */ private $options = ''; /** * List of all widget classes name. * * @var array */ private $blocks_list = array(); /** * Injector object * * @var null */ private $injector = null; /** * Fire the construct */ public function __construct( array $options = array(), $injector = null ) { $this->options = $options; $this->injector = $injector; $this->blocks_list = array( 'block_posts' => 'ItalyStrap\\Blocks\\Posts', ); } /** * Enqueue script */ public function enqueue() { wp_enqueue_script( 'italystrap-posts', plugins_url( 'index.build.js', __FILE__ ), array( 'wp-blocks', 'wp-element', 'wp-api' ), rand(), true ); } /** * Add action to widget_init * Initialize widget */ public function register() { if ( ! function_exists( 'register_block_type' ) ) { return; } foreach ( (array) $this->blocks_list as $option_name => $class_name ) { // if ( empty( $this->options[ $option_name ] ) ) { // continue; // } $block_name = str_replace( 'block_', '', $option_name ); /** * Block object */ $$block_name = $this->injector->make( $class_name, [ ':block_type' => "italystrap/{$block_name}", ':args' => [ // 'render_callback' => '', // Defined in abstract Block::class 'attributes' => [ 'orderBy' => [ 'type' => 'string', 'default' => 'date', ], ], ], ] ); /** * We don't need to pass the string name and args for the block because * we just passed the obj to the first argument. */ register_block_type( $$block_name ); } } }