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
0423911e
Commit
0423911e
authored
Jul 15, 2025
by
rajshah
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: download count in resource detail
parent
a0bfab52
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
50 additions
and
45 deletions
+50
-45
script.js
assets/js/script.js
+26
-0
functions.php
functions.php
+21
-45
single-resource.php
single-resource.php
+3
-0
No files found.
assets/js/script.js
View file @
0423911e
...
...
@@ -341,3 +341,28 @@
})
})(
jQuery
);
// download count
jQuery
(
document
).
ready
(
function
(
$
)
{
$
(
'.btn-download'
).
on
(
'click'
,
function
()
{
const
resourceId
=
$
(
this
).
data
(
'resource-id'
);
$
.
ajax
({
url
:
ajax_object
.
ajax_url
,
type
:
'POST'
,
data
:
{
action
:
'increment_download_count'
,
resource_id
:
resourceId
,
},
success
:
function
(
response
)
{
if
(
response
.
success
)
{
$
(
'#download-count'
).
text
(
response
.
new_count
);
}
else
{
console
.
log
(
response
.
message
);
}
},
error
:
function
()
{
console
.
log
(
'Error processing the request.'
);
},
});
});
});
\ No newline at end of file
functions.php
View file @
0423911e
...
...
@@ -404,47 +404,23 @@ function calculate_reading_time($content)
}
// !article read time
//
//
//
//
// Define the CF7 API endpoint and form ID
// $form_id = '504'; // Replace with your actual form ID
// $api_url = 'http://localhost/makura/wp-json/contact-form-7/v1/contact-forms/' . $form_id . '/feedback';
// // Prepare data to send
// $data = array(
// 'first_name' => 'John', // Correspond to CF7 field names
// 'last_name' => 'Doe', // Correspond to CF7 field names
// 'email' => 'john@example.com',
// );
// // WordPress user credentials (use your admin username and the application password)
// $username = 'makura_user'; // Replace with your WordPress username
// $app_password = 'makura_backend@2024'; // Replace with your generated application password
// // Initialize cURL session
// $ch = curl_init($api_url);
// // Set cURL options
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// curl_setopt($ch, CURLOPT_POST, true);
// curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
// curl_setopt($ch, CURLOPT_HTTPHEADER, array(
// 'Authorization: Basic ' . base64_encode($username . ':' . $app_password)
// ));
// // Execute the cURL request and get the response
// $response = curl_exec($ch);
// // Check for cURL errors
// if ($response === false) {
// echo 'Error: ' . curl_error($ch);
// } else {
// echo 'Response: ' . $response;
// }
// // Close cURL session
//
curl_close
(
$ch
);
\ No newline at end of file
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
()
{
if
(
!
isset
(
$_POST
[
'resource_id'
])
||
!
is_numeric
(
$_POST
[
'resource_id'
]))
{
wp_send_json_error
([
'message'
=>
'Invalid resource ID'
]);
}
$resource_id
=
intval
(
$_POST
[
'resource_id'
]);
if
(
!
isset
(
$_SESSION
))
{
session_start
();
}
if
(
isset
(
$_SESSION
[
'downloaded_resources'
])
&&
in_array
(
$resource_id
,
$_SESSION
[
'downloaded_resources'
]))
{
wp_send_json_success
([
'message'
=>
'Already counted'
]);
}
$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
);
$_SESSION
[
'downloaded_resources'
][]
=
$resource_id
;
wp_send_json_success
([
'new_count'
=>
$new_count
]);
}
\ No newline at end of file
single-resource.php
View file @
0423911e
<?php
// Single Resources
get_header
();
$download_count
=
(
int
)
get_post_meta
(
get_the_ID
(),
'download_count'
,
true
);
$post_thumbnail_url
=
get_the_post_thumbnail_url
(
get_the_ID
(),
'full'
);
if
(
!
$post_thumbnail_url
)
{
$post_thumbnail_url
=
get_field
(
'resources_fallback_featured_image'
,
'option'
);
...
...
@@ -69,6 +70,8 @@ endif;
d=
"M4 13L16.17 13L10.58 18.59L12 20L20 12L12 4L10.59 5.41L16.17 11L4 11L4 13Z"
fill=
"white"
></path>
</svg></a>
<p>
Total Downloads:
<span
id=
"download-count"
>
<?php
echo
$download_count
;
?>
</span></p>
</div>
</div>
<div
class=
"col-lg-5 col-md-5 mx-auto col-12 text-center right-image"
>
...
...
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