Added delete feature to post importer
This commit is contained in:
@ -1,33 +1,88 @@
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
// Check if the button exists before adding the event listener
|
||||
const importButton = document.getElementById('import-demo-posts');
|
||||
if (importButton) {
|
||||
importButton.addEventListener('click', function () {
|
||||
const formData = new FormData();
|
||||
|
||||
// Append action and nonce to the form data
|
||||
formData.append('action', 'lcp_import_demo_posts');
|
||||
formData.append('lcp_import_nonce', lcp_ajax_obj.nonce); // Add the nonce passed by wp_localize_script
|
||||
console.log("I Tried"); // This will log the button element to the console
|
||||
|
||||
// Send the AJAX request
|
||||
fetch(lcp_ajax_obj.ajax_url, {
|
||||
method: 'POST',
|
||||
body: formData
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (data.success) {
|
||||
alert(data.data); // Success message
|
||||
} else {
|
||||
alert('Error: ' + (data.data || 'Unknown error')); // Error message
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error:', error);
|
||||
alert('An error occurred while processing your request.');
|
||||
// Check if the 'Import Demo Posts' button exists
|
||||
const importButton = document.getElementById('import-demo-posts');
|
||||
console.log(importButton); // This will log the button element to the console
|
||||
|
||||
if (importButton) {
|
||||
// Check if the lcp_demo_posts option exists
|
||||
if (lcp_ajax_obj.lcp_demo_posts && lcp_ajax_obj.lcp_demo_posts.post_ids && lcp_ajax_obj.lcp_demo_posts.post_ids.length > 0) {
|
||||
// Disable the 'Import Demo Posts' button if demo posts have already been imported
|
||||
importButton.disabled = true;
|
||||
importButton.textContent = 'Demo Posts Already Imported'; // Optional: Change button text
|
||||
} else {
|
||||
// Add event listener to import demo posts if the button is enabled
|
||||
importButton.addEventListener('click', function () {
|
||||
console.log("Clicked"); // This should be triggered on button click
|
||||
const formData = new FormData();
|
||||
|
||||
// Append action and nonce to the form data
|
||||
formData.append('action', 'lcp_import_demo_posts');
|
||||
formData.append('lcp_import_nonce', lcp_ajax_obj.nonce);
|
||||
|
||||
// Send the AJAX request to import demo posts
|
||||
fetch(lcp_ajax_obj.ajax_url, {
|
||||
method: 'POST',
|
||||
body: formData
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (data.success) {
|
||||
alert(data.data); // Success message
|
||||
importButton.disabled = true;
|
||||
importButton.textContent = 'Demo Posts Imported'; // Optional: Change button text
|
||||
} else {
|
||||
alert('Error: ' + (data.data || 'Unknown error')); // Error message
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error:', error);
|
||||
alert('An error occurred while processing your request.');
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
} else {
|
||||
console.warn('Import button not found.');
|
||||
}
|
||||
|
||||
// Check if the 'Delete Demo Posts' button exists
|
||||
const deleteButton = document.getElementById('delete-demo-posts');
|
||||
if (deleteButton) {
|
||||
deleteButton.addEventListener('click', function () {
|
||||
const formData = new FormData();
|
||||
|
||||
// Append action and nonce to the form data for deletion
|
||||
formData.append('action', 'lcp_delete_demo_posts'); // Use the correct action here
|
||||
formData.append('lcp_import_nonce', lcp_ajax_obj.nonce); // Pass nonce for security
|
||||
|
||||
// Send the AJAX request to delete demo posts
|
||||
fetch(lcp_ajax_obj.ajax_url, {
|
||||
method: 'POST',
|
||||
body: formData
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (data.success) {
|
||||
alert(data.data); // Success message
|
||||
deleteButton.disabled = true;
|
||||
deleteButton.textContent = 'Demo Posts Deleted';
|
||||
|
||||
// Re-enable the "Import Demo Posts" button and reset its text
|
||||
const importButton = document.getElementById('import-demo-posts');
|
||||
if (importButton) {
|
||||
importButton.disabled = false;
|
||||
importButton.textContent = 'Import Demo Posts';
|
||||
}
|
||||
} else {
|
||||
alert('Error: ' + (data.data || 'Unknown error'));
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error:', error);
|
||||
alert('An error occurred while processing your request.');
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user