<?php
/**
 * Server-side render for blocksify/icon-list-item.
 *
 * A self-contained leaf — builds its own `<li>` from attributes. The
 * parent `blocksify/icon-list` render.php concatenates each item's
 * output into the `<ul>` shell (the child is NOT short-circuited at
 * `pre_render_block`, unlike Tabs). All visual styling (item box, icon
 * decoration, text typography, icon layout) is emitted by the PARENT's
 * per-instance CSS targeting the shared marker classes below — the item
 * carries no per-instance class of its own.
 *
 * Output shape (linked):
 *   <li class="bsy-iconlist__item">
 *     <a class="bsy-iconlist__link" href="…">
 *       <span class="bsy-iconlist__icon"><svg>…</svg></span>
 *       <span class="bsy-iconlist__text">Label</span>
 *     </a>
 *   </li>
 *
 * Non-linked uses a <span class="bsy-iconlist__link"> wrapper so the
 * flex layout CSS targets one class on both paths.
 *
 * `attributes.icon` is resolved by `Blocksify_Icons::render` (Lucide
 * sparse bag / custom-svg paste / inline-upload) — SVG is sanitised at
 * JS commit time so it's echoed verbatim.
 *
 * @package Blocksify
 *
 * @var array    $attributes Block attributes.
 * @var string   $content    Saved block markup (always empty for dynamic).
 * @var WP_Block $block      Parsed block instance.
 */

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

$icon        = isset( $attributes['icon'] ) && is_array( $attributes['icon'] ) ? $attributes['icon'] : null;
$text        = isset( $attributes['text'] ) ? (string) $attributes['text'] : '';
$url         = isset( $attributes['url'] ) ? (string) $attributes['url'] : '';
$link_target = isset( $attributes['linkTarget'] ) ? (string) $attributes['linkTarget'] : '';
$rel         = isset( $attributes['rel'] ) ? (string) $attributes['rel'] : '';

$icon_svg = Blocksify_Icons::render( $icon );
$icon_url = ( $icon && '' === $icon_svg && isset( $icon['url'] ) && is_string( $icon['url'] ) ) ? $icon['url'] : '';

$icon_html = '';
if ( '' !== $icon_svg ) {
	$icon_html = '<span class="bsy-iconlist__icon" aria-hidden="true">' . $icon_svg . '</span>';
} elseif ( '' !== $icon_url ) {
	$icon_html = '<span class="bsy-iconlist__icon" aria-hidden="true"><img src="'
		. esc_url( $icon_url ) . '" alt="" /></span>';
}

$text_html = '' !== $text
	? '<span class="bsy-iconlist__text">' . wp_kses_post( $text ) . '</span>'
	: '';

if ( '' === $icon_html && '' === $text_html ) {
	return '';
}

$inner = $icon_html . $text_html;

if ( '' !== $url ) {
	$link_attrs = ' href="' . esc_url( $url ) . '"';
	if ( '' !== $link_target ) {
		$link_attrs .= ' target="' . esc_attr( $link_target ) . '"';
	}
	if ( '' !== $rel ) {
		$link_attrs .= ' rel="' . esc_attr( $rel ) . '"';
	}
	$content_el = '<a class="bsy-iconlist__link"' . $link_attrs . '>' . $inner . '</a>';
} else {
	$content_el = '<span class="bsy-iconlist__link">' . $inner . '</span>';
}

echo '<li class="bsy-iconlist__item">' . $content_el . '</li>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
