<?php
if (!function_exists('newsnation_custom_taxonomy_field')):

    // Add term page
    function newsnation_custom_taxonomy_field()
    {

        // this will add the custom meta field to the add new term page ?>

        <div class="form-field">

            <?php wp_nonce_field('newsnation_save_taxonomy_meta', 'newsnation_taxonomy_meta_nonce'); ?>

            <label><?php esc_html_e('Feature Image', 'newsnation'); ?></label>

            <div class="wpi-img-fields-wrap">
                <div class="attachment-media-view">
                    <div class="wpi-img-fields-wrap">
                        <div class="wpi-attachment-media-view">

                            <div class="wpi-attachment-child wpi-uploader">

                                <button type="button" class="wpi-img-upload-button">
                                    <span class="dashicons dashicons-upload wpi-icon wpi-icon-large"></span>
                                </button>

                                <input class="upload-id" name="wpi-term-featured-image" type="hidden" />

                            </div>

                            <div class="wpi-attachment-child wpi-thumbnail-image">

                                <button type="button" class="wpi-img-delete-button">
                                    <span class="dashicons dashicons-no-alt wpi-icon"></span>
                                </button>

                                <div class="wpi-img-container">
                                </div>

                            </div>

                        </div>
                    </div>
                </div>
            </div>

        </div>

        <div class="form-field">

            <label><?php esc_html_e('Category Color', 'newsnation'); ?></label>

            <input type="text" name="newsnation-cat-color" id="newsnation-cat-color" value="">

        </div>

        <?php
    }

endif;

add_action('category_add_form_fields', 'newsnation_custom_taxonomy_field', 10, 2);


if (!function_exists('newsnation_taxonomy_edit_meta_field')):

    // Edit term page
    function newsnation_taxonomy_edit_meta_field($term)
    {

        // retrieve the existing value(s) for this meta field. This returns an array
        $wpi_term_image = get_term_meta($term->term_id, 'wpi-term-featured-image', true);
        $wpi_term_color = get_term_meta($term->term_id, 'newsnation-cat-color', true); ?>

        <tr>

            <?php wp_nonce_field('newsnation_save_taxonomy_meta', 'newsnation_taxonomy_meta_nonce'); ?>

            <th scope="row" valign="top"><label><?php esc_html_e('Feature Image', 'newsnation'); ?></label></th>

            <td>

                <div class="wpi-img-fields-wrap">
                    <div class="attachment-media-view">
                        <div class="wpi-img-fields-wrap">
                            <div class="wpi-attachment-media-view">

                                <div class="wpi-attachment-child wpi-uploader">

                                    <button type="button" class="wpi-img-upload-button">
                                        <span class="dashicons dashicons-upload wpi-icon wpi-icon-large"></span>
                                    </button>

                                    <input class="upload-id" name="wpi-term-featured-image" type="hidden"
                                        value="<?php echo esc_url($wpi_term_image); ?>" />

                                </div>

                                <div class="wpi-attachment-child wpi-thumbnail-image">

                                    <button type="button"
                                        class="wpi-img-delete-button <?php if ($wpi_term_image) {
                                            echo 'wpi-img-show';
                                        } ?>">
                                        <span class="dashicons dashicons-no-alt wpi-icon"></span>
                                    </button>

                                    <div class="wpi-img-container">

                                        <?php if ($wpi_term_image) { ?>

                                            <img src="<?php echo esc_url($wpi_term_image); ?>" style="width:200px;height:auto;">

                                        <?php } ?>

                                    </div>

                                </div>

                            </div>
                        </div>
                    </div>
                </div>

            </td>

        </tr>

        <tr>

            <td><label><?php esc_html_e('Category Color', 'newsnation'); ?></label></td>

            <td><input type="text" name="newsnation-cat-color" id="newsnation-cat-color"
                    value="<?php echo esc_attr($wpi_term_color); ?>"></td>

        </tr>
        <?php
    }

endif;

add_action('category_edit_form_fields', 'newsnation_taxonomy_edit_meta_field', 10, 2);


if (!function_exists('newsnation_save_taxonomy_color_class_meta')):

    // Save extra taxonomy fields callback function.
    function newsnation_save_taxonomy_color_class_meta($term_id)
    {

        if (!isset($_POST['newsnation_taxonomy_meta_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['newsnation_taxonomy_meta_nonce'])), 'newsnation_save_taxonomy_meta')) {
            return;
        }

        if (isset($_POST['wpi-term-featured-image'])) {

            update_term_meta(
                $term_id,
                'wpi-term-featured-image',
                esc_url_raw(wp_unslash($_POST['wpi-term-featured-image']))
            );


        }

        if (isset($_POST['newsnation-cat-color'])) {

            update_term_meta(
                $term_id,
                'newsnation-cat-color',
                sanitize_hex_color(wp_unslash($_POST['newsnation-cat-color']))
            );


        }



    }

endif;

add_action('edited_category', 'newsnation_save_taxonomy_color_class_meta', 10, 2);
add_action('create_category', 'newsnation_save_taxonomy_color_class_meta', 10, 2);