<?php
class PZY_Categories_Widget extends WP_Widget {

    public function widget( $args, $instance ) {
        $title = apply_filters( 'widget_title', $instance['title'] );
        $depth = ! empty( $instance['depth'] ) ? $instance['depth'] : 0;
        $hierarchical = ! empty( $instance['hierarchical'] ) ? true : false;
        $show_count = ! empty( $instance['show_count'] ) ? true : false;
        $hide_empty = ! empty( $instance['hide_empty'] ) ? true : false;

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

        echo '<ul class="pzy_categories_block">';
        wp_list_categories( array(
            'title_li' => '',
            'depth' => $depth,
            'hierarchical' => $hierarchical,
            'show_count' => $show_count,
            'hide_empty' => $hide_empty,
        ) );
        echo '</ul>';

        echo $args['after_widget'];
    }

    public function form( $instance ) {
        $title = ! empty( $instance['title'] ) ? $instance['title'] : __( 'دسته های مقالات', 'pezeshkyar' );
        $depth = ! empty( $instance['depth'] ) ? $instance['depth'] : 0;
        $hierarchical = isset( $instance['hierarchical'] ) ? (bool) $instance['hierarchical'] : false;
        $show_count = isset( $instance['show_count'] ) ? (bool) $instance['show_count'] : false;
        $hide_empty = isset( $instance['hide_empty'] ) ? (bool) $instance['hide_empty'] : false;
        ?>
        <p>
            <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'عنوان:', 'pezeshkyar' ); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
        </p>
        <p>
            <input class="checkbox" type="checkbox" <?php checked( $hierarchical ); ?> id="<?php echo $this->get_field_id( 'hierarchical' ); ?>" name="<?php echo $this->get_field_name( 'hierarchical' ); ?>" />
            <label for="<?php echo $this->get_field_id( 'hierarchical' ); ?>"><?php _e( 'سلسله مراتبی' , 'pezeshkyar'); ?></label>
        </p>
        <p>
            <input class="checkbox" type="checkbox" <?php checked( $show_count ); ?> id="<?php echo $this->get_field_id( 'show_count' ); ?>" name="<?php echo $this->get_field_name( 'show_count' ); ?>" />
            <label for="<?php echo $this->get_field_id( 'show_count' ); ?>"><?php _e( 'نمایش تعداد مقالات دسته', 'pezeshkyar' ); ?></label>
        </p>
        <p>
            <input class="checkbox" type="checkbox" <?php checked( $hide_empty ); ?> id="<?php echo $this->get_field_id( 'hide_empty' ); ?>" name="<?php echo $this->get_field_name( 'hide_empty' ); ?>" />
            <label for="<?php echo $this->get_field_id( 'hide_empty' ); ?>"><?php _e( 'مخفی کردن دسته های خالی', 'pezeshkyar' ); ?></label>
        </p>
        <p>
            <label for="<?php echo $this->get_field_id( 'depth' ); ?>"><?php _e( 'سطح دسته' , 'pezeshkyar'); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id( 'depth' ); ?>" name="<?php echo $this->get_field_name( 'depth' ); ?>" type="number" value="<?php echo esc_attr( $depth ); ?>" />
        </p>
        <?php
    }

    public function update( $new_instance, $old_instance ) {
        $instance = array();
        $instance['title'] = strip_tags( $new_instance['title'] );
        $instance['depth'] = absint( $new_instance['depth'] );
        $instance['hierarchical'] = ! empty( $new_instance['hierarchical'] ) ? 1 : 0;
        $instance['show_count'] = ! empty( $new_instance['show_count'] ) ? 1 : 0;
        $instance['hide_empty'] = ! empty( $new_instance['hide_empty'] ) ? 1 : 0;
        return $instance;
    }
}