<?php
if (!defined('ABSPATH')) {
    exit;
}
$wrapper_id = sanitize_html_class((string) ($wrapper_id ?? wp_unique_id('eep-widget-bento-')));
$template = sanitize_html_class((string) ($template ?? '1'));
$title = sanitize_text_field((string) ($title ?? ''));
$subtitle = sanitize_text_field((string) ($subtitle ?? ''));
$style_vars = trim((string) ($style_vars ?? ''));
$custom_class = trim((string) ($custom_class ?? ''));
$results_markup = (string) ($results_markup ?? '');
$query_count = absint((int) ($query_count ?? 0));
$custom_css = trim((string) ($custom_css ?? ''));
$custom_css_scoped = '';
if ($custom_css !== '') {
    $custom_css_scoped = str_replace('{{WRAPPER}}', '#' . $wrapper_id, $custom_css);
    if (strpos($custom_css_scoped, '{') === false) {
        $custom_css_scoped = '#' . $wrapper_id . '{' . $custom_css_scoped . '}';
    }
}
$wrapper_classes = trim('eep-widget-block eep-widget-bento-block eep-widget-template-' . $template . ' ' . $custom_class);
?>
<section id="<?php echo esc_attr($wrapper_id); ?>" class="<?php echo esc_attr($wrapper_classes); ?>"<?php echo $style_vars !== '' ? ' style="' . esc_attr($style_vars) . '"' : ''; ?>>
    <?php if ($title !== '' || $subtitle !== '') : ?>
        <header class="eep-widget-header">
            <?php if ($title !== '') : ?>
                <h3 class="eep-widget-title"><?php echo esc_html($title); ?></h3>
            <?php endif; ?>
            <?php if ($subtitle !== '') : ?>
                <p class="eep-widget-subtitle"><?php echo esc_html($subtitle); ?></p>
            <?php endif; ?>
        </header>
    <?php endif; ?>

    <div class="eep-widget-meta">
        <span class="eep-widget-count"><?php echo esc_html(sprintf(_n('%d result', '%d results', $query_count, 'nexdirectory'), $query_count)); ?></span>
    </div>

    <div class="eep-widget-results-wrap">
        <?php echo $results_markup; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
    </div>

    <?php if ($custom_css_scoped !== '') : ?>
        <style><?php echo eep_strip_all_tags($custom_css_scoped); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></style>
    <?php endif; ?>
</section>
