<?php
/**
 * OMag - Primary Recent Posts Widget
 *
 * @package OMag
 */

if ( ! class_exists( 'OMag_Primary_Recent_Posts_Widget' ) ) :

/**
* OMag - Primary Recent Posts widget class
*
* @since 1.0.0
*/
class OMag_Primary_Recent_Posts_Widget extends WP_Widget {

    function __construct() {
        $options = array(
            'classname'   => 'omag-primary-rec-posts',
            'description' => esc_html__( 'OMag Widget to display recent posts', 'omag' ),
        );

        parent::__construct( 'omag-primary-recent-posts', esc_html__( 'OMag: Primary Recent Posts', 'omag' ), $options );
    }


    function widget( $args, $instance ) {

        $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );

        $post_nos    = ! empty( $instance['post_nos'] ) ? $instance['post_nos'] : 4;

        echo $args['before_widget']; ?>

            <?php 
            if ( ! empty( $title ) ) {
                echo $args['before_title'] . esc_html( $title ). $args['after_title'];
            } else {
                $default_title = 'Recent Posts';
                echo $args['before_title'] . esc_html( $default_title ). $args['after_title'];
            }

            ?>

        <?php

        $omag_primary_recent_args = array(
            'posts_per_page'        => absint( $post_nos ),
            'no_found_rows'         => true,
            'ignore_sticky_posts'   => 1,
            'post_status'           => 'publish', 
        );

        $omag_primary_recent_query = new WP_Query( $omag_primary_recent_args );

        if ( $omag_primary_recent_query->have_posts() ) :

            ?>

            <div class="widget-content">

                <?php
                $count = 1;
                while ( $omag_primary_recent_query->have_posts() ) :

                    $omag_primary_recent_query->the_post(); 

                    if( 1 === $count ) {

                        ?>

                        <div class="primary-post sidebar-post">
                            <?php if ( has_post_thumbnail() ) {  ?>
                            <figure>
                                <a href="<?php the_permalink(); ?>">
                                    <?php the_post_thumbnail(); ?>
                                </a>
                            </figure>
                            <?php } ?>
                            <div class="article-content">
                                <div class="entry-header">
                                    <h4>
                                        <a href="<?php the_permalink(); ?>"> <?php the_title(); ?> </a>
                                    </h4>
                                    <div class="banner-info">
                                        <?php omag_posted_by(); ?>
                                        <span>-</span>
                                        <span class="post-date">
                                            <time class="entry-date">
                                                <?php omag_posts_category(); ?>
                                            </time>
                                        </span>
                                    </div>
                                    <div class="entry-meta">
                                        <?php omag_posted_on(); ?>

                                        <?php
                                        echo '<span class="comments-link">';
                                        echo '<i class="far fa-comments"></i>';
                                        comments_popup_link( esc_html__( 'No Comments', 'omag' ), esc_html__( '1 Comment', 'omag' ), '% '. esc_html__( 'Comments', 'omag' ), 'post-comments');
                                        echo '</span>';
                                        ?>
                                    </div>
                                </div>
                                <div class="entry-content">
                                    <?php 
                                    $prp_content = omag_get_the_excerpt( absint( 10 ) );
                                    echo wp_kses_post($prp_content) ? wpautop( wp_kses_post($prp_content) ) : '';
                                    ?>
                                </div>
                            </div>
                        </div>

                        <?php } else { ?>



                        <div class="following-post sidebar-post">
                            <?php if ( has_post_thumbnail() ) {  ?>
                            <figure class="float-left">
                                <a href="<?php the_permalink(); ?>">
                                    <?php the_post_thumbnail(); ?>
                                </a>
                            </figure>
    
                            <div class="article-content float-right">

                            <?php } else { ?>

                            <div class="article-content float-right article-mini-full">

                            <?php } ?>

                                <div class="entry-header">
                                    <h4>
                                        <a href="<?php the_permalink(); ?>"> <?php the_title(); ?> </a>
                                    </h4>
                                    <div class="banner-info">

                                        <?php omag_posted_on(); ?>

                                    </div>
                                </div>
                            </div>
                            <div class="clearfix"></div>
                        </div>


                        <?php }

                        $count++;

                    endwhile; 

                    wp_reset_postdata();

                    ?>    

                </div>

            <?php endif; ?>


            <?php
            echo $args['after_widget'];

        }

        function update( $new_instance, $old_instance ) {
            $instance = $old_instance;
            $instance['title']           = sanitize_text_field( $new_instance['title'] );
            $instance['post_nos']     = absint( $new_instance['post_nos'] );

            return $instance;
        }

        function form( $instance ) {

            $instance = wp_parse_args( (array) $instance, array(
                'title'         => esc_html__( 'Recent Posts', 'omag' ),
                'post_nos'   => 4,
            ) );
            ?>
            <p>
                <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'omag' ); ?></label>
                <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" />
            </p>

            <p>
                <label for="<?php echo esc_attr( $this->get_field_name('post_nos') ); ?>">
                    <?php esc_html_e('Number of Posts to Show:', 'omag'); ?>
                </label>
                <input class="widefat" id="<?php echo esc_attr( $this->get_field_id('post_nos') ); ?>" name="<?php echo esc_attr( $this->get_field_name('post_nos') ); ?>" type="number" value="<?php echo absint( $instance['post_nos'] ); ?>" />
            </p>

            <?php
        }

    }

endif;
