Skip to content

desafio-finalizado-nick-granados #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions likes-plugin/css/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* Like Box */
.generic-content .like-box {
float: right;
font-size: 1rem;
background-color: #ededed;
box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.25);
color: red;
padding: 10px 10px 10px 25px;
border-radius: 4px;
cursor: pointer;
margin: 0 0 30px 30px;
position: relative;
}

.generic-content .like-box:hover {
background-color: #dedede;
}

.like-count {
padding-left: 10px;
}

.like-box .fa-heart-o {
transition: all 0.35s ease-out;
position: absolute;
left: 10px;
}

.like-box .fa-heart {
left: 10px;
transition: all 0.4s ease-out;
position: absolute;
visibility: hidden;
transform: scale(0.2);
opacity: 0;
}

.like-box[data-exists=yes] .fa-heart {
transform: scale(1);
visibility: visible;
opacity: 1;
}

.like-box[data-exists=yes] .fa-heart-o {
visibility: hidden;
opacity: 0;
}
90 changes: 90 additions & 0 deletions likes-plugin/inc/LikeRoute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace inc;

/**
* Class LikeRoute
*
* @category Api Route
* @package Back-end
* @author Nick Granados <[email protected]>
* @license http://opensource.org/licenses/MIT MIT
* @link https://github.com/internick2017/wordpress-back-end-challenge
*/
class LikeRoute
{
/**
* Função constructor init api
*
*
*/
public function __construct()
{
add_action('rest_api_init', array($this, 'postLikePluginRoute'));
}

/**
* Registrar rota and methods da api
*
* @return void
*/
public function postLikePluginRoute(): void
{
register_rest_route('likeuri/v1', 'manageLike', array(
'methods' => 'POST',
'callback' => array($this, 'createLike')
));
register_rest_route('likeuri/v1', 'manageLike', array(
'methods' => 'DELETE',
'callback' => array($this, 'deleteLike')
));
}

/**
* Função que cria o like
*
* @param $data
* @return int|void
*/
public function createLike($data)
{
if (is_user_logged_in()) {
global $wpdb;
$tableName = $wpdb->prefix . "likes";
$user = sanitize_text_field($data['user']);
$post = sanitize_text_field($data['post']);
$like = array(
'post_id' => $post,
'user_id' => $user,
);
$wpdb->insert($tableName, $like);
return $wpdb->insert_id;
}

die('Só usuário logado pode curtir a postagem');
}

/**
* Função que exclui o like
*
* @param $data
* @return string|void
*/
public function deleteLike($data)
{
global $wpdb;
$like = sanitize_text_field($data['like']);
$userID = get_current_user_id();
$post = sanitize_text_field($data['post']);
$tableName = $wpdb->prefix . "likes";
$result = $wpdb->get_results("SELECT * FROM $tableName WHERE user_id = $userID AND post_id = $post");
if (is_user_logged_in() && get_current_user_id() == $result[0]->user_id) {
$wpdb->delete($tableName, array('id' => $like));
return 'like apagado';
}
die('Só usuário logado pode curtir a postagem');
}
}



71 changes: 71 additions & 0 deletions likes-plugin/js/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
class Like {
constructor() {
this.events()
}

events() {
jQuery(".like-box").on("click", this.ourClickDispatcher.bind(this))
}

ourClickDispatcher(e) {
let currentLikeBox = jQuery(e.target).closest(".like-box");

if (currentLikeBox.attr("data-exists") == "yes") {
this.deleteLike(currentLikeBox)
} else {
this.createLike(currentLikeBox)
}
}

createLike(currentLikeBox) {
jQuery.ajax({
beforeSend: (xhr) => {
xhr.setRequestHeader('X-WP-Nonce', themeData.nonce);
},
url: themeData.rootUrl + '/wp-json/likeuri/v1/manageLike',
type: 'POST',
data: {
'user': currentLikeBox.data('user'),
'post': currentLikeBox.data('post'),
},
success: (response) => {
currentLikeBox.attr('data-exists', 'yes');
let likeCount = parseInt(currentLikeBox.find(".like-count").html(), 10);
likeCount++;
currentLikeBox.find(".like-count").html(likeCount);
currentLikeBox.attr("data-like", response);
console.log(response);
},
error: (response) => {
console.log(response);
}
})
}

deleteLike(currentLikeBox) {
jQuery.ajax({
beforeSend: (xhr) => {
xhr.setRequestHeader('X-WP-Nonce', themeData.nonce);
},
url: themeData.rootUrl + '/wp-json/likeuri/v1/manageLike',
data: {
'like': parseInt(currentLikeBox.attr('data-like'), 10),
'post': currentLikeBox.data('post'),
},
type: 'DELETE',
success: (response) => {
console.log(response);
currentLikeBox.attr('data-exists', 'no');
let likeCount = parseInt(currentLikeBox.find(".like-count").html(), 10);
likeCount--;
currentLikeBox.find(".like-count").html(likeCount);
currentLikeBox.attr("data-like", '');
},
error: (response) => {
console.log(response);
}
})
}
}

const like = new Like();
Loading