<?php
/**
 * Breadcrumbs.
 *
 * @package MadeByAura\Prime\Helpers
 * @author  MadeByAura.com
 */

namespace MadeByAura\Prime\Helpers;

defined( 'ABSPATH' ) || die();

/**
 * Breadcrumbs.
 */
class Breadcrumbs {
	/**
	 * Arguments.
	 *
	 * @var array  Arguments.
	 */
	protected $args = [];

	/**
	 * Breadcrumbs.
	 *
	 * @var array  Breadcrumbs.
	 */
	protected $crumbs = [];

	/**
	 * Constructor.
	 *
	 * @param array $args  Arguments.
	 */
	public function __construct( $args = [] ) {
		$this->args = array_replace_recursive(
			[
				'home_text' => __( 'Home', 'aura-prime' ),
				'class'     => '',
			],
			$args,
		);

		$this->set_crumbs();
	}

	/**
	 * Have posts.
	 *
	 * @access public
	 * @return bool
	 */
	public function have_crumbs() {
		return 2 <= count( $this->crumbs ) ? true : false;
	}

	/**
	 * Render crumbs.
	 */
	public function render_crumbs() {
		$last = count( $this->crumbs ) - 1;

		// Classes.
		$classes[] = $this->args['class'];
		$classes[] = 'aura-breadcrumbs';
		?>
			<div class="<?php Markup::echo_classes( $classes ); ?>">
				<?php foreach ( $this->crumbs as $index => $crumb ) : ?>
					<div class="aura-breadcrumbs__item">
						<?php if ( $index !== $last ) : ?>
							<a class="aura-breadcrumbs__item-link" href="<?php echo esc_url( $crumb['url'] ); ?>">
						<?php endif; ?>

						<?php echo esc_html( $crumb['text'] ); ?>

						<?php if ( $index !== $last ) : ?>
							</a>
						<?php endif; ?>
					</div>
				<?php endforeach; ?>
			</div>
		<?php
	}

	/**
	 * Set crumbs.
	 */
	protected function set_crumbs() {
		if ( is_front_page() ) {
			$this->add_home();

		} elseif ( is_home() ) {
			$this->add_home();
			$this->add_post_ancestors();
			$this->add_posts_page();

		} elseif ( is_singular( 'post' ) ) {
			$this->add_home();
			$this->add_posts_page();
			$this->add_post();

		} elseif ( is_singular() ) {
			$this->add_home();
			$this->add_post_ancestors();
			$this->add_post();

		} elseif ( is_category() || is_tag() ) {
			$this->add_home();
			$this->add_posts_page();
			$this->add_term_ancestors();
			$this->add_term();

		} elseif ( is_tax() ) {
			$this->add_home();
			$this->add_term_ancestors();
			$this->add_term();
		}
	}

	/**
	 * Add home.
	 */
	protected function add_home() {
		$this->crumbs[] = [
			'text' => esc_html( $this->args['home_text'] ),
			'url'  => home_url(),
		];
	}

	/**
	 * Add post.
	 */
	protected function add_post() {
		$this->crumbs[] = [
			'text' => get_the_title(),
			'url'  => get_permalink(),
		];
	}

	/**
	 * Add post ancestors.
	 */
	protected function add_post_ancestors() {
		$post      = get_queried_object();
		$ancestors = get_ancestors( $post->ID, $post->post_type );

		// Do not proceed if there are no ancestors.
		if ( ! $ancestors ) {
			return;
		}

		// Add crumbs for post ancestors.
		foreach ( array_reverse( $ancestors ) as $ancestor ) {
			$this->crumbs[] = [
				'text' => get_the_title( $ancestor ),
				'url'  => get_permalink( $ancestor ),
			];
		}
	}

	/**
	 * Add post page.
	 */
	protected function add_posts_page() {
		if ( 'page' === get_option( 'show_on_front' ) ) {
			$this->crumbs[] = [
				'text' => get_the_title( get_option( 'page_for_posts' ) ),
				'url'  => get_permalink( get_option( 'page_for_posts' ) ),
			];
		} else {
			$this->crumbs[] = [
				'text' => get_post_type_object( 'post' )->label,
				'url'  => home_url(),
			];
		}
	}

	/**
	 * Add term.
	 */
	protected function add_term() {
		$term = get_queried_object();

		$this->crumbs[] = [
			'text' => $term->name,
			'url'  => get_term_link( $term->term_id, $term->taxonomy ),
		];
	}

	/**
	 * Add term ancestors.
	 */
	protected function add_term_ancestors() {
		$term      = get_queried_object();
		$ancestors = get_ancestors( $term->term_id, $term->taxonomy );

		// Do not proceed if there are no ancestors.
		if ( ! $ancestors ) {
			return;
		}

		// Add crumbs for term ancestor.
		foreach ( array_reverse( $ancestors ) as $ancestor ) {
			$this->crumbs[] = [
				'text' => get_term( $ancestor, $term->taxonomy )->name,
				'url'  => get_term_link( $ancestor, $term->taxonomy ),
			];
		}
	}
}
