<?php

/**
 * Manage Tags.
 *
 * @link    https://aiopodcasting.com
 * @since   1.0.0
 *
 * @package AiO_Podcast_Studio
 */

// Exit if accessed directly
if ( ! defined( 'WPINC' ) ) {
	die;
}

/**
 * AIOPOD_Admin_Tags class.
 *
 * @since 1.0.0
 */
class AIOPOD_Admin_Tags {

	/**
	 * Add "Episode Tags" menu.
	 *
	 * @since 1.0.0
	 */
	public function admin_menu() {	
		add_submenu_page(
			'aio-podcast-studio',
			__( 'AiO Podcast Studio - Tags', 'aio-podcast-studio' ),
			__( 'Episode Tags', 'aio-podcast-studio' ),
			'manage_aiopod_options',
			'edit-tags.php?taxonomy=aiopod_tags&post_type=aiopod_episodes'
		);	
	}

	/**
	 * Move "Tags" submenu under our plugin's main menu.
	 *
	 * @since  1.0.0
	 * @param  string $parent_file The parent file.
	 * @return string $parent_file The parent file.
	 */
	public function parent_file( $parent_file ) {	
		global $submenu_file, $current_screen;

		if ( 'aiopod_tags' === $current_screen->taxonomy ) {
			$parent_file  = 'aio-podcast-studio';
			$submenu_file = 'edit-tags.php?taxonomy=aiopod_tags&post_type=aiopod_episodes';			
		}

		return $parent_file;
	}
	
	/**
	 * Register the custom taxonomy "aiopod_tags".
	 *
	 * @since 1.0.0
	 */
	public function register_taxonomy() {	
		$labels = array(
			'name'                       => _x( 'Episode Tags', 'Taxonomy General Name', 'aio-podcast-studio' ),
			'singular_name'              => _x( 'Tag', 'Taxonomy Singular Name', 'aio-podcast-studio' ),
			'menu_name'                  => __( 'Episode Tags', 'aio-podcast-studio' ),
			'all_items'                  => __( 'All Tags', 'aio-podcast-studio' ),
			'parent_item'                => null,
			'parent_item_colon'          => null,
			'new_item_name'              => __( 'New Tag Name', 'aio-podcast-studio' ),
			'add_new_item'               => __( 'Add New Tag', 'aio-podcast-studio' ),
			'edit_item'                  => __( 'Edit Tag', 'aio-podcast-studio' ),
			'update_item'                => __( 'Update Tag', 'aio-podcast-studio' ),
			'view_item'                  => __( 'View Tag', 'aio-podcast-studio' ),
			'separate_items_with_commas' => __( 'Separate tags with commas', 'aio-podcast-studio' ),
			'add_or_remove_items'        => __( 'Add or remove tags', 'aio-podcast-studio' ),
			'choose_from_most_used'      => __( 'Choose from the most used', 'aio-podcast-studio' ),
			'popular_items'              => __( 'Popular Tags', 'aio-podcast-studio' ),
			'search_items'               => __( 'Search Tags', 'aio-podcast-studio' ),
			'not_found'                  => __( 'No tags found.', 'aio-podcast-studio' ),
			'no_terms'                   => __( 'No tags', 'aio-podcast-studio' ),
			'items_list'                 => __( 'Tags list', 'aio-podcast-studio' ),
			'items_list_navigation'      => __( 'Tags list navigation', 'aio-podcast-studio' ),
		);
		
		$args = array(
			'labels'                     => $labels,
			'hierarchical'               => false,
			'public'                     => true,
			'show_ui'                    => true,
			'show_admin_column'          => true,
			'show_in_nav_menus'          => true,
			'show_in_rest'               => true,
			'show_tagcloud'              => true,
			'capabilities'               => array(
				'manage_terms' => 'manage_aiopod_options',
				'edit_terms'   => 'manage_aiopod_options',				
				'delete_terms' => 'manage_aiopod_options',
				'assign_terms' => 'edit_aiopod_podcasts'
			)
		);
		
		register_taxonomy( 'aiopod_tags', array( 'aiopod_episodes' ), $args );	
	}
	
	/**
	 * Retrieve the table columns.
	 *
	 * @since  1.0.0
	 * @param  array $columns Array of default table columns.
	 * @return array $columns Updated list of table columns.
	 */
	public function get_columns( $columns ) {	
		$columns = aiopod_insert_array_after( 'slug', $columns, array( 
			'tax_id' => __( 'ID', 'aio-podcast-studio' )
		));
		
    	return $columns;		
	}
	
	/**
	 * This function renders the custom columns in the list table.
	 *
	 * @since 1.0.0
	 * @param string $content Content of the column.
	 * @param string $column  Name of the column.
	 * @param string $term_id Term ID.
	 */
	public function custom_column_content( $content, $column, $term_id ) {		
		switch ( $column ) {
			case 'tax_id':
				$content = $term_id;
				break;
		}	
		
		return $content;	
	}

}
