<?php
/**
 * EDM: Default Tabbed
 *
 * Widget to display latest posts and comment in tabbed layout.
 *
 * @package MatrixAddons
 * @subpackage trainify
 * @since 1.0.0
 */

if(!class_exists( 'Trainify_Default_Tabbed_Widget' ) ):

    class Trainify_Default_Tabbed_Widget extends TrainifyFramework_Master_Widget{

	   /**
         * Register widget with WordPress.
         */
       public function __construct() {

            $widget_ops = array(
                'classname' => 'trainify_default_tabbed_widget',
                'description' => esc_html__( 'A widget shows recent posts and comment in tabbed layout.', 'trainify' )
            );
            parent::__construct( 'trainify_default_tabbed_widget', esc_html__( 'TF - Default Tabbed', 'trainify' ), $widget_ops );

        }

        /**
         * Helper function that holds widget fields
         * Array is used in update and form functions
         */
        public function widget_fields( $instance = array() ) {

            $fields = array(
                'trainify_framework_widget_tab'       => array(
                    'trainify_framework_widget_field_name'     => 'trainify_framework_widget_tab',
                    'trainify_framework_widget_field_title'    => esc_html__( 'General', 'trainify' ),
                    'trainify_framework_widget_field_default'  => 'general',
                    'trainify_framework_widget_field_type'     => 'tabgroup',
                    'trainify_framework_widget_field_options'  => array(
                        'general'=>array(
                            'trainify_framework_widget_field_title'=>esc_html__('General', 'trainify'),
                            'trainify_framework_widget_field_options'=> array(
                                'post_accordion' => array(
                                    'trainify_framework_widget_field_name'         => 'post_accordion',
                                    'trainify_framework_widget_field_title'        => esc_html__( 'Latest Post Accordion', 'trainify' ),
                                    'trainify_framework_widget_field_type'   => 'accordion',
                                    'trainify_framework_widget_field_options'=> array(
                                        array(
                                            'trainify_framework_widget_field_name'         => 'latest_tab_title',
                                            'trainify_framework_widget_field_title'        => esc_html__( 'Latest Posts Tab', 'trainify' ),
                                            'trainify_framework_widget_field_options'=> array(
                                                'latest_tab_title' => array(
                                                    'trainify_framework_widget_field_name'         => 'latest_tab_title', 'trainify_framework_widget_field_title'        => esc_html__( 'Latest Tab title', 'trainify' ), 'trainify_framework_widget_field_default'      => esc_html__( 'Latest', 'trainify' ), 'trainify_framework_widget_field_type'   => 'text',
                                                ),
                                                'no_of_posts' => array(
                                                    'trainify_framework_widget_field_name'         => 'no_of_posts', 'trainify_framework_widget_field_title'        => esc_html__( 'No of latest post.', 'trainify' ), 'trainify_framework_widget_field_default'      => 5, 'trainify_framework_widget_field_type'   => 'number',
                                                ),
                                                'excerpt_length' => array(
                                                    'trainify_framework_widget_field_name'         => 'excerpt_length', 
                                                    'trainify_framework_widget_field_title'        => esc_html__( 'Excerpt Length', 'trainify' ),
                                                    'trainify_framework_widget_field_default'      => 150, 
                                                    'trainify_framework_widget_field_type'   => 'number', 
                                                ),
                                                'image_thumbnail' => array(
                                                    'trainify_framework_widget_field_name'         => 'image_thumbnail', 
                                                    'trainify_framework_widget_field_title'        => esc_html__( 'Thumbnail Size', 'trainify' ),
                                                    'trainify_framework_widget_field_default' => 'trainify-framework-thumb-400x300',
                                                    'trainify_framework_widget_field_options' => trainify_framework_get_image_sizes(), 
                                                    'trainify_framework_widget_field_type'   => 'select', 
                                                ),
                                            )
                                        ),
                                    ),
                                ),
                                'comments_accordion' => array(
                                    'trainify_framework_widget_field_name'         => 'comments_accordion',
                                    'trainify_framework_widget_field_title'        => esc_html__( 'Comments Accordion', 'trainify' ),
                                    'trainify_framework_widget_field_type'   => 'accordion',
                                    'trainify_framework_widget_field_options'=> array(
                                        array(
                                            'trainify_framework_widget_field_name'         => 'latest_tab_title',
                                            'trainify_framework_widget_field_title'        => esc_html__( 'Comments Tab', 'trainify' ),
                                            'trainify_framework_widget_field_options'=> array(
                                                'comments_tab_title' => array(
                                                    'trainify_framework_widget_field_name'         => 'comments_tab_title',
                                                    'trainify_framework_widget_field_title'        => esc_html__( 'Comments Tab title', 'trainify' ),
                                                    'trainify_framework_widget_field_default'      => esc_html__( 'Comments', 'trainify' ),
                                                    'trainify_framework_widget_field_type'   => 'text'
                                                ),
                                                'no_of_comments' => array(
                                                    'trainify_framework_widget_field_name'         => 'no_of_comments', 'trainify_framework_widget_field_title'        => esc_html__( 'No of comments.', 'trainify' ), 'trainify_framework_widget_field_default'      => 5, 'trainify_framework_widget_field_type'   => 'number',
                                                ),
                                                'comment_length' => array(
                                                    'trainify_framework_widget_field_name'         => 'comment_length', 'trainify_framework_widget_field_title'        => esc_html__( 'Comment Length', 'trainify' ), 'trainify_framework_widget_field_default'      => 150, 'trainify_framework_widget_field_type'   => 'number',
                                                ),
                                                'comment_thumbnail' => array(
                                                    'trainify_framework_widget_field_name'         => 'comment_thumbnail', 'trainify_framework_widget_field_title'        => esc_html__( 'Comment Image Size(in px).', 'trainify' ), 'trainify_framework_widget_field_default'      => 150, 'trainify_framework_widget_field_type'   => 'number',
                                                ),
                                            )
                                        ),
                                    ),
                                ),
                            )
                        )
                    )
                )
            );

            $widget_fields_key = 'fields_'.$this->id_base;
            $widgets_fields = apply_filters( $widget_fields_key, $fields );
            return $widgets_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 ;
            }

            //Post Accordion
            $latest_tab_title   = isset( $instance['latest_tab_title'] ) ? $instance['latest_tab_title'] :  esc_html__( 'Latest', 'trainify' );
            $no_of_posts   = isset( $instance['no_of_posts'] ) ? $instance['no_of_posts'] :  5;
            $excerpt_length   = isset( $instance['excerpt_length'] ) ? $instance['excerpt_length'] :  150;
            $image_thumbnail   = isset( $instance['image_thumbnail'] ) ? $instance['image_thumbnail'] : 'trainify-framework-thumb-400x300';

            //Comments Accordion
            $comments_tab_title   = isset( $instance['comments_tab_title'] ) ? $instance['comments_tab_title'] :  esc_html__( 'Comments', 'trainify' );
            $no_of_comments   = isset( $instance['no_of_comments'] ) ? $instance['no_of_comments'] :  5;
            $comment_thumbnail   = isset( $instance['comment_thumbnail'] ) ? $instance['comment_thumbnail'] :  150;
            $comment_length   = isset( $instance['comment_length'] ) ? $instance['comment_length'] :  150;

            trainify_framework_before_widget($args);
            ?>
            <div class="tff-default-tabbed-wrapper tff-clearfix" id="tff-tabbed-widget<?php echo esc_attr($this->number); ?>">

                <ul class="widget-tabs tff-clearfix tff-widget-tab" id="tff-widget-tab<?php echo esc_attr($this->number); ?>">
                    <li class="active-item"><a href="#latest<?php echo esc_attr($this->number); ?>"><?php echo esc_html( $latest_tab_title ); ?></a></li>
                    <li><a href="#comments<?php echo esc_attr($this->number); ?>"><?php echo esc_html( $comments_tab_title ); ?></a></li>
                </ul><!-- .widget-tabs -->

                <div id="latest<?php echo esc_attr($this->number); ?>" class="tff-tabbed-section active-content tff-clearfix">
                    <?php
                    $latest_args = array(
                        'post_type' => 'post',
                        'post_status' => 'publish',
                        'posts_per_page' => absint($no_of_posts),
                    );
                    $latest_query = new WP_Query( $latest_args );
                    if( $latest_query->have_posts() ) {
                        while( $latest_query->have_posts() ) {
                            $latest_query->the_post();
                            $thumbanil_class = (has_post_thumbnail( get_the_ID() ) ) ? 'has-thumbnail' : 'no-thumbnail';
                            ?>
                            <div class="tff-single-post tff-clearfix">
                                <div class="tff-post-thumb <?php echo esc_attr($thumbanil_class); ?>">
                                    <a href="<?php the_permalink(); ?>"> <?php the_post_thumbnail( 'trainify-block-thumb' ); ?> </a>
                                </div><!-- .tff-post-thumb -->
                                <div class="tff-post-content">
                                    <h3 class="tff-post-title small-size"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                                    <?php trainify_framework_the_excerpt($excerpt_length, false); ?>
                                    <div class="tff-post-meta"><?php trainify_posted_on(); ?></div>
                                </div><!-- .tff-post-content -->
                            </div><!-- .tff-single-post -->
                            <?php
                        }
                    }
                    wp_reset_postdata();
                    ?>
                </div><!-- #latest -->

                <div id="comments<?php echo esc_attr($this->number); ?>" class="tff-tabbed-section tff-comments-content tff-clearfix">
                    <ul>
                        <?php
                        $trainify_comments = get_comments(
                            array( 
                                'number' => $no_of_comments 
                            )
                        );
                        foreach( $trainify_comments as $comment  ) {
                            ?>
                            <li class="tff-single-comment tff-clearfix">
                                <?php
                                $title = get_the_title( $comment->comment_post_ID );
                                echo '<div class="tff-comment-avatar">'. get_avatar( $comment, $comment_thumbnail ) .'</div>';
                                ?>
                                <div class="tff-comment-desc-wrap">
                                    <strong><?php echo esc_html(strip_tags( $comment->comment_author )); ?></strong>
                                    <?php esc_html_e( '&nbsp;commented on', 'trainify' ); ?>
                                    <a href="<?php echo esc_url( get_permalink( $comment->comment_post_ID ) ); ?>" rel="external nofollow" title="<?php echo esc_attr( $title ); ?>"> <?php echo esc_html( $title ); ?></a>: <?php echo esc_html(wp_html_excerpt( $comment->comment_content, $comment_length ) ); ?>
                                </div><!-- .tff-comment-desc-wrap -->
                            </li>
                            <?php
                        }
                        ?>
                    </ul>
                </div><!-- #comments -->

            </div><!-- .tff-default-tabbed-wrapper -->
            <?php
            trainify_framework_after_widget($args);
        }

    }

endif;