<?php
/**
 * Template: Taxonomy Terms as Input with Datalist
 *
 * Override this in your theme by copying it to wp-content/themes/{yourtheme}/matador/jobs-taxonomies-datalist.php.
 *
 * @link        https://matadorjobs.com/
 * @since       3.9.0
 *
 * @package     Matador Jobs
 * @subpackage  Templates
 * @author      Matador Software LLC, Jeremy Scott, Paul Bearne
 * @copyright   (c) 2017-2025, Matador Software, LLC
 *
 * @license     https://opensource.org/licenses/GPL-3.0 GNU General Public License version 3
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

/**
 * Defined before include:
 *
 * @var array $taxonomy
 * @var string $label
 * @var string $as
 * @var mixed $class
 * @var array $terms
 * @var array $args (contains all the above in one array)
 */
?>

<?php
/**
 * Action: Matador Taxonomy Terms Before
 *
 * Fires before opening markup for the [matador_taxonomy] shortcode and matador_taxonomy() function.
 *
 * @since 3.3.0
 *
 * @var array $args
 */
do_action( 'matador_taxonomy_terms_before', $args );
?>

<div class="<?php matador_build_classes( 'matador-terms', 'matador-terms-datalist', "matador-taxonomy-{$taxonomy['slug']}-terms-datalist", $class ); ?>">

	<?php
	/**
	 * Action: Matador Taxonomy Terms Before Terms
	 *
	 * Fires before terms but after opening markup for the [matador_taxonomy] shortcode and matador_taxonomy() function.
	 *
	 * Note: Action fires in list, select, and datalist type templates. Pass your function the $args attribute and use the
	 * $args['as'] to narrow the focus of your action.
	 *
	 * @since 3.3.0
	 *
	 * @param array $args
	 */
	do_action( 'matador_taxonomy_terms_before_terms', $args );
	?>

	<label for="<?php echo esc_attr( $taxonomy['key'] ); ?>">
		<span class="matador-screen-reader-text">
		<?php
		if ( 'value' === $method && ! $multi ) {
			$screen_reader_text = __( 'Limit jobs to this', 'matador-jobs' ) . ' ' . $taxonomy['single'];
		} elseif ( 'value' === $method && $multi ) {
			$screen_reader_text = __( 'Limit jobs to these', 'matador-jobs' ) . ' ' . $taxonomy['plural'];
		} else {
			$screen_reader_text = __( 'View jobs in this', 'matador-jobs' ) . ' ' . $taxonomy['single'];
		}
		/**
		 * Filter: Matador Taxonomy Terms Datalist Label Screen Reader Text
		 *
		 * To ensure ease of use, Matador includes some a11y phrases. This can be filtered for clearer
		 * statements.
		 *
		 * @since 3.3.0
		 *
		 * @var string
		 * @var array $args
		 */
		echo esc_html(
			apply_filters( 'matador_taxonomy_terms_datalist_label_screen_reader_text', $screen_reader_text, $args )
		);
		?>
		</span>
		<?php if ( $label ) : ?>
			<?php echo esc_html( $label ); ?>
		<?php endif; ?>
		<?php
		/**
		 * Action: Matador Taxonomy Terms Datalist Label
		 *
		 * Fires inside the <label> for the [matador_taxonomy] shortcode and matador_taxonomy() function in 'datalist'
		 * mode.
		 *
		 * @since 3.9.0
		 *
		 * @param array $args
		 */
		do_action( 'matador_taxonomy_terms_datalist_label', $args );
		?>
	</label>

	<?php
	/**
	 * Filter: Matador Taxonomy Terms Datalist Placeholder Text
	 *
	 * Modifies the text of the placeholder text for a taxonomy field input
	 *
	 * @since 3.9.0
	 */
	$placeholder = apply_filters(
		'matador_taxonomy_terms_datalist_placeholder',
		esc_html( ucfirst( $taxonomy['plural'] ) )
	);

	$value = ( empty( $_GET[ $taxonomy['key'] ] ) ? '' : ( sanitize_text_field( wp_unslash( $_GET[ $taxonomy['key'] ] ) ) ) );

	// Check if the value from the query param corresponds to one of the datalist options.
	if ( ! empty( $value ) ) {
		foreach ( $terms as $term ) {
			if ( $term->slug === $value ) {
				$value = $term->name;
				break;
			}
		}
	}

	?>

	<input
		type="text"
		id="<?php echo esc_attr( $taxonomy['key'] ); ?>"
		name="<?php echo esc_attr( $taxonomy['key'] ); ?>"
		value="<?php echo $value; ?>"
		placeholder="<?php echo esc_attr( $placeholder ); ?>"
		list="matador-datalist-<?php echo esc_attr( $taxonomy['key'] ); ?>"
	/>

	<datalist id="matador-datalist-<?php echo esc_attr( $taxonomy['key'] ); ?>">
		<?php foreach ( $terms as $datalist_term ) : ?>
			<option value="<?php echo esc_attr( $datalist_term->name ); ?>" data-value="<?php echo esc_html( $datalist_term->slug ); ?>"></option>
		<?php endforeach; ?>
	</datalist>

	<?php
	/**
	 * Action: Matador Taxonomy Terms After Terms
	 *
	 * Fires after closing markup for the [matador_taxonomy] shortcode and matador_taxonomy() function. Note: Action
	 * fires in both list and select type templates. Pass your function the $args attribute and use the $args['as'] to
	 * narrow the focus of your action.
	 *
	 * @since 3.3.0
	 *
	 * @var array $args
	 */
	do_action( 'matador_taxonomy_terms_after_terms', $args );
	?>

</div>

<?php
/**
 * Action: Matador Taxonomy Terms After
 *
 * Fires after closing markup for the [matador_taxonomy] shortcode and matador_taxonomy() function.
 *
 * @since 3.3.0
 *
 * @param array $args
 */
do_action( 'matador_taxonomy_terms_after', $args );
?>
