<?php
/*
Plugin Name: SF Some button Widget
Plugin URI: 
Description: Custom widget plugin for some-buttons
Author: Sitefactory - Matias
Version: 1.0
Author URI: http://sitefactory.fi
*/
// Block direct requests
if ( !defined('ABSPATH') )
	die('-1');
	
	
add_action( 'widgets_init', function(){
     register_widget( 'SomeButtonWidget' );
});	
/**
 * Adds SomeButtonWidget widget.
 */
class SomeButtonWidget extends WP_Widget {

	public $aIcons = array( 
		'fab fa-facebook' => 'Facebook official (fa-facebook)', 
		'fab fa-facebook-f' => 'Facebook F (fa-facebook-f)', 
		'fab fa-twitter' => 'Twitter (fa-twitter)', 
		'fab fa-twitter-square' => 'Twitter neliö (fa-twitter-square)', 
		'fab fa-instagram' => 'Instagram (fa-instagram)', 
		'fab fa-pinterest' => 'Pinterest official (fa-pinterest)', 
		'fab fa-pinterest-p' => 'Pinterest p (fa-pinterest-p)', 
		'fab fa-pinterest-square' => 'Pinterest neliö (fa-pinterest-square)', 
		'fab fa-youtube-square' => 'YouTube neliö (fa-youtube-square)', 
		'fab fa-youtube' => 'YouTube official (fa-youtube)', 
		'fab fa-google' => 'Google official (fa-google)', 
		'fab fa-google-plus-g' => 'Google plus G (fa-google-plus-g)', 
		'fab fa-google-plus-square' => 'Google plus neliö (fa-google-plus-square)', 
		'fab fa-google-plus' => 'Google plus pyöreä (fa-google-plus)', 
		'fab fa-linkedin-in' => 'Linked in (fa-linkedin-in)', 
		'fab fa-linkedin' => 'Linked in neliö (fa-linkedin)', 
		'fab fa-blogger' => 'Blogger neliö (fa-blogger)', 
		'fab fa-blogger-b' => 'Blogger b (fa-blogger-b)', 
	);
	public $aLinkTargets = array( '_blank' => 'Avaa uuteen ikkunaan / välilehteen', '_self' => 'Avaa samaan ikkunaan' );

