<?php

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

/*----------------------------------------
メールフォーム：設定メタボックス登録
----------------------------------------*/
function immf_add_contact_form_settings_meta_box() {

    add_meta_box(
        'im_mail_form_settings',
        __(
            'General Settings',
            'im-mail-form'
        ),
        'immf_settings_meta_box_callback',
        'im_mail_form',
        'side',
        'default'
    );

}

add_action(
    'add_meta_boxes',
    'immf_add_contact_form_settings_meta_box'
);


/*----------------------------------------
メールフォーム：設定メタボックス表示
----------------------------------------*/
function immf_settings_meta_box_callback($post) {

    wp_nonce_field(
        'immf_save_contact_form_settings',
        'im_mail_form_settings_nonce'
    );

    $form_id = get_post_meta(
        $post->ID,
        'immf_id',
        true
    );

    $use_confirm = get_post_meta(
        $post->ID,
        'immf_use_confirm',
        true
    );

    $confirm_button_label = get_post_meta(
        $post->ID,
        'immf_confirm_button_label',
        true
    );

    if (
        !metadata_exists(
            'post',
            $post->ID,
            'immf_confirm_button_label'
        )
    ) {
        $confirm_button_label = __(
            'Confirm',
            'im-mail-form'
        );
    }

    $back_button_label = get_post_meta(
        $post->ID,
        'immf_back_button_label',
        true
    );

    if (
        !metadata_exists(
            'post',
            $post->ID,
            'immf_back_button_label'
        )
    ) {
        $back_button_label = __(
            'Edit',
            'im-mail-form'
        );
    }

    $submit_button_label = get_post_meta(
        $post->ID,
        'immf_submit_button_label',
        true
    );

    if (
        !metadata_exists(
            'post',
            $post->ID,
            'immf_submit_button_label'
        )
    ) {
        $submit_button_label = __(
            'Submit',
            'im-mail-form'
        );
    }

    $required_label = get_post_meta(
        $post->ID,
        'immf_required_label',
        true
    );

    if (
        !metadata_exists(
            'post',
            $post->ID,
            'immf_required_label'
        )
    ) {
        $required_label = _x(
            '*',
            'Default required field label',
            'im-mail-form'
        );
    }

    $shortcode = $form_id
        ? '[im_mail_form id="' . $form_id . '"]'
        : '';

    ?>

    <p>
        <label for="im-mail-form-id">
            <strong>
                <?php esc_html_e(
                    'Form ID',
                    'im-mail-form'
                ); ?>
                <span
                    class="im-mail-form-admin__required"
                    aria-label="<?php esc_attr_e(
                        'Required',
                        'im-mail-form'
                    ); ?>"
                >*</span>
            </strong>
        </label>
    </p>

    <input
        type="text"
        id="im-mail-form-id"
        name="im_mail_form_id"
        value="<?php echo esc_attr($form_id); ?>"
        class="widefat"
        placeholder="<?php esc_attr_e(
            'e.g. contact',
            'im-mail-form'
        ); ?>"
        pattern="[a-z0-9_\-]+"
        title="<?php esc_attr_e(
            'Use lowercase letters, numbers, hyphens, and underscores.',
            'im-mail-form'
        ); ?>"
        required
    >

    <p class="description">
        <?php esc_html_e(
            'This ID is used to display the form with a shortcode.',
            'im-mail-form'
        ); ?><br>
        <?php esc_html_e(
            'Use lowercase letters, numbers, hyphens, and underscores.',
            'im-mail-form'
        ); ?>
    </p>

    <hr>

    <p>
        <strong>
            <?php esc_html_e(
                'Shortcode',
                'im-mail-form'
            ); ?>
        </strong>
    </p>

    <?php if ($shortcode) : ?>

        <input
            type="text"
            value="<?php echo esc_attr($shortcode); ?>"
            class="widefat"
            data-shortcode-copy
            readonly
        >
        <p
            class="description"
            data-shortcode-copy-message
            role="status"
            hidden
        >
            <?php esc_html_e(
                'Copied',
                'im-mail-form'
            ); ?>
        </p>

    <?php else : ?>

        <p class="description">
            <?php esc_html_e(
                'Enter a Form ID and save the form to display the shortcode.',
                'im-mail-form'
            ); ?>
        </p>

    <?php endif; ?>

    <hr>

    <p>
        <label>
            <input
                type="checkbox"
                name="im_mail_form_use_confirm"
                value="1"
                <?php checked($use_confirm, '1'); ?>
            >
            <?php esc_html_e(
                'Use a confirmation screen',
                'im-mail-form'
            ); ?>
        </label>
    </p>

    <p>
        <strong>
            <?php esc_html_e(
                'Button labels',
                'im-mail-form'
            ); ?>
        </strong>
    </p>

    <p>
        <label for="im-mail-form-confirm-button-label">
            <?php esc_html_e(
                'Confirmation button',
                'im-mail-form'
            ); ?>
            <span
                class="im-mail-form-admin__required"
                aria-label="<?php esc_attr_e(
                    'Required',
                    'im-mail-form'
                ); ?>"
            >*</span>
        </label>
    </p>

    <input
        type="text"
        id="im-mail-form-confirm-button-label"
        name="im_mail_form_confirm_button_label"
        value="<?php echo esc_attr($confirm_button_label); ?>"
        class="widefat"
        maxlength="50"
        required
    >

    <p>
        <label for="im-mail-form-back-button-label">
            <?php esc_html_e(
                'Back button',
                'im-mail-form'
            ); ?>
            <span
                class="im-mail-form-admin__required"
                aria-label="<?php esc_attr_e(
                    'Required',
                    'im-mail-form'
                ); ?>"
            >*</span>
        </label>
    </p>

    <input
        type="text"
        id="im-mail-form-back-button-label"
        name="im_mail_form_back_button_label"
        value="<?php echo esc_attr($back_button_label); ?>"
        class="widefat"
        maxlength="50"
        required
    >

    <p>
        <label for="im-mail-form-submit-button-label">
            <?php esc_html_e(
                'Submit button',
                'im-mail-form'
            ); ?>
            <span
                class="im-mail-form-admin__required"
                aria-label="<?php esc_attr_e(
                    'Required',
                    'im-mail-form'
                ); ?>"
            >*</span>
        </label>
    </p>

    <input
        type="text"
        id="im-mail-form-submit-button-label"
        name="im_mail_form_submit_button_label"
        value="<?php echo esc_attr($submit_button_label); ?>"
        class="widefat"
        maxlength="50"
        required
    >

    <hr>

    <p>
        <label for="im-mail-form-required-label">
            <strong>
                <?php esc_html_e(
                    'Required field label',
                    'im-mail-form'
                ); ?>
            </strong>
        </label>
    </p>

    <input
        type="text"
        id="im-mail-form-required-label"
        name="im_mail_form_required_label"
        value="<?php echo esc_attr($required_label); ?>"
        class="widefat"
        maxlength="20"
        placeholder="<?php esc_attr_e(
            'e.g. Required or *',
            'im-mail-form'
        ); ?>"
    >

    <p class="description">
        <?php esc_html_e(
            'This text is displayed next to required fields.',
            'im-mail-form'
        ); ?><br>
        <?php esc_html_e(
            'Leave this field blank to hide it.',
            'im-mail-form'
        ); ?>
    </p>

    <?php
}