/** * Processing like/unlike voting activity * @param no-param * @return json data */ function WtiLikePostProcessVote() { global $wpdb, $wti_ip_address; $post_id = (int) $_REQUEST['post_id']; $task = $_REQUEST['task']; $can_vote = $can_change_mind = false; $voted_id = 0; // Check for valid access if (!wp_verify_nonce($_REQUEST['nonce'], 'wti_like_post_vote_nonce')) { $error = 1; $msg = __('Invalid access', 'wti-like-post'); $show_user_likes = get_option('wti_like_post_show_user_likes'); $user_likes = null; if ($show_user_likes) { $user_likes = GetWtiUserLikes($post_id, $show_user_likes); } // Get like/dislike count $wti_like_count = GetWtiLikeCount($post_id); $wti_unlike_count = GetWtiUnlikeCount($post_id); $wti_total_count = GetWtiTotalCount($post_id); $check_option = get_option('wti_like_post_check_option'); $voting_period = get_option('wti_like_post_voting_period'); } else { // Get setting data $is_logged_in = is_user_logged_in(); $login_required = get_option('wti_like_post_login_required'); $check_option = get_option('wti_like_post_check_option'); $datetime_now = date('Y-m-d H:i:s'); // Get the voting period to check for revoting $voting_period = get_option('wti_like_post_voting_period'); $disallow_author_voting = get_option('wti_like_post_disallow_author_voting'); // Get user details $current_user = wp_get_current_user(); $user_id = (int) $current_user->ID; if ($login_required && !$is_logged_in || $check_option == 1 && !$is_logged_in) { // User needs to login to vote but has not logged in $error = 1; $msg = get_option('wti_like_post_login_message'); } else { $cookie_value = microtime(true) . rand(1, 99999); // Check whether user has already voted or not $voted_result = HasWtiAlreadyVoted($post_id, $check_option, $voting_period, $get_voted_id = true); $has_already_voted = $voted_result['has_voted']; $voted_id = $voted_result['voted_id']; $voted_count = $voted_result['voted_count']; // Get the post details $post_data = get_post($post_id); if ($disallow_author_voting && $user_id == $post_data->post_author) { // Author should not be allowed to vote his/her own post. $error = 1; $msg = get_option('wti_like_post_author_disallowed_message'); } else { if ("once" == $voting_period && $has_already_voted == 1) { // Check for change of mind if ($voted_count >= 0 && $task == 'unlike' || $voted_count <= 0 && $task == 'like') { $can_change_mind = true; } else { // User can vote only once and has already voted. $error = 1; $msg = get_option('wti_like_post_voted_message'); } } elseif (0 == $voting_period) { // User can vote as many times as he want $can_vote = true; } else { if (!$has_already_voted) { // Never voted befor so can vote $can_vote = true; } else { // Get the last date when the user had voted $last_voted_date = GetWtiLastVotedDate($post_id, $check_option, $user_id); // Get the next voted date when user can vote $next_vote_date = GetWtiNextVoteDate($last_voted_date, $voting_period); if ($next_vote_date > $datetime_now) { // Check for change of mind if ($voted_count >= 0 && $task == 'unlike' || $voted_count <= 0 && $task == 'like') { $can_change_mind = true; } else { $revote_duration = strtotime($next_vote_date) - strtotime($datetime_now); if ($revote_duration > 86400) { // In terms of days $revote_message = ceil($revote_duration / (3600 * 24)) . __('day(s)', 'wti-like-post'); } else { if ($revote_duration > 3600) { // In terms of hour and minute $revote_hours = (int) ($revote_duration / 3600); $revote_mins = (int) ($revote_duration % 3600 / 60); $revote_message = $revote_hours . __('hour(s)', 'wti-like-post') . ' ' . $revote_mins . __('minute(s)', 'wti-like-post'); } else { // In terms of minutes $revote_mins = (int) ($revote_duration / 60); $revote_message = $revote_mins . __('minute(s)', 'wti-like-post'); } } $error = 1; $msg = __('You can vote after', 'wti-like-post') . ' ' . $revote_message; } } else { $can_vote = true; } } } } } if ($can_vote) { if ($task == "like") { if ($has_already_voted > 0) { switch ($check_option) { case '2': // Cookies $query = "UPDATE {$wpdb->prefix}wti_like_post SET "; $query .= "value = value + 1, "; $query .= "ip = '{$wti_ip_address}', "; $query .= "user_id = {$user_id}, "; $query .= "date_time = '" . $datetime_now . "', "; $query .= "cookie_value = '" . $cookie_value . "' "; $query .= "WHERE post_id = '" . $post_id . "' AND "; $query .= "cookie_value = '" . $_COOKIE["wtilp_count_{$post_id}"] . "'"; break; case '1': // User Id $query = "UPDATE {$wpdb->prefix}wti_like_post SET "; $query .= "value = value + 1, "; $query .= "ip = '{$wti_ip_address}', "; $query .= "cookie_value = '{$cookie_value}', "; $query .= "date_time = '" . $datetime_now . "' "; $query .= "WHERE id = '" . $voted_id . "'"; break; case '0': default: // IP $query = "UPDATE {$wpdb->prefix}wti_like_post SET "; $query .= "value = value + 1, "; $query .= "user_id = {$user_id}, "; $query .= "cookie_value = '{$cookie_value}', "; $query .= "date_time = '" . $datetime_now . "' "; $query .= "WHERE id = '" . $voted_id . "'"; break; } } else { if ($voted_id > 0) { $query = "UPDATE {$wpdb->prefix}wti_like_post SET "; $query .= "value = value + 1, "; $query .= "user_id = {$user_id}, "; $query .= "ip = '{$wti_ip_address}', "; $query .= "cookie_value = '{$cookie_value}', "; $query .= "date_time = '" . $datetime_now . "' "; $query .= "WHERE id = '" . $voted_id . "'"; } else { $query = "INSERT INTO {$wpdb->prefix}wti_like_post SET "; $query .= "post_id = '" . $post_id . "', "; $query .= "value = '1', "; $query .= "user_id = {$user_id}, "; $query .= "date_time = '" . $datetime_now . "', "; $query .= "ip = '{$wti_ip_address}', "; $query .= "cookie_value = '{$cookie_value}'"; } } } else { if ($has_already_voted > 0) { switch ($check_option) { case '2': // Cookies $query = "UPDATE {$wpdb->prefix}wti_like_post SET "; $query .= "value = value - 1, "; $query .= "ip = '{$wti_ip_address}', "; $query .= "user_id = {$user_id}, "; $query .= "date_time = '" . $datetime_now . "', "; $query .= "cookie_value = '" . $cookie_value . "' "; $query .= "WHERE post_id = '" . $post_id . "' AND "; $query .= "cookie_value = '" . $_COOKIE["wtilp_count_{$post_id}"] . "'"; break; case '1': // User Id $query = "UPDATE {$wpdb->prefix}wti_like_post SET "; $query .= "value = value - 1, "; $query .= "ip = '{$wti_ip_address}', "; $query .= "cookie_value = '{$cookie_value}', "; $query .= "date_time = '" . $datetime_now . "' "; $query .= "WHERE id = '" . $voted_id . "'"; break; case '0': default: // IP $query = "UPDATE {$wpdb->prefix}wti_like_post SET "; $query .= "value = value - 1, "; $query .= "user_id = {$user_id}, "; $query .= "cookie_value = '{$cookie_value}', "; $query .= "date_time = '" . $datetime_now . "' "; $query .= "WHERE id = '" . $voted_id . "'"; break; } } else { if ($voted_id > 0) { $query = "UPDATE {$wpdb->prefix}wti_like_post SET "; $query .= "value = value - 1, "; $query .= "user_id = {$user_id}, "; $query .= "ip = '{$wti_ip_address}', "; $query .= "cookie_value = '{$cookie_value}', "; $query .= "date_time = '" . $datetime_now . "' "; $query .= "WHERE id = '" . $voted_id . "'"; } else { $query = "INSERT INTO {$wpdb->prefix}wti_like_post SET "; $query .= "post_id = '" . $post_id . "', "; $query .= "value = '-1', "; $query .= "user_id = {$user_id}, "; $query .= "date_time = '" . $datetime_now . "', "; $query .= "ip = '{$wti_ip_address}', "; $query .= "cookie_value = '{$cookie_value}'"; } //WtiLikePostAddLikeData($post_id, $value, $user_id, $date_time, $wti_ip_address, $cookie_value); } } $success = $wpdb->query($query); if ($success) { // Update the last voted time for the post update_post_meta($post_id, '_wti_last_voted_time', $datetime_now); $error = 0; $msg = get_option('wti_like_post_thank_message'); // Check for buddypress integration $bp_like_activity = get_option('wti_like_post_bp_like_activity'); // Integrate with buddypress if installed if (function_exists('bp_is_active') && bp_is_active('activity') && $bp_like_activity > 0) { if ($user_id > 0) { // Record this on the user's profile $from_user_link = bp_core_get_userlink($user_id); $post_link = "<a href='" . get_permalink($post_id) . "'>" . $post_data->post_title . "</a>"; $primary_link = bp_core_get_userlink($user_id, false, true); if (($bp_like_activity == 1 || $bp_like_activity == 3) && $task == 'like') { $activity_action = sprintf(__('%s liked %s', 'wti-like-post'), $from_user_link, $post_link); WtiLikePostAddBpActivity($user_id, $activity_action, $primary_link, __('liked', 'wti-like-post') . ' ' . $post_link); } elseif (($bp_like_activity == 2 || $bp_like_activity == 3) && $task == 'unlike') { $activity_action = sprintf(__('%s disliked %s', 'wti-like-post'), $from_user_link, $post_link); WtiLikePostAddBpActivity($user_id, $activity_action, $primary_link, __('disliked', 'wti-like-post') . ' ' . $post_link); } } } // Do the action do_action('wti_like_post_vote_action', $post_id, $wti_ip_address, $user_id, $task, $msg, $error); // Remove all the entries with 0 values //$wpdb->query("DELETE FROM {$wpdb->prefix}wti_like_post WHERE `value` = 0"); // Set the cookie for 1 year //setcookie("wtilp_count_{$post_id}", $cookie_value, time() + 3600 * 24 * 365); } else { $error = 1; $msg = __('Could not process your vote.', 'wti-like-post'); } } // Allow user to cancel voting if ($can_change_mind) { $query = "DELETE FROM {$wpdb->prefix}wti_like_post WHERE id = '" . $voted_id . "'"; if ($wpdb->query($query)) { $error = 0; $msg = __('Your vote was cancelled.', 'wti-like-post'); } else { $error = 1; $msg = __('Could not cancel your vote.', 'wti-like-post'); } } $show_user_likes = get_option('wti_like_post_show_user_likes'); $user_likes = null; if ($show_user_likes) { $user_likes = GetWtiUserLikes($post_id, $show_user_likes); } $options = get_option('wti_most_liked_posts'); $number = $options['number']; $show_count = $options['show_count']; // Get like/dislike count $wti_like_count = GetWtiLikeCount($post_id); $wti_unlike_count = GetWtiUnlikeCount($post_id); // Update post meta update_post_meta($post_id, '_wti_like_count', (int) str_replace('+', '', $wti_like_count)); update_post_meta($post_id, '_wti_unlike_count', (int) str_replace('-', '', $wti_unlike_count)); $wti_total_count = GetWtiTotalCount($post_id); update_post_meta($post_id, '_wti_total_count', $wti_total_count); } // Check own vote count $voted_result = HasWtiAlreadyVoted($post_id, $check_option, $voting_period); $voted_count = $voted_result['voted_count']; // Create the complete response $result = array("msg" => apply_filters('wti_like_post_ajax_message', $msg, $error), "error" => $error, "like" => $wti_like_count, "unlike" => $wti_unlike_count, "total" => $wti_total_count, "own_count" => $voted_count, "users" => $user_likes); // Check for method of processing the data if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { header('content-type: application/json; charset=utf-8'); header('access-control-allow-origin: *'); echo json_encode($result); } else { header('location:' . $_SERVER['HTTP_REFERER']); } exit; }
/** * Get the like output on site * @param array * @return string */ function GetWtiLikePost($arg = null) { global $wpdb, $wti_ip_address; $post_id = get_the_ID(); $wti_like_post = ''; $msg = ''; // Get the posts ids where we do not need to show like functionality $allowed_posts = explode(",", get_option('wti_like_post_allowed_posts')); $excluded_posts = explode(",", get_option('wti_like_post_excluded_posts')); $allowed_categories = get_option('wti_like_post_allowed_categories'); $excluded_categories = get_option('wti_like_post_excluded_categories'); $excluded_sections = get_option('wti_like_post_excluded_sections'); if (empty($allowed_categories)) { $allowed_categories = array(); } if (empty($excluded_categories)) { $excluded_categories = array(); } if (!empty($excluded_sections)) { // Checking for excluded section. if yes, then dont show the like/dislike option if (in_array('home', $excluded_sections) && is_home() || in_array('archive', $excluded_sections) && is_archive() || in_array('search', $excluded_sections) && is_search()) { return; } } $title_text = get_option('wti_like_post_title_text'); $category = get_the_category(); $excluded = false; if (count($category) > 0) { $cateory_ids = wp_list_pluck($category, 'term_id'); if (count($excluded_categories) > 0) { if (count(array_intersect($cateory_ids, $excluded_categories)) > 0 && !in_array($post_id, $allowed_posts)) { $excluded = true; } } else { if (count($allowed_categories) > 0) { if (count(array_intersect($cateory_ids, $allowed_categories)) == 0 || in_array($post_id, $excluded_posts)) { $excluded = true; } } } } //if excluded category, then dont show the like/dislike option if ($excluded) { return; } // Check for title text. if empty then have the default value if (empty($title_text)) { $title_text_like = __('Like', 'wti-like-post'); $title_text_unlike = __('Unlike', 'wti-like-post'); } else { $title_text = explode('/', get_option('wti_like_post_title_text')); $title_text_like = $title_text[0]; $title_text_unlike = $title_text[1]; } // Checking for excluded posts if (!in_array($post_id, $excluded_posts)) { // Get the nonce for security purpose and create the like and unlike urls $nonce = wp_create_nonce("wti_like_post_vote_nonce"); // Check for voting settings $check_option = get_option('wti_like_post_check_option'); $voting_period = get_option('wti_like_post_voting_period'); $like_count = GetWtiLikeCount($post_id); $unlike_count = GetWtiUnlikeCount($post_id); $alignment = "left" == get_option('wti_like_post_alignment') ? 'align-left' : 'align-right'; $show_dislike = get_option('wti_like_post_show_dislike'); $liked_style = $unliked_style = get_option('wti_like_post_voting_style') == "" ? 'style1' : get_option('wti_like_post_voting_style'); $show_user_likes = get_option('wti_like_post_show_user_likes'); $show_like_unlike_text = get_option('wti_like_post_show_like_unlike_text'); // Get voted details $voted_result = HasWtiAlreadyVoted($post_id, $check_option, $voting_period); $wti_has_voted = $voted_result['has_voted']; $voted_count = $voted_result['voted_count']; if ($like_count != 0 || $unlike_count != 0) { if ($wti_has_voted == 1) { $msg = get_option('wti_like_post_voted_message'); // Active class if already voted if ($voted_count > 0) { $liked_style .= '-active'; } else { if ($voted_count < 0) { $unliked_style .= '-active'; } } } } else { if ($wti_has_voted == 0) { $msg = get_option('wti_like_post_default_message'); } } $wti_like_post .= "<div class='watch-action'>"; $wti_like_post .= "<div class='watch-position " . $alignment . "'>"; $wti_like_post .= "<div class='action-like'>"; $wti_like_post .= "<a class='lbg-" . $liked_style . " like-" . $post_id . " jlk' data-task='like' data-post_id='" . $post_id . "' data-nonce='" . $nonce . "' rel='nofollow'>"; if ($show_like_unlike_text == 1) { $wti_like_post .= "<span class='wti-text'>" . get_option('wti_like_post_like_text') . "</span>"; } else { $wti_like_post .= "<img src='" . plugins_url('images/pixel.gif', __FILE__) . "' title='" . apply_filters('wti_like_post_like_title', $title_text_like, $post_id, $like_count) . "' />"; } $wti_like_post .= "<span class='lc-" . $post_id . " lc'>" . $like_count . "</span></a></div>"; if ($show_dislike) { $wti_like_post .= "<div class='action-unlike'>"; $wti_like_post .= "<a class='unlbg-" . $unliked_style . " unlike-" . $post_id . " jlk' data-task='unlike' data-post_id='" . $post_id . "' data-nonce='" . $nonce . "' rel='nofollow'>"; if ($show_like_unlike_text == 1) { $wti_like_post .= "<span class='wti-text'>" . get_option('wti_like_post_unlike_text') . "</span>"; } else { $wti_like_post .= "<img src='" . plugins_url('images/pixel.gif', __FILE__) . "' title='" . apply_filters('wti_like_post_unlike_title', $title_text_unlike, $post_id, $unlike_count) . "' />"; } $wti_like_post .= "<span class='unlc-" . $post_id . " unlc'>" . $unlike_count . "</span></a></div> "; } $wti_like_post .= "</div> "; $wti_like_post .= "<div class='status-" . $post_id . " status " . $alignment . "'>" . apply_filters('wti_like_post_load_message', $msg, $post_id, $like_count, $unlike_count) . "</div>"; $wti_like_post .= "</div><div class='wti-clear'></div>"; if ($show_user_likes) { $wti_like_post .= "<div class='wti-user-likes wti-likes-" . $post_id . "'>"; $wti_like_post .= GetWtiUserLikes($post_id); $wti_like_post .= "</div>"; } } if ($arg == 'put') { return apply_filters('get_wti_like_post', $wti_like_post); } else { echo apply_filters('get_wti_like_post', $wti_like_post); } }