<?php
function dw_dynamic_widget_js() {
  wp_enqueue_style( 'dynamic-widget-style', get_template_directory_uri() . '/inc/css/dynamic-widget.css', array(), '20130716' );

  wp_enqueue_script( 'dynamic-widget-js', get_template_directory_uri() . '/inc/js/dynamic-widget.js', array('jquery','jquery-ui-datepicker','jquery-ui-sortable','jquery-ui-sortable', 'jquery-ui-draggable','jquery-ui-droppable','admin-widgets' ), '20130716', true );
}
add_action( 'admin_enqueue_scripts', 'dw_dynamic_widget_js' );

/**
 * Dynamic Widget
 */
class dw_dynamic_Widget extends WP_Widget {
  function widget( $args, $instance ) {
    extract( $args, EXTR_SKIP );
    echo $before_widget;
    $this->dw_display_widgets_front($instance);
    echo $after_widget;
  }

  function update( $new_instance, $old_instance ) {
    $updated_instance = $new_instance;
    return $updated_instance;
  }

  function form( $instance ) {
    global $wp_registered_widgets;
    $instance = wp_parse_args( $instance, array( 
      'widgets'    =>  '',
      'title'     =>  ''
    ) );
    ?>
      <input type="hidden" class="widefat" name="<?php echo $this->get_field_name('widgets') ?>" id="<?php echo $this->get_field_id('widgets') ?>" value="<?php echo htmlentities( $instance['widgets'] ) ?>" >
      <input type="hidden" name="<?php echo $this->get_field_name('title') ?>" id="<?php echo $this->get_field_id('title') ?>" value="<?php echo $instance['title'] ?>" class="widefat">
      <div class="dw-widget-extends" data-setting="#<?php echo $this->get_field_id('widgets') ?>" >
        <p class="description"><?php _e('Drag & Drop Widgets Here','dw_minion') ?></p>
        <?php
          $widgets = explode(':dw-data:', $instance['widgets'] );
          if( !empty($widgets) && is_array($widgets) ){
            $number = 1;
            foreach ($widgets as $widget) {
              if( !empty( $widget ) ) {
                $url = rawurldecode($widget);
                parse_str($widget,$s);
                $this->dw_display_widgets($s, $number);
              }
              $number++;
            }
          }
        ?>
      </div>
    <?php
  }

  function dw_get_widgets( $id_base, $number ){
    global $wp_registered_widgets;

    $widget = false;
    foreach ($wp_registered_widgets as $key => $wdg) {
      if( strpos( $key, $id_base ) === 0 ) {
        if( isset($wp_registered_widgets[ $key ]['callback'][0]) && is_object($wp_registered_widgets[ $key ]['callback'][0]) ) {
          $classname = get_class( $wp_registered_widgets[ $key ]['callback'][0] );
          $widget = new $classname;
          $widget->id_base = $id_base;
          $widget->number = $number;
          break;
        }
      }
    }

    return $widget;
  }

  function dw_display_widgets($s, $number){
    $instance = !empty($s['widget-'.$s['id_base']]) ? array_shift( $s['widget-'.$s['id_base']] ) : array();
    $widget = $this->dw_get_widgets( $s['id_base'], $number );
  ?>  
    <?php if( $widget ) { ?>
    <div id="<?php echo esc_attr($s['widget-id']); ?>" class="widget">
      <div class="widget-top">
        <div class="widget-title-action">
          <a class="widget-action hide-if-no-js" href="#available-widgets"></a>
          <a class="widget-control-edit hide-if-js" href="<?php echo esc_url( add_query_arg( $query_arg ) ); ?>">
            <span class="edit"><?php _ex( 'Edit', 'widget' ); ?></span>
            <span class="add"><?php _ex( 'Add', 'widget' ); ?></span>
            <span class="screen-reader-text"><?php echo $widget->name; ?></span>
          </a>
        </div>
        <div class="widget-title"><h4><?php echo $widget->name; ?><span class="in-widget-title"></span></h4></div>
      </div>

      <div class="widget-inside">
        <div class="widget-content">
          <?php if( isset($s['id_base'] ) ) { 
            $widget->form($instance); 
          } else { 
            echo "\t\t<p>" . __('There are no options for this widget.','dw_minion') . "</p>\n"; 
          } ?>
        </div>
        <input data-dw="true" type="hidden" name="widget-id" class="widget-id" value="<?php echo esc_attr($s['widget-id']); ?>" />
        <input data-dw="true" type="hidden" name="id_base" class="id_base" value="<?php echo esc_attr($s['id_base']); ?>" />
        <input data-dw="true" type="hidden" name="widget-width" class="widget-width" value="<?php echo esc_attr($s['widget-width']); ?>">
        <div class="widget-control-actions">
          <div class="alignleft">
            <a class="widget-control-remove" href="#remove"><?php _e('Delete','dw_minion'); ?></a> |
            <a class="widget-control-close" href="#close"><?php _e('Close','dw_minion'); ?></a>
          </div>
          <div class="alignright widget-control-noform">
            <?php submit_button( __( 'Save', 'dw_minion' ), 'button-primary widget-control-save right', 'savewidget', false, array( 'id' => 'widget-' . esc_attr( $s['widget-id'] ) . '-savewidget' ) ); ?>
            <span class="spinner"></span>
          </div>
          <br class="clear" />
        </div>
      </div>

      <div class="widget-description"><?php echo ( $widget_description = wp_widget_description($widget_id) ) ? "$widget_description\n" : "$widget_title\n"; ?></div>
    </div>
    <?php } ?>
  <?php
  }
}

