init_hooks(); } } /** * Init Hooks */ public function init_hooks() { add_action( 'enqueue_block_assets', array( $this, 'enqueue_block_assets' ) ); add_action( 'init', array( $this, 'register_block' ), 0 ); add_action( 'init', array( $this, 'register_attributes_for_registered_blocks' ), 99 ); } /** * Enqueue block assets */ public function enqueue_block_assets() { $js_deps = array( 'wp-i18n', 'wp-element', 'wp-components', 'wp-block-editor', 'lodash', 'jquery', 'jarallax', 'jarallax-video' ); $css_deps = array(); // enqueue block js. wp_register_script( 'awb-gutenberg', nk_awb()->plugin_url . 'assets/admin/gutenberg/index.min.js', $js_deps, filemtime( nk_awb()->plugin_path . 'assets/admin/gutenberg/index.min.js' ), true ); // enqueue block css. wp_register_style( 'awb-gutenberg', nk_awb()->plugin_url . 'assets/admin/gutenberg/editor.min.css', $css_deps, filemtime( nk_awb()->plugin_path . 'assets/admin/gutenberg/editor.min.css' ) ); // add variables to script. $data = array( 'placeholder_url' => nk_awb()->plugin_url . 'assets/images/placeholder.jpg', 'full_width_fallback' => ! ( ( function_exists( 'wp_is_block_theme' ) && wp_is_block_theme() ) || get_theme_support( 'align-wide' ) ), 'is_ghostkit_active' => class_exists( 'GhostKit' ), ); wp_localize_script( 'awb-gutenberg', 'AWBGutenbergData', $data ); } /** * Enqueue admin scripts hook */ public function register_block() { // register block. register_block_type_from_metadata( nk_awb()->plugin_path . 'assets/admin/gutenberg', array( 'render_callback' => array( $this, 'render_block' ), ) ); // Register the block support. WP_Block_Supports::get_instance()->register( 'awb', array( 'register_attribute' => array( $this, 'register_attribute' ), ) ); } /** * Renders the block on server. * Adds Featured Image when useFeaturedImage is true. * * @param array $attributes The block attributes. * @param string $content The block rendered content. * * @return string */ public function render_block( $attributes, $content ) { if ( 'image' !== $attributes['type'] || false === $attributes['useFeaturedImage'] ) { return $content; } $regex = '/