<?php
/*
Theme Designed By: S O R O U S H M A R K E T
Email: info@soroushmarket.ir
Demo Website: soroushmarket.ir
Author Website: soroushmarket.ir
*/
   // Exit if accessed directly
   
   if ( ! defined( 'ABSPATH' ) ) exit;
   
   
   
   class Random_post_widget extends WP_Widget {
   
   
   
   	public function __construct() {
   
   		$widget_ops = array('classname' => 'random-post_widget', 'description' => 'نمایش تصادفی مطالب' );
   
   		parent::__construct('random-post', 'مطالب تصادفی', $widget_ops);
   
   		$this->alt_option_name = 'random-post_widget';
   
   	}
   
   
   
   	function widget($args, $instance) {
   
              
   
   			extract( $args );
   
   		
   
   			$title = apply_filters( 'widget_title', empty($instance['title']) ? 'مطالب تصادفی' : $instance['title'], $instance, $this->id_base);	
   
   			
   
   			if ( ! $number = absint( $instance['number'] ) ) $number = 5;
   
   
   
   		$my_args=array(
   
   						   
   
   				'showposts' => $number,
   
   				'orderby'   => 'rand',
   
   				'post_type' => 'post'
   
   				
   
   				);
   
   			
   
   			$random_posts = null;
   
   			
   
   			$random_posts = new WP_Query($my_args);
   
   			
   
   			echo $before_widget;
   
   			
   
   			// Widget title
   
   			
   
   			echo $before_title;
   
   			
   
   			echo $instance["title"];
   
   			
   
   			echo $after_title;
   
   		
   
   		echo "<ul>\n";
   
   			
   		if( $random_posts->have_posts() ){
   		while ( $random_posts->have_posts() )
   
   
   
   		{
   
   
   
   			$random_posts->the_post();
   
   
   
   		?>
<li class="random-post-item">
   <div class="post-image">
      <?php if ( has_post_thumbnail()) :  ?>
      <?php the_post_thumbnail( 'widget-img' ); ?>
      <?php else: ?>
      <img src="<?php echo esc_url( get_template_directory_uri() ); ?>/images/tumb.png" class="post-tumb"/>
      <?php endif; ?>
   </div>
   <div class="w-details">
      <a  href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
      <?php the_title();?>
      </a>
      <br />
      <span class="w-price"><?php echo get_the_date('j F Y'); ?></span>
   </div>
</li>
<?php
   }
	}
   else
   {
   	echo 'نوشته ای پیدا نشد!';
   }
    wp_reset_query();
   
   
   
   echo "</ul>\n";
   
   
   
   echo $after_widget;
   
   	
   
   }
   
   
   
   public function update( $new_instance, $old_instance ) {
   
   $instance = $old_instance;
   
   $instance['title'] = sanitize_text_field( $new_instance['title'] );
   
   $instance['number'] = (int) $new_instance['number'];
   
   return $instance;
   
   }
   
   
   
   public function form( $instance ) {
   
   $title     = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
   
   $number    = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
   
   ?>
<p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
   <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" />
</p>
<p><label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of posts to show:' ); ?></label>
   <input class="tiny-text" id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="number" step="1" min="1" value="<?php echo $number; ?>" size="3" />
</p>
<?php
   }
   
   }
   
   // register Random Posts Widget
   
   add_action( 'widgets_init', create_function( '', 'return register_widget("Random_post_widget");' ) );
   
   ?>