<?php
/**
 * PageSpark WP Theme — Functions
 *
 * Minimal, fast, Bootstrap 5 theme with offcanvas nav and Customizer support.
 * Designed to work seamlessly with the PageSpark WP plugin.
 *
 * @package WPBS_Theme
 */

if (!defined('ABSPATH')) exit;

define('WPBS_THEME_VERSION', '1.1.0');
define('WPBS_THEME_DIR', get_template_directory());
define('WPBS_THEME_URI', get_template_directory_uri());

// Theme includes
require_once WPBS_THEME_DIR . '/inc/enqueue.php';
require_once WPBS_THEME_DIR . '/inc/customizer.php';
require_once WPBS_THEME_DIR . '/inc/widgets.php';
require_once WPBS_THEME_DIR . '/inc/navwalker.php';
require_once WPBS_THEME_DIR . '/inc/extras.php';

/**
 * Theme setup
 */
function wpbs_theme_setup() {
    // Textdomain
    load_theme_textdomain('wpbs-theme', WPBS_THEME_DIR . '/languages');

    // Theme support
    add_theme_support('title-tag');
    add_theme_support('post-thumbnails');
    add_theme_support('html5', ['search-form', 'comment-form', 'comment-list', 'gallery', 'caption', 'style', 'script']);
    add_theme_support('custom-logo', [
        'height'      => 100,
        'width'       => 300,
        'flex-height' => true,
        'flex-width'  => true,
    ]);
    add_theme_support('custom-background');
    add_theme_support('customize-selective-refresh-widgets');
    add_theme_support('responsive-embeds');
    add_theme_support('align-wide');

    // Nav menus
    register_nav_menus([
        'primary'   => esc_html__('Primary Menu', 'wpbs-theme'),
        'footer'    => esc_html__('Footer Menu', 'wpbs-theme'),
    ]);

    // Image sizes
    set_post_thumbnail_size(1200, 630, true);
    add_image_size('wpbs-hero', 1920, 1080, true);
    add_image_size('wpbs-card', 600, 400, true);
}
add_action('after_setup_theme', 'wpbs_theme_setup');

/**
 * Remove unnecessary WordPress bloat for speed
 */
function wpbs_cleanup_head() {
    remove_action('wp_head', 'rsd_link');
    remove_action('wp_head', 'wlwmanifest_link');
    remove_action('wp_head', 'wp_generator');
    remove_action('wp_head', 'wp_shortlink_wp_head');
    remove_action('wp_head', 'rest_output_link_wp_head');
    remove_action('wp_head', 'wp_oembed_add_discovery_links');
    remove_action('wp_head', 'wp_oembed_add_host_js');
    remove_action('wp_head', 'print_emoji_detection_script', 7);
    remove_action('wp_print_styles', 'print_emoji_styles');
    remove_action('admin_print_scripts', 'print_emoji_detection_script');
    remove_action('admin_print_styles', 'print_emoji_styles');

    // Remove block library CSS on frontend if not using Gutenberg blocks
    if (!is_admin()) {
        wp_dequeue_style('wp-block-library');
        wp_dequeue_style('wp-block-library-theme');
        wp_dequeue_style('wc-blocks-style');
        wp_dequeue_style('classic-theme-styles');
        wp_dequeue_style('global-styles');
    }
}
add_action('init', 'wpbs_cleanup_head');
add_action('wp_enqueue_scripts', function() {
    wp_dequeue_style('wp-block-library');
    wp_dequeue_style('wp-block-library-theme');
    wp_dequeue_style('classic-theme-styles');
    wp_dequeue_style('global-styles');
}, 100);

/**
 * Disable Gutenberg block editor for pages (we use raw HTML)
 */
function wpbs_disable_gutenberg_pages($use_block_editor, $post) {
    if ($post->post_type === 'page') {
        // Check if Bootstrap Studio is enabled for this page
        $wpbs_enabled = get_post_meta($post->ID, '_wpbs_enabled', true);
        if ($wpbs_enabled) {
            return false; // Use classic editor for WPBS pages
        }
    }
    return $use_block_editor;
}
add_filter('use_block_editor_for_post', 'wpbs_disable_gutenberg_pages', 10, 2);

/**
 * Allow unfiltered HTML for admins (needed for Bootstrap components)
 */
function wpbs_allow_unfiltered_html($caps, $cap, $user_id) {
    if ($cap === 'unfiltered_html') {
        $caps = ['unfiltered_html' => true];
    }
    return $caps;
}
add_filter('map_meta_cap', 'wpbs_allow_unfiltered_html', 1, 3);

/**
 * Allow iframes in post content (Google Maps, YouTube, etc.)
 */
function wpbs_allow_iframes_in_kses($tags, $context) {
    if ($context === 'post') {
        $tags['iframe'] = [
            'src'             => true,
            'width'           => true,
            'height'          => true,
            'frameborder'     => true,
            'allowfullscreen' => true,
            'allow'           => true,
            'loading'         => true,
            'referrerpolicy'  => true,
            'style'           => true,
            'class'           => true,
            'id'              => true,
            'title'           => true,
            'name'            => true,
            'sandbox'         => true,
        ];
    }
    return $tags;
}
add_filter('wp_kses_allowed_html', 'wpbs_allow_iframes_in_kses', 10, 2);

/**
 * Preserve HTML comments in post content (Classic Editor strips them).
 * Save raw content before kses filtering, restore comments after.
 */
function wpbs_preserve_html_comments($data, $postarr) {
    if (!current_user_can('unfiltered_html')) return $data;
    // Let the raw content through — unfiltered_html cap handles security
    if (isset($postarr['ID']) && $postarr['ID'] > 0) {
        $wpbs_enabled = get_post_meta($postarr['ID'], '_wpbs_enabled', true);
        if ($wpbs_enabled) {
            remove_filter('content_save_pre', 'wp_filter_post_kses');
        }
    }
    return $data;
}
add_filter('wp_insert_post_data', 'wpbs_preserve_html_comments', 1, 2);

/**
 * Don't auto-wrap content in <p> tags for WPBS pages
 */
function wpbs_disable_wpautop() {
    if (is_singular('page')) {
        $wpbs_enabled = get_post_meta(get_the_ID(), '_wpbs_enabled', true);
        if ($wpbs_enabled) {
            remove_filter('the_content', 'wpautop');
            remove_filter('the_content', 'wptexturize');
        }
    }
}
add_action('template_redirect', 'wpbs_disable_wpautop');
