/** * @package air * @since 2.0 * @name Post like/dislike * Author: http://code.tutsplus.com/articles/how-to-create-a-simple-post-rating-system-with-wordpress-and-jquery--wp-24474 */ function post_vote() { // Check for nonce security $nonce = $_POST['nonce']; if (!wp_verify_nonce($nonce, 'ajax-nonce')) { die('Busted!'); } if (isset($_POST['post_like'])) { // Retrieve user IP address $ip = $_SERVER['REMOTE_ADDR']; $post_id = $_POST['post_id']; // Get voters'IPs for the current post $meta_IP = get_post_meta($post_id, "voted_IP"); $voted_IP = $meta_IP[0]; if (!is_array($voted_IP)) { $voted_IP = array(); } // Get votes count for the current post $meta_count = get_post_meta($post_id, "votes_count", true); // Use has already voted ? if (!hasAlreadyVoted($post_id)) { $voted_IP[$ip] = time(); // Save IP and increase votes count update_post_meta($post_id, "voted_IP", $voted_IP); update_post_meta($post_id, "votes_count", ++$meta_count); // Display count (ie jQuery return value) echo $meta_count; } else { echo "already"; } } exit; }
function like_status($post_id) { if (hasAlreadyVoted($post_id)) { echo 'just-voted'; } else { echo 'not-voted'; } }
function getPostLikeLink($post_id) { $vote_count = get_post_meta($post_id, "votes_count", true); if (empty($vote_count)) { $vote_count = 0; } echo '<span class="post-like">' . "\n"; if (hasAlreadyVoted($post_id)) { echo '<span title="' . __('I like this article', 'color-theme-framework') . '" class="qtip like icon-heart alreadyvoted"></span>' . "\n"; } else { echo '<a href="#" data-post_id="' . $post_id . '">' . "\n" . '<span title="' . __('I like this article', 'color-theme-framework') . '" class="qtip like icon-heart"></span></a>' . "\n"; } echo '<span class="count">' . $vote_count . '</span><!-- .count -->' . "\n" . '</span><!-- .post-like -->' . "\n"; // return $output; }
function getPostLikeLink($post_id) { $vote_count = get_post_meta($post_id, "votes_count", true); if ($vote_count == '') { $vote_count = 0; } $output = '<p class="post-like">'; if (hasAlreadyVoted($post_id)) { $output .= ' <span title="I like this article" class="qtip like alreadyvoted"></span>'; } else { $output .= '<a href="#" data-post_id="' . $post_id . '"> <span title="I like this article" class="qtip like"></span> </a>'; } $output .= '<span class="count">' . $vote_count . '</span></p>'; return $output; }
/** * Post Like. Social Share * @param integer $post_id Post ID * @return string Post like code */ function getPostLikeLink($post_id = null) { if (!$post_id) { global $post; $post_id = $post->ID; } $vote_count = get_post_meta($post_id, "_votes_count", true); $output = '<span class="post-like">'; if (hasAlreadyVoted($post_id)) { $output .= ' <span title="' . __('I like this article', 'dfd') . '" class="like alreadyvoted"><i class="icon-add-1"></i></span>'; } else { $output .= '<a href="#" data-post_id="' . $post_id . '"> <i class="icon-add-1"></i> <span title="' . __('I like this article', 'dfd') . '" class="qtip like"></span> </a>'; } $output .= '<span class="count">' . $vote_count . '</span></span>'; return $output; }
function getPostLikeLink($post_id) { $themename = "twentyeleven"; $vote_count = get_post_meta($post_id, "votes_count", true); $output = '<p class="post-like">'; if (hasAlreadyVoted($post_id)) { $output .= ' <span title="' . __('I like this article', $themename) . '" class="like alreadyvoted fa"></span>'; } else { $output .= '<a href="#" data-post_id="' . $post_id . '"> <span title="' . __('I like this article', $themename) . '"class="qtip like fa"></span> </a>'; } $output .= '<span class="count">' . $vote_count . '</span></p>'; return $output; }
function get_post_like($post_id) { $themename = "sergeymotovilets"; $vote_count = get_post_meta($post_id, "votes_count", true); if (!$vote_count) { $vote_count = 0; } $output = '<div class="post-like">'; if (hasAlreadyVoted($post_id)) { $output .= ' <p class="already-voted-wrapper"><span title="' . __('I like this article', $themename) . '" class="like already-voted"><svg><use xlink:href="#heart" /></svg></span></p>'; } else { $output .= '<a href="#" data-post_id="' . $post_id . '"> <span title="' . __('I like this article', $themename) . '"class="qtip like voted"><svg><use xlink:href="#heart" /></svg></span> </a>'; } $output .= '<p class="count">' . $vote_count . '</p></div>'; return $output; }
?> </td> </tr> </tbody> </table> </div> <div class="pigura-tags"> <h2>Tag</h2> <?php the_tags('', ''); ?> </div> <div class="pigura-acts info-box"> <span class="act-love-wrap"> <?php if (hasAlreadyVoted(get_the_ID())) { ?> <a class="voted" data-post_id="<?php echo get_the_ID(); ?> " href="#"> <span class="act-love" id="act-voted"><i class="fa fa-heart"></i> Anda Suka</span></a> <?php } else { ?> <a data-post_id="<?php echo get_the_ID(); ?> " href="#"> <span class="act-love"><i class="fa fa-heart"></i> Suka</span></a> <?php
function generateRatingHTML($post_id) { // get meta stuff $meta_up = get_post_meta($post_id, "votes_yesvotes", true); $meta_total = get_post_meta($post_id, "votes_totalvotes", true); // the html to write $output = '<div class="post-rate">'; // what they'll see if they've already voted if (hasAlreadyVoted($post_id)) { $output .= '<div class="ratePercentage"><div class="percentageChart" style="width:' . calculatePercentage($meta_up, $meta_total) . ';"></div><h2>' . calculatePercentage($meta_up, $meta_total) . ' </h2></div><!--.ratePercentage-->'; $output .= '<span class="voteUp alreadyVoted" data-post_id="' . $post_id . '"></span>'; $output .= '<span class="voteDown alreadyVoted" data-post_id="' . $post_id . '"></span>'; $output .= '</div><!--.post-rate-->'; } else { $output .= '<div class="ratePercentage"><div class="percentageChart" style="width:' . calculatePercentage($meta_up, $meta_total) . ';"></div><h2>' . calculatePercentage($meta_up, $meta_total) . '</h2></div><!--.ratePercentage-->'; $output .= '<a class="voteUp" href="#" data-post_id="' . $post_id . '"></a>'; $output .= '<a class="voteDown" href="#" data-post_id="' . $post_id . '"></a>'; $output .= '</div><!--.post-rate-->'; } return $output; }