<?php
/**
 * Functions that enhance the Yaatra theme by integrating with WordPress hooks.
 *
 * These functions extend the theme's functionality and customize its behavior
 * by leveraging WordPress's action and filter hooks.
 *
 * @package Yaatra
 */

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Adds custom classes to the array of body classes.
 *
 * @param array $classes Classes for the body element.
 * @return array
 */
function yaatra_body_classes( $classes ) {
    global $post;

    // Adds a class of hfeed to non-singular pages.
    if ( !is_singular() ) {
        $classes[] = 'hfeed';
    }

    // Initialize sidebar meta option
    $sidebar_meta_option = '';

    /**
     * Sidebar option for post/page/archive
     *
     * @since 1.0.0
     */
    if ( is_singular( 'post' ) || is_singular( 'page' ) ) {
        $sidebar_meta_option = get_post_meta( $post->ID, 'yaatra_sidebar_layout', true );
    }

    if ( is_home() ) {
        $home_id                = get_option( 'page_for_posts' );
        $sidebar_meta_option    = get_post_meta( $home_id, 'yaatra_sidebar_layout', true );
    }

    // Set default sidebar options if the meta option is not set or if it's an archive or search page
    if ( empty( $sidebar_meta_option ) || is_archive() || is_search() ) {
        $sidebar_meta_option = 'default_sidebar';
    }

    $archive_sidebar        = yaatra_get_customizer_option_value( 'yaatra_archive_sidebar' );
    $post_default_sidebar   = yaatra_get_customizer_option_value( 'yaatra_global_post_sidebar' );
    $page_default_sidebar   = yaatra_get_customizer_option_value( 'yaatra_global_page_sidebar' );

    // Determine which sidebar class to add
    if ( $sidebar_meta_option === 'default_sidebar' ) {
        if ( is_single() ) {
            $classes[] = str_replace( '_', '-', $post_default_sidebar );
        } elseif ( is_page() ) {
            $classes[] = str_replace( '_', '-', $page_default_sidebar );
        } else {
            $classes[] = str_replace( '_', '-', $archive_sidebar );
        }
    } else {
        $classes[] = str_replace( '_', '-', $sidebar_meta_option );
    }

    // Archive layout class
    $yaatra_archive_layout = yaatra_get_customizer_option_value( 'yaatra_archive_layout' );
    if ( is_archive() || is_home() || is_search() ) {
        $classes[] = esc_attr( $yaatra_archive_layout ) . '-archive-layout';
    }

     /**
     * option for web site layout 
     */
    $yaatra_website_layout = esc_attr( get_theme_mod( 'yaatra_site_layout', 'fullwidth_layout' ) );
    
    if ( !empty( $yaatra_website_layout ) ) {
        $classes[] = $yaatra_website_layout;
    }

    return $classes;
}

add_filter( 'body_class', 'yaatra_body_classes' );

/**
 * Add a pingback url auto-discovery header for single posts, pages, or attachments.
 */
function yaatra_pingback_header() {
    if ( is_singular() && pings_open() ) {
        echo '<link rel="pingback" href="', esc_url( get_bloginfo( 'pingback_url' ) ), '">';
    }
}
add_action( 'wp_head', 'yaatra_pingback_header' );

/*--------------------------------------------------------------------------------*/

if ( ! function_exists( 'yaatra_fonts_url' ) ) :

    /**
     * Register Google Fonts for the Yaatra theme.
     *
     * This function returns the URL for the Google Fonts used in the theme, ensuring proper font loading.
     *
     * @return string Google Fonts URL for the theme.
     * @since 1.0.0
     */
    function yaatra_fonts_url() {
        $fonts_url = '';
        $font_families = array();

        /*
         * Translators: If there are characters in your language that are not supported
         * by Roboto Condensed, translate this to 'off'. Do not translate into your own language.
         */
        if ( 'off' !== _x( 'on', 'Playfair Display font: on or off', 'yaatra' ) ) {
            $font_families[] = 'Playfair Display:400,700';
        }

        if ( 'off' !== _x( 'on', 'Overpass font: on or off', 'yaatra' ) ) {
            $font_families[] = 'Overpass:300,400,600,700';
        }

        if ( 'off' !== _x( 'on', 'Pacifico font: on or off', 'yaatra' ) ) {
            $font_families[] = 'Pacifico:400';
        }

        if ( $font_families ) {
            $query_args = array(
                'family' => urlencode( implode( '|', $font_families ) ),
                'subset' => urlencode( 'latin,latin-ext' ),
            );

            $fonts_url = add_query_arg( $query_args, 'https://fonts.googleapis.com/css' );
        }

        return $fonts_url;
    }

endif;

/*--------------------------------------------------------------------------------*/
/**
 * Enqueue Scripts and styles for admin
 *
 * @since 1.0.0
 */