/**
 * Tabs Widget
 */
class dw_tabs_Widget extends dw_dynamic_Widget {
  function dw_tabs_Widget() {
    $widget_ops = array( 'classname' => 'dw_tabs news-tab', 'description' => __('Display widgets inside a tab','dw_minion') );
    $this->WP_Widget( 'dw_tabs', 'DW: Tabs', $widget_ops );
  }

  function dw_display_widgets_front($instance){
    global $wp_registered_widget_updates;
    wp_parse_args($instance, array(
      'widgets' => array()
    ));
    
    $widgets = explode(':dw-data:', $instance['widgets'] );
    if( !empty($widgets) && is_array($widgets) ){ ?>
      <ul class="nav nav-tabs" id="nav-tabs-<?php echo $this->id ?>">
      <?php
      $i = 0;
      foreach ($widgets as $widget ) {
        $active = '';
        if( $i == 0 ){ $active='active'; } 
        $i++;
        if( !empty( $widget ) ) {
          $url = rawurldecode($widget);
          parse_str($widget,$s);
          $instance = !empty($s['widget-'.$s['id_base']]) ? array_shift( $s['widget-'.$s['id_base']] ) : array();

          $widget = $this->dw_get_widgets( $s['id_base'], $i );
          // $widget = isset( $wp_registered_widget_updates[$s['id_base']] ) ? $wp_registered_widget_updates[$s['id_base']]['callback'][0] : false;
          if( $widget ) {
            $widget_title = isset($instance['title']) ? $instance['title'] : $widget->name;
            echo '<li class="'.$active.'"><a href="#'.$s['widget-id'].'" data-toggle="tab">'.$widget_title.'</a></li>';
          }
        }
      }
      ?>
      </ul>
      <div class="tab-content">
        <?php
          $i=0;
          foreach ($widgets as $widget) {
            $active = '';
            if( $i == 0 ) { $active='active'; }
             $i++;
              if( !empty( $widget ) ) {
                $url = rawurldecode($widget);
                parse_str($widget,$s);
                $instance = isset($s['widget-'.$s['id_base']]) ? array_shift( $s['widget-'.$s['id_base']] ) : array();

                $widget = $this->dw_get_widgets( $s['id_base'], $i );
                // $widget = isset( $wp_registered_widget_updates[$s['id_base']] ) ? $wp_registered_widget_updates[$s['id_base']]['callback'][0] : false;

                if( isset($s['id_base'] ) && $widget ) {
                  $widget_options = $widget->widget_options; 
                ?>
                  <div class="tab-pane <?php echo 'widget_'.$s['widget-id'].' '.$widget_options['classname'] ?> <?php echo $active ?>" id="<?php echo $s['widget-id'] ?>">
                    <?php  
                      $default_args = array( 
                        'before_widget' => '', 
                        'after_widget' => '', 
                        'before_title' => '<h3 class="widget-title">',
                        'after_title' => '</h3>' 
                      );
                      $widget->widget($default_args,$instance);
                    ?>
                  </div>
                <?php
                }
              }
            }
          } 
        ?>
      </div>
  <?php
  }
}
add_action( 'widgets_init', create_function( '', "register_widget('dw_tabs_Widget');" ) );

/**
 * Accordion Widget
 */
