Untitled
webwizards
plain_text
10 months ago
16 kB
11
Indexable
function b2bking_pdf_button_handler_script() {
// Frontend: Account page or WooCommerce pages
if (is_account_page() || (function_exists('is_woocommerce') && is_woocommerce())) {
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
// First, we need to prevent the original handler from running on duplicates
// We'll intercept clicks on elements with data-is-duplicate="true"
$('body').on('click.b2bking_duplicate', '.b2bking_offer_download[data-is-duplicate="true"]', function(e) {
e.preventDefault();
e.stopImmediatePropagation(); // This stops other handlers from running
var clickedDuplicate = $(this);
var offerId = clickedDuplicate.val(); // Get offer ID
var originalButton = clickedDuplicate.prev('.b2bking_offer_download:not([data-is-duplicate="true"])'); // Get the original button
// Disable duplicate button to prevent multiple clicks
clickedDuplicate.prop('disabled', true);
clickedDuplicate.css('opacity', '0.6');
// AJAX request to get expiry date
$.ajax({
url: '<?php echo admin_url('admin-ajax.php'); ?>',
type: 'POST',
data: {
action: 'b2bking_get_offer_expiry_date',
offer_id: offerId
},
success: function(response) {
var expiryDate = response;
var offerContainer = clickedDuplicate.closest('.b2bking_myaccount_individual_offer_container');
if (offerContainer.length) {
var offerTitleElement = offerContainer.find('.b2bking_myaccount_individual_offer_top');
if (offerTitleElement.length) {
var offerTitle = offerTitleElement.text().trim();
if (typeof window.b2bking_display_settings !== 'undefined') {
window.b2bking_display_settings.offer_details = 'Offer Details: ' + offerTitle + "\n" + 'Date d\'expiration: ' + expiryDate;
} else {
window.b2bking_display_settings = {
offer_details: 'Offer Details: ' + offerTitle + "\n" + 'Date d\'expiration: ' + expiryDate
};
}
}
}
// Trigger click on original button after delay
setTimeout(function() {
originalButton.trigger('click');
// Re-enable duplicate button after a delay
setTimeout(function() {
clickedDuplicate.prop('disabled', false);
clickedDuplicate.css('opacity', '1');
}, 1000);
}, 100); // 100ms delay to ensure data is set
},
error: function() {
// Re-enable button on error
clickedDuplicate.prop('disabled', false);
clickedDuplicate.css('opacity', '1');
console.error('Failed to fetch offer expiry date');
}
});
});
// Process each original button
$('.b2bking_offer_download').each(function() {
var originalButton = $(this);
// Skip if already processed
if (originalButton.data('duplicate-created')) {
return;
}
// Mark as processed
originalButton.data('duplicate-created', true);
// Hide the original button
originalButton.hide();
// Create a duplicate button
var duplicateButton = originalButton.clone();
duplicateButton.addClass('b2bking_offer_download_duplicate') // Add duplicate class but keep original
.attr('data-is-duplicate', 'true') // Add data attribute to identify
.removeAttr('id') // Remove ID to avoid duplicates
.show(); // Make sure it's visible
// Insert the duplicate after the original
originalButton.after(duplicateButton);
});
// Handle dynamically added buttons (if any are added after page load)
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
$(mutation.addedNodes).find('.b2bking_offer_download').each(function() {
var originalButton = $(this);
// Skip if already processed
if (originalButton.data('duplicate-created')) {
return;
}
// Mark as processed
originalButton.data('duplicate-created', true);
// Hide the original button
originalButton.hide();
// Create a duplicate button
var duplicateButton = originalButton.clone();
duplicateButton.addClass('b2bking_offer_download_duplicate') // Add duplicate class but keep original
.attr('data-is-duplicate', 'true') // Add data attribute to identify
.removeAttr('id')
.show();
// Insert the duplicate after the original
originalButton.after(duplicateButton);
});
});
});
// Start observing the document with the configured parameters
if (document.body) {
observer.observe(document.body, {
childList: true,
subtree: true
});
}
});
</script>
<?php
}
}
add_action('wp_footer', 'b2bking_pdf_button_handler_script');
// Backend script for admin area
function b2bking_pdf_button_handler_script_admin() {
if (is_admin()) {
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
// Use setTimeout to ensure our code runs after other handlers are registered
setTimeout(function() {
// First, prevent the original handler from running on duplicates
// Use capturing phase to intercept before bubbling handlers
document.addEventListener('click', function(e) {
var target = $(e.target);
// Check if it's our duplicate button
if (target.hasClass('b2bking_download_offer_button') && target.attr('data-is-duplicate') === 'true') {
e.stopImmediatePropagation();
e.preventDefault();
var clickedDuplicate = target;
// Get offer ID - in backend, extract from URL
var offerId = clickedDuplicate.val(); // Try button value first
if (!offerId || offerId === '') {
// Extract from URL if button has no value
var urlParams = new URLSearchParams(window.location.search);
offerId = urlParams.get('post');
}
var originalButton = clickedDuplicate.prev('.b2bking_download_offer_button:not([data-is-duplicate="true"])'); // Get the original button
// Disable duplicate button to prevent multiple clicks
clickedDuplicate.prop('disabled', true);
clickedDuplicate.css('opacity', '0.6');
// AJAX request to get expiry date
$.ajax({
url: '<?php echo admin_url('admin-ajax.php'); ?>',
type: 'POST',
data: {
action: 'b2bking_get_offer_expiry_date',
offer_id: offerId
},
success: function(response) {
var expiryDate = response;
// In backend, we need to find offer details differently
// This might need adjustment based on your backend HTML structure
var offerTitle = 'Offer #' + offerId; // Default title
// Try to find offer title from the page (adjust selector as needed)
var titleElement = clickedDuplicate.closest('tr').find('td:first');
if (!titleElement.length) {
titleElement = clickedDuplicate.closest('.offer-container').find('.offer-title');
}
if (titleElement.length) {
offerTitle = titleElement.text().trim();
}
// Use b2bking object for backend
if (typeof window.b2bking !== 'undefined') {
window.b2bking.offer_details = 'Offer Details: ' + offerTitle + "\n" + 'Date d\'expiration: ' + expiryDate;
} else {
window.b2bking = {
offer_details: 'Offer Details: ' + offerTitle + "\n" + 'Date d\'expiration: ' + expiryDate
};
}
// Trigger click on original button after delay
setTimeout(function() {
originalButton.trigger('click');
// Re-enable duplicate button after a delay
setTimeout(function() {
clickedDuplicate.prop('disabled', false);
clickedDuplicate.css('opacity', '1');
}, 1000);
}, 100); // 100ms delay to ensure data is set
},
error: function() {
// Re-enable button on error
clickedDuplicate.prop('disabled', false);
clickedDuplicate.css('opacity', '1');
console.error('Failed to fetch offer expiry date');
}
});
}
}, true); // true = use capturing phase
// Process each original backend button
$('.b2bking_download_offer_button').each(function() {
var originalButton = $(this);
// Skip if already processed
if (originalButton.data('duplicate-created')) {
return;
}
// Mark as processed
originalButton.data('duplicate-created', true);
// Hide the original button
originalButton.hide();
// Create a duplicate button
var duplicateButton = originalButton.clone();
duplicateButton.addClass('b2bking_download_offer_button_duplicate') // Add duplicate class but keep original
.attr('data-is-duplicate', 'true') // Add data attribute to identify
.removeAttr('id') // Remove ID to avoid duplicates
.show(); // Make sure it's visible
// Insert the duplicate after the original
originalButton.after(duplicateButton);
});
// Handle dynamically added buttons in backend
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
$(mutation.addedNodes).find('.b2bking_download_offer_button').each(function() {
var originalButton = $(this);
// Skip if already processed
if (originalButton.data('duplicate-created')) {
return;
}
// Mark as processed
originalButton.data('duplicate-created', true);
// Hide the original button
originalButton.hide();
// Create a duplicate button
var duplicateButton = originalButton.clone();
duplicateButton.addClass('b2bking_download_offer_button_duplicate')
.attr('data-is-duplicate', 'true')
.removeAttr('id')
.show();
// Insert the duplicate after the original
originalButton.after(duplicateButton);
});
});
});
// Start observing the document
if (document.body) {
observer.observe(document.body, {
childList: true,
subtree: true
});
}
}, 100); // Small delay to ensure other scripts have loaded
});
</script>
<?php
}
}
add_action('admin_footer', 'b2bking_pdf_button_handler_script_admin');
// PHP function for AJAX request
add_action('wp_ajax_b2bking_get_offer_expiry_date', 'b2bking_get_offer_expiry_date_callback');
add_action('wp_ajax_nopriv_b2bking_get_offer_expiry_date', 'b2bking_get_offer_expiry_date_callback');
function b2bking_get_offer_expiry_date_callback() {
$offer_id = isset($_POST['offer_id']) ? intval($_POST['offer_id']) : 0;
if ($offer_id) {
$expiry_date = get_field('offer_expiry_date', $offer_id);
echo $expiry_date;
} else {
echo 'Invalid offer ID';
}
wp_die();
}Editor is loading...
Leave a Comment