RP_Rave1_theme/template-parts/achievement.php

280 lines
9 KiB
PHP
Raw Permalink Normal View History

2024-11-06 10:10:48 +00:00
<?php
// INFO: achievement.js Script betöltése
function rp_enqueue_toggle_script()
{
wp_enqueue_script(
"toggle-script",
get_template_directory_uri() . "/template-parts/achievement.js",
["jquery"],
false,
true
);
wp_localize_script("toggle-script", "ajax_object", [
"ajax_url" => admin_url("admin-ajax.php"),
"post_id" => get_the_ID(),
"nonce" => wp_create_nonce("heart_toggle_nonce"),
]);
}
add_action("admin_enqueue_scripts", "rp_enqueue_toggle_script");
// INFO: Metabox létrehozása az admin felületen
function rp_heart_meta_box_add()
{
add_meta_box(
"heart_meta_box_id",
__("Achievement szív", "codewp"),
"rp_heart_meta_box_html",
"post",
"side",
"default"
);
}
add_action("add_meta_boxes", "rp_heart_meta_box_add");
// INFO: Toggle gomb létrehozása az admin felületen
function rp_heart_meta_box_html($post)
{
$value = get_post_meta($post->ID, "_heart_enabled", true);
wp_nonce_field("save_heart_meta_box_data", "heart_meta_box_nonce");
?>
<div class="components-base-control">
<label class="components-base-control__label" for="heart_enabled_field"><?php _e(
"Megjelenítés:",
"codewp"
); ?></label>
<span class="components-form-toggle">
<input
type="checkbox"
id="heart_enabled_field"
name="heart_enabled_field"
class="components-form-toggle__input"
value="1"
<?php checked($value, "1"); ?>
/>
<span class="components-form-toggle__track"></span>
<span class="components-form-toggle__thumb"></span>
</span>
</div>
<?php
}
add_action("wp_ajax_toggle_heart", "rp_toggle_heart");
add_action("wp_ajax_nopriv_toggle_heart", "rp_toggle_heart");
function rp_toggle_heart()
{
if (!isset($_POST["post_id"])) {
wp_send_json_error("Invalid post ID");
}
$post_id = intval($_POST["post_id"]);
$current_value = get_post_meta($post_id, "_heart_enabled", true);
$new_value = $current_value === "1" ? "0" : "1";
update_post_meta($post_id, "_heart_enabled", $new_value);
wp_send_json_success(["new_value" => $new_value]);
}
// INFO: Achievement oszlop hozzáadása a bejegyzések listájához
function rp_add_achievement_column($columns)
{
$columns["achievement"] = __("Achievement", "codewp");
$columns["heart_count"] = __("Kattintások száma", "codewp");
return $columns;
}
add_filter("manage_posts_columns", "rp_add_achievement_column");
function rp_display_achievement_column($column, $post_id)
{
if ($column === "achievement") {
$heart_enabled = get_post_meta($post_id, "_heart_enabled", true);
echo $heart_enabled ? __("Igen", "codewp") : __("Nem", "codewp");
}
if ($column === "heart_count") {
$heart_enabled = get_post_meta($post_id, "_heart_enabled", true);
if ($heart_enabled) {
$heart_count = intval(get_post_meta($post_id, "_heart_count", true)) ?: 0;
echo $heart_count; // Kattintások számának megjelenítése
} else {
echo __("N/A", "codewp"); // Ha az achievement nincs engedélyezve, N/A megjelenítése
}
}
}
add_action(
"manage_posts_custom_column",
"rp_display_achievement_column",
10,
2
);
// INFO: Meta adatok mentése
function rp_save_heart_meta_box_data($post_id)
{
if (
!isset($_POST["heart_meta_box_nonce"]) ||
!wp_verify_nonce(
$_POST["heart_meta_box_nonce"],
"save_heart_meta_box_data"
)
) {
return;
}
if (array_key_exists("heart_enabled_field", $_POST)) {
$sanitized_value = sanitize_text_field($_POST["heart_enabled_field"]);
if (empty(get_post_meta($post_id, "_clicked_ips", true))) {
update_post_meta($post_id, "_clicked_ips", []);
}
if (empty(get_post_meta($post_id, "_heart_count", true))) {
update_post_meta($post_id, "_heart_count", 0);
}
update_post_meta($post_id, "_heart_enabled", $sanitized_value);
} else {
delete_post_meta($post_id, "_heart_enabled");
}
}
add_action("save_post", "rp_save_heart_meta_box_data");
// INFO: Szív állapot lekérése
function rp_get_heart_toggle_state()
{
if (!isset($_POST["post_id"])) {
wp_send_json_error("Invalid post ID");
}
$post_id = intval($_POST["post_id"]);
$current_value = get_post_meta($post_id, "_heart_enabled", true);
wp_send_json_success($current_value === "1");
}
add_action("wp_ajax_get_heart_toggle_state", "rp_get_heart_toggle_state");
// INFO: Szív állapot mentése
function rp_save_heart_toggle_state()
{
if (!isset($_POST["post_id"]) || !isset($_POST["state"])) {
wp_send_json_error("Invalid parameters");
}
$post_id = intval($_POST["post_id"]);
$state = sanitize_text_field($_POST["state"]) === "true" ? "1" : "0";
update_post_meta($post_id, "_heart_enabled", $state);
wp_send_json_success();
}
add_action("wp_ajax_save_heart_toggle_state", "rp_save_heart_toggle_state");
// INFO: IP cím titkosítása
function rp_get_hashed_ip()
{
$ip_address = $_SERVER["REMOTE_ADDR"];
return hash("sha256", $ip_address);
}
// INFO: Szívre kattintás kezelése
function rp_has_user_clicked($post_id)
{
$hashed_ip = rp_get_hashed_ip();
$clicked_ips = get_post_meta($post_id, "_clicked_ips", true) ?: [];
return in_array($hashed_ip, $clicked_ips);
}
function rp_register_heart_click()
{
if (!isset($_POST["post_id"])) {
return;
}
$post_id = intval($_POST["post_id"]);
$hashed_ip = rp_get_hashed_ip();
if (rp_has_user_clicked($post_id)) {
wp_send_json_error(__("Már kattintottál te kis huncut!", "codewp"));
}
$clicked_ips = get_post_meta($post_id, "_clicked_ips", true) ?: [];
$clicked_ips[] = $hashed_ip;
update_post_meta($post_id, "_clicked_ips", array_unique($clicked_ips));
$heart_count = intval(get_post_meta($post_id, "_heart_count", true)) ?: 0;
$heart_count++;
update_post_meta($post_id, "_heart_count", $heart_count);
wp_send_json_success(["heart_count" => $heart_count]);
}
add_action("wp_ajax_nopriv_register_heart_click", "rp_register_heart_click");
add_action("wp_ajax_register_heart_click", "rp_register_heart_click");
// INFO: Megjelenés a bejegyzésben
function rp_display_heart_for_post($post_id = null)
{
if (is_null($post_id)) {
global $post;
$post_id = $post->ID;
}
$show_heart = get_post_meta($post_id, "_heart_enabled", true);
$heart_count = intval(get_post_meta($post_id, "_heart_count", true)) ?: 0;
$user_has_clicked = rp_has_user_clicked($post_id);
if ($show_heart) { ?>
<div id="heart-container-<?php echo esc_attr(
$post_id
); ?>" class="heart-container">
<p class="heart"><i id="heart-icon-<?php echo esc_attr(
$post_id
); ?>" class="<?php echo $user_has_clicked
? "fa-solid fa-heart"
: "fa-regular fa-heart"; ?>" style="cursor:pointer; color:red;"></i>
<span id="heart-count-<?php echo esc_attr(
$post_id
); ?>" class="heart-count"><?php echo esc_html(
$heart_count
); ?></span></p>
<p id="heart-text-<?php echo esc_attr(
$post_id
); ?>" class="heart-text"><?php echo $user_has_clicked
? __(
"Gratulálok! Te már azok között vagy, akik sikeresen végigcsinálták a leírást!",
"codewp"
)
: __(
"Sikerült a leírás alapján végig csinálnod? <br />Kérlek nyomd be a szívet, hogy mások is lássák, eddig hány embernek sikerült végig csinálnia!",
"codewp"
); ?></p>
</div>
<script>
jQuery(document).ready(function($) {
$('#heart-icon-<?php echo esc_attr(
$post_id
); ?>').on('click', function() {
$.post('<?php echo admin_url("admin-ajax.php"); ?>', {
action: 'register_heart_click',
post_id: '<?php echo esc_attr($post_id); ?>'
}, function(response) {
if (response.success) {
$('#heart-count-<?php echo esc_attr(
$post_id
); ?>').text(response.data.heart_count);
$('#heart-icon-<?php echo esc_attr(
$post_id
); ?>').removeClass('fa-regular').addClass('fa-solid');
$('#heart-text-<?php echo esc_attr(
$post_id
); ?>').text('<?php echo __(
"Gratulálok! Te már azok között vagy, akik sikeresen végigcsinálták a leírást!",
"codewp"
); ?>');
} else {
alert(response.data);
}
});
});
});
</script>
<?php }
add_shortcode("display_heart", "rp_display_heart_for_post");
}
?>