<?php
// Do not load directly...
if ( ! defined( 'ABSPATH' ) ) { die( 'Direct access forbidden.' ); }

class KtzSoc_Google_Badge_Widget extends WP_Widget {
	
	/**
	 * Register widget with WordPress.
	 */
	function __construct() {
		parent::__construct(
			'kentooz_socializer_googlebadge_plugin', // Base ID
			__('KTZSOC Google Badge', 'kentooz-socializer'), // Name
			array( 'description' => __( 'Drag and drop this widget for display google badge.', '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'] );
        
		$page_type = empty( $instance['page_type'] ) ? 'profile' : $instance['page_type'];
		
        $url = $instance['url'];
        
		$layout = empty( $instance['layout'] ) ? 'portrait' : $instance['layout'];
		
        $width = $instance['width'];
        
		$theme = empty( $instance['theme'] ) ? 'light' : $instance['theme'];
		
		$tagline = $instance['tagline'] == 'true' ? 'true' : 'false';
        
		$cover_photo = $instance['cover_photo'] == 'true' ? 'true' : 'false';
 
		echo $args['before_widget'];
		if ( ! empty( $title ) )
			echo $args['before_title'] . $title . $args['after_title'];
			
		if($url) :
			echo '<div id="google-badge" style="width: 100%;">';
			echo '<div ';
			if($page_type == 'profile') { 
				echo 'class="g-person"';
			} elseif($page_type == 'page') { 
				echo 'class="g-page"';
			} elseif($page_type == 'community') { 
				echo 'class="g-community"';
			} 
			echo 'data-width="' . $width . '" ';
			echo 'data-href="' . $url . '" ';
			echo 'data-layout="' . $layout . '" ';
			echo 'data-theme="' . $theme . '" ';
			echo 'data-rel="publisher" ';
			echo 'data-showtagline="' . $tagline . '" ';
			echo 'data-showcoverphoto="' . $cover_photo . '">';
			echo '</div>';
	        echo '<script type="text/javascript">document.getElementsByClassName(\'g-person\')[0].setAttribute(\'data-width\', document.getElementById(\'google-badge\').clientWidth);</script>';
			echo '</div>';
				
		endif;
		
		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['page_type'] = strip_tags( $new_instance['page_type'] );
		
        $instance['url'] = strip_tags( $new_instance['url'] );
		
        $instance['width'] = strip_tags( $new_instance['width'] );
		
        $instance['layout'] = strip_tags( $new_instance['layout'] );
		
		$instance['theme'] = strip_tags( $new_instance['theme'] );
		
		$instance['tagline'] = isset($new_instance['tagline']) ? true : false;
		
		$instance['cover_photo'] = isset($new_instance['cover_photo']) ? true : false;
		
        return $instance;
    }

	public function form( $instance ) {
		$instance = wp_parse_args( (array) $instance, 
			array(
				'title' => '',
				'url' => '', 
				'width' => '', 
				'layout' => 'portrait', 
				'theme' => 'light', 
				'page_type' => 'profile',
				'tagline' => true,
				'cover_photo' => true
			)
		);
		
		$title = esc_attr( $instance['title'] );
		
		$page_types = array( 
			'profile'  => esc_attr__( 'Profile', 'kentooz-socializer' ), 
			'page' => esc_attr__( 'Page', 'kentooz-socializer' ), 
			'community' => esc_attr__( 'Community', 'kentooz-socializer' )
		);
		
		$url = esc_attr( $instance['url'] );
		
		$width = esc_attr( $instance['width'] );
		
		$layouts = array(
			'portrait'  => esc_attr__( 'Portrait', 'kentooz-socializer' ), 
			'landscape' => esc_attr__( 'Landscape', 'kentooz-socializer' )
		);
		
		$themes = array( 
			'light'  => esc_attr__( 'Light', 'kentooz-socializer' ), 
			'dark' => esc_attr__( 'Dark', 'kentooz-socializer' )
		);
		
		?>

        <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('page_type'); ?>"><?php _e( 'Page Type:', 'kentooz-socializer' ); ?></label>
			<select id="<?php echo $this->get_field_id( 'page_type' ); ?>" name="<?php echo $this->get_field_name( 'page_type' ); ?>" class="widefat">
				<?php foreach ( $page_types as $k => $v ) { ?>
					<option value="<?php echo esc_attr( $k ); ?>" <?php selected( $instance['page_type'], $k ); ?>><?php echo esc_html( $v ); ?></option>
				<?php } ?>
			</select>	
			<br/>
			<small><?php _e( 'Select page type your Google+ URL, profile, page or community.', 'kentooz-socializer' ); ?></small>
        </p>
		
        <p>
			<label for="<?php echo $this->get_field_id('url'); ?>"><?php _e( 'Google+ Page 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 Google+ Page URL.','kentooz-socializer' ); ?></small>
		</p>
		
		<p>
			<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 Google+ Badge','kentooz-socializer' ); ?></small>
        </p>
		
        <p>
			<label for="<?php echo $this->get_field_id('layout'); ?>"><?php _e( 'Layout:', 'kentooz-socializer' ); ?></label>
			<select id="<?php echo $this->get_field_id( 'layout' ); ?>" name="<?php echo $this->get_field_name( 'layout' ); ?>" class="widefat">
				<?php foreach ( $layouts as $k => $v ) { ?>
					<option value="<?php echo esc_attr( $k ); ?>" <?php selected( $instance['layout'], $k ); ?>><?php echo esc_html( $v ); ?></option>
				<?php } ?>
			</select>	
			<br/>
			<small><?php _e( 'Select your Google+ URL layout.', '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 Google+ Badge dark or light theme.', 'kentooz-socializer' ); ?></small>
        </p>
		
		<p>
			<label for="<?php echo $this->get_field_id('tagline'); ?>"><?php _e( 'Tagline:','kentooz-socializer' ); ?></label>
			<br />
            <input class="checkbox" type="checkbox" <?php checked($instance['tagline'], true) ?> id="<?php echo $this->get_field_id('tagline'); ?>" name="<?php echo $this->get_field_name('tagline'); ?>" />
            <small><?php _e( 'Check for display tagline','kentooz-socializer' ); ?></small>
		</p>
		
		<p>
			<label for="<?php echo $this->get_field_id('cover_photo'); ?>"><?php _e( 'Cover photo:','kentooz-socializer' ); ?></label>
			<br /> 
            <input class="checkbox" type="checkbox" <?php checked($instance['cover_photo'], true) ?> id="<?php echo $this->get_field_id('cover_photo'); ?>" name="<?php echo $this->get_field_name('cover_photo'); ?>" />
            <small><?php _e( 'Check for display cover photo','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_Google_Badge_Widget' );
});