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"); ?>
/>
$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) { ?>

" style="cursor:pointer; color:red;">

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" ); ?>