<?php
/**
 * Recent Posts Widget
 *
 * @package Theme Palace
 * @subpackage FavBlog
 * @since FavBlog 1.0.0
 */

if ( ! class_exists( 'favblog_Recent_Post' ) ) :

     
    class favblog_Recent_Post extends WP_Widget {
        /**
         * Sets up the widgets name etc
         */
        public $default_title   = '';

        public function __construct() {
            $tp_widget_recent_post = array(
                'description' => esc_html__( 'Retrive Recent posts.', 'favblog' ),
            );
            $this->default_title = __( 'Top Posts &amp; Pages', 'favblog' );

            parent::__construct( 'favblog_recent_post', esc_html__( 'TP : Recent Posts', 'favblog' ), $tp_widget_recent_post );
        }



        function form( $instance ) {
            $instance = wp_parse_args( (array) $instance, $this->defaults() );
    
            if ( false === $instance['title'] ) {
                $instance['title'] = $this->default_title;
            }
            $title = stripslashes( $instance['title'] );
            $subtitle = isset( $instance['subtitle'] ) ?  $instance['subtitle'] : esc_html__( 'Sub Title ', 'favblog' ) ;
    
            $count = isset( $instance['count'] ) ? (int) $instance['count'] : 10;
            if ( $count < 1 || 10 < $count ) {
                $count = 10;
            }
    
            $allowed_post_types = array_values( get_post_types( array( 'public' => true ) ) );
            $types              = isset( $instance['types'] ) ? (array) $instance['types'] : 'post';
    
    
            if ( isset( $instance['display'] ) && in_array( $instance['display'], array( 'widget_no_image_post', 'widget_most_read_post', 'widget_popular_post' ) ) ) {
                $display = $instance['display'];
            } else {
                $display = 'widget_no_image_post';
            }
    
            ?>
            <p>
                <label for="<?php echo $this->get_field_id( 'subtitle' ); ?>"><?php esc_html_e( 'Sub Title:', 'favblog' ); ?></label>
                <input class="widefat" id="<?php echo $this->get_field_id( 'subtitle' ); ?>" name="<?php echo $this->get_field_name( 'subtitle' ); ?>" type="text" value="<?php echo esc_attr( $subtitle ); ?>" />
            </p>
            <p>
                <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php esc_html_e( 'Title:', 'favblog' ); ?></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( 'count' ); ?>"><?php esc_html_e( 'Maximum number of posts to show (no more than 10):', 'favblog' ); ?></label>
                <input id="<?php echo $this->get_field_id( 'count' ); ?>" name="<?php echo $this->get_field_name( 'count' ); ?>" type="number" value="<?php echo (int) $count; ?>" min="1" max="10" />
            </p>
            <hr>
            <p>
                <label for="<?php echo $this->get_field_id( 'types' ); ?>"><?php esc_html_e( 'Types of content to display:', 'favblog' ); ?></label>
                <ul>
                    <?php
                    foreach ( $allowed_post_types as $type ) {
                        // Get the Post Type name to display next to the checkbox
                        $post_type_object = get_post_type_object( $type );
                        $label            = $post_type_object->labels->name;
    
                        $checked = '';
                        if ( in_array( $type, $types ) ) {
                            $checked = 'checked="checked" ';
                        }
                        ?>
    
                        <li><label>
                            <input value="<?php echo esc_attr( $type ); ?>" name="<?php echo $this->get_field_name( 'types' ); ?>" id="<?php echo $this->get_field_id( 'types' ); ?>-<?php echo $type; ?>" type="checkbox" <?php echo $checked; ?> />
                            <?php echo esc_html( $label ); ?>
                        </label></li>
    
                    <?php } // End foreach ?>
                </ul>
            </p>
            <hr>
            <p>
                <label><?php esc_html_e( 'Display as:', 'favblog' ); ?></label>
                <ul>
                    <li>
                        <label>
                            <input id="<?php echo $this->get_field_id( 'display' ); ?>" name="<?php echo $this->get_field_name( 'display' ); ?>" type="radio" value="widget_most_read_post" <?php checked( 'widget_most_read_post', $display ); ?> /> 
                            <?php esc_html_e( 'Design 1', 'favblog' ); ?>
                        </label>
                    </li>
                    <li>
                        <label>
                            <input id="<?php echo $this->get_field_id( 'display' ); ?>" name="<?php echo $this->get_field_name( 'display' ); ?>" type="radio" value="widget_no_image_post" <?php checked( 'widget_no_image_post', $display ); ?> />
                             <?php esc_html_e( 'Design 2', 'favblog' ); ?>
                            </label>
                        </li>
                    <li>
                        <label>
                            <input id="<?php echo $this->get_field_id( 'display' ); ?>" name="<?php echo $this->get_field_name( 'display' ); ?>" type="radio" value="widget_popular_post" <?php checked( 'widget_popular_post', $display ); ?> /> 
                            <?php esc_html_e( 'Design 3', 'favblog' ); ?>
                        </label>
                    </li>
                </ul>
            </p>
            <?php
    
        }

        function widget( $args, $instance ) {
    
            $instance = wp_parse_args( (array) $instance, $this->defaults() );
    
            $title = isset( $instance['title'] ) ? $instance['title'] : false;
            if ( false === $title ) {
                $title = $this->default_title;
            }
            /** This filter is documented in core/src/wp-includes/default-widgets.php */
            $title = apply_filters( 'widget_title', $title );

            $subtitle = isset( $instance['subtitle'] ) ?  $instance['subtitle'] : esc_html__( 'Sub Title ', 'favblog' ) ;

            $count = isset( $instance['count'] ) ? (int) $instance['count'] : false;
            if ( $count < 1 || 10 < $count ) {
                $count = 5;
            }
            /**
             * Control the number of displayed posts.
             *
             * @module widgets
             *
             * @since 3.3.0
             *
             * @param string $count Number of Posts displayed in the Top Posts widget. Default is 10.
             */
    
            $types = isset( $instance['types'] ) ? (array) $instance['types'] : array( 'post', 'page' );
    
            if ( isset( $instance['display'] ) && in_array( $instance['display'], array( 'widget_no_image_post', 'widget_most_read_post', 'widget_popular_post' ) ) ) {
                $display = $instance['display'];
             } else {
                $display = 'widget_no_image_post';
            }
 
            echo $args['before_widget'];
            if ( ! empty( $title ) ) {
                echo "<div class='widget-header'><p class='widget-subtitle'>". esc_html($subtitle). '</p>'. $args['before_title'] . esc_html($title) . $args['after_title'] . "</div>";
            } 
            $post_args = array(
                'numberposts' => $count,
                'post_type'   => $types,
                'category'    => 0,
                'orderby'     =>'date',
                'order'       => 'ASC',
              );           

              $posts = get_posts( $post_args ); ?>
                <ul class="<?php echo $instance['display']; ?>">
                    <?php foreach($posts as $index=>$post) : ?>
                        <li class="has-post-thumbnail">
                            <?php if($display == "widget_no_image_post") :?>
                                <div class="post-count">
                                    <span><?php echo sprintf("%02d", absint( $index+1 ));; ?></span>
                                </div>
                            <?php else: ?>
                                <div class="featured-image">
                                    <a href="<?php echo esc_url( get_permalink( $post->ID ) ); ?>"><img src="<?php
                                    echo has_post_thumbnail( $post->ID ) ? get_the_post_thumbnail_url( $post->ID) : get_template_directory_uri() .'/assets/uploads/no-featured-image-600x450.jpg'; ?>" alt="<?php echo esc_attr($post->post_title); ?>"></a>
                                </div><!-- .featured-image -->
                            <?php endif; ?>

                            <div class="entry-container">
                                <header class="entry-header">
                                    <h2 class="entry-title"><a href="<?php echo esc_url(get_permalink($post->ID) ); ?>"><?php echo esc_html($post->post_title); ?></a></h2>
                                </header>

                                <div class="entry-meta">
                                    <?php favblog_posted_on($post->ID); ?>
                                </div><!-- .entry-meta -->
                            </div><!-- .entry-container -->
                        </li>
                    <?php endforeach; 
                    wp_reset_postdata(); ?>
            <?php echo $args['after_widget'];
        }
    
     
        function update( $new_instance, $old_instance ) {
            // processes widget options to be saved
            $instance          = array();
            $instance['title'] = wp_kses( $new_instance['title'], array() );
            if ( $instance['title'] === $this->default_title ) {
                $instance['title'] = false; // Store as false in case of language change
            }
            $instance['subtitle'] = wp_kses( $new_instance['subtitle'], array() );
    
            $instance['count'] = (int) $new_instance['count'];
            if ( $instance['count'] < 1 || 10 < $instance['count'] ) {
                $instance['count'] = 10;
            }

    
            $allowed_post_types = array_values( get_post_types( array( 'public' => true ) ) );
            $instance['types']  = $new_instance['types'];
            foreach ( $new_instance['types'] as $key => $type ) {
                if ( ! in_array( $type, $allowed_post_types ) ) {
                    unset( $new_instance['types'][ $key ] );
                }
            }
            if ( isset( $new_instance['display'] ) && in_array( $new_instance['display'], array( 'widget_no_image_post', 'widget_most_read_post', 'widget_popular_post' ) ) ) {
                $instance['display'] = $new_instance['display'];
            } else {
                $instance['display'] = 'widget_no_image_post';
            }
            return $instance;
        } 
         
        public static function defaults() {
            return array(
                'subtitle' => esc_html__( 'Sub Title ', 'favblog' ),
                'title'    => esc_html__( 'Widget Title ', 'favblog' ),
                'count'    => absint( 4 ),
                'types'    => 'post',
                'display'  => 'widget_no_image_post',
            );
        }
    
    }
endif;
