<?php

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

/*----------------------------------------
管理画面：フォーム一覧カラム追加
----------------------------------------*/


/*----------------------------------------
フォーム一覧のカラム構成を変更
----------------------------------------*/
function immf_add_shortcode_column($columns) {

    // 日付列は不要なため削除
    unset($columns['date']);

    $new_columns = [];

    foreach ($columns as $key => $label) {

        // タイトル列の表示名を変更
        if ($key === 'title') {
            $new_columns[$key] = __(
                'Form Name',
                'im-mail-form'
            );
        } else {
            $new_columns[$key] = $label;
        }

        if ($key === 'title') {
            $new_columns['shortcode'] = __(
                'Shortcode',
                'im-mail-form'
            );
        }
    }

    return $new_columns;
}

add_filter(
    'manage_im_mail_form_posts_columns',
    'immf_add_shortcode_column'
);


/*----------------------------------------
ショートコード列の表示
----------------------------------------*/
function immf_render_shortcode_column(
    $column,
    $post_id
) {

    if ($column !== 'shortcode') {
        return;
    }

    $form_id = get_post_meta(
        $post_id,
        'immf_id',
        true
    );

    if (!$form_id) {
        echo '&#8212;';
        return;
    }

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

    $copy_label = __(
        'Copy shortcode',
        'im-mail-form'
    );

    $copied_label = __(
        'Copied!',
        'im-mail-form'
    );

    ?>

    <div class="im-mail-form-shortcode">

        <button
            type="button"
            class="im-mail-form-shortcode__copy"
            aria-label="<?php echo esc_attr($copy_label); ?>"
            data-shortcode-copy-button
            data-shortcode="<?php echo esc_attr($shortcode); ?>"
            data-copy-label="<?php echo esc_attr($copy_label); ?>"
            data-copied-label="<?php echo esc_attr($copied_label); ?>"
        >
            <svg
                class="im-mail-form-shortcode__icon im-mail-form-shortcode__icon--copy"
                viewBox="0 0 24 24"
                fill="none"
                stroke="currentColor"
                stroke-width="2"
                stroke-linecap="round"
                stroke-linejoin="round"
                aria-hidden="true"
                focusable="false"
            ><rect x="9" y="9" width="12" height="12" rx="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></svg>

            <svg
                class="im-mail-form-shortcode__icon im-mail-form-shortcode__icon--check"
                viewBox="0 0 24 24"
                fill="none"
                stroke="currentColor"
                stroke-width="2"
                stroke-linecap="round"
                stroke-linejoin="round"
                aria-hidden="true"
                focusable="false"
            ><polyline points="20 6 9 17 4 12"></polyline></svg>
        </button>

        <code
            class="im-mail-form-shortcode__code"
            data-shortcode-copy-target
        >
            <?php echo esc_html($shortcode); ?>
        </code>

        <span
            class="im-mail-form-shortcode__sr-status"
            data-shortcode-copy-status
            role="status"
        ></span>

    </div>

    <?php
}

add_action(
    'manage_im_mail_form_posts_custom_column',
    'immf_render_shortcode_column',
    10,
    2
);