<?php

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

/**
 * Class Thim_Recipe_Categories
 */
class Thim_Recipe_Categories extends WP_Widget {

	/**
	 * Register widget with WordPress.
	 */
	function __construct() {
		parent::__construct(
			'thim_recipe_categories',
			esc_html__( 'Thim: Recipe Categories', 'cakeart' ),
			array(
				'description' => esc_html__( 'A list of recipe categories.', 'cakeart' ),
			)
		);
	}

	public function form( $instance ) {
		$default = array(
			'title' => 'Recipe Categories'
		);

		$instance = wp_parse_args( (array) $instance, $default );

		$title = $instance['title'];

		?>

		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'widget_title' ) ); ?>"><?php esc_html_e( 'Title', 'cakeart' ); ?></label>
			<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'widget_title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ) ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
		</p>

	<?php
	}

	public function update( $new_instance, $old_instance ) {
		$instance          = $old_instance;
		$instance['title'] = strip_tags( $new_instance['title'] );
		return $instance;
	}


	public function widget( $args, $instance ) {

		extract( $args, EXTR_SKIP );

		$title = apply_filters( 'widget-title', $instance['title'] );

		echo ent2ncr( $args['before_widget'] );

		echo '<h3 class="widget-title">' . esc_html( $title ) . '</h3>';

		$query = array(
			'orderby'      => 'name',
			'show_count'   => 1,
			'pad_counts'   => 0,
			'hierarchical' => 1,
			'taxonomy'     => 'recipe_category',
			'title_li'     => '',
		);

		?>

		<div class="widget_product_categories">
			<ul>
				<?php wp_list_categories( $query ); ?>
			</ul>
			<?php wp_reset_postdata(); ?>
		</div>

		<?php

		echo ent2ncr( $args['after_widget'] );

	}

}

add_action( 'widgets_init', 'thim_register_recipe_categories_widget' );

function thim_register_recipe_categories_widget() {
	register_widget( 'Thim_Recipe_Categories' );
}