<?php
/**
 * Custom Native Breadcrumbs
 *
 * @package One Elementor
 */

if ( ! function_exists( 'one_elementor_breadcrumb' ) ) {
	function one_elementor_breadcrumb() {
		// Define formatting
		$separator = '<span class="sep">/</span>';
		$home_title = esc_html__( 'Home', 'one-elementor' );
		
		// If it's the front page or home page, we only show "Home" if explicitly needed, but typically breadcrumbs aren't shown here.
		// If you want to show it, you'd output it. We'll only output if not front page.
		if ( ! is_front_page() ) {
			echo '<a href="' . esc_url( home_url( '/' ) ) . '" class="home">' . $home_title . '</a>' . $separator;

			if ( is_single() ) {
				// Single Post
				$categories = get_the_category();
				if ( ! empty( $categories ) ) {
					// Recursively get parent categories
					echo get_category_parents( $categories[0]->term_id, true, $separator );
				}
				echo '<span class="current">' . get_the_title() . '</span>';
				
			} elseif ( is_page() ) {
				// Page
				global $post;
				if ( $post->post_parent ) {
					$ancestors = get_post_ancestors( $post->ID );
					$ancestors = array_reverse( $ancestors );
					foreach ( $ancestors as $ancestor ) {
						echo '<a href="' . esc_url( get_permalink( $ancestor ) ) . '">' . get_the_title( $ancestor ) . '</a>' . $separator;
					}
				}
				echo '<span class="current">' . get_the_title() . '</span>';

			} elseif ( is_category() ) {
				// Category Archive
				$category = get_category( get_query_var( 'cat' ) );
				if ( $category->parent != 0 ) {
					echo get_category_parents( $category->parent, true, $separator );
				}
				echo '<span class="current">' . single_cat_title( '', false ) . '</span>';

			} elseif ( is_search() ) {
				// Search
				echo '<span class="current">' . esc_html__( 'Search Results for: ', 'one-elementor' ) . get_search_query() . '</span>';

			} elseif ( is_404() ) {
				// 404
				echo '<span class="current">' . esc_html__( 'Error 404', 'one-elementor' ) . '</span>';

			} elseif ( is_author() ) {
				// Author Archive
				global $author;
				$userdata = get_userdata( $author );
				echo '<span class="current">' . esc_html__( 'Articles posted by ', 'one-elementor' ) . $userdata->display_name . '</span>';

			} elseif ( is_day() ) {
				// Daily Archive
				echo '<a href="' . esc_url( get_year_link( get_the_time( 'Y' ) ) ) . '">' . get_the_time( 'Y' ) . '</a>' . $separator;
				echo '<a href="' . esc_url( get_month_link( get_the_time( 'Y' ), get_the_time( 'm' ) ) ) . '">' . get_the_time( 'F' ) . '</a>' . $separator;
				echo '<span class="current">' . get_the_time( 'd' ) . '</span>';

			} elseif ( is_month() ) {
				// Monthly Archive
				echo '<a href="' . esc_url( get_year_link( get_the_time( 'Y' ) ) ) . '">' . get_the_time( 'Y' ) . '</a>' . $separator;
				echo '<span class="current">' . get_the_time( 'F' ) . '</span>';

			} elseif ( is_year() ) {
				// Yearly Archive
				echo '<span class="current">' . get_the_time( 'Y' ) . '</span>';

			} elseif ( is_tag() ) {
				// Tag Archive
				echo '<span class="current">' . esc_html__( 'Posts tagged "', 'one-elementor' ) . single_tag_title( '', false ) . '"</span>';

			} elseif ( is_archive() ) {
				// Generic Archive
				echo '<span class="current">' . get_the_archive_title() . '</span>';
			}
		}
	}
}
