<?php

/*-----------------------------------------------------------------------------------

	Plugin Name: Recent Posts Widget
	Plugin URI: https://www.bloompixel.com
	Description: A widget that displays recent posts.
	Version: 1.0

-----------------------------------------------------------------------------------*/

add_action( 'widgets_init', 'bloomy_recent_posts_widget' );  

// Register Widget
function bloomy_recent_posts_widget() {
    register_widget( 'bloomy_recent_widget' );
}

// Widget Class
class bloomy_recent_widget extends WP_Widget {
    function __construct() {
        parent::__construct(
        // Base ID of your widget
        'bloomy_pp_widget', 

        // Widget name will appear in UI
        esc_html__('(Bloomy) Recent Posts', 'bloomy'), 

        // Widget description
        array( 'description' => esc_html__('A widget that displays the recent posts of your blog', 'bloomy'), ) 
        );
    }
	
	public function widget( $args, $instance ) {
		
		//Our variables from the widget settings.
        $title = ! empty( $instance['title'] ) ? $instance['title'] : esc_html__('Recent Posts', 'bloomy');
        $posts = isset ( $instance['posts'] ) ? intval( $instance['posts'] ) : '4';
		$show_thumb = isset( $instance[ 'show_thumb' ] ) ? esc_attr( $instance[ 'show_thumb' ] ) : 1;
		$show_cat = isset( $instance[ 'show_cat' ] ) ? esc_attr( $instance[ 'show_cat' ] ) : 0;
		$show_author = isset( $instance[ 'show_author' ] ) ? esc_attr( $instance[ 'show_author' ] ) : 0;
		$show_date = isset( $instance[ 'show_date' ] ) ? esc_attr( $instance[ 'show_date' ] ) : 1;
		$show_comments = isset( $instance[ 'show_comments' ] ) ? esc_attr( $instance[ 'show_comments' ] ) : 0;
		$order = isset( $instance['order'] ) ? apply_filters('order', $instance['order'] ) : '';
		$order_by = isset( $instance['order_by'] ) ? apply_filters('order_by', $instance['order_by'] ) : '';
		$widget_style = isset( $instance['widget_style'] ) ? esc_attr( $instance['widget_style'] ) : 'style-one';
        
        $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
		
		// Before Widget
        echo $args['before_widget'];
		$i = 1;
        
		// Display the widget title
		if ( ! empty( $title ) ) {
            echo $args['before_title'] . $title . $args['after_title'];
        }
		?>
		<!-- START WIDGET -->
        <div class="recent-posts-widget recent_posts">
            <ul class="recent-posts">
                <?php
                    $post_args = array(
                        'showposts'             => $posts,
                        'orderby'               => $order_by,
                        'order'                 => $order,
                        'ignore_sticky_posts'   => '1',
                    );
                    $recentposts = new WP_Query( $post_args );

                    if ( $recentposts->have_posts()) : while ( $recentposts->have_posts() ) : $recentposts->the_post(); ?>
                    <li>
                        <?php if ( $show_thumb == 1 ) {
                            if (has_post_thumbnail()):
                                if ( $widget_style == 'style-one' ) { ?>
                                    <div class="thumbnail">
                                        <a class="widgetthumb small-thumb" href='<?php the_permalink(); ?>'>
                                            <?php the_post_thumbnail('bloomy-widgetthumb'); ?>
                                        </a>
                                    </div><?php
                                } else { ?>
                                    <div class="thumbnail-big clearfix thumbnail">
                                        <a class="widgetthumb" href='<?php the_permalink(); ?>'>
                                            <?php the_post_thumbnail('bloomy-widgetthumb-big'); ?>
                                        </a>
                                    </div><?php
                                }
                            endif;
                        } ?>
                        <div class="info">
                            <span class="widgettitle"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></span>
                            <span class="meta">
                                <?php if ( $show_author == 1 ) { ?>
                                    <span class="post-author">
                                        <?php the_author_posts_link(); ?>
                                    </span>
                                <?php } ?>
                                <?php if ( $show_date == 1 ) { ?>
                                    <span class="post-date">
                                        <time datetime="<?php echo esc_attr( get_the_date( 'c' ) ); ?>"> <?php the_time(get_option( 'date_format' )); ?></time>
                                    </span>
                                <?php } ?>
                                <?php if ( $show_cat == 1 ) { ?>
                                    <span class="post-cats">
                                        <?php the_category(', '); ?>
                                    </span>
                                <?php } ?>
                                <?php if ( $show_comments == 1 ) { ?>
                                    <span class="post-comments">
                                        <?php
                                            comments_popup_link( 
                                                esc_html__( '0 Comments', 'bloomy' ), 
                                                esc_html__( '1 Comment', 'bloomy' ), 
                                                esc_html__( '% Comments', 'bloomy' ),
                                                'comments-link',
                                                ''
                                            );
                                        ?>
                                    </span>
                                <?php } ?>
                            </span>
                        </div>
                    </li>
                <?php endwhile; ?>
                <?php wp_reset_postdata(); ?>
                <?php endif; ?>
            </ul>
		</div>
		<!-- END WIDGET -->
		<?php
		
		// After Widget
        echo $args['after_widget'];
	}
	
