diff --git a/assets/js/demo-posts-import.js b/assets/js/demo-posts-import.js index 859f1dd..7cbea3e 100644 --- a/assets/js/demo-posts-import.js +++ b/assets/js/demo-posts-import.js @@ -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.'); + }); + }); +} + }); diff --git a/assets/json/demo-posts.json b/assets/json/demo-posts.json index 8958d22..974ff06 100644 --- a/assets/json/demo-posts.json +++ b/assets/json/demo-posts.json @@ -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" diff --git a/functions.php b/functions.php index 35dab69..460e9dd 100644 --- a/functions.php +++ b/functions.php @@ -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 we’ll 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.'); +} diff --git a/includes/classes/backend.php b/includes/classes/backend.php index 3355425..ac9fd92 100644 --- a/includes/classes/backend.php +++ b/includes/classes/backend.php @@ -307,7 +307,22 @@ function render_lcp_theme_settings_page() {

Miscellaneous Settings

- +

Demo Posts

+ ' . count($demo_posts_data['post_ids']) . ' Demo posts have been imported.'; + // If demo posts have been imported, display the "Delete Demo Posts" button (this can be disabled or hidden based on your logic) + echo ''; + } else { + // If demo posts have NOT been imported, display the "Import Demo Posts" button + echo ''; + } + ?> + @@ -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 */ diff --git a/includes/classes/wp-hooks.php b/includes/classes/wp-hooks.php index 7795ac8..4dbd0a3 100644 --- a/includes/classes/wp-hooks.php +++ b/includes/classes/wp-hooks.php @@ -1,15 +1,60 @@ 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 we’ll 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'); \ No newline at end of file diff --git a/style.css b/style.css index dcf13c5..7dc92b4 100644 --- a/style.css +++ b/style.css @@ -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 */