<?php
// Do not load directly...
if ( ! defined( 'ABSPATH' ) ) { die( 'Direct access forbidden.' ); }

class KtzSoc_Twiter_Timeline_Widget extends WP_Widget {
	
	/**
	 * Register widget with WordPress.
	 */
	function __construct() {
		parent::__construct(
			'kentooz_socializer_timeline_plugin', // Base ID
			__('KTZSOC Twitter Timeline', 'kentooz-socializer'), // Name
			array( 'description' => __( 'Drag and drop this widget for twitter timeline.', 'kentooz-socializer' ), ) // Args
		);
	}
	
	/**
	 * Front-end display of widget.
	 *
	 * @see WP_Widget::widget()
	 *
	 * @param array $args     Widget arguments.
	 * @param array $instance Saved values from database.
	 */
	public function widget( $args, $instance ) {
		extract( $args );
		
		$title = apply_filters( 'widget_title', $instance['title'] );
		
        $username = $instance['username'];
		
        $width = $instance['width'];
		
		$height = apply_filters('widget_height_timeline', empty($instance['height']) ? '450' : $instance['height']);
        
		$theme = empty( $instance['theme'] ) ? 'light' : $instance['theme'];
        
		$linkcolor = empty( $instance['linkcolor'] ) ? '#cc0000' : $instance['linkcolor'];
        
		$widgetid = empty( $instance['widgetid'] ) ? '675105436337590272' : $instance['widgetid'];
 
		echo $args['before_widget'];
		if ( ! empty( $title ) )
			echo $args['before_title'] . $title . $args['after_title'];
			
        echo '<div style="display:block;width:100%;overflow:hidden">';
			echo '<a class="twitter-timeline" data-dnt="true" href="https://twitter.com/' . $username . '" min-width="' . $width . '" height="' . $height . '" data-theme="' . $theme . '" data-link-color="' . $linkcolor . '" data-widget-id="' . $widgetid . '">Twitter Tweets</a>';
		echo '</div>';
		
		echo $args['after_widget'];
	}

	/* Update the widget settings. */
    public function update( $new_instance, $old_instance ) {
    	$instance = $old_instance;
		
		$instance['title'] = strip_tags($new_instance['title']);
		
        $instance['username'] = strip_tags( $new_instance['username'] );
		
        $instance['width'] = strip_tags( $new_instance['width'] );
		
        $instance['height'] = strip_tags( $new_instance['height'] );
		
		$instance['theme'] = strip_tags( $new_instance['theme'] );
		
		$instance['linkcolor'] = strip_tags( $new_instance['linkcolor'] );
		
		$instance['widgetid'] = strip_tags( $new_instance['widgetid'] );
		
        return $instance;
    }

	public function form( $instance ) {
		$instance = wp_parse_args( (array) $instance, 
			array( 
				'title' => '',
				'username' => '', 
				'width' => '', 
				'height' => '450', 
				'theme' => 'light', 
				'linkcolor' => '#cc0000', 
				'widgetid' => ''
			) 
		);
		
		$title = esc_attr( $instance['title'] );
		
		$username = esc_attr( $instance['username'] );
		
		$width = esc_attr( $instance['width'] );
		
		$height = esc_attr( $instance['height'] );
		
		$themes = array( 
			'light'  => esc_attr__( 'Light', 'kentooz-socializer' ), 
			'dark' => esc_attr__( 'Dark', 'kentooz-socializer' )
		);
		
		$linkcolor = esc_attr( $instance['linkcolor'] );
		
		$widgetid = esc_attr( $instance['widgetid'] );
		?>

        <p>
			<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Title:','kentooz-socializer'); ?></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('username'); ?>"><?php _e( 'Username:','kentooz-socializer'); ?></label> 
			<input class="widefat" id="<?php echo $this->get_field_id('username'); ?>" name="<?php echo $this->get_field_name('username'); ?>" type="text" value="<?php echo $username; ?>" />
			<br />
            <small><?php _e( 'Fill with your twitter Username example g14nnakal.','kentooz-socializer' ); ?></small>
			<br />
			<label for="<?php echo $this->get_field_id('width'); ?>"><?php _e( 'Width:','kentooz-socializer'); ?></label> 
			<input class="widefat" id="<?php echo $this->get_field_id('width'); ?>" name="<?php echo $this->get_field_name('width'); ?>" type="text" value="<?php echo $width; ?>" />
			<br />
            <small><?php _e( 'Fill width twitter timeline','kentooz-socializer' ); ?></small>
        </p>
		
        <p>
			<label for="<?php echo $this->get_field_id('height'); ?>"><?php _e( 'Height:','kentooz-socializer'); ?></label> <input class="widefat" id="<?php echo $this->get_field_id('height'); ?>" name="<?php echo $this->get_field_name('height'); ?>" type="text" value="<?php echo $height; ?>" />
            <br />
            <small><?php _e( 'Fill height like box','kentooz-socializer' ); ?></small>
        </p>
		
        <p>
			<label for="<?php echo $this->get_field_id('theme'); ?>"><?php _e( 'Theme:', 'kentooz-socializer' ); ?></label>
			<select id="<?php echo $this->get_field_id( 'theme' ); ?>" name="<?php echo $this->get_field_name( 'theme' ); ?>" class="widefat">
				<?php foreach ( $themes as $k => $v ) { ?>
					<option value="<?php echo esc_attr( $k ); ?>" <?php selected( $instance['theme'], $k ); ?>><?php echo esc_html( $v ); ?></option>
				<?php } ?>
			</select>	
			<br/>
			<small><?php _e( 'Select theme timeline dark or light theme.', 'kentooz-socializer' ); ?></small>
        </p>
		
        <p>
			<label for="<?php echo $this->get_field_id('linkcolor'); ?>"><?php _e( 'Link color:','kentooz-socializer'); ?></label> 
			<input class="widefat" id="<?php echo $this->get_field_id('linkcolor'); ?>" name="<?php echo $this->get_field_name('linkcolor'); ?>" type="text" value="<?php echo $linkcolor; ?>" />
            <small><?php _e( 'Fill with color for link in timeline.','kentooz-socializer' ); ?></small>
        </p>
		
        <p>
			<label for="<?php echo $this->get_field_id('widgetid'); ?>"><?php _e( 'Widget ID:','kentooz-socializer'); ?></label> 
			<input class="widefat" id="<?php echo $this->get_field_id('widgetid'); ?>" name="<?php echo $this->get_field_name('widgetid'); ?>" type="text" value="<?php echo $widgetid; ?>" />
            <small><?php _e( 'Fill with your twitter widget ID. You can found answer about widget ID <a href="http://stackoverflow.com/questions/16375116/what-is-data-widget-id-in-twitter-api-how-i-can-get-the-data-widget-id">here</a>','kentooz-socializer' ); ?></small>
        </p>
		
	<?php
	}
}

// TODO: Remember to change 'Widget_Name' to match the class name definition
add_action( 'widgets_init', function(){
     register_widget( 'KtzSoc_Twiter_Timeline_Widget' );
});