<?php 
class sidebar_related extends WP_Widget {

	public function __construct(){
		$widget_ops = array(
			'classname'   => 'sidebar-related',
			'description' => __( THEMES_NAMES.' Sidebar Related Post Widget for Single', THEMES_NAMES ),
		);
		parent::__construct( 'sidebar-related', __( '(MDR) Sidebar Related Post', THEMES_NAMES ), $widget_ops );
	}
	
	public function widget( $args, $instance ){
		$widget_id			= $this->id_base . '-' . $this->number; 
		global $opt_themes;
				
		/*
		https://codeless.co/wordpress-related-posts-without-plugin/
		*/ 
		  $terms			= get_the_terms( get_the_ID(), 'category' );
		  if ( empty( $terms ) ) $terms = array();
		  $term_list		= wp_list_pluck( $terms, 'slug' );
		  $related_args		= array(
			'post_type'			=> 'post',
			'posts_per_page'	=> $instance['number_posts'],
			'post_status'		=> 'publish',
			'post__not_in'		=> array( get_the_ID() ),
			'orderby'			=> $instance['orderby'],
			'tax_query'			=> array(
			  array(
				'taxonomy'		=> 'category',
				'field'			=> 'slug',
				'terms'			=> $term_list
			  )
			)
		  );
		  
		if( $instance['orderby'] == 'views' ){
			$related_args = array(
			'post_type'			=> 'post',
			'posts_per_page'	=> $instance['number_posts'],
			'post_status'		=> 'publish',
			'post__not_in'		=> array( get_the_ID() ),
			'order'				=> 'DESC',
			'orderby'			=> 'meta_value_num',
			'meta_key'			=> 'post_views_count',
			'tax_query'			=> array(
			  array(
				'taxonomy'		=> 'category',
				'field'			=> 'slug',
				'terms'			=> $term_list
			  )
			) 
			);
		}
		 

		$categorys		= get_the_terms( get_the_ID(), 'category' );  
		
		$rp = new WP_Query( apply_filters( 'related_post_sidebar_widget_posts_args', $related_args ) );
		 
		
		global $cl_from_element;
		$cl_from_element['is_related'] = true;		 
		if ( $rp->have_posts() ) :  
		switch($opt_themes['themes_styles']){
			case 'moddroid':
			?>
			<section class="mb-2">
			<h2 class="h5 font-weight-semibold mb-3 text-body border-bottom-2 border-secondary d-inline-block pb-1"><?php if($instance['show_cats_name']){ ?><?php echo $categorys[0]->name; ?> - <?php } ?><?php echo $instance['title']; ?> </h2> 
			<?php 
			break; 
			
			case 'modyolo':
			?>
			<section class="bg-white border rounded shadow-sm pt-3 px-2 px-md-3 mb-3">
			<header class="d-flex align-items-end mb-3">
			<h2 class="h5 font-weight-semibold mb-0"> 
			<?php echo $instance['title']; ?>	 
			</h2> 
			</header>
			<?php 
			break; 
			
			case 'reborn':
			?> 
			<section class="bg-white rounded pb-3 pt-3 px-2 mb-4 ">
			
			<div class="d-flex align-items-baseline justify-content-between ">
			<h2 class="h5 font-weight-semibold m-0 p-0 mb-3 "><?php echo $instance['title']; ?> </h2> 
			</div>
			<?php 
			break; 
			
			case 'liteapks':
			?> 
			<section class="mb-4">
			<h4 class="h4 font-weight-bold mb-3"><?php if($instance['show_cats_name']){ ?><?php echo $categorys[0]->name; ?> - <?php } ?><?php echo $instance['title']; ?></h4> 
			<?php 
			break; 
			
			default:
			?>
			<section class="mb-2">
			<h2 class="h5 font-weight-semibold mb-3 text-body border-bottom-2 border-secondary d-inline-block pb-1"><?php echo $instance['title']; ?></h2> 
			<?php 
			break;
		} ?>  
		
		<?php $i = 0;
        while ( $rp->have_posts() ) : $rp->the_post();
       
		//set_query_var( 'value', $value );  	
		//set_query_var( 'show_image', $instance['show_image'] );  	
			
		global $wpdb, $post, $opt_themes;  
		 
		switch($opt_themes['themes_styles']){
			case 'moddroid':
				get_template_part('loop/item.sidebar.mdr');			 
			break; 
			
			case 'modyolo':
				get_template_part('loop/item.sidebar.mdy');			 
			break; 
			
			case 'reborn':			
				get_template_part('loop/item.sidebar.reborn');			
			break; 
			
			case 'liteapks':
				get_template_part('loop/item.sidebar.liteapks');	
			break;
			
			default:			
				get_template_part('loop/item.sidebar.mdr');		
			break;
		} ?> 
        <?php
          
		if( $i == 2 )
		break;
		endwhile;
		?>
 
		</section>
		<?php endif; 

		wp_reset_query();
		$cl_from_element['is_related'] = false;
		?>
		 
		 
		
		<?php
		  
	}

