<?php
// Do not load directly...
if ( ! defined( 'ABSPATH' ) ) { die( 'Direct access forbidden.' ); }

class KtzSoc_Facebook_LikePage_Widget extends WP_Widget {
	
	/**
	 * Register widget with WordPress.
	 */
	function __construct() {
		parent::__construct(
			'kentooz_socializer_likepage_plugin', // Base ID
			__('KTZSOC Facebook Likebox', 'kentooz-socializer'), // Name
			array( 'description' => __( 'Drag and drop this widget for facebook like page.', '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'] );
		
        $url = $instance['url'];
		
		$width = apply_filters('widget_width_fblike', empty($instance['width']) ? '500' : $instance['width']);
		
        $height = $instance['height'];
        
		$show_faces = $instance['show_faces'] == 'true' ? 'true' : 'false';
        
		$stream = $instance['stream'] == 'true' ? 'true' : 'false';
        
		$header = $instance['header'] == 'true' ? 'true' : 'false';
 
		echo $args['before_widget'];
		if ( ! empty( $title ) )
			echo $args['before_title'] . $title . $args['after_title'];
			
		echo '<div class="fb-page ktzsoc-responsive" data-href="' . $url . '" data-width="' . $width . '" data-height="' . $height . '" data-hide-cover="' . $header . '" data-show-facepile="' .  $show_faces . '" data-show-posts="' . $stream . '" data-adapt-container-width="true"><div class="fb-xfbml-parse-ignore"></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['url'] = strip_tags( $new_instance['url'] );
		
        $instance['width'] = strip_tags( $new_instance['width'] );
		
        $instance['height'] = strip_tags( $new_instance['height'] );
		
		$instance['show_faces'] = isset($new_instance['show_faces']) ? true : false;
		
		$instance['stream'] = isset($new_instance['stream']) ? true : false;
		
		$instance['header'] = isset($new_instance['header']) ? true : false;
		
        return $instance;
    }

	public function form( $instance ) {
		$instance = wp_parse_args( (array) $instance, 
			array( 
				'title' => '',
				'url' => '', 
				'width' => '', 
				'height' => '', 
				'show_faces' => false, 
				'stream' => false, 
				'header' => false
			) 
		);
		
		$title = esc_attr( $instance['title'] );
		
		$url = esc_attr( $instance['url'] );
		
		$width = esc_attr( $instance['width'] );
		
		$height = esc_attr( $instance['height'] );
		?>

        <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('url'); ?>"><?php _e( 'URL:','kentooz-socializer'); ?></label> <input class="widefat" id="<?php echo $this->get_field_id('url'); ?>" name="<?php echo $this->get_field_name('url'); ?>" type="text" value="<?php echo $url; ?>" />
			<br />
            <small><?php _e( 'Fill with your facebook URL fanpage','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 like box','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('show_faces'); ?>"><?php _e( 'Show face:','kentooz-socializer' ); ?></label>
			<br />
            <input class="checkbox" type="checkbox" <?php checked($instance['show_faces'], true) ?> id="<?php echo $this->get_field_id('show_faces'); ?>" name="<?php echo $this->get_field_name('show_faces'); ?>" />
            <small><?php _e( 'Check for show face','kentooz-socializer' ); ?></small>
			<br />
			<label for="<?php echo $this->get_field_id('header'); ?>"><?php _e( 'Hide header/cover:','kentooz-socializer' ); ?></label>
			<br />
            <input class="checkbox" type="checkbox" <?php checked($instance['header'], true) ?> id="<?php echo $this->get_field_id('header'); ?>" name="<?php echo $this->get_field_name('header'); ?>" />
            <small><?php _e( 'Check for hide header(cover)','kentooz-socializer' ); ?></small>
			<br />        
			<label for="<?php echo $this->get_field_id('stream'); ?>"><?php _e( 'Show post:','kentooz-socializer' ); ?></label>
			<br />            
			<input class="checkbox" type="checkbox" <?php checked($instance['stream'], true) ?> id="<?php echo $this->get_field_id('stream'); ?>" name="<?php echo $this->get_field_name('stream'); ?>" />
            <small><?php _e( 'Check for show post','kentooz-socializer' ); ?></small><br />        
        </p>
	<?php
	}
}

// TODO: Remember to change 'Widget_Name' to match the class name definition
add_action( 'widgets_init', function(){
     register_widget( 'KtzSoc_Facebook_LikePage_Widget' );
});