changes to lcp-button and icon uploader

This commit is contained in:
Jeremy Rangel
2024-12-29 22:52:17 -08:00
parent 372d5aa2c1
commit 0d59719440
8 changed files with 13490 additions and 13212 deletions

View File

@ -28,6 +28,8 @@ function lcp_enqueue() {
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());
@ -158,18 +160,7 @@ add_action('save_post', 'save_key_points_meta_box');
/* INSERT ICONS TO DB */
// Function to load the JSON file and insert SVG data into the database
function load_material_icons_into_db() {
// Path to the input JSON file in the theme's directory
$inputFile = get_template_directory() . '/assets/json/material-icons-baseline-unescaped.json';
// Step 1: Load the JSON data
$jsonData = file_get_contents($inputFile);
// Check if the file was read successfully
if ($jsonData === false) {
die("Error reading the JSON file.");
}
function load_material_icons_into_db($jsonData) {
// Step 2: Decode the JSON data into a PHP array
$data = json_decode($jsonData, true);
@ -185,7 +176,7 @@ function load_material_icons_into_db() {
// Create the table if it does not exist
$sql = "
CREATE TABLE IF NOT EXISTS $table_name (
id INT(11) NOT NULL AUTO_INCREMENT,
id VARCHAR(255) NOT NULL, -- Use the id from the JSON file
set_name VARCHAR(255) NOT NULL,
set_family VARCHAR(255) NOT NULL,
name VARCHAR(255) NOT NULL,
@ -214,9 +205,11 @@ function load_material_icons_into_db() {
$paths = stripslashes($paths); // Remove any additional escaping, such as slashes
$viewbox = sanitize_text_field($svg['viewBox']);
$id = sanitize_text_field($svg['id']); // Use the id from the JSON object
// Prepare the data for insertion
$data = array(
'id' => $id, // Use the id from the JSON object
'set_name' => $setName,
'set_family' => $setFamily,
'name' => $name,
@ -230,13 +223,12 @@ function load_material_icons_into_db() {
}
}
echo '<p>Icons have been imported into the database.</p>';
}
// Hook the function to the theme activation process
// Register the function to run after the theme is switched
add_action('after_switch_theme', 'load_material_icons_into_db');
// Function to drop the lcp_icons table when the theme is deactivated
function drop_lcp_icons_table() {
@ -257,4 +249,133 @@ function drop_lcp_icons_table() {
// Hook the function to the theme deactivation process
// Register the function to run when the theme is switched (deactivated)
add_action('switch_theme', 'drop_lcp_icons_table');
add_action('switch_theme', 'drop_lcp_icons_table');
/* BACKEND ICON ADDER */
function icon_sets_dashboard_callback() {
// Get the file path to the JSON file
$json_file_path = get_template_directory() . '/assets/json/icons/icon-definitions.json';
// Check if the file exists
if (file_exists($json_file_path)) {
// Read the file contents and decode the JSON
$json_data = file_get_contents($json_file_path);
$icon_sets = json_decode($json_data, true);
if ($icon_sets) {
// Group icon sets by setFamily
$grouped_sets = [];
foreach ($icon_sets as $set) {
// Check if the set has the setFamily key
$setFamily = isset($set['setFamily']) ? $set['setFamily'] : 'Unknown';
// Initialize the family group if not already initialized
if (!isset($grouped_sets[$setFamily])) {
$grouped_sets[$setFamily] = [];
}
// Handle both 'file' and 'fileName'
// Check if 'file' is set or fallback to 'fileName'
$setFile = isset($set['file']) ? $set['file'] : (isset($set['fileName']) ? $set['fileName'] : '');
// Ensure we are correctly adding the set data
$grouped_sets[$setFamily][] = [
'id' => $set['id'],
'setName' => $set['setName'],
'file' => $setFile
];
}
// Output the HTML for the dashboard
echo '<div class="wrap">';
echo '<h1>' . esc_html('Icon Sets Dashboard') . '</h1>';
// Loop through each setFamily and create a section with checkboxes
foreach ($grouped_sets as $setFamily => $sets) {
echo '<div class="lcp-icon-set">';
echo '<h2>' . esc_html($setFamily) . '</h2>';
echo '<ul>';
// Loop through each setName and create a checkbox
foreach ($sets as $set) {
$checkbox_id = 'icon_set_' . $set['id'];
echo '<li><input type="checkbox" id="' . esc_attr($checkbox_id) . '" name="icon_sets[]" value="' . esc_attr($set['file']) . '" data-icon-set-id="' . esc_attr($set['id']) . '">';
echo '<label for="' . esc_attr($checkbox_id) . '">' . esc_html($set['setName']) . '</label></li>';
}
echo '</ul>';
echo '</div>';
}
// Add submit button
echo '<form method="POST" action="">'; // Ensure the form method is POST
echo '<input type="submit" name="submit_icon_sets" value="Import Selected Icons" class="button-primary">';
echo '</form>';
echo '</div>';
// Handle form submission
if (isset($_POST['submit_icon_sets'])) {
// Debugging: Check if form is actually submitting data
echo '<pre>';
var_dump($_POST['icon_sets']); // Check if checkboxes are selected
echo '</pre>';
// Debugging: Display selected icon sets
if (isset($_POST['icon_sets']) && !empty($_POST['icon_sets'])) {
echo '<p>Selected icon sets:</p>';
echo '<pre>';
var_dump($_POST['icon_sets']); // Output the selected icon set values
echo '</pre>';
// Loop through selected icon sets and process the corresponding JSON files
foreach ($_POST['icon_sets'] as $file) {
// Look for the icon set that matches the selected file path
foreach ($icon_sets as $set) {
// Check if the 'file' or 'fileName' matches the selected file
if ($set['file'] == $file || (isset($set['fileName']) && $set['fileName'] == $file)) {
// Get the JSON file for the selected set
$jsonFilePath = get_template_directory() . '/assets/json/icons' . (isset($set['file']) ? $set['file'] : $set['fileName']);
// Debugging: Print the json file path being used
echo '<p>Loading file: ' . esc_html($jsonFilePath) . '</p>';
if (file_exists($jsonFilePath)) {
$jsonData = file_get_contents($jsonFilePath);
load_material_icons_into_db($jsonData); // Pass the JSON data to the function
} else {
echo '<p>Error: File ' . esc_html($jsonFilePath) . ' not found.</p>';
}
break;
}
}
}
} else {
echo '<p>No icon sets selected.</p>';
}
}
} else {
echo '<p>No icon sets found in the JSON file.</p>';
}
} else {
echo '<p>Icon definitions JSON file not found.</p>';
}
}
function register_icon_dashboard_page() {
add_menu_page(
'Icon Sets', // Page Title
'Icon Sets', // Menu Title
'manage_options', // Capability required
'icon-sets-dashboard', // Menu slug
'icon_sets_dashboard_callback', // Callback function
'dashicons-admin-generic', // Icon
60 // Position in the menu
);
}
add_action('admin_menu', 'register_icon_dashboard_page');