<?php
/**
 * Kipya functions and definitions.
 *
 * @link https://urreno.org
 *
 * @package Kipya
 */

// Buffer output to prevent header errors
ob_start();

set_time_limit(300);

if (!function_exists('kipya_setup')) :
    function kipya_setup() {
        load_theme_textdomain('kipya', get_template_directory() . '/languages');
        add_theme_support('automatic-feed-links');
        add_theme_support('custom-logo');
        add_theme_support('title-tag');
        add_theme_support('post-thumbnails');
        
        register_nav_menus(array(
            'primary'   => esc_html__('Primary Menu', 'kipya'),
            'top-menu'  => esc_html__('Top Menu', 'kipya'),
            'sidebar'   => esc_html__('Sidebar Menu', 'kipya'),
            'footer'    => esc_html__('Footer Menu', 'kipya'),
        ));

        add_theme_support('kipya', array(
            'search-form',
            'comment-form',
            'comment-list',
            'gallery',
            'caption',
        ));

        add_theme_support('custom-background', apply_filters('kipya_custom_background_args', array(
            'default-color' => 'ffffff',
            'default-image' => '',
        )));

        add_theme_support('customize-selective-refresh-widgets');
        add_theme_support('post-formats', array('aside', 'gallery', 'quote', 'image', 'video'));
    }
endif;
add_action('after_setup_theme', 'kipya_setup');

function kipya_styles() {
    wp_register_style('kipya-style', get_template_directory_uri() . '/style.css', array(), '1.0.3', 'all');
    wp_register_style('custom', get_template_directory_uri() . '/assets/css/custom.css', array(), '1.0.4', 'all');
    wp_register_style('animate', get_template_directory_uri() . '/assets/css/animate.css', array(), '4.1.1', 'all');
    wp_register_style('bootstrap.min', get_template_directory_uri() . '/assets/vendor/bootstrap/css/bootstrap.min.css', array(), '5.3.2', 'all');
    wp_register_style('bootstrap-icons', get_template_directory_uri() . '/assets/vendor/bootstrap-icons/font/bootstrap-icons.min.css', array(), '1.11.3', 'all');
    wp_register_style('aos', get_template_directory_uri() . '/assets/vendor/aos/aos.css', array(), '2.3.4', 'all');
    wp_register_style('swiper', get_template_directory_uri() . '/assets/vendor/swiper/swiper.min.css', array(), '5.4.5', 'all');
    wp_register_style('toastr.min', get_template_directory_uri() . '/assets/vendor/toastr/toastr.min.css', array(), '2.1.3', 'all');
    wp_register_style('datatables.min', get_template_directory_uri() . '/assets/vendor/DataTables/datatables.min.css', array(), '1.13.3', 'all');

    wp_enqueue_style('kipya-style');
    wp_enqueue_style('custom');
    wp_enqueue_style('bootstrap.min');
    wp_enqueue_style('bootstrap-icons');
    wp_enqueue_style('aos');
    wp_enqueue_style('swiper');
    wp_enqueue_style('toastr.min');
    wp_enqueue_style('datatables.min');
}
add_action('wp_enqueue_scripts', 'kipya_styles');

function kipya_scripts() {
    // Load jQuery first (before other scripts that depend on it)
    wp_enqueue_script('jquery-script', get_template_directory_uri() . '/assets/vendor/jquery/jquery-3.7.1.min.js', array(), '3.7.1', 'all');
    
    // Load libraries in dependency order
    wp_enqueue_script('bootstrap-script', get_template_directory_uri() . '/assets/vendor/bootstrap/js/bootstrap.bundle.min.js', array('jquery-script'), '4.5.2', 'all');
    wp_enqueue_script('aos-script', get_template_directory_uri() . '/assets/vendor/aos/aos.js', array('jquery-script'), '2.3.4', 'all');
    wp_enqueue_script('swiper-script', get_template_directory_uri() . '/assets/vendor/swiper/swiper.min.js', array('jquery-script'), '5.4.5', 'all');
    wp_enqueue_script('toastr-script', get_template_directory_uri() . '/assets/vendor/toastr/toastr.min.js', array('jquery-script'), '2.1.3', 'all');
    wp_enqueue_script('datatables.min', get_template_directory_uri() . '/assets/vendor/DataTables/datatables.min.js', array('jquery-script'), '1.13.3', 'all');
    
    // Load main scripts that depend on jQuery
    wp_enqueue_script('kipya-script', get_template_directory_uri() . '/assets/js/main.js', array('jquery-script', 'swiper-script'), '1.0.1', 'all');
    wp_enqueue_script('search-script', get_template_directory_uri() . '/assets/js/search.js', array('jquery-script'), '1.0.1', 'all');

    wp_localize_script('search-script', 'kipya_ajax_object', array(
        'ajaxurl' => admin_url('admin-ajax.php'),
    ));
}
add_action('wp_footer', 'kipya_scripts');

