/*
Theme Name: Qi Child
Theme URI: https://qodeinteractive.com/qi-theme
Template: qi
Author: Qode Interactive
Author URI: https://qodeinteractive.com
Description: Qi theme is the latest free offering developed by Qode Interactive, winners of multiple awards and accolades in the fields of web design and development and one of bestselling WordPress theme creators. We approached Qi the same way we do when we develop premium themes - this means that the theme features comprehensive modules for your portfolio, shop, blog, a beautiful selection of contemporary premade inner page templates great for a wide array of purposes, and all the options and functionalities you may ever need to set up a professional website the easy way. Qi theme features 100 beautifully designed starter sites for all types of modern websites. Alongside stunningly designed websites for all types of businesses including spa, beauty salon, medical, SaaS, marketing, gym, and dentist websites, just to name a few, Qi also has a huge collection of sites great for personal uses. Do you wish to create a new CV or a resume, or perhaps you are a restaurant owner, a videographer or an SEO expert? Or maybe you’re just looking for a captivating yet simple way to announce your wedding? Whatever it might be, Qi has you covered! Top all this off with the authentic design style that Qode Interactive collective is known for, and you get a true creative powerhouse that’s fast, reliable and comes with virtually limitless customization options!
Tags: food-and-drink,e-commerce,portfolio,grid-layout,one-column,two-columns,three-columns,four-columns,left-sidebar,right-sidebar,featured-images,custom-logo,custom-menu,footer-widgets,post-formats,full-width-template,sticky-post,threaded-comments,translation-ready,editor-style
Version: 1.3.1756938615
Updated: 2025-09-04 00:30:15

*/

<?php
/**
 * SPACS – Currency Switcher (menu-friendly)
 * - Symbol trigger + dropdown (EUR, PLN, CZK)
 * - Posts 'currency' to current URL (jak Twój obecny switcher)
 * - Shortcode: [spacs_currency]
 * - Menu: dodaj pozycję z klasą CSS 'spacs-currency-slot' (URL może być #)
 */

if ( ! function_exists('spacs_currency_markup') ) {
  function spacs_currency_markup() {
    // domyślna waluta do wyświetlenia (spróbuj z cookie 'currency', inaczej EUR)
    $current = isset($_COOKIE['currency']) ? strtoupper(sanitize_text_field($_COOKIE['currency'])) : 'EUR';
    if ( ! in_array($current, ['EUR','PLN','CZK'], true) ) { $current = 'EUR'; }

    // mapy symboli
    $symbols = ['EUR'=>'€','PLN'=>'zł','CZK'=>'Kč'];
    $sym = $symbols[$current];

    ob_start(); ?>
    <div class="spcs-cs" data-current="<?php echo esc_attr($current); ?>">
      <!-- ukryty natywny select jak w Twoim widżecie – plugin multicurrency go „rozumie” -->
      <form class="spcs-cs__form" method="post" action="">
        <select name="currency" class="spcs-cs__select" aria-label="Currency" style="display:none" onchange="this.form.submit()">
          <option value="EUR" <?php selected($current,'EUR'); ?> data-symbol="€">€ EUR</option>
          <option value="PLN" <?php selected($current,'PLN'); ?> data-symbol="zł">zł PLN</option>
          <option value="CZK" <?php selected($current,'CZK'); ?> data-symbol="Kč">Kč CZK</option>
        </select>
      </form>

      <!-- przycisk z samym symbolem -->
      <button class="spcs-cs__btn" type="button" aria-haspopup="listbox" aria-expanded="false" aria-label="Change currency">
        <span class="spcs-cs__symbol"><?php echo esc_html($sym); ?></span>
        <svg class="spcs-cs__chev" width="10" height="10" viewBox="0 0 10 10" aria-hidden="true"><path d="M1 3l4 4 4-4" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round"/></svg>
      </button>

      <!-- dropdown z symbolami -->
      <ul class="spcs-cs__list" role="listbox" hidden>
        <li role="option" data-value="EUR" data-symbol="€">€</li>
        <li role="option" data-value="PLN" data-symbol="zł">zł</li>
        <li role="option" data-value="CZK" data-symbol="Kč">Kč</li>
      </ul>
    </div>
    <?php
    return ob_get_clean();
  }
}

