Example #1
0
/**
 * Get post stats(views/comments/likes)
 *
 * @since deTube 1.0
 */
function dp_get_post_stats($pid = '')
{
    global $post;
    if (!$pid) {
        $pid = $post->ID;
    }
    if (!$pid) {
        return;
    }
    $views = sprintf(__('%s <span class="suffix">Views</span>', 'dp'), '<i class="count">' . dp_get_post_views($pid) . '</i>');
    $comments = sprintf(__('%s <span class="suffix">Comments</span>', 'dp'), '<i class="count">' . get_comments_number($pid) . '</i>');
    $likes = sprintf(__('%s <span class="suffix">Likes</span>', 'dp'), '<i class="count" data-pid="' . $pid . '">' . dp_get_post_likes($pid) . '</i>');
    $liked = dp_is_user_liked_post($pid) ? ' liked' : '';
    $stats = '<span class="views">' . $views . '</span>';
    $stats .= '<span class="comments">' . $comments . '</span>';
    $stats .= '<span class="dp-post-likes likes' . $liked . '">' . $likes . '</span>';
    return $stats;
}
Example #2
0
/**
 * Insert a post like into database
 */
function dp_insert_post_like($arr, $wp_error = false)
{
    global $wpdb;
    $defaults = array('post_id' => '', 'user_id' => 0, 'like_ip' => '', 'like_time' => '');
    $arr = wp_parse_args($arr, $defaults);
    // export array as variables
    extract($arr, EXTR_SKIP);
    $like_id = 0;
    // Check post id
    if (!$post_id) {
        if ($wp_error) {
            return new WP_Error('invalid_post_id', __('Invaild post ID.', 'dp'));
        } else {
            return 0;
        }
    }
    // Check liked
    $liked = dp_is_user_liked_post($post_id);
    if ($liked) {
        if ($wp_error) {
            return new WP_Error('liked', __('You already liked.', 'dp'));
        } else {
            return 0;
        }
    }
    // Get user ip
    $like_ip = preg_replace('/[^0-9a-fA-F:., ]/', '', $_SERVER['REMOTE_ADDR']);
    // Get current time
    $like_time = current_time('mysql');
    // expected_slashed (everything!)
    $data = compact(array('post_id', 'user_id', 'like_ip', 'like_time'));
    $data = stripslashes_deep($data);
    if (false === $wpdb->insert($wpdb->postlikes, $data)) {
        if ($wp_error) {
            return new WP_Error('db_insert_error', __('Could not insert post like into the database', 'dp'), $wpdb->last_error);
        } else {
            return 0;
        }
    }
    $like_id = (int) $wpdb->insert_id;
    // update post meta
    $likes = (int) get_post_meta($post_id, 'likes', true);
    update_post_meta($post_id, 'likes', $likes + 1);
    return $like_id;
}