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
f62d9cb5
Commit
f62d9cb5
authored
Jul 16, 2025
by
rajshah
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: download resource count fix v1.9
parent
9b8b6ef4
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
59 additions
and
31 deletions
+59
-31
script.js
assets/js/script.js
+30
-23
functions.php
functions.php
+28
-8
enqueuer.php
includes/enqueuer.php
+1
-0
No files found.
assets/js/script.js
View file @
f62d9cb5
...
...
@@ -343,28 +343,34 @@
})(
jQuery
);
// download count
jQuery
(
document
).
ready
(
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
,
type
:
"POST"
,
data
:
{
action
:
"increment_download_count"
,
resource_id
:
resourceId
,
},
success
:
function
(
response
)
{
console
.
log
(
"AJAX Response:"
,
response
);
if
(
response
.
success
)
{
$
(
"#download-count"
).
text
(
response
.
data
.
new_count
);
}
else
{
console
.
log
(
"Error:"
,
response
.
data
.
message
);
}
},
error
:
function
(
xhr
,
status
,
error
)
{
console
.
log
(
"AJAX Error:"
,
error
);
},
});
$
(
".btn-resource-download"
).
on
(
"click"
,
function
(
e
)
{
e
.
preventDefault
();
// Prevent default download
const
resourceId
=
$
(
this
).
data
(
"resource-id"
);
const
downloadUrl
=
$
(
this
).
attr
(
"href"
);
console
.
log
(
"Resource ID:"
,
resourceId
);
$
.
ajax
({
url
:
download_resource_object
.
ajax_url
,
type
:
"POST"
,
data
:
{
action
:
"increment_download_count"
,
resource_id
:
resourceId
,
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
}
else
{
console
.
log
(
"Error:"
,
response
.
data
?
response
.
data
.
message
:
"Invalid response"
);
window
.
location
.
href
=
downloadUrl
;
// Trigger download even on error
}
},
error
:
function
(
xhr
,
status
,
error
)
{
console
.
log
(
"AJAX Error:"
,
error
);
window
.
location
.
href
=
downloadUrl
;
// Trigger download on AJAX failure
},
});
});
});
\ No newline at end of file
functions.php
View file @
f62d9cb5
...
...
@@ -406,10 +406,19 @@ function calculate_reading_time($content)
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
()
{
// Verify the AJAX request
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'
]);
}
...
...
@@ -420,18 +429,28 @@ function increment_download_count()
session_start
();
}
// Check if the user has already clicked the button in this session
if
(
isset
(
$_SESSION
[
'downloaded_resources'
])
&&
in_array
(
$resource_id
,
$_SESSION
[
'downloaded_resources'
]))
{
wp_send_json_success
([
'message'
=>
'Already counted'
]);
// 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
([
'message'
=>
'Already counted'
,
'new_count'
=>
(
int
)
get_post_meta
(
$resource_id
,
'download_count'
,
true
)
]);
}
// Increment
the
download count
// 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
the resource ID in the session to prevent multiple counts
// 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
includes/enqueuer.php
View file @
f62d9cb5
...
...
@@ -50,6 +50,7 @@ function conserve_register_styles()
'download_resource_object'
,
array
(
'ajaxurl'
=>
admin_url
(
'admin-ajax.php'
),
'nonce'
=>
wp_create_nonce
(
'increment_download_count_nonce'
),
)
);
}
...
...
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