<?php
/**
 * Class for adding Recent Post Widget
 *
 * @package Canyon Themes
 * @subpackage Sewa
 * @since 1.0.0
 */
if (!class_exists('Sewa_Recent_Post_Widget')) {
 
    class Sewa_Recent_Post_Widget extends WP_Widget
 
    {
        /*defaults values for fields*/
        private function defaults() 
        {
            $defaults       = array(
                'cat_id'    => -1,
                'title'     => esc_html__('Recent Posts','sewa'),
                'sub_title' => esc_html__('Read Our Recent Updates','sewa'),
                'read_more' => esc_html__('Read More','sewa'),
                'meta_options' => 0,
                'post_number' => 3, 
            );

            return $defaults;
        }

     public function __construct()
        {
            parent::__construct(
                /* Widget Unique ID */
                'sewa-recent-post-widget',
                 /* Widget Name */
                esc_html__( 'Sewa Recent Post Widget', 'sewa' ),
                 /* Widget Description */
                array( 'description' => esc_html__( 'Sewa Recent Post Section', 'sewa' ) )
            );
        }

        /**
         * Function to Creating widget front-end. This is where the action happens
         *
         * @access public
         * @since 1.0
         *
         * @param array $args widget setting
         * @param array $instance saved values
         *
         * @return void
         *
         */
        public function widget( $args, $instance )
        {

            if ( !empty( $instance ) ) {
                
                $instance = wp_parse_args( (array ) $instance, $this->defaults() );
                
                echo $args['before_widget'];
                $catid        = absint( $instance['cat_id'] );
                $title        = apply_filters('widget_title', !empty($instance['title']) ? esc_html( $instance['title']): '', $instance, $this->id_base);
                $subtitle     =  esc_html( $instance['sub_title'] );
                $read_more    =  esc_html( $instance['read_more'] );
                $meta_options = esc_attr( $instance['meta_options'] );
                $post_number  = absint( $instance['post_number'] );

                ?>
                <section id="section14" class="section-margine section14 front-blog">
                    
                    <div class="container">
                    
                        <div class="row">
                    
                            <div class="col-md-12">
                    
                                <div class="section-title-wrap text-center">
                                    <div class="section-title">
                        
                                        <?php
                        
                                        if ( !empty ( $title ) ) {
                                            
                                            ?>
                                            <h2><?php echo $args['before_title'] . $title . $args['after_title']; ?></h2>

                                        <?php }

                                        if ( !empty( $subtitle ) )
                                            
                                         {
                                            ?>
                                            <h6><?php echo $subtitle; ?></h6>
                                        
                                        <?php } ?>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="row">
                            <?php
                            $i = 0;
                            $sticky = get_option( 'sticky_posts' );
                            if ($catid != -1) {
                                $home_recent_post_section = array(
                                    'ignore_sticky_posts' => true,
                                    'post__not_in'        => $sticky,
                                    'cat'                 => $catid,
                                    'posts_per_page'      => $post_number,
                                    'order'               => 'DESC'
                                );
                            } else {
                                $home_recent_post_section = array(
                                    'ignore_sticky_posts' => true,
                                    'post__not_in'        => $sticky,
                                    'post_type'           => 'post',
                                    'posts_per_page'      => $post_number,
                                    'order'               => 'DESC'
                                );
                            }

                            $home_recent_post_section_query = new WP_Query($home_recent_post_section);

                            if ( $home_recent_post_section_query->have_posts() ) {
                                
                                while ($home_recent_post_section_query->have_posts()) {
                                    
                                    $home_recent_post_section_query->the_post();
                                    
                                    ?>
                                    <div class="col-md-4">
                                        <div class="section-14-box <?php if ( !has_post_thumbnail() ) {
                                            echo "no-image";
                                        } ?> " data-wow-delay="<?php echo esc_attr($i); ?>s">

                                            <?php
                                            
                                            if (has_post_thumbnail()) {
                                            
                                                $image_id = get_post_thumbnail_id();
                                            
                                                $image_url = wp_get_attachment_image_src($image_id, 'full', true);
                                                ?>
                                                <img src="<?php echo esc_url($image_url[0]); ?>" class="img-responsive">
                                            <?php }
                                            ?>
                                            <div class="entry-box">
                                                <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                                                <?php if( $meta_options == 1 ) : ?>
                                                <div class="meta-options">
                                                    <div class="date">
                                                        <span><?php echo esc_html( get_the_date('M') ); ?></span>
                                                        <span><?php echo esc_html( get_the_date('d') ); ?></span>
                                                        <span><?php echo esc_html( get_the_date('Y') ); ?></span>
                                                    </div>
                                                    <div class="text-left author-post">
                                                        <a href="<?php echo esc_url( get_author_posts_url( get_the_author_meta('ID') ) ); ?> ">
                                                            <?php the_author(); ?>
                                                        </a>
                                                    </div>
                                                </div>
                                            <?php endif; ?>
                                                <?php the_excerpt(); ?>
                                                <?php if(!empty($read_more)){ ?>
                                                <a href="<?php the_permalink(); ?>" class="btn btn-primary">
                                                    <?php echo esc_html($read_more); ?>
                                                </a>
                                                <?php } ?>
                                            </div>
                                        </div>
                                    </div>
                                     <?php
                                    $i++;
                                }
                            }
                            wp_reset_postdata();
                            ?>
                        </div>
                    </div>
                </section>
                <?php
                echo $args['after_widget'];
            }
        }
        /**
         * Function to Updating widget replacing old instances with new
         *
         * @access public
         * @since 1.0
         *
         * @param array $new_instance new arrays value
         * @param array $old_instance old arrays value
         *
         * @return array
         *
         */
        public function update($new_instance, $old_instance)
        {
            $instance              = $old_instance;
            $instance['cat_id']    = (isset( $new_instance['cat_id'] ) ) ? absint($new_instance['cat_id']) : '';
            $instance['title']     = sanitize_text_field( $new_instance['title'] );
            $instance['sub_title'] = sanitize_text_field( $new_instance['sub_title'] );
            $instance['read_more'] = sanitize_text_field( $new_instance['read_more'] );
            $instance['post_number']    = (isset( $new_instance['post_number'] ) ) ? absint($new_instance['post_number']) : '';
            $instance['meta_options'] = isset($new_instance['meta_options'])? 1 : 0; 

            return $instance;

        }
        /*Widget Backend*/
        public function form( $instance )
        {
            $instance  = wp_parse_args( (array ) $instance, $this->defaults() );
            $catid     = esc_attr( $instance['cat_id'] );
            $title     = esc_attr( $instance['title'] );
            $subtitle  = esc_attr( $instance['sub_title'] );
            $read_more = esc_attr( $instance['read_more'] );
            $post_number = esc_attr( $instance['post_number'] );
            $meta_options     = esc_attr( $instance['meta_options'] );

            ?>

            <p>
                <label for="<?php echo esc_attr($this->get_field_id('title')); ?>">
                    <?php esc_html_e('Title', 'sewa'); ?>
                </label><br/>
                <input type="text" name="<?php echo esc_attr( $this->get_field_name('title') ); ?>" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title') ); ?>" value="<?php echo $title; ?>">
            </p>

            <p>
                <label for="<?php echo esc_attr( $this->get_field_id('sub_title') ); ?>">
                    <?php esc_html_e( 'Sub Title', 'sewa'); ?>
                </label><br/>
                <textarea class="widefat" rows="5" cols="15" name="<?php echo esc_attr($this->get_field_name('sub_title')); ?>" class="widefat" id="<?php echo esc_attr($this->get_field_id('sub_title')); ?>" value="<?php echo $subtitle; ?>"><?php echo $subtitle; ?>
                </textarea>
            </p>

            <p>
                <label for="<?php echo esc_attr( $this->get_field_id('cat_id') ); ?>">
                    <?php esc_html_e('Select Category', 'sewa'); ?>
                </label><br/>
                <?php
                $sewa_con_dropown_cat = array(
                    'show_option_none' => esc_html__('From Recent Posts', 'sewa'),
                    'orderby'          => 'name',
                    'order'            => 'asc',
                    'show_count'       => 1,
                    'hide_empty'       => 1,
                    'echo'             => 1,
                    'selected'         => $catid,
                    'hierarchical'     => 1,
                    'name'             => esc_attr( $this->get_field_name('cat_id') ),
                    'id'               => esc_attr( $this->get_field_name('cat_id') ),
                    'class'            => 'widefat',
                    'taxonomy'         => 'category',
                    'hide_if_empty'    => false,
                );

                wp_dropdown_categories( $sewa_con_dropown_cat );
                
                ?>
            </p>
            <p>
                <label for="<?php echo esc_attr( $this->get_field_id('read_more') ); ?>">
                    <?php esc_html_e( 'Read More Text', 'sewa'); ?>
                </label><br/>
                <input type="text" name="<?php echo esc_attr($this->get_field_name('read_more')); ?>" class="widefat" id="<?php echo esc_attr($this->get_field_id('read_more')); ?>" value="<?php echo $read_more; ?>">
            </p>
            <p>
            <label for="<?php echo esc_attr( $this->get_field_id('post_number') ); ?>">
                    <?php esc_html_e( 'Number of Post ', 'sewa'); ?>
                </label><br/>
                <input type="number" name="<?php echo esc_attr($this->get_field_name('post_number')); ?>" class="widefat" id="<?php echo esc_attr($this->get_field_id('post_number')); ?>" value="<?php echo $post_number; ?>">
            </p>
            <p>
                <label for="<?php echo esc_attr( $this->get_field_id('meta_options')); ?>">
                        <?php esc_html_e('Show/Hide Meta Tags', 'sewa'); ?>
                </label>
                <input class="widefat" id="<?php echo  esc_attr( $this->get_field_id( 'meta_options' ) ); ?>" name="<?php echo  esc_attr( $this->get_field_name( 'meta_options' ) ); ?>" type="checkbox" value="<?php echo esc_attr( $meta_options ); ?>" <?php checked( 1, esc_attr( $meta_options ), 1 ); ?>/>
            </p>

            <?php
        }
    }
}

add_action('widgets_init', 'sewa_recent_post_widget');

function sewa_recent_post_widget()

{
    register_widget('Sewa_Recent_Post_Widget');

}