/**
 * Show like count as a column in admin posts page
 * 
 * @param $column_name string
 * @param $post_id integer
 * @return string
 */
function WtiAdminPostDataRow($column_name, $post_id)
{
    global $wpdb;
    switch ($column_name) {
        case 'like_count':
            // Logic to display post 'Note' field information here.
            $like_count = GetWtiLikeCount($post_id);
            echo str_replace("+", "", $like_count);
            break;
        default:
            break;
    }
}
/**
 * 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;
}
            if ($next_vote_date > $datetime_now) {
                $revote_duration = (strtotime($next_vote_date) - strtotime($datetime_now)) / (3600 * 24);
                if ($tm_vote > 0) {
                    echo $mes;
                } else {
                    if ($tm_vote < 0) {
                        echo $mes_un;
                    }
                }
            }
        }
    }
}
$like = $unlike = $fill_cl = $sum = '';
if (function_exists('GetWtiLikeCount')) {
    $like = GetWtiLikeCount(get_the_ID());
}
if (function_exists('GetWtiUnlikeCount')) {
    $unlike = GetWtiUnlikeCount(get_the_ID());
}
$re_like = str_replace('+', '', $like);
$re_unlike = str_replace('-', '', $unlike);
$sum = $re_like + $re_unlike;
if ($sum != 0 && $sum != '') {
    $fill_cl = $re_like / $sum * 100;
} else {
    if ($sum == 0) {
        $fill_cl = 50;
    }
}
?>
Beispiel #4
0
/**
 * Get the like output on site
 * @param array
 * @return string
 */
