<?php if ( ! defined( 'ABSPATH' ) ) {
	die( 'Direct access forbidden.' );
}

/*
Name:  WordPress Post Like System
Description:  A simple and efficient post like system for WordPress.
Version:      0.5.2
Author:       Jon Masterson
Author URI:   http://jonmasterson.com/
License:
Copyright (C) 2015 Jon Masterson
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


/**
 * Processes like/unlike
 *
 * @since    0.5
 */
add_action( 'wp_ajax_nopriv_process_crumina_like', 'process_crumina_like' );
add_action( 'wp_ajax_process_crumina_like', 'process_crumina_like' );
function process_crumina_like() {
	// Security
	$nonce = isset( $_REQUEST['nonce'] ) ? sanitize_text_field( $_REQUEST['nonce'] ) : 0;
	if ( ! wp_verify_nonce( $nonce, 'crumina-likes-nonce' ) ) {
		exit( esc_html__( 'Not permitted', 'seosight' ) );
	}
	// Test if javascript is disabled
	$disabled = ( isset( $_REQUEST['disabled'] ) && $_REQUEST['disabled'] == true ) ? true : false;
	// Test if this is a comment
	$is_comment = ( isset( $_REQUEST['is_comment'] ) && $_REQUEST['is_comment'] == 1 ) ? 1 : 0;
	// Base variables
	$post_id    = ( isset( $_REQUEST['post_id'] ) && is_numeric( $_REQUEST['post_id'] ) ) ? $_REQUEST['post_id'] : '';
	$result     = array();
	$post_users = null;
	$like_count = 0;
	// Get plugin options
	if ( $post_id != '' ) {
		$count = ( $is_comment == 1 ) ? get_comment_meta( $post_id, "_comment_like_count", true ) : get_post_meta( $post_id, "_post_like_count", true ); // like count
		$count = ( isset( $count ) && is_numeric( $count ) ) ? $count : 0;
		if ( ! crumina_already_liked( $post_id, $is_comment ) ) { // Like the post
			if ( is_user_logged_in() ) { // user is logged in
				$user_id    = get_current_user_id();
				$post_users = crumina_post_user_likes( $user_id, $post_id, $is_comment );
				if ( $is_comment == 1 ) {
					// Update User & Comment
					$user_like_count = get_user_option( "_comment_like_count", $user_id );
					$user_like_count = ( isset( $user_like_count ) && is_numeric( $user_like_count ) ) ? $user_like_count : 0;
					update_user_option( $user_id, "_comment_like_count", ++ $user_like_count );
					if ( $post_users ) {
						update_comment_meta( $post_id, "_user_comment_liked", $post_users );
					}
				} else {
					// Update User & Post
					$user_like_count = get_user_option( "_user_like_count", $user_id );
					$user_like_count = ( isset( $user_like_count ) && is_numeric( $user_like_count ) ) ? $user_like_count : 0;
					update_user_option( $user_id, "_user_like_count", ++ $user_like_count );
					if ( $post_users ) {
						update_post_meta( $post_id, "_user_liked", $post_users );
					}
				}
			} else { // user is anonymous
				$user_ip    = crumina_get_ip();
				$post_users = crumina_post_ip_likes( $user_ip, $post_id, $is_comment );
				// Update Post
				if ( $post_users ) {
					if ( $is_comment == 1 ) {
						update_comment_meta( $post_id, "_user_comment_IP", $post_users );
					} else {
						update_post_meta( $post_id, "_user_IP", $post_users );
					}
				}
			}
			$like_count         = ++ $count;
			$response['status'] = "liked";
			$response['title'] = __('Unlike', 'seosight');
			$response['icon']   = crumina_get_liked_icon();
		} else { // Unlike the post
			if ( is_user_logged_in() ) { // user is logged in
				$user_id    = get_current_user_id();
				$post_users = crumina_post_user_likes( $user_id, $post_id, $is_comment );
				// Update User
				if ( $is_comment == 1 ) {
					$user_like_count = get_user_option( "_comment_like_count", $user_id );
					$user_like_count = ( isset( $user_like_count ) && is_numeric( $user_like_count ) ) ? $user_like_count : 0;
					if ( $user_like_count > 0 ) {
						update_user_option( $user_id, "_comment_like_count", -- $user_like_count );
					}
				} else {
					$user_like_count = get_user_option( "_user_like_count", $user_id );
					$user_like_count = ( isset( $user_like_count ) && is_numeric( $user_like_count ) ) ? $user_like_count : 0;
					if ( $user_like_count > 0 ) {
						update_user_option( $user_id, '_user_like_count', -- $user_like_count );
					}
				}
				// Update Post
				if ( $post_users ) {
					$uid_key = array_search( $user_id, $post_users );
					unset( $post_users[ $uid_key ] );
					if ( $is_comment == 1 ) {
						update_comment_meta( $post_id, "_user_comment_liked", $post_users );
					} else {
						update_post_meta( $post_id, "_user_liked", $post_users );
					}
				}
			} else { // user is anonymous
				$user_ip    = crumina_get_ip();
				$post_users = crumina_post_ip_likes( $user_ip, $post_id, $is_comment );
				// Update Post
				if ( $post_users ) {
					$uip_key = array_search( $user_ip, $post_users );
					unset( $post_users[ $uip_key ] );
					if ( $is_comment == 1 ) {
						update_comment_meta( $post_id, "_user_comment_IP", $post_users );
					} else {
						update_post_meta( $post_id, "_user_IP", $post_users );
					}
				}
			}
			$like_count         = ( $count > 0 ) ? -- $count : 0; // Prevent negative number
			$response['status'] = "unliked";
            $response['title'] = __('Like', 'seosight');
			$response['icon']   = crumina_get_unliked_icon();
		}
		if ( $is_comment == 1 ) {
			update_comment_meta( $post_id, "_comment_like_count", $like_count );
			update_comment_meta( $post_id, "_comment_like_modified", date( 'Y-m-d H:i:s' ) );
		} else {
			update_post_meta( $post_id, "_post_like_count", $like_count );
			update_post_meta( $post_id, "_post_like_modified", date( 'Y-m-d H:i:s' ) );
		}
		$response['count']   = crumina_get_like_count( $like_count );
		$response['testing'] = $is_comment;
		if ( $disabled == true ) {
			if ( $is_comment == 1 ) {
				wp_redirect( get_permalink( get_the_ID() ) );
				exit();
			} else {
				wp_redirect( get_permalink( $post_id ) );
				exit();
			}
		} else {
			wp_send_json( $response );
		}
	}
}