class dw_accordion_Widget extends dw_dynamic_Widget {
  function dw_accordion_Widget() {
    $widget_ops = array( 'classname' => 'dw_accordion news-accordion', 'description' => __('Display widgets inside an accordion','dw_minion') );
    $this->WP_Widget( 'dw_accordions', 'DW: Accordion', $widget_ops );
  }

  function dw_display_widgets_front($instance){
    global $wp_registered_widget_updates;
    wp_parse_args($instance, array(
      'widgets' => array()
    ));

    $widgets = explode(':dw-data:', $instance['widgets'] );
    if( !empty($widgets) && is_array($widgets) ) {
      echo '<div class="accordion" id="accordion-'.$this->id.'">';

      $collapse = ''; $i = 0;
      foreach ($widgets as $widget) {
        $collapse = ( $i == 0 ) ? 'active' : 'collapsed';

        if( !empty( $widget ) ) {
          $url = rawurldecode($widget);
          parse_str($url,$s);
          $instance = !empty($s['widget-'.$s['id_base']]) ? array_shift( $s['widget-'.$s['id_base']] ) : array();

          $widget = $this->dw_get_widgets( $s['id_base'], $i );
          // $widget = isset($wp_registered_widget_updates[$s['id_base']]) ? $wp_registered_widget_updates[$s['id_base']]['callback'][0] : false;

          if( isset($s['id_base'] ) && $widget ){ ?>
            <?php $widget_options = $widget->widget_options; ?>
            <div class="accordion-group">
              <div class="accordion-heading"><a class="accordion-toggle <?php echo $collapse ?>" data-toggle="collapse" data-parent="#accordion-<?php echo $this->id ?>" href="#<?php echo $this->id ?>-<?php echo $s['widget-id'] ?>"><?php echo ( isset($instance['title']) ) ? $instance['title'] : $widget->name; ?></a></div>
              <div id="<?php echo $this->id ?>-<?php echo $s['widget-id'] ?>" class="<?php echo 'widget_'.$s['widget-id'].' ' ?> accordion-body collapse <?php echo $collapse == 'active' ? 'in' : ''; ?> <?php echo $widget_options['classname'] ?>">
                <div class="accordion-inner">
                <?php  
                $default_args = array( 
                    'before_widget' => '', 
                    'after_widget' => '', 
                    'before_title' => '<h3 class="widget-title">', 
                    'after_title' => '</h3>' 
                );
                $widget->widget($default_args,$instance);
                ?>
                </div>
              </div>
            </div>
          <?php
          }
        }
        $i++;
      }

      echo '</div>';
    }
  }
}
add_action( 'widgets_init', create_function( '', "register_widget('dw_accordion_Widget');" ) );
/**
 * Query_Posts widget class
 */
class DW_Widget_Query_Posts extends WP_Widget {

  function __construct() {
    $widget_ops = array('classname' => 'dw_widget_query_posts', 'description' => __( 'Query posts from your site', 'dw-gamez' ) );
    parent::__construct('query-posts', __('DW: Query Posts', 'dw-gamez'), $widget_ops);
    $this->alt_option_name = 'dw_widget_query_posts';

    add_action( 'save_post', array($this, 'flush_widget_cache') );
    add_action( 'deleted_post', array($this, 'flush_widget_cache') );
    add_action( 'switch_theme', array($this, 'flush_widget_cache') );
  }

