<?php
/**
 * Brentwood Testimonial — front-end render.
 *
 * A quote testimonial migrated from brentwood.ca: large faded quote marks
 * (top-left + bottom-right), an italic body, a short dash, and an attribution
 * ("from who"). The attribution aligns right by default (left on mobile, like
 * the live site) or left when chosen.
 *
 * @var array    $attributes Block attributes.
 * @var string   $content    Inner content (unused — dynamic block).
 * @var WP_Block $block      Block instance.
 */

defined( 'ABSPATH' ) || exit;

// Quote body: in "blocks" mode it comes from the InnerBlocks content ($content,
// already rendered/safe); otherwise from the plain-text "quote" attribute. This
// keeps every existing plain-text testimonial rendering exactly as before.
$use_blocks = ! empty( $attributes['useBlocks'] );
if ( $use_blocks ) {
	$quote_body = trim( (string) $content );
} else {
	$plain      = trim( (string) ( $attributes['quote'] ?? '' ) );
	$quote_body = ( '' !== $plain ) ? wp_kses_post( $plain ) : '';
}

$attribution = trim( (string) ( $attributes['attribution'] ?? '' ) );
$align       = ( 'left' === ( $attributes['attributionAlign'] ?? 'right' ) ) ? 'left' : 'right';
$show_marks  = ! empty( $attributes['showQuoteMarks'] );
$show_dash   = ! empty( $attributes['showDash'] );

if ( '' === $quote_body && '' === $attribution ) {
	return;
}

// Quote block: the faded quote-mark images bracket the quote text, offset
// outward (top-left + bottom-right) exactly like brentwood.ca.
$quote_html = '';
if ( '' !== $quote_body ) {
	$open = $close = '';
	if ( $show_marks ) {
		$base  = get_stylesheet_directory_uri() . '/blocks/testimonial/images/';
		$open  = '<img class="bw-tt__mark bw-tt__mark--open" src="' . esc_url( $base . 'quote-start.png' ) . '" alt="" aria-hidden="true">';
		$close = '<img class="bw-tt__mark bw-tt__mark--close" src="' . esc_url( $base . 'quote-end.png' ) . '" alt="" aria-hidden="true">';
	}
	// Open mark anchors to the card (stays inside); close mark sits at the
	// bottom-right of the quote text. $quote_body is either rendered inner-block
	// HTML (already safe) or the kses'd legacy attribute.
	$quote_html = $open . '<div class="bw-tt__quote">' . $close . '<div class="bw-tt__body">' . $quote_body . '</div></div>';
}

$dash = ( $show_dash && '' !== $attribution )
	? '<img class="bw-tt__dash" src="' . esc_url( get_stylesheet_directory_uri() . '/blocks/testimonial/images/dash.svg' ) . '" alt="" aria-hidden="true">'
	: '';

$from = ( '' !== $attribution )
	? '<div class="bw-tt__from"><div class="bw-tt__from-inner">' . wp_kses_post( $attribution ) . '</div></div>'
	: '';

printf(
	'<div %s><div class="bw-tt__inner">%s%s%s</div></div>',
	get_block_wrapper_attributes( array( 'class' => 'bw-tt bw-tt--' . $align ) ), // phpcs:ignore WordPress.Security.EscapeOutput
	$quote_html, // phpcs:ignore WordPress.Security.EscapeOutput -- kses'd / escaped above
	$dash,       // phpcs:ignore WordPress.Security.EscapeOutput -- literal markup
	$from        // phpcs:ignore WordPress.Security.EscapeOutput -- kses'd above
);
