get( 'chat_widget_title', __( 'AI Support Assistant', 'ai-support-assistant' ) ); if ( empty( $default_title ) ) { $default_title = __( 'AI Support Assistant', 'ai-support-assistant' ); } wp_localize_script( 'ai-support-assistant-block-js', 'AISupportAssistantBlockConfig', array( 'defaultTitle' => esc_html( $default_title ), ) ); // If block.json exists, attempt registering via block.json first. $block_json = AI_SUPPORT_ASSISTANT_PATH . 'blocks/block.json'; if ( file_exists( $block_json ) && function_exists( 'register_block_type_from_metadata' ) ) { register_block_type_from_metadata( $block_json, array( 'editor_script' => 'ai-support-assistant-block-js', 'render_callback' => array( $this, 'render_block' ), ) ); } else { register_block_type( 'ai-support-assistant/widget', array( 'editor_script' => 'ai-support-assistant-block-js', 'render_callback' => array( $this, 'render_block' ), 'title' => __( 'AI Support Assistant Widget', 'ai-support-assistant' ), 'description' => __( 'Embed the AI Support Assistant assistant widget into any page or post.', 'ai-support-assistant' ), 'category' => 'widgets', 'icon' => 'format-chat', 'keywords' => array( 'chat', 'ai', 'support', 'assistant' ), 'attributes' => array( 'title' => array( 'type' => 'string', 'default' => '' ), 'theme' => array( 'type' => 'string', 'default' => 'light' ), 'width' => array( 'type' => 'string', 'default' => '450px' ), 'height' => array( 'type' => 'string', 'default' => '600px' ), ), ) ); } } /** * Dynamic block render callback. * * @param array $attributes Block attributes from editor. * @return string HTML rendered output. */ public function render_block( array $attributes ): string { $shortcode = Shortcode::get_instance(); $mapped_atts = array(); if ( isset( $attributes['title'] ) && trim( (string) $attributes['title'] ) !== '' ) { $mapped_atts['title'] = trim( (string) $attributes['title'] ); } if ( isset( $attributes['theme'] ) && trim( (string) $attributes['theme'] ) !== '' ) { $mapped_atts['theme'] = trim( (string) $attributes['theme'] ); } if ( isset( $attributes['width'] ) && trim( (string) $attributes['width'] ) !== '' ) { $mapped_atts['width'] = trim( (string) $attributes['width'] ); } if ( isset( $attributes['height'] ) && trim( (string) $attributes['height'] ) !== '' ) { $mapped_atts['height'] = trim( (string) $attributes['height'] ); } return $shortcode->render_shortcode( $mapped_atts ); } }