<?php
add_action( 'widgets_init', 'wp_posts_slider_widget' );
function wp_posts_slider_widget() {
	register_widget( 'wp_posts_slider' );
}

class wp_posts_slider extends WP_Widget {

	public function __construct(){
		$widget_ops = array( 'classname' => 'posts-slider','description' => 'نمایش مطالب به صورت اسلاید'  );
		$control_ops = array( 'width' => 250, 'height' => 350, 'id_base' => 'posts-slider-widget' );
		parent::__construct( 'posts-slider-widget',''. __( 'mellat - اسلایدر کوچک ' , 'wp'), $widget_ops, $control_ops );
	}
	
	function widget( $args, $instance ) {
		extract( $args );
		
        if ( isset( $instance[ 'category_link' ] ) ) {
			$category_link = $instance[ 'category_link' ];
		}

        if ( isset( $instance[ 'category_target' ] ) ) {
			$category_target = $instance[ 'category_target' ];
		}

		$title     = $title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
		$category  = $instance['category'];
		$number    = $instance['number'];
?>
<div id="<?php echo $args['widget_id']; ?>" class="ap_slider">
 <?php
    $my_query = new WP_Query('showposts='.$number.'&cat='.$category);
    while ($my_query->have_posts()):  $my_query->the_post(); ?>
<div class="item clearfix">
<div class="thumbnail">
<?php if(has_post_thumbnail()){?>
<a href="<?php the_permalink(); ?>"><img src="<?php the_post_thumbnail_url('loop-bar'); ?>" alt="<?php the_title(); ?>"></a>
<?php }else{ ?>
<img src="<?php bloginfo("template_url"); ?>/img/none.png">
<?php } ?>
 </div>
 <div class="det">
<div class="met">
<?php if(get_post_meta( get_the_ID(), 'meta-text-1', true )!=null){ ?><span class="cat"><?php echo get_post_meta( get_the_ID(), 'meta-text-1', true ); ?></span> <?php } ?>
</div>
<h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
</div>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</div>
 	<?php 
	}

	function update( $new_instance, $old_instance ) {
		$instance              = $old_instance;
		$instance['title']     = wp_strip_all_tags( $new_instance['title'] );
		$instance['category']  = wp_strip_all_tags( $new_instance['category'] );
		$instance['category_link']     = wp_strip_all_tags( $new_instance['category_link'] );
		$instance['category_target']  = wp_strip_all_tags( $new_instance['category_target'] );
		$instance['number']    = is_numeric( $new_instance['number'] ) ? intval( $new_instance['number'] ) : 5;
		return $instance;
	}

	function form( $instance ) {
		$defaults  = array( 'title' => '', 'category' => '', 'number' => 3, 'show_date' => '' );
		$instance  = wp_parse_args( ( array ) $instance, $defaults );
		$title     = $instance['title'];
		$category  = $instance['category'];
		$category_link     = $instance['category_link'];
		$category_target  = $instance['category_target'];	
		$number    = $instance['number'];
		if ( isset( $instance[ 'icon_code' ] ) ) {
		$icon_code = $instance[ 'icon_code' ];
		}
		?>
		<p></p>
			<label for="<?php echo $this->get_field_id( 'category' ); ?>">انتخاب دسته:</label>					
			<?php

			wp_dropdown_categories( array(

				'orderby'    => 'title',
				'hide_empty' => false,
				'name'       => $this->get_field_name( 'category' ),
				'id'         => $this->get_field_id( 'category' ),
				'class'      => 'widefat',
				'selected'   => $category

			) );

			?>

		</p>
		<p>
		    <input type="checkbox" name="<?php echo $this->get_field_name( 'category_target' ); ?>" id="<?php echo $this->get_field_id( 'category_target' ); ?>" class="checkbox" <?php checked( $category_target, "on" ); ?>>
		    <label for="<?php echo $this->get_field_id( 'category_target' ); ?>">در زبانه جدید باز شود</label>
		</p>		
		<p>
			<label for="<?php echo $this->get_field_id( 'number' ); ?>">تعداد نوشته : </label>
			<input type="text" id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" value="<?php echo esc_attr( $number ); ?>" size="3" />
		</p>

		
		
		<?php
	
	}
}
?>