<?php

add_action( 'widgets_init', 'qaleb_popular_posts_widgets' );

function qaleb_popular_posts_widgets() {
	register_widget( 'qaleb_popular_posts_widget' );
}

class qaleb_popular_posts_widget extends WP_Widget {

public function __construct(){
$widget_ops = array(
'classname' => 'qaleb_popular_posts_widget',
'description' => 'مهمترین خبرها با شماره',
);
parent::__construct( 'qaleb_popular_posts_widget', 'mellat - اخبار مهم', $widget_ops );
}

	function widget( $args, $instance ) {
		
		extract( $args );
	    $title = apply_filters('widget_title', $instance['title'] );
		echo $before_widget;
		if ( $title )
		echo $before_title . $title . $after_title;
  
       ?>
	    <div class="popular-post">
       <?php
	   $entries_display = $instance['entries_display'];
	   if(empty($entries_display)){ $entries_display = '5'; }
			$display_category = $instance['display_category'];

        $args = array(
			'cat' => $display_category,
			'post_type' => 'post',
			'ignore_sticky_posts' => 1,
			'posts_per_page' => $entries_display,
			'orderby' => 'comment_count'			
		);
		$i = 0;
		$popular_posts = new WP_Query( $args );
		while($popular_posts->have_posts()): $popular_posts->the_post();
		$i++;
		
        ?>
		<div class="item-post <?php echo $class; ?>">		
			<div class="post-number"><?php echo $i; ?></div>
			<div class="post-right">
				<h2>
					<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
				</h2>	
			</div>				
		</div><!-- /item-post -->
       <?php endwhile; ?>
	   </div>
	   <?php
		
		/* After widget (defined by themes). */
		echo $after_widget;
	}

	function form( $instance ) {
		$defaults = array('title' => 'اخبار مهم', 'entries_display' => 5, 'display_category' => '');
		$instance = wp_parse_args((array) $instance, $defaults);
	?>
		<p><label for="<?php echo $this->get_field_id( 'title' ); ?>">عنوان</label>
        <input type="text" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $instance['title']; ?>" style="width:100%;" /></p>
		
	    <label for="<?php echo $this->get_field_id( 'display_category' ); ?>">دسته بندی:</label>
	    <select name="<?php echo $this->get_field_name( 'display_category' ); ?>" id="<?php echo $this->get_field_id( 'display_category' ); ?>" class="widefat">
	    <option value='0' <?php selected( $instance['display_category'], "0" ); ?>>دسته بندی</option>
	    <?php foreach(get_categories( array('hide_empty' => 0) ) as $cat) { ?>
	    <option <?php selected( $instance['display_category'], $cat->cat_ID ); ?> value="<?php echo $cat->cat_ID; ?>"><?php echo $cat->cat_name; ?></option>
	    <?php } ?>
	    </select>
		
				<p><label for="<?php echo $this->get_field_id( 'entries_display' ); ?>">تعداد نوشته</label>
		<input type="text" id="<?php echo $this->get_field_id('entries_display'); ?>" name="<?php echo $this->get_field_name('entries_display'); ?>" value="<?php echo $instance['entries_display']; ?>" style="width:100%;" /></p>
	
	<?php
	}
}
?>