Skip to content
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
25 changes: 25 additions & 0 deletions css/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
.likeImg{
display: inline-block;
width: 20px;
cursor: pointer;
transition: transform 0.3s ease-in-out;
}

.likeImg:hover {
transform: scale(1.4);
}

.alert{
position: relative;
padding: 0.75rem 1.25rem;
margin-bottom: 1rem;
border: 1px solid transparent;
border-radius: 0.25rem;
}

.alert-warning{
color: #856404;
background-color: #fff3cd;
border-color: #ffeeba;
}

3 changes: 3 additions & 0 deletions img/heart-active.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions img/heart.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
92 changes: 92 additions & 0 deletions includes/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

function add_text_after_content($content) {



global $wpdb;
$post_id = get_the_ID();
$user_id = get_current_user_id();

$table_name = $wpdb->prefix . 'liked';

$query = $wpdb->prepare("SELECT * FROM $table_name WHERE id_post = %d AND id_user = %d", $post_id, $user_id);
$already_liked = $wpdb->get_row($query);

$active = '';
$liked = 'false';
if($already_liked){
$active = '-active';
$liked = 'true';
}

if (is_singular('post')) {

if(!is_user_logged_in()){
$content = "<div class='alert alert-warning'>funcionalidade de like somente para usuários cadastrados</div>";
return $content;
}

$img_url = plugin_dir_url(dirname(__FILE__)) . "img/heart$active.svg";
$img_html = '<div class="likeContainer"><img src="' . $img_url . '" class="likeImg" id="clickToLike" data-post-id="' . $post_id . '" data-user-id="' . $user_id . '" data-liked="' . $liked . '" alt="like"/></div>';
$content .= $img_html;
}

return $content;
}
add_filter('the_content', 'add_text_after_content');

// Função para adicionar o like
function register_like() {

if (isset($_POST['post_id']) && isset($_POST['user_id']) && isset($_POST['is_liked'])) {
global $wpdb;

$table_name = $wpdb->prefix . 'liked';

$post_id = $_POST['post_id'];
$user_id = $_POST['user_id'];
$is_liked = $_POST['is_liked'];

if($is_liked == "true"){
$where = array(
'id_user' => $user_id,
'id_post' => $post_id
);
$wpdb->delete($table_name, $where);
wp_send_json(array(
'success' => true,
'message' => 'disliked'
));
return false;

}

$wpdb->insert(
$table_name,
array(
'id_post' => $post_id,
'id_user' => $user_id
)
);

wp_send_json(array(
'success' => true,
'message' => 'liked'
));

} else {

wp_send_json(array(
'success' => false,
'message' => 'deu b.o'
));

}

wp_die();
}

// Hook para adicionar a função no arquivo admin-ajax.php
add_action('wp_ajax_register_like', 'register_like');
add_action('wp_ajax_nopriv_register_like', 'register_like');
18 changes: 18 additions & 0 deletions includes/install.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

function create_custom_table(){
global $wpdb;

$nome_tabela = $wpdb->prefix . 'liked'; // Prefixo do WordPress + nome da tabela

$sql = "CREATE TABLE IF NOT EXISTS $nome_tabela (
id INT NOT NULL AUTO_INCREMENT,
id_post INT NOT NULL,
id_user INT NOT NULL,
PRIMARY KEY (id)
)";

$wpdb->query($sql);
}

register_activation_hook( __FILE__, 'create_custom_table' );
39 changes: 39 additions & 0 deletions js/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
jQuery(document).ready(function ($) {
$(".likeImg").click(function () {
let postID = $(this).data("post-id");
let userID = $(this).data("user-id");
let liked = $(this).data("liked");

let data = {
action: "register_like",
post_id: postID,
user_id: userID,
is_liked: liked,
};
// console.log(liked);
// console.log(myAjax);

$.post(myAjax.ajaxurl, data, function (response) {
// console.log(response);
if (response.success == true) {
if (response.message == "disliked") {
// console.log("tirar active");
$(".likeImg").attr("src", myAjax.imgUrl + "heart.svg");
$(".likeImg").data("liked", "false");
return false;
}

if (response.message == "liked") {
// console.log("mudar para active");
$(".likeImg").attr("src", myAjax.imgUrl + "heart-active.svg");
$(".likeImg").data("liked", "true");
return false;
}
}

if (response.success == false) {
alert(response.message);
}
});
});
});
35 changes: 35 additions & 0 deletions like.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/*
Plugin Name: Like
Description: Like nos posts
Version: 1.0
Author: André Nascimento
*/

function active_plugin() {
require_once plugin_dir_path(__FILE__) . 'includes/install.php';
create_custom_table();
}
register_activation_hook(__FILE__, 'active_plugin');

function enqueue_scripts() {
$plugin_dir = plugin_dir_url(__FILE__);

wp_enqueue_script('like-js', $plugin_dir . 'js/script.js', array('jquery'), '1.0', true);
wp_enqueue_style('like-style', $plugin_dir . 'css/style.css', array(), '1.0');
// Passando o valor do admin-ajax.php para o JavaScript
$paths = array(
'ajaxurl' => admin_url('admin-ajax.php'),
'imgUrl' => plugin_dir_url(dirname(__FILE__)) . "like/img/"
);
wp_localize_script('like-js', 'myAjax', $paths);
}
add_action('wp_enqueue_scripts', 'enqueue_scripts');


require_once plugin_dir_path(__FILE__) . 'includes/functions.php';