function GetWtiLikePost($arg = null)
{
    global $wpdb;
    $post_id = get_the_ID();
    $wti_like_post = "";
    // 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'));
    $excluded_categories = get_option('wti_like_post_excluded_categories');
    $excluded_sections = get_option('wti_like_post_excluded_sections');
    if (empty($excluded_categories)) {
        $excluded_categories = array();
    }
    if (empty($excluded_sections)) {
        $excluded_sections = array();
    }
    $title_text = get_option('wti_like_post_title_text');
    $category = get_the_category();
    $excluded = false;
    // 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()) {
        return;
    }
    // Checking for excluded categories
    foreach ($category as $cat) {
        if (in_array($cat->cat_ID, $excluded_categories) && !in_array($post_id, $allowed_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 = isset($title_text[1]) ? $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");
        $ajax_like_link = admin_url('admin-ajax.php?action=wti_like_post_process_vote&task=like&post_id=' . $post_id . '&nonce=' . $nonce);
        $ajax_unlike_link = admin_url('admin-ajax.php?action=wti_like_post_process_vote&task=unlike&post_id=' . $post_id . '&nonce=' . $nonce);
        $like_count = GetWtiLikeCount($post_id);
        $unlike_count = GetWtiUnlikeCount($post_id);
        $msg = GetWtiVotedMessage($post_id);
        $alignment = "left" == get_option('wti_like_post_alignment') ? 'align-left' : 'align-right';
        $show_dislike = get_option('wti_like_post_show_dislike');
        $style = get_option('wti_like_post_voting_style') == "" ? 'style1' : get_option('wti_like_post_voting_style');
        $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-" . $style . " like-" . $post_id . " jlk' href='javascript:void(0)' data-task='like' data-post_id='" . $post_id . "' data-nonce='" . $nonce . "' rel='nofollow'>";
        $wti_like_post .= "<img src='" . plugins_url('images/pixel.gif', __FILE__) . "' title='" . __($title_text_like, 'wti-like-post') . "' />";
        $wti_like_post .= "<span class='lc-" . $post_id . " lc'>" . $like_count . "</span>";
        $wti_like_post .= "</a></div>";
        if ($show_dislike) {
            $wti_like_post .= "<div class='action-unlike'>";
            $wti_like_post .= "<a class='unlbg-" . $style . " unlike-" . $post_id . " jlk' href='javascript:void(0)' data-task='unlike' data-post_id='" . $post_id . "' data-nonce='" . $nonce . "' rel='nofollow'>";
            $wti_like_post .= "<img src='" . plugins_url('images/pixel.gif', __FILE__) . "' title='" . __($title_text_unlike, 'wti-like-post') . "' />";
            $wti_like_post .= "<span class='unlc-" . $post_id . " unlc'>" . $unlike_count . "</span>";
            $wti_like_post .= "</a></div> ";
        }
        $wti_like_post .= "</div> ";
        $wti_like_post .= "<div class='status-" . $post_id . " status " . $alignment . "'>" . $msg . "</div>";
        $wti_like_post .= "</div><div class='wti-clear'></div>";
    }
    if ($arg == 'put') {
        return $wti_like_post;
    } else {
        echo $wti_like_post;
    }
}
Beispiel #5
0
function WtiLikePostProcessVote()
{
    global $wpdb, $wti_ip_address;
    // Get request data
    $post_id = (int) $_REQUEST['post_id'];
    $task = $_REQUEST['task'];
    // Check for valid access
    if (!wp_verify_nonce($_REQUEST['nonce'], 'wti_like_post_vote_nonce')) {
        $error = 1;
        $msg = __('Invalid access', 'wti-like-post');
    } else {
        // Get setting data
        $is_logged_in = is_user_logged_in();
        $login_required = get_option('wti_like_post_login_required');
        $can_vote = false;
        if ($login_required && !$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 {
            $has_already_voted = HasWtiAlreadyVoted($post_id, $wti_ip_address);
            $voting_period = get_option('wti_like_post_voting_period');
            $datetime_now = date('Y-m-d H:i:s');
            if ("once" == $voting_period && $has_already_voted) {
                // 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, $wti_ip_address);
                    // Get the bext voted date when user can vote
                    $next_vote_date = GetWtiNextVoteDate($last_voted_date, $voting_period);
                    if ($next_vote_date > $datetime_now) {
                        $revote_duration = (strtotime($next_vote_date) - strtotime($datetime_now)) / (3600 * 24);
                        $can_vote = false;
                        $error = 1;
                        $msg = __('You can vote after', 'wti-like-post') . ' ' . ceil($revote_duration) . ' ' . __('day(s)', 'wti-like-post');
                    } else {
                        $can_vote = true;
                    }
                }
            }
        }
        if ($can_vote) {
            $current_user = wp_get_current_user();
            $user_id = (int) $current_user->ID;
            if ($task == "like") {
                if ($has_already_voted) {
                    $query = $wpdb->prepare("UPDATE {$wpdb->prefix}wti_like_post SET \n\t\t\t\t\t\t\t\tvalue = value + 1,\n\t\t\t\t\t\t\t\tdate_time = '" . date('Y-m-d H:i:s') . "',\n\t\t\t\t\t\t\t\tuser_id = %d WHERE post_id = %d AND ip = %s", $user_id, $post_id, $wti_ip_address);
                } else {
                    $query = $wpdb->prepare("INSERT INTO {$wpdb->prefix}wti_like_post SET \n\t\t\t\t\t\t\t\tpost_id = %d, value = '1',\n\t\t\t\t\t\t\t\tdate_time = '" . date('Y-m-d H:i:s') . "',\n\t\t\t\t\t\t\t\tuser_id = %d, ip = %s", $post_id, $user_id, $wti_ip_address);
                }
            } else {
                if ($has_already_voted) {
                    $query = $wpdb->prepare("UPDATE {$wpdb->prefix}wti_like_post SET \n\t\t\t\t\t\t\t\tvalue = value - 1,\n\t\t\t\t\t\t\t\tdate_time = '" . date('Y-m-d H:i:s') . "',\n\t\t\t\t\t\t\t\tuser_id = %d WHERE post_id = %d AND ip = %s", $user_id, $post_id, $wti_ip_address);
                } else {
                    $query = $wpdb->prepare("INSERT INTO {$wpdb->prefix}wti_like_post SET \n\t\t\t\t\t\t\t\tpost_id = %d, value = '-1',\n\t\t\t\t\t\t\t\tdate_time = '" . date('Y-m-d H:i:s') . "',\n\t\t\t\t\t\t\t\tuser_id = %d, ip = %s", $post_id, $user_id, $wti_ip_address);
                }
            }
            $success = $wpdb->query($query);
            if ($success) {
                $error = 0;
                $msg = get_option('wti_like_post_thank_message');
            } else {
                $error = 1;
                $msg = __('Could not process your vote.', 'wti-like-post');
            }
        }
        $options = get_option('wti_most_liked_posts');
        $number = $options['number'];
        $show_count = $options['show_count'];
        $wti_like_count = GetWtiLikeCount($post_id);
        $wti_unlike_count = GetWtiUnlikeCount($post_id);
    }
    // Check for method of processing the data
    if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
        $result = array("msg" => $msg, "error" => $error, "like" => $wti_like_count, "unlike" => $wti_unlike_count);
        echo json_encode($result);
    } else {
        header("location:" . $_SERVER["HTTP_REFERER"]);
    }
    exit;
}
Beispiel #6
0
        if ($has_already_voted) {
            $query = "UPDATE {$wpdb->prefix}wti_like_post SET ";
            $query .= "value = value - 1, ";
            $query .= "date_time = '" . date('Y-m-d H:i:s') . "' ";
            $query .= "WHERE post_id = '" . $post_id . "' AND ";
            $query .= "ip = '{$ip}'";
        } else {
            $query = "INSERT INTO {$wpdb->prefix}wti_like_post SET ";
            $query .= "post_id = '" . $post_id . "', ";
            $query .= "value = '-1', ";
            $query .= "date_time = '" . date('Y-m-d H:i:s') . "', ";
            $query .= "ip = '{$ip}'";
        }
    }
    //echo $query;
    $success = $wpdb->query($query);
    if ($success) {
        $error = 0;
        $msg = get_option('wti_like_post_thank_message');
    } else {
        $error = 1;
        $msg = __('Could not process your vote.', 'wti-like-post');
    }
}
$options = get_option("wti_most_liked_posts");
$number = $options['number'];
$show_count = $options['show_count'];
$wti_like_count = GetWtiLikeCount($post_id);
$wti_unlike_count = GetWtiUnlikeCount($post_id);
$result = array("msg" => $msg, "error" => $error, "like" => $wti_like_count, "unlike" => $wti_unlike_count);
echo json_encode($result);
Beispiel #7
0
    function tm_html_video_meta($single = false, $label = false, $break = false, $listing_page = false, $post_id = false)
    {
        global $post;
        $post_id = $post_id ? $post_id : get_the_ID();
        ob_start();
        $view_count = get_post_meta($post_id, '_count-views_all', true);
        if ($single == 'view') {
            echo '<span class="pp-icon"><i class="fa fa-eye"></i> ' . ($view_count ? $view_count : 0) . '</span>';
        } elseif ($single == 'like') {
            if (function_exists('GetWtiLikeCount')) {
                echo '<span class="pp-icon iclike"><i class="fa fa-thumbs-up"></i> ' . str_replace('+', '', GetWtiLikeCount($post_id)) . '</span>';
            }
        } elseif ($single == 'comment') {
            echo '<span class="pp-icon"><i class="fa fa-comment"></i> ' . get_comments_number($post_id) . '</span>';
        } elseif ($listing_page) {
            if (ot_get_option('blog_show_meta_view', 1)) {
                ?>
        	<span><i class="fa fa-eye"></i> <?php 
                echo ($view_count ? $view_count : 0) . ($label ? __('  Views') : '');
                ?>
</span><?php 
                echo $break ? '<br>' : '';
                ?>
            <?php 
            }
            if (ot_get_option('blog_show_meta_comment', 1)) {
                ?>
            <span><i class="fa fa-comment"></i> <?php 
                echo get_comments_number($post_id) . ($label ? __('  Comments') : '');
                ?>
</span><?php 
                echo $break ? '<br>' : '';
                ?>
            <?php 
            }
            if (ot_get_option('blog_show_meta_like', 1) && function_exists('GetWtiLikeCount')) {
                ?>
            <span><i class="fa fa-thumbs-up"></i> <?php 
                echo str_replace('+', '', GetWtiLikeCount($post_id)) . ($label ? __('  Likes') : '');
                ?>
</span>
		<?php 
            }
        } else {
            ?>
            <span><i class="fa fa-eye"></i> <?php 
            echo ($view_count ? $view_count : 0) . ($label ? __('  Views') : '');
            ?>
</span>
            <?php 
            echo $break ? '<br>' : '';
            ?>
            <span><i class="fa fa-comment"></i> <?php 
            echo get_comments_number($post_id) . ($label ? __('  Comments') : '');
            ?>
</span>
            <?php 
            echo $break ? '<br>' : '';
            ?>
            <?php 
            if (function_exists('GetWtiLikeCount')) {
                ?>
            <span><i class="fa fa-thumbs-up"></i> <?php 
                echo str_replace('+', '', GetWtiLikeCount($post_id)) . ($label ? __('  Likes') : '');
                ?>
</span>
            <?php 
            }
        }
        $html = ob_get_clean();
        return $html;
    }
    function widget($args, $instance)
    {
        wp_enqueue_script('jquery-isotope');
        $cache = wp_cache_get('widget_trending_videos', 'widget');
        if (!is_array($cache)) {
            $cache = array();
        }
        if (!isset($argsxx['widget_id'])) {
            $argsxx['widget_id'] = $this->id;
        }
        if (isset($cache[$argsxx['widget_id']])) {
            echo $cache[$argsxx['widget_id']];
            return;
        }
        ob_start();
        extract($args);
        $conditions = empty($instance['conditions']) ? '' : $instance['conditions'];
        $title = empty($instance['title']) ? '' : $instance['title'];
        $title = apply_filters('widget_title', $title);
        $number = empty($instance['number']) ? '' : $instance['number'];
        $timerange = empty($instance['timerange']) ? '' : $instance['timerange'];
        $show_likes = empty($instance['show_likes']) ? '' : $instance['show_likes'];
        $show_com = empty($instance['show_com']) ? '' : $instance['show_com'];
        $link = empty($instance['link']) ? '' : $instance['link'];
        $show_view = empty($instance['show_view']) ? '' : $instance['show_view'];
        $show_rate = empty($instance['show_rate']) ? '' : $instance['show_rate'];
        $show_excerpt = empty($instance['show_excerpt']) ? '' : $instance['show_excerpt'];
        $num_ex = empty($instance['num_ex']) ? '' : $instance['num_ex'];
        if (function_exists('ot_get_option')) {
            $themes_pur = ot_get_option('theme_purpose');
        }
        if ($conditions == 'most_liked' && class_exists('CT_ContentHelper')) {
            global $wpdb;
            $show_count = 1;
            if ($timerange == 'day') {
                $time_range = '1';
            } else {
                if ($timerange == 'week') {
                    $time_range = '7';
                } else {
                    if ($timerange == 'month') {
                        $time_range = '1m';
                    } else {
                        if ($timerange == 'year') {
                            $time_range = '1y';
                        }
                    }
                }
            }
            //$time_range = $instance['time_range'];
            //$show_type = $instance['show_type'];
            $order_by = 'ORDER BY like_count DESC, post_title';
            if ($number > 0) {
                $limit = "LIMIT " . $number;
            }
            $widget_data = $before_widget;
            $widget_data .= $before_title . $title . $after_title;
            $show_excluded_posts = get_option('wti_like_post_show_on_widget');
            $excluded_post_ids = explode(',', get_option('wti_like_post_excluded_posts'));
            if (!$show_excluded_posts && count($excluded_post_ids) > 0) {
                $where = "AND post_id NOT IN (" . get_option('wti_like_post_excluded_posts') . ")";
            }
            if ($time_range != 'all') {
                $last_date = GetWtiLastDate($time_range);
                $where .= " AND date_time >= '{$last_date}'";
            }
            //getting the most liked posts
            $query = "SELECT post_id, SUM(value) AS like_count, post_title FROM `{$wpdb->prefix}wti_like_post` L, {$wpdb->prefix}posts P ";
            $query .= "WHERE L.post_id = P.ID AND post_status = 'publish' AND value > 0 {$where} GROUP BY post_id {$order_by} {$limit}";
            $posts = $wpdb->get_results($query);
            if (count($posts) > 0) {
                $item_loop_video = new CT_ContentHtml();
                $widget_data .= '
			<div class="widget-content">
					<div class="list-rating-item row">';
                foreach ($posts as $post) {
                    $p_data = $excerpt = '';
                    $post_title = stripslashes($post->post_title);
                    $permalink = get_permalink($post->post_id);
                    $like_count = $post->like_count;
                    $p_data = get_post($post->post_id);
                    $excerpt = strip_tags($p_data->post_content);
                    $excerpt = wp_trim_words($excerpt, $num_ex, $more = '');
                    $widget_data .= $item_loop_video->tm_likes_html($post, $like_count, $themes_pur, $show_likes, $show_com, $show_rate, $show_view, $show_excerpt, $excerpt);
                }
                $widget_data .= '</div>
			</div>';
            }
            $widget_data .= $after_widget;
            echo $widget_data;
        } else {
            if (class_exists('CT_ContentHelper')) {
                if ($conditions == 'most_viewed' || $conditions == '') {
                    if ($timerange == 'day') {
                        $args = array('post_type' => 'post', 'posts_per_page' => $number, 'meta_key' => '_count-views_day-' . date("Ymd"), 'orderby' => 'meta_value_num', 'order' => 'DESC', 'post_status' => 'publish');
                    } else {
                        if ($timerange == 'week') {
                            $args = array('post_type' => 'post', 'posts_per_page' => $number, 'meta_key' => '_count-views_week-' . date("YW"), 'orderby' => 'meta_value_num', 'order' => '', 'post_status' => 'publish');
                        } else {
                            if ($timerange == 'month') {
                                $args = array('post_type' => 'post', 'posts_per_page' => $number, 'meta_key' => '_count-views_month-' . date("Ym"), 'orderby' => 'meta_value_num', 'order' => '', 'post_status' => 'publish');
                            } else {
                                if ($timerange == 'year') {
                                    $args = array('post_type' => 'post', 'posts_per_page' => $number, 'meta_key' => '_count-views_year-' . date("Y"), 'orderby' => 'meta_value_num', 'order' => '', 'post_status' => 'publish');
                                }
                            }
                        }
                    }
                    if ($themes_pur != '0') {
                        $args['tax_query'] = array(array('taxonomy' => 'post_format', 'field' => 'slug', 'terms' => 'post-format-video'));
                    }
                    $the_query = new WP_Query($args);
                    $html = $before_widget;
                    if ($title) {
                        $html .= $before_title . $title . $after_title;
                    }
                    if ($the_query->have_posts()) {
                        $html .= '
						<div class="widget-content">
						<div class="list-rating-item row">
					';
                        $item_video = new CT_ContentHtml();
                        $i = 0;
                        while ($the_query->have_posts()) {
                            $the_query->the_post();
                            $i++;
                            $excerpt = get_the_excerpt();
                            $excerpt = wp_trim_words($excerpt, $num_ex, $more = '');
                            $html .= $item_video->get_item_video_trending($conditions, $themes_pur, $show_likes, $show_com, $show_rate, $show_view, $show_excerpt, $excerpt);
                        }
                        $html .= '</div>
					</div>';
                    }
                    $html .= $after_widget;
                    echo $html;
                } else {
                    if ($conditions == 'most_comments') {
                        wp_reset_postdata();
                        if ($timerange == 'day') {
                            $some_comments = get_comments(array('date_query' => array(array('after' => '1 day ago'))));
                        } else {
                            if ($timerange == 'week') {
                                $some_comments = get_comments(array('date_query' => array(array('after' => '1 week ago'))));
                            } else {
                                if ($timerange == 'month') {
                                    $some_comments = get_comments(array('date_query' => array(array('after' => '1 month ago'))));
                                } else {
                                    if ($timerange == 'year') {
                                        $some_comments = get_comments(array('date_query' => array(array('after' => '1 year ago'))));
                                    }
                                }
                            }
                        }
                        $html = $before_widget;
                        if ($title) {
                            $html .= $before_title . $title . $after_title;
                        }
                        $arr_id = array();
                        foreach ($some_comments as $comment) {
                            $arr_id[] = $comment->comment_post_ID;
                        }
                        $arr_id = array_unique($arr_id, SORT_REGULAR);
                        //$arr_id = implode(",", $arr_id);
                        $args = array('post_type' => 'post', 'posts_per_page' => $number, 'order' => $sort_by, 'post_status' => 'publish', 'post__in' => $arr_id, 'ignore_sticky_posts' => 1);
                        $query = new WP_Query($args);
                        if ($query->have_posts()) {
                            $html .= '
				<div class="widget-content">
					<div class="list-rating-item row">';
                            while ($query->have_posts()) {
                                $query->the_post();
                                $excerpt = get_the_excerpt();
                                $html .= '
						<div class="col-md-12 col-sm-4">
						  <div class="video-item">
							  <div class="videos-row">
									<div class="item-thumbnail">
										<a href="' . get_permalink(get_the_ID()) . '" title="' . get_the_title(get_the_ID()) . '">';
                                if (has_post_thumbnail(get_the_ID())) {
                                    $thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID()), 'thumb_139x89', true);
                                } else {
                                    $thumbnail[0] = function_exists('tm_get_default_image') ? tm_get_default_image() : '';
                                }
                                $html .= '<img src="' . $thumbnail[0] . '" alt="' . the_title_attribute('echo=0') . '" title="' . the_title_attribute('echo=0') . '">';
                                //'.get_the_post_thumbnail(get_the_ID(), array(139,89) ).'
                                if ($themes_pur != '0') {
                                    $html .= '<div class="link-overlay fa fa-play "></div>';
                                }
                                $html .= '</a>';
                                if ($show_rate != 'hide_r') {
                                    $html .= tm_post_rating(get_the_ID());
                                }
                                $html .= '</div>
								  <div class="item-info">
									<div class="all-info">
									<h2 class="rt-article-title"> <a href="' . get_permalink(get_the_ID()) . '" title="' . get_the_title(get_the_ID()) . '">' . get_the_title(get_the_ID()) . '</a></h2>
									<div class="item-meta">';
                                if ($show_view != 'hide_v') {
                                    $html .= '<span class="pp-icon"><i class="fa fa-eye"></i> ' . get_post_meta(get_the_ID(), '_count-views_all', true) . '</span><br>';
                                }
                                if ($show_likes != 'hide_l' && function_exists('GetWtiLikeCount')) {
                                    $html .= '<span class="pp-icon iclike"><i class="fa fa-thumbs-up"></i> ' . str_replace('+', '', GetWtiLikeCount(get_the_ID())) . '</span><br>';
                                }
                                if ($show_com != 'hide_c') {
                                    $html .= '<span class="pp-icon"><i class="fa fa-comment"></i> ' . get_comments_number(get_the_ID()) . '</span><br>';
                                }
                                $html .= '
										</div>
										</div>
										</div>';
                                if ($show_excerpt != 'hide_ex') {
                                    $html .= '<div class="pp-exceprt">' . $excerpt . '</div>';
                                }
                                $html .= '
							   </div>
						  </div>
						</div>
						';
                            }
                            $html .= '</div>
				</div>';
                        }
                        $html .= $after_widget;
                        echo $html;
                    }
                }
                wp_reset_postdata();
                $cache[$argsxx['widget_id']] = ob_get_flush();
                wp_cache_set('widget_trending_videos', $cache, 'widget');
            }
        }
    }
