Digital Issues
Sign in to your account
Sign in to your account
<?php
global $wpdb;

$current_year = isset($_GET['issue_year']) ? absint($_GET['issue_year']) : 0;

$years = $wpdb->get_col("
  SELECT DISTINCT YEAR(post_date)
  FROM {$wpdb->posts}
  WHERE post_type = 'issue'
    AND post_status = 'publish'
  ORDER BY YEAR(post_date) DESC
");

$base_url = (is_singular() || is_page())
  ? get_permalink()
  : home_url( add_query_arg([], $GLOBALS['wp']->request ) );

$base_url = trailingslashit($base_url);

$all_years_url = remove_query_arg('issue_year', $base_url);
?>

<div class="year-filter" data-issue-year-filter data-query-id="687796">
  <a class="year-btn <?php echo $current_year === 0 ? 'is-active' : ''; ?>"
     href="<?php echo esc_url($all_years_url); ?>"
     data-year="0">All Years</a>

  <?php foreach ($years as $year): $year = (int) $year; ?>
    <a class="year-btn <?php echo $current_year === $year ? 'is-active' : ''; ?>"
       href="<?php echo esc_url(add_query_arg('issue_year', (int) $year, $base_url)); ?>"
       data-year="<?php echo esc_attr($year); ?>">
      <?php echo esc_html($year); ?>
    </a>
  <?php endforeach; ?>
</div>
.year-filter{
  display:flex;
  gap:16px;
  justify-content:center;
  align-items:center;
  flex-wrap:wrap;
}

.year-btn{
  display:inline-flex;
  align-items:center;
  justify-content:center;
  padding: var(--space-xs) var(--space-m);
  border:1px solid #0b2b3a;
  border-radius:8px;
  text-decoration:none;
  font-size: var(--text-s);
  font-weight:600;
  background:#fff;
  color:#0b2b3a;
}

.year-btn.is-active{
  background:#0b2b3a;
  color:#fff;
}

.year-filter.is-loading{
  opacity:.75;
  pointer-events:none;
}

/* Skeleton on the REAL card structure */
ul.catalog_digital_issues__grid.is-loading{
  pointer-events:none;
}

ul.catalog_digital_issues__grid.is-loading .card-catalog_digital_issues__img{
  position:relative;
  overflow:hidden;
  border-radius: var(--radius-m);
  background: rgba(0,0,0,.06);
}

ul.catalog_digital_issues__grid.is-loading .card-catalog_digital_issues__img img{
  opacity:0 !important;
}

ul.catalog_digital_issues__grid.is-loading .card-catalog_digital_issues__img::after{
  content:"";
  position:absolute;
  inset:0;
  background: linear-gradient(90deg, rgba(0,0,0,.04), rgba(0,0,0,.09), rgba(0,0,0,.04));
  background-size: 200% 100%;
  animation: issueShimmer 1.1s linear infinite;
  border-radius: var(--radius-m);
}

ul.catalog_digital_issues__grid.is-loading .card-catalog_digital_issues__heading{
  position:relative;
  color: transparent !important;
}

ul.catalog_digital_issues__grid.is-loading .card-catalog_digital_issues__heading a{
  color: transparent !important;
}

ul.catalog_digital_issues__grid.is-loading .card-catalog_digital_issues__heading::before{
  content:"";
  display:block;
  height: 1em;
  width: 70%;
  border-radius: 6px;
  background: rgba(0,0,0,.08);
}

ul.catalog_digital_issues__grid.is-loading .card-catalog_digital_issues__divider{
  opacity:0;
}

@keyframes issueShimmer{
  0%{background-position:200% 0}
  100%{background-position:-200% 0}
}
document.addEventListener('DOMContentLoaded', function () {
  const filter = document.querySelector('[data-issue-year-filter][data-query-id="687796"]');
  if (!filter) return;

  const QUERY_ID = '687796';
  const GRID_SELECTOR = 'ul.catalog_digital_issues__grid';
  let isLoading = false;

  function setActive(year) {
    filter.querySelectorAll('.year-btn').forEach(a => {
      a.classList.toggle('is-active', String(a.dataset.year) === String(year));
    });
  }

  function getGrid(doc) {
    return doc.querySelector(GRID_SELECTOR);
  }

  // Fix "featured image missing" by converting Bricks lazy attrs to real attrs
  function hydrateBricksLazy(root) {
    if (!root) return;

    // Remove lazy-hidden class from everything inside the swapped grid
    root.querySelectorAll('.bricks-lazy-hidden').forEach(el => el.classList.remove('bricks-lazy-hidden'));

    // Promote data-src/data-srcset/data-sizes to src/srcset/sizes
    root.querySelectorAll('img[data-src], img[data-srcset], img[data-sizes]').forEach(img => {
      const ds = img.getAttribute('data-src');
      const dss = img.getAttribute('data-srcset');
      const dsz = img.getAttribute('data-sizes');

      if (ds) img.setAttribute('src', ds);
      if (dss) img.setAttribute('srcset', dss);
      if (dsz) img.setAttribute('sizes', dsz);

      img.removeAttribute('data-src');
      img.removeAttribute('data-srcset');
      img.removeAttribute('data-sizes');
    });
  }

  function setLoading(on, grid) {
    filter.classList.toggle('is-loading', on);
    if (grid) grid.classList.toggle('is-loading', on);
  }

  async function loadYear(url, year) {
    if (isLoading) return;
    isLoading = true;

    const currentGrid = getGrid(document);
    if (!currentGrid) {
      window.location.href = url;
      return;
    }

    setLoading(true, currentGrid);

    try {
      const res = await fetch(url, {
        method: 'GET',
        headers: { 'X-Requested-With': 'XMLHttpRequest' },
        cache: 'no-store',
        credentials: 'same-origin'
      });

      if (!res.ok) throw new Error('Fetch failed');

      const html = await res.text();
      const doc = new DOMParser().parseFromString(html, 'text/html');
      const nextGrid = getGrid(doc);

      if (!nextGrid) throw new Error('Grid not found');

      const importedGrid = document.importNode(nextGrid, true);
      currentGrid.replaceWith(importedGrid);

      hydrateBricksLazy(importedGrid);

      setActive(year);
      window.history.pushState({ issue_year: year }, '', url);

    } catch (e) {
      window.location.href = url;
      return;
    } finally {
      filter.classList.remove('is-loading');
      isLoading = false;
    }
  }

  // Capture click so navigation never happens
  document.addEventListener('click', function (e) {
    const a = e.target.closest('[data-issue-year-filter] .year-btn');
    if (!a) return;

    if (e.metaKey || e.ctrlKey || e.shiftKey || e.altKey || a.target === '_blank') return;

    e.preventDefault();
    e.stopPropagation();
    e.stopImmediatePropagation();

    loadYear(a.href, a.dataset.year || '0');
  }, true);

  window.addEventListener('popstate', () => window.location.reload());
});