<?php
  add_action('widgets_init', 'tweets_load_widgets');
  function tweets_load_widgets()
  {
    register_widget('Latest_Tweets');
  }
    class Latest_Tweets extends WP_Widget {
  function Latest_Tweets()
  {
    $widget_ops = array(
   'classname' => 'tweets',
   'description' => 'Show Recent Tweets.');
    $control_ops = array(
   'id_base' => 'latest-tweets');
    $this->WP_Widget('latest-tweets', 'GAMELEON: Recent Tweets', $widget_ops, $control_ops);
  }
  function widget($args, $instance)
  {
    extract($args);

/* User selected settings. */
    $title = apply_filters('widget_title', $instance['title']);
    $twitter_username = $instance['twitter_username'];
    $widget_id = $instance['widget_id'];
    $tweets_number = $instance['tweets_number'];
    $twitter_height = $instance['twitter_height'];
    $twitter_theme = $instance['twitter_theme'];
    $twitter_link_color = $instance['twitter_link_color'];
	$hide_header = isset($instance['hide_header']) ? 'true' : 'false';
	$hide_footer = isset($instance['hide_footer']) ? 'true' : 'false';
	$hide_borders = isset($instance['hide_borders']) ? 'true' : 'false';
	$hide_scrollbar = isset($instance['hide_scrollbar']) ? 'true' : 'false';
	$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;
  }
?>

<!-- START WIDGET -->
<div class="grid-sidebar">

<a class="twitter-timeline" data-tweet-limit="<?php echo $tweets_number; ?>" data-chrome="noborders noscrollbar <?php if($hide_footer == 'true'): ?>nofooter<?php endif; ?>"  data-theme="<?php echo $twitter_theme; ?>" data-link-color="<?php echo $twitter_link_color; ?>" height="<?php echo $twitter_height; ?>"  href="https://twitter.com/twitterapi" data-widget-id="<?php echo $widget_id; ?>">Tweets by @<?php echo $twitter_username; ?></a>
 
    <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>

</div>


<!-- END WIDGET -->

<?php

    echo $after_widget;
  }
  }

  function update($new_instance, $old_instance)
  {
    $instance = $old_instance;
    $instance['title'] = strip_tags($new_instance['title']);
    $instance['twitter_username'] = strip_tags($new_instance['twitter_username']);
    $instance['widget_id'] = strip_tags($new_instance['widget_id']);
    $instance['tweets_number'] = $new_instance['tweets_number'];
    $instance['twitter_height'] = strip_tags($new_instance['twitter_height']);
    $instance['twitter_theme'] = strip_tags($new_instance['twitter_theme']);
    $instance['twitter_link_color'] = strip_tags($new_instance['twitter_link_color']);
    $instance['hide_header'] = $new_instance['hide_header'];
    $instance['hide_footer'] = $new_instance['hide_footer'];
    $instance['hide_borders'] = $new_instance['hide_borders'];
    $instance['hide_scrollbar'] = $new_instance['hide_scrollbar'];
    $instance['show_home'] = $new_instance['show_home'];
    $instance['show_page'] = $new_instance['show_page'];
	$instance['show_single'] = $new_instance['show_single'];
    return $instance;
  }
  function form($instance)
  {
    $defaults = array(
    'title' => 'Recent Tweets',
    'twitter_username' => '',
    'widget_id' => '',
    'tweets_number' => 3,
    'twitter_height' => 400,
    'twitter_theme' => '',
    'twitter_link_color' => '#00B2FF',
	'hide_header' => 'on',
	'hide_footer' => 'on',
	'hide_borders' => 'on',
	'hide_scrollbar' => 'on',
	'show_home' => 'on',
    'show_page' => 'on',
    'show_single' => 'on'
);
     $instance = wp_parse_args((array) $instance, $defaults); ?>

<!-- START ADMIN WIDGETS AREA -->
    <p>
      <label for="<?php echo $this->get_field_id('title'); ?>"><strong>Title:</strong></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('twitter_username'); ?>"><strong>Twitter Username:</strong></label>
	<input class="widefat" style="width: 216px;" id="<?php echo $this->get_field_id('twitter_username'); ?>" name="<?php echo $this->get_field_name('twitter_username'); ?>" value="<?php echo $instance['twitter_username']; ?>" />
	</p>

    <p>
<small>Now, <a style="color:red;" target="blank" href="https://twitter.com/settings/widgets">create a Twitter widget</a> and obtain a widget ID. After widget creation, the Widget ID will be a number on the url between the widgets and edit portion of the url. Example: <br />www.twitter.com/settings/widgets/<span style="color:red">3469915</span>/edit... See the theme documentation for more info.</small><br /><br />

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

    <p>
	<label for="<?php echo $this->get_field_id('tweets_number'); ?>"><strong>Limit  number of Tweets:</strong>
<br /><small>*This option will fix the height of the timeline to a preset number of Tweets without scrolling. Enter any value between 1 and 20 Tweets. Note that since the widget is of a fixed size, it will not poll for updates and will ignore the heigh of timeline value and Timeline Scrollbar option bellow when using this option. Just leave it blank if you want to scroll all the Tweets but adjust your height of timeline bellow.</small>
</label>
	<input class="widefat" style="width: 216px;" id="<?php echo $this->get_field_id('tweets_number'); ?>" name="<?php echo $this->get_field_name('tweets_number'); ?>" value="<?php echo $instance['tweets_number']; ?>" />
	</p>

    <p>
	<label for="<?php echo $this->get_field_id('twitter_height'); ?>"><strong>Height of Timeline:</strong><br /><small>*The minimum height is 200</small></label>
	<input class="widefat" style="width: 216px;" id="<?php echo $this->get_field_id('twitter_height'); ?>" name="<?php echo $this->get_field_name('twitter_height'); ?>" value="<?php echo $instance['twitter_height']; ?>" />
	</p>

    <p>
	<label for="<?php echo $this->get_field_id('twitter_theme'); ?>"><strong>Timeline Theme Background Color:</strong><br /><small>*Leave the field empty for default theme(white) or enter <em>dark</em> for a dark theme</small></label>
	<input class="widefat" style="width: 216px;" id="<?php echo $this->get_field_id('twitter_theme'); ?>" name="<?php echo $this->get_field_name('twitter_theme'); ?>" value="<?php echo $instance['twitter_theme']; ?>" />
	</p>

    <p>
	<label for="<?php echo $this->get_field_id('twitter_link_color'); ?>"><strong>Timeline Links Color:</strong><br /><small>*Set the links color by adding the desired hex code. The default links color is <em>#00B2FF</em>. Please use hex format color e.g. <em>#00B2FF</em></small></label>
	<input class="widefat" style="width: 216px;" id="<?php echo $this->get_field_id('twitter_link_color'); ?>" name="<?php echo $this->get_field_name('twitter_link_color'); ?>" value="<?php echo $instance['twitter_link_color']; ?>" />
	</p> 

	<p>
    <input class="checkbox" type="checkbox" <?php checked($instance['hide_footer'], 'on'); ?> id="<?php echo $this->get_field_id('hide_footer'); ?>" name="<?php echo $this->get_field_name('hide_footer'); ?>" />
    <label for="<?php echo $this->get_field_id('hide_footer'); ?>">Hide Timeline Footer</label>
    </p>

<br /> 
 <div style="border:1px solid #008EFF; padding:10px;"> 

	<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></div>
<!-- END ADMIN WIDGETS AREA -->

<?php
}
}
?>