Skip to content

Commit

Permalink
- Added AJAX call for clear data
Browse files Browse the repository at this point in the history
- New file pdf-generator-ajax-script.js added
- Registered and Enqueued Script
- Moved Flush button out of the form
- Updated cleardata() function for ajax call
  • Loading branch information
Li8ning committed Feb 5, 2023
1 parent 8ca8413 commit c88d4f0
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 13 deletions.
14 changes: 8 additions & 6 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ function generate_certificate($cert_data,$input_data){
$pdf->SetFont('Helvetica', '', $general_font_size);
// Event Date
$pdf->Cell(0, 8, $value[2], 0, 2, 'C', 0, '', 0);
if (!file_exists($path.'/certificate/')) {
mkdir($path.'/certificate/', 0777, true);
}
$pdf_file_path = $path.'/certificate/'.$value[0].'-'.date('ymdHis').'.pdf';
$pdf_file_name = basename($pdf_file_path);
$pdf->Output($pdf_file_path,'F');
Expand All @@ -106,7 +109,6 @@ function generate_certificate($cert_data,$input_data){
$input_data['email_body'] = str_replace("{{variable2}}", $value[1], $input_data['email_body']);
$input_data['email_body'] = str_replace("{{variable3}}", $value[2], $input_data['email_body']);
$msg = htmlspecialchars_decode($input_data['email_body']);
// print_r($input_data['email_body']);
}
else{
// Default MSG
Expand Down Expand Up @@ -154,25 +156,25 @@ function send_mail($to,$sub,$msg,$headers,$attachment){
return wp_mail($to,$sub,$msg,$headers,$attachment);
}

// Clear Data
// Clear Data function. This will be called via AJAX call
function cleardata(){
echo "Clearing Data<br>";
echo "Clearing Data of certificates and generated PDFs";
$path = dirname(__FILE__);
// Clearing Certificate data
echo "Clearing Cert<br>";
$files = glob($path.'/data/*'); // get all file names
foreach($files as $file){ // iterate files
if(is_file($file)) {
unlink($file); // delete file
}
}
// Clearing PDFs
echo "Clearing PDFs<br>";
$files = glob($path.'/certificate/*'); // get all file names
foreach($files as $file){ // iterate files
if(is_file($file)) {
unlink($file); // delete file
}
}
wp_die();
}
// add_action('clear_all_data','read_data','',2);
add_action('wp_ajax_cleardata', 'cleardata');
add_action('wp_ajax_nopriv_cleardata', 'cleardata');
10 changes: 5 additions & 5 deletions generate.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

$content = '
<style>
.upload-wrapper,.delete_data{margin-bottom:10px;}
.upload-wrapper,#delete_data_btn{margin-bottom:10px;}
fieldset{
display: block;
margin-inline-start: 2px;
Expand Down Expand Up @@ -32,8 +32,8 @@
}
</style>
<h1>Generate PDF</h1>
<button name="clearData" id="delete_data_btn">Flush the PDF Generator\'s Cache</button>
<form method="POST" action="" enctype="multipart/form-data">
<button type="submit" name="clearData" value="Delete" class="delete_data">Flush the PDF Generator\'s Cache</button>
<p>Required fields are followed by <strong><abbr title="required">*</abbr></strong></p>
<fieldset>
<legend>Sender\'s Configurations</legend>
Expand Down Expand Up @@ -97,6 +97,9 @@
$allowedfileExtensions = array('csv');
if (in_array($fileExtension, $allowedfileExtensions)) {
// directory in which the uploaded file will be moved
if (!file_exists(plugin_dir_path(__FILE__).'data/')) {
mkdir(plugin_dir_path(__FILE__).'data/', 0777, true);
}
$uploadFileDir = plugin_dir_path(__FILE__).'data/';
$dest_path = $uploadFileDir . $newFileName;

Expand Down Expand Up @@ -124,7 +127,4 @@
exit();
}
}
}
elseif(isset($_POST['clearData']) && $_POST['clearData'] == 'Delete'){
cleardata();
}
17 changes: 17 additions & 0 deletions pdf-generator-ajax-script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
jQuery(document).ready(function($) {
// Button click event to flush out generated data
$('#delete_data_btn').click(function() {
console.log('Clicked');
$.ajax({
type: 'GET',
url: pdf_generator_ajax_params.ajax_url,
data: {
'action': 'cleardata'
},
success: function(response) {
console.log(response);
}
});
});
});

14 changes: 12 additions & 2 deletions pdf-generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
Description: Generate PDF from CSV files
Version: 1.2
Version: 1.2.1
Author: Speed
Expand All @@ -35,4 +35,14 @@ function pdf_generator_function(){
require($pluginDirPath.'generate.php');
}
$pluginDirPath = plugin_dir_path(__FILE__);
require($pluginDirPath.'functions.php');
require($pluginDirPath.'functions.php');

// Hook to register AJAX action that will call functions from functions.php file
function pdf_generator_register_ajax_action() {
wp_register_script('pdf-generator-ajax-script', plugins_url('/pdf-generator-ajax-script.js', __FILE__), array('jquery'));
wp_localize_script('pdf-generator-ajax-script', 'pdf_generator_ajax_params', array(
'ajax_url' => admin_url('admin-ajax.php')
));
wp_enqueue_script('pdf-generator-ajax-script');
}
add_action('admin_enqueue_scripts', 'pdf_generator_register_ajax_action');

0 comments on commit c88d4f0

Please sign in to comment.