<?php
/**
 * PixiGo Recent Posts widget.
 *
 * @package pixigo
 */

/**
 * PixiGo_Recent_Posts class
 */
class PixiGo_Recent_Posts extends WP_Widget {

	/**
	 * Register widget with WordPress.
	 */
	public function __construct() {
		parent::__construct(
			'pixigo_recent_posts',
			esc_html__( 'PixiGo Recent Posts', 'pixigo' ),
			array( 'description' => esc_html__( 'Shows recent posts in cards format', 'pixigo' ) )
		);
	}

	/**
	 * Front-end display of widget.
	 *
	 * @see WP_Widget::widget()
	 *
	 * @param array $args     Widget arguments.
	 * @param array $instance Saved values from database.
	 */
	public function widget( $args, $instance ) {

		echo $args['before_widget']; // WPCS: XSS OK.

		if ( ! empty( $instance['title'] ) ) {
			echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title']; // WPCS: XSS OK.
		}

		if ( empty( $instance['post_count'] ) ) {
			$instance['post_count'] = 4;
		}

		$pixigo_recent_posts = new WP_Query();
		$pixigo_recent_posts->query( 'showposts=' . $instance['post_count'] );

		while ( $pixigo_recent_posts->have_posts() ) :

			$pixigo_recent_posts->the_post(); ?>

			<div class="card horizontal">

				<?php if ( has_post_thumbnail() ) : ?>
					<div class="card-img">
						<a href="<?php the_permalink(); ?>">
							<?php the_post_thumbnail( 'pixigo_sidebar_thumb' ); ?>
						</a>
					</div>
				<?php endif; ?>

				<div class="card-content">

					<h4 class="card-title">
						<a href="<?php esc_url( the_permalink() ); ?>">
							<?php esc_html( the_title() ); ?>
						</a>
					</h4>

					<div class="card-meta">
						<?php pixigo_posted_by(); ?>
						<?php pixigo_posted_on(); ?>
					</div>

				</div>

			</div>


			<?php

		endwhile;

		wp_reset_postdata();

		echo $args['after_widget']; // WPCS: XSS OK.
	}

	/**
	 * Back-end widget form.
	 *
	 * @see WP_Widget::form()
	 *
	 * @param array $instance Previously saved values from database.
	 */
	public function form( $instance ) {
		$title      = ! empty( $instance['title'] ) ? $instance['title'] : esc_html__( 'Recent Posts', 'pixigo' );
		$post_count = ! empty( $instance['post_count'] ) ? $instance['post_count'] : 4;
		?>
		<p>
			<label>
				<?php esc_attr_e( 'Title:', 'pixigo' ); ?>
				<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( $title ); ?>">
			</label>
			<label>
				<?php esc_attr_e( 'No of posts to show:', 'pixigo' ); ?>
				<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'post_count' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'post_count' ) ); ?>" type="number" min="1" max="10" value="<?php echo esc_attr( $post_count ); ?>">
			</label>
		</p>
		<?php
	}

	/**
	 * Sanitize widget form values as they are saved.
	 *
	 * @see WP_Widget::update()
	 *
	 * @param Array $new_data Values just sent to be saved.
	 * @param Array $old_data Previously saved values from database.
	 *
	 * @return Array Updated safe values to be saved.
	 */
	public function update( $new_data, $old_data ) {
		$instance               = array();
		$instance['title']      = ( ! empty( $new_data['title'] ) ) ? sanitize_text_field( $new_data['title'] ) : '';
		$instance['post_count'] = ( ! empty( $new_data['post_count'] ) ) ? $new_data['post_count'] : 4;

		return $instance;
	}

}
