Files
local-content-pro/functions.php
2024-12-18 02:27:09 -08:00

441 lines
16 KiB
PHP

<?php
/* Blocks */
//include get_template_directory(). '/includes/blocks/lcp-gallery/lcp-gallery.php';
//include get_template_directory(). '/includes/blocks/lcp-gallery/lcp-gallery.php';
include get_template_directory() . '/includes/classes/blocks.php';
new Lcp_Blocks();
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');
// 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
function render_lcp_theme_settings_page() {
?>
<div class="wrap">
<h1>Theme Settings</h1>
<form method="post" action="options.php">
<?php
// Output necessary settings fields for WordPress to save
settings_fields('lcp_theme_settings_group');
do_settings_sections('lcp_theme_settings_page'); // This outputs all the fields defined by add_settings_field
?>
<?php submit_button(); ?>
</form>
</div>
<?php
}
// 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');
/* BACKEND PAGES */
// Create backend pages
function lcp_backend_pages() {
// Separator
// Theme Settings page
add_menu_page(
'Local Content Pro', // Page title
'Local Content Pro', // Menu title
'manage_options', // Capability required to access this menu
'local-content-pro', // Menu slug
'render_lcp_theme_settings_page', // Function to render the page
'dashicons-admin-generic', // Icon for the menu
25 // Position
);
// Register the Custom Code settings page
add_menu_page(
'Custom Code Inserter', // Page title
'Code Inserter', // Menu title
'manage_options', // Capability required - admin only
'lcp-code-inserter', // Menu slug
'custom-code-inserter', // Callback function
'', // Icon
25
);
}
add_action( 'admin_menu', 'lcp_backend_pages' );
// Function to render the theme settings page
// Function to render the theme settings page
function lcp_register_custom_svg_settings() {
// Register the settings group and option for custom SVGs
register_setting(
'lcp_theme_settings_group', // Settings group
'lcp_custom_svgs', // Option name
'lcp_custom_svgs_sanitize' // Sanitization function
);
// Add a section to organize the settings
add_settings_section(
'lcp_custom_svgs_section', // Section ID
'Custom SVGs', // Section title
'', // Section callback (optional)
'lcp_theme_settings_page' // Page where to show this section
);
// Add the custom SVGs field
add_settings_field(
'lcp_custom_svgs', // Field ID
'Add Custom SVGs', // Field title
'lcp_custom_svgs_field', // Field callback function
'lcp_theme_settings_page', // Page where this field appears
'lcp_custom_svgs_section' // Section where this field belongs
);
}
add_action('admin_init', 'lcp_register_custom_svg_settings');
// Callback to display the custom SVGs field
function lcp_custom_svgs_field() {
// Retrieve current custom SVGs
$custom_svgs = get_option( 'lcp_custom_svgs', array() );
?>
<div id="svg-repeater-container">
<?php
// Loop through each SVG and display its name and path
if (!empty($custom_svgs)) {
foreach ($custom_svgs as $svg) {
?>
<div class="repeater-row">
<input type="text" name="lcp_custom_svgs[][name]" value="<?php echo esc_attr($svg['name']); ?>" placeholder="SVG Name" />
<textarea name="lcp_custom_svgs[][path]" placeholder="SVG Path"><?php echo esc_textarea($svg['path']); ?></textarea>
<button type="button" class="remove-row">Remove</button>
</div>
<?php
}
}
?>
</div>
<button type="button" id="add-svg-row">Add SVG</button>
<?php
}
// Enqueue the JavaScript for handling the repeater functionality
// Function to register settings for the theme
function lcp_register_settings() {
// Register the settings group and the option for lcp_theme_settings
register_setting(
'lcp_theme_settings_group', // Settings group
'lcp_theme_settings', // Option name
'lcp_theme_settings_sanitize' // Sanitization callback (optional)
);
// Add a section to organize the settings
add_settings_section(
'lcp_theme_settings_section', // Section ID
'Custom Code Settings', // Section title
'', // Section callback (optional)
'lcp_theme_settings_page' // Page where to show this section
);
// Add the field for the 'Enable Key Points Meta' checkbox
add_settings_field(
'enable_key_points_meta', // Field ID
'Enable Key Points Meta', // Field title
'lcp_enable_key_points_meta_field', // Field callback function
'lcp_theme_settings_page', // Page where this field appears
'lcp_theme_settings_section' // Section where this field belongs
);
}
add_action('admin_init', 'lcp_register_settings');
// Function to output the checkbox for 'Enable Key Points Meta'
function lcp_enable_key_points_meta_field() {
// Retrieve current value for 'enable_key_points_meta'
$options = get_option( 'lcp_theme_settings', array() );
$checked = isset( $options['enable_key_points_meta'] ) ? $options['enable_key_points_meta'] : false;
// Output the checkbox
echo '<input type="checkbox" name="lcp_theme_settings[enable_key_points_meta]" value="1" ' . checked( $checked, 1, false ) . ' />';
}
// Optional: Sanitization callback function
function lcp_theme_settings_sanitize( $input ) {
// Ensure that only the expected values are saved (e.g., bool for the checkbox)
if ( isset( $input['enable_key_points_meta'] ) ) {
$input['enable_key_points_meta'] = (bool) $input['enable_key_points_meta']; // Convert to boolean
}
return $input;
}
// The callback function to display the custom code inserterpage content
function lcp_custom_code_inserter_page() {
// Get the stored options from wp_options
$custom_code = get_option( 'custom_code_options', array() );
?>
<div class="wrap">
<h1>Custom Code Settings</h1>
<form method="post" action="options.php">
<?php settings_fields( 'custom_code_group' );
if (defined('LCP_DISABLE_CODE_INSERTER') && LCP_DISABLE_CODE_INSERTER === true) {
echo "<h2 style=\"margin:10px 5px\"> LCP Custom Code Inserter is currently disabled. </h2>
<h3 style=\"margin:5px 5px\"> You can add and modify code snippets, but they will not be in effect. </h3> ";
}
?>
<table class="form-table">
<tr valign="top">
<th scope="row">CSS in Header</th>
<td><textarea name="custom_code_options[css_header]" rows="10" cols="50"><?php echo esc_textarea( isset( $custom_code['css_header'] ) ? $custom_code['css_header'] : '' ); ?></textarea></td>
</tr>
<tr valign="top">
<th scope="row">JavaScript in Header</th>
<td><textarea name="custom_code_options[js_header]" rows="10" cols="50"><?php echo esc_textarea( isset( $custom_code['js_header'] ) ? $custom_code['js_header'] : '' ); ?></textarea></td>
</tr>
<tr valign="top">
<th scope="row">JavaScript in Footer</th>
<td><textarea name="custom_code_options[js_footer]" rows="10" cols="50"><?php echo esc_textarea( isset( $custom_code['js_footer'] ) ? $custom_code['js_footer'] : '' ); ?></textarea></td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
}
// Register a custom code setting in wp_options to store the custom code snippets
function lcp_custom_code_settings_init() {
register_setting( 'custom_code_group', 'custom_code_options', 'sanitize_custom_code' );
}
add_action( 'admin_init', 'lcp_custom_code_settings_init' );
// Sanitize the custom code input before saving it
function sanitize_custom_code( $input ) {
// Sanitize each field
if ( isset( $input['js_header'] ) ) {
$input['js_header'] = sanitize_textarea_field( $input['js_header'] );
}
if ( isset( $input['js_footer'] ) ) {
$input['js_footer'] = sanitize_textarea_field( $input['js_footer'] );
}
if ( isset( $input['css_header'] ) ) {
$input['css_header'] = sanitize_textarea_field( $input['css_header'] );
}
if ( isset( $input['css_footer'] ) ) {
$input['css_footer'] = sanitize_textarea_field( $input['css_footer'] );
}
return $input;
}
function lcp_custom_code_output($hook) {
// Disable the custom code inserter if wp_env is set
if (defined('LCP_DISABLE_CODE_INSERTER') && LCP_DISABLE_CODE_INSERTER === true) {
// Retrieve the custom code options
$custom_code = get_option( 'custom_code_options', array() );
// Prevent execution in the admin area
if ( is_admin() ) return;
// Check if the hook is for the header
if ($hook == "header") {
// Check and output JS for header
if ( isset( $custom_code['js_header'] ) && ! empty( $custom_code['js_header'] ) ) {
echo '<script type="text/javascript">' . $custom_code['js_header'] . '</script>';
}
// Check and output CSS for header
if ( isset( $custom_code['css_header'] ) && ! empty( $custom_code['css_header'] ) ) {
echo '<style type="text/css">' . $custom_code['css_header'] . '</style>';
}
}
// Check if the hook is for the footer
if ($hook == "footer") {
// Check and output JS for footer
if ( isset( $custom_code['js_footer'] ) && ! empty( $custom_code['js_footer'] ) ) {
echo '<script type="text/javascript">' . $custom_code['js_footer'] . '</script>';
}
}
}
}
// Attach to wp_head and wp_footer, passing the location as a parameter
add_action( 'wp_head', function() {
lcp_custom_code_output('header');
});
add_action( 'wp_footer', function() {
lcp_custom_code_output('footer');
});
// Hook into wp_footer to add the login form in the footer
function add_login_form_to_footer() {
?>
<div class="lcp-popup lcp-login-form" id="lcp-login-form">
<div class="lcp-popup-inner">
<h2><?php esc_html_e( 'Login', 'your-theme' ); ?></h2>
<!-- WordPress Login Form -->
<?php wp_login_form(); ?>
<!-- Optionally add a link to register or reset password -->
<p>
<a href="<?php echo esc_url( wp_registration_url() ); ?>"><?php esc_html_e( 'Register', 'your-theme' ); ?></a> |
<a href="<?php echo esc_url( wp_lostpassword_url() ); ?>"><?php esc_html_e( 'Forgot Password?', 'your-theme' ); ?></a>
</p>
</div>
</div>
<?php
}
add_action( 'wp_footer', 'add_login_form_to_footer' );