<?php

/**
 * Walker Terms Checklist.
 *
 * @link    https://aiopodcasting.com
 * @since   1.0.0
 *
 * @package AiO_Podcast_Studio
 */

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

/**
 * AIOPOD_Walker_Terms_Checklist class.
 *
 * @since 1.0.0
 */
class AIOPOD_Walker_Terms_Checklist extends Walker_Category_Checklist {
	
	/**
	 * Field name.
	 *
	 * @since  1.0.0
	 * @access private
	 * @var    string
	 */
	private $name;
	
	/**
	 * Get things started.
	 *
	 * @since 1.0.0
	 * @param string $name Field name.
	 */
	public function __construct( $name ) {
		$this->name = $name;
	}
	
	/**
	 * Start the element output.
	 *
	 * @since 1.0.0
	 * @param string $output   Used to append additional content (passed by reference).
	 * @param object $category The current term object.
	 * @param int    $depth    Depth of the term in reference to parents. Default 0.
	 * @param array  $args     An array of arguments. @see wp_terms_checklist()
	 * @param int    $id       ID of the current term.
	 */
	public function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {	
		$taxonomy = $args['taxonomy'];
		$name = $this->name;

		$args['popular_cats'] = empty( $args['popular_cats'] ) ? array() : $args['popular_cats'];
		$class = in_array( $category->term_id, $args['popular_cats'] ) ? ' class="popular-category"' : '';

		$args['selected_cats'] = empty( $args['selected_cats'] ) ? array() : $args['selected_cats'];

		/** This filter is documented in wp-includes/category-template.php */
		$output .= "\n<li id='{$taxonomy}-{$category->term_id}'$class>" .
			'<label class="selectit"><input value="' . $category->term_id . '" type="checkbox" name="'.$name.'[]" id="in-'.$taxonomy.'-' . $category->term_id . '"' .
			checked( in_array( $category->term_id, $args['selected_cats'] ), true, false ) .
			disabled( empty( $args['disabled'] ), false, false ) . ' /> ' .
			esc_html( apply_filters( 'the_category', $category->name, '', '' ) ) . '</label>';		
	}

}
