<?php
/**
 * Kopa Widget Category
 *
 * @author 		Kopatheme
 * @category 	Widgets
 * @package 	KopaFramework/Widgets
 * @since    	1.0.0
 * @extends 	Kopa_Widget
 */

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

class Kopa_Widget_Category extends Kopa_Widget {
	/**
	 * Constructor
	 */
	public function __construct() {
		//Categories Array
        $categories = get_categories();
        $arr_cats = array(
            '' => __('All Categories', 'kopa-framework')
            );
        foreach ($categories as $category)
            $arr_cats[$category->term_id] = $category->name.' ('.$category->count.')';

		$this->widget_cssclass    = 'kopa-categories-widget';
		$this->widget_description = __( 'Show list categories with number post', 'newmoment' );
		$this->widget_id          = 'kopa-categories-widget';
		$this->widget_name        = __( 'Kopa Widget Category', 'newmoment' );
		// $this->widget_width       = 400;
		$this->settings           = array(
			'title'  => array(
				'type'  => 'text',
				'std'   => __( 'Kopa Widget Category', 'newmoment' ),
				'label' => __( 'Title:', 'newmoment' )
			),
            'category' => array(
				'type'    => 'multiselect',
				'std'     => '',
				'label'   => __( 'Category:', 'newmoment' ),
				'options' => $arr_cats,
            )
		);
		parent::__construct();
	}

	/**
	 * widget function.
	 *
	 * @see WP_Widget
	 * @access public
	 * @param array $args
	 * @param array $instance
	 * @return void
	 */
	public function widget( $args, $instance ) {

		extract( $args );

		$title    = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
		$categories = $instance['category'];
        echo $before_widget;
        ?>   
        	<?php if($title) { ?>
	            <h3 class="widget-title">
	                <span class="text-title"><?php echo $title; ?></span>
	                <span class="left-corner"></span>
	                <span class="right-corner"></span>
	            </h3>
            <?php } ?>
            <ul>
			<?php
				$args=array(
				  'include' => $categories,
				  'orderby' => 'name',
				  'order' => 'ASC'
				 );
				$categories=get_categories($args);
				  	foreach($categories as $category) {
				    echo '<li><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s", 'newmoment' ), $category->name ) . '" ' . '>' . $category->name . ' <span> '  . $category->count . ' </span></a> </li> ';
				}
			?>
			</ul>
		<?php
        echo $after_widget;
        
		wp_reset_postdata();
	}
}

register_widget( 'Kopa_Widget_Category' );
