<?php

	/**
	* @This is the function that displays
	* @WP Superlike button!
	*/
	function wp_superlike( $args=array() ) {
	
		global $wp_superlike, $post;
				
		$button = null;
	
		/* default args */
		$defaults = array(
				'post_id' => null, /* we do not know which post yet! */
				'layout' => $wp_superlike->get_option('button_layout'), // button layout standard|box
				'position' => null,
				'size' => null,
				'like_text' => $wp_superlike->get_option('like_text'), // default like button text
				'format' => $wp_superlike->get_option('number_format'), // number format setting
				'count' => null
		);
		
		/* extract args */
		$args = wp_parse_args( $args, $defaults );
		extract( $args, EXTR_SKIP );
		
		/* use in loop or pass post ID */
		if (!$post_id) {
			global $post;
			if (!isset($post->ID)) {
				$post_id = 0;
			} else {
				$post_id = $post->ID;
			}
		}
		
		/* get position */
		$position = (isset($position)) ? $position : $wp_superlike->get_option('add_to_content_after');
		
		/* already liked text? */
		$like_text = $wp_superlike->like_text_changed($post_id, $like_text);
				
		/* count of likes */
		$count = (isset($count)) ? $wp_superlike->format_number($count, $format) : $wp_superlike->show_likes($post_id, $format);
		
		/* no icon */
		if ($wp_superlike->get_option('icon_only')) {
			$like_text = null;
		} else {
			$like_text = '<span>'.$like_text.'</span>';
		}
		
		switch($layout) {
		
			case 'standard':
	
				$button = 	'<div class="wp-superlike '.wp_superlike_classes($position, $size).wp_superlike_cannot_like_class($post_id).' " data-id="'.$post_id.'">
								<div class="wp-superlike-button"><i></i>'.$like_text.'</div>
								<div class="wp-superlike-bubble">
									<span class="wp-superlike-arrow"></span>
									<span class="wp-superlike-count">'.$count.'</span>
								</div></div>';
							
				if (!in_array($position, array('left','right'))) // button floated do not clear
					$button .= '<div class="wp-superlike-clearfix"></div>';
					
				break;
		
			case 'box_count':
			
				$button = 	'<div class="wp-superlike '.wp_superlike_classes($position, $size).wp_superlike_cannot_like_class($post_id).' " data-id="'.$post_id.'">
								<div class="wp-superlike-bubble wp-superlike-bubble-box">
									<span class="wp-superlike-arrow-box"></span>
									<span class="wp-superlike-count wp-superlike-count-box">'.$count.'</span>
								</div><div class="wp-superlike-clearfix"></div>
								<div class="wp-superlike-button"><i></i>'.$like_text.'</div></div>';
							
				if (!in_array($position, array('left','right'))) // button floated do not clear
					$button .= '<div class="wp-superlike-clearfix"></div>';
			
				break;
		
		}
	
		return $button;
	
	}
	
	/**
	* @This function automatically prints specific classes
	* @to the like button to quickly change appearance
	*/
	function wp_superlike_classes($position,$size=null) {
	
		global $wp_superlike;
		$classes = null;
		
		$classes .= 'wp-superlike-align-'.$position.' ';
		
		$size = (isset($size)) ? $size : $wp_superlike->get_option('button_size');
		$classes .= 'wp-superlike-'.$size.' ';
		
		return $classes;

	}
	
	/**
	* @This function automatically adds a new class
	* @If you user cannot like a post
	* @will be used when cannot_like_method = disable_button
	*/
	function wp_superlike_cannot_like_class($post_id) {
	
		global $wp_superlike;
		
		$classes = null;
		
		/* validate user like */
		$already_liked = $wp_superlike->already_liked($post_id, $_SERVER["REMOTE_ADDR"]);
		
		/* in case we disable IP validation */
		if ($wp_superlike->get_option('multiple_vote') != 'ip_check')
			$already_liked = false;
			
		if ($already_liked) {
			if ($wp_superlike->get_option('cannot_like_method') == 'disable_button') {
				$classes .= 'wp-superlike-disabled ';
			}
		}
		
		return $classes;
	
	}