<?php if (!defined('ABSPATH')) exit; ?>
<?php
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Template variable used by included files.
$ofc = false;
?>

<?php include(ANNFU_PLUGIN_PATH_TEMPLATE_DEFAULT . '_custom-css.php') ?>

<?php
// Cache per evitare chiamate multiple allo stesso endpoint
global $annfu_cache_annunci;
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Local variable for cache key.
$cache_key = annfu_query_annunci();

if (!isset($annfu_cache_annunci[$cache_key])) {
    // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Local variable for API response.
    $response = wp_remote_get(ANNFU_SITE_API . '/v2/annunci' . $cache_key, ['sslverify' => false, 'timeout' => ANNFU_REMOTE_TIMEOUT]);
    $annfu_cache_annunci[$cache_key] = json_decode(wp_remote_retrieve_body($response), true);
}
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Template variable used by included files.
$r = $annfu_cache_annunci[$cache_key];
?>

<?php
// Controlla se c'è un template Elementor selezionato per la lista annunci
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Local variable for template ID.
$templateElementorId = get_option('annfu_template_elementor_annunci', '');
if (!empty($templateElementorId) && did_action('elementor/loaded')) {
    // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
    echo \Elementor\Plugin::instance()->frontend->get_builder_content_for_display($templateElementorId);
} else {
    // Usa la logica di template tradizionale
    // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Template variable used by included files.
    $template = get_annfu_template();
    if (file_exists(ANNFU_PLUGIN_PATH . 'templates/' . $template . '/_annunci.php')) {
        include(ANNFU_PLUGIN_PATH . 'templates/' . $template . '/_annunci.php');
    } else {
        include(ANNFU_PLUGIN_PATH_TEMPLATE_DEFAULT . '_annunci.php');
    }
}
?>
