Skip to content

Instantly share code, notes, and snippets.

@lukecav
Last active December 13, 2017 17:11
Show Gist options
  • Save lukecav/ebbb7b06291e3a3b214a3f05dbcd62b8 to your computer and use it in GitHub Desktop.
Save lukecav/ebbb7b06291e3a3b214a3f05dbcd62b8 to your computer and use it in GitHub Desktop.
Simple Likes
jQuery( document ).ready( function ( $ ) {
$( document ).on( 'click', '.simple-like-btn', function( e ) {
e.preventDefault();
var like = {
'action' : $( this ).data( 'action' ),
'pid' : $( this ).data( 'pid' )
}
var likebtn = this;
$.post( ajax_object.ajax_url, like ).done( function( response ) {
response = JSON.parse( response );
if ( response.status == 'success' ) {
if ( response.action == 'like' ) {
$( '.eig-like-gravatars' ).fadeIn('slow').append( response.gravatar );
$( likebtn ).data( 'action', 'eig-unlike' );
$( likebtn ).html( 'Liked' );
$( likebtn ).removeClass( 'like' );
$( likebtn ).addClass( 'liked' );
} else {
$( '.eig-like-gravatars .eig-like-user-' + response.uid ).fadeOut();
$( likebtn ).data( 'action', 'eig-like' );
$( likebtn ).html( 'Like' );
$( likebtn ).removeClass( 'liked' );
$( likebtn ).addClass( 'like' );
}
}
} );
} );
} );
<?php
/*
Plugin Name: Simple Likes
Description: This plugin adds simple like functionality per post.
Version: 1.0
Author: Luke Cavanagh
Author URI: http://www.lcavanagh.bluehoststaff.com/
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
function simple_like_css() {
?>
<style type="text/css">
.simple-like-btn {
background: transparent;
border: 1px #cccccc solid;
border-radius: 3px;
box-shadow: none;
text-align: left;
text-decoration: none;
color: #666666 !important;
padding: 3px;
padding-top: 5px;
padding-right: 10px;
margin: 0;
line-hsimpleht: 1.5;
hsimpleht: auto;
font-size: 12px;
background-color: #f8f8f8;
box-shadow: 0 1px 0 rgba(0, 0, 0, 0.08);
-webkit-box-shadow: 0 1px 0 0 rgba(0, 0, 0, 0.08);
-webkit-font-smoothing: auto !important;
-moz-osx-font-smoothing: auto;
}
.simple-like-btn:hover {
border-color: #999999;
color: #777777;
background-color: #f8f8f8;
}
.simple-like-gravatars {
padding-right: 5px;
padding-left: 5px;
}
.simple-like-gravatars img{
margin: 3px;
}
</script>
<?php
}
add_action( 'wp_head', 'simple_like_css' );
function simple_ajax_like() {
if ( isset( $_POST['pid'] ) && is_numeric( $_POST['pid'] ) ) {
$pid = (int)$_POST['pid'];
$likes = get_post_meta( $pid, 'likes', true );
if ( is_array( $likes ) ) {
$likes[] = get_current_user_id();
} else {
$likes = array( get_current_user_id() );
}
$likes = array_unique( $likes );
if ( update_post_meta( $pid, 'likes', $likes ) ) {
echo json_encode( array( 'status' => 'success', 'action' => 'like', 'gravatar' => get_avatar( get_current_user_id(), 30, '', false, array( 'class' => 'simple-like-user-' . get_current_user_id() ) ) ) );
} else {
echo json_encode( array( 'status' => 'error' ) );
}
}
die;
}
add_action( 'wp_ajax_simple-like', 'simple_ajax_like' );
function simple_ajax_unlike() {
if ( isset( $_POST['pid'] ) && is_numeric( $_POST['pid'] ) ) {
$pid = (int)$_POST['pid'];
$likes = get_post_meta( $pid, 'likes', true );
if ( is_array( $likes ) && false !== ( $key = array_search( get_current_user_id(), $likes ) ) ) {
unset( $likes[ $key ] );
}
$likes = array_unique( $likes );
if ( update_post_meta( $pid, 'likes', $likes ) ) {
echo json_encode( array( 'status' => 'success', 'action' => 'unlike', 'uid' => get_current_user_id() ) );
} else {
echo json_encode( array( 'status' => 'error' ) );
}
}
die;
}
add_action( 'wp_ajax_simple-unlike', 'simple_ajax_unlike' );
function simple_add_button( $content ) {
global $post;
$post_id = $post->ID;
$likes = get_post_meta( $post_id, 'likes', true );
if ( ! is_array( $likes ) || ! in_array( get_current_user_id(), $likes ) ) {
$output = '<a data-action="simple-like" data-pid="' . $post_id . '" class="simple-like-btn like genericon genericon-star" title="Like" href="#">Like</a>';
} else {
$output = '<a data-action="simple-unlike" data-pid="' . $post_id . '" class="simple-like-btn liked genericon genericon-star" title="Like" href="#">Liked</a>';
}
$output .= '<span class="simple-like-gravatars">';
if ( is_array( $likes ) ) {
foreach ( $likes as $user ) {
$output .= get_avatar( $user, 30, '', false, array( 'class' => 'simple-like-user-' . $user ) );
}
}
$output .= '</span>';
return $content . $output;
}
add_filter( 'the_content', 'simple_add_button', 40, 1 );
function simple_head_js() {
wp_enqueue_script( 'simple-like-actions', plugin_dir_url( __FILE__ ) . 'js/like-actions.js', 'jquery' );
wp_localize_script( 'simple-like-actions', 'ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
}
add_action( 'wp_enqueue_scripts', 'simple_head_js', 80 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment