<?php
/**
 * @package TA Newspaper
 */

add_action('widgets_init', 'ta_newspaper_post_category_register');

function ta_newspaper_post_category_register() {
    register_widget('TA_Newspaper_Post_Category_Widget');
}

class TA_Newspaper_Post_Category_Widget extends WP_Widget {

    public function __construct() {
        parent::__construct(
                'TA_Newspaper_Post_Category_Widget', esc_html__('TA : Post Category', 'ta-newspaper'), array(
                'description' => esc_html__('This Widget show Post Category Profile', 'ta-newspaper')
                )
        );
    }

    /**
     * Helper function that holds widget fields
     * Array is used in update and form functions
     */
    private function widget_fields() {
        $ta_newspaper_cat_list = ta_newspaper_Category_list();
        $fields = array(
           'ta_newspaper_blog_cat_category_1' => array(
                  'ta_newspaper_widgets_name' => 'ta_newspaper_blog_cat_category_1',
                  'ta_newspaper_widgets_title' => esc_html__('Select Category One', 'ta-newspaper'),
                  'ta_newspaper_widgets_field_type' => 'select',
                  'ta_newspaper_widgets_field_options' => $ta_newspaper_cat_list,
            ),
            'ta_newspaper_blog_cat_category_2' => array(
                  'ta_newspaper_widgets_name' => 'ta_newspaper_blog_cat_category_2',
                  'ta_newspaper_widgets_title' => esc_html__('Select Category Two', 'ta-newspaper'),
                  'ta_newspaper_widgets_field_type' => 'select',
                  'ta_newspaper_widgets_field_options' => $ta_newspaper_cat_list,
            ),
            'ta_newspaper_blog_cat_category_3' => array(
                  'ta_newspaper_widgets_name' => 'ta_newspaper_blog_cat_category_3',
                  'ta_newspaper_widgets_title' => esc_html__('Select Category Three', 'ta-newspaper'),
                  'ta_newspaper_widgets_field_type' => 'select',
                  'ta_newspaper_widgets_field_options' => $ta_newspaper_cat_list,
            ),
            'ta_newspaper_blog_cat_category_4' => array(
                  'ta_newspaper_widgets_name' => 'ta_newspaper_blog_cat_category_4',
                  'ta_newspaper_widgets_title' => esc_html__('Select Category Four', 'ta-newspaper'),
                  'ta_newspaper_widgets_field_type' => 'select',
                  'ta_newspaper_widgets_field_options' => $ta_newspaper_cat_list,
            ),
            'ta_newspaper_blog_cat_category_5' => array(
                  'ta_newspaper_widgets_name' => 'ta_newspaper_blog_cat_category_5',
                  'ta_newspaper_widgets_title' => esc_html__('Select Category Five', 'ta-newspaper'),
                  'ta_newspaper_widgets_field_type' => 'select',
                  'ta_newspaper_widgets_field_options' => $ta_newspaper_cat_list,
            ),
            'ta_newspaper_blog_cat_category_6' => array(
                  'ta_newspaper_widgets_name' => 'ta_newspaper_blog_cat_category_6',
                  'ta_newspaper_widgets_title' => esc_html__('Select Category Six', 'ta-newspaper'),
                  'ta_newspaper_widgets_field_type' => 'select',
                  'ta_newspaper_widgets_field_options' => $ta_newspaper_cat_list,
            ),
        );

        return $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);
    

        echo $before_widget; ?>
        <div class="ta-post-category">

            <div class="ta-secondary-post-cat clearfix">
                
                <?php 
                $i = 1;
                while($i <= 6) {

                    $ta_newspaper_blog_cat_categor = isset( $instance['ta_newspaper_blog_cat_category_'.$i] ) ? $instance['ta_newspaper_blog_cat_category_'.$i] : '' ;
                    if( $ta_newspaper_blog_cat_categor ){
                      $cat_attr = get_category_by_slug( $ta_newspaper_blog_cat_categor );
                      $term_name = $cat_attr->name;
                      $term_desc = $cat_attr->description;
                      $term_id = $cat_attr->term_id;
                      if( function_exists('z_taxonomy_image_url' ) ){
                          $image = z_taxonomy_image_url($term_id);
                      }
                      $cat_link = get_category_link( $term_id ); ?>
                      <div class="cat-image-content-loop">
                          <?php if( function_exists('z_taxonomy_image_url' ) && $image ){ ?>
                              <a class="pc-img-wraper"  href="<?php echo esc_url( $cat_link ); ?>"><img src="<?php echo esc_url($image); ?>"></a>
                          <?php } ?>
                          <div class="pc-title-desc">
                              <?php if( $term_name ){ ?>
                                  <h4><a href="<?php echo esc_url( $cat_link ); ?>"><?php echo esc_html( $term_name ); ?></a></h4>
                              <?php } ?>
                              <?php if( $term_desc ){ ?>
                                  <p><?php echo esc_html( $term_desc ); ?></p>
                              <?php } ?>
                          </div>
                      </div>
                    
                <?php }
                $i++;
                } ?>

            </div>

        </div>
        <?php
        echo $after_widget;
    }

    public function update($new_instance, $old_instance) {
          $instance = $old_instance;
          $widget_fields = $this->widget_fields();
          foreach ($widget_fields as $widget_field) {
              extract($widget_field);
              $instance[$ta_newspaper_widgets_name] = ta_newspaper_widgets_updated_field_value($widget_field, $new_instance[$ta_newspaper_widgets_name]);
          }
          return $instance;
      }

      public function form($instance) {
          $widget_fields = $this->widget_fields();
          foreach ($widget_fields as $widget_field) {
              extract($widget_field);
              $ta_newspaper_widgets_field_value = !empty($instance[$ta_newspaper_widgets_name]) ? $instance[$ta_newspaper_widgets_name] : '';
              ta_newspaper_widgets_show_widget_field($this, $widget_field, $ta_newspaper_widgets_field_value);
          }
      }

}
