<?php

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

/*
 *	CakeArt Search Form Widget
 */

class Thim_Search_Form extends WP_Widget {

	function __construct() {
		parent::__construct(
			'thim_search_form_widget',
			esc_html__( 'Thim: Search Form', 'cakeart' ),
			array(
				'description' => esc_html__( 'Search post types in site.', 'cakeart' )
			)
		);
	}

	public function form( $instance ) {
		$default = array(
			'title'       => 'Search Form',
			'placeholder' => 'Search...',
			'post_type'   => 'post'
		);

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

		$title       = $instance['title'];
		$placeholder = $instance['placeholder'];
		$post_type   = $instance['post_type'];
		?>

		<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>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'placeholder_form' ) ); ?>"><?php esc_html_e( 'Placeholder', 'cakeart' ); ?></label>
			<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'placeholder_form' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'placeholder' ) ) ?>" type="text" value="<?php echo esc_attr( $placeholder ); ?>" />
		</p>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'post_type_search' ) ); ?>"><?php esc_html_e( 'Post type', 'cakeart' ); ?></label>
			<select class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'post_type_search' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'post_type' ) ); ?>">
				<?php
				$types = get_post_types(
					array(
						'public' => 'true',
					),
					'names'
				);
				?>
				<?php foreach ( $types as $type ) { ?>
					<?php if ( $type != 'attachment' && $type != 'our_team' && $type != 'testimonials' && $type != 'page' ) : ?>
						<option value="<?php echo esc_attr( $type ); ?>" <?php echo ( $post_type === $type ) ? 'selected' : ''; ?>><?php printf( esc_html__( '%s', 'cakeart' ), $type ); ?></option>
					<?php endif; ?>
				<?php } ?>
				<?php wp_reset_postdata(); ?>
			</select>
		</p>

		<?php
	}

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

	}

	public function widget( $args, $instance ) {
		extract( $args, EXTR_SKIP );

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

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

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

		echo '<form class="search-post-types" method="get" action="' . esc_url( get_site_url() ) . '">';
		echo '<div class="ps_container">';
		echo '<input class="search-field-product" type="search" name="s" placeholder="' . esc_html( $placeholder ) . '" autocomplete="off">';
		echo '<button class="icon-search" onclick="jQuery(this).closest(\'form\').submit();"><i class="fa fa-search"></i></button>';
		echo '<input type="hidden" name="post_type" value="' . esc_html( $post_type ) . '">';
		echo '</div>';
		echo '</form>';

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

}

add_action( 'widgets_init', 'thim_register_search_form_widget' );

function thim_register_search_form_widget() {
	register_widget( 'Thim_Search_Form' );
}