<?php
class qaleb_box_2_widget extends WP_Widget {

	public function __construct(){
		$widget_ops = array( 'classname' => 'qaleb_box_2_widget','description' => 'تصویر کوچک و متن'  );
		$control_ops = array( 'id_base' => 'qaleb_box_2_widget' );
		parent::__construct( 'qaleb_box_2_widget',( 'Jam - نوشته طرح دوم'), $widget_ops, $control_ops );
	}		

function widget( $args, $instance ) {
$widget_title = apply_filters( 'widget_title', $instance['widget_title'] );
$post_num = apply_filters( 'post_num', $instance['post_num'] );
$post_cat = apply_filters( 'post_cat', $instance['post_cat'] );
$limit_title = apply_filters( 'limit_title', $instance['limit_title'] );
echo $args['before_widget'];
if( !empty($widget_title) ) {
echo $args['before_title'];
echo $widget_title;
echo $args['after_title'];
}
?>
<div class="wi_n wi_n_one clearfix">
<div class="polist">
<?php
$catquery = new WP_Query('cat=' . $post_cat . '&posts_per_page=' . $post_num .'');
if( $catquery->have_posts() ) :
while( $catquery->have_posts() ) : $catquery->the_post();
?>
<div class="item">
<div class="thumbnail">
<?php if(has_post_thumbnail()){?>
<a href="<?php the_permalink(); ?>"><img src="<?php the_post_thumbnail_url('loop-1'); ?>"></a>
<?php }else{ ?>
<img src="<?php bloginfo("template_url"); ?>/img/none.png">
<?php } ?>
</div>
<div class="det">
<?php if(get_post_meta( get_the_ID(), 'meta-text-3', true )!=null){ ?><div class="roti"><?php echo get_post_meta( get_the_ID(), 'meta-text-3', true ); ?></div><?php } ?>
<h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
</div>
</div>
<?php
endwhile;
wp_reset_postdata();
endif;
?>
</div></div>
<?php
echo $args['after_widget'];
}

function update( $new_instance, $old_instance ) {
$instance = array();
$instance['widget_title'] = ( ! empty( $new_instance['widget_title'] ) ) ? strip_tags( $new_instance['widget_title'] ) : '';
$instance['post_num'] = ( ! empty( $new_instance['post_num'] ) ) ? strip_tags( $new_instance['post_num'] ) : '';
$instance['post_cat'] = ( ! empty( $new_instance['post_cat'] ) ) ? strip_tags( $new_instance['post_cat'] ) : '';
$instance['limit_title'] = ( ! empty( $new_instance['limit_title'] ) ) ? $new_instance['limit_title'] : '';
return $instance;
}

function form( $instance ) {
if ( isset( $instance[ 'widget_title' ] ) ) {
$widget_title = $instance[ 'widget_title' ];
} else {
$widget_title = 'آخرین اخبار';
}

if ( isset( $instance[ 'post_num' ] ) ) {
$post_num = $instance[ 'post_num' ];
} else {
$post_num = '4';
}

if ( isset( $instance[ 'limit_title' ] ) ) {
$limit_title = $instance[ 'limit_title' ];
}
?>
<p>
<label for="<?php echo $this->get_field_id( 'widget_title' ); ?>">عنوان :</label>
<input name="<?php echo $this->get_field_name( 'widget_title' ); ?>" type="text" value="<?php echo esc_attr( $widget_title ); ?>" id="<?php echo $this->get_field_id( 'widget_title' ); ?>" class="widefat">
</p>
<p>
<label for="<?php echo $this->get_field_id( 'post_cat' ); ?>">دسته بندی :</label>
<select name="<?php echo $this->get_field_name( 'post_cat' ); ?>" id="<?php echo $this->get_field_id( 'post_cat' ); ?>" class="widefat">
<option value='0' <?php selected( $instance['post_cat'], "0" ); ?>>همه دسته ها </option>
<?php foreach(get_categories( array('hide_empty' => 0) ) as $cat) { ?>
<option <?php selected( $instance['post_cat'], $cat->cat_ID ); ?> value="<?php echo $cat->cat_ID; ?>"><?php echo $cat->cat_name; ?></option>
<?php } ?>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'post_num' ); ?>">تعداد :</label>
<input name="<?php echo $this->get_field_name( 'post_num' ); ?>" type="number" step="1" min="1" value="<?php echo esc_attr( $post_num ); ?>" size="3" id="<?php echo $this->get_field_id( 'post_num' ); ?>" class="tiny-text">
</p>
<?php
}
}

function qaleb_box_2_widget() {
register_widget( 'qaleb_box_2_widget' );
}

add_action( 'widgets_init', 'qaleb_box_2_widget' );
?>