<?php
/**
 * WordPress Nav Menu With Bootstrap Fallback
 *
 * Provides a fallback navigation menu that lists all published pages
 * when no custom menu is assigned in WordPress.
 *
 * @package    Wira
 * @since      1.0.0
 */

namespace Wira;

if ( ! class_exists( 'Menu_Fallback' ) ) {

	/**
	 * Menu_Fallback Class.
	 *
	 * Renders a Bootstrap-compatible navigation menu using all published pages
	 * as fallback when no custom menu is set.
	 *
	 * @since 1.0.0
	 */
	class Menu_Fallback {

		/**
		 * Render fallback menu.
		 *
		 * Outputs a Bootstrap-compatible <ul> with all published pages
		 * as menu items.
		 *
		 * @since 1.0.0
		 * @return void
		 */
		public static function render() {
			// Get all published pages ordered by menu order and title.
			$pages = get_pages(
				array(
					'sort_column' => 'menu_order, post_title',
					'post_status' => 'publish',
				)
			);

			echo '<ul class="navbar-nav mb-2 mb-lg-0">';

			// Loop through each page and render as menu item.
			foreach ( $pages as $page ) {
				// Mark current page as active.
				$active = ( get_queried_object_id() === $page->ID ) ? ' current-menu-item' : '';

				echo '<li class="nav-item' . esc_attr( $active ) . '">';
				echo '<a class="nav-link" href="' . esc_url( get_permalink( $page->ID ) ) . '">';
				echo esc_html( $page->post_title );
				echo '</a>';
				echo '</li>';
			}

			echo '</ul>';
		}
	}
}
