function process_simple_like() { // Security $nonce = isset($_REQUEST['nonce']) ? sanitize_text_field($_REQUEST['nonce']) : 0; if (!wp_verify_nonce($nonce, 'simple-likes-nonce')) { exit(__('Not permitted', 'tbr')); } // 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 (!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 = 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 = sl_get_ip(); $post_users = 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['icon'] = get_liked_icon(); } else { // Unlike the post if (is_user_logged_in()) { // user is logged in $user_id = get_current_user_id(); $post_users = 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 = sl_get_ip(); $post_users = 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['icon'] = 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'] = 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); } } }
function process_post_like() { // Security $nonce = isset($_REQUEST['nonce']) ? sanitize_text_field($_REQUEST['nonce']) : 0; if (!wp_verify_nonce($nonce, 'simple-likes-nonce')) { exit(__('Not permitted', 'kindel')); } // Test if javascript is disabled $disabled = isset($_REQUEST['disabled']) && $_REQUEST['disabled'] == true ? true : false; // 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 != '') { // Get like count $count = get_post_meta($post_id, "_post_like_count", true); $count = isset($count) && is_numeric($count) ? $count : 0; // If they haven't liked the post already if (!already_liked($post_id)) { if (is_user_logged_in()) { // user is logged in // Get user ID $user_id = get_current_user_id(); // Checks if user $post_users = post_user_likes($user_id, $post_id); // Update Post if ($post_users) { update_post_meta($post_id, "_user_liked", $post_users); } } else { // user is anonymous // Get user IP Adress $user_ip = get_user_ip(); $post_users = post_ip_likes($user_ip, $post_id); // Update Post if ($post_users) { update_post_meta($post_id, "_user_IP", $post_users); } } $like_count = ++$count; $response['status'] = "liked"; } else { // User has already liked the post - make them able to unlike the post // User is logged in if (is_user_logged_in()) { // Get user ID $user_id = get_current_user_id(); $post_users = post_user_likes($user_id, $post_id); // Update Post if ($post_users) { $uid_key = array_search($user_id, $post_users); unset($post_users[$uid_key]); update_post_meta($post_id, "_user_liked", $post_users); } } else { // User is anonymous $user_ip = get_user_ip(); $post_users = post_ip_likes($user_ip, $post_id); // Update Post if ($post_users) { $uip_key = array_search($user_ip, $post_users); unset($post_users[$uip_key]); update_post_meta($post_id, "_user_IP", $post_users); } } $like_count = $count > 0 ? --$count : 0; // Prevent negative number $response['status'] = "unliked"; } // Now we update the actual post like count update_post_meta($post_id, "_post_like_count", $like_count); // Update the modified time of the post update_post_meta($post_id, "_post_like_modified", date('Y-m-d H:i:s')); // Send back response with the new count $response['count'] = get_like_count($like_count); // If javascript is disable then send us back to the post // else send the response as JSON if ($disabled == true) { wp_redirect(get_permalink($post_id)); exit; } else { wp_send_json($response); } } }