  function widget($args, $instance) {
    $cache = wp_cache_get('widget_query_posts', 'widget');

    if ( !is_array($cache) )
      $cache = array();

    if ( ! isset( $args['widget_id'] ) )
      $args['widget_id'] = $this->id;

    if ( isset( $cache[ $args['widget_id'] ] ) ) {
      echo $cache[ $args['widget_id'] ];
      return;
    }

    ob_start();
    extract($args);

    $title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Query Posts', 'dw-gamez' );
    $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
    $number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 10;
    if ( ! $number )
      $number = 10;
    $show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false;
    $show_thumb = isset( $instance['show_thumb'] ) ? $instance['show_thumb'] : false;
    $more_text = ( ! empty( $instance['more_text'] ) ) ? $instance['more_text'] : 'Continue reading ...';
    $thumb_size = ( ! empty( $instance['thumb_size'] ) ) ? $instance['thumb_size'] : 'thumbnail';
    $cat_id = ( ! empty( $instance['cat_id'] ) ) ? absint( $instance['cat_id'] ) : 0;
    if ( ! $cat_id )
      $cat_id = 0;
    $tags = ( ! empty( $instance['tags'] ) ) ? $instance['tags'] : '';
    $classes = ( ! empty( $instance['classes'] ) ) ? $instance['classes'] : '';
    $show_content = ( ! empty( $instance['show_content'] )) ? $instance['show_content'] : '';

    $r = new WP_Query( apply_filters( 'widget_posts_args', array( 'posts_per_page' => $number, 'no_found_rows' => true, 'post_status' => 'publish', 'cat' => $cat_id, 'tag' => $tags, 'ignore_sticky_posts' => true ) ) );
    if ($r->have_posts()) :
?>
    <?php echo str_replace('class="widget', 'class="widget '.$classes, $before_widget); ?>
    <div class="query_post_inner">
      <?php if ( $title ) echo $before_title . $title . $after_title; ?>
      <ul>
      <?php while ( $r->have_posts() ) : $r->the_post(); ?>
        <?php $format = get_post_format(); if ( false === $format ) $format = 'standard'; ?>
        <li class="format-<?php echo $format; ?>">
          <div class="query_post_item">
          <?php if ( $show_thumb && has_post_thumbnail() ) : ?>
            <div class="entry-thumbnail">
              <a href="<?php the_permalink() ?>" title="<?php echo esc_attr( get_the_title() ? get_the_title() : get_the_ID() ); ?>">
                <?php the_post_thumbnail($thumb_size); ?>
              </a>
            </div>
          <?php endif; ?>
            <a href="<?php the_permalink() ?>" class="entry-title" title="<?php echo esc_attr( get_the_title() ? get_the_title() : get_the_ID() ); ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); ?></a>
          <?php if ( $show_date ) : ?>
            <span class="entry-date"><i class="icon-time"></i> <?php echo get_the_date(); ?></span>
          <?php endif; ?>
          <?php if($show_content == 'content') :
          global $more;
          $more = 0;
          ?>
            <div class="entry-content"><?php echo get_the_content($more_text); ?></div>
          <?php elseif($show_content == 'excerpt') : ?>
            <div class="entry-summary"><?php echo get_the_excerpt(); ?></div>
          <?php endif; ?>
          </div>
        </li>
      <?php endwhile; ?>
      </ul>
    </div>
    <?php echo $after_widget; ?>
<?php wp_reset_query();

    endif;

    $cache[$args['widget_id']] = ob_get_flush();
    wp_cache_set('widget_query_posts', $cache, 'widget');
  }

  function update( $new_instance, $old_instance ) {
    $instance = $old_instance;
    $instance['title'] = strip_tags($new_instance['title']);
    $instance['number'] = (int) $new_instance['number'];
    $instance['show_date'] = isset( $new_instance['show_date'] ) ? (bool) $new_instance['show_date'] : false;
    $instance['show_thumb'] = isset( $new_instance['show_thumb'] ) ? (bool) $new_instance['show_thumb'] : false;
    $instance['more_text'] = strip_tags($new_instance['more_text']);
    $instance['thumb_size'] = strip_tags($new_instance['thumb_size']);
    $instance['cat_id'] = (int) $new_instance['cat_id'];
    $instance['tags'] = strip_tags($new_instance['tags']);
    $instance['classes'] = strip_tags($new_instance['classes']);
    $instance['show_content'] = strip_tags($new_instance['show_content']);
    $this->flush_widget_cache();

    $alloptions = wp_cache_get( 'alloptions', 'options' );
    if ( isset($alloptions['dw_widget_query_posts']) )
      delete_option('dw_widget_query_posts');

    return $instance;
  }

  function flush_widget_cache() {
    wp_cache_delete('widget_query_posts', 'widget');
  }

  function form( $instance ) {
    $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
    $number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
    $show_date = isset( $instance['show_date'] ) ? (bool) $instance['show_date'] : false;
    $show_thumb = isset( $instance['show_thumb'] ) ? (bool) $instance['show_thumb'] : false;
    $more_text = isset( $instance['more_text'] ) ? esc_attr( $instance['more_text'] ) : 'Continue reading ...';
    $thumb_size = isset( $instance['thumb_size'] ) ? esc_attr( $instance['thumb_size'] ) : 'thumbnail';
    $cat_id = isset( $instance['cat_id'] ) ? esc_attr( $instance['cat_id'] ) : 0;
    $tags = isset( $instance['tags'] ) ? esc_attr( $instance['tags'] ) : '';
    $classes = isset( $instance['classes'] ) ? esc_attr( $instance['classes'] ) : '';
    $show_content = isset( $instance['show_content'] ) ? esc_attr( $instance['show_content'] ) : '';
?>
    <p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'dw-gamez' ); ?></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 $title; ?>" /></p>