/* shortcode do łatwego wstawienia */
add_shortcode('spacs_currency', function(){ return spacs_currency_markup(); });

/* Podmiana pozycji menu – jeżeli ma klasę CSS 'spacs-currency-slot' */
add_filter('walker_nav_menu_start_el', function( $output, $item, $depth, $args ){
  $classes = is_array($item->classes) ? $item->classes : [];
  if ( in_array('spacs-currency-slot', $classes, true) ) {
    return spacs_currency_markup();
  }
  return $output;
}, 10, 4);

/* Minimalne style (kompaktowe pod menu) */
add_action('wp_head', function(){ ?>
  <style>
    .spcs-cs{ position:relative; display:inline-flex; align-items:center; }
    .spcs-cs__btn{
      display:inline-flex; align-items:center; gap:6px;
      padding:6px 8px; border:1px solid transparent; background:transparent; cursor:pointer;
      line-height:1; font:inherit; color:inherit;
    }
    .spcs-cs__btn:focus-visible{ outline:2px solid var(--accent, #02BCFF); outline-offset:2px; }
    .spcs-cs__list{
      position:absolute; top:100%; right:0; z-index:9999;
      list-style:none; margin:6px 0 0; padding:6px;
      background:#fff; border:1px solid #e5e5e5; border-radius:8px; box-shadow:0 6px 20px rgba(0,0,0,.08);
      min-width:80px;
    }
    .spcs-cs__list li{
      padding:6px 8px; border-radius:6px; cursor:pointer; white-space:nowrap;
    }
    .spcs-cs__list li:hover{ background:#f3f3f3; }
    /* dopasowanie wysokości do wiersza menu */
    .menu-item .spcs-cs__btn{ padding:4px 6px; }
  </style>
<?php });

/* Skrypt: toggle + wybór waluty → submit jak w oryginalnym widgetcie */
add_action('wp_footer', function(){ ?>
  <script>
  (function(){
    function init(el){
      if (el.__inited) return; el.__inited = true;
      var btn = el.querySelector('.spcs-cs__btn');
      var list = el.querySelector('.spcs-cs__list');
      var select = el.querySelector('.spcs-cs__select');
      var sym = el.querySelector('.spcs-cs__symbol');

      function close(){ list.hidden = true; btn.setAttribute('aria-expanded','false'); }
      function open(){ list.hidden = false; btn.setAttribute('aria-expanded','true'); }

      btn.addEventListener('click', function(e){
        e.stopPropagation();
        list.hidden ? open() : close();
      });
      document.addEventListener('click', function(){ close(); });

      list.addEventListener('click', function(e){
        var li = e.target.closest('li[role="option"]');
        if (!li) return;
        var val = li.getAttribute('data-value');
        var symbol = li.getAttribute('data-symbol') || li.textContent.trim();
        if (select) {
          select.value = val;
          sym.textContent = symbol;
          // ustaw cookie 'currency' dla spójności wyświetlania
          try { document.cookie = "currency="+val+"; path=/; max-age="+(60*60*24*365); } catch(e){}
          // submit – tak działa Twój obecny widget
          select.dispatchEvent(new Event('change', {bubbles:true}));
        }
        close();
      }, false);
    }

    document.querySelectorAll('.spcs-cs').forEach(init);

    // gdy menu renderuje się dynamicznie (Elementor/Qode), obserwuj DOM:
    var mo = new MutationObserver(function(){
      document.querySelectorAll('.spcs-cs').forEach(init);
    });
    mo.observe(document.body, {subtree:true, childList:true});
  })();
  </script>
<?php });
