Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
makura-2025
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Raj Shah
makura-2025
Commits
cb2d17c5
Commit
cb2d17c5
authored
Jul 16, 2025
by
rajshah
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: download resource count fix v1.6
parent
3f0f54d5
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
4 additions
and
12 deletions
+4
-12
script.js
assets/js/script.js
+4
-4
functions.php
functions.php
+0
-8
No files found.
assets/js/script.js
View file @
cb2d17c5
...
...
@@ -343,12 +343,12 @@
})(
jQuery
);
// download count
jQuery
(
document
).
ready
(
function
(
$
)
{
$
(
"
.btn
-download"
).
on
(
"click"
,
function
()
{
$
(
"
#btn-resource
-download"
).
on
(
"click"
,
function
()
{
const
resourceId
=
$
(
this
).
data
(
"resource-id"
);
console
.
log
(
"Resource ID:"
,
resourceId
);
$
.
ajax
({
url
:
download_resource_object
.
ajax
url
,
url
:
download_resource_object
.
ajax_
url
,
type
:
"POST"
,
data
:
{
action
:
"increment_download_count"
,
...
...
@@ -359,11 +359,11 @@ jQuery(document).ready(function ($) {
if
(
response
.
success
)
{
$
(
"#download-count"
).
text
(
response
.
new_count
);
}
else
{
console
.
error
(
"Error:"
,
response
.
message
);
console
.
log
(
"Error:"
,
response
.
message
);
}
},
error
:
function
(
xhr
,
status
,
error
)
{
console
.
error
(
"AJAX Error:"
,
error
);
console
.
log
(
"AJAX Error:"
,
error
);
},
});
});
...
...
functions.php
View file @
cb2d17c5
...
...
@@ -410,35 +410,28 @@ function increment_download_count()
{
// Verify the AJAX request
if
(
!
isset
(
$_POST
[
'resource_id'
])
||
!
is_numeric
(
$_POST
[
'resource_id'
]))
{
error_log
(
'Invalid resource ID'
);
wp_send_json_error
([
'message'
=>
'Invalid resource ID'
]);
}
$resource_id
=
intval
(
$_POST
[
'resource_id'
]);
error_log
(
'Resource ID: '
.
$resource_id
);
// Start session if not already started
if
(
!
session_id
())
{
session_start
();
error_log
(
'Session started'
);
}
// Check if the user has already clicked the button in this session
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'
]);
}
// Increment the download count
$current_count
=
(
int
)
get_post_meta
(
$resource_id
,
'download_count'
,
true
);
error_log
(
'Current count: '
.
$current_count
);
$new_count
=
$current_count
+
1
;
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
$_SESSION
[
'downloaded_resources'
][]
=
$resource_id
;
error_log
(
'Resource added to session: '
.
$resource_id
);
wp_send_json_success
([
'new_count'
=>
$new_count
]);
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment