<?php
/**
 * EDM: Featured Posts
 *
 * Widget show the featured posts from selected categories in different layouts.
 *
 * @package MatrixAddons
 * @subpackage trainify
 * @since 1.0.0
 */

if( !class_exists( 'Trainify_Featured_Blocks_Widget' ) ):

    class Trainify_Featured_Blocks_Widget extends TrainifyFramework_Master_Widget {

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

            $widget_ops = array(
                'classname' => 'trainify_featured_posts tff-full-widget',
                'description' => esc_html__( 'Displays featured posts from selected categories in different layouts.', 'trainify' )
            );
            parent::__construct( 'trainify_featured_posts', esc_html__( 'TF - Featured Posts', '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(
                                'title' => array(
                                    'trainify_framework_widget_field_name'         => 'title',
                                    'trainify_framework_widget_field_title'        => esc_html__( 'Block title', 'trainify' ),
                                    'trainify_framework_widget_field_description'  => esc_html__( 'Enter your block title. (Optional - Leave blank to hide title.)', 'trainify' ),
                                    'trainify_framework_widget_field_type'   => 'text'
                                ),
                                'title_target'    => array(
                                    'trainify_framework_widget_field_name'     => 'title_target',
                                    'trainify_framework_widget_field_wraper'   => 'title-target',
                                    'trainify_framework_widget_field_title'    => esc_html__( 'Title Target', 'trainify' ),
                                    'trainify_framework_widget_field_default'  => '',
                                    'trainify_framework_widget_field_type'     => 'select',
                                    'trainify_framework_widget_field_options'  => trainify_framework_link_target(),
                                    'trainify_framework_widget_field_relation' => array(
                                        'exist' => array(
                                            'show_fields'   => array(
                                                'title-link', 
                                            ),
                                        ),
                                        'empty' => array(
                                            'hide_fields'   => array(
                                                'title-link', 
                                            ),
                                        ),
                                    ),
                                ),
                                'title_link'    => array(
                                    'trainify_framework_widget_field_name'     => 'title_link',
                                    'trainify_framework_widget_field_wraper'   => 'title-link',
                                    'trainify_framework_widget_field_title'    => esc_html__( 'Title link', 'trainify' ),
                                    'trainify_framework_widget_field_default'  => '',
                                    'trainify_framework_widget_field_type'     => 'text',
                                ),
                                'block_term_ids' => array(
                                    'trainify_framework_widget_field_name'         => 'block_term_ids',
                                    'trainify_framework_widget_field_title'        => esc_html__( 'Block Categories', 'trainify' ),
                                    'trainify_framework_widget_field_type'   => 'multitermlist',
                                    'trainify_framework_widget_taxonomy_type' => 'category',
                                ),
                                'show_author' => array(
                                    'trainify_framework_widget_field_name'          => 'show_author',
                                    'trainify_framework_widget_field_wraper'        => 'show-author',
                                    'trainify_framework_widget_field_title'         => esc_html__( 
                                        'Show Author', 'trainify' ),
                                    'trainify_framework_widget_field_default'       => 0,
                                    'trainify_framework_widget_field_type'          => 'checkbox',
                                ),
                                'show_postdate' => array(
                                    'trainify_framework_widget_field_name'          => 'show_postdate',
                                    'trainify_framework_widget_field_wraper'        => 'show-postdate',
                                    'trainify_framework_widget_field_title'         => esc_html__( 
                                        'Show Post Date', 'trainify' ),
                                    'trainify_framework_widget_field_default'       => 0,
                                    'trainify_framework_widget_field_type'          => 'checkbox',
                                ),
                            )
                        ),
                        'layout'=>array(
                            'trainify_framework_widget_field_title'=>esc_html__('Layout', 'trainify'),
                            'trainify_framework_widget_field_options'=> array(
                                'thumbnail_size' => array(
                                    'trainify_framework_widget_field_name'         => 'thumbnail_size',
                                    'trainify_framework_widget_field_wraper'   => 'thumbnail-size',
                                    'trainify_framework_widget_field_title'        => esc_html__( 
                                        'Image Size', 'trainify' ),
                                    'trainify_framework_widget_field_default'=> 'thumbnail',
                                    'trainify_framework_widget_field_type' => 'select',
                                    'trainify_framework_widget_field_options'   => trainify_framework_get_image_sizes(),
                                ),
                                'show_excerpt' => array(
                                    'trainify_framework_widget_field_name'         => 'show_excerpt',
                                    'trainify_framework_widget_field_wraper'   => 'show-excerpt',
                                    'trainify_framework_widget_field_title'        => esc_html__( 'Show Description?', 'trainify' ),
                                    'trainify_framework_widget_field_default'=> 1,
                                    'trainify_framework_widget_field_description'  => esc_html__( 'Check to show short description.', 'trainify' ),
                                    'trainify_framework_widget_field_type'   => 'checkbox'
                                ),
                                'excerpt_length' => array(
                                    'trainify_framework_widget_field_name'         => 'excerpt_length',
                                    'trainify_framework_widget_field_wraper'   => 'excerpt-length',
                                    'trainify_framework_widget_field_title'        => esc_html__( 'Description Length', 'trainify' ),
                                    'trainify_framework_widget_field_default'=> '150',
                                    'trainify_framework_widget_field_description'  => esc_html__( 'Choose excerpt length in character. default length description length is 150. you can set zero if you want to hide description.', 'trainify' ),
                                    '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 ) {
                       
            if( empty( $instance ) ) {
                return ;
            }

            trainify_framework_before_widget($args);

            $featured_widget_layout = apply_filters( 'trainify_featured_post_widget_layout', 'layout1', $instance );
            if($featured_widget_layout == 'layout1' ){
                $this->default_template($args, $instance);
            }else{
                do_action( 'trainify_featured_post_widget_template', $args, $instance, $this );
            }
            
            trainify_framework_after_widget($args);
            wp_reset_postdata();

            ?>
            
            <?php

        }

        public function default_template($args, $instance){

            extract($args);

            $title  = isset( $instance['title'] ) ? $instance['title'] : '';
            $title  = apply_filters( 'widget_title', $title, $instance, $this->id_base );
            $title_target      = isset( $instance['title_target'] ) ? $instance['title_target'] : '';
            $title_link  = isset( $instance['title_link'] ) ? $instance['title_link'] : '';
            $block_term_ids  = empty( $instance['block_term_ids'] ) ? '' : $instance['block_term_ids'];
            $show_postdate  = isset( $instance['show_postdate'] ) ? $instance['show_postdate'] : 0;
            $show_author  = isset( $instance['show_author'] ) ? $instance['show_author'] : 0;

             /*
             * Layout tabs
             */
             $thumbnail_size = isset( $instance['thumbnail_size'] ) ? $instance['thumbnail_size'] : 'thumbnail';
             $show_excerpt = isset( $instance['show_excerpt'] ) ? $instance['show_excerpt'] : 0;
             $excerpt_length = isset( $instance['excerpt_length'] ) ? $instance['excerpt_length'] : 0;
            ?>
            <div class="tff-container">
                <div class="tff-block-wrapper featured-posts tff-clearfix">
                    <?php
                    $title_args = array(
                        'title'         => $title,
                        'title_target'  => $title_target,
                        'title_link'    => $title_link,
                        'before_title'  => $before_title,
                        'after_title'   => $after_title
                    );
                    do_action('trainify_framework_widget_title', $title_args);
                    ?>
                    <div class="tff-featured-posts-wrapper">
                        <?php
                        $trainify_post_count = apply_filters( 'trainify_featured_posts_block_count', 4, $instance, $args );
                        $trainify_posts_args = array(
                            'post_type' => 'post',
                            'post_status' => 'publish',
                            'posts_per_page' => absint( $trainify_post_count )
                        );
                        if( $block_term_ids ) {
                            $trainify_posts_args['tax_query'][] = array(
                                'taxonomy' => 'category',
                                'field'    => 'term_id',
                                'terms'    => $block_term_ids,
                            );
                        }
                        $trainify_posts_query = new WP_Query( $trainify_posts_args );
                        if( $trainify_posts_query->have_posts() ) {
                            while( $trainify_posts_query->have_posts() ) {
                                $trainify_posts_query->the_post();
                                ?>
                                <div class="tff-single-post-wrap tff-clearfix">
                                    <div class="tff-single-post">
                                        <div class="tff-post-thumb">
                                            <a href="<?php the_permalink(); ?>">
                                                <?php
                                                if( has_post_thumbnail() ) {
                                                    the_post_thumbnail( $thumbnail_size );
                                                }
                                                ?>
                                            </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>
                                            <div class="tff-post-meta"><?php trainify_posted_on( $show_postdate, $show_author ); ?></div>
                                            <?php if($excerpt_length>0 && $show_excerpt ){ ?>
                                                <div class="tff-post-description">
                                                    <?php trainify_framework_the_excerpt($excerpt_length, false); ?>
                                                </div><!-- .trainify-post-description -->
                                            <?php } ?>
                                        </div><!-- .tff-post-content -->
                                    </div> <!-- tff-single-post -->
                                </div><!-- .tff-single-post-wrap -->
                                <?php
                            }
                        }
                        ?>
                    </div><!-- .tff-featured-posts-wrapper -->
                </div><!--- .tff-block-wrapper -->
            </div>
            <?php
        }

    }

endif;