<?php
   add_action('widgets_init', 'tigu_sidebar_scroller_load_widgets');
   function tigu_sidebar_scroller_load_widgets(){
    register_widget('tigu_Sidebar_Scroller_Widget');
   }
      class Tigu_Sidebar_Scroller_Widget extends WP_Widget {
   function Tigu_Sidebar_Scroller_Widget(){
      $widget_ops = array(
     'classname' => 'tigu_sidebar_scroller_widget',
     'description' => 'Show newest games on sidebar widget with scroller.' );
      $control_ops = array(
     'id_base' => 'tigu_sidebar_scroller_widget' );
      $this->WP_Widget('tigu_sidebar_scroller_widget', 'GAMELEON: Latest Posts - Scroller', $widget_ops, $control_ops);
    }
   function widget($args,$instance){
      extract($args);

/* User selected settings. */
      $title = $instance['title'];
      $post_type = $instance['post_type'];
      $categories = $instance['categories'];
      $exclude_cat = !empty($instance['exclude_cat']) ? $instance['exclude_cat'] : NULL;
      $show_scroller = isset($instance['show_scroller']) ? 'true' : 'false';
      $posts = $instance['posts'];
	  $show_home = isset($instance['show_home']) ? 'true' : 'false';
      $show_page = isset($instance['show_page']) ? 'true' : 'false';
      $show_single = isset($instance['show_single']) ? 'true' : 'false';
      if ( ( is_home() && $show_home == 'false')  ||  ( is_page() && $show_page == 'false' ) || ( is_single() && $show_single == 'false' )) {
        } else {
        echo $before_widget;
?>
<?php
      if($title) {
      echo $before_title.$title.$after_title;
   }
?>
<?php
    $posts_per_page = $posts;
    $recent_posts = new WP_Query();
    $recent_posts->query('showposts='.$posts_per_page.'&orderby=date&cat='.$exclude_cat.'');
     
?>
<!-- START WIDGET -->

      <div class="grid-sidebar">
<?php if($show_scroller == 'true'): ?>
            <div class="scrolly" id="scrolly_<?php echo $this->number; ?>">
      <ul>
<?php
      $post_types = get_post_types();
      unset($post_types['page'], $post_types['attachment'], $post_types['revision'], $post_types['nav_menu_item']);
      if($post_type == 'all') {
      $post_type_array = $post_types;
   } else {
      $post_type_array = $post_type;
   }
      if($categories != 'all') {
      $categories_array = array($categories);
   }
?>
<?php while($recent_posts->have_posts()): $recent_posts->the_post(); ?>
<?php if(has_post_thumbnail() || is_myarcade_game()): ?>
<?php
      if(has_post_format('video') || has_post_format('audio') || has_post_format('gallery')) {
      $icon = '<span class="' . get_post_format($post->ID) . '-icon"></span>';
   } else {
      $icon = '';
   }
?>
<?php $image = wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID()), 'sidebar-thumbs'); ?>
<?php if ( empty($image[0]) ) $image[0] = myarcade_get_thumbnail_url(); ?>
      <li>

<?php echo $icon; ?><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'>
<img src="<?php echo $image[0]; ?>" width="50" height="50" alt="<?php the_title(); ?>" />
</a>

      <h2 class="gameicon"><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'>
<?php if(get_option('tigu_shorten_titles') == 'true'): ?>
<?php $shorttitle = substr(the_title('','',FALSE),0,26); ?><?php echo $shorttitle; if (strlen($shorttitle) >25){ echo '&hellip;'; } ?>
<?php else: ?>
<?php the_title(); ?>
<?php endif; ?>
</a></h2>
      <p><?php echo get_excerpt(92); ?></p>

      </li>
<?php else: ?>
      <li><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'>
<img src="<?php echo get_template_directory_uri(); ?>/images/thumbnail-76.png" width="50" height="50" alt="<?php the_title(); ?>" />
</a></li>
<?php endif; ?>
<?php endwhile; ?>
      </ul>
      </div>
<?php else: ?>
      <div class="scrollyoff" id="scrollyoff_<?php echo $this->number; ?>">
      <ul>
<?php
      $post_types = get_post_types();
      unset($post_types['page'], $post_types['attachment'], $post_types['revision'], $post_types['nav_menu_item']);
      if($post_type == 'all') {
      $post_type_array = $post_types;
   } else {
      $post_type_array = $post_type;
   }
      if($categories != 'all') {
      $categories_array = array($categories);
   }
?>
<?php while($recent_posts->have_posts()): $recent_posts->the_post(); ?>
<?php if(has_post_thumbnail() || is_myarcade_game() ): ?>
<?php
      if(has_post_format('video') || has_post_format('audio') || has_post_format('gallery')) {
      $icon = '<span class="' . get_post_format($post->ID) . '-icon"></span>';
   } else {
      $icon = '';
   }
?>
<?php $image = wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID()), 'sidebar-thumbs'); ?>
<?php if ( empty($image[0]) ) $image[0] = myarcade_get_thumbnail_url(); ?>
      <li>

