<?php

class WP_SuperLike {

	/* function: load skin as array */
	function load_skin($preview_theme=null) {
		/* preview theme */
		if ($preview_theme) {
			$styles = apply_filters('wp_superlike_builtin_styles', array());
			return $styles[$preview_theme];
		} else {
		
			/* using set theme */
			$style = $this->get_option('style');
			
			/* check if we are in single.php */
			$style = $this->override_style_from_post();

			$styles = apply_filters('wp_superlike_builtin_styles', array());
			
			if (isset($styles[$style]))
				return $styles[$style];
			
		}
	}
	
	/* function: load custom theme */
	function load_custom_skin() {
		$custom = get_option('wp_superlike_settings');
		return $custom;
	}
	
	/* find if post has specific style to use */
	function override_style_from_post() {
		global $post;
		if (isset($post->ID)) $style = get_post_meta($post->ID, 'style', true);
		if (is_single() || is_page()) {
			if (isset($style) ) {
				if ($style !== '') {
					return $style;
				}
			}
		}
		return $this->get_option('style');
	}

	/* function: add a like to a post */
	function add_like($post_id=null) {
		$likes_of_post = $this->get_likes($post_id);
		$this->update_likes($post_id, $likes_of_post, $likes_of_post+1);
	}
	
	/* function: update post likes */
	function update_likes($post_id=null, $current, $new) {
		update_post_meta($post_id, 'wp_superlike_count', $new);
		$this->update_who_liked($post_id);
	}
	
	/* function: find if post has users liked */
	function post_has_likes_loggedin($post_id=null) {
		$users = (array)get_post_meta($post_id, 'wp_superlike_users', true);
		if (is_array($users) && count($users) > 0) /* deal with array */
			return true;
		return false;
	}
	
	/* function: users who liked */
	function users_who_liked($post_id=null) {
		$users = get_post_meta($post_id, 'wp_superlike_users', true);
		return $users;
	}
	
	/* update who liked a post */
	function update_who_liked($post_id=null) {
		if (is_user_logged_in()) {
			$users = (array)get_post_meta($post_id, 'wp_superlike_users', true);
			global $current_user;
			get_currentuserinfo();
			$id = $current_user->ID;
			if (!in_array($id, $users)) {
				$users[] = $id;
				update_post_meta($post_id, 'wp_superlike_users', $users);
			}
		}
	}
	
	/* function: get likes count of a post */
	function get_likes($post_id=null) {
		$count = (int)get_post_meta($post_id, 'wp_superlike_count', true);
		return $count;
	}
	
	/* function: show likes (formatted) */
	function show_likes($post_id=null, $number_format=null) {
		$n = $this->get_likes($post_id);
		if ($number_format == 0) {
			return number_format_i18n($n);
		} else {
			return $this->format_number($n);
		}
	}
	
	/* function: eligible for alert */
	function show_alert($post_id) {
		if ($this->get_option('cannot_like_method') == 'show_popup') {
			if ($this->user_liked_and_ip_check_active($post_id)) {
				return true;
			}
		}
	}
	
	/* function: a very nice number format */
	function format_number($n, $number_format=1, $precision = 1) {
		if ($number_format == 0) {
			return number_format_i18n($n);
		} else {
		
			if ($n < 1000) {
				$n_format = number_format_i18n($n);
			} else if ($n < 1000000) {
				$n_format = number_format($n / 1000, $precision) . 'K';
			} else if ($n < 1000000000) {
				$n_format = number_format($n / 1000000, $precision) . 'M';
			} else {
				$n_format = number_format($n / 1000000000, $precision) . 'B';
			}
			return str_replace('.0','', $n_format);
			
		}
	}
	
	/* function: store like in DB option */
	function add_like_to_blog($post_id=null) {
	
		/* record the like in blog */
		$likes = get_option('wp_superlike');
		$likes[] = array(
							'post_id' => $post_id,
							'time' => time(),
							'ip' => $_SERVER["REMOTE_ADDR"]
						);
		update_option('wp_superlike', $likes);
		
		/* record the like by IP */
		$ipaddress = $_SERVER["REMOTE_ADDR"];
		$user_votes = get_option('wp_superlike_by_ip');
		if (!isset($user_votes["$ipaddress"])) {
			$user_votes["$ipaddress"] = array( $post_id => array( 'liked' => 1, 'time' => time() ) );
		} else {
			if (!isset($user_votes["$ipaddress"][$post_id])) {
				$user_votes["$ipaddress"][$post_id] = array(
					'liked' => 1,
					'time' => time()
				);
			} else { // IP address votes for this post
				$user_votes["$ipaddress"][$post_id]['last_liked'] = time(); // update last liked time
			}
		}
		update_option('wp_superlike_by_ip', $user_votes);
		
	}
	
	/* function: validate a like post/IP basis */
	function already_liked($post_id=null, $ip) {
		$user_votes = get_option('wp_superlike_by_ip');
		if (isset($user_votes["$ip"][$post_id])) // vote exists in that post
			return true;
		return false;
	}
	
	/* function figure out if like text will be changed */
	function like_text_changed($post_id=null, $like_text) {
		if ($this->get_option('multiple_vote') == 'ip_check') {
			if ($this->already_liked($post_id, $_SERVER["REMOTE_ADDR"])) {
				if ($this->get_option('already_liked_text') != '') {
					$like_text = $this->get_option('already_liked_text');
				}
			}
		}
		return $like_text;
	}
	
	/* function: this function returns true if check by IP is enabled
	* and user already liked */
	function user_liked_and_ip_check_active($post_id=null) {
		if ($this->get_option('multiple_vote') == 'ip_check') {
			if ($this->already_liked($post_id, $_SERVER["REMOTE_ADDR"])) {
				return true;
			}
		}
	}
	
	/* function: this function returns true if check by IP is enabled
	* and user already liked */
	function grey_out_button($post_id=null) {
		if ($this->get_option('multiple_vote') == 'ip_check') {
			if ($this->already_liked($post_id, $_SERVER["REMOTE_ADDR"])) {
				if ($this->get_option('cannot_like_method') == 'disable_button') {
					return true;
				}
			}
		}
	}
	
	/* function: removes a like from post based on IP */
	function delete_user_like($post_id=null, $ip) {
		$user_votes = get_option('wp_superlike_by_ip');
		if (isset($user_votes["$ip"][$post_id])) {
			unset($user_votes["$ip"][$post_id]);
		}
		update_option('wp_superlike_by_ip', $user_votes);
	}
	
	/* function: retrieves true if post disabled like button */
	function do_not_show_on_post($post_id=null) {
		$state = get_post_meta($post_id, 'showbutton', true);
		if (isset($state) && $state == -1) {
			return true;
		}
	}
	
	/* function: gets a plugin option */
	function get_option($option) {
	
		$settings = get_option('wp_superlike_settings');
		
		if (isset($settings[$option])) {
			return $settings[$option];
		}
		
	}

}

$wp_superlike = new WP_SuperLike();