/**
 * Utility to test if the post is already liked
 *
 * @since    0.5
 */
function crumina_already_liked( $post_id, $is_comment ) {
	$post_users = null;
	$user_id    = null;
	if ( is_user_logged_in() ) { // user is logged in
		$user_id         = get_current_user_id();
		$post_meta_users = ( $is_comment == 1 ) ? get_comment_meta( $post_id, "_user_comment_liked" ) : get_post_meta( $post_id, "_user_liked" );
		if ( count( $post_meta_users ) != 0 ) {
			$post_users = $post_meta_users[0];
		}
	} else { // user is anonymous
		$user_id         = crumina_get_ip();
		$post_meta_users = ( $is_comment == 1 ) ? get_comment_meta( $post_id, "_user_comment_IP" ) : get_post_meta( $post_id, "_user_IP" );
		if ( count( $post_meta_users ) != 0 ) { // meta exists, set up values
			$post_users = $post_meta_users[0];
		}
	}
	if ( is_array( $post_users ) && in_array( $user_id, $post_users ) ) {
		return true;
	} else {
		return false;
	}
} // crumina_already_liked()

/**
 * Output the like button
 *
 * @since    0.5
 */
function get_crumina_likes_button( $post_id, $is_comment = null ) {
	$is_comment = ( null == $is_comment ) ? 0 : 1;
	$output     = '';
	$nonce      = wp_create_nonce( 'crumina-likes-nonce' ); // Security
	if ( $is_comment == 1 ) {
		$post_id_class = esc_attr( ' sl-comment-button-' . $post_id );
		$comment_class = esc_attr( ' sl-comment' );
		$like_count    = get_comment_meta( $post_id, "_comment_like_count", true );
		$like_count    = ( isset( $like_count ) && is_numeric( $like_count ) ) ? $like_count : 0;
	} else {
		$post_id_class = esc_attr( ' sl-button-' . $post_id );
		$comment_class = esc_attr( '' );
		$like_count    = get_post_meta( $post_id, "_post_like_count", true );
		$like_count    = ( isset( $like_count ) && is_numeric( $like_count ) ) ? $like_count : 0;
	}
	$count      = crumina_get_like_count( $like_count );
	$icon_empty = crumina_get_unliked_icon();
	$icon_full  = crumina_get_liked_icon();
	// Loader
	$loader = '<span id="sl-loader"></span>';
	// Liked/Unliked Variables
	if ( crumina_already_liked( $post_id, $is_comment ) ) {
		$class = esc_attr( ' liked' );
		$title = esc_html__( 'Unlike', 'seosight' );
		$icon  = $icon_full;
	} else {
		$class = '';
		$title = esc_html__( 'Like', 'seosight' );
		$icon  = $icon_empty;
	}
	$output = '<span class="sl-wrapper"><a href="' . admin_url( 'admin-ajax.php?action=process_crumina_like' . '&post_id=' . $post_id . '&nonce=' . $nonce . '&is_comment=' . $is_comment . '&disabled=true' ) . '" class="sl-button likes' . $post_id_class . $class . $comment_class . '" data-nonce="' . $nonce . '" data-post-id="' . $post_id . '" data-iscomment="' . $is_comment . '" title="' . $title . '">' . $icon . $count . '</a>' . $loader . '</span>';

	return $output;
} // get_crumina_likes_button()