	/**
	 * Register widget with WordPress.
	 */
	function __construct() {
		parent::__construct(
			'SomeButtonWidget', // Base ID
			__('SomeIcon Widget', 'sf-somebutton-widget'), // Name
			array( 'description' => __( 'Custom widget plugin for Social Media icons', 'sf-somebutton-widget' ), ) // 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 ) {
	
     	echo $args['before_widget'];

		$widget_classes = ''; 
		if(!empty($instance['widget_class'])){
			$widget_classes .= ' ' . $instance['widget_class']; 
		}

		if(!empty($instance['icon'] || !empty($instance['class']) )){ 
			$widget_classes .= ' has-icon'; 
			if( !empty($instance['class']) ){
				$classes = $instance['class']; 
			}else{
				$classes = $instance['icon']; 
			}
		}

		if(!empty($classes)) {
			if(!empty($instance['link'])){ 
				echo '<a title="'.$instance['title'].'" class="social-media-btn-container'.$widget_classes.'" target="'. $instance['link_target'] .'" href="'.$instance['link'].'" rel="noopener">';
			}else{
				echo '<span title="'.$instance['title'].'" class="social-media-btn-container no-link'.$widget_classes.'">';
			}

				if(!empty($instance['title']) && $instance['show_title'] ) {
					echo '<span class="social-media-btn-text">'.$instance['title'].'</span>';
				}

				if(!empty($classes)){ 
					echo '<span class="social-media-btn-icon ' . $classes . '"></span>';
				}
				
			if(!empty($instance['link'])) { 
				echo '</a>';
			}else{
				echo '</span>';
			}
		}
		echo $args['after_widget'];
	}
	/**
	 * Back-end widget form.
	 *
	 * @see WP_Widget::form()
	 *
	 * @param array $instance Previously saved values from database.
	 */
	public function form( $instance ) {

		if ( isset( $instance[ 'title' ] ) ) {
			$title = $instance[ 'title' ];
		} else {
			$title = '';
		}

		if ( isset( $instance[ 'show_title' ] ) ) {
			$show_title = $instance[ 'show_title' ];
		} else {
			$show_title = '';
		}

		if ( isset( $instance[ 'link' ] ) ) {
			$link = $instance[ 'link' ];
		} else {
			$link = '';
		}

		if ( isset( $instance[ 'link_target' ] ) ) {
			$link_target = $instance[ 'link_target' ];
		} else {
			$link_target = '';
		}

		if ( isset( $instance[ 'icon' ] ) ) {
			$icon = $instance[ 'icon' ];
		} else {
			$icon = '';
		}

		if ( isset( $instance[ 'class' ] ) ) {
			$class = $instance[ 'class' ];
		} else {
			$class = '';
		}

		if ( isset( $instance[ 'widget_class' ] ) ) {
			$widget_class = $instance[ 'widget_class' ];
		} else {
			$widget_class = '';
		}

		?>
		<p>
			<label for="<?php echo $this->get_field_id( 'icon' ); ?>"><?php _e( 'Ikoni:' , 'sf-somebutton-widget'); ?></label>
			<select class="widefat" id="<?php echo $this->get_field_id( 'icon' ); ?>" name="<?php echo $this->get_field_name( 'icon' ); ?>">
				<option value="" <?php if( ! $icon ){ echo 'selected'; } ?>> -- <?php _e( 'Ei kuvaketta' , 'sf-somebutton-widget'); ?> -- </option>
				<?php foreach ($this->aIcons as $key => $icon_name) : ?>
					<?php $selected = ( $key == $icon ) ? 'selected' : '';  ?>
					<option value="<?php echo $key; ?>" <?php echo $selected; ?>><?php echo $icon_name; ?></option>
				<?php endforeach; ?>
			</select>

			<br><br>

			<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Ikonin teksti: (tulee myös title-attribuutiksi)' , 'sf-somebutton-widget'); ?></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 esc_attr( $title ); ?>">

			<br><br>
			
			<label for="<?php echo $this->get_field_id( 'show_title' ); ?>"><?php _e( 'Näytetäänkö teksti ennen ikonia' , 'sf-somebutton-widget'); ?></label>
			<input class="widefat" id="<?php echo $this->get_field_id( 'show_title' ); ?>" name="<?php echo $this->get_field_name( 'show_title' ); ?>" <?php if( $show_title ){ echo 'checked'; } ?> type="checkbox" value="show_title">

			<br><br>

			<label for="<?php echo $this->get_field_id( 'link' ); ?>"><?php _e( 'Ikonin linkki:' , 'sf-somebutton-widget'); ?></label> 
			<input class="widefat" id="<?php echo $this->get_field_id( 'link' ); ?>" name="<?php echo $this->get_field_name( 'link' ); ?>" type="text" value="<?php echo esc_attr( $link ); ?>">

			<br><br>

			<label for="<?php echo $this->get_field_id( 'link_target' ); ?>"><?php _e( 'Ikonin linkin kohde:' , 'sf-somebutton-widget'); ?></label>
			<select class="widefat" id="<?php echo $this->get_field_id( 'link_target' ); ?>" name="<?php echo $this->get_field_name( 'link_target' ); ?>">
				<?php foreach ($this->aLinkTargets as $key => $link_target_name) : ?>
					<?php $selected = ( $key == $link_target ) ? 'selected' : '';  ?>
					<option value="<?php echo $key; ?>" <?php echo $selected; ?>><?php echo $link_target_name; ?></option>
				<?php endforeach; ?>
			</select>

			<br><br>

			<label for="<?php echo $this->get_field_id( 'widget_class' ); ?>"><?php _e( 'Ikonin Luokka (erikoistyylejä varten)' , 'sf-somebutton-widget'); ?></label>
			<input class="widefat" id="<?php echo $this->get_field_id( 'widget_class' ); ?>" name="<?php echo $this->get_field_name( 'widget_class' ); ?>" type="text" value="<?php echo esc_attr( $widget_class ); ?>">

			<br><br>

			<label for="<?php echo $this->get_field_id( 'class' ); ?>"><?php _e( 'Ikoni kuvakkeen Luokka (korvaa kuvakkeen, voit itse kirjoittaa kuvakkeen luokan)' , 'sf-somebutton-widget'); ?></label>
			<input class="widefat" id="<?php echo $this->get_field_id( 'class' ); ?>" name="<?php echo $this->get_field_name( 'class' ); ?>" type="text" value="<?php echo esc_attr( $class ); ?>">


		</p>
		<?php 
	}
	/**
	 * Sanitize widget form values as they are saved.
	 *
	 * @see WP_Widget::update()
	 *
	 * @param array $new_instance Values just sent to be saved.
	 * @param array $old_instance Previously saved values from database.
	 *
	 * @return array Updated safe values to be saved.
	 */
	public function update( $new_instance, $old_instance ) {
		$instance = array();
		$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
		$instance['link'] = ( ! empty( $new_instance['link'] ) ) ? strip_tags( $new_instance['link'] ) : '';
		$instance['link_target'] = ( ! empty( $new_instance['link_target'] ) ) ? strip_tags( $new_instance['link_target'] ) : '';
		$instance['icon'] = ( ! empty( $new_instance['icon'] ) ) ? strip_tags( $new_instance['icon'] ) : '';
		$instance['class'] = ( ! empty( $new_instance['class'] ) ) ? strip_tags( $new_instance['class'] ) : '';
		$instance['show_title'] = ( ! empty( $new_instance['show_title'] ) ) ? strip_tags( $new_instance['show_title'] ) : '';
		$instance['widget_class'] = ( ! empty( $new_instance['widget_class'] ) ) ? strip_tags( $new_instance['widget_class'] ) : '';
		return $instance;
	}
} // class SomeButtonWidget