<?php

if (!defined('ABSPATH')) {
    exit;
}

/*----------------------------------------
システム予約フィールド名を取得
----------------------------------------*/
function immf_reserved_field_names() {

    return [
        'inquiry_number',
        'form_title',
        'all_fields',
    ];

}

/*----------------------------------------
現在の言語用テンプレート設定名を取得
----------------------------------------*/
function immf_template_option_name() {

    $locale = sanitize_key(
        determine_locale()
    );

    return 'immf_templates_'
        . $locale;

}

/*----------------------------------------
従来のテンプレート設定を現在の言語へ移行
----------------------------------------*/
function immf_migrate_template_settings() {

    if (
        get_option(
            'immf_templates_locale_migrated',
            false
        )
    ) {
        return;
    }

    $legacy_templates = get_option(
        'immf_templates',
        null
    );

    if (is_array($legacy_templates)) {

        $option_name =
            immf_template_option_name();

        if (get_option($option_name, null) === null) {
            add_option(
                $option_name,
                $legacy_templates
            );
        }
    }

    update_option(
        'immf_templates_locale_migrated',
        true
    );

}

add_action(
    'admin_init',
    'immf_migrate_template_settings',
    5
);

/*----------------------------------------
テンプレート設定の標準値を取得
----------------------------------------*/
function immf_default_templates() {

    $admin_email = sanitize_email(
        get_option('admin_email')
    );

    $site_name = sanitize_text_field(
        get_bloginfo('name')
    );

    return [
        'admin_to'        => $admin_email,
        'admin_from_name' => $site_name,
        'admin_from'      => $admin_email,
        'admin_cc'        => '',
        'admin_bcc'       => '',
        'admin_subject'   => __(
            'Inquiry from {form_title}',
            'im-mail-form'
        ),
        'admin_body'      => __(
            "A new inquiry has been submitted through {form_title}.\n\nInquiry number: {inquiry_number}\n\n{all_fields}",
            'im-mail-form'
        ),

        'reply_from_name' => $site_name,
        'reply_from'      => $admin_email,
        'reply_bcc'       => '',
        'reply_subject'   => __(
            'Thank you for your inquiry',
            'im-mail-form'
        ),
        'reply_body'      => __(
            "Thank you for your inquiry.\n\nWe have received the following information.\n\nInquiry number: {inquiry_number}\n\n{all_fields}\n\nWe will review your inquiry and contact you shortly.",
            'im-mail-form'
        ),

        'complete_message' => __(
            "Thank you for your inquiry.\n\nWe will review your message and contact you shortly.\n\nPlease wait for our response.",
            'im-mail-form'
        ),
    ];

}

/*----------------------------------------
テンプレート設定を取得
----------------------------------------*/
function immf_get_templates() {

    $default_templates =
        immf_default_templates();

    $saved_templates = get_option(
        immf_template_option_name(),
        []
    );

    if (!is_array($saved_templates)) {
        return $default_templates;
    }

    $saved_templates = array_intersect_key(
        $saved_templates,
        $default_templates
    );

    return array_merge(
        $default_templates,
        $saved_templates
    );

}