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
a7b7c6a8
Commit
a7b7c6a8
authored
Jul 16, 2025
by
rajshah
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: download resource count fix v1.4
parent
fd02c47d
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
45 additions
and
35 deletions
+45
-35
script.js
assets/js/script.js
+24
-23
functions.php
functions.php
+15
-7
enqueuer.php
includes/enqueuer.php
+6
-5
No files found.
assets/js/script.js
View file @
a7b7c6a8
...
@@ -343,28 +343,28 @@
...
@@ -343,28 +343,28 @@
})(
jQuery
);
})(
jQuery
);
// download count
// download count
jQuery
(
document
).
ready
(
function
(
$
)
{
jQuery
(
document
).
ready
(
function
(
$
)
{
$
(
"
#btn-resource
-download"
).
on
(
"click"
,
function
()
{
$
(
"
.btn
-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
.
ajax_
url
,
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
.
log
(
"Error:"
,
response
.
message
);
console
.
error
(
"Error:"
,
response
.
message
);
}
}
},
},
error
:
function
(
xhr
,
status
,
error
)
{
error
:
function
(
xhr
,
status
,
error
)
{
console
.
log
(
"AJAX Error:"
,
error
);
console
.
error
(
"AJAX Error:"
,
error
);
},
},
});
});
});
});
});
});
\ No newline at end of file
functions.php
View file @
a7b7c6a8
...
@@ -403,27 +403,34 @@ function calculate_reading_time($content)
...
@@ -403,27 +403,34 @@ function calculate_reading_time($content)
return
$reading_time
;
return
$reading_time
;
}
}
// !article read time
// !article read time
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
()
{
}
// Verify the AJAX request
function
increment_download_count
()
{
if
(
!
isset
(
$_POST
[
'resource_id'
])
||
!
is_numeric
(
$_POST
[
'resource_id'
]))
{
if
(
!
isset
(
$_POST
[
'resource_id'
])
||
!
is_numeric
(
$_POST
[
'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'
]);
if
(
!
isset
(
$_SESSION
))
{
// Start session if not already started
if
(
!
session_id
())
{
session_start
();
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'
]))
{
if
(
isset
(
$_SESSION
[
'downloaded_resources'
])
&&
in_array
(
$resource_id
,
$_SESSION
[
'downloaded_resources'
]))
{
wp_send_json_success
([
'message'
=>
'Already counted'
]);
wp_send_json_success
([
'message'
=>
'Already counted'
]);
}
}
// 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
);
$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
);
// Store the resource ID in the session to prevent multiple counts
$_SESSION
[
'downloaded_resources'
][]
=
$resource_id
;
$_SESSION
[
'downloaded_resources'
][]
=
$resource_id
;
wp_send_json_success
([
'new_count'
=>
$new_count
]);
wp_send_json_success
([
'new_count'
=>
$new_count
]);
}
}
\ No newline at end of file
includes/enqueuer.php
View file @
a7b7c6a8
...
@@ -46,11 +46,11 @@ function conserve_register_styles()
...
@@ -46,11 +46,11 @@ function conserve_register_styles()
/** CUSTOM JS */
/** CUSTOM JS */
wp_enqueue_script
(
'custom-JS'
,
get_template_directory_uri
()
.
'/assets/js/script.js'
,
array
(
'jquery'
),
null
,
true
);
wp_enqueue_script
(
'custom-JS'
,
get_template_directory_uri
()
.
'/assets/js/script.js'
,
array
(
'jquery'
),
null
,
true
);
wp_localize_script
(
wp_localize_script
(
'custom-JS'
,
'custom-JS'
,
'download_resource_object'
,
'download_resource_object'
,
array
(
array
(
'ajaxurl'
=>
admin_url
(
'admin-ajax.php'
),
'ajaxurl'
=>
admin_url
(
'admin-ajax.php'
),
)
)
);
);
}
}
add_action
(
'wp_enqueue_scripts'
,
'conserve_register_styles'
);
add_action
(
'wp_enqueue_scripts'
,
'conserve_register_styles'
);
\ 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