function yaatra_admin_scripts_style( $hook ) {
    global $yaatra_theme_version;

    if ( 'widgets.php' === $hook && 'edit.php' === $hook && 'post.php' === $hook && 'post-new.php' === $hook ) {
        return;
    }

    if ( function_exists( 'wp_enqueue_media' ) ) {
        wp_enqueue_media();
    }
    wp_enqueue_style( 'yaatra-admin-styles', get_template_directory_uri() .'/assets/css/cv-admin-styles.css', array(), esc_attr( $yaatra_theme_version ) );

    wp_enqueue_script( 'jquery-ui-button' );

    wp_enqueue_script( 'yaatra-admin-scripts', get_template_directory_uri() .'/assets/js/cv-admin-scripts.js', array('jquery'), esc_attr( $yaatra_theme_version ), true );
    
}
add_action( 'admin_enqueue_scripts', 'yaatra_admin_scripts_style' );

function yaatra_scripts() {

    global $yaatra_theme_version;

    wp_enqueue_style( 'yaatra-fonts', yaatra_fonts_url(), array(), null );

    wp_enqueue_style( 'font-awesome', get_template_directory_uri() . '/assets/library/font-awesome/css/all.min.css', array(), '6.5.1' );

    wp_enqueue_style( 'animate', get_template_directory_uri(). '/assets/library/animate/animate.min.css', array(), '3.5.1' );

    wp_enqueue_style( 'yaatra-style', get_stylesheet_uri(), array(), esc_attr( $yaatra_theme_version ) );

            $yaatra_preloader_enable = yaatra_get_customizer_option_value( 'yaatra_preloader_enable' );

            if ( false !== $yaatra_preloader_enable ) {
                wp_enqueue_style( 'yaatra-preloader', get_template_directory_uri(). '/assets/css/cv-preloader.css', array(), esc_attr( $yaatra_theme_version ) );
            }

   wp_enqueue_style( 'yaatra-responsive-style', get_template_directory_uri() . '/assets/css/cv-responsive.css', array(), esc_attr( $yaatra_theme_version ) );

    wp_enqueue_script( 'yaatra-navigation', get_template_directory_uri() . '/assets/js/navigation.js', array(), '20151215', true );

    wp_enqueue_script( 'yaatra-skip-link-focus-fix', get_template_directory_uri() . '/assets/js/skip-link-focus-fix.js', array(), '20151215', true );

    wp_enqueue_script( 'theia-sticky-sidebar', get_template_directory_uri() . '/assets/library/sticky-sidebar/theia-sticky-sidebar.min.js', array( 'jquery' ), '1.4.0', true );

    wp_enqueue_script( 'wow-scripts', get_template_directory_uri() . '/assets/library/wow/wow.min.js', array('jquery'), '1.1.3', true );

    wp_enqueue_script( 'yaatra-custom-scripts', get_template_directory_uri() . '/assets/js/cv-custom-scripts.js', array('jquery'), esc_attr( $yaatra_theme_version ), true );

    $yaatra_wow_option = yaatra_get_customizer_option_value( 'yaatra_wow_option' );
    if ( $yaatra_wow_option === true ) {
        $wow_value = 'on';
    } else {
        $wow_value = 'off';
    }

    wp_localize_script( 'yaatra-custom-scripts', 'yaatraObject', array(
        'wow_effect'     => $wow_value
    ) );

    if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
        wp_enqueue_script( 'comment-reply' );
    }
}
add_action( 'wp_enqueue_scripts', 'yaatra_scripts' );

/*--------------------------------------------------------------------------------*/

if ( ! function_exists( 'yaatra_social_icons_array' ) ) :
    
    /**
     * Define font awesome social media icons
     *
     * @return array();
     * @since 1.0.0
     */
    function yaatra_social_icons_array() {
        return array(
            "fab fa-facebook-square","fab fa-facebook-f","fab fa-facebook","fab fa-facebook-official","fab fa-twitter-square","fab fa-twitter","fab fa-yahoo","fab fa-google","fab fa-google-wallet","fab fa-google-plus-circle","fab fa-google-plus-official","fab fa-instagram","fab fa-linkedin-square","fab fa-linkedin","fab fa-pinterest-p","fab fa-pinterest","fab fa-pinterest-square","fab fa-google-plus-square","fab fa-google-plus","fab fa-youtube-square","fab fa-youtube","fab fa-youtube-play","fab fa-vimeo","fab fa-vimeo-square",
        );
    }

endif;

/*--------------------------------------------------------------------------------*/

if ( ! function_exists( 'yaatra_inner_header_bg_image' ) ) :

    /**
     * Background image for the inner page header.
     *
     * This image is displayed in the header area of inner pages.
     *
     * @since 1.0.0
     */
    function yaatra_inner_header_bg_image( $input ) {
        $image_attr = array();
        if ( empty( $image_attr ) ) {
            // Fetch from Custom Header Image.
            $image = get_header_image();
            if ( ! empty( $image ) ) {
                $image_attr['url']    = $image;
                $image_attr['width']  = get_custom_header()->width;
                $image_attr['height'] = get_custom_header()->height;
            }
        }

        if ( ! empty( $image_attr ) ) {
            $input .= 'background-image:url(' . esc_url( $image_attr['url'] ) . ');';
            $input .= 'background-size:cover;';
        }

        return $input;
    }