    <p><label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of posts to show:', 'dw-gamez' ); ?></label>
    <input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p>

    <p><label for="<?php echo $this->get_field_id('show_content'); ?>"><?php _e('Display post content?', 'dw-gamez' ) ?></label>
    <select id="<?php echo $this->get_field_id('show_content'); ?>" name="<?php echo $this->get_field_name('show_content'); ?>">
      <option value="" <?php selected($show_content, '') ?>></option>
      <option value="excerpt" <?php selected($show_content, 'excerpt') ?>><?php _e( 'Excerpt', 'dw-gamez' ); ?></option>
      <option value="content" <?php selected($show_content, 'content') ?>><?php _e( 'Content', 'dw-gamez' ); ?></option>
    </select></p>

    <p><input class="checkbox" type="checkbox" <?php checked( $show_date ); ?> id="<?php echo $this->get_field_id( 'show_date' ); ?>" name="<?php echo $this->get_field_name( 'show_date' ); ?>" />
    <label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php _e( 'Display post date?', 'dw-gamez' ); ?></label></p>

    <p><input class="checkbox" type="checkbox" <?php checked( $show_thumb ); ?> id="<?php echo $this->get_field_id( 'show_thumb' ); ?>" name="<?php echo $this->get_field_name( 'show_thumb' ); ?>" />
    <label for="<?php echo $this->get_field_id( 'show_thumb' ); ?>"><?php _e( 'Display post thumbnail?', 'dw-gamez' ); ?></label></p>

    <p><label for="<?php echo $this->get_field_id( 'more_text' ); ?>"><?php _e( 'Read more text:', 'dw-gamez' ); ?></label>
    <input class="widefat" id="<?php echo $this->get_field_id( 'more_text' ); ?>" name="<?php echo $this->get_field_name( 'more_text' ); ?>" type="text" value="<?php echo $more_text; ?>" /></p>

    <p><label for="<?php echo $this->get_field_id( 'thumb_size' ); ?>"><?php _e( 'Thumbnail Size:', 'dw-gamez' ); ?></label>
    <input class="widefat" id="<?php echo $this->get_field_id( 'thumb_size' ); ?>" name="<?php echo $this->get_field_name( 'thumb_size' ); ?>" type="text" value="<?php echo $thumb_size; ?>" /></p>
    
    <p><label for="<?php echo $this->get_field_id( 'cat_id' ); ?>"><?php _e( 'Category:', 'dw-gamez' ); ?></label>
    <?php wp_dropdown_categories('name='.$this->get_field_name( 'cat_id' ).'&class=widefat&show_option_all=All&selected='.$cat_id); ?></p>

    <p><label for="<?php echo $this->get_field_id( 'tags' ); ?>"><?php _e( 'Tags:', 'dw-gamez' ); ?></label>
    <input class="widefat" id="<?php echo $this->get_field_id( 'tags' ); ?>" name="<?php echo $this->get_field_name( 'tags' ); ?>" placeholder="<?php _e( 'tag 1, tag 2, tag 3','dw-gamez' )?>" type="text" value="<?php echo $tags; ?>" /></p>

    <p><label for="<?php echo $this->get_field_id( 'classes' ); ?>"><?php _e( 'Classes:', 'dw-gamez' ); ?></label>
    <input class="widefat" id="<?php echo $this->get_field_id( 'classes' ); ?>" name="<?php echo $this->get_field_name( 'classes' ); ?>" type="text" value="<?php echo $classes; ?>" /></p>
<?php
  }
}
add_action( 'widgets_init', create_function( '', "register_widget('DW_Widget_Query_Posts');" ) );

/**
 * Posts_Carousel widget class
 */
class DW_Widget_Posts_Carousel extends WP_Widget {

  function __construct() {
    $widget_ops = array('classname' => 'widget_posts_carousel slide', 'description' => __( 'Display Posts as an Carousel', 'dw-gamez') );
    parent::__construct('posts-carousel', __('DW: Posts Carousel', 'dw-gamez'), $widget_ops);
    $this->alt_option_name = 'widget_posts_carousel';

    add_action( 'save_post', array($this, 'flush_widget_cache') );
    add_action( 'deleted_post', array($this, 'flush_widget_cache') );
    add_action( 'switch_theme', array($this, 'flush_widget_cache') );
  }

