Example #1
0
function ltpAjaxCallback()
{
    global $wpdb;
    $mydbname = $wpdb->prefix . 'ltp_datas';
    $post_id = (int) $_REQUEST['post_id'];
    $task = $_REQUEST['task'];
    $hashealreadyvoted = hasheAlreadyVoted($post_id);
    $loginplease = get_option('ltp_login_message');
    $thanksforlike = get_option('ltp_thanks_message');
    $alreadylikes = get_option('ltp_already_liked_message');
    if (!wp_verify_nonce($_REQUEST['nonce'], 'ltp_vote_nonce')) {
        $error = 1;
        $msg = 'Invalid Access';
    } else {
        if (is_user_logged_in()) {
            $current_user = wp_get_current_user();
            $user_id = $current_user->ID;
            if ($task == 'like') {
                if ($hashealreadyvoted == 0) {
                    $query = "INSERT INTO " . $mydbname . " (`post_id`, `value`, `user_id`, `date_time`) VALUES ('" . $post_id . "', '1', '" . $user_id . "', '" . date('Y-m-d H:i:s') . "');";
                    $sqlchangesee = 1;
                } else {
                    $error = 1;
                    $msg = $alreadylikes;
                }
            }
            if ($sqlchangesee == 1) {
                $success = $wpdb->query($query);
                if ($success) {
                    $error = 0;
                    $msg = $thanksforlike;
                } else {
                    $error = 1;
                    $msg = 'Could not process your vote';
                }
            }
        } else {
            $error = 0;
            $msg = $loginplease;
        }
    }
    $ltp_alllikes = ltp_likecount($post_id);
    $ltp_like_users = GetAllUserslikethePost($post_id);
    $ltp_user_like_data = showLikedUsers($ltp_like_users);
    if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
        $result = array("msg" => $msg, "like" => $ltp_alllikes, "alllikeusers" => $ltp_user_like_data);
        echo json_encode($result);
    } else {
        header("location:" . $_SERVER["HTTP_REFERER"]);
    }
    exit;
}
function ltpCurrrentLike()
{
    global $wpdb;
    $post_id = get_the_ID();
    $show_ajax_notify = get_option('ltp_show_ajax_notify');
    $show_only_count = get_option('ltp_show_only_count');
    $nonce = wp_create_nonce("ltp_vote_nonce");
    $ltp_alllikes = ltp_likecount($post_id);
    $ltp_like_users = GetAllUserslikethePost($post_id);
    $ltp_user_like_data = showLikedUsers($ltp_like_users);
    $ajax_like_link = admin_url('admin-ajax.php?action=ltp_ajax_process&task=like&post_id=' . $post_id . '&nonce=' . $nonce);
    $showLikeBox = '<div class="ltp-box">';
    $showLikeBox .= '<div class="like-box"><a class="likebutton" href="' . $ajax_like_link . '" data-task="like" data-post_id="' . $post_id . '" data-nonce="' . $nonce . '"><span class="icon-like"></span><span class="no-like no-like-' . $post_id . ' like-content">' . $ltp_alllikes . '</span></a>';
    if ($show_only_count == 0) {
        $showLikeBox .= "<div class='likeusers likeusers-" . $post_id . "'>" . $ltp_user_like_data . "</div>";
    }
    if ($show_ajax_notify == 1) {
        $showLikeBox .= '<div class="no-msg no-msg-' . $post_id . '"></div>';
    }
    $showLikeBox .= '</div></div>';
    return $showLikeBox;
}