Commit 81e870c1 by rajshah

feat: download count in resource detail v0.7

parent ca4b48c3
...@@ -610,27 +610,28 @@ ...@@ -610,27 +610,28 @@
})(jQuery); })(jQuery);
// download count // download count
jQuery(document).ready(function ($) { jQuery(document).ready(function ($) {
$('#btn-resource-download').on('click', function () { $("#btn-resource-download").on("click", function () {
const resourceId = $(this).data('resource-id'); const resourceId = $(this).data("resource-id");
console.log(resourceId); console.log("Resource ID:", resourceId);
$.ajax({
url: frontend_ajax_object.ajax_url, $.ajax({
type: "POST", url: frontend_ajax_object.ajax_url,
data: { type: "POST",
action: "increment_download_count", data: {
resource_id: resourceId, action: "increment_download_count",
}, resource_id: resourceId,
success: function (response) { },
console.log(response); success: function (response) {
if (response.success) { console.log("AJAX Response:", response);
$("#download-count").text(response.new_count); if (response.success) {
} else { $("#download-count").text(response.new_count);
console.log(response.message); } else {
} console.log("Error:", response.message);
}, }
error: function () { },
console.log("Error processing the request."); error: function (xhr, status, error) {
}, console.log("AJAX Error:", error);
}); },
});
}); });
}); });
\ No newline at end of file
...@@ -406,6 +406,9 @@ function calculate_reading_time($content) ...@@ -406,6 +406,9 @@ function calculate_reading_time($content)
add_action('wp_ajax_increment_download_count', 'increment_download_count'); add_action('wp_ajax_increment_download_count', 'increment_download_count');
add_action('wp_ajax_nopriv_increment_download_count', 'increment_download_count'); add_action('wp_ajax_nopriv_increment_download_count', 'increment_download_count');
if (!session_id()) {
session_start();
}
function increment_download_count() function increment_download_count()
{ {
if (!isset($_POST['resource_id']) || !is_numeric($_POST['resource_id'])) { if (!isset($_POST['resource_id']) || !is_numeric($_POST['resource_id'])) {
......
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