  function widget($args, $instance) {
    $cache = wp_cache_get('widget_posts_carousel', 'widget');

    if ( !is_array($cache) )
      $cache = array();

    if ( ! isset( $args['widget_id'] ) )
      $args['widget_id'] = $this->id;

    if ( isset( $cache[ $args['widget_id'] ] ) ) {
      echo $cache[ $args['widget_id'] ];
      return;
    }

    ob_start();
    extract($args);

    $title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : '';
    $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
    $number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 10;
    if ( ! $number )
      $number = 10;
    $number_per_item = ( ! empty( $instance['number_per_item'] ) ) ? absint( $instance['number_per_item'] ) : 1;
    if ( ! $number_per_item )
      $number_per_item = 1;

    $number_item = $number/$number_per_item;
    $show_title = isset( $instance['show_title'] ) ? $instance['show_title'] : false;
    $show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false;
    $show_control = isset( $instance['show_control'] ) ? $instance['show_control'] : false;
    $show_indicators = isset( $instance['show_indicators'] ) ? $instance['show_indicators'] : false;
    $show_indicators_image = isset( $instance['show_indicators_image'] ) ? $instance['show_indicators_image'] : false;
    $show_thumbnail = isset( $instance['show_thumbnail'] ) ? $instance['show_thumbnail'] : false;
    $thumbnail_size = ( ! empty( $instance['thumbnail_size'] ) ) ? $instance['thumbnail_size'] : 'medium';
    $cat_id = ( ! empty( $instance['cat_id'] ) ) ? absint( $instance['cat_id'] ) : 0;
    if ( ! $cat_id )
      $cat_id = 0;
    $tags = ( ! empty( $instance['tags'] ) ) ? $instance['tags'] : '';
    $classes = ( ! empty( $instance['classes'] ) ) ? $instance['classes'] : 'carousel-1';
    $r = new WP_Query( apply_filters( 'widget_posts_args', array( 'posts_per_page' => $number, 'no_found_rows' => true, 'post_status' => 'publish', 'cat' => $cat_id, 'tag' => $tags, 'ignore_sticky_posts' => true ) ) );
    if ($r->have_posts()) :
?>
    <?php echo $before_widget; ?>
    <div class="<?php echo 'carousel '.$classes; ?>">
    <?php if ( $title ) echo $before_title . $title . $after_title; ?>
      <?php if($show_indicators) : ?>
      <div class="carousel-indicators-wrap">
        <ol class="carousel-indicators">
        <?php $i=0; ?>
        <?php while ( $r->have_posts() ) : $r->the_post(); ?>
          <?php if($i%$number_per_item==0) { ?><li <?php if($i==0) echo 'class="active" '; ?>data-slide-to="<?php echo $i; ?>" data-target="#<?php echo $this->id; ?>"><?php } ?>
          <?php if($show_indicators_image && $number_per_item==1) the_post_thumbnail( 'thumbnail' )?>
        <?php if( ($i+1) % $number_per_item == 0 ) echo '</li>'; ?>
        <?php $i++; endwhile; ?>
        <?php if( ($i<>0) && ($i % $number_per_item != 0 ) ) echo '</li>'; ?>
        </ol>
      </div>
      <?php endif; ?>

      <div class="carousel-inner">
        
        <?php $j=0; ?>
        <?php while ( $r->have_posts() ) : $r->the_post(); ?>
          <?php if($j%$number_per_item==0) { ?><div class="item<?php if($j==0) { echo ' active'; }?>"><div class="row"><?php } ?>
          <div class="col-sm-<?php echo 12/$number_per_item; ?>">
          <?php if($show_thumbnail) : ?>
          <a href="<?php the_permalink() ?>">
            <?php the_post_thumbnail( $thumbnail_size )?>
          </a>
          <?php endif; ?>
          <?php if($show_title || $show_date ) : ?>
          <div class="carousel-caption">
            <?php if( $show_title ) : ?><h4 class="carousel-title"><a href="<?php the_permalink() ?>" class="post-title" title="<?php echo esc_attr( get_the_title() ? get_the_title() : get_the_ID() ); ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); ?></a></h4><?php endif; ?>
            <?php if( $show_date ) : ?><div class="carousel-meta"><time><a href="<?php the_permalink() ?>"><i class="icon-time"></i> <?php echo get_the_date(); ?></a></time></div><?php endif; ?>
          </div>
          <?php endif; ?>
          </div>
          <?php if( ($j+1) % $number_per_item == 0 ) echo '</div></div>'; ?>
        <?php $j++; endwhile; ?>
        <?php if( ($j<>0) && ($j % $number_per_item != 0 ) ) echo '</div></div>'; ?>
      </div>

      <?php if($show_control) : ?>
      <div class="carousel-control-wrap">
        <a data-slide="prev" href="#<?php echo $this->id; ?>" class="left carousel-control"><span class="icon-prev"></span></a>
        <a data-slide="next" href="#<?php echo $this->id; ?>" class="right carousel-control"><span class="icon-next"></span></a>
      </div>
      <?php endif; ?>
    </div>
    <?php echo $after_widget; ?>
<?php
    wp_reset_postdata();

    endif;

    $cache[$args['widget_id']] = ob_get_flush();
    wp_cache_set('widget_posts_carousel', $cache, 'widget');
  }

