<?php
/*
 * Legal Breadcrumbs
 */
global $legal_options;

function legal_custom_breadcrumbs() {

    $legal_showonhome = 0; // 1 - show breadcrumbs on the homepage, 0 - don't show
    $legal_showcurrent = 1; // 1 - show current post/page title in breadcrumbs, 0 - don't show
     

    global $post;
    if (is_home() || is_front_page()) {

        if ($legal_showonhome == 1)
            echo '<ol class="breadcrumb breadcrumb-menubar"><li><a href="' . esc_url(home_url('/')) . '">' . esc_html__('Home', 'legal') . '</a></li></ol>';
    } else {

        echo '<ol class="breadcrumb breadcrumb-menubar"><li><a href="' . esc_url(home_url('/')). '">' . esc_html__('Home', 'legal') . '</a> ';
        if (is_category()) {
            $legal_thisCat = get_category(get_query_var('cat'), false);
            if ($legal_thisCat->parent != 0)
                echo get_category_parents($legal_thisCat->parent, TRUE, ' ');
            echo  esc_html__('Archive by category', 'legal') . ' " ' . single_cat_title('', false) . ' "';
        } elseif (is_search()) {
            echo  esc_html__('Search Results For ', 'legal') . ' " ' . get_search_query() . ' "';
        } elseif (is_day()) {
            echo '<a href="' . esc_url(get_year_link(get_the_time('Y'))) . '">' . esc_html(get_the_time('Y')) . '</a> ';
            echo '<a href="' . esc_url(get_month_link(get_the_time('Y'), get_the_time('m'))) . '">' . esc_html(get_the_time('F') ). '</a> ';
            echo  esc_html(get_the_time('d'));
        } elseif (is_month()) {
            echo '<a href="' . esc_url(get_year_link(get_the_time('Y'))) . '">' . esc_html(get_the_time('Y')) . '</a> ';
            echo  esc_html(get_the_time('F')) ;
        } elseif (is_year()) {
            echo esc_html(get_the_time('Y')) ;
        } elseif (is_single() && !is_attachment()) {
            if (get_post_type() != 'post') {
                $legal_post_type = get_post_type_object(get_post_type());
                $legal_slug = $legal_post_type->rewrite;
                echo '<a href="' . esc_url(home_url('/'. $legal_slug['slug'] . '/')) .'">' . esc_html($legal_post_type->labels->singular_name) . '</a>';
                if ($legal_showcurrent == 1)
                    echo  esc_html(get_the_title()) ;
            } else {
                $legal_cat = get_the_category();
                $legal_cat = $legal_cat[0];
                $legal_cats = get_category_parents($legal_cat, TRUE, ' ');
                if ($legal_showcurrent == 0)
                    $legal_cats =
                            preg_replace("#^(.+)\s\s$#", "$1", $legal_cats);
                echo $legal_cats;
                if ($legal_showcurrent == 1)
                    echo  get_the_title();
            }
        } elseif (!is_single() && !is_page() && get_post_type() != 'post' && !is_404()) {
            $legal_post_type = get_post_type_object(get_post_type());
            echo esc_html($legal_post_type->labels->singular_name );
        } elseif (is_attachment()) {
            $legal_parent = get_post($post->post_parent);
            $legal_cat = get_the_category($legal_parent->ID);
            $legal_cat = $legal_cat[0];
            echo get_category_parents($legal_cat, TRUE, '  ');
            echo '<a href="' . esc_url(get_permalink($legal_parent)) . '">' . esc_html($legal_parent->post_title) . '</a>';
            if ($legal_showcurrent == 1)
                echo   esc_html(get_the_title());
        } elseif (is_page() && !$post->post_parent) {
            if ($legal_showcurrent == 1)
                echo esc_html(get_the_title());
        } elseif (is_page() && $post->post_parent) {
            $legal_parent_id = $post->post_parent;
            $legal_breadcrumbs = array();
            while ($legal_parent_id) {
                $legal_page = get_page($legal_parent_id);
                $legal_breadcrumbs[] = '<a href="' . get_permalink($legal_page->ID) . '">' . get_the_title($legal_page->ID) . '</a>';
                $legal_parent_id = $legal_page->post_parent;
            }
            $legal_breadcrumbs = array_reverse($legal_breadcrumbs);
            for ($legal_i = 0; $legal_i < count($legal_breadcrumbs); $legal_i++) {
                echo $legal_breadcrumbs[$legal_i];
                if ($legal_i != count($legal_breadcrumbs) - 1)
                    echo ' ';
            }
            if ($legal_showcurrent == 1)
                echo get_the_title();
        } elseif (is_tag()) {
            echo  esc_html__('Posts tagged', 'legal') . ' " ' . esc_html(single_tag_title('', false)) . ' "';
        } elseif (is_author()) {
            global $author;
            $legal_userdata = get_userdata($author);
            echo  esc_html__('Articles Published by', 'legal') . ' " ' . esc_html($legal_userdata->display_name ). ' "';
        } elseif (is_404()) {
            echo esc_html__('Error 404', 'legal') ;
        }

        if (get_query_var('paged')) {
            if (is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author())
                echo ' (';
            echo esc_html__('Page', 'legal') . ' ' . esc_html(get_query_var('paged'));
            if (is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author())
                echo ')';
        }

        echo '</li></ol>';
    }
} 