/**
 * Utility retrieves post meta user likes (user id array),
 * then adds new user id to retrieved array
 *
 * @since    0.5
 */
function crumina_post_user_likes( $user_id, $post_id, $is_comment ) {
	$post_users      = '';
	$post_meta_users = ( $is_comment == 1 ) ? get_comment_meta( $post_id, "_user_comment_liked" ) : get_post_meta( $post_id, "_user_liked" );
	if ( count( $post_meta_users ) != 0 ) {
		$post_users = $post_meta_users[0];
	}
	if ( ! is_array( $post_users ) ) {
		$post_users = array();
	}
	if ( ! in_array( $user_id, $post_users ) ) {
		$post_users[ 'user-' . $user_id ] = $user_id;
	}

	return $post_users;
} // crumina_post_user_likes()

/**
 * Utility retrieves post meta ip likes (ip array),
 * then adds new ip to retrieved array
 *
 * @since    0.5
 */
function crumina_post_ip_likes( $user_ip, $post_id, $is_comment ) {
	$post_users      = '';
	$post_meta_users = ( $is_comment == 1 ) ? get_comment_meta( $post_id, "_user_comment_IP" ) : get_post_meta( $post_id, "_user_IP" );
	// Retrieve post information
	if ( count( $post_meta_users ) != 0 ) {
		$post_users = $post_meta_users[0];
	}
	if ( ! is_array( $post_users ) ) {
		$post_users = array();
	}
	if ( ! in_array( $user_ip, $post_users ) ) {
		$post_users[ 'ip-' . $user_ip ] = $user_ip;
	}

	return $post_users;
} // crumina_post_ip_likes()

/**
 * Utility to retrieve IP address
 *
 * @since    0.5
 */
