<?php
/**
 * Native dynamic blocks bundled with HaPantheon.
 *
 * @package HaPantheon
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Apply Gutenberg wrapper attributes to the source form element itself.
 *
 * The source contract does not contain a generic block wrapper around either
 * form. This helper merges the generated block class/id/style attributes into
 * the first form tag instead of changing the public hierarchy.
 *
 * @param string $html        Rendered form HTML.
 * @param string $class_name  Additional block class.
 * @return string
 */
function hapantheon_form_block_root( $html, $class_name ) {
	if ( ! function_exists( 'get_block_wrapper_attributes' ) ) {
		return $html;
	}

	$wrapper = get_block_wrapper_attributes( array( 'class' => $class_name ) );
	$classes = '';
	if ( preg_match( '/(?:^|\s)class="([^"]*)"/', $wrapper, $match ) ) {
		$classes = trim( $match[1] );
		$wrapper = trim( preg_replace( '/(?:^|\s)class="[^"]*"/', '', $wrapper, 1 ) );
	}

	return preg_replace_callback(
		'/<form\b([^>]*)\bclass="([^"]*)"([^>]*)>/',
		function ( $match ) use ( $wrapper, $classes ) {
			$attributes = trim( $wrapper . ' ' . $match[1] . ' class="' . trim( $match[2] . ' ' . $classes ) . '" ' . $match[3] );
			return '<form ' . $attributes . '>';
		},
		$html,
		1
	);
}

/**
 * Render the contact form block.
 *
 * @param array $attributes Block attributes.
 * @return string
 */
function hapantheon_render_contact_form_block( $attributes ) {
	return hapantheon_form_block_root(
		hapantheon_contact_form_shortcode( $attributes ),
		'hapantheon-form-block hapantheon-contact-form-block'
	);
}

/**
 * Render the newsletter block.
 *
 * @param array $attributes Block attributes.
 * @return string
 */
function hapantheon_render_newsletter_block( $attributes ) {
	return hapantheon_form_block_root(
		hapantheon_newsletter_shortcode( $attributes ),
		'hapantheon-form-block hapantheon-newsletter-block'
	);
}

/**
 * Register editor assets and dynamic blocks.
 */
function hapantheon_register_blocks() {
	wp_register_script(
		'hapantheon-editor-blocks',
		HAPANTHEON_URI . '/assets/js/editor-blocks.js',
		array( 'wp-blocks', 'wp-block-editor', 'wp-components', 'wp-element', 'wp-i18n' ),
		HAPANTHEON_VERSION,
		true
	);

	wp_register_style(
		'hapantheon-editor-blocks',
		HAPANTHEON_URI . '/assets/css/editor-blocks.css',
		array( 'wp-edit-blocks' ),
		HAPANTHEON_VERSION
	);

	$contact_attributes = array();
	foreach ( hapantheon_contact_form_defaults() as $key => $default ) {
		$contact_attributes[ $key ] = array(
			'type'    => 'string',
			'default' => $default,
		);
	}

	register_block_type(
		'hapantheon/contact-form',
		array(
			'api_version'     => 2,
			'title'           => __( 'HaPantheon Contact Form', 'hapantheon' ),
			'description'     => __( 'Editable contact form with mail delivery, consent and status messages.', 'hapantheon' ),
			'category'        => 'widgets',
			'icon'            => 'email-alt',
			'keywords'        => array( __( 'contact', 'hapantheon' ), __( 'form', 'hapantheon' ), __( 'email', 'hapantheon' ) ),
			'attributes'      => $contact_attributes,
			'supports'        => array(
				'align'   => array( 'wide', 'full' ),
				'anchor'  => true,
				'html'    => false,
				'color'   => array(
					'background' => true,
					'text'       => true,
					'gradients'  => true,
				),
				'spacing' => array(
					'margin'  => true,
					'padding' => true,
				),
			),
			'editor_script'   => 'hapantheon-editor-blocks',
			'editor_style'    => 'hapantheon-editor-blocks',
			'render_callback' => 'hapantheon_render_contact_form_block',
		)
	);

	$newsletter_attributes = array();
	foreach ( hapantheon_newsletter_defaults() as $key => $default ) {
		$newsletter_attributes[ $key ] = array(
			'type'    => 'string',
			'default' => $default,
		);
	}

	register_block_type(
		'hapantheon/newsletter',
		array(
			'api_version'     => 2,
			'title'           => __( 'HaPantheon Newsletter', 'hapantheon' ),
			'description'     => __( 'Editable newsletter signup form stored in WordPress.', 'hapantheon' ),
			'category'        => 'widgets',
			'icon'            => 'megaphone',
			'keywords'        => array( __( 'newsletter', 'hapantheon' ), __( 'signup', 'hapantheon' ), __( 'email', 'hapantheon' ) ),
			'attributes'      => $newsletter_attributes,
			'supports'        => array(
				'align'   => array( 'wide', 'full' ),
				'anchor'  => true,
				'html'    => false,
				'color'   => array(
					'background' => true,
					'text'       => true,
					'gradients'  => true,
				),
				'spacing' => array(
					'margin'  => true,
					'padding' => true,
				),
			),
			'editor_script'   => 'hapantheon-editor-blocks',
			'editor_style'    => 'hapantheon-editor-blocks',
			'render_callback' => 'hapantheon_render_newsletter_block',
		)
	);
}
add_action( 'init', 'hapantheon_register_blocks' );
