Commit ba7c3a74 by rajshah

feat: download resource count fix v2.1

parent 68a4c915
......@@ -344,11 +344,9 @@
// download count
jQuery(document).ready(function ($) {
$(".btn-resource-download").on("click", function (e) {
e.preventDefault(); // Prevent default download
e.preventDefault();
const resourceId = $(this).data("resource-id");
const downloadUrl = $(this).attr("href");
console.log("Resource ID:", resourceId);
console.log("AJAX URL:", download_resource_object.ajaxurl);
$.ajax({
url: download_resource_object.ajaxurl,
type: "POST",
......@@ -358,18 +356,17 @@ jQuery(document).ready(function ($) {
nonce: download_resource_object.nonce,
},
success: function (response) {
console.log("AJAX Response:", response);
if (response.success) {
$("#download-count").text(response.data.new_count);
window.location.href = downloadUrl; // Trigger download
window.location.href = downloadUrl;
} else {
console.log("Error:", response.data ? response.data.message : "Invalid response");
window.location.href = downloadUrl; // Trigger download even on error
// console.log("Error:", response.data ? response.data.message : "Invalid response");
window.location.href = downloadUrl;
}
},
error: function (xhr, status, error) {
console.log("AJAX Error:", error);
window.location.href = downloadUrl; // Trigger download on AJAX failure
// console.log("AJAX Error:", error);
window.location.href = downloadUrl;
},
});
});
......
......@@ -403,38 +403,27 @@ function calculate_reading_time($content)
return $reading_time;
}
// !article read time
// resource download count
add_action('wp_ajax_increment_download_count', 'increment_download_count');
add_action('wp_ajax_nopriv_increment_download_count', 'increment_download_count');
function increment_download_count() {
// Prevent unwanted output
ob_start();
// Verify nonce
if (!check_ajax_referer('increment_download_count_nonce', 'nonce', false)) {
ob_end_clean();
wp_send_json_error(['message' => 'Nonce verification failed']);
}
// Verify resource ID
if (!isset($_POST['resource_id']) || !is_numeric($_POST['resource_id'])) {
ob_end_clean();
wp_send_json_error(['message' => 'Invalid resource ID']);
}
$resource_id = intval($_POST['resource_id']);
// Start session if not already started
if (!session_id()) {
session_start();
}
// Initialize session array
if (!isset($_SESSION['downloaded_resources'])) {
$_SESSION['downloaded_resources'] = [];
}
// Check if already counted in this session
if (in_array($resource_id, $_SESSION['downloaded_resources'])) {
ob_end_clean();
wp_send_json_success([
......@@ -442,15 +431,10 @@ function increment_download_count() {
'new_count' => (int) get_post_meta($resource_id, 'download_count', true)
]);
}
// Increment download count
$current_count = (int) get_post_meta($resource_id, 'download_count', true);
$new_count = $current_count + 1;
update_post_meta($resource_id, 'download_count', $new_count);
// Store resource ID in session
$_SESSION['downloaded_resources'][] = $resource_id;
ob_end_clean();
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