Files
local-content-pro/functions.php
Jeremy Rangel 1234341241 New dev branch
2024-12-16 20:12:25 -08:00

456 lines
17 KiB
PHP

<?php
/* Blocks */
include get_template_directory(). '/blocks/lcp-viewport/lcp-viewport.php';
include get_template_directory(). '/blocks/lcp-sidecontent/lcp-sidecontent.php';
include get_template_directory(). '/blocks/lcp-main-area/main-area.php';
include get_template_directory(). '/blocks/lcp-header-container/header-container.php';
include get_template_directory(). '/blocks/lcp-container/lcp-dynamic-container.php';
include get_template_directory(). '/blocks/lcp-key-points/key-points.php';
include get_template_directory(). '/blocks/lcp-button/lcp-button.php';
include get_template_directory(). '/blocks/lcp-gallery/lcp-gallery.php';
function my_block_theme_setup() {
add_theme_support( 'block-templates' );
}
add_action( 'after_setup_theme', 'my_block_theme_setup' );
function my_theme_enqueue_styles() {
// Enqueue the theme's main stylesheet (style.css)
wp_enqueue_style('my-theme-style', get_stylesheet_uri());
wp_enqueue_script('my-theme-script', get_template_directory_uri() . '/script.js');
// Optionally, enqueue additional stylesheets (if any)
// wp_enqueue_style('my-custom-style', get_template_directory_uri() . '/path/to/your/custom-style.css');
}
add_action('wp_enqueue_scripts', 'my_theme_enqueue_styles');
// CUSTOM POST TYPES
// Register a Custom Post Type called "Drawer"
function register_drawer_cpt() {
// Set up the labels for the Drawer CPT
$labels = array(
'name' => 'Drawers',
'singular_name' => 'Drawer',
'menu_name' => 'Drawers',
'name_admin_bar' => 'Drawer',
'add_new' => 'Add New',
'add_new_item' => 'Add New Drawer',
'new_item' => 'New Drawer',
'edit_item' => 'Edit Drawer',
'view_item' => 'View Drawer',
'all_items' => 'All Drawers',
'search_items' => 'Search Drawers',
'parent_item_colon' => 'Parent Drawers:',
'not_found' => 'No drawers found.',
'not_found_in_trash' => 'No drawers found in Trash.',
'featured_image' => 'Drawer Image',
'set_featured_image' => 'Set drawer image',
'remove_featured_image' => 'Remove drawer image',
'use_featured_image' => 'Use as drawer image',
'archives' => 'Drawer Archives',
'insert_into_item' => 'Insert into drawer',
'uploaded_to_this_item' => 'Uploaded to this drawer',
'filter_items_list' => 'Filter drawers list',
);
// Set up the arguments for the Drawer CPT
$args = array(
'labels' => $labels,
'public' => true, // Set this to false if you want it private
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'drawer' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false, // Set to true if you want the CPT to behave like pages
'menu_position' => 5,
'supports' => array( 'title', 'editor', 'thumbnail', 'custom-fields' ),
'taxonomies' => array(), // Optionally add taxonomies if needed
'show_in_rest' => true, // Enable for Gutenberg editor
);
// Register the Drawer CPT
register_post_type( 'drawer', $args );
}
// Hook into the 'init' action to register the CPT
add_action( 'init', 'register_drawer_cpt' );
/* 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_action('add_meta_boxes', 'add_key_points_meta_box');
// 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');
/* CUSTOM CODE PAGE */
function lcp_custom_dashboard_page() {
// Register the menu item for the admin page
add_menu_page(
'Custom Code Settings', // Page title
'Custom Code', // Menu title
'manage_options', // Required capability
'custom_code_settings', // Menu slug
'lcp_custom_code_page', // Callback function
'dashicons-admin-code', // Icon
100 // Position
);
}
add_action( 'admin_menu', 'lcp_custom_dashboard_page' );
// The callback function to display the page content
function lcp_custom_code_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' ); ?>
<table class="form-table">
<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>
<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">CSS in Footer</th>
<td><textarea name="custom_code_options[css_footer]" rows="10" cols="50"><?php echo esc_textarea( isset( $custom_code['css_footer'] ) ? $custom_code['css_footer'] : '' ); ?></textarea></td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
}
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) {
// 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>';
}
// Check and output CSS for footer
if ( isset( $custom_code['css_footer'] ) && ! empty( $custom_code['css_footer'] ) ) {
echo '<style type="text/css">' . $custom_code['css_footer'] . '</style>';
}
}
}
// 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');
});
/* ------------------------ POPUPS ------------------------------- */
/**
* Register the Custom Post Type (CPT) 'lcp-popups'
*/
function lcp_popups_custom_post_type() {
$labels = array(
'name' => 'LCP Popups',
'singular_name' => 'LCP Popup',
'menu_name' => 'LCP Popups',
'name_admin_bar' => 'LCP Popup',
'add_new' => 'Add New',
'add_new_item' => 'Add New Popup',
'new_item' => 'New Popup',
'edit_item' => 'Edit Popup',
'view_item' => 'View Popup',
'all_items' => 'All Popups',
'search_items' => 'Search Popups',
'not_found' => 'No popups found.',
'not_found_in_trash' => 'No popups found in Trash.',
'parent_item_colon' => '',
'menu_icon' => 'dashicons-lightbulb', // Optional: Set an icon
);
$args = array(
'labels' => $labels,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_admin_bar' => true,
'menu_position' => 20,
'supports' => array( 'title', 'editor' ), // You can add other supports like 'thumbnail', 'excerpt', etc.
'has_archive' => true,
'rewrite' => array( 'slug' => 'lcp-popups' ),
'show_in_rest' => true,
);
register_post_type( 'lcp-popup', $args );
}
add_action( 'init', 'lcp_popups_custom_post_type' );
/**
* Add a meta box with a toggle switch for "Show on All Pages"
*/
function lcp_popups_add_meta_box() {
add_meta_box(
'lcp_popups_toggle_meta_box', // Meta box ID
'Popup Settings', // Meta box title
'lcp_popups_meta_box_callback', // Callback function
'lcp_popups', // Post type
'side', // Context (normal, side, etc.)
'high' // Priority (default, low, high)
);
}
add_action( 'add_meta_boxes', 'lcp_popups_add_meta_box' );
/**
* Callback function to display the meta box content
*/
function lcp_popups_meta_box_callback( $post ) {
// Add a nonce for security
wp_nonce_field( 'lcp_popups_nonce_action', 'lcp_popups_nonce' );
// Get the current value of the toggle (default to false)
$show_on_all_pages = get_post_meta( $post->ID, '_lcp_popups_show_on_all_pages', true );
// Display the toggle switch (checkbox)
?>
<p>
<label for="lcp_popups_show_on_all_pages">
<input type="checkbox" id="lcp_popups_show_on_all_pages" name="lcp_popups_show_on_all_pages" value="1" <?php checked( $show_on_all_pages, 1 ); ?> />
Show on All Pages
</label>
</p>
<?php
}
/**
* Save the meta box data when the post is saved
*/
function lcp_popups_save_meta_box_data( $post_id ) {
// Check if our nonce is set
if ( ! isset( $_POST['lcp_popups_nonce'] ) ) {
return $post_id;
}
$nonce = $_POST['lcp_popups_nonce'];
// Verify that the nonce is valid
if ( ! wp_verify_nonce( $nonce, 'lcp_popups_nonce_action' ) ) {
return $post_id;
}
// Check if this is an autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
// Check if the current user has permission to edit the post
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
// Sanitize and save the value of the toggle
$show_on_all_pages = isset( $_POST['lcp_popups_show_on_all_pages'] ) ? 1 : 0;
update_post_meta( $post_id, '_lcp_popups_show_on_all_pages', $show_on_all_pages );
// If the toggle is checked, update the wp_options field
if ( $show_on_all_pages ) {
$popups = get_option( 'lcp_popups_every_page', [] );
$popups[] = $post_id; // Add the current post ID to the array
update_option( 'lcp_popups_every_page', array_unique( $popups ) ); // Save the updated array (remove duplicates)
} else {
// If unchecked, remove the post ID from the array
$popups = get_option( 'lcp_popups_every_page', [] );
$popups = array_filter( $popups, function( $id ) use ( $post_id ) {
return $id !== $post_id;
});
update_option( 'lcp_popups_every_page', array_values( $popups ) ); // Reindex the array
}
}
add_action( 'save_post', 'lcp_popups_save_meta_box_data' );
function lcp_display_popups_on_every_page() {
echo '<div class="lcp-popup">' . $content . '</div>';
// Get the array of post IDs from the wp_option
$post_ids = get_option('lcp_popups_every_page');
if (!empty($post_ids) && is_array($post_ids)) {
// Loop through the post IDs and get the content for each post
foreach ($post_ids as $post_id) {
// Make sure the post exists and get its content
$post = get_post($post_id);
if ($post) {
// Get the content and wrap it with a class
$content = apply_filters('the_content', $post->post_content);
// Output the popup content in the footer
echo '<div class="lcp-popup">' . $content . '</div>';
}
}
}
}
add_action('wp_footer', 'lcp_display_popups_on_every_page');
// 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' );