<?php
/**
 * Creates the template for the search filters taxonomy block.
 *
 * PHP version 7.3
 *
 * @link       https://crosswindsframework.com/downloads/crosswinds-blocks
 * @since      1.0.0
 *
 * @package    Crosswinds_Blocks
 * @subpackage Crosswinds_Blocks/blocks/search-filters-taxonomy
 */

// If this file is called directly, then about execution.
if ( ! defined( 'WPINC' ) ) {
	die;
}

$taxonomy_terms = get_terms(
	array(
		'taxonomy' => $attributes['taxonomy'],
	)
);
$queried_term   = get_queried_object();
?>

<div <?php echo wp_kses_data( get_block_wrapper_attributes() ); ?>>
	<label for="<?php echo esc_attr( $attributes['taxonomy'] ); ?>-search"><?php echo wp_kses_post( $attributes['label'] ); ?></label>
	<select name="<?php echo esc_attr( $attributes['taxonomy'] ); ?>" id="<?php echo esc_attr( $attributes['taxonomy'] ); ?>-search">
		<option value=""><?php esc_html_e( 'Select a', 'crosswinds-blocks' ); ?> <?php echo esc_attr( $attributes['taxonomyName'] ); ?></option>
		<?php
		if ( $taxonomy_terms ) {
			foreach ( $taxonomy_terms as $the_taxonomy ) {
				if ( $attributes['taxonomy'] === $queried_term->taxonomy && $queried_term->term_id === $the_taxonomy->term_id ) {
					$selected = 'selected="selected"';
				} else {
					$selected = '';
				}
				echo '<option value="' . esc_attr( $the_taxonomy->slug ) . '" ' . wp_kses_post( $selected ) . '>' . wp_kses_post( $the_taxonomy->name ) . '</option>';
			}
		}
		?>
	</select>
</div>