endif;

add_filter( 'yaatra_inner_header_style_attribute', 'yaatra_inner_header_bg_image' );

/*--------------------------------------------------------------------------*/

/**
 * Yaatra Dynamic styles
 */
add_action( 'wp_enqueue_scripts', 'yaatra_dynamic_styles' );

if ( ! function_exists( 'yaatra_dynamic_styles' ) ) :

    function yaatra_dynamic_styles() {
        $yaatra_theme_color = yaatra_get_customizer_option_value( 'yaatra_theme_color' );

        $output_css = '';
       
        $output_css .= ".preloader-wrapper .yaatra-wave .az-rect,
        .preloader-wrapper .yaatra-three-bounce .az-child,
        .preloader-wrapper .yaatra-folding-cube .az-cube:before, .reply .comment-reply-link,.widget_search .search-submit,.widget_search .search-submit,article.sticky:before,.widget_search .search-submit:hover, .widget-title::before, .widget-title::after, #site-navigation ul li a:hover, .site-header.widget_categories li a:hover, #site-navigation ul li a:hover, .widget_tag_cloud .tagcloud a:hover,.widget.widget_tag_cloud a:hover{ background: ". esc_attr( $yaatra_theme_color ) ."}\n";
        
 
        $output_css .= "a,a:hover,a:focus,a:active,.entry-footer a:hover,.comment-author .fn .url:hover,.commentmetadata .comment-edit-link,#cancel-comment-reply-link,#cancel-comment-reply-link:before,.logged-in-as a,.widget a:hover,.widget a:hover::before,.widget li:hover::before,.banner-btn a:hover,.entry-title a:hover,.entry-title a:hover,.cat-links a,.yaatra_latest_posts .cv-post-title a:hover, .entry-btn a:hover, h1.entry-title span, .cv-social-icons-wrapper a:hover, #footer-menu li a:hover, .menu-toggle:hover,.widget_tag_cloud a:hover, #cv-scrollup:hover,.search-no-results #primary .search-submit:hover,.banner-btn a:hover i{ color: ". esc_attr( $yaatra_theme_color ) ."}\n";

        $output_css .= ".widget_search .search-submit, .widget_search .search-submit:hover, .entry-btn a:hover, .widget_tag_cloud a:hover,.site-header .widget_categories li a:hover,.widget_tag_cloud .tagcloud a:hover,.widget.widget_tag_cloud a:hover{ border-color: ". esc_attr( $yaatra_theme_color ) ."}\n";

        $refine_output_css = yaatra_css_strip_whitespace( $output_css );
        wp_add_inline_style( 'yaatra-style', $refine_output_css );
    }

endif;

/*--------------------------------------------------------------------------------*/

if ( ! function_exists( 'yaatra_css_strip_whitespace' ) ) :
    
    /**
     * Retrieve minified CSS by removing whitespace and comments.
     *
     * This function optimizes CSS for improved loading performance.
     *
     * @since 1.0.0
     */
    function yaatra_css_strip_whitespace( $css ) {

        $replace = array(
            "#/\*.*?\*/#s" => "",  // Strip C style comments.
            "#\s\s+#"      => " ", // Strip excess whitespace.
        );
        $search = array_keys( $replace );
        $css = preg_replace( $search, $replace, $css );

        $replace = array(
            ": "  => ":",
            "; "  => ";",
            " {"  => "{",
            " }"  => "}",
            ", "  => ",",
            "{ "  => "{",
            ";}"  => "}", // Strip optional semicolons.
            ",\n" => ",", // Don't wrap multiple selectors.
            "\n}" => "}", // Don't wrap closing braces.
            "} "  => "}\n", // Put each rule on it's own line.
        );
        $search = array_keys( $replace );
        $css = str_replace( $search, $replace, $css );

        return trim( $css );
    }

endif;

if ( ! function_exists( 'yaatra_read_more_button' ) ) :
    
    /**
     * Function to apply a filter for customizing the "Read More" button label.
     *
     * Allows modification of the label text displayed on the "Read More" button.
     *
     * @since 1.0.0
     */
    function yaatra_read_more_button() {
        $button_label = apply_filters( 'yaatra_read_more_button_label', __( 'Continue Reading', 'yaatra' ) );

        echo '<div class="entry-btn"><a href="'. esc_url( get_the_permalink() ) .'">'.esc_html( $button_label ).'</a></div>';
    }

endif;

/*--------------------------------------------------------------------------------*/

add_filter( 'wp_kses_allowed_html', 'yaatra_required_data_attributes' , 10, 4 );

if ( ! function_exists( 'yaatra_required_data_attributes' ) ) :
    
    /**
     * Added required attributes while using wp_kses by using `wp_kses_allowed_html` filter.
     *
     * @since 1.0.5
     */
    function yaatra_required_data_attributes( $required_attributes, $context ) {

        $required_attributes['time']['class'] = true;
        $required_attributes['time']['datetime'] = true;

        return $required_attributes;
    }

endif;