<?php
/**
 * Includes: Terms

 */
if (!defined('ABSPATH')) {
    die('Sorry, you cannot access this page.');
}
function chama_term_add_form_fields()
{
    $tier_list = chama_get_tier_levels();
    wp_nonce_field('chama_term_nonce', 'chama_tier_level_nonce');
    ?>
		<div class="form-field">
			<label for="chama_tier_level">Tier Level</label>
			<select id="chama_tier_level" name="chama_tier_level">
				<?php foreach ($tier_list as $value => $label): ?>
					<option value="<?php echo esc_attr($value) ?>"><?php echo esc_html($label) ?> </option>
					<?php endforeach;?>
			</select>
			<p><em>Choose the tier level you would like this content protected.</em></p>
		</div>
	<?php
}
add_action('category_add_form_fields', 'chama_term_add_form_fields');
add_action('post_tag_add_form_fields', 'chama_term_add_form_fields');
add_action('admin_init', 'chama_taxonomies_add_form_fields');
function chama_taxonomies_add_form_fields()
{
    $taxonomies = get_taxonomies(array('public' => true));
    foreach ($taxonomies as $taxonomy) {
        add_action($taxonomy . '_add_form_fields', 'chama_term_add_form_fields');
    }
}
function chama_edit_form_fields($term, $taxonomy)
{

    // get meta data value
    $current_tier = get_term_meta($term->term_id, 'chama_tier_level', true);
    $tier_list = chama_get_tier_levels();
    wp_nonce_field('chama_term_nonce', 'chama_tier_level_nonce');
    ?><tr class="form-field">
			<th><label for="chama_tier_level">Tier Level</label></th>
			<td>
			<select id="chama_tier_level" name="chama_tier_level">
				<?php foreach ($tier_list as $value => $label): ?>
					<option value="<?php echo esc_attr($value) ?>" <?php echo selected($value, $current_tier); ?>><?php echo esc_html($label) ?> </option>
					<?php endforeach;?>
			</select>
			<p><em>Choose the tier level you would like this content protected.</em></p>
			</td>
		</tr>
		<?php
}

add_action('category_edit_form_fields', 'chama_edit_form_fields', 10, 2);
add_action('post_tag_edit_form_fields', 'chama_edit_form_fields', 10, 2);
add_action('admin_init', 'chama_taxonomies_edit_form_fields');
function chama_taxonomies_edit_form_fields()
{
    $taxonomies = get_taxonomies(array('public' => true));
    foreach ($taxonomies as $taxonomy) {
        add_action($taxonomy . '_edit_form_fields', 'chama_edit_form_fields', 10, 2);
    }
}
add_action('saved_category', 'chama_term_saved');
add_action('saved_post_tag', 'chama_term_saved');
add_action('admin_init', 'chama_saved_taxonomies');
function chama_term_saved($term_id)
{
    // Check if our nonce is set.
    if (!isset($_POST['chama_tier_level_nonce'])) {
        return;
    }

    // Verify that the nonce is valid.
    if (!wp_verify_nonce(sanitize_key($_POST['chama_tier_level_nonce']), 'chama_term_nonce')) {
        return;
    }

    if (isset($_POST['chama_tier_level'])) {
        update_term_meta(
            $term_id,
            'chama_tier_level',
            sanitize_textarea_field(wp_unslash($_POST['chama_tier_level']))
        );
    }

}
function chama_saved_taxonomies()
{
    $taxonomies = get_taxonomies(array('public' => true));
    foreach ($taxonomies as $taxonomy) {
        add_action('saved_' . $taxonomy, 'chama_term_saved');
    }
}
/**
 * Add tier level taxonomy
 */
function chama_term_add_tier_level_column(array $columns)
{
    $columns['tier_level'] = __('Tier Level', 'chama');
    return $columns;
}

// Filter: manage_edit-{$taxonomy}_columns
add_filter('manage_edit-category_columns', 'chama_term_add_tier_level_column', 10, 1);
add_filter('manage_edit-post_tag_columns', 'chama_term_add_tier_level_column', 10, 1);
add_action('admin_init', 'chama_taxonomies_add_tier_level_column');
function chama_taxonomies_add_tier_level_column()
{
    $taxonomies = get_taxonomies(array('public' => true));
    foreach ($taxonomies as $taxonomy) {
        add_filter('manage_edit-' . $taxonomy . '_columns', 'chama_term_add_tier_level_column', 10, 1);
    }
}
/**
 * Show content of term meta.
 */
function chama_term_populate_tier_level_column(string $content, string $column_name, int $term_id)
{
    // Skip if this is not our column
    if ($column_name != 'tier_level') {
        return;
    }

    $tier_level = get_term_meta($term_id, 'chama_tier_level', true);

    // Skip if term or field does not exist
    if (!$tier_level) {
        return;
    }

    echo esc_html(chama_get_tier_level_from_amount($tier_level));
}

// Action: manage_{$taxonomy}_custom_column
add_action('manage_category_custom_column', 'chama_term_populate_tier_level_column', 10, 3);
add_action('manage_post_tag_custom_column', 'chama_term_populate_tier_level_column', 10, 3);
add_action('admin_init', 'chama_taxonomies_populate_tier_level_column');
function chama_taxonomies_populate_tier_level_column()
{
    $taxonomies = get_taxonomies(array('public' => true));
    foreach ($taxonomies as $taxonomy) {
        add_filter('manage_' . $taxonomy . '_custom_column', 'chama_term_populate_tier_level_column', 10, 3);
    }
}
