<?php
class anews_Recent_Posts_Widget extends WP_Widget {
// Main constructor
	public function __construct() {

		$widget_id       = 'anews_recent_post';
		$widget_title    = esc_html__( 'Anews Recent Post With Thumbnails', 'anews' );
		$widget_settings = array(
			'customize_selective_refresh' => true,
			'classname'                   => 'anews_Recent_Posts_Widget',
			'description'                 => esc_html__( 'Display Recent Posts with thumbnails', 'anews' )
		);

		parent::__construct( $widget_id, $widget_title, $widget_settings );
	}
// The widget form (for the backend )
	public function form( $instance ) {
// Set widget defaults
		$defaults = array(
			'title' => esc_html__( 'Recent Posts', 'anews' ),
			'anews_count' => 3,
		);
		$instance = wp_parse_args( (array) $instance, $defaults ); ?>
		<!-- Title -->
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Widget Title', 'anews' ); ?></label>
			<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>"/>
		</p>
		<!-- Posts anews_count -->
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'anews_count' ) ); ?>"><?php esc_html_e( 'Posts Count:', 'anews' ); ?></label>
			<input type="number" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'anews_count' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'anews_count' ) ); ?>" value="<?php echo esc_attr( $instance['anews_count'] ); ?>"/>
		</p>
		<?php
	}
// Update widget settings
	public function update( $new_instance, $old_instance ) {
		$instance          = $old_instance;
		$instance['title'] = isset( $new_instance['title'] ) ? wp_strip_all_tags( $new_instance['title'] ) : '';
		$instance['anews_count'] = isset( $new_instance['anews_count'] ) ? wp_strip_all_tags( $new_instance['anews_count'] ) : '';
		return $instance;
	}
// Display the widget
	public function widget( $args, $instance ) {
		extract( $args );
		// Check the widget options
		$title = isset( $instance['title'] ) ? apply_filters( 'widget_title', $instance['title'] ) : esc_html_e('Instagram Feed','anews');
		$anews_count = isset( $instance['anews_count'] ) ? $instance['anews_count'] : 3 ;
		echo wp_kses_post($before_widget);
		if ( ! empty($title) ) :
			echo wp_kses_post( $before_title ) . esc_html( $title ) . wp_kses_post( $after_title );
		endif; ?>
		<div class="anews-recent-post-widget">
			<?php
			$anews_sticky = get_option( 'sticky_posts' );
			$anews_query   = new WP_Query( array(
				'posts_per_page'      => $anews_count,
				'ignore_sticky_posts' => 1,
				'post__not_in'        => $anews_sticky,
			) );
			?>
			<?php if ( $anews_query ->have_posts() ) : while ( $anews_query ->have_posts() ) : $anews_query ->the_post(); ?>
			<div class="anews-hpost-item">
				<?php if ( has_post_thumbnail() ) { ?>
				<div class="hpost-img">
					<a href="<?php esc_url(the_permalink()); ?>"><img src="<?php esc_url(the_post_thumbnail_url()); ?>" alt="<?php the_title_attribute(); ?>" class="widget-posts-img"></a>
				</div>
				<?php } ?>
				<div class="hpost-contents">
					<h2><a href="<?php esc_url( the_permalink() ); ?>"><?php echo esc_html( get_the_title(), 4 ); ?></a></h2>
					<span><i class="fas fa-calendar"></i><?php echo esc_html( get_the_date() ); ?></span>
				</div>
            </div>
			<?php endwhile; endif; ?>
		</div>
		<?php echo wp_kses_post($after_widget);
	}
}
/**
 * Register widgets
 */
function anews_register_recent_posts_widget() {
	register_widget( 'anews_Recent_Posts_Widget' );
}
add_action( 'widgets_init', 'anews_register_recent_posts_widget' );