Added delete feature to post importer

This commit is contained in:
Jeremy Rangel
2025-01-02 21:31:25 -08:00
parent 4447e50bcf
commit 3a205a53cf
6 changed files with 176 additions and 93 deletions

View File

@ -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.');
});
});
}
});

View File

@ -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": "Faketowns 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": "Faketowns 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"

View File

@ -45,23 +45,6 @@ add_action('wp_enqueue_scripts', 'lcp_enqueue');
function lcp_backend_enqueue() {
// Enqueue the theme's main stylesheet (style.css)
wp_enqueue_style('lcp-style', get_stylesheet_uri());
wp_enqueue_script('lcp-script', get_template_directory_uri() . '/script.js');
wp_enqueue_script('lcp-ui', get_template_directory_uri() . '/assets/js/lcp-ui.js');
// Enqueue custom script to handle the Demo post import AJAX request
wp_enqueue_script('lcp-import-demo-posts-ajax', get_template_directory_uri() . '/assets/js/demo-posts-import.js', array(), null, true);
// Add the AJAX URL and nonce as JavaScript variables
wp_localize_script('lcp-import-demo-posts-ajax', 'lcp_ajax_obj', array(
'ajax_url' => admin_url('admin-ajax.php'), // This is the URL that well send the AJAX request to
'nonce' => wp_create_nonce('lcp_import_demo_posts_nonce') // Security nonce for validation
));
}
add_action('admin_enqueue_scripts', 'lcp_backend_enqueue');
/* KEY POINTS */
@ -418,25 +401,6 @@ function lcp_create_icons_table() {
}
function lcp_enqueue_dashboard_scripts($hook) {
// Only load the script on the icon-management page
// Enqueue JavaScript for AJAX
wp_enqueue_script(
'icon-import-script',
get_template_directory_uri() . '/assets/js/icon-import.js',
array(), // No dependencies for vanilla JS
null,
true
);
// Pass the AJAX URL to the script
wp_localize_script('icon-import-script', 'lcp_ajax', array(
'ajax_url' => admin_url('admin-ajax.php')
));
}
add_action('admin_enqueue_scripts', 'lcp_enqueue_dashboard_scripts');
// Handle the AJAX request for uninstalling icon sets
@ -617,3 +581,23 @@ function lcp_import_demo_posts_ajax_handler() {
wp_send_json_success('Demo posts imported successfully.');
}
add_action('wp_ajax_lcp_delete_demo_posts', 'lcp_delete_demo_posts_ajax_handler');
// Define the function to handle the delete demo posts AJAX request
function lcp_delete_demo_posts_ajax_handler() {
// Verify nonce for security
if (!isset($_POST['lcp_import_nonce']) || !wp_verify_nonce($_POST['lcp_import_nonce'], 'lcp_import_demo_posts_nonce')) {
wp_send_json_error('Invalid nonce');
}
// Check if the current user has permissions to manage options
if (!current_user_can('manage_options')) {
wp_send_json_error('You do not have sufficient permissions to perform this action.');
}
// Call the function that deletes the demo posts
lcp_delete_demo_posts();
// Send a success response back to the browser
wp_send_json_success('Demo posts deleted successfully.');
}

View File

@ -307,7 +307,22 @@ function render_lcp_theme_settings_page() {
<h2>Miscellaneous Settings</h2>
<!-- Button to trigger the AJAX import demo posts -->
<button id="import-demo-posts" class="button button-primary">Import Demo Posts</button>
<h3> Demo Posts </h3>
<?php
// Get the 'lcp_demo_posts' option from the database
$demo_posts_data = get_option('lcp_demo_posts', false);
// Check if the lcp_demo_posts option exists
if ($demo_posts_data) {
echo '<span>' . count($demo_posts_data['post_ids']) . ' Demo posts have been imported.</span>';
// If demo posts have been imported, display the "Delete Demo Posts" button (this can be disabled or hidden based on your logic)
echo '<button class="lcp" id="delete-demo-posts" class="button">Delete Demo Posts</button>';
} else {
// If demo posts have NOT been imported, display the "Import Demo Posts" button
echo '<button class="lcp" id="import-demo-posts" class="button button-primary" >Import Demo Posts</button>';
}
?>
</div>
</div>
</div>
@ -330,23 +345,4 @@ function highlight_to_share_popup() {
";
}
/* HOOKS */
//Move to custom hooks class or file in future
function lcp_wp_head_hooks(){
// Define lcp_theme_settings array from wp_options
$options = get_option('lcp_theme_settings', array());
// Echo highlight-to-share markup and enqueue javascript
// Highlight-to-share css is already in style.css
if ( isset($options['enable_highlight_to_share']) && $options['enable_highlight_to_share'] ) {
highlight_to_share_popup();
wp_enqueue_script( 'lcp-highlight-to-share', get_template_directory_uri() . '/assets/js/highlight-to-share.js', array(), null, true );
}
}
add_action('wp_head', 'lcp_wp_head_hooks');
/* ICONS */

View File

@ -1,15 +1,60 @@
<?php
// Register the function to run when the theme is switched (deactivated)
add_action('switch_theme', 'drop_lcp_icons_table');
// Hook into theme activation to call the function
function lcp_activate() {
// Call the function to import the default icon sets
lcp_install_icon_set(true);
}
function lcp_theme_deactivate() {
lcp_delete_demo_posts();
}
// Register the function to run when the theme is switched (deactivated)
add_action('switch_theme', 'lcp_theme_deactivate');
// Add the action hook to run the function when the theme is activated
add_action('after_switch_theme', 'lcp_activate');
function lcp_enqueue_dashboard_scripts($hook) {
// Only load the script on the icon-management page
// Enqueue JavaScript for AJAX
wp_enqueue_script(
'icon-import-script',
get_template_directory_uri() . '/assets/js/icon-import.js',
array(), // No dependencies for vanilla JS
null,
true
);
// Pass the AJAX URL to the script
wp_localize_script('icon-import-script', 'lcp_ajax', array(
'ajax_url' => admin_url('admin-ajax.php')
));
}
add_action('admin_enqueue_scripts', 'lcp_enqueue_dashboard_scripts');
function lcp_backend_enqueue() {
// Enqueue the theme's main stylesheet (style.css)
wp_enqueue_style('lcp-style', get_stylesheet_uri());
wp_enqueue_script('lcp-script', get_template_directory_uri() . '/script.js');
wp_enqueue_script('lcp-ui', get_template_directory_uri() . '/assets/js/lcp-ui.js');
// Enqueue custom script to handle the Demo post import AJAX request
wp_enqueue_script('lcp-import-demo-posts-ajax', get_template_directory_uri() . '/assets/js/demo-posts-import.js', array(), null, true);
// Add the AJAX URL and nonce as JavaScript variables
wp_localize_script('lcp-import-demo-posts-ajax', 'lcp_ajax_obj', array(
'ajax_url' => admin_url('admin-ajax.php'), // This is the URL that well send the AJAX request to
'nonce' => wp_create_nonce('lcp_import_demo_posts_nonce') // Security nonce for validation
));
}
add_action('admin_enqueue_scripts', 'lcp_backend_enqueue');

View File

@ -5,6 +5,9 @@ Description: A simple Block Theme for local content publishers.
Version: 0.0.1
*/
/* Main */
.lcp {display:block}
/* Main viewport container */
#lcp-viewport-outer {
overflow: hidden; /* Hide overflow */