<?php

class Good_Widget_Recent_Posts extends WP_Widget {

	function __construct() {
		parent::__construct( 'good_recent_posts', __( 'Good - Recent Posts', 'good' ), array(
			'classname'   => 'widget_posts_wrap',
			'description' => __( 'Good - Recent Posts', 'good' )
		) );

		add_filter( 'query_vars', array( $this, 'register_query_var' ) );
	}

	public function widget( $args, $instance ) {
		$default      = array(
			'widget_title' => __( 'Recent Posts', 'good' ),
			'cats'         => '',
			'tags'         => '',
			'quantity'     => 5,
			'order'        => 'date',
			'display'      => 'two-columns',
			'not_in'       => ''
		);
		$instance     = wp_parse_args( $instance, $default );
		$widget_title = apply_filters( 'widget_title', $instance['widget_title'] );
		$cats         = $instance['cats'];
		$tags         = $instance['tags'];
		$quantity     = $instance['quantity'];
		$order        = $instance['order'];
		$display      = $instance['display'];
		$not_in       = $instance['not_in'];
		$time         = $instance['time'];

		if ( is_array( $cats ) ) {
			$cats = implode( ',', $cats );
		}
		if ( is_array( $tags ) ) {
			$tags = implode( ',', $tags );
		}

		echo $args['before_widget'];

		if ( ! empty( $instance['widget_title'] ) ) {
			echo $args['before_title'] . $widget_title . $args['after_title'];
		} else {
			echo '<hr class="no-title">';
		}

		$paged      = ( get_query_var( 'posts_widget_page' ) ) ? get_query_var( 'posts_widget_page' ) : 1;
		$query_args = array(
			'cat'                 => $cats,
			'tag_slug__in'        => $tags,
			'orderby'             => $order,
			'posts_per_page'      => $quantity,
			'paged'               => $paged,
			'post_status'         => 'publish',
			'post__not_in'        => $not_in,
			'ignore_sticky_posts' => 1,
		);

		$today = getdate();

		if ( $time == '2' ) {
			$query_args['year']     = $today["year"];
			$query_args['monthnum'] = $today["mon"];
			$query_args['day']      = $today["mday"];
		}
		if ( $time == '3' ) {
			$query_args['date_query'] = Array( 'year' => $today["year"], 'week' => date( 'W' ) );
		}
		if ( $time == '4' ) {
			$query_args['year']     = $today["year"];
			$query_args['monthnum'] = $today["mon"];
		}

		$recent = new WP_Query( $query_args );

		if ( isset( $args['widget_position'] ) && $args['widget_position'] == 'sidebar' ) {
			$display_view = 'no-thumbnail';
		} else {
			$display_view = ! empty( $display ) ? $display : 'two-columns';
		}

		if ( ! $recent->have_posts() ) {
			return;
		} ?>
		<div
			class="site-content group posts-block <?php echo esc_attr( $display_view ); ?> <?php echo 'sort-' . esc_attr( $order ); ?>">
			<?php
			while ( $recent->have_posts() ) : $recent->the_post();

				if ( isset( $args['widget_position'] ) && $args['widget_position'] == 'sidebar' ) {
					get_template_part( 'template-parts/content-widget', 'no-thumbnail' );
				} else {
					get_template_part( 'template-parts/content-widget', $display_view );
				}

			endwhile;

			wp_reset_query();
			?>
		</div>
		<?php

		if ( $instance['pagination'] == 'more' && isset( $args['widget_position'] ) && $args['widget_position'] !== 'sidebar' ) {
			$max_pages = $recent->max_num_pages;

			if ( ( $max_pages > $paged ) && ( $max_pages > 1 ) ) {
				echo '<nav class="page-navigation load-more">';
				echo '<a href="' . add_query_arg( 'posts_widget_page', $paged + 1 ) . '">' . __( 'Load More', 'good' ) . '</a>';
				echo '</nav>';
			}
		}

		echo $args['after_widget'];
	}

	public function update( $new_instance, $old_instance ) {
		$instance                 = $old_instance;
		$instance['widget_title'] = strip_tags( $new_instance['widget_title'] );
		$instance['cats']         = $new_instance['cats'];
		$instance['tags']         = $new_instance['tags'];
		$instance['quantity']     = strip_tags( $new_instance['quantity'] );
		$instance['order']        = strip_tags( $new_instance['order'] );
		$instance['time']         = strip_tags( $new_instance['time'] );
		$instance['pagination']   = $new_instance['pagination'];
		$instance['display']      = strip_tags( $new_instance['display'] );

		$default = array(
			'widget_title' => __( 'Recent Posts', 'good' ),
			'cats'         => '',
			'tags'         => '',
			'quantity'     => 5,
			'order'        => 'date',
			'display'      => 'two-columns'
		);

		$instance = wp_parse_args( $instance, $default );

		return $instance;
	}