  function update( $new_instance, $old_instance ) {
    $instance = $old_instance;
    $instance['title'] = strip_tags($new_instance['title']);
    $instance['number'] = (int) $new_instance['number'];
    $instance['number_per_item'] = (int) $new_instance['number_per_item'];
    $instance['show_title'] = isset( $new_instance['show_title'] ) ? (bool) $new_instance['show_title'] : false;
    $instance['show_date'] = isset( $new_instance['show_date'] ) ? (bool) $new_instance['show_date'] : false;
    $instance['show_control'] = isset( $new_instance['show_control'] ) ? (bool) $new_instance['show_control'] : false;
    $instance['show_indicators'] = isset( $new_instance['show_indicators'] ) ? (bool) $new_instance['show_indicators'] : false;
    $instance['show_indicators_image'] = isset( $new_instance['show_indicators_image'] ) ? (bool) $new_instance['show_indicators_image'] : false;
    $instance['show_thumbnail'] = isset( $new_instance['show_thumbnail'] ) ? (bool) $new_instance['show_thumbnail'] : false;
    $instance['thumbnail_size'] = strip_tags($new_instance['thumbnail_size']);
    $instance['cat_id'] = (int) $new_instance['cat_id'];
    $instance['tags'] = strip_tags($new_instance['tags']);
    $instance['classes'] = strip_tags($new_instance['classes']);
    $this->flush_widget_cache();

    $alloptions = wp_cache_get( 'alloptions', 'options' );
    if ( isset($alloptions['widget_posts_carousel']) )
      delete_option('widget_posts_carousel');
    return $instance;
  }

  function flush_widget_cache() {
    wp_cache_delete('widget_posts_carousel', 'widget');
  }

  function form( $instance ) {
    $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
    $number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
    $number_per_item    = isset( $instance['number_per_item'] ) ? absint( $instance['number_per_item'] ) : 1;
    $show_title = isset( $instance['show_title'] ) ? (bool) $instance['show_title'] : false;
    $show_date = isset( $instance['show_date'] ) ? (bool) $instance['show_date'] : false;
    $show_control = isset( $instance['show_control'] ) ? (bool) $instance['show_control'] : false;
    $show_indicators = isset( $instance['show_indicators'] ) ? (bool) $instance['show_indicators'] : false;
    $show_indicators_image = isset( $instance['show_indicators_image'] ) ? (bool) $instance['show_indicators_image'] : false;
    $show_thumbnail = isset( $instance['show_thumbnail'] ) ? (bool) $instance['show_thumbnail'] : false;
    $thumbnail_size = isset( $instance['thumbnail_size'] ) ? esc_attr( $instance['thumbnail_size'] ) : 'medium';
    $cat_id = isset( $instance['cat_id'] ) ? esc_attr( $instance['cat_id'] ) : 0;
    $tags = isset( $instance['tags'] ) ? esc_attr( $instance['tags'] ) : '';
    $classes = isset( $instance['classes'] ) ? esc_attr( $instance['classes'] ) : '';
    
?>
    <p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'dw-gamez' ); ?></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 $title; ?>" /></p>

    <p><label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of posts to show:', 'dw-gamez' ); ?></label>
    <input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p>

