<?php
/**
 * Widget for display latest post
 * 
 * @package PureNote
 */

//Exit if accesses directly
if (!defined('ABSPATH')){
	exit;
}

class purenote_Latest_Posts extends WP_Widget{
	/**
	 * Register widget with wordpress.
	 */
	public function __construct(){
		$widget_ops = array(
			'classname' 					=> 'purenote-widget purenote_latest_posts',
			'description'					=> __('DISPLAY LATEST POSTS IN VARIOUS LAYOUTS.', 'purenote'),
			'customize_selective_refresh'	=> true,
		);
		parent::__construct('purenote_latest_posts', __('PureNote: Latest Posts', 'purenote'), $widget_ops);
	}
	/**
     * Helper function that holds widget fields
     * Array is used in update and form functions
     */
    private function widget_fields() {
        
        $fields = array(
            'widget_title' => array(
                'widget_field_name'         => 'widget_title',
                'widget_field_title'        => __( 'Widget Title', 'purenote'),
                'widget_field_default'		=> __('Latest Posts', 'purenote'),
                'widget_field_type'			=> 'title',
                'widget_field_placeholder'	=> __('Widget Title', 'purenote')
            ),
            'posts_query_filter'			=> array(
            	'widget_field_name'			=>'posts_query_filter',
            	'widget_field_title'    => __( 'Posts Query Filter', 'purenote'),
            	'widget_field_default'  => 'latest',
                'widget_field_type'     => 'select',
                'widget_field_options'  => purenote_posts_query_filter_choices()
            ),
            'posts_count' => array(
                'widget_field_name'     => 'posts_count',
                'widget_field_title'    => __( 'No. of posts', 'purenote' ),
                'widget_field_default'  => 5,
                'widget_field_type'     => 'number',
                'input_attr'            => array(
                    'min'   => 1,
                    'max'   => 10,
                    'step'  => 1
                )
            ),
        );

        return $fields;

    }

    /**
     * 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 ) {
    extract( $args );
    if ( empty( $instance ) ) {
        return;
    }

    $widget_title = empty( $instance['widget_title'] ) ? '' : $instance['widget_title'];
    $posts_query_filter = empty( $instance['posts_query_filter'] ) ? 'latest' : $instance['posts_query_filter'];
    $posts_count = empty( $instance['posts_count'] ) ? 5 : $instance['posts_count'];

    $latest_args = array(
        'posts_per_page'      => absint( $posts_count ),
        'ignore_sticky_posts' => true
    );

    if ( 'random' === $posts_query_filter ) {
        $latest_args['orderby'] = 'rand';
    }

    $latest_query = new WP_Query( $latest_args );
    $widget_custom_classes[] = 'latest-posts-wrapper';

    echo $before_widget;
?>
    <div class="purenote-aside <?php echo esc_attr( implode( ' ', $widget_custom_classes ) ); ?>">
        <?php if ( ! empty( $widget_title ) ) : ?>
            <h3 class="widget-title">
                <span><?php echo wp_kses_post( $widget_title ); ?></span>
            </h3>
        <?php endif; ?>

        <div class="posts-wrapper latest-posts">
            <?php
            $total_posts_count = $latest_query->post_count;
            if ( $latest_query->have_posts() ) :
                while ( $latest_query->have_posts() ) : $latest_query->the_post();
                    $current_post = $latest_query->current_post;
                    $post_img = has_post_thumbnail() ? 'has-image' : 'no-image';

                    if ( 0 === $current_post ) {
                        $thumb_size = 'purenote-block-medium';
                    }

                    if ( 1 === $current_post ) {
                       
                        $thumb_size = 'thumbnail';
                    }
            ?>
                    <div class="post-wrap <?php echo esc_attr( $post_img ); ?>">

                        <div class="post-thumbnail-wrap">
                            <?php purenote_post_thumbnail($thumb_size); ?>
                        
                        </div><!-- .post-thumbnail-wrap -->

                        <div class="post-content-wrap">
                            <div class="post-title-wrap">
                                <h3 class="entry-title">
                                    <?php purenote_the_post_categories_list( get_the_ID(), 1 ); ?>
                                    <a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a>
                                </h3>                                                          
                            </div><!-- .post-title-wrap -->
                        </div> <!-- post-content-wrap -->
                    </div><!-- .post-wrap -->

            <?php
                    if ( 0 === $current_post ) {
                    }
                    if ( $total_posts_count === $current_post + 1 && $total_posts_count > 1 ) {
                    }

                    wp_reset_postdata();
                endwhile;
            endif;
            ?>
        </div><!-- .posts-wrapper -->
    </div><!-- .purenote-aside -->
<?php
    echo $after_widget;
}

    /**
     * Sanitize widget form values as they are saved.
     *
     * @see WP_Widget::update()
     *
     * @param   array   $new_instance   Values just sent to be saved.
     * @param   array   $old_instance   Previously saved values from database.
     *
     * @uses    purenote_widget_updated_field_value()      defined in purenote-widgets-helper.php
     *
     * @return  array Updated safe values to be saved.
     */
    public function update( $new_instance, $old_instance ) {
        $instance = $old_instance;

        $widget_fields = $this->widget_fields();

        // Loop through fields
        foreach ( $widget_fields as $widget_field ) {

            extract( $widget_field );

            // Use helper function to get updated field values
            $instance[$widget_field_name] = purenote_widget_updated_field_value($widget_field, $new_instance[$widget_field_name]);
            }
            return $instance;                      
        }

    /**
     * Back-end widget form.
     *
     * @see WP_Widget::form()
     *
     * @param   array $instance Previously saved values from database.
     *
     * @uses  purenote_show_widget_field() defined in purenote-widgets-helper.php
     */
    public function form( $instance ) {

        $widget_fields = $this->widget_fields();

        // Loop through fields
        foreach ( $widget_fields as $widget_field ) {

            // Make array elements available as variables
            extract( $widget_field );

            if ( empty( $instance ) && isset( $widget_field_default ) ) {
                $widget_field_value = $widget_field_default;
            } elseif ( empty( $instance ) ) {
                $widget_field_value = '';
            } else {
                $widget_field_value = $instance[$widget_field_name];
            }
            purenote_show_widget_field( $this, $widget_field, $widget_field_value );
        }
    }
} //end Class purenote_Latest_Posts