	public function form( $instance ) {
		$default      = array(
			'widget_title' => __( 'Recent Posts', 'good' ),
			'title'        => '',
			'cats'         => '',
			'tags'         => '',
			'quantity'     => 5,
			'order'        => 'date',
			'display'      => 'two-columns',
			'pagination'   => ''
		);
		$instance     = wp_parse_args( $instance, $default );
		$widget_title = $instance['widget_title'];
		$cats         = $instance['cats'];
		$tags         = $instance['tags'];
		$quantity     = $instance['quantity'];
		$order        = $instance['order'];
		$display      = $instance['display'];
		$pagination   = $instance['pagination'];
		?>

		<p>
			<?php _e( 'Widget title : ', 'good' ); ?>
			<input class="widefat" type="text" name="<?php echo esc_attr( $this->get_field_name( 'widget_title' ) ); ?>"
			       value="<?php echo esc_attr( $widget_title ); ?>"/>
		</p>
		<p>
			<?php _e( 'Category filter :', 'good' ); ?>
			<select name="<?php echo esc_attr( $this->get_field_name( 'cats' ) ); ?>[]" class="widefat"
			        multiple="multiple">
				<?php
				$of_categories_obj = get_categories();
				foreach ( $of_categories_obj as $of_cat ) {
					?>
					<option
						value="<?php echo intval( $of_cat->cat_ID ); ?>" <?php if ( is_array( $cats ) && in_array( $of_cat->cat_ID, $cats ) ) {
						echo 'selected';
					} ?>><?php echo $of_cat->cat_name; ?></option>
					<?php
				}
				?>
			</select>
		</p>
		<p>
			<?php _e( 'Tags filter :', 'good' ); ?>
			<select name="<?php echo esc_attr( $this->get_field_name( 'tags' ) ); ?>[]" class="widefat" multiple>
				<?php
				$of_tags_obj = get_tags();
				foreach ( $of_tags_obj as $of_tag ) {
					?>
					<option
						value="<?php echo esc_attr( $of_tag->slug ); ?>" <?php if ( is_array( $tags ) && in_array( $of_tag->slug, $tags ) ) {
						echo 'selected';
					} ?>><?php echo $of_tag->name; ?></option>
					<?php
				}
				?>
			</select>
		</p>
		<p>
			<?php _e( 'Number of posts :', 'good' ); ?>
			<select name="<?php echo esc_attr( $this->get_field_name( 'quantity' ) ); ?>">
				<option
					value="1" <?php if ( $quantity == "1" ): ?> selected <?php endif; ?>><?php echo number_format_i18n( 1 ); ?></option>
				<option
					value="2" <?php if ( $quantity == "2" ): ?> selected <?php endif; ?>><?php echo number_format_i18n( 2 ); ?></option>
				<option
					value="3" <?php if ( $quantity == "3" ): ?> selected <?php endif; ?>><?php echo number_format_i18n( 3 ); ?></option>
				<option
					value="4" <?php if ( $quantity == "4" ): ?> selected <?php endif; ?>><?php echo number_format_i18n( 4 ); ?></option>
				<option
					value="5" <?php if ( $quantity == "5" ): ?> selected <?php endif; ?>><?php echo number_format_i18n( 5 ); ?></option>
				<option
					value="6" <?php if ( $quantity == "6" ): ?> selected <?php endif; ?>><?php echo number_format_i18n( 6 ); ?></option>
				<option
					value="7" <?php if ( $quantity == "7" ): ?> selected <?php endif; ?>><?php echo number_format_i18n( 7 ); ?></option>
				<option
					value="8" <?php if ( $quantity == "8" ): ?> selected <?php endif; ?>><?php echo number_format_i18n( 8 ); ?></option>
				<option
					value="9" <?php if ( $quantity == "9" ): ?> selected <?php endif; ?>><?php echo number_format_i18n( 9 ); ?></option>
				<option
					value="10" <?php if ( $quantity == "10" ): ?> selected <?php endif; ?>><?php echo number_format_i18n( 10 ); ?></option>
				<option
					value="11" <?php if ( $quantity == "11" ): ?> selected <?php endif; ?>><?php echo number_format_i18n( 11 ); ?></option>
				<option
					value="12" <?php if ( $quantity == "12" ): ?> selected <?php endif; ?>><?php echo number_format_i18n( 12 ); ?></option>
				<option
					value="13" <?php if ( $quantity == "13" ): ?> selected <?php endif; ?>><?php echo number_format_i18n( 13 ); ?></option>
				<option
					value="14" <?php if ( $quantity == "14" ): ?> selected <?php endif; ?>><?php echo number_format_i18n( 14 ); ?></option>
				<option
					value="15" <?php if ( $quantity == "15" ): ?> selected <?php endif; ?>><?php echo number_format_i18n( 15 ); ?></option>
			</select>
		</p>
		<p>
			<input type="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'pagination' ) ); ?>"
			       id="<?php echo esc_attr( $this->get_field_name( 'pagination' ) ); ?>"
			       value="more" <?php checked( $pagination, 'more', true ); ?> />
			<label
				for="<?php echo esc_attr( $this->get_field_name( 'pagination' ) ); ?>"><?php _e( 'Load more button', 'good' ); ?></label><br>
		</p>
		<p>
			<?php _e( 'Order posts by:', 'good' ); ?>
			<select name="<?php echo esc_attr( $this->get_field_name( 'order' ) ); ?>">
				<option value="date" <?php selected( $order, 'date', true ); ?>><?php _e( 'Date', 'good' ); ?></option>
				<option
					value="rand" <?php selected( $order, 'rand', true ); ?>><?php _e( 'Random', 'good' ); ?></option>
				<option
					value="comment_count" <?php selected( $order, 'comment_count', true ); ?>><?php _e( 'Popular', 'good' ); ?></option>
			</select>
		</p>
		<p>
			<?php _e( 'Display type:', 'good' ); ?>
			<select name="<?php echo esc_attr( $this->get_field_name( 'display' ) ); ?>">
				<option
					value="two-columns" <?php selected( $display, 'two-columns', true ); ?>><?php _e( 'Two columns', 'good' ); ?></option>
				<option
					value="three-columns" <?php selected( $display, 'three-columns', true ); ?>><?php _e( 'Three columns', 'good' ); ?></option>
				<option
					value="small-thumbnail" <?php selected( $display, 'small-thumbnail', true ); ?>><?php _e( 'Two columns with small thumbnail', 'good' ); ?></option>
			</select>
		</p>
		<?php
	}

	public function register_query_var( $vars ) {
		$vars[] = "posts_widget_page";

		return $vars;
	}
}
