/*
Theme Name: Enfold Child
Description: A <a href='http://codex.wordpress.org/Child_Themes'>Child Theme</a> for the Enfold Wordpress Theme. If you plan to do a lot of file modifications we recommend to use this Theme instead of the original Theme. Updating will be much easier then.
Version: 1.0
Author: Kriesi
Author URI: http://www.kriesi.at
Template: enfold
*/



/*Add your own styles here:*/
<?php
/**
 * Aviso en header cuando hay una noticia nueva (categoría: noticias).
 * - Muestra un pill “Nueva noticia” con enlace a la última entrada.
 * - Se oculta cuando ya la has abierto (usa localStorage por navegador).
 */

add_action('wp_footer', function () {

    // Cambia el slug si tu categoría no se llama "noticias"
    $category_slug = 'noticias';

    $q = new WP_Query([
        'posts_per_page'      => 1,
        'post_status'         => 'publish',
        'ignore_sticky_posts' => true,
        'category_name'       => $category_slug,
    ]);

    if (!$q->have_posts()) {
        wp_reset_postdata();
        return;
    }

    $q->the_post();
    $post_id = get_the_ID();
    $title   = wp_strip_all_tags(get_the_title());
    $url     = get_permalink();

    wp_reset_postdata();

    // Contenedor “fuente” (no se ve). JS lo reubica dentro del header.
    echo '<div id="ud-latest-news-source"
              data-post-id="' . esc_attr($post_id) . '"
              data-post-url="' . esc_url($url) . '"
              data-post-title="' . esc_attr($title) . '"
              style="display:none!important"></div>';

    ?>
    <script>
    (function(){
      const src = document.getElementById('ud-latest-news-source');
      if(!src) return;

      const postId = src.dataset.postId;
      const postUrl = src.dataset.postUrl;
      const postTitle = src.dataset.postTitle;

      // Si ya he visto esta noticia en este navegador, no muestro el aviso
      const key = 'ud_last_seen_news_id';
      const seen = localStorage.getItem(key);
      if(seen && seen === postId) return;

      // Intento insertar el aviso dentro del header de Enfold
      const headerContainer =
        document.querySelector('#header_main .container') ||
        document.querySelector('#header_main') ||
        document.querySelector('#header');

      if(!headerContainer) return;

      // Aseguro posición relativa para anclar el pill
      headerContainer.classList.add('ud-has-news-badge');

      const a = document.createElement('a');
      a.className = 'ud-news-badge';
      a.href = postUrl;
      a.setAttribute('aria-label', 'Nueva noticia: ' + postTitle);
      a.innerHTML = `
        <span class="ud-dot" aria-hidden="true"></span>
        <span class="ud-news-badge__text"><strong>Nueva:</strong> ${postTitle}</span>
      `;

      // Al hacer click, marcamos como vista (aunque abra en otra pestaña)
      a.addEventListener('click', () => {
        try { localStorage.setItem(key, postId); } catch(e) {}
      });

      headerContainer.appendChild(a);
    })();
    </script>
    <?php
}, 50);