@charset "UTF-8";
/*
    Template: swell
    Theme Name: SWELL CHILD
    Theme URI: https://swell-theme.com/
    Description: SWELLの子テーマ
    Version: 1.0.0
    Author: LOOS WEB STUDIO
    Author URI: https://loos-web-studio.com/

    License: GNU General Public License
    License URI: http://www.gnu.org/licenses/gpl.html
*/
//更新日時順に記事を並べ替え
 function my_sort_order_by_modifired ( $query ) {
      if ( !is_admin() && $query->is_main_query() ) {
          $query->set( 'orderby', 'modified' );
      }
 } 
 add_action( 'pre_get_posts', 'my_sort_order_by_modifired' );
/**
 * SWELLのFAQブロックをアコーディオン化する関数
 * アクセシビリティに配慮した実装
 */
function motoki_faq_accordion() {
    echo <<< EOM
    <script>
    document.addEventListener('DOMContentLoaded', function() {
        const faqItems = document.querySelectorAll('.swell-block-faq__item');
        
        // 各FAQ項目にアクセシビリティ対応を実装
        faqItems.forEach(function(faqItem, index) {
            const faqQuestion = faqItem.querySelector('.faq_q');
            const faqAnswer = faqItem.querySelector('.faq_a');
            
            // IDを割り振る
            const questionId = 'faq-question-' + index;
            const answerId = 'faq-answer-' + index;
            
            // 質問要素にWAI-ARIA属性を設定
            faqQuestion.setAttribute('id', questionId);
            faqQuestion.setAttribute('aria-expanded', 'false');
            faqQuestion.setAttribute('aria-controls', answerId);
            faqQuestion.setAttribute('role', 'button');
            faqQuestion.setAttribute('tabindex', '0');
            
            // 回答要素にWAI-ARIA属性を設定
            faqAnswer.setAttribute('id', answerId);
            faqAnswer.setAttribute('role', 'region');
            faqAnswer.setAttribute('aria-labelledby', questionId);
            faqAnswer.setAttribute('hidden', '');
            
            // 初期状態で開いている場合の処理
            if (faqItem.classList.contains('open')) {
                faqAnswer.classList.add('open');
                faqQuestion.setAttribute('aria-expanded', 'true');
                faqAnswer.removeAttribute('hidden');
            }
            
            // クリックイベントの設定
            faqQuestion.addEventListener('click', function() {
                toggleFaq(faqItem, faqQuestion, faqAnswer);
            });
            
            // キーボードイベントの設定
            faqQuestion.addEventListener('keydown', function(event) {
                if (event.key === 'Enter' || event.key === ' ') {
                    event.preventDefault();
                    toggleFaq(faqItem, faqQuestion, faqAnswer);
                }
            });
        });
        
        // FAQ開閉の共通関数
        function toggleFaq(item, question, answer) {
            const isExpanded = question.getAttribute('aria-expanded') === 'true';
            
            // 開閉状態を切り替え
            question.setAttribute('aria-expanded', !isExpanded);
            
            if (isExpanded) {
                answer.classList.remove('open');
                item.classList.remove('open');
                setTimeout(function() {
                    answer.setAttribute('hidden', '');
                }, 500);
            } else {
                answer.removeAttribute('hidden');
                setTimeout(function() {
                    answer.classList.add('open');
                    item.classList.add('open');
                }, 10);
            }
        }
    });
    </script>
EOM;
}
add_action('wp_footer', 'motoki_faq_accordion');
