<?php
/**
 * Display Widget
 *
 * @package Blogger_Era
 */

/**
* A widget that display Latest Blog
*/
class Blogger_Era_Latest_Blog extends WP_Widget
{

    function __construct() {

        global $control_ops;

        $widget_ops = array(
            'classname'   => 'latest-post',
            'description' => esc_html__( 'Add Widget to Display Latest Blog.', 'blogger-era' )
        );

        parent::__construct( 'blogger_era_latest_blog',esc_html__( 'Blogger Era: Latest Blog', 'blogger-era' ), $widget_ops, $control_ops );
    }

    function form( $instance ) {
        $title     = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
        $number    = isset( $instance['number'] ) ? absint( $instance['number'] ) : 4;
        $show_date = isset( $instance['show_date'] ) ? (bool) $instance['show_date'] : false;
        ?>
        <p><label for="<?php echo esc_attr($this->get_field_id( 'title' )); ?>"><?php echo esc_html__( 'Title:', 'blogger-era' ); ?></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($title); ?>" /></p>

        <p><label for="<?php echo esc_attr($this->get_field_id( 'number' )); ?>"><?php echo esc_html__( 'Number of posts to show:', 'blogger-era' );?></label>
        <input class="tiny-text" id="<?php echo esc_attr($this->get_field_id( 'number' )); ?>" name="<?php echo esc_attr($this->get_field_name( 'number' )); ?>" type="number" step="1" min="1" value="<?php echo esc_attr($number); ?>" size="4" /></p>

        <p><input class="checkbox" type="checkbox"<?php checked( $show_date ); ?> id="<?php echo esc_attr($this->get_field_id( 'show_date' )); ?>" name="<?php echo esc_attr($this->get_field_name( 'show_date' )); ?>" />
        <label for="<?php echo esc_attr($this->get_field_id( 'show_date' )); ?>"><?php echo esc_html__( 'Display post date?', 'blogger-era' ); ?></label></p>      


        <?php
    }

    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = sanitize_text_field( $new_instance['title'] );
        $instance['number'] = (int) $new_instance['number'];
        $instance['show_date'] = isset( $new_instance['show_date'] ) ? (bool) $new_instance['show_date'] : false;
        return $instance;
    }

    function widget( $args, $instance ) {

    $title = ( ! empty( $instance['title'] ) ) ? esc_html($instance['title']) : esc_html__( 'Latest Blog','blogger-era' );

    $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );

    $number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5;
    if ( ! $number )
    $number = 4;

    $show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false;
        $r = new WP_Query( apply_filters( 'widget_posts_args', array(
            'posts_per_page'      => absint( $number ),
            'no_found_rows'       => true,
            'post_status'         => 'publish',
            'ignore_sticky_posts' => true
        ) ) );
    echo $args['before_widget'];     
        if ($r->have_posts()) : ?>           

                <?php if ( $title ) : ?>
                    <h2 class="widget-title"><?php echo esc_html( $title );?></h2>
                <?php endif;?>

                <?php while ( $r->have_posts() ) : $r->the_post(); ?>

                    <article class="post">

                        <?php if ( has_post_thumbnail() ) : ?>

                            <figure>

                                <?php the_post_thumbnail( 'thumbnail' );?>

                            </figure>

                        <?php endif;?>
                        

                        <div class="post-wrap">
                            <?php blogger_era_categories();?>
                            <header class="entry-header">
                                <h2 class="entry-title">

                                <a href="<?php the_permalink();?>"><?php the_title();?></a>

                                </h2>
                            </header>

                            <?php if ( $show_date ) : ?>

                                <div class="entry-meta">
                                    <?php blogger_era_posted_on(); ?>
                                </div>

                            <?php endif;?> 
                        </div>

                    </article>
                <?php endwhile;
                wp_reset_postdata();?>
        <?php endif;
    echo $args['after_widget'];    
    } 

}

if ( ! class_exists( 'Blogger_Era_Social_Widget' ) ) :

    /**
     * Social widget class.
     *
     * @since 1.0.0
     */
    class Blogger_Era_Social_Widget extends WP_Widget {
        /**
        * Constructor.
        *
        * @since 1.0.0
        */
        function __construct() {
            $opts = array(
            'classname'   => 'follow-us',
            'description' => esc_html__( 'Social Link Widget', 'blogger-era' ),
            );
            parent::__construct( 'blogger-era-social', esc_html__( 'Blogger Era: Social', 'blogger-era' ), $opts );
        }

        /**
        * Echo the widget content.
        *
        * @since 1.0.0
        *
        * @param array $args     Display arguments including before_title, after_title,
        *                        before_widget, and after_widget.
        * @param array $instance The settings for the particular instance of the widget.
        */
        function widget( $args, $instance ) {

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

            echo $args['before_widget'];

            if ( ! empty( $title ) ) {
                echo $args['before_title'] . $title . $args['after_title'];
            }

            if ( has_nav_menu( 'social-menu' ) ) {
                wp_nav_menu( array(
                    'theme_location' => 'social-menu',
                    'depth'          => 1,
                     'container'      => 'div',
                    'container_class'=> 'social-links inline-design',
                ) );
            }

            echo $args['after_widget'];

        }

        /**
        * Update widget instance.
        *
        * @since 1.0.0
        *
        * @param array $new_instance New settings for this instance as input by the user via
        *                            {@see WP_Widget::form()}.
        * @param array $old_instance Old settings for this instance.
        * @return array Settings to save or bool false to cancel saving.
        */
        function update( $new_instance, $old_instance ) {
            $instance = $old_instance;

            $instance['title'] = sanitize_text_field( $new_instance['title'] );

            return $instance;
            }

            /**
            * Output the settings update form.
            *
            * @since 1.0.0
            *
            * @param array $instance Current settings.
            * @return void
            */
        function form( $instance ) {

            $instance = wp_parse_args( (array) $instance, array(
                'title' => '',
            ) );
            ?>
            <p>
            <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'blogger-era' ); ?></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>

            <?php if ( ! has_nav_menu( 'social-menu' ) ) : ?>
                <p>
                <?php esc_html_e( 'Social menu is not set. Please create menu and assign it to Social Theme Location.', 'blogger-era' ); ?>
                </p>
            <?php endif; ?>
            <?php
        }
    }

endif;

function Blogger_Era_Action_Latest_Blog() {

    register_widget( 'Blogger_Era_Latest_Blog' );
    register_widget( 'Blogger_Era_Social_Widget' );

}
add_action( 'widgets_init', 'Blogger_Era_Action_Latest_Blog' );