<?php
/**
 * Plugin Name: Euthemians Social Networking
 * Description: A widget that displays social networking links.
 * @author		Euthemians Team
 * @URI			http://euthemians.com
 */

add_action( 'widgets_init', 'engic_eutf_widget_social' );

function engic_eutf_widget_social() {
	register_widget( 'Engic_EUTF_Widget_Social' );
}

class Engic_EUTF_Widget_Social extends WP_Widget {

	function __construct() {
		$widget_ops = array(
			'classname' => 'eut-social',
			'description' => esc_html__( 'A widget that displays social networking links', 'engic' ),
		);
		$control_ops = array(
			'width' => 400,
			'height' => 600,
			'id_base' => 'eut-widget-social',
		);
		parent::__construct( 'eut-widget-social', '(Euthemians) ' . esc_html__( 'Social Networking', 'engic' ), $widget_ops, $control_ops );
	}

	function Engic_EUTF_Widget_Social() {
		$this->__construct();
	}

	function widget( $args, $instance ) {
	
		global $engic_eutf_social_list_extended;
		
		extract( $args );

		//Our variables from the widget settings.
		$title = apply_filters( 'widget_title', $instance['title'] );

		echo $before_widget; // XSS OK

		// Display the widget title
		if ( $title ) {
			echo $before_title . esc_html( $title ) . $after_title; // XSS OK
		}
	?>

		<ul>
		<?php

		$icon_size = engic_eutf_array_value( $instance, 'icon_size' );
		$shape = engic_eutf_array_value( $instance, 'shape' );
		$shape_type = engic_eutf_array_value( $instance, 'shape_type' );

		$social_shape_classes = array();

		array_push( $social_shape_classes, 'eut-' . $icon_size );
		if ( !empty( $shape ) ) {
			array_push( $social_shape_classes, 'eut-' . $shape );
			array_push( $social_shape_classes, 'eut-' . $shape_type );
		}

		$social_shape_class_string = implode( ' ', $social_shape_classes );
		
		foreach ( $engic_eutf_social_list_extended as $social_item ) {

			$social_item_url = engic_eutf_array_value( $instance, $social_item['url'] );
			if ( ! empty( $social_item_url ) ) {

				if ( 'skype' == $social_item['id'] ) {
		?>
					<li>
						<a href="<?php echo esc_url( $social_item_url, array( 'skype', 'http', 'https' ) ); ?>" class="<?php echo esc_attr( $social_shape_class_string ); ?> <?php echo esc_attr( $social_item['class'] ); ?>"></a>
					</li>
		<?php
				} else {
		?>
					<li>
						<a href="<?php echo esc_url( $social_item_url ); ?>" class="<?php echo esc_attr( $social_shape_class_string ); ?> <?php echo esc_attr( $social_item['class'] ); ?>" target="_blank"></a>
					</li>
		<?php
				}
			}
		}
		?>
		</ul>


	<?php

		echo $after_widget; // XSS OK
	}

	//Update the widget

	function update( $new_instance, $old_instance ) {
	
		global $engic_eutf_social_list_extended;
		
		$instance = $old_instance;
		//Strip tags from title to remove HTML
		$instance['title'] = strip_tags( $new_instance['title'] );
		$instance['icon_size'] = strip_tags( $new_instance['icon_size'] );
		$instance['shape'] = strip_tags( $new_instance['shape'] );
		$instance['shape_type'] = strip_tags( $new_instance['shape_type'] );

		foreach ( $engic_eutf_social_list_extended as $social_item ) {
			$instance[ $social_item['url'] ] = strip_tags( $new_instance[ $social_item['url'] ] );
		}		

		return $instance;
	}


	function form( $instance ) {

		global $engic_eutf_social_list_extended;
		
		//Set up some default widget settings.
		$defaults = array(
			'title' => '',
			'icon_size' => 'medium',
			'shape' => 'square',
			'shape_type' => 'outline',
		);

		$instance = wp_parse_args( (array) $instance, $defaults );

		?>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php echo esc_html__( 'Title:', 'engic' ); ?></label>
			<input id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" style="width:100%;" />
		</p>

		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'icon_size' ) ); ?>"><?php echo esc_html__( 'Icon Size:', 'engic' ); ?></label>
			<select name="<?php echo esc_attr( $this->get_field_name( 'icon_size' ) ); ?>" style="width:100%;">
				<option value="large" <?php selected( "large", engic_eutf_array_value( $instance, 'icon_size' ) ); ?>><?php echo esc_html__( "Large", 'engic' ); ?></option>
				<option value="medium" <?php selected( "medium", engic_eutf_array_value( $instance, 'icon_size' ) ); ?>><?php echo esc_html__( "Medium", 'engic' ); ?></option>
				<option value="small" <?php selected( "small", engic_eutf_array_value( $instance, 'icon_size' ) ); ?>><?php echo esc_html__( "Small", 'engic' ); ?></option>
			</select>
		</p>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'shape' ) ); ?>"><?php echo esc_html__( 'Shape:', 'engic' ); ?></label>
			<select  name="<?php echo esc_attr( $this->get_field_name( 'shape' ) ); ?>" style="width:100%;">
				<option value="square" <?php selected( "square", engic_eutf_array_value( $instance, 'shape' ) ); ?>><?php echo esc_html__( "Square", 'engic' ); ?></option>
				<option value="round" <?php selected( "round", engic_eutf_array_value( $instance, 'shape' ) ); ?>><?php echo esc_html__( "Round", 'engic' ); ?></option>
				<option value="circle" <?php selected( "circle", engic_eutf_array_value( $instance, 'shape' ) ); ?>><?php echo esc_html__( "Circle", 'engic' ); ?></option>
				<option value="" <?php selected( "", engic_eutf_array_value( $instance, 'shape' ) ); ?>><?php echo esc_html__( "None", 'engic' ); ?></option>
			</select>
		</p>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'shape_type' ) ); ?>"><?php echo esc_html__( 'Shape Type:', 'engic' ); ?></label>
			<select  name="<?php echo esc_attr( $this->get_field_name( 'shape_type' ) ); ?>" style="width:100%;">
				<option value="simple" <?php selected( "simple", engic_eutf_array_value( $instance, 'shape_type' ) ); ?>><?php echo esc_html__( "Simple", 'engic' ); ?></option>
				<option value="outline" <?php selected( "outline", engic_eutf_array_value( $instance, 'shape_type' ) ); ?>><?php echo esc_html__( "Outline", 'engic' ); ?></option>
			</select>
		</p>
		<p>
			<em><?php echo esc_html__( 'Note: Make sure you include the full URL (i.e. http://www.samplesite.com)', 'engic' ); ?></em>
			<br>
			 <?php echo esc_html__( 'If you do not want a social to be visible, simply delete the supplied URL.', 'engic' ); ?>
		</p>

		<?php
		foreach ( $engic_eutf_social_list_extended as $social_item ) {

			$social_item_url = engic_eutf_array_value( $instance, $social_item['url'] );		
		?>

			<p>
				<label for="<?php echo esc_attr( $this->get_field_id( $social_item['url'] ) ); ?>"><?php echo esc_html( $social_item['title'] ); ?>:</label>
				<input style="width: 100%;" id="<?php echo esc_attr( $this->get_field_id( $social_item['url'] ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( $social_item['url'] ) ); ?>" value="<?php echo esc_attr( $social_item_url ); ?>" />
			</p>

		<?php
		}
		?>

	<?php
	}
}

//Omit closing PHP tag to avoid accidental whitespace output errors.
