Commit 0423911e by rajshah

feat: download count in resource detail

parent a0bfab52
...@@ -341,3 +341,28 @@ ...@@ -341,3 +341,28 @@
}) })
})(jQuery); })(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
...@@ -404,47 +404,23 @@ function calculate_reading_time($content) ...@@ -404,47 +404,23 @@ function calculate_reading_time($content)
} }
// !article read time // !article read time
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']);
// Define the CF7 API endpoint and form ID }
// $form_id = '504'; // Replace with your actual form ID $resource_id = intval($_POST['resource_id']);
// $api_url = 'http://localhost/makura/wp-json/contact-form-7/v1/contact-forms/' . $form_id . '/feedback'; if (!isset($_SESSION)) {
session_start();
// // Prepare data to send }
// $data = array( if (isset($_SESSION['downloaded_resources']) && in_array($resource_id, $_SESSION['downloaded_resources'])) {
// 'first_name' => 'John', // Correspond to CF7 field names wp_send_json_success(['message' => 'Already counted']);
// 'last_name' => 'Doe', // Correspond to CF7 field names }
// 'email' => 'john@example.com', $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);
// // WordPress user credentials (use your admin username and the application password) $_SESSION['downloaded_resources'][] = $resource_id;
// $username = 'makura_user'; // Replace with your WordPress username wp_send_json_success(['new_count' => $new_count]);
// $app_password = 'makura_backend@2024'; // Replace with your generated application password }
\ No newline at end of file
// // 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
<?php <?php
// Single Resources // Single Resources
get_header(); 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'); $post_thumbnail_url = get_the_post_thumbnail_url(get_the_ID(), 'full');
if (!$post_thumbnail_url) { if (!$post_thumbnail_url) {
$post_thumbnail_url = get_field('resources_fallback_featured_image', 'option'); $post_thumbnail_url = get_field('resources_fallback_featured_image', 'option');
...@@ -69,6 +70,8 @@ endif; ...@@ -69,6 +70,8 @@ endif;
d="M4 13L16.17 13L10.58 18.59L12 20L20 12L12 4L10.59 5.41L16.17 11L4 11L4 13Z" d="M4 13L16.17 13L10.58 18.59L12 20L20 12L12 4L10.59 5.41L16.17 11L4 11L4 13Z"
fill="white"></path> fill="white"></path>
</svg></a> </svg></a>
<p>Total Downloads: <span id="download-count"><?php echo $download_count; ?></span></p>
</div> </div>
</div> </div>
<div class="col-lg-5 col-md-5 mx-auto col-12 text-center right-image"> <div class="col-lg-5 col-md-5 mx-auto col-12 text-center right-image">
......
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