Changes to blocks and added basic framework for Newsletters
This commit is contained in:
198
functions.php
198
functions.php
@ -3,7 +3,8 @@
|
||||
|
||||
|
||||
|
||||
|
||||
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';
|
||||
@ -18,11 +19,24 @@ function lcp_block_theme_setup() {
|
||||
add_theme_support( 'block-templates' );
|
||||
}
|
||||
add_action( 'after_setup_theme', 'lcp_block_theme_setup' );
|
||||
/* Add Styles */
|
||||
function lcp_header_height_style() {
|
||||
// Fetch the lcp_header_height value from wp_options.
|
||||
$header_height = get_option('lcp_header_height', 0); // Default to 0 if not found
|
||||
|
||||
// Ensure we have a valid value
|
||||
if ($header_height) {
|
||||
// Output the inline style tag with the CSS variable for header height
|
||||
echo "<style>:root { --lcp--header--height: {$header_height}px; }</style>";
|
||||
}
|
||||
}
|
||||
add_action('wp_enqueue_scripts', 'lcp_header_height_style',1);
|
||||
|
||||
|
||||
function lcp_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-script', get_template_directory_uri() . '/assets/js/lcp.js');
|
||||
wp_enqueue_script('lcp-ui-script', get_template_directory_uri() . '/assets/js/lcp-ui.js');
|
||||
|
||||
}
|
||||
@ -161,10 +175,6 @@ 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');
|
||||
|
||||
|
||||
|
||||
@ -241,88 +251,138 @@ add_action('admin_enqueue_scripts', 'mytheme_enqueue_dashboard_styles');
|
||||
|
||||
|
||||
// Handle the AJAX request for installing icon sets
|
||||
function mytheme_install_icon_set() {
|
||||
if (!isset($_POST['icon_set_id'])) {
|
||||
wp_send_json_error(array('message' => 'Invalid icon set ID.'));
|
||||
}
|
||||
|
||||
$icon_set_id = intval($_POST['icon_set_id']);
|
||||
function lcp_install_icon_set($defaults = false) {
|
||||
$json_path = get_template_directory() . '/assets/json/icons/icon-definitions.json';
|
||||
|
||||
// Create the database table if it doesn't exist
|
||||
mytheme_create_icons_table();
|
||||
if ($defaults) {
|
||||
// Import only the default icons
|
||||
if (file_exists($json_path)) {
|
||||
$json_data = file_get_contents($json_path);
|
||||
$icon_sets = json_decode($json_data, true);
|
||||
|
||||
// Read the icon definitions JSON file
|
||||
if (file_exists($json_path)) {
|
||||
$json_data = file_get_contents($json_path);
|
||||
$icon_sets = json_decode($json_data, true);
|
||||
// Loop through all icon sets and filter for default icons
|
||||
foreach ($icon_sets as $icon_set) {
|
||||
if (isset($icon_set['default']) && $icon_set['default'] === true) {
|
||||
$file_name = $icon_set['fileName']; // Assuming the JSON structure has 'fileName' key
|
||||
$icon_file_path = get_template_directory() . '/assets/json/icons/' . $file_name;
|
||||
|
||||
// Search for the selected icon set by ID
|
||||
$selected_icon_set = null;
|
||||
foreach ($icon_sets as $icon_set) {
|
||||
if ($icon_set['id'] === $icon_set_id) {
|
||||
$selected_icon_set = $icon_set;
|
||||
break;
|
||||
if (file_exists($icon_file_path)) {
|
||||
$icon_data = file_get_contents($icon_file_path);
|
||||
$icon_json = json_decode($icon_data, true);
|
||||
|
||||
// Check if svgs exist and process them
|
||||
if (isset($icon_json[0]['svgs']) && is_array($icon_json[0]['svgs'])) {
|
||||
foreach ($icon_json[0]['svgs'] as $svg) {
|
||||
global $wpdb;
|
||||
$table_name = $wpdb->prefix . 'lcp_icons';
|
||||
|
||||
// Insert the SVG data into the database
|
||||
$wpdb->insert(
|
||||
$table_name,
|
||||
array(
|
||||
'icon_name' => $svg['name'],
|
||||
'icon_set_id' => $icon_set['id'],
|
||||
'icon_family' => $icon_set['setFamily'],
|
||||
'icon_version' => $icon_set['version'],
|
||||
'paths' => ($svg['paths']),
|
||||
'viewbox' => sanitize_text_field($svg['viewBox']),
|
||||
'icon_id' => sanitize_text_field($svg['id']),
|
||||
)
|
||||
);
|
||||
}
|
||||
} else {
|
||||
wp_send_json_error(array('message' => 'SVGs data not found in the JSON file.'));
|
||||
}
|
||||
} else {
|
||||
wp_send_json_error(array('message' => 'SVG file for default set not found.'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
wp_send_json_error(array('message' => 'Icon definitions file not found.'));
|
||||
}
|
||||
} elseif (isset($_POST['icon_set_id'])) {
|
||||
// Default behavior - check for icon_set_id in $_POST
|
||||
if (!isset($_POST['icon_set_id'])) {
|
||||
wp_send_json_error(array('message' => 'Invalid icon set ID.'));
|
||||
}
|
||||
|
||||
// If the icon set is found
|
||||
if ($selected_icon_set) {
|
||||
// Dynamically build the icon file path using the fileName from icon-definitions.json
|
||||
$file_name = $selected_icon_set['fileName']; // Assuming the JSON structure has 'fileName' key
|
||||
$icon_file_path = get_template_directory() . '/assets/json/icons/' . $file_name;
|
||||
$icon_set_id = intval($_POST['icon_set_id']);
|
||||
|
||||
// Now read the corresponding SVG file for the icon set
|
||||
if (file_exists($icon_file_path)) {
|
||||
$icon_data = file_get_contents($icon_file_path);
|
||||
$icon_json = json_decode($icon_data, true);
|
||||
// Create the database table if it doesn't exist
|
||||
mytheme_create_icons_table();
|
||||
|
||||
// Debugging: Check the structure of the decoded JSON
|
||||
if (is_array($icon_json)) {
|
||||
error_log(print_r($icon_json, true)); // Logs to PHP error log for inspection
|
||||
// Read the icon definitions JSON file
|
||||
if (file_exists($json_path)) {
|
||||
$json_data = file_get_contents($json_path);
|
||||
$icon_sets = json_decode($json_data, true);
|
||||
|
||||
// Search for the selected icon set by ID
|
||||
$selected_icon_set = null;
|
||||
foreach ($icon_sets as $icon_set) {
|
||||
if ($icon_set['id'] === $icon_set_id) {
|
||||
$selected_icon_set = $icon_set;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Loop through svgs if it exists and is an array
|
||||
if (isset($icon_json[0]['svgs']) && is_array($icon_json[0]['svgs'])) {
|
||||
// Insert rows with actual data from the SVG file
|
||||
foreach ($icon_json[0]['svgs'] as $svg) {
|
||||
global $wpdb;
|
||||
$table_name = $wpdb->prefix . 'lcp_icons';
|
||||
// If the icon set is found
|
||||
if ($selected_icon_set) {
|
||||
$file_name = $selected_icon_set['fileName']; // Assuming the JSON structure has 'fileName' key
|
||||
$icon_file_path = get_template_directory() . '/assets/json/icons/' . $file_name;
|
||||
|
||||
// Insert each SVG data into the database
|
||||
$wpdb->insert(
|
||||
$table_name,
|
||||
array(
|
||||
'icon_name' => $svg['name'], // Using the name from the SVG JSON
|
||||
'icon_set_id' => $selected_icon_set['id'],
|
||||
'icon_family' => $selected_icon_set['setFamily'], // Get family dynamically from the selected icon set
|
||||
'icon_version' => $selected_icon_set['version'], // Get version dynamically from the selected icon set
|
||||
'paths' => ($svg['paths']), // SVG paths from the JSON
|
||||
'viewbox' => sanitize_text_field($svg['viewBox']), // Viewbox for the SVG
|
||||
'icon_id' => sanitize_text_field($svg['id']) // Using the actual icon ID from the JSON
|
||||
)
|
||||
);
|
||||
// Now read the corresponding SVG file for the icon set
|
||||
if (file_exists($icon_file_path)) {
|
||||
$icon_data = file_get_contents($icon_file_path);
|
||||
$icon_json = json_decode($icon_data, true);
|
||||
|
||||
// Check the structure of the decoded JSON
|
||||
if (is_array($icon_json)) {
|
||||
error_log(print_r($icon_json, true)); // Logs to PHP error log for inspection
|
||||
}
|
||||
|
||||
wp_send_json_success(array('message' => 'Icon set installed and rows added with actual data.'));
|
||||
// Loop through svgs if it exists and is an array
|
||||
if (isset($icon_json[0]['svgs']) && is_array($icon_json[0]['svgs'])) {
|
||||
foreach ($icon_json[0]['svgs'] as $svg) {
|
||||
global $wpdb;
|
||||
$table_name = $wpdb->prefix . 'lcp_icons';
|
||||
|
||||
// Insert each SVG data into the database
|
||||
$wpdb->insert(
|
||||
$table_name,
|
||||
array(
|
||||
'icon_name' => $svg['name'],
|
||||
'icon_set_id' => $selected_icon_set['id'],
|
||||
'icon_family' => $selected_icon_set['setFamily'],
|
||||
'icon_version' => $selected_icon_set['version'],
|
||||
'paths' => ($svg['paths']),
|
||||
'viewbox' => sanitize_text_field($svg['viewBox']),
|
||||
'icon_id' => sanitize_text_field($svg['id']),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
wp_send_json_success(array('message' => 'Icon set installed and rows added with actual data.'));
|
||||
} else {
|
||||
wp_send_json_error(array('message' => 'SVGs data not found in the JSON file.'));
|
||||
}
|
||||
} else {
|
||||
wp_send_json_error(array('message' => 'SVGs data not found in the JSON file.'));
|
||||
wp_send_json_error(array('message' => 'SVG file not found.'));
|
||||
}
|
||||
} else {
|
||||
wp_send_json_error(array('message' => 'SVG file not found.'));
|
||||
wp_send_json_error(array('message' => 'Icon set not found.'));
|
||||
}
|
||||
} else {
|
||||
wp_send_json_error(array('message' => 'Icon set not found.'));
|
||||
wp_send_json_error(array('message' => 'Icon definitions not found.'));
|
||||
}
|
||||
} else {
|
||||
wp_send_json_error(array('message' => 'Icon definitions not found.'));
|
||||
}
|
||||
}
|
||||
add_action('wp_ajax_install_icon_set', 'mytheme_install_icon_set');
|
||||
|
||||
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 to create the icons table in the database if it doesn't exist
|
||||
function mytheme_create_icons_table() {
|
||||
global $wpdb;
|
||||
@ -403,15 +463,3 @@ function mytheme_uninstall_icon_set() {
|
||||
add_action('wp_ajax_uninstall_icon_set', 'mytheme_uninstall_icon_set');
|
||||
|
||||
|
||||
/* Add Styles */
|
||||
function lcp_header_height_style() {
|
||||
// Fetch the lcp_header_height value from wp_options.
|
||||
$header_height = get_option('lcp_header_height', 0); // Default to 0 if not found
|
||||
|
||||
// Ensure we have a valid value
|
||||
if ($header_height) {
|
||||
// Output the inline style tag with the CSS variable for header height
|
||||
echo "<style>:root { --lcp--header--height: {$header_height}px; }</style>";
|
||||
}
|
||||
}
|
||||
add_action('wp_head', 'lcp_header_height_style');
|
||||
|
||||
Reference in New Issue
Block a user