<?php
/**
 * Gutenberg block wrapper for the visit-request form.
 *
 * Registers the wrenchpress/visit-form dynamic block from its block.json. The
 * render_callback enqueues the form assets and RETURNS the rendered markup
 * string from the same shared PHP core as the shortcode, so the editor preview,
 * the front end, and the classic editor all render identically.
 *
 * @package WrenchPress
 */

defined( 'ABSPATH' ) || exit;

/**
 * Class WrenchPress_Form_Block
 */
class WrenchPress_Form_Block {

	/**
	 * Register the block on init.
	 *
	 * @return void
	 */
	public static function register() {
		add_action( 'init', array( __CLASS__, 'register_block' ) );
	}

	/**
	 * Register the dynamic block from its block.json metadata.
	 *
	 * @return void
	 */
	public static function register_block() {
		if ( ! function_exists( 'register_block_type' ) ) {
			return;
		}

		register_block_type(
			WRENCHPRESS_PATH . 'blocks/visit-form',
			array(
				'render_callback' => array( __CLASS__, 'render' ),
			)
		);
	}

	/**
	 * Render the form for the block.
	 *
	 * @param array $attributes Block attributes.
	 * @return string Form markup.
	 */
	public static function render( $attributes ) {
		unset( $attributes );

		WrenchPress_Visit_Form::enqueue_assets();

		return WrenchPress_Visit_Form::render( array( 'source' => 'block' ) );
	}
}
