/*
 Theme Name:   GeneratePress Child
 Theme URI:    https://generatepress.com
 Description:  Default GeneratePress child theme
 Author:       Tom Usborne
 Author URI:   https://tomusborne.com
 Template:     generatepress
 Version:      0.1
*/
/* checkout email */
add_filter( 'woocommerce_billing_fields', 'bbloomer_move_checkout_email_field' );
function bbloomer_move_checkout_email_field( $address_fields ) {
    $address_fields['billing_email']['priority'] = 1;
    return $address_fields;
}
// Remove the product description Title
add_filter( 'woocommerce_product_description_heading', '__return_null' );

/* Change No. of Thumbnails per Row @ Product Gallery | WooCommerce */
add_filter( 'woocommerce_single_product_image_gallery_classes', 'bbloomer_5_columns_product_gallery' );
function bbloomer_5_columns_product_gallery( $wrapper_classes ) {
$columns = 8; // 默认是4，修改自己想要的数量即可.
$wrapper_classes[2] = 'woocommerce-product-gallery--columns-' . absint( $columns );
return $wrapper_classes;
}
/* catelog unfold */
add_action('init', function () {
    remove_action('woocommerce_archive_description', 'woocommerce_taxonomy_archive_description', 10);
    add_action('woocommerce_archive_description', function () {
        if (!is_product_category()) return;
        $term = get_queried_object();
        if (!$term || empty($term->taxonomy)) return;
        $desc = term_description($term, $term->taxonomy);
        if (!$desc) return;

        echo '
        <div class="wc-catdesc-wrapper" data-state="collapsed">
          <div class="wc-catdesc-clip" id="wc-catdesc-content">
            ' . $desc . '
          </div>
          <button class="wc-catdesc-toggle" type="button" aria-expanded="false" aria-controls="wc-catdesc-content">More</button>
        </div>';
    }, 10);
});

add_action('wp_enqueue_scripts', function () {
    if (!is_product_category()) return;
    $css = <<<CSS
.wc-catdesc-wrapper { position: relative; }
.wc-catdesc-clip {
  max-height: 100px;
  overflow: hidden;
  position: relative;
  transition: max-height .3s ease;
}
.wc-catdesc-wrapper[data-state="collapsed"] .wc-catdesc-clip::after {
  content: "";
  position: absolute; left: 0; right: 0; bottom: 0;
  height: 3.5rem;
  pointer-events: none;
  background: linear-gradient(to bottom, rgba(255,255,255,0), rgba(255,255,255,.95));
}
.wc-catdesc-wrapper[data-state="expanded"] .wc-catdesc-clip { max-height: 2000px; }
.wc-catdesc-wrapper[data-state="expanded"] .wc-catdesc-clip::after { display: none; }
.wc-catdesc-toggle {
  display: inline-block;
  margin:10px 0 10px;  
  padding: 0;    
  background: #fff;   
  color: #011605;    
  font-size: 16px;
  line-height: 1.4;
  cursor: pointer;
  text-decoration: underline;
}
.wc-catdesc-toggle:hover,
.wc-catdesc-toggle:focus {
  background: #fff;
  color: #011605;
  text-decoration: underline;
  outline: none; /* 去掉聚焦高亮 */
}
.no-js .wc-catdesc-clip { max-height: none !important; }
.no-js .wc-catdesc-toggle { display: none !important; }
.wc-catdesc-wrapper .wc-catdesc-clip > :first-child { margin-top: 0; }
.wc-catdesc-wrapper .wc-catdesc-clip > :last-child { margin-bottom: 0; }
CSS;
    wp_register_style('wc-catdesc-toggle-inline', false);
    wp_enqueue_style('wc-catdesc-toggle-inline');
    wp_add_inline_style('wc-catdesc-toggle-inline', $css);
}, 20);

add_action('wp_enqueue_scripts', function () {
    if (!is_product_category()) return;
    $js = <<<JS
(function(){
  try {
    var html = document.documentElement;
    html.classList.remove('no-js');
    html.classList.add('js');
  } catch(e){}
  function initCatDescToggle(){
    var wrapper = document.querySelector('.wc-catdesc-wrapper');
    var clip    = document.getElementById('wc-catdesc-content');
    var btn     = document.querySelector('.wc-catdesc-toggle');
    if(!wrapper || !clip || !btn) return;
    var collapsedMax = parseInt(getComputedStyle(clip).maxHeight, 10) || 140;
    var realHeight = clip.scrollHeight;
    if(realHeight <= collapsedMax + 4){
      btn.style.display = 'none';
      wrapper.setAttribute('data-state', 'expanded');
      return;
    }
    function toggle(){
      var expanded = wrapper.getAttribute('data-state') === 'expanded';
      if(expanded){
        wrapper.setAttribute('data-state', 'collapsed');
        btn.setAttribute('aria-expanded', 'false');
        btn.textContent = 'More';
      }else{
        wrapper.setAttribute('data-state', 'expanded');
        btn.setAttribute('aria-expanded', 'true');
        btn.textContent = 'Less';
      }
    }
    btn.addEventListener('click', toggle, false);
  }
  if(document.readyState === 'loading'){
    document.addEventListener('DOMContentLoaded', initCatDescToggle);
  }else{
    initCatDescToggle();
  }
})();
JS;
    add_action('wp_head', function () {
        echo "<script>(function(){try{document.documentElement.classList.add('no-js');}catch(e){}})();</script>\n";
    }, 0);
    wp_register_script('wc-catdesc-toggle-inline', '', [], false, true);
    wp_enqueue_script('wc-catdesc-toggle-inline');
    wp_add_inline_script('wc-catdesc-toggle-inline', $js);
}, 21);
