<?php
/**
 * Functions which enhance the theme by hooking into WordPress
 *
 * @package Zentorone
 */

/**
 * Adds custom classes to the array of body classes.
 *
 * @param array $classes Classes for the body element.
 * @return array
 */
function zentorone_body_classes( $classes ) {
	// Adds a class of hfeed to non-singular pages.
	if ( ! is_singular() ) {
		$classes[] = 'hfeed';
	}

    // Add layout class
    $layout = zentorone_get_option( 'zentorone_global_layout', 'right-sidebar' );
    $classes[] = 'layout-' . esc_attr( $layout );

    // Add sticky header class
    if ( zentorone_get_option( 'zentorone_sticky_header', false ) ) {
        $classes[] = 'has-sticky-header';
    }

	return $classes;
}
add_filter( 'body_class', 'zentorone_body_classes' );

/**
 * Display Breadcrumbs
 */
function zentorone_breadcrumbs() {
    if ( ! zentorone_get_option( 'zentorone_breadcrumbs_enabled', true ) ) {
        return;
    }

    if ( is_front_page() || is_home() ) {
        return;
    }

    echo '<nav aria-label="breadcrumb" class="zentorone-breadcrumbs my-3">';
    echo '<ol class="breadcrumb bg-light p-2 px-3 rounded">';
    echo '<li class="breadcrumb-item"><a href="' . esc_url( home_url( '/' ) ) . '">' . esc_html__( 'Home', 'zentorone' ) . '</a></li>';

    if ( class_exists( 'WooCommerce' ) && ( is_woocommerce() || is_cart() || is_checkout() || is_account_page() ) ) {
        if ( is_shop() ) {
            echo '<li class="breadcrumb-item active" aria-current="page">' . esc_html( woocommerce_page_title( false ) ) . '</li>';
        } elseif ( is_product() ) {
            $shop_page_id = wc_get_page_id( 'shop' );
            echo '<li class="breadcrumb-item"><a href="' . esc_url( get_permalink( $shop_page_id ) ) . '">' . esc_html( get_the_title( $shop_page_id ) ) . '</a></li>';
            echo '<li class="breadcrumb-item active" aria-current="page">' . esc_html( get_the_title() ) . '</li>';
        } elseif ( is_product_category() || is_product_tag() ) {
            $shop_page_id = wc_get_page_id( 'shop' );
            echo '<li class="breadcrumb-item"><a href="' . esc_url( get_permalink( $shop_page_id ) ) . '">' . esc_html( get_the_title( $shop_page_id ) ) . '</a></li>';
            echo '<li class="breadcrumb-item active" aria-current="page">' . esc_html( single_term_title( '', false ) ) . '</li>';
        } else {
            echo '<li class="breadcrumb-item active" aria-current="page">' . esc_html( get_the_title() ) . '</li>';
        }
    } elseif ( is_category() || is_single() ) {
        echo '<li class="breadcrumb-item">';
        the_category( ' </li><li class="breadcrumb-item"> ' );
        if ( is_single() ) {
            echo '</li><li class="breadcrumb-item active" aria-current="page">';
            the_title();
            echo '</li>';
        } else {
            echo '</li>';
        }
    } elseif ( is_page() ) {
        echo '<li class="breadcrumb-item active" aria-current="page">';
        the_title();
        echo '</li>';
    } elseif ( is_archive() ) {
        echo '<li class="breadcrumb-item active" aria-current="page">';
        $title = get_the_archive_title();
        if ( strpos( $title, ': ' ) !== false ) {
            $title = explode( ': ', $title )[1];
        }
        echo esc_html( $title );
        echo '</li>';
    } elseif ( is_search() ) {
        echo '<li class="breadcrumb-item active" aria-current="page">' . esc_html__( 'Search results', 'zentorone' ) . '</li>';
    }

    echo '</ol>';
    echo '</nav>';
}
/**
 * Get Theme Mod with JSON fallback
 */
function zentorone_get_option( $name, $default = '' ) {
    return get_theme_mod( $name, $default );
}

/**
 * Get Column Classes based on Layout
 */
function zentorone_column_classes( $side = 'main' ) {
    $layout = zentorone_get_option( 'zentorone_global_layout', 'right-sidebar' );

    if ( 'full-width' === $layout ) {
        return 'col-12';
    }

    if ( 'no-sidebar' === $layout ) {
        return 'col-lg-8 mx-auto';
    }

    if ( 'main' === $side ) {
        return 'col-lg-8';
    } else {
        return 'col-lg-4';
    }
}

/**
 * Menu Fallback
 */
function zentorone_menu_fallback() {
    $args = array(
        'title_li'     => '',
        'echo'         => false,
        'number'       => 5,
    );
    
    $pages = wp_list_pages( $args );
    
    // Add Bootstrap classes to page list items and links manually for the fallback
    $pages = str_replace( '<li class="', '<li class="nav-item ', $pages );
    $pages = str_replace( '<a href="', '<a class="nav-link" href="', $pages );
    
    echo '<ul class="navbar-nav ms-auto mb-2 mb-lg-0">' . $pages . '</ul>';
}
