<?php


// Create custom taxonomy for taxes.
function wde_create_taxes() {
  $options = WDFDb::get_options();
  $labels = array(
		'name'                            => _x( 'Taxes', 'post type general name', 'wde' ),
		'singular_name'                   => _x( 'Tax', 'post type singular name', 'wde' ),
		'menu_name'                       => _x( 'Taxes', 'admin menu', 'wde' ),
		'all_items'                       => _x( 'All Taxes', 'admin menu', 'wde' ),
		'edit_item'                       => __( 'Edit Tax', 'wde' ),
		'view_item'                       => __( 'View Tax', 'wde' ),
		'update_item'                     => __( 'Update Tax', 'wde' ),
		'add_new_item'                    => __( 'Add New Tax', 'wde' ),
		'new_item_name'                   => __( 'New Tax Name', 'wde' ),
		'parent_item  '                   => __( 'Parent Tax', 'wde' ),
		'parent_item_colon'               => __( 'Parent Tax:', 'wde' ),
		'search_items'                    => __( 'Search Taxes', 'wde' ),
		'popular_items'                   => __( 'Popular Taxes', 'wde' ),
		'separate_items_with_commas'      => __( 'Separate Taxes with commas', 'wde' ),
		'add_or_remove_items'             => __( 'Add or remove Taxes', 'wde' ),
		'choose_from_most_used'           => __( 'Choose from most used Taxes', 'wde' ),
		'not_found'                       => __( 'No Taxes found.', 'wde' )
	);
	$args = array(
		'labels'                 => $labels,
		'public'                 => TRUE,
		'show_ui'                => TRUE,
		'show_in_menu'           => FALSE,
		'show_in_nav_menus'      => FALSE,
		'show_tagcloud'          => FALSE,
		'show_in_quick_edit'     => FALSE,
		'meta_box_cb'            => NULL,
		'show_admin_column'      => TRUE,
		'hierarchical'           => FALSE,
		'update_count_callback'  => NULL,
		'query_var'              => isset($options->option_tax_base) ? $options->option_tax_base : TRUE,
		'rewrite'                => isset($options->option_tax_base) ? array('slug' => $options->option_tax_base) : TRUE,
		'sort'                   => TRUE,
  );
  register_taxonomy('wde_taxes', 'wde_products', $args);
}
add_action('init', 'wde_create_taxes');

// Remove taxes meta box from siderbar.
function wde_taxes_remove_meta() {
	remove_meta_box('tagsdiv-wde_taxes', 'wde_products', 'side');
}
add_action('admin_menu' , 'wde_taxes_remove_meta');

// Add extra taxonomy fields to the main column on the custom post edit screens.
function wde_add_taxes_meta_fields() {
  wd_ecommerce();
}
add_action('wde_taxes_add_form_fields', 'wde_add_taxes_meta_fields', 10, 2);

// Add extra taxonomy fields to the term edit page.
function wde_taxes_edit_meta_field($term) {
	wd_ecommerce($term->term_id);
}
add_action('wde_taxes_edit_form_fields', 'wde_taxes_edit_meta_field', 10, 2);

// Save extra taxonomy fields.
function save_wde_taxes_custom_meta($term_id) {
  $term_meta = get_option("wde_taxes_" . $term_id);
	if (isset($_POST['rate']) && $_POST['rate'] != '') {
    $term_meta['rate'] = sanitize_text_field($_POST['rate']);
  }
  // Save the option array.
  update_option("wde_taxes_" . $term_id, $term_meta);
}
add_action('edited_wde_taxes', 'save_wde_taxes_custom_meta', 10, 2);
add_action('create_wde_taxes', 'save_wde_taxes_custom_meta', 10, 2);

// Change custom taxonomy columns to show in administrator table.
function wde_taxes_columns($columns) {
  unset($columns['description']);
  unset($columns['slug']);
  unset($columns['posts']);
  return array_merge($columns, 
          array('rate' => __('Rate', 'wde')));
}
add_filter('manage_edit-wde_taxes_columns', 'wde_taxes_columns');

// Change custom taxonomy column content in administrator table.
function wde_taxes_column($columns, $column, $term_id) {
  if ($column == 'rate') {
    $term_meta = get_option("wde_taxes_" . $term_id);
    $columns .= $term_meta['rate'] . '%';
  }
  return $columns;
}
add_filter('manage_wde_taxes_custom_column', 'wde_taxes_column', 10, 3);

// Delete custom taxonomy extra fields options.
function wde_taxes_delete_term($term_id, $tt_id, $taxonomy) {
  delete_option("wde_taxes_" . $term_id);
}
add_action('delete_wde_taxes', 'wde_taxes_delete_term', 10, 3);