<?php
/**
 * Widget Name: AIO Social Media
 *
 * @package All In One Custom Post
 * @since All In One Custom Post 1.0.0 
 */

if ( ! class_exists( 'vr_widget_social' ) ) {
	class vr_widget_social extends WP_Widget {

		// Widget Construct
		public function __construct() {
			parent::__construct(
				false,
				_x( 'AIO Social Media' , 'backend', PLUGIN_NAME),
				array(
					'description' => _x( 'Display social media link.', 'backend', PLUGIN_NAME),
					'classname'   => 'aio-widget-social-media',
				)
			);
		}

		// Widget Init
		public function widget( $args, $instance ) {
			
			$title = empty( $instance['title'] ) ? '' : $instance['title'];
			
			$message = empty( $instance['message'] ) ? '' : $instance['message'];
			$module = empty( $instance['module'] ) ? '' : $instance['module'];			
			$facebook = empty( $instance['facebook'] ) ? '' : $instance['facebook'];
			$twitter = empty( $instance['twitter'] ) ? '' : $instance['twitter'];
			$google = empty( $instance['google'] ) ? '' : $instance['google'];
			$linkedin = empty( $instance['linkedin'] ) ? '' : $instance['linkedin'];			
			$youtube = empty( $instance['youtube'] ) ? '' : $instance['youtube'];
			$email = empty( $instance['email'] ) ? '' : $instance['email'];				
			
			$animation = empty( $instance['animation'] ) ? '' : $instance['animation'];
			$ani  = '';
			
			if ( $animation != '' ) { $animation = 'data-animation="' . $animation . '"'; $ani = 'fx-start'; }

			echo $args['before_widget'];
			if ( $title ) { echo $args['before_title'] . $title . $args['after_title']; }
			?>
		
			<?php if ( $module == 'group' ) : ?>
			
				<div class="social-media-group clearfix <?php echo sanitize_html_class( $ani ); ?>" <?php echo $animation; ?>>
					<div class="social-media-head"><h3><?php echo wp_filter_kses( $message ); ?></div>
					<div class="social-media-body">
						<div class="social-media-item column-equal col-xs-2 ">
							<a href="<?php echo esc_url( $facebook ); ?>" target="_blank">
							<i class="fa fa-facebook icon-facebook"></i>
							</a>
						</div>
						<div class="social-media-item column-equal col-xs-2 ">
							<a href="<?php echo esc_url( $twitter ); ?>" target="_blank">
								<i class="fa fa-twitter icon-twitter"></i>
							</a>
						</div>
						<div class="social-media-item column-equal col-xs-2 ">
							<a href="<?php echo esc_url( $google ); ?>" target="_blank">
								<i class="fa fa-google-plus icon-googleplus"></i>
							</a>
						</div>
						<div class="social-media-item column-equal col-xs-2 ">
							<a href="<?php echo esc_url( $linkedin ); ?>" target="_blank">
								<i class="fa fa-linkedin icon-linkedin"></i>
							</a>
						</div>
						<div class="social-media-item column-equal col-xs-2 ">
							<a href="<?php echo esc_url( $youtube ); ?>" target="_blank">
								<i class="fa fa-youtube icon-youtube"></i>
							</a>
						</div>
						<div class="social-media-item column-equal col-xs-2 ">
							<a href="mailto:<?php echo sanitize_email( $email ); ?>" target="_blank">
								<i class="fa fa-envelope icon-email"></i>
							</a>
						</div>
					</div>
				</div>
				
			<?php elseif ( $module == 'widget' ) : ?>

				<div class="social-media-widget row row-equal">
					<?php if ($facebook != '' ) : ?>
					<div class="social-media-item column-equal col-xs-4">
						<a href="<?php echo esc_url( $facebook ); ?>" class="icon-facebook" target="_blank">
							<i class="fa fa-facebook"></i>
							<span><?php _e( 'Facebook', PLUGIN_NAME ); ?></span>
						</a>
					</div>
					<?php endif; ?>
					<?php if ($twitter != '' ) : ?>
					<div class="social-media-item column-equal col-xs-4">
						<a href="<?php echo esc_url( $twitter ); ?>" class="icon-twitter" target="_blank">
							<i class="fa fa-twitter"></i>
							<span><?php _e( 'Twitter', PLUGIN_NAME ); ?></span>
						</a>
					</div>
					<?php endif; ?>
					<?php if ($google != '' ) : ?>
					<div class="social-media-item column-equal col-xs-4">
						<a href="<?php echo esc_url( $google ); ?>" class="icon-googleplus" target="_blank">
							<i class="fa fa-google-plus"></i>
							<span><?php _e( 'Google+', PLUGIN_NAME ); ?></span>
						</a>
					</div>
					<?php endif; ?>
					<?php if ($linkedin != '' ) : ?>
					<div class="social-media-item column-equal col-xs-4">
						<a href="<?php echo esc_url( $linkedin ); ?>" class=" icon-linkedin" target="_blank">
							<i class="fa fa-linkedin"></i>
							<span><?php _e( 'LinkedIn', PLUGIN_NAME ); ?></span>
						</a>
					</div>
					<?php endif; ?>
					<?php if ($youtube != '' ) : ?>
					<div class="social-media-item column-equal col-xs-4">
						<a href="<?php echo esc_url( $youtube ); ?>" class="icon-youtube" target="_blank">
							<i class="fa fa-youtube"></i>
							<span><?php _e( 'YouTube', PLUGIN_NAME ); ?></span>
						</a>
					</div>
					<?php endif; ?>
					<?php if ($email != '' ) : ?>
					<div class="social-media-item column-equal col-xs-4">
						<a href="mailto:<?php echo sanitize_email( $email ); ?>" class="icon-mail" target="_blank">
						<i class="fa fa-envelope"></i>
							<span><?php _e( 'EMail', PLUGIN_NAME ); ?></span>
						</a>
					</div>
					<?php endif; ?>
				</div>
			
			<?php else : ?>

				<div class="social-media-widget row row-equal">
					<?php if ($facebook != '' ) : ?>
					<div class="social-media-item column-equal col-sm-2">
						<a href="<?php echo esc_url( $facebook ); ?>" class="icon-facebook" target="_blank">
							<i class="fa fa-facebook"></i>
							<span><?php _e( 'Facebook', PLUGIN_NAME ); ?></span>
						</a>
					</div>
					<?php endif; ?>
					<?php if ($twitter != '' ) : ?>
					<div class="social-media-item column-equal col-sm-2">
						<a href="<?php echo esc_url( $twitter ); ?>" class="icon-twitter" target="_blank">
							<i class="fa fa-twitter"></i>
							<span><?php _e( 'Twitter', PLUGIN_NAME ); ?></span>
						</a>
					</div>
					<?php endif; ?>
					<?php if ($google != '' ) : ?>
					<div class="social-media-item column-equal col-sm-2">
						<a href="<?php echo esc_url( $google ); ?>" class="icon-googleplus" target="_blank">
							<i class="fa fa-google-plus"></i>
							<span><?php _e( 'Google+', PLUGIN_NAME ); ?></span>
						</a>
					</div>
					<?php endif; ?>
					<?php if ($linkedin != '' ) : ?>
					<div class="social-media-item column-equal col-sm-2">
						<a href="<?php echo esc_url( $linkedin ); ?>" class=" icon-linkedin" target="_blank">
							<i class="fa fa-linkedin"></i>
							<span><?php _e( 'LinkedIn', PLUGIN_NAME ); ?></span>
						</a>
					</div>
					<?php endif; ?>
					<?php if ($youtube != '' ) : ?>
					<div class="social-media-item column-equal col-sm-2">
						<a href="<?php echo esc_url( $youtube ); ?>" class="icon-youtube" target="_blank">
							<i class="fa fa-youtube"></i>
							<span><?php _e( 'YouTube', PLUGIN_NAME ); ?></span>
						</a>
					</div>
					<?php endif; ?>
					<?php if ($email != '' ) : ?>
					<div class="social-media-item column-equal col-sm-2">
						<a href="mailto:<?php echo sanitize_email( $email ); ?>" class="icon-mail" target="_blank">
						<i class="fa fa-envelope"></i>
							<span><?php _e( 'EMail', PLUGIN_NAME ); ?></span>
						</a>
					</div>
					<?php endif; ?>
				</div>
				
			<?php endif; ?>
		
			<?php
			echo $args['after_widget'];
		}

		// Form Update
		public function update( $new_instance, $old_instance ) {
			$instance = array();

			$instance['title'] 		= wp_strip_all_tags( $new_instance['title'] );
			$instance['message']   	= wp_filter_kses( $new_instance['message'] );
			$instance['facebook']   = esc_url( $new_instance['facebook'] );
			$instance['twitter'] 	= esc_url( $new_instance['twitter'] );		
			$instance['google'] 	= esc_url( $new_instance['google'] );
			$instance['linkedin']   = esc_url( $new_instance['linkedin'] );
			$instance['youtube'] 	= esc_url( $new_instance['youtube'] );
			$instance['email'] 		= sanitize_email( $new_instance['email'] );
			$instance['module']   	= esc_attr( $new_instance['module'] );
			$instance['animation'] 	= esc_attr( $new_instance['animation'] );

			return $instance;
		}

		// Widget Form
		public function form( $instance ) {

			$title = empty( $instance['title'] ) ? '' : $instance['title'];
			
			$message = empty( $instance['message'] ) ? 'Connect with us on <strong>World Wide Web</strong>' : $instance['message'];
			$module = empty( $instance['module'] ) ? 'widget' : $instance['module'];			
			$facebook = empty( $instance['facebook'] ) ? '' : $instance['facebook'];
			$twitter = empty( $instance['twitter'] ) ? '' : $instance['twitter'];
			$google = empty( $instance['google'] ) ? '' : $instance['google'];
			$linkedin = empty( $instance['linkedin'] ) ? '' : $instance['linkedin'];			
			$youtube = empty( $instance['youtube'] ) ? '' : $instance['youtube'];
			$email = empty( $instance['email'] ) ? '' : $instance['email'];				
			
			$animation = empty( $instance['animation'] ) ? 'bounceIn' : $instance['animation'];
			
			?>

			<p>
				<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Widget Title :', PLUGIN_NAME ); ?></label>
				<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo esc_attr( $title ); ?>" style="width:100%;" type="text" />
			</p>
			<!--
			<p>
				<label for="<?php echo $this->get_field_id( 'message' ); ?>"><?php _e( 'Caption :', PLUGIN_NAME ); ?></label>
				<input id="<?php echo $this->get_field_id( 'message' ); ?>" name="<?php echo $this->get_field_name( 'message' ); ?>" value="<?php echo wp_filter_kses( $message ); ?>" style="width:100%;" />
				<small>Note: widget message for widget group only.</small>
			</p>
			-->
			<p>
				<label for="<?php echo $this->get_field_id( 'module' ); ?>"><?php _e( 'Module :', PLUGIN_NAME ); ?></label>
				<select id="<?php echo $this->get_field_id( 'module' ); ?>" name="<?php echo $this->get_field_name( 'module' ); ?>" style="width:100%;">
					<option value="widget" <?php if ( $module == 'widget' ) { echo 'selected="selected"'; } ?>>Widget</option>
				</select>
			</p>
			<p>
				<label for="<?php echo $this->get_field_id( 'facebook' ); ?>"><?php _e( 'Facebook :', PLUGIN_NAME ); ?></label>
				<input id="<?php echo $this->get_field_id( 'facebook' ); ?>" name="<?php echo $this->get_field_name( 'facebook' ); ?>" value="<?php echo esc_url( $facebook ); ?>" style="width:100%;" />
			</p>
			<p>
				<label for="<?php echo $this->get_field_id( 'twitter' ); ?>"><?php _e( 'Twitter :', PLUGIN_NAME ); ?></label>
				<input id="<?php echo $this->get_field_id( 'twitter' ); ?>" name="<?php echo $this->get_field_name( 'twitter' ); ?>" value="<?php echo esc_url( $twitter ); ?>" style="width:100%;" />
			</p>
			<p>
				<label for="<?php echo $this->get_field_id( 'google' ); ?>"><?php _e( 'Google :', PLUGIN_NAME ); ?></label>
				<input id="<?php echo $this->get_field_id( 'google' ); ?>" name="<?php echo $this->get_field_name( 'google' ); ?>" value="<?php echo esc_url( $google ); ?>" style="width:100%;" />
			</p>
			<p>
				<label for="<?php echo $this->get_field_id( 'linkedin' ); ?>"><?php _e( 'Linkedin :', PLUGIN_NAME ); ?></label>
				<input id="<?php echo $this->get_field_id( 'linkedin' ); ?>" name="<?php echo $this->get_field_name( 'linkedin' ); ?>" value="<?php echo esc_url($linkedin ); ?>" style="width:100%;" />
			</p>
			<p>
				<label for="<?php echo $this->get_field_id( 'youtube' ); ?>"><?php _e( 'Youtube :', PLUGIN_NAME ); ?></label>
				<input id="<?php echo $this->get_field_id( 'youtube' ); ?>" name="<?php echo $this->get_field_name( 'youtube' ); ?>" value="<?php echo esc_url( $youtube ); ?>" style="width:100%;" />
			</p>
			<p>
				<label for="<?php echo $this->get_field_id( 'email' ); ?>"><?php _e( 'EMail :', PLUGIN_NAME ); ?></label>
				<input id="<?php echo $this->get_field_id( 'email' ); ?>" name="<?php echo $this->get_field_name( 'email' ); ?>" value="<?php echo sanitize_email( $email ); ?>" style="width:100%;" />
			</p>
			<p>
				<label for="<?php echo $this->get_field_id( 'animation' ); ?>"><?php _e( 'Animation :', PLUGIN_NAME ); ?></label>
				<select id="<?php echo $this->get_field_id( 'animation' ); ?>" name="<?php echo $this->get_field_name( 'animation' ); ?>" style="width:100%;">
					<option value="" <?php if ( $animation == '' ) { echo 'selected="selected"'; } ?>>None</option>
					<option value="fadeIn" <?php if ( $animation == 'fadeIn' ) { echo 'selected="selected"'; } ?>>fadeIn</option>
					<option value="fadeInUp" <?php if ( $animation == 'fadeInUp' ) { echo 'selected="selected"'; } ?>>fadeInUp</option>
					<option value="fadeInDown" <?php if ( $animation == 'fadeInDown' ) { echo 'selected="selected"'; } ?>>fadeInDown</option>
					<option value="fadeInLeft" <?php if ( $animation == 'fadeInLeft' ) { echo 'selected="selected"'; } ?>>fadeInLeft</option>
					<option value="fadeInRight" <?php if ( $animation == 'fadeInRight' ) { echo 'selected="selected"'; } ?>>fadeInRight</option>
					<option value="rotateFromLeft" <?php if ( $animation == 'rotateFromLeft' ) { echo 'selected="selected"'; } ?>>rotateFromLeft</option>
					<option value="rotateFromRight" <?php if ( $animation == 'rotateFromRight' ) { echo 'selected="selected"'; } ?>>rotateFromRight</option>
					<option value="flipInX" <?php if ( $animation == 'flipInX' ) { echo 'selected="selected"'; } ?>>flipInX</option>
					<option value="flipOutX" <?php if ( $animation == 'flipOutX' ) { echo 'selected="selected"'; } ?>>flipOutX</option>
					<option value="flipInY" <?php if ( $animation == 'flipInY' ) { echo 'selected="selected"'; } ?>>flipInY</option>
					<option value="flipOutY" <?php if ( $animation == 'flipOutY' ) { echo 'selected="selected"'; } ?>>flipOutY</option>
					<option value="bounceIn" <?php if ( $animation == 'bounceIn' ) { echo 'selected="selected"'; } ?>>zoomIn</option>
					<option value="bounceInUp" <?php if ( $animation == 'bounceInUp' ) { echo 'selected="selected"'; } ?>>bounceInUp</option>
					<option value="bounceInDown" <?php if ( $animation == 'bounceInDown' ) { echo 'selected="selected"'; } ?>>bounceInDown</option>
					<option value="bounceInLeft" <?php if ( $animation == 'bounceInLeft' ) { echo 'selected="selected"'; } ?>>bounceInLeft</option>
					<option value="bounceInRight" <?php if ( $animation == 'bounceInRight' ) { echo 'selected="selected"'; } ?>>bounceInRight</option>
				</select>
			</p>

			<?php
		}

	}
	add_action( 'widgets_init', create_function( '', 'register_widget( "vr_widget_social" );' ) );
}