function crumina_get_ip() {
    $ip        = '0.0.0.0';
    $client_ip = filter_input( INPUT_SERVER, 'HTTP_CLIENT_IP', FILTER_VALIDATE_IP );
    $forwarded = filter_input( INPUT_SERVER, 'HTTP_X_FORWARDED_FOR', FILTER_VALIDATE_IP );
    $remote    = filter_input( INPUT_SERVER, 'REMOTE_ADDR', FILTER_VALIDATE_IP );

    if ( $client_ip ) {
        $ip = $client_ip;
    } elseif ( $forwarded ) {
        $ip = $forwarded;
    } elseif ( $remote ) {
        $ip = $remote;
    }

    return $ip;
} // crumina_get_ip()

/**
 * Utility returns the button icon for "like" action
 *
 * @since    0.5
 */
function crumina_get_liked_icon() {
	/* If already using Font Awesome with your theme, replace svg with:  */
	$icon = '<i class="seoicon-shape-heart"></i>';

	return $icon;
} // crumina_get_liked_icon()
/**
 * Utility returns the button icon for "unlike" action
 *
 * @since    0.5
 */
function crumina_get_unliked_icon() {
	/* If already using Font Awesome with your theme, replace svg with: <i class="fa fa-heart-o"></i> */
	$icon = '<span class="unliked"><i class="seoicon-shape-heart"></i></span>';

	return $icon;
} // crumina_get_unliked_icon()

/**
 * Utility function to format the button count,
 * appending "K" if one thousand or greater,
 * "M" if one million or greater,
 * and "B" if one billion or greater (unlikely).
 * $precision = how many decimal points to display (1.25K)
 *
 * @since    0.5
 */
function crumina_format_count( $number ) {
	$precision = 2;
	if ( $number >= 1000 && $number < 1000000 ) {
		$formatted = number_format( $number / 1000, $precision ) . 'K';
	} else if ( $number >= 1000000 && $number < 1000000000 ) {
		$formatted = number_format( $number / 1000000, $precision ) . 'M';
	} else if ( $number >= 1000000000 ) {
		$formatted = number_format( $number / 1000000000, $precision ) . 'B';
	} else {
		$formatted = $number; // Number is less than 1000
	}
	$formatted = str_replace( '.00', '', $formatted );

	return $formatted;
} // crumina_format_count()

/**
 * Utility retrieves count plus count options,
 * returns appropriate format based on options
 *
 * @since    0.5
 */
function crumina_get_like_count( $like_count ) {
	if ( is_numeric( $like_count ) && $like_count > 0 ) {
		$number = crumina_format_count( $like_count );
	} else {
		$number = '0';
	}
	$count = '<span class="count-likes">' . $number . '</span>';

	return $count;
} // crumina_get_like_count()

// User Profile List
add_action( 'show_user_profile', 'crumina_show_user_likes' );
add_action( 'edit_user_profile', 'crumina_show_user_likes' );
function crumina_show_user_likes( $user ) { ?>
	<table class="form-table">
		<tr>
			<th><label for="user_likes"><?php esc_html_e( 'You Like:', 'seosight' ); ?></label></th>
			<td>
				<?php
				$types      = get_post_types( array( 'public' => true ) );
				$args       = array(
					'numberposts' => - 1,
					'post_type'   => $types,
					'meta_query'  => array(
						array(
							'key'     => '_user_liked',
							'value'   => $user->ID,
							'compare' => 'LIKE'
						)
					)
				);
				$sep        = '';
				$like_query = new WP_Query( $args );
				if ( $like_query->have_posts() ) : ?>
					<p>
						<?php while ( $like_query->have_posts() ) : $like_query->the_post();
							seosight_render($sep); ?><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
							<?php
							$sep = ' &middot; ';
						endwhile;
						?>
					</p>
				<?php else : ?>
					<p><?php esc_html_e( 'You do not like anything yet.', 'seosight' ); ?></p>
					<?php
				endif;
				wp_reset_postdata();
				?>
			</td>
		</tr>
	</table>
<?php } // crumina_show_user_likes()