| Server IP : 13.126.101.145 / Your IP : 216.73.217.37 Web Server : Apache/2.4.52 (Ubuntu) System : Linux ip-11-115-0-196 6.8.0-1039-aws #41~22.04.1-Ubuntu SMP Thu Sep 11 10:54:48 UTC 2025 x86_64 User : www-data ( 33) PHP Version : 8.3.17 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /var/www/html/rentals_updated/wp-content/plugins/agreement/ |
Upload File : |
<?php
// Ensure this file is being called by WordPress
if (!defined('ABSPATH')) {
exit;
}
// Function to add the custom endpoint for My Agreements
function custom_add_agreements_endpoint() {
// Register the endpoint with a unique slug
add_rewrite_endpoint('my-agreements', EP_ROOT | EP_PAGES);
}
add_action('init', 'custom_add_agreements_endpoint');
// Add My Agreements to the WooCommerce My Account menu in the middle
function custom_add_agreements_link_my_account($items) {
// Define where you want to insert the My Agreements tab
$new_items = array();
// Insert it after a specific item. For example, after "orders"
foreach ($items as $key => $value) {
$new_items[$key] = $value; // Add existing items
if ($key === 'orders') { // After the 'orders' tab
$new_items['my-agreements'] = 'Agreements'; // Add new 'My Agreements' tab here
}
}
return $new_items;
}
add_filter('woocommerce_account_menu_items', 'custom_add_agreements_link_my_account');
// Show content when the user clicks on "My Agreements"
function custom_my_agreements_content() {
// Get the current logged-in user
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
// Handle PDF upload if a form is submitted
if (isset($_POST['upload_pdf']) && !empty($_FILES['pdf_file']['name'])) {
custom_handle_pdf_upload($user_id);
}
// Fetch agreements for the logged-in user by joining wp_agreement and wp_quotation tables
global $wpdb;
// Fetch agreements using a join between wp_agreement and wp_quotation
$agreements = $wpdb->get_results($wpdb->prepare(
"SELECT ag.*, qt.product_name, qt.start_date, qt.end_date, qt.final_price, qt.user_id
FROM wp_agreement ag
INNER JOIN wp_quotation qt ON ag.qt_id = qt.id
WHERE qt.user_id = %d
ORDER BY ag.ag_created_at DESC", $user_id
));
?>
<h3>My Agreements</h3>
<?php if ($agreements) { ?>
<table id="agreementTable" class="wp-list-table widefat fixed striped table-view-list">
<thead>
<tr>
<th>SN</th>
<th>Product Name</th>
<th>Start Date</th>
<th>End Date</th>
<th>Final Price</th>
<!-- <th>Status</th> -->
<th>Created At</th>
<th>View/Download PDF</th>
<th>Upload Signed PDF</th>
</tr>
</thead>
<tbody>
<?php foreach ($agreements as $key => $agreement) { ?>
<tr>
<td><?php echo esc_html($key + 1); ?></td>
<td><?php echo esc_html($agreement->product_name); ?></td>
<td><?php echo esc_html($agreement->start_date); ?></td>
<td><?php echo esc_html($agreement->end_date); ?></td>
<td><?php echo esc_html($agreement->final_price); ?></td>
<!-- <td><?php echo esc_html($agreement->status); ?></td> -->
<td><?php echo esc_html($agreement->ag_created_at); ?></td>
<!-- View/Download Original PDF -->
<td style="text-align:center;">
<?php if (!empty($agreement->upload_pdf)) { ?>
<a href="<?php echo esc_url($agreement->upload_pdf); ?>" target="_blank" class="button button-secondary"><img src="/wp-content/uploads/2025/09/download-pdf.png" alt="download-pdf"/></a>
<?php } else { ?>
<span>No PDF Available</span>
<?php } ?>
</td>
<td style="padding: 8px 30px 8px 15px;">
<?php if (!isset($agreement->upload_pdf_cus) ) { ?>
<form method="post" enctype="multipart/form-data">
<input type="hidden" name="agreement_id" value="<?php echo esc_attr($agreement->agreement_id); ?>">
<!-- Hidden File Input -->
<input type="file" id="pdf_file" name="pdf_file" accept="application/pdf" required style="display:none;">
<!-- Image Icon as Trigger -->
<img src="/wp-content/uploads/2025/09/upload-pdf.png"
alt="Upload PDF"
id="uploadIcon">
<input type="submit" name="upload_pdf" class="button button-primary upload-signed-pdf" value="Upload">
</form>
<script>
document.getElementById('uploadIcon').addEventListener('click', function () {
document.getElementById('pdf_file').click();
});
</script>
<?php } else {
echo"<p>Uploaded</p>";
}
?>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<?php } else { ?>
<p>No agreements found.</p>
<?php }
}
// Handle PDF upload and update in wp_agreement table
function custom_handle_pdf_upload($user_id) {
global $wpdb;
// Check if agreement ID is present and file is uploaded
if (!isset($_POST['agreement_id']) || empty($_FILES['pdf_file']['name'])) {
return;
}
// Set up the upload directory for PDFs
$upload_dir = wp_upload_dir();
$pdf_upload_dir = $upload_dir['basedir'] . '/pdfuploads-cus/';
// Create the directory if it doesn't exist
if (!file_exists($pdf_upload_dir)) {
wp_mkdir_p($pdf_upload_dir);
}
// Get the file info
$file_name = basename($_FILES['pdf_file']['name']);
$target_file = $pdf_upload_dir . $file_name;
$file_type = wp_check_filetype($file_name);
// Check if the uploaded file is a PDF
if ($file_type['ext'] !== 'pdf') {
echo "<p>Error: Only PDF files are allowed.</p>";
return;
}
// Move the uploaded file to the target directory
if (move_uploaded_file($_FILES['pdf_file']['tmp_name'], $target_file)) {
$file_url = $upload_dir['baseurl'] . '/pdfuploads-cus/' . $file_name;
// Update the wp_agreement table with the PDF URL
$agreement_id = intval($_POST['agreement_id']);
$wpdb->update(
'wp_agreement',
array('upload_pdf_cus' => $file_url),
array('agreement_id' => $agreement_id),
array('%s'),
array('%d')
);
// In App Notification
$first_name = get_user_meta($user_id, 'first_name', true);
$email_id = get_userdata($user_id)->user_email;
create_notification(
'User uploaded signed PDF',
'User "' . $first_name .'" uploaded signed PDF. Email id "' . $email_id .'" ',
array(
'meta_input' => array(
'user_id' => $user_id,
'action' => 'User submited offer',
)
)
);
echo "<p>PDF uploaded successfully!</p>";
} else {
echo "<p>Error uploading PDF.</p>";
}
}
add_action('woocommerce_account_my-agreements_endpoint', 'custom_my_agreements_content');