@charset "UTF-8";
/*!
Theme Name: Cocoon Child
Description: Cocoon専用の子テーマ（リセット状態）
Author: わいひら
Template: cocoon-master
Version: 1.1.3
*/

/* ---- カスタムCSS停止中 ---- */
<?php
// ===========================
// スマホでメニューのサブ項目を展開するスクリプト
// ===========================
function enable_mobile_dropdown_menu() { ?>
  <script>
  document.addEventListener('DOMContentLoaded', function() {
    const parents = document.querySelectorAll('.menu-item-has-children > a');
    parents.forEach(link => {
      link.addEventListener('click', function(e) {
        const li = this.parentElement;
        li.classList.toggle('open');
        e.preventDefault();
      });
    });
  });
  </script>
  <style>
  .menu-item-has-children.open > ul {
    display: block !important;
  }
  </style>
<?php }
add_action('wp_footer', 'enable_mobile_dropdown_menu');


// ===========================
// 旧スラッグから新スラッグへリダイレクト
// ===========================
function redirect_old_visa_infomaetion() {
    if (strpos($_SERVER['REQUEST_URI'], 'visa_infomaetion') !== false) {
        $new_url = str_replace('visa_infomaetion', 'visa_information', $_SERVER['REQUEST_URI']);
        wp_redirect(site_url($new_url), 301);
        exit;
    }
}
add_action('template_redirect', 'redirect_old_visa_infomaetion');


// ===========================
// カスタム投稿タイプ 再登録（スラッグ修正済）
// ===========================
function register_visa_information_post_type() {

    $labels = array(
        'name'               => '在留資格情報',
        'singular_name'      => '在留資格情報',
        'add_new'            => '新規追加',
        'add_new_item'       => '在留資格情報を追加',
        'edit_item'          => '在留資格情報を編集',
        'new_item'           => '新しい在留資格情報',
        'all_items'          => '在留資格情報まとめ',
        'view_item'          => '在留資格情報を見る',
        'search_items'       => '在留資格情報を検索',
        'not_found'          => '記事がありません',
        'not_found_in_trash' => 'ゴミ箱にありません'
    );

    $args = array(
        'labels'        => $labels,
        'public'        => true,
        'has_archive'   => true,
        'menu_position' => 5,
        'supports'      => array('title', 'editor', 'thumbnail'),
        'rewrite'       => array('slug' => 'visa_information'),
    );

    register_post_type('visa_information', $args);
}
add_action('init', 'register_visa_information_post_type');

// ===========================
// Cocoon のアーカイブタイトルを変更
// ===========================
function change_visa_information_archive_title($title) {
    if (is_post_type_archive('visa_information')) {
        return '在留資格情報まとめ';
    }
    return $title;
}
add_filter('get_the_archive_title', 'change_visa_information_archive_title');