function enqueue_custom_scripts() {
    // REMOVED: Lightbox scripts that were conflicting with Swiper slider
    // wp_enqueue_style('lightbox-css', 'https://cdnjs.cloudflare.com/ajax/libs/lightbox2/2.11.3/css/lightbox.min.css');
    // wp_enqueue_script('lightbox-js', 'https://cdnjs.cloudflare.com/ajax/libs/lightbox2/2.11.3/js/lightbox.min.js', array('jquery'), null, true);
    // wp_enqueue_script('custom-gallery-lightbox', get_template_directory_uri() . '/assets/js/gallery-lightbox.js', array('jquery'), null, true);

    if (is_page('projects-page')) {
        wp_enqueue_script('custom-ajax-script', get_template_directory_uri() . '/assets/js/custom-ajax-script.js', array('jquery'), null, true);
        wp_localize_script('custom-ajax-script', 'ajax_object', array('ajax_url' => admin_url('admin-ajax.php')));
    }
}
add_action('wp_enqueue_scripts', 'enqueue_custom_scripts');

if (function_exists('register_sidebar')) {
    register_sidebar(array(
        'name' => __('Widget Area 1', 'kipya'),
        'description' => __('Description for this widget-area...', 'kipya'),
        'id' => 'widget-area-1',
        'before_widget' => '<div id="%1$s" class="%2$s">',
        'after_widget' => '</div>',
        'before_title' => '<h3>',
        'after_title' => '</h3>'
    ));

    register_sidebar(array(
        'name' => __('Widget Area 2', 'kipya'),
        'description' => __('Description for this widget-area...', 'kipya'),
        'id' => 'widget-area-2',
        'before_widget' => '<div id="%1$s" class="%2$s">',
        'after_widget' => '</div>',
        'before_title' => '<h3>',
        'after_title' => '</h3>'
    ));
    
    register_sidebar(array(
        'name' => esc_html__('Top Left', 'kipya'),
        'id' => 'topl',
        'description' => esc_html__('Add widgets here.', 'kipya'),
        'before_widget' => '<section id="%1$s" class="widget %2$s">',
        'after_widget' => '</section>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ));
    
    register_sidebar(array(
        'name' => esc_html__('Top Right', 'kipya'),
        'id' => 'topr',
        'description' => esc_html__('Add widgets here.', 'kipya'),
        'before_widget' => '<section id="%1$s" class="widget %2$s">',
        'after_widget' => '</section>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ));
    
    register_sidebar(array(
        'name' => esc_html__('Search Area', 'kipya'),
        'id' => 'searchi',
        'description' => esc_html__('Add widgets here.', 'kipya'),
        'before_widget' => '<section id="%1$s" class="widget %2$s">',
        'after_widget' => '</section>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ));
    
    register_sidebar(array(
        'name' => esc_html__('Quick Number', 'kipya'),
        'id' => 'inno',
        'description' => esc_html__('Add widgets here.', 'kipya'),
        'before_widget' => '<section id="%1$s" class="widget %2$s">',
        'after_widget' => '</section>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ));
    
    register_sidebar(array(
        'name' => esc_html__('Menu Right', 'kipya'),
        'id' => 'nav-right',
        'description' => esc_html__('Add widgets here.', 'kipya'),
        'before_widget' => '<section id="%1$s" class="widget %2$s">',
        'after_widget' => '</section>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ));
    
    register_sidebar(array(
        'name' => __('Services Sidebar', 'kipya'),
        'description' => __('Description for this widget-area...', 'kipya'),
        'id' => 'servsidebar',
        'before_widget' => '<div id="%1$s" class="%2$s">',
        'after_widget' => '</div>',
        'before_title' => '<h3>',
        'after_title' => '</h3>'
    ));
    
    register_sidebar(array(
        'name' => esc_html__('Advert Top', 'kipya'),
        'id' => 'advtop',
        'description' => esc_html__('Add widgets here.', 'kipya'),
        'before_widget' => '<section id="%1$s" class="widget %2$s">',
        'after_widget' => '</section>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ));
    
    register_sidebar(array(
        'name' => esc_html__('Newsletter', 'kipya'),
        'id' => 'footernewz',
        'description' => esc_html__('Add widgets here.', 'kipya'),
        'before_widget' => '<section id="%1$s" class="widget %2$s">',
        'after_widget' => '</section>',
        'before_title' => '<h4 class="widget-title">',
        'after_title' => '</h4>',
    ));
    
    register_sidebar(array(
        'name' => esc_html__('Footer Starter', 'kipya'),
        'id' => 'footeri',
        'description' => esc_html__('Add widgets here.', 'kipya'),
        'before_widget' => '<section id="%1$s" class="widget %2$s">',
        'after_widget' => '</section>',
        'before_title' => '<h4 class="widget-title">',
        'after_title' => '</h4>',
    ));
    
    register_sidebar(array(
        'name' => esc_html__('Footer A', 'kipya'),
        'id' => 'footera',
        'description' => esc_html__('Add widgets here.', 'kipya'),
        'before_widget' => '<section id="%1$s" class="widget %2$s">',
        'after_widget' => '</section>',
        'before_title' => '<h4 class="widget-title">',
        'after_title' => '</h4>',
    ));
    
    register_sidebar(array(
        'name' => esc_html__('Footer B', 'kipya'),
        'id' => 'footerb',
        'description' => esc_html__('Add widgets here.', 'kipya'),
        'before_widget' => '<section id="%1$s" class="widget %2$s">',
        'after_widget' => '</section>',
        'before_title' => '<h4 class="widget-title">',
        'after_title' => '</h4>',
    ));
    
    register_sidebar(array(
        'name' => esc_html__('Footer C', 'kipya'),
        'id' => 'footerc',
        'description' => esc_html__('Add widgets here.', 'kipya'),
        'before_widget' => '<section id="%1$s" class="widget %2$s">',
        'after_widget' => '</section>',
        'before_title' => '<h4 class="widget-title">',
        'after_title' => '</h4>',
    ));
    
    register_sidebar(array(
        'name' => esc_html__('Footer D', 'kipya'),
        'id' => 'footerd',
        'description' => esc_html__('Add widgets here.', 'kipya'),
        'before_widget' => '<section id="%1$s" class="widget %2$s">',
        'after_widget' => '</section>',
        'before_title' => '<h4 class="widget-title">',
        'after_title' => '</h4>',
    ));
    
    register_sidebar(array(
        'name' => esc_html__('Copyright Links', 'kipya'),
        'id' => 'footer-links',
        'description' => esc_html__('Add widgets here.', 'kipya'),
        'before_widget' => '<section id="%1$s" class="widget %2$s">',
        'after_widget' => '</section>',
        'before_title' => '<h4 class="widget-title">',
        'after_title' => '</h4>',
    ));
}

function enable_page_excerpts() {
    add_post_type_support('page', 'excerpt');
}
add_action('init', 'enable_page_excerpts');

function load_custom_single_template($template) {
    if (is_single() && in_category('photo-gallery')) {
        $new_template = locate_template(array('single-gallery.php'));
        if ($new_template) {
            return $new_template;
        }
    }
    if (is_singular('events')) {
        $eve_template = locate_template(array('single-event.php'));
        if ($eve_template) {
            return $eve_template;
        }
    }
    return $template;
}
add_filter('single_template', 'load_custom_single_template');

function force_gallery_links_to_media($block_content, $block) {
    if ($block['blockName'] === 'core/gallery' && !empty($block['innerBlocks'])) {
        foreach ($block['innerBlocks'] as &$innerBlock) {
            if ($innerBlock['blockName'] === 'core/image' && isset($innerBlock['attrs']['id'])) {
                $image_id = $innerBlock['attrs']['id'];
                $image_url = wp_get_attachment_url($image_id);
                if ($image_url) {
                    $innerBlock['attrs']['linkDestination'] = 'media';
                    $innerBlock['attrs']['href'] = $image_url;
                }
            }
        }
    }
    return $block_content;
}
add_filter('render_block', 'force_gallery_links_to_media', 10, 2);

require_once get_template_directory() . '/inc/bootstrap-5-wordpress-navbar-walker.php';
require_once get_template_directory() . '/inc/custom-nav-walker.php';
require_once get_template_directory() . '/inc/seo-post-types.php';
require_once get_template_directory() . '/inc/ajax-search.php';
require_once get_template_directory() . '/inc/slides-post-types.php';
require_once get_template_directory() . '/inc/add_page_category.php';
require_once get_template_directory() . '/inc/downloads-post-types.php';
require_once get_template_directory() . '/inc/youtube-playlist.php';
require_once get_template_directory() . '/inc/partners-post-types.php';
require_once get_template_directory() . '/inc/aos-animation.php';
require_once get_template_directory() . '/inc/breadcrumbs.php';
require_once get_template_directory() . '/inc/accordion.php';
require_once get_template_directory() . '/inc/all_news-shortcode.php';
require_once get_template_directory() . '/inc/duplicate-page.php';
require_once get_template_directory() . '/inc/publications.php';
require_once get_template_directory() . '/inc/all_partners-page.php';
require_once get_template_directory() . '/inc/homeservices.php';

function allow_video_featured_image($mime_types) {
    $mime_types['mp4'] = 'video/mp4';
    $mime_types['webm'] = 'video/webm';
    $mime_types['ogg'] = 'video/ogg';
    return $mime_types;
}
add_filter('upload_mimes', 'allow_video_featured_image');

add_action('back_button', 'kipya_back_button');
function kipya_back_button() {
    if (wp_get_referer()) {
        $back_text = __('&laquo; BACK');
        $button = "\n<button id='my-back-button' class='btn button my-back-button' onclick='javascript:history.back()'>$back_text</button>";
        echo ($button);
    }
}

function html5wp_pagination() {
    global $wp_query;
    $big = 999999999;
    echo paginate_links(array(
        'base' => str_replace($big, '%#%', get_pagenum_link($big)),
        'format' => '?paged=%#%',
        'current' => max(1, get_query_var('paged')),
        'total' => $wp_query->max_num_pages
    ));
}

function html5wp_index($length) {
    return 50;
}

function html5wp_custom_post($length) {
    return 50;
}

function enable_svg_support($mimes) {
    $mimes['svg'] = 'image/svg+xml';
    return $mimes;
}
add_filter('upload_mimes', 'enable_svg_support');

function html5wp_excerpt($length_callback = '', $more_callback = '') {
    global $post;
    if (function_exists($length_callback)) {
        add_filter('excerpt_length', $length_callback);
    }
    if (function_exists($more_callback)) {
        add_filter('excerpt_more', $more_callback);
    }
    $output = get_the_excerpt();
    $output = apply_filters('wptexturize', $output);
    $output = apply_filters('convert_chars', $output);
    $output = '<p>' . $output . '</p>';
    echo $output;
}

function html5_blank_view_article($more) {
    global $post;
    return '... <a class="view-article" href="' . get_permalink($post->ID) . '">' . __('Read More', 'kipya') . '</a>';
}

function remove_admin_bar() {
    return false;
}
add_filter('show_admin_bar', 'remove_admin_bar');

function html5_style_remove($tag) {
    return preg_replace('~\s+type=["\'][^"\']++["\']~', '', $tag);
}

function remove_thumbnail_dimensions($html) {
    $html = preg_replace('/(width|height)=\"\d*\"\s/', "", $html);
    return $html;
}


function rotary_enqueue_font_awesome() {
    wp_enqueue_style('font-awesome', 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css', array(), '6.4.0');
}
add_action('wp_enqueue_scripts', 'rotary_enqueue_font_awesome');

function mytheme_enqueue_scroll_effect() {
    wp_enqueue_script('fadeup-effect', get_template_directory_uri() . 'assets/js/main.js', array(), null, true);
}
add_action('wp_enqueue_scripts', 'mytheme_enqueue_scroll_effect');

add_action('wp_ajax_nopriv_live_search', 'live_search_callback');
add_action('wp_ajax_live_search', 'live_search_callback');

function live_search_callback() {
    $term = isset($_GET['term']) ? sanitize_text_field($_GET['term']) : '';
    $args = array(
        's' => $term,
        'post_status' => 'publish',
        'posts_per_page' => 5
    );
    $query = new WP_Query($args);
    if ($query->have_posts()) {
        echo '<ul class="list-unstyled m-0">';
        while ($query->have_posts()) {
            $query->the_post();
            echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
        }
        echo '</ul>';
    } else {
        echo '<p class="m-0">No results found.</p>';
    }
    wp_die();
}

function custom_pre_get_posts($query) {
    if (!is_admin() && $query->is_main_query()) {
        if (!is_front_page() && is_page()) {
            $query->set('paged', get_query_var('paged') ? get_query_var('paged') : 1);
        }
    }
}
add_action('pre_get_posts', 'custom_pre_get_posts');



function mwe_enqueue_youtube_api_and_scripts() {
    wp_enqueue_script('mwe-youtube-iframe-api', 'https://www.youtube.com/iframe_api', array(), null, true);
    wp_enqueue_script('mwe-video-js', plugin_dir_url(__FILE__) . 'assets/mwe-video.js', array('jquery', 'mwe-youtube-iframe-api'), '1.0', true);
}
add_action('wp_enqueue_scripts', 'mwe_enqueue_youtube_api_and_scripts');

// Flush output buffer at the end
add_action('shutdown', function() {
    if (ob_get_level() > 0) {
        ob_end_flush();
    }
}, 999);



add_action('wp_enqueue_scripts', function () {
    wp_enqueue_style(
        'swiper-css',
        'https://cdn.jsdelivr.net/npm/swiper@11/swiper-bundle.min.css',
        [],
        null
    );
    wp_enqueue_script(
        'swiper-js',
        'https://cdn.jsdelivr.net/npm/swiper@11/swiper-bundle.min.js',
        [],
        null,
        true
    );
});