	// Update the widget
	public function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		
        
        $instance['title'] = sanitize_text_field( $new_instance['title'] );
		$instance['posts'] = absint( $new_instance['posts'] );
        $instance['show_thumb'] = $new_instance['show_thumb'] ? 1 : 0;
        $instance['show_cat'] = $new_instance['show_cat'] ? 1 : 0;
        $instance['show_author'] = $new_instance['show_author'] ? 1 : 0;
        $instance['show_date'] = $new_instance['show_date'] ? 1 : 0;
        $instance['show_comments'] = $new_instance['show_comments'] ? 1 : 0;
		$instance['order'] = stripslashes( $new_instance['order'] );
		$instance['order_by'] = stripslashes( $new_instance['order_by'] );
		$instance['widget_style'] = stripslashes( $new_instance['widget_style'] );
		return $instance;
	}


	//Widget Settings
	public function form( $instance ) {
		//Set up some default widget settings.
		$defaults = array(
			'title'         => esc_html__('Recent Posts', 'bloomy'),
			'posts'         => '4',
			'show_thumb'    => 1,
			'show_cat'      => 0,
			'show_author'   => 0,
			'show_date'     => 1,
			'show_comments' => 0,
		);
		$instance = wp_parse_args( (array) $instance, $defaults );
		$show_thumb = isset( $instance[ 'show_thumb' ] ) ? esc_attr( $instance[ 'show_thumb' ] ) : 1;
		$show_cat = isset( $instance[ 'show_cat' ] ) ? esc_attr( $instance[ 'show_cat' ] ) : 1;
		$show_author = isset( $instance[ 'show_author' ] ) ? esc_attr( $instance[ 'show_author' ] ) : 1;
		$show_comments = isset( $instance[ 'show_comments' ] ) ? esc_attr( $instance[ 'show_comments' ] ) : 1;
		$show_date = isset( $instance[ 'show_date' ] ) ? esc_attr( $instance[ 'show_date' ] ) : 1;
		$order = isset( $instance['order'] ) ? esc_attr( $instance['order'] ) : '';
		$order_by = isset( $instance['order_by'] ) ? esc_attr( $instance['order_by'] ) : '';
		$widget_style = isset( $instance['widget_style'] ) ? esc_attr( $instance['widget_style'] ) : '';

		// Widget Title: Text Input
		?>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e('Title:', 'bloomy'); ?></label>
			<input id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" value="<?php if(!empty($instance['title'])) { echo esc_attr( $instance['title'] ); } ?>" class="widefat" type="text" />
		</p>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'posts' ) ); ?>"><?php esc_html_e('Number of posts to show:','bloomy'); ?></label>
			<input id="<?php echo esc_attr( $this->get_field_id( 'posts' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'posts' ) ); ?>" value="<?php echo intval( $instance['posts'] ); ?>" class="widefat" type="number" />
		</p>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'order' ) ); ?>"><?php esc_html_e( 'Order:','bloomy' ); ?></label> 
			<select id="<?php echo esc_attr( $this->get_field_id( 'order' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'order' ) ); ?>" style="width:100%;" >
				<option value="DESC" <?php selected( $order, 'DESC' ); ?>><?php esc_html_e( 'Descending','bloomy' ); ?></option>
				<option value="ASC" <?php selected( $order, 'ASC' ); ?>><?php esc_html_e( 'Ascending','bloomy' ); ?></option>
			</select>
		</p>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'order_by' ) ); ?>"><?php esc_html_e( 'Orderby:','bloomy' ); ?></label> 
			<select id="<?php echo esc_attr( $this->get_field_id( 'order_by' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'order_by' ) ); ?>" style="width:100%;" >
				<option value="date" <?php selected( $order_by, 'date' ); ?>><?php esc_html_e( 'Date','bloomy' ); ?></option>
				<option value="modified" <?php selected( $order_by, 'modified' ); ?>><?php esc_html_e( 'Last Modified Date','bloomy' ); ?></option>
				<option value="rand" <?php selected( $order_by, 'rand' ); ?>><?php esc_html_e( 'Random','bloomy' ); ?></option>
				<option value="comment_count" <?php selected( $order_by, 'comment_count' ); ?>><?php esc_html_e( 'Comment Count','bloomy' ); ?></option>
				<option value="title" <?php selected( $order_by, 'title' ); ?>><?php esc_html_e( 'Title','bloomy' ); ?></option>
				<option value="ID" <?php selected( $order_by, 'ID' ); ?>><?php esc_html_e( 'Post ID','bloomy' ); ?></option>
				<option value="author" <?php selected( $order_by, 'author' ); ?>><?php esc_html_e( 'Author','bloomy' ); ?></option>
			</select>
		</p>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'widget_style' ) ); ?>"><?php esc_html_e( 'Widget Style:','bloomy' ); ?></label> 
			<select id="<?php echo esc_attr( $this->get_field_id( 'widget_style' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'widget_style' ) ); ?>" style="width:190px;" >
				<option value="style-one" <?php if ($widget_style == 'style-one') echo 'selected="selected"'; ?>><?php esc_html_e( 'Small Thumbnail','bloomy' ); ?></option>
				<option value="style-two" <?php if ($widget_style == 'style-two') echo 'selected="selected"'; ?>><?php esc_html_e( 'Big Thumbnail','bloomy' ); ?></option>
			</select>
		</p>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id("show_thumb") ); ?>">
				<input type="checkbox" class="checkbox" id="<?php echo esc_attr( $this->get_field_id("show_thumb") ); ?>" name="<?php echo esc_attr( $this->get_field_name("show_thumb") ); ?>" value="1" <?php if (isset($instance['show_thumb'])) { checked( 1, esc_attr( $instance['show_thumb'] ), true ); } ?> />
				<?php esc_html_e( 'Show Thumbnails', 'bloomy'); ?>
			</label>
		</p>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id("show_cat") ); ?>">
				<input type="checkbox" class="checkbox" id="<?php echo esc_attr( $this->get_field_id("show_cat") ); ?>" name="<?php echo esc_attr( $this->get_field_name("show_cat") ); ?>" value="1" <?php if (isset($instance['show_cat'])) { checked( 1, esc_attr( $instance['show_cat'] ), true ); } ?> />
				<?php esc_html_e( 'Show Categories', 'bloomy'); ?>
			</label>
		</p>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id("show_author") ); ?>">
				<input type="checkbox" class="checkbox" id="<?php echo esc_attr( $this->get_field_id("show_author") ); ?>" name="<?php echo esc_attr( $this->get_field_name("show_author") ); ?>" value="1" <?php if (isset($instance['show_author'])) { checked( 1, esc_attr( $instance['show_author'] ), true ); } ?> />
				<?php esc_html_e( 'Show Post Author', 'bloomy'); ?>
			</label>
		</p>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id("show_date") ); ?>">
				<input type="checkbox" class="checkbox" id="<?php echo esc_attr( $this->get_field_id("show_date") ); ?>" name="<?php echo esc_attr( $this->get_field_name("show_date") ); ?>" value="1" <?php if (isset($instance['show_date'])) { checked( 1, esc_attr( $instance['show_date'] ), true ); } ?> />
				<?php esc_html_e( 'Show Post Date', 'bloomy'); ?>
			</label>
		</p>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id("show_comments") ); ?>">
				<input type="checkbox" class="checkbox" id="<?php echo esc_attr( $this->get_field_id("show_comments") ); ?>" name="<?php echo esc_attr( $this->get_field_name("show_comments") ); ?>" value="1" <?php if (isset($instance['show_comments'])) { checked( 1, esc_attr( $instance['show_comments'] ), true ); } ?> />
				<?php esc_html_e( 'Show Post Comments', 'bloomy'); ?>
			</label>
		</p>
		<?php
	}
}
?>