/**
 * 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);
    }
}
Beispiel #10
0
function GetWtiLikePost($arg = null)
{
    global $wpdb;
    $post_id = get_the_ID();
    $wti_like_post = "";
    //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'));
    $excluded_categories = get_option('wti_like_post_excluded_categories');
    $excluded_sections = get_option('wti_like_post_excluded_sections');
    if (empty($excluded_categories)) {
        $excluded_categories = array();
    }
    if (empty($excluded_sections)) {
        $excluded_sections = array();
    }
    $title_text = get_option('wti_like_post_title_text');
    $category = get_the_category();
    $excluded = false;
    //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()) {
        return;
    }
    //checking for excluded categories
    foreach ($category as $cat) {
        if (in_array($cat->cat_ID, $excluded_categories) && !in_array($post_id, $allowed_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)) {
        $like_count = GetWtiLikeCount($post_id);
        $unlike_count = GetWtiUnlikeCount($post_id);
        $msg = GetWtiVotedMessage($post_id);
        $alignment = "left" == get_option('wti_like_post_alignment') ? 'left' : 'right';
        $show_dislike = get_option('wti_like_post_show_dislike');
        $style = get_option('wti_like_post_voting_style') == "" ? 'style1' : get_option('wti_like_post_voting_style');
        $wti_like_post .= "<div id='watch_action'>";
        $wti_like_post .= "<div id='watch_position' style='float:" . $alignment . "; '>";
        $wti_like_post .= "<div id='action_like' >" . "<span class='like-" . $post_id . " like'><img title='" . __($title_text_like, 'wti-like-post') . "' id='like-" . $post_id . "' rel='like' class='lbg-{$style} jlk' src='" . WP_PLUGIN_URL . "/wti-like-post/images/pixel.gif'></span>" . "<span id='lc-" . $post_id . "' class='lc'>" . $like_count . "</span>" . "</div>";
        if ($show_dislike) {
            $wti_like_post .= "<div id='action_unlike' >" . "<span class='unlike-" . $post_id . " unlike'><img title='" . __($title_text_unlike, 'wti-like-post') . "' id='unlike-" . $post_id . "' rel='unlike' class='unlbg-{$style} jlk' src='" . WP_PLUGIN_URL . "/wti-like-post/images/pixel.gif'></span>" . "<span id='unlc-" . $post_id . "' class='unlc'>" . $unlike_count . "</span>" . "</div> ";
        }
        $wti_like_post .= "</div> ";
        $wti_like_post .= "<div id='status-" . $post_id . "' class='status' style='float:" . $alignment . "; '>&nbsp;&nbsp;" . $msg . "</div>";
        $wti_like_post .= "</div><div id='clear'></div>";
    }
    if ($arg == 'put') {
        return $wti_like_post;
    } else {
        echo $wti_like_post;
    }
}