<?php

/**
 * Layout: Dropdown.
 *
 * @link    https://aiopodcasting.com
 * @since   1.0.0
 *
 * @package AiO_Podcast_Studio
 */

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

$pages_settings = aiopod_get_option( 'aiopod_pages_settings' );

$tag_url = '/';
if ( ! empty( $pages_settings['tag'] ) ) {
	$tag_url = get_permalink( (int) $pages_settings['tag'] );
}
?>
<div class="aiopod aiopod-tags aiopod-tags--layout-dropdown" data-uri="<?php echo esc_url( $tag_url ); ?>">
	<?php
	$args = array(
		'taxonomy'          => 'aiopod_tags',
		'name'              => 'aiopod_tags',
		'show_option_none'  => '— ' . esc_html__( 'Select a Tag', 'aio-podcast-studio' ) . ' —',
		'option_none_value' => 0,
		'orderby'           => sanitize_text_field( $attributes['orderby'] ),
		'order'             => sanitize_text_field( $attributes['order'] ),
		'hide_empty'        => (int) $attributes['hide_empty'], 
		'show_count'        => (int) $attributes['show_count'], 		
		'walker'            => new AIOPOD_Walker_Terms_Dropdown(),   
		'echo'              => 0
	);

	if ( ! empty( $attributes['include'] ) ) { // Include tag IDs
		$args['include'] = array_map( 'intval', explode( ',', $attributes['include'] ) );
	}

	if ( ! empty( $attributes['exclude'] ) ) { // Exclude tag IDs
		$args['exclude'] = array_map( 'intval', explode( ',', $attributes['exclude'] ) );
	}

	if ( $term_slug = get_query_var( 'aiopod_tag' ) ) {
		$term = get_term_by( 'slug', sanitize_title( $term_slug ), 'aiopod_tags' );
		$args['selected'] = $term->term_id;
	}

	if ( isset( $_GET['topic'] ) ) {
		$args['selected'] = (int) wp_unslash( $_GET['topic'] );
	}
	
	$args = apply_filters( 'aiopod_tags_args', $args, $attributes );
	$html = wp_dropdown_categories( $args );

	echo $html;
	?>
</div>