	public function update( $new_instance, $old_instance ){
		$instance		= $old_instance;
		$new_instance	= wp_parse_args(
			(array) $new_instance,
			array(
			'title'				=> '', 
			'number_posts'		=> 5,  
			'show_cats_name'	=> false, 
			'orderby'			=> 'date',
			)
		); 
		$instance['title']			= sanitize_text_field( $new_instance['title'] ); 
		$instance['number_posts']	= absint( $new_instance['number_posts'] ); 
		$instance['show_cats_name']	= (bool) $new_instance['show_cats_name'];
		$instance['orderby']		= $new_instance['orderby'];
		 
		return $instance;
	}

	public function form( $instance ){
		$instance = wp_parse_args(
			(array) $instance,
			array(
			'title'				=> __( 'Related', THEMES_NAMES ), 
			'number_posts'		=> 5,   
			'show_cats_name'	=> true,  
			'orderby'			=> 'date',
			)
		);
		$title					= sanitize_text_field( $instance['title'] ); 
		$number_posts			= absint( $instance['number_posts'] );	
		$show_cats_name			= (bool) $instance['show_cats_name']; 
		 
		?>
		<p class="info-notice danger"><?php _e( 'Sidebar Post by Related post', THEMES_NAMES ); ?> </p>
		<hr />
		<p>
			<label for="<?php echo esc_html( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:', THEMES_NAMES ); ?></label>
			<input class="widefat" id="<?php echo esc_html( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_html( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
		</p>
		<hr />
		 
		<p>
			<label for="<?php echo esc_html( $this->get_field_id( 'number_posts' ) ); ?>"><?php _e( 'Number post', THEMES_NAMES ); ?></label>
			<input class="widefat" id="<?php echo esc_html( $this->get_field_id( 'number_posts' ) ); ?>" name="<?php echo esc_html( $this->get_field_name( 'number_posts' ) ); ?>" type="number" value="<?php echo esc_attr( $number_posts ); ?>" />
		</p> 
		<hr />	
		
		<p>
			<label for="<?php echo $this->get_field_id('orderby'); ?>"><?php _e('Mode:', THEMES_NAMES) ?> </label>
			<select id="<?php echo $this->get_field_id('orderby'); ?>" name="<?php echo $this->get_field_name('orderby'); ?>">
			<option <?php if ($instance['orderby'] == 'date') echo 'selected="selected"'; ?> value="date"><?php _e('Recent Posts', THEMES_NAMES); ?></option>
			<option <?php if ($instance['orderby'] == 'rand') echo 'selected="selected"'; ?> value="rand"><?php _e('Random Posts', THEMES_NAMES); ?></option>
			<option <?php if ($instance['orderby'] == 'modified') echo 'selected="selected"'; ?> value="modified"><?php _e('Recent Modified date', THEMES_NAMES); ?></option>
			<option <?php if ($instance['orderby'] == 'views') echo 'selected="selected"'; ?> value="views"><?php _e('Post views', THEMES_NAMES); ?></option>			
			 
			</select>
		</p>
		<hr />
		
		<p>
			<input class="checkbox" type="checkbox" <?php checked( $show_cats_name ); ?> id="<?php echo esc_html( $this->get_field_id( 'show_cats_name' ) ); ?>" name="<?php echo esc_html( $this->get_field_name( 'show_cats_name' ) ); ?>" /><label for="<?php echo esc_html( $this->get_field_id( 'show_cats_name' ) ); ?>"><?php _e( 'Show Category Name ?', THEMES_NAMES ); ?></label>
		</p> 
		<?php
	}
	 
	
}
add_action(	'widgets_init',	function(){	register_widget( 'sidebar_related' );	});
