Commit cb2d17c5 by rajshah

feat: download resource count fix v1.6

parent 3f0f54d5
...@@ -343,28 +343,28 @@ ...@@ -343,28 +343,28 @@
})(jQuery); })(jQuery);
// download count // download count
jQuery(document).ready(function ($) { jQuery(document).ready(function ($) {
$(".btn-download").on("click", function () { $("#btn-resource-download").on("click", function () {
const resourceId = $(this).data("resource-id"); const resourceId = $(this).data("resource-id");
console.log("Resource ID:", resourceId); console.log("Resource ID:", resourceId);
$.ajax({ $.ajax({
url: download_resource_object.ajaxurl, url: download_resource_object.ajax_url,
type: "POST", type: "POST",
data: { data: {
action: "increment_download_count", action: "increment_download_count",
resource_id: resourceId, resource_id: resourceId,
}, },
success: function (response) { success: function (response) {
console.log("AJAX Response:", response); console.log("AJAX Response:", response);
if (response.success) { if (response.success) {
$("#download-count").text(response.new_count); $("#download-count").text(response.new_count);
} else { } else {
console.error("Error:", response.message); console.log("Error:", response.message);
} }
}, },
error: function (xhr, status, error) { error: function (xhr, status, error) {
console.error("AJAX Error:", error); console.log("AJAX Error:", error);
}, },
}); });
}); });
}); });
\ No newline at end of file
...@@ -410,35 +410,28 @@ function increment_download_count() ...@@ -410,35 +410,28 @@ function increment_download_count()
{ {
// Verify the AJAX request // Verify the AJAX request
if (!isset($_POST['resource_id']) || !is_numeric($_POST['resource_id'])) { if (!isset($_POST['resource_id']) || !is_numeric($_POST['resource_id'])) {
error_log('Invalid resource ID');
wp_send_json_error(['message' => 'Invalid resource ID']); wp_send_json_error(['message' => 'Invalid resource ID']);
} }
$resource_id = intval($_POST['resource_id']); $resource_id = intval($_POST['resource_id']);
error_log('Resource ID: ' . $resource_id);
// Start session if not already started // Start session if not already started
if (!session_id()) { if (!session_id()) {
session_start(); session_start();
error_log('Session started');
} }
// Check if the user has already clicked the button in this session // Check if the user has already clicked the button in this session
if (isset($_SESSION['downloaded_resources']) && in_array($resource_id, $_SESSION['downloaded_resources'])) { if (isset($_SESSION['downloaded_resources']) && in_array($resource_id, $_SESSION['downloaded_resources'])) {
error_log('Already counted for resource ID: ' . $resource_id);
wp_send_json_success(['message' => 'Already counted']); wp_send_json_success(['message' => 'Already counted']);
} }
// Increment the download count // Increment the download count
$current_count = (int) get_post_meta($resource_id, 'download_count', true); $current_count = (int) get_post_meta($resource_id, 'download_count', true);
error_log('Current count: ' . $current_count);
$new_count = $current_count + 1; $new_count = $current_count + 1;
update_post_meta($resource_id, 'download_count', $new_count); update_post_meta($resource_id, 'download_count', $new_count);
error_log('New count: ' . $new_count);
// Store the resource ID in the session to prevent multiple counts // Store the resource ID in the session to prevent multiple counts
$_SESSION['downloaded_resources'][] = $resource_id; $_SESSION['downloaded_resources'][] = $resource_id;
error_log('Resource added to session: ' . $resource_id);
wp_send_json_success(['new_count' => $new_count]); wp_send_json_success(['new_count' => $new_count]);
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment