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.');
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
"excerpt": "Faketown residents have been experiencing unpredictable weather patterns, baffling local meteorologists.",
|
||||
"category": "Local News",
|
||||
"tags": ["weather", "Faketown", "mystery"],
|
||||
"date": "2025-01-01",
|
||||
"date": "2024-01-01",
|
||||
"status": "publish",
|
||||
"thumbnail": "demo-post-thumbnail-1.jpg"
|
||||
},
|
||||
@ -16,7 +16,7 @@
|
||||
"excerpt": "Faketown’s mayor unveils a new green initiative to reduce carbon emissions by 40%.",
|
||||
"category": "Politics",
|
||||
"tags": ["green", "climate change", "Faketown"],
|
||||
"date": "2025-01-02",
|
||||
"date": "2024-01-02",
|
||||
"status": "publish",
|
||||
"thumbnail": "demo-post-thumbnail-2.jpg"
|
||||
},
|
||||
@ -26,7 +26,7 @@
|
||||
"excerpt": "Faketown's new restaurant is offering a fusion of global flavors with a local twist.",
|
||||
"category": "Food & Drink",
|
||||
"tags": ["restaurant", "food", "Faketown"],
|
||||
"date": "2025-01-03",
|
||||
"date": "2024-01-03",
|
||||
"status": "publish",
|
||||
"thumbnail": "demo-post-thumbnail-3.jpg"
|
||||
},
|
||||
@ -36,7 +36,7 @@
|
||||
"excerpt": "Faketown residents report strange creatures spotted in nearby forests, baffling experts.",
|
||||
"category": "Strange Happenings",
|
||||
"tags": ["mystery", "Faketown", "creatures"],
|
||||
"date": "2025-01-04",
|
||||
"date": "2024-01-04",
|
||||
"status": "publish",
|
||||
"thumbnail": "demo-post-thumbnail-1.jpg"
|
||||
|
||||
@ -47,7 +47,7 @@
|
||||
"excerpt": "Faketown’s annual festival saw record-breaking attendance, with thousands of people participating.",
|
||||
"category": "Community Events",
|
||||
"tags": ["festival", "community", "Faketown"],
|
||||
"date": "2025-01-05",
|
||||
"date": "2024-01-05",
|
||||
"status": "publish",
|
||||
"thumbnail": "demo-post-thumbnail-2.jpg"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user