<?php
/**
 * 汉字字典插件 - 自定义拼音和部首列表示例
 *
 * 将此文件的内容复制到您的主题 functions.php 文件中，
 * 或创建一个单独的插件来使用这些自定义功能。
 */

// 添加自定义拼音到列表中
function my_custom_character_pinyin_list($pinyin_list) {
    // 添加一些新的拼音
    $new_pinyin = array(
        // 添加一些不常用的拼音
        'm̄', 'n̄', 'ēr', 'ì', 'ǹg',
        
        // 添加带数字声调的拼音（用于输入法）
        'a1', 'a2', 'a3', 'a4', 'a5',
        'o1', 'o2', 'o3', 'o4', 'o5',
        
        // 添加方言拼音
        'a̋', 'ă', 'āi', 'a̍',
    );
    
    // 合并到现有列表中
    $pinyin_list = array_merge($pinyin_list, $new_pinyin);
    
    // 按字母顺序重新排序
    sort($pinyin_list);
    
    return $pinyin_list;
}
add_filter('chinese_character_pinyin_list', 'my_custom_character_pinyin_list');

// 添加自定义部首到列表中
function my_custom_character_radical_list($radical_list) {
    // 添加一些扩展部首
    $new_radicals = array(
        // 添加一些扩展部首
        '新部首一',
        '新部首二',
        '扩展部首甲',
        '扩展部首乙',
    );
    
    // 合并到现有列表中
    $radical_list = array_merge($radical_list, $new_radicals);
    
    return $radical_list;
}
add_filter('chinese_character_radical_list', 'my_custom_character_radical_list');

// 创建一个管理页面来动态添加拼音和部首
function add_custom_pinyin_radical_admin_menu() {
    add_options_page(
        '自定义拼音部首',
        '拼音部首设置',
        'manage_options',
        'custom-pinyin-radical',
        'custom_pinyin_radical_settings_page'
    );
}
add_action('admin_menu', 'add_custom_pinyin_radical_admin_menu');

// 设置页面内容
function custom_pinyin_radical_settings_page() {
    // 保存设置
    if (isset($_POST['save_custom_pinyin_radical'])) {
        // 保存自定义拼音
        if (isset($_POST['custom_pinyin'])) {
            $custom_pinyin = explode("\n", sanitize_textarea_field($_POST['custom_pinyin']));
            $custom_pinyin = array_map('trim', $custom_pinyin);
            $custom_pinyin = array_filter($custom_pinyin);
            update_option('custom_pinyin_list', $custom_pinyin);
        }
        
        // 保存自定义部首
        if (isset($_POST['custom_radicals'])) {
            $custom_radicals = explode("\n", sanitize_textarea_field($_POST['custom_radicals']));
            $custom_radicals = array_map('trim', $custom_radicals);
            $custom_radicals = array_filter($custom_radicals);
            update_option('custom_radicals_list', $custom_radicals);
        }
        
        echo '<div class="notice notice-success"><p>设置已保存！</p></div>';
    }
    
    // 获取当前设置
    $custom_pinyin = get_option('custom_pinyin_list', array());
    $custom_radicals = get_option('custom_radicals_list', array());
    
    ?>
    <div class="wrap">
        <h1>自定义拼音和部首</h1>
        <form method="post" action="">
            <?php wp_nonce_field('custom_pinyin_radical_settings'); ?>
            
            <table class="form-table">
                <tr>
                    <th scope="row">
                        <label for="custom_pinyin">自定义拼音</label>
                    </th>
                    <td>
                        <textarea 
                            id="custom_pinyin" 
                            name="custom_pinyin" 
                            rows="10" 
                            cols="50" 
                            placeholder="每行输入一个拼音，例如：&#10;xīn&#10;xín&#10;xǐn&#10;xìn"
                        ><?php echo esc_textarea(implode("\n", $custom_pinyin)); ?></textarea>
                        <p class="description">每行输入一个拼音，支持带声调符号的拼音。</p>
                    </td>
                </tr>
                
                <tr>
                    <th scope="row">
                        <label for="custom_radicals">自定义部首</label>
                    </th>
                    <td>
                        <textarea 
                            id="custom_radicals" 
                            name="custom_radicals" 
                            rows="10" 
                            cols="50" 
                            placeholder="每行输入一个部首，例如：&#10;新部首&#10;扩展部首"
                        ><?php echo esc_textarea(implode("\n", $custom_radicals)); ?></textarea>
                        <p class="description">每行输入一个部首。</p>
                    </td>
                </tr>
            </table>
            
            <?php submit_button('保存设置', 'primary', 'save_custom_pinyin_radical'); ?>
        </form>
    </div>
    
    <style>
        .form-table textarea {
            font-family: monospace;
            width: 100%;
            max-width: 500px;
        }
    </style>
    <?php
}

// 从选项加载自定义拼音
function load_custom_pinyin_from_options($pinyin_list) {
    $custom_pinyin = get_option('custom_pinyin_list', array());
    
    if (!empty($custom_pinyin)) {
        $pinyin_list = array_merge($pinyin_list, $custom_pinyin);
        sort($pinyin_list);
    }
    
    return $pinyin_list;
}
add_filter('chinese_character_pinyin_list', 'load_custom_pinyin_from_options', 20);

// 从选项加载自定义部首
function load_custom_radicals_from_options($radical_list) {
    $custom_radicals = get_option('custom_radicals_list', array());
    
    if (!empty($custom_radicals)) {
        $radical_list = array_merge($radical_list, $custom_radicals);
    }
    
    return $radical_list;
}
add_filter('chinese_character_radical_list', 'load_custom_radicals_from_options', 20);