Changes to lcp button and icons
This commit is contained in:
100
functions.php
100
functions.php
@ -156,5 +156,105 @@ function save_key_points_meta_box($post_id) {
|
||||
}
|
||||
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.");
|
||||
}
|
||||
|
||||
// Step 2: Decode the JSON data into a PHP array
|
||||
$data = json_decode($jsonData, true);
|
||||
|
||||
// Check if JSON decoding was successful
|
||||
if ($data === null) {
|
||||
die("Error decoding the JSON data.");
|
||||
}
|
||||
|
||||
// Step 3: Create the table if it doesn't exist
|
||||
global $wpdb;
|
||||
$table_name = $wpdb->prefix . 'lcp_icons'; // Table name with WordPress prefix
|
||||
|
||||
// Create the table if it does not exist
|
||||
$sql = "
|
||||
CREATE TABLE IF NOT EXISTS $table_name (
|
||||
id INT(11) NOT NULL AUTO_INCREMENT,
|
||||
set_name VARCHAR(255) NOT NULL,
|
||||
set_family VARCHAR(255) NOT NULL,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
paths LONGTEXT NOT NULL,
|
||||
viewbox VARCHAR(255) NOT NULL,
|
||||
PRIMARY KEY (id)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
";
|
||||
|
||||
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
|
||||
dbDelta($sql);
|
||||
|
||||
// Step 4: Insert data into the database
|
||||
foreach ($data as $iconSet) {
|
||||
// Extract values for the set_name and set_family from the top-level object
|
||||
$setName = sanitize_text_field($iconSet['name']);
|
||||
$setFamily = sanitize_text_field($iconSet['family']);
|
||||
|
||||
// Insert each SVG in the 'svgs' array
|
||||
if (isset($iconSet['svgs']) && is_array($iconSet['svgs'])) {
|
||||
foreach ($iconSet['svgs'] as $svg) {
|
||||
$name = sanitize_text_field($svg['name']);
|
||||
|
||||
// Unescape the paths field (HTML entities or Unicode escapes)
|
||||
$paths = html_entity_decode($svg['paths']); // Decode HTML entities (like " or &)
|
||||
$paths = stripslashes($paths); // Remove any additional escaping, such as slashes
|
||||
|
||||
$viewbox = sanitize_text_field($svg['viewBox']);
|
||||
|
||||
// Prepare the data for insertion
|
||||
$data = array(
|
||||
'set_name' => $setName,
|
||||
'set_family' => $setFamily,
|
||||
'name' => $name,
|
||||
'paths' => $paths,
|
||||
'viewbox' => $viewbox,
|
||||
);
|
||||
|
||||
// Insert into the database
|
||||
$wpdb->insert($table_name, $data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
// 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() {
|
||||
global $wpdb;
|
||||
|
||||
// Table name with WordPress prefix
|
||||
$table_name = $wpdb->prefix . 'lcp_icons';
|
||||
|
||||
// SQL query to drop the table
|
||||
$sql = "DROP TABLE IF EXISTS $table_name;";
|
||||
|
||||
// Execute the query
|
||||
$wpdb->query($sql);
|
||||
|
||||
|
||||
}
|
||||
|
||||
// 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');
|
||||
Reference in New Issue
Block a user