Added Demo posts inserter

This commit is contained in:
Jeremy Rangel
2025-01-02 03:07:43 -08:00
parent 1ce1a08442
commit 4447e50bcf
18 changed files with 278 additions and 360 deletions

View File

@ -4,7 +4,7 @@
include get_template_directory() . '/includes/classes/wp-hooks.php';
include get_template_directory() . '/includes/classes/lcp-newsletters.php';
//include get_template_directory(). '/includes/blocks/lcp-gallery/lcp-gallery.php';
include get_template_directory() . '/includes/classes/blocks.php';
@ -49,10 +49,19 @@ 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_enqueue');
add_action('admin_enqueue_scripts', 'lcp_backend_enqueue');
/* KEY POINTS */
@ -180,26 +189,26 @@ function drop_lcp_icons_table() {
/* BACKEND ICON ADDER */
// Register the dashboard page in the admin menu
function mytheme_register_dashboard_page() {
function lcp_register_dashboard_page() {
add_menu_page(
'Icon Management', // Page Title
'Icon Management', // Menu Title
'manage_options', // Capability
'icon-management', // Menu Slug
'mytheme_dashboard_page', // Callback function
'lcp_dashboard_page', // Callback function
'dashicons-images-alt2', // Icon for the menu item
60 // Position
);
}
add_action('admin_menu', 'mytheme_register_dashboard_page');
add_action('admin_menu', 'lcp_register_dashboard_page');
// Callback function to display the dashboard page content
function mytheme_dashboard_page() {
function lcp_dashboard_page() {
?>
<div class="wrap">
<h1><?php esc_html_e('Icon Management Dashboard', 'mytheme'); ?></h1>
<h1><?php esc_html_e('Icon Management Dashboard', 'lcp'); ?></h1>
<div id="icon-dashboard-container">
<?php mytheme_display_icon_sets(); ?>
<?php lcp_display_icon_sets(); ?>
</div> <!-- This will be populated by PHP -->
</div>
<?php
@ -207,7 +216,7 @@ function mytheme_dashboard_page() {
// Function to read and display icon sets from the JSON file
// Function to read and display icon sets from the JSON file
function mytheme_display_icon_sets() {
function lcp_display_icon_sets() {
// Path to the JSON file
$json_path = get_template_directory() . '/assets/json/icons/icon-definitions.json';
@ -238,16 +247,16 @@ function mytheme_display_icon_sets() {
// Enqueue the dashboard styles only on the icon-management page
function mytheme_enqueue_dashboard_styles($hook) {
function lcp_enqueue_dashboard_styles($hook) {
// Enqueue CSS for the dashboard
wp_enqueue_style(
'mytheme-dashboard-css',
'lcp-dashboard-css',
get_template_directory_uri() . '/assets/css/dashboard.css'
);
}
add_action('admin_enqueue_scripts', 'mytheme_enqueue_dashboard_styles');
add_action('admin_enqueue_scripts', 'lcp_enqueue_dashboard_styles');
// Handle the AJAX request for installing icon sets
@ -311,7 +320,7 @@ function lcp_install_icon_set($defaults = false) {
$icon_set_id = intval($_POST['icon_set_id']);
// Create the database table if it doesn't exist
mytheme_create_icons_table();
lcp_create_icons_table();
// Read the icon definitions JSON file
if (file_exists($json_path)) {
@ -384,7 +393,7 @@ add_action('wp_ajax_install_icon_set', 'lcp_install_icon_set');
// Function to create the icons table in the database if it doesn't exist
function mytheme_create_icons_table() {
function lcp_create_icons_table() {
global $wpdb;
$table_name = $wpdb->prefix . 'lcp_icons';
@ -409,7 +418,7 @@ function mytheme_create_icons_table() {
}
function mytheme_enqueue_dashboard_scripts($hook) {
function lcp_enqueue_dashboard_scripts($hook) {
// Only load the script on the icon-management page
@ -423,15 +432,15 @@ function mytheme_enqueue_dashboard_scripts($hook) {
);
// Pass the AJAX URL to the script
wp_localize_script('icon-import-script', 'mytheme_ajax', array(
wp_localize_script('icon-import-script', 'lcp_ajax', array(
'ajax_url' => admin_url('admin-ajax.php')
));
}
add_action('admin_enqueue_scripts', 'mytheme_enqueue_dashboard_scripts');
add_action('admin_enqueue_scripts', 'lcp_enqueue_dashboard_scripts');
// Handle the AJAX request for uninstalling icon sets
function mytheme_uninstall_icon_set() {
function lcp_uninstall_icon_set() {
// Ensure icon set ID is passed in the request
if (!isset($_POST['icon_set_id'])) {
wp_send_json_error(array('message' => 'Invalid icon set ID.'));
@ -460,6 +469,151 @@ function mytheme_uninstall_icon_set() {
}
}
add_action('wp_ajax_uninstall_icon_set', 'mytheme_uninstall_icon_set');
add_action('wp_ajax_uninstall_icon_set', 'lcp_uninstall_icon_set');
/* Import Demo Posts */
function lcp_import_demo_posts() {
$json_data = file_get_contents(get_template_directory() . '/assets/json/demo-posts.json');
// Check if the JSON data is successfully fetched and decoded
if (empty($json_data)) {
return;
}
$posts = json_decode($json_data, true)['posts'];
if (empty($posts)) {
return;
}
// Arrays to store post IDs and media (thumbnail) IDs
$post_ids = [];
$media_ids = [];
foreach ($posts as $post) {
// Prepare post data
$post_data = array(
'post_title' => wp_strip_all_tags($post['title']),
'post_content' => $post['content'],
'post_excerpt' => isset($post['excerpt']) ? $post['excerpt'] : '',
'post_status' => isset($post['status']) ? $post['status'] : 'draft', // Default to 'draft' if no status
'post_date' => isset($post['date']) ? $post['date'] : current_time('mysql'),
'post_type' => 'post',
'post_author' => 1, // Change if you have specific user IDs
);
// Insert the post into the database
$post_id = wp_insert_post($post_data);
// If there was an issue inserting the post, skip this one
if (is_wp_error($post_id)) {
continue;
}
// Store the post ID in the array
$post_ids[] = $post_id;
// Assign categories (if they exist)
if (!empty($post['category'])) {
$category = term_exists($post['category'], 'category');
if ($category) {
wp_set_post_terms($post_id, array($category['term_id']), 'category');
} else {
// If category doesn't exist, create it
$category_id = wp_create_term($post['category'], 'category');
wp_set_post_terms($post_id, array($category_id['term_id']), 'category');
}
}
// Assign tags (if they exist)
if (!empty($post['tags'])) {
wp_set_post_terms($post_id, $post['tags'], 'post_tag');
}
// Handle thumbnail (featured image)
if (!empty($post['thumbnail'])) {
$thumbnail_url = get_template_directory_uri() . '/assets/img/demo-post-thumbnails/' . $post['thumbnail'];
// Download the image and add it to the media library
$thumbnail_id = media_handle_sideload(
array(
'name' => basename($thumbnail_url),
'tmp_name' => download_url($thumbnail_url)
),
$post_id
);
// If the image was uploaded successfully, set it as the post's featured image
if (!is_wp_error($thumbnail_id)) {
set_post_thumbnail($post_id, $thumbnail_id);
// Store the media ID in the array
$media_ids[] = $thumbnail_id;
}
}
// You can add additional meta fields if necessary
// Example:
// update_post_meta($post_id, 'some_meta_key', 'some_value');
}
// After all posts are imported, update the 'lcp_demo_posts' option
$demo_posts_data = array(
'post_ids' => $post_ids,
'media_ids' => $media_ids,
);
update_option('lcp_demo_posts', $demo_posts_data);
// Optionally, you can log the post and media IDs
// var_dump($post_ids, $media_ids); // Uncomment this line if you need to inspect the result
}
/* Delete the Demo Posts */
// Get the lcp_demo_posts option and delete all the posts from there
function lcp_delete_demo_posts() {
// Get the lcp_demo_posts option
$demo_posts_data = get_option('lcp_demo_posts');
// Check if the option exists and contains data
if (!empty($demo_posts_data) && isset($demo_posts_data['post_ids']) && isset($demo_posts_data['media_ids'])) {
// Loop through and delete posts
foreach ($demo_posts_data['post_ids'] as $post_id) {
// Delete post, force delete if necessary (set true for delete permanently)
wp_delete_post($post_id, true); // true will permanently delete
}
// Loop through and delete media (attachments)
foreach ($demo_posts_data['media_ids'] as $media_id) {
// Delete attachment (media file)
wp_delete_attachment($media_id, true); // true will permanently delete
}
// After deletion, remove the option from wp_options table
delete_option('lcp_demo_posts');
// Optionally, you can log the deletion for confirmation
// error_log('Demo posts and media have been deleted.');
} else {
// If the option doesn't exist, or it's empty
error_log('No demo posts data found or already deleted.');
}
}
/* Localize */
add_action('wp_ajax_lcp_import_demo_posts', 'lcp_import_demo_posts_ajax_handler');
// Define the function to handle the AJAX request
function lcp_import_demo_posts_ajax_handler() {
// Check for permissions (optional: can be used to ensure only admins can trigger this)
if (!current_user_can('manage_options')) {
wp_send_json_error('You do not have sufficient permissions to perform this action.');
}
// Call the existing function that imports the demo posts
lcp_import_demo_posts();
// Send a success response back to the browser
wp_send_json_success('Demo posts imported successfully.');
}