<?php echo $icon; ?><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'>
<img src="<?php echo $image[0]; ?>" width="50" height="50" alt="<?php the_title(); ?>" />
</a>

<h2 class="gameicon"><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'>
<?php if(get_option('tigu_shorten_titles') == 'true'): ?>
<?php $shorttitle = substr(the_title('','',FALSE),0,26); ?><?php echo $shorttitle; if (strlen($shorttitle) >25){ echo '&hellip;'; } ?>
<?php else: ?>
<?php the_title(); ?>
<?php endif; ?>
</a></h2>
      <p><?php echo get_excerpt(92); ?></p>

      </li>
<?php else: ?>
      <li><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'>
<img src="<?php echo get_template_directory_uri(); ?>/images/thumbnail-76.png" width="50" height="50" alt="<?php the_title(); ?>" />
</a></li>
<?php endif; ?>
<?php endwhile; ?>
      </ul>
      </div>
<?php endif; ?>
      </div>

<!-- END WIDGET -->

<?php
      echo $after_widget;
   }
   }
   function update($new_instance, $old_instance)
   {
      $instance = $old_instance;
      $instance['title'] = $new_instance['title'];
      $instance['exclude_cat'] =  $new_instance['exclude_cat'];
      $instance['post_type'] = $new_instance['post_type'];
      $instance['categories'] = $new_instance['categories'];
      $instance['show_home'] = $new_instance['show_home'];
      $instance['show_page'] = $new_instance['show_page'];
	  $instance['show_single'] = $new_instance['show_single'];
      $instance['posts'] = $new_instance['posts'];
      $instance['show_scroller'] = $new_instance['show_scroller'];
      return $instance;
   }
   function form($instance)
   {
      $defaults = array(
     'title' => 'Latest Posts',
     'post_type' => 'all',
     'show_scroller' => 'on',
     'exclude_cat' => '',
	 'show_home' => 'on',
     'show_page' => 'on',
     'show_single' => 'on',
     'categories' => 'all',
     'posts' => 6 );
      $instance = wp_parse_args((array) $instance, $defaults); ?>

<!-- START ADMIN WIDGETS AREA -->

      <p>
         <label for="<?php echo $this->get_field_id('title'); ?>">Title:</label>
         <input class="widefat" style="width: 216px;" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $instance['title']; ?>" />
      </p>

 <p>
          <label for="<?php echo $this->get_field_id('exclude_cat'); ?>">Exclude categories by ID:</label>
         <input class="widefat" style="width: 216px;" id="<?php echo $this->get_field_id('exclude_cat'); ?>" name="<?php echo $this->get_field_name('exclude_cat'); ?>" value="<?php echo $instance['exclude_cat']; ?>" />
     <br />
     <small>You may exclude some categories by typing their IDs, with minus sign in front of them, separated by comma and no spaces. Example: -3,-4,-21 for multiple IDs and just -3 without comma, for one ID only. Just leave it empty if you do not need to exclude any categories.</small>
     </p>
      <p>
         <label for="<?php echo $this->get_field_id('posts'); ?>">Number of posts to scroll:</label>
         <input class="widefat" style="width: 30px;" id="<?php echo $this->get_field_id('posts'); ?>" name="<?php echo $this->get_field_name('posts'); ?>" value="<?php echo $instance['posts']; ?>" />
      <br />
      <small>The widget will only show 3 posts, so you should set number of posts to scroll greater than 3.</small>
      </p>

      <p>
         <input class="checkbox" type="checkbox" <?php checked($instance['show_scroller'], 'on'); ?> id="<?php echo $this->get_field_id('show_scroller'); ?>" name="<?php echo $this->get_field_name('show_scroller'); ?>" />
         <label for="<?php echo $this->get_field_id('show_scroller'); ?>">Autoplay</label>
      </p>
	<p>
    <input class="checkbox" type="checkbox" <?php checked($instance['show_home'], 'on'); ?> id="<?php echo $this->get_field_id('show_home'); ?>" name="<?php echo $this->get_field_name('show_home'); ?>" />
    <label for="<?php echo $this->get_field_id('show_home'); ?>">Show on Home Page?</label>
    </p>
    <p>
    <input class="checkbox" type="checkbox" <?php checked($instance['show_page'], 'on'); ?> id="<?php echo $this->get_field_id('show_page'); ?>" name="<?php echo $this->get_field_name('show_page'); ?>" />
    <label for="<?php echo $this->get_field_id('show_page'); ?>">Show on Single Page?</label>
    </p>
    <p>
     <input class="checkbox" type="checkbox" <?php checked($instance['show_single'], 'on'); ?> id="<?php echo $this->get_field_id('show_single'); ?>" name="<?php echo $this->get_field_name('show_single'); ?>" />
     <label for="<?php echo $this->get_field_id('show_single'); ?>">Show on Single Post?</label>
    </p>
<!-- END ADMIN WIDGETS AREA -->

<?php }
}
?>