jQuery(document).ready(function ($) {
$(".reaction")
.stop()
.click(function (e) {
var rel = $(this).attr("rel");
var type = $(this).attr("data-type");
var data = {
data: rel,
action: "like_callback",
post_action: type,
};
$.ajax({
type: "GET",
url: ajaxSite.ajaxurl,
dataType: "json",
data: data,
success: function (data) {
if (data.success == false) {
alert("Вы уже поставили оценку");
}
},
});
e.preventDefault();
});
});
<?php
add_action( 'wp_enqueue_scripts', 'ajax_scripts' );
function ajax_scripts() {
wp_enqueue_script( 'ajax', get_template_directory_uri() . '/assets/js/ajax.js', array( 'jquery' ));
wp_localize_script( 'ajax', 'ajaxSite', array(
'ajaxurl' => admin_url( 'admin-ajax.php' )
) );
}
add_action('wp_ajax_like_callback', 'reaction_callback');
add_action('wp_ajax_nopriv_like_callback', 'reaction_callback');
function set_cookie($type) {
return setcookie($type, 'true', time() + (86400 * 365)); // 86400 = 1 день в секундах
}
function reaction_callback(){
$id = json_decode($_GET['data']);
$post_action = isset($_GET['post_action']) ? $_GET['post_action'] : '';
$err_message = array();
$reactions = array(
"likes" => "",
"dislikes" => "",
"neutrals" => ""
);
$currentIp = get_post_meta( $id, '_likers', true );
$likesarray = explode(', ', $currentIp);
$likes = intval(get_post_meta( $id, 'likes', true ));
$dislikes = intval(get_post_meta( $id, 'dislikes', true ));
$neutrals = intval(get_post_meta( $id, 'neutrals', true ));
if($_COOKIE['reaction-'.$id.''] != true) {
if($post_action == 'like') {
update_post_meta($id, 'likes', $likes + 1, $likes);
$reactions["likes"] = ["count" => $likes, "status" => true];
} elseif($post_action == 'dislike') {
update_post_meta($id, 'dislikes', $dislikes + 1, $dislikes);
$reactions["dislikes"] = ["count" => $dislikes, "status" => true];
} elseif($post_action == 'neutral') {
update_post_meta($id, 'neutrals', $neutrals + 1, $neutrals);
$reactions["neutrals"] = ["count" => $neutrals, "status" => true];
}
echo json_encode($reactions);
set_cookie('reaction-'.$id.'');
} else {
$err_message['cookie'] = 'Вы уже поставили оценку.';
wp_send_json_error( $err_message );
}
die();
}
const quizzes = document.querySelectorAll(".calc__fields");
quizzes.forEach((quiz) => {
const questions = quiz.querySelectorAll(".fields__field"),
btnsPrev = quiz.querySelectorAll(".btn-prev"),
btnsNext = quiz.querySelectorAll(".btn--icon-next"),
stagesWrapper = quiz.querySelector(".fields__stages");
stages = quiz.querySelectorAll(".fields__stage");
// progress
const progressCount = questions.length;
btnsNext.forEach((btnNext, btnNextIndex) => {
btnNext.addEventListener("click", (e) => {
e.preventDefault();
questions[btnNextIndex].classList.remove("_active");
questions[btnNextIndex + 1].classList.add("_active");
stages[btnNextIndex].classList.remove("_active");
stages[btnNextIndex + 1].classList.add("_active");
});
if (!questions[btnNextIndex].classList.contains("_range")) {
btnNext.disabled = true;
}
});
btnsPrev.forEach((btnPrev, btnPrevIndex) => {
btnPrev.addEventListener("click", (e) => {
e.preventDefault();
questions[btnPrevIndex + 1].classList.remove("_active");
questions[btnPrevIndex].classList.add("_active");
// count--;
// currentNum.innerHTML = count;
});
});
questions.forEach((question, questionIndex) => {
if (questionIndex === 0) {
question.classList.add("_active");
} else {
question.classList.remove("_active");
}
const count = questionIndex + 1;
if (count === 1) {
stagesWrapper.innerHTML += '<li class="fields__stage _active">' + "Шаг " + count + "</li>";
} else {
stagesWrapper.innerHTML += '<li class="fields__stage">' + "Шаг " + count + "</li>";
}
question.addEventListener("change", (e) => {
const target = e.target,
inputsChecked = question.querySelectorAll("input:checked");
if (inputsChecked.length > 0 || target.classList.contains("range__input")) {
btnsNext[questionIndex].disabled = false;
} else {
btnsNext[questionIndex].disabled = true;
}
});
});
});
stages[btnNextIndex].classList.remove("_active");
stages[btnNextIndex + 1].classList.add("_active");
"style": "file:./style-index.css" из block.json подключать только если на странице есть блок