<?php
/*-----------------------------------------------------------------------------------

	Plugin Name: MyThemeShop Recent Posts
	Version: 2.0.2

-----------------------------------------------------------------------------------*/
if ( ! class_exists( 'mts_recent_posts_widget' ) ) {
	class mts_recent_posts_widget extends WP_Widget {

		public function __construct() {
			parent::__construct(
				'mts_recent_posts_widget',
				sprintf( __( '%sRecent Posts', 'seekers' ), MTS_THEME_WHITE_LABEL ? '' : 'MTS ' ),
				array( 'description' => __( 'Display the most recent posts from all categories', 'seekers' ) )
			);
			add_action( 'admin_enqueue_scripts', array( $this, 'widget_scripts' ) );
		}

		/**
		 * Enqueue Widget Scripts
		 *
		 * @param $hook
		 */
		function widget_scripts( $hook ) {
			if ( 'widgets.php' !== $hook ) {
				return;
			}
			wp_enqueue_script( 'switch-layout', trailingslashit( get_template_directory_uri() ) . '/includes/widgets/js/switch-layout.js', false );
		}

		public function form( $instance ) {
			$defaults       = array(
				'title_length'   => 8,
				'category'       => 1,
				'author_name'    => 1,
				'date'           => 1,
				'show_thumb2'    => 1,
				'box_layout'     => 'vertical-small',
				'border_radius'  => 0,
				'show_excerpt'   => 0,
				'excerpt_length' => 10,
			);
			$instance       = wp_parse_args( (array) $instance, $defaults );
			$title          = isset( $instance['title'] ) ? $instance['title'] : __( 'Recent Posts', 'seekers' );
			$qty            = isset( $instance['qty'] ) ? esc_attr( $instance['qty'] ) : 3;
			$title_length   = isset( $instance['title_length'] ) ? intval( $instance['title_length'] ) : 7;
			$category       = isset( $instance['category'] ) ? intval( $instance['category'] ) : 0;
			$author_name    = isset( $instance['author_name'] ) ? esc_attr( $instance['author_name'] ) : 1;
			$show_excerpt   = isset( $instance['show_excerpt'] ) ? esc_attr( $instance['show_excerpt'] ) : 1;
			$date           = isset( $instance['date'] ) ? esc_attr( $instance['date'] ) : 1;
			$excerpt_length = isset( $instance['excerpt_length'] ) ? intval( $instance['excerpt_length'] ) : 10;
			$show_thumb2    = isset( $instance['show_thumb2'] ) ? esc_attr( $instance['show_thumb2'] ) : 1;
			$box_layout     = $instance['box_layout'];
			$border_radius  = isset( $instance['border_radius'] ) ? intval( $instance['border_radius'] ) : 0;
			?>
			<p>
				<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'seekers' ); ?></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 esc_attr( $title ); ?>" />
			</p>

			<p>
				<label for="<?php echo $this->get_field_id( 'qty' ); ?>"><?php _e( 'Number of Posts to show', 'seekers' ); ?></label>
				<input id="<?php echo $this->get_field_id( 'qty' ); ?>" name="<?php echo $this->get_field_name( 'qty' ); ?>" type="number" class="small-text" min="1" step="1" value="<?php echo esc_attr( $qty ); ?>" />
			</p>

			<p>
				<label for="<?php echo $this->get_field_id( 'title_length' ); ?>"><?php _e( 'Title Length:', 'seekers' ); ?>
				<input id="<?php echo $this->get_field_id( 'title_length' ); ?>" name="<?php echo $this->get_field_name( 'title_length' ); ?>" type="number" class="small-text" min="1" step="1" value="<?php echo esc_attr( $title_length ); ?>" />
				</label>
			</p>

			<p>
				<label for="<?php echo $this->get_field_id( 'show_thumb2' ); ?>">
					<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'show_thumb2' ); ?>" name="<?php echo $this->get_field_name( 'show_thumb2' ); ?>" value="1" <?php if ( isset( $instance['show_thumb2'] ) ) { checked( 1, $instance['show_thumb2'], true ); } ?> />
					<?php _e( 'Show Thumbnails', 'seekers' ); ?>
				</label>
			</p>

			<p class="post-box-layout">
				<label for="<?php echo $this->get_field_id( 'box_layout' ); ?>"><?php _e('Posts layout:', 'seekers' ); ?></label>
				<select id="<?php echo $this->get_field_id( 'box_layout' ); ?>" name="<?php echo $this->get_field_name( 'box_layout' ); ?>">
					<option value="horizontal-small" <?php selected( $box_layout, 'horizontal-small', true ); ?>><?php _e( 'Horizontal', 'seekers' ); ?></option>
					<option value="vertical-small" <?php selected( $box_layout, 'vertical-small', true ); ?>><?php _e( 'Vertical', 'seekers' ); ?></option>
				</select>
			</p>

			<p class="post-border-radius">
				<label for="<?php echo $this->get_field_id( 'border_radius' ); ?>"><?php _e( 'Thumb Border Radius: ', 'seekers' ); ?></label>
				<input id="<?php echo $this->get_field_id( 'border_radius' ); ?>" name="<?php echo $this->get_field_name( 'border_radius' ); ?>" type="number" class="small-text" step="1" value="<?php echo esc_attr( $border_radius ); ?>" />
			</p>

			<p>
				<label for="<?php echo $this->get_field_id( 'category' ); ?>">
					<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'category' ); ?>" name="<?php echo $this->get_field_name( 'category' ); ?>" value="1" <?php checked( 1, $instance['category'], true ); ?> />
					<?php _e( 'Show posts categories', 'seekers' ); ?>
				</label>
			</p>

			<p>
				<label for="<?php echo $this->get_field_id( 'author_name' ); ?>">
					<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'author_name' ); ?>" name="<?php echo $this->get_field_name( 'author_name' ); ?>" value="1" <?php checked( 1, $instance['author_name'], true ); ?> />
					<?php _e( 'Show author name with link', 'seekers' ); ?>
				</label>
			</p>

			<p>
				<label for="<?php echo $this->get_field_id( 'date' ); ?>">
					<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'date' ); ?>" name="<?php echo $this->get_field_name( 'date' ); ?>" value="1" <?php checked( 1, $instance['date'], true ); ?> />
					<?php _e( 'Show post date', 'seekers' ); ?>
				</label>
			</p>

			<p>
				<label for="<?php echo $this->get_field_id( 'show_excerpt' ); ?>">
					<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'show_excerpt' ); ?>" name="<?php echo $this->get_field_name( 'show_excerpt' ); ?>" value="1" <?php checked( 1, $instance['show_excerpt'], true ); ?> />
					<?php _e( 'Show excerpt', 'seekers' ); ?>
				</label>
			</p>

			<p>
				<label for="<?php echo $this->get_field_id( 'excerpt_length' ); ?>"><?php _e( 'Excerpt Length:', 'seekers' ); ?>
				<input id="<?php echo $this->get_field_id( 'excerpt_length' ); ?>" name="<?php echo $this->get_field_name( 'excerpt_length' ); ?>" type="number" class="small-text" min="1" step="1" value="<?php echo esc_attr( $excerpt_length ); ?>" />
				</label>
			</p>

			<?php
		}

		public function update( $new_instance, $old_instance ) {
			$instance                   = array();
			$instance['title']          = strip_tags( $new_instance['title'] );
			$instance['qty']            = intval( $new_instance['qty'] );
			$instance['title_length']   = intval( $new_instance['title_length'] );
			$instance['category']       = intval( $new_instance['category'] );
			$instance['author_name']    = intval( $new_instance['author_name'] );
			$instance['date']           = intval( $new_instance['date'] );
			$instance['show_thumb2']    = intval( $new_instance['show_thumb2'] );
			$instance['box_layout']     = $new_instance['box_layout'];
			$instance['border_radius']  = intval( $new_instance['border_radius'] );
			$instance['show_excerpt']   = isset( $new_instance['show_excerpt'] ) ? intval( $new_instance['show_excerpt'] ) : 0;
			$instance['excerpt_length'] = intval( $new_instance['excerpt_length'] );
			return $instance;
		}

		public function widget( $args, $instance ) {
			extract( $args );
			$title          = apply_filters( 'widget_title', $instance['title'] );
			$title_length   = $instance['title_length'];
			$category       = $instance['category'];
			$author_name    = $instance['author_name'];
			$date           = $instance['date'];
			$qty            = (int) $instance['qty'];
			$show_thumb2    = (int) $instance['show_thumb2'];
			$box_layout     = isset( $instance['box_layout'] ) ? $instance['box_layout'] : 'horizontal-small';
			$border_radius  = esc_attr( $instance['border_radius'] ) . 'px';
			$show_excerpt   = $instance['show_excerpt'];
			$excerpt_length = $instance['excerpt_length'];

			$before_widget = preg_replace( '/class="([^"]+)"/i', 'class="$1 ' . ( isset( $instance['box_layout'] ) ? $instance['box_layout'] : 'horizontal-small' ) . '"', $before_widget ); // Add horizontal/vertical class to widget
			echo $before_widget;
			if ( ! empty( $title ) ) echo $before_title . $title . $after_title;
			echo self::get_cat_posts( $qty, $title_length, $category, $author_name, $date, $show_thumb2, $box_layout, $border_radius, $show_excerpt, $excerpt_length );
			echo $after_widget;
		}

		public function get_cat_posts( $qty, $title_length, $category, $author_name, $date, $show_thumb2, $box_layout, $border_radius, $show_excerpt, $excerpt_length ) {

			$no_image = ( $show_thumb2 ) ? '' : ' no-thumb';

			if ( 'horizontal-small' === $box_layout ) {
				$thumbnail     = 'widgetthumb';
				$open_li_item  = '<li class="post-box horizontal-small horizontal-container'.$no_image.'"><div class="horizontal-container-inner">';
				$close_li_item = '</div></li>';
			} else {
				$thumbnail     = 'widgetfull';
				$open_li_item  = '<li class="post-box vertical-small'.$no_image.'">';
				$close_li_item = '</li>';
			}

			$posts = new WP_Query( array(
				'orderby' => 'date',
				'order' => 'DESC',
				'posts_per_page' => $qty,
				'ignore_sticky_posts' => true,
				'no_found_rows' => true,
				'post_status' => 'publish',
			) );

			echo '<ul class="advanced-recent-posts">';

			while ( $posts->have_posts() ) { $posts->the_post(); ?>
				<?php echo $open_li_item; ?>
					<?php if ( $show_thumb2 == 1 ) : ?>
					<div class="post-img">
						<a href="<?php the_permalink() ?>" title="<?php the_title_attribute() ?>">
							<?php if ( has_post_thumbnail() ) { ?>
								<?php the_post_thumbnail( 'seekers-' . $thumbnail, array( 'title' => '' ) ); ?>
							<?php } else { ?>
								<img class="wp-post-image" src="<?php echo get_template_directory_uri() . '/images/nothumb-seekers-' . $thumbnail . '.png'; ?>" alt="<?php the_title_attribute() ?>"/>
							<?php } ?>
						</a>
					</div>
					<?php endif; ?>
					<div class="post-data">
						<div class="post-data-container">
							<?php
							if ( 1 === $category ) :
								printf( '<div class="post-info top"><span class="thecategory">%s</span></div>', seekers_get_the_category( ' ' ) );
							endif;
							?>
							<div class="post-title">
								<a href="<?php the_permalink() ?>" title="<?php the_title_attribute() ?>"><?php echo esc_html( seekers_truncate( get_the_title(), $title_length, 'words' ) ); ?></a>
							</div>
							<?php if ( 1 === $date || 1 === $author_name ) : ?>
							<div class="post-info">
								<?php
								if ( 1 === $author_name ) :
									printf( '<span class="theauthor"><span>%s</span></span>', get_the_author_posts_link() );
								endif;
								if ( 1 === $date ) :
									printf( '<span class="thetime updated">%s</span>', get_the_time( get_option( 'date_format' ) ) );
								endif;
								?>
							</div> <!--.post-info-->
							<?php endif; ?>
							<?php if ( 1 === $show_excerpt ) : ?>
							<div class="post-excerpt">
								<?php echo seekers_excerpt( $excerpt_length ); ?>
							</div>
							<?php endif; ?>
						</div>
					</div>
				<?php echo $close_li_item; ?>
			<?php }
			wp_reset_postdata();
			echo '</ul>'."\r\n"; ?>

			<style>
				<?php echo '#' . $this->id . ' .horizontal-small img { border-radius:' . $border_radius . ' }'; ?>
			</style>

		<?php }

	}
}
// Register widget
add_action( 'widgets_init', 'register_mts_recent_posts_widget' );
function register_mts_recent_posts_widget() {
	register_widget( 'mts_recent_posts_widget' );
}