    <p><label for="<?php echo $this->get_field_id( 'number_per_item' ); ?>"><?php _e( 'Number of posts per item:', 'dw-gamez' ); ?></label>
    <input id="<?php echo $this->get_field_id( 'number_per_item' ); ?>" name="<?php echo $this->get_field_name( 'number_per_item' ); ?>" type="text" value="<?php echo $number_per_item; ?>" size="3" /></p>

    <p><input class="checkbox" type="checkbox" <?php checked( $show_title ); ?> id="<?php echo $this->get_field_id( 'show_title' ); ?>" name="<?php echo $this->get_field_name( 'show_title' ); ?>" />
    <label for="<?php echo $this->get_field_id( 'show_title' ); ?>"><?php _e( 'Display post title?', 'dw-gamez' ); ?></label></p>

    <p><input class="checkbox" type="checkbox" <?php checked( $show_date ); ?> id="<?php echo $this->get_field_id( 'show_date' ); ?>" name="<?php echo $this->get_field_name( 'show_date' ); ?>" />
    <label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php _e( 'Display post date?', 'dw-gamez' ); ?></label></p>

    <p><input class="checkbox" type="checkbox" <?php checked( $show_thumbnail ); ?> id="<?php echo $this->get_field_id( 'show_thumbnail' ); ?>" name="<?php echo $this->get_field_name( 'show_thumbnail' ); ?>" />
    <label for="<?php echo $this->get_field_id( 'show_thumbnail' ); ?>"><?php _e( 'Display post thumbnail?', 'dw-gamez' ); ?></label></p>

    <p><label for="<?php echo $this->get_field_id( 'thumbnail_size' ); ?>"><?php _e( 'Thumbnail Size:', 'dw-gamez' ); ?></label>
    <input class="widefat" id="<?php echo $this->get_field_id( 'thumbnail_size' ); ?>" name="<?php echo $this->get_field_name( 'thumbnail_size' ); ?>" type="text" value="<?php echo $thumbnail_size; ?>" /></p>

    <p><input class="checkbox" type="checkbox" <?php checked( $show_control ); ?> id="<?php echo $this->get_field_id( 'show_control' ); ?>" name="<?php echo $this->get_field_name( 'show_control' ); ?>" />
    <label for="<?php echo $this->get_field_id( 'show_control' ); ?>"><?php _e( 'Display carousel control?', 'dw-gamez' ); ?></label></p>

    <p><input class="checkbox" type="checkbox" <?php checked( $show_indicators ); ?> id="<?php echo $this->get_field_id( 'show_indicators' ); ?>" name="<?php echo $this->get_field_name( 'show_indicators' ); ?>" />
    <label for="<?php echo $this->get_field_id( 'show_indicators' ); ?>"><?php _e( 'Display carousel indicators?', 'dw-gamez' ); ?></label></p>

    <p><input class="checkbox" type="checkbox" <?php checked( $show_indicators_image ); ?> id="<?php echo $this->get_field_id( 'show_indicators_image' ); ?>" name="<?php echo $this->get_field_name( 'show_indicators_image' ); ?>" />
    <label for="<?php echo $this->get_field_id( 'show_indicators_image' ); ?>"><?php _e( 'Display carousel indicators image?', 'dw-gamez' ); ?></label></p>

    <p><label for="<?php echo $this->get_field_id( 'cat_id' ); ?>"><?php _e( 'Category:', 'dw-gamez' ); ?></label>
    <?php wp_dropdown_categories('name='.$this->get_field_name( 'cat_id', 'dw-gamez' ).'&class=widefat&show_option_all=All&selected='.$cat_id); ?></p>

    <p><label for="<?php echo $this->get_field_id( 'tags' ); ?>"><?php _e( 'Tags:', 'dw-gamez' ); ?></label>
    <input class="widefat" id="<?php echo $this->get_field_id( 'tags' ); ?>" name="<?php echo $this->get_field_name( 'tags' ); ?>" placeholder="<?php _e( 'tag 1, tag 2, tag 3','dw-gamez' )?>" type="text" value="<?php echo $tags; ?>" /></p>

    <p><label for="<?php echo $this->get_field_id( 'classes' ); ?>"><?php _e( 'Classes:', 'dw-gamez' ); ?></label>
    <input class="widefat" id="<?php echo $this->get_field_id( 'classes' ); ?>" name="<?php echo $this->get_field_name( 'classes' ); ?>" type="text" value="<?php echo $classes; ?>" /></p>

    <?php
  }
}
add_action( 'widgets_init', create_function( '', "register_widget('DW_Widget_Posts_Carousel');" ) );