Files
local-content-pro/functions.php
2024-12-27 22:56:39 -08:00

161 lines
5.6 KiB
PHP

<?php
//include get_template_directory(). '/includes/blocks/lcp-gallery/lcp-gallery.php';
include get_template_directory() . '/includes/classes/blocks.php';
new Lcp_Blocks();
// Include backend php
include get_template_directory() . '/includes/classes/backend.php';
function lcp_block_theme_setup() {
add_theme_support( 'block-templates' );
}
add_action( 'after_setup_theme', 'lcp_block_theme_setup' );
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');
}
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');
}
add_action('admin_enqueue_scripts', 'lcp_enqueue');
// Backend enqueue
function lcp_enqueue_svg_repeater_script($hook) {
// Only enqueue on the theme settings page
// if ($hook !== 'settings_page_lcp-theme-settings') {
// return;
// }
// Enqueue the script with a dependency on jQuery
wp_enqueue_script(
'lcp-svg-repeater', // Handle for the script
get_template_directory_uri() . '/assets/js/backend-script.js', // Path to your JS file
array(), // Dependency on jQuery
null, // No specific version
true // Load in footer (true)
);
}
add_action('admin_enqueue_scripts', 'lcp_enqueue_svg_repeater_script');
/* KEY POINTS */
function add_key_points_meta_box() {
add_meta_box(
'key_points_meta_box', // ID of the meta box
'Key Points', // Title of the meta box
'render_key_points_meta_box', // Callback function to display the meta box
'post', // Post type (you can change it to other post types)
'normal', // Context (normal, side, advanced)
'default' // Priority
);
}
// Add meta box if setting is turned on
$options = get_option('lcp_theme_settings', array());
$enable_key_points_meta = isset($options['enable_key_points_meta']) ? $options['enable_key_points_meta'] : 0;
// Check if 'enable_key_points_meta' is enabled (1), then add the meta box
if ($enable_key_points_meta) {
// Hook into add_meta_boxes to register the custom meta box
add_action('add_meta_boxes', 'add_key_points_meta_box');
}
// Sanitize the SVGs data
function lcp_custom_svgs_sanitize($input) {
// Ensure the input is an array
if (is_array($input)) {
foreach ($input as $key => $svg) {
// Sanitize the name and path for each SVG
if (isset($svg['name'])) {
$input[$key]['name'] = sanitize_text_field($svg['name']);
}
if (isset($svg['path'])) {
$input[$key]['path'] = sanitize_textarea_field($svg['path']);
}
}
}
return $input;
}
// Existing function to render the theme settings page
// Callback function to render the meta box
function render_key_points_meta_box($post) {
// Retrieve the stored key points (serialized data)
$key_points_serialized = get_post_meta($post->ID, '_key_points', true);
$key_points = !empty($key_points_serialized) ? unserialize($key_points_serialized) : [];
?>
<div id="key-points-repeater">
<ul id="key-points-list">
<?php foreach ($key_points as $index => $point) : ?>
<li class="key-point">
<input type="text" name="key_points[<?php echo $index; ?>]" value="<?php echo esc_attr($point); ?>" class="key-point-input" />
<button type="button" class="remove-key-point">Remove</button>
</li>
<?php endforeach; ?>
</ul>
<button type="button" id="add-key-point">Add Key Point</button>
</div>
<script>
document.addEventListener('DOMContentLoaded', function () {
const addButton = document.getElementById('add-key-point');
const keyPointsList = document.getElementById('key-points-list');
// Function to add a new key point input field
addButton.addEventListener('click', function () {
const index = keyPointsList.children.length;
const newItem = document.createElement('li');
newItem.classList.add('key-point');
newItem.innerHTML = `
<input type="text" name="key_points[${index}]" value="" class="key-point-input" />
<button type="button" class="remove-key-point">Remove</button>
`;
keyPointsList.appendChild(newItem);
});
// Function to remove a key point input field
keyPointsList.addEventListener('click', function (e) {
if (e.target && e.target.classList.contains('remove-key-point')) {
e.target.closest('.key-point').remove();
}
});
});
</script>
<?php
}
// Save the meta box data
function save_key_points_meta_box($post_id) {
// Check if this is an autosave or if the nonce is missing
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id;
if (!isset($_POST['key_points'])) return $post_id;
// Sanitize and store the key points as a serialized array
$key_points = array_map('sanitize_text_field', $_POST['key_points']);
update_post_meta($post_id, 'key_points', serialize($key_points)); // Use serialize instead of json_encode
}
add_action('save_post', 'save_key_points_meta_box');