/**
 * NFIS Mortgage Theme
 *
 * @package     nfis-theme
 * @author      NFIS Mortgage Brokers
 * @license     GPL-2.0-or-later
 * @version     1.0.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

if ( ! defined( 'NFIS_THEME_VERSION' ) ) {
	define( 'NFIS_THEME_VERSION', '1.0.0' );
}
if ( ! defined( 'NFIS_THEME_DIR' ) ) {
	define( 'NFIS_THEME_DIR', get_template_directory() );
}
if ( ! defined( 'NFIS_THEME_URI' ) ) {
	define( 'NFIS_THEME_URI', get_template_directory_uri() );
}

/**
 * Theme setup.
 */
if ( ! function_exists( 'nfis_theme_setup' ) ) :
function nfis_theme_setup() {
	load_theme_textdomain( 'nfis-mortgage', NFIS_THEME_DIR . '/languages' );

	add_theme_support( 'automatic-feed-links' );
	add_theme_support( 'title-tag' );
	add_theme_support( 'post-thumbnails' );
	add_theme_support( 'custom-logo', array(
		'height'      => 60,
		'width'       => 200,
		'flex-height' => true,
		'flex-width'  => true,
	) );
	add_theme_support( 'html5', array(
		'search-form',
		'comment-form',
		'comment-list',
		'gallery',
		'caption',
		'script',
		'style',
	) );
	add_theme_support( 'wp-block-styles' );
	add_theme_support( 'responsive-embeds' );
	add_theme_support( 'align-wide' );
	add_theme_support( 'editor-styles' );
	add_editor_style( 'assets/css/editor-style.css' );

	register_nav_menus( array(
		'primary'  => __( 'Primary Menu', 'nfis-mortgage' ),
		'footer'   => __( 'Footer Menu', 'nfis-mortgage' ),
		'mobile'   => __( 'Mobile Menu', 'nfis-mortgage' ),
	) );

	// Image sizes.
	add_image_size( 'nfis-featured', 1200, 630, true );
	add_image_size( 'nfis-blog-thumb', 400, 260, true );
	add_image_size( 'nfis-team', 400, 400, true );
}
add_action( 'after_setup_theme', 'nfis_theme_setup' );
endif;

/**
 * Enqueue scripts and styles.
 */
if ( ! function_exists( 'nfis_theme_scripts' ) ) :
function nfis_theme_scripts() {
	// Google Fonts.
	$fonts_url = add_query_arg( array(
		'family' => urlencode( 'Spectral:ital,wght@0,500;0,600;0,700;1,500;1,600&Source+Sans+3:wght@400;500;600;700' ),
		'display' => 'swap',
	), 'https://fonts.googleapis.com/css' );

	wp_enqueue_style( 'nfis-fonts', esc_url( $fonts_url ), array(), NFIS_THEME_VERSION );
	wp_style_add_data( 'nfis-fonts', 'rtl', 'replace' );

	// Main stylesheet.
	wp_enqueue_style( 'nfis-style', get_stylesheet_uri(), array(), NFIS_THEME_VERSION );

	// Theme JavaScript.
	wp_enqueue_script( 'nfis-main', NFIS_THEME_URI . '/assets/js/main.js', array(), NFIS_THEME_VERSION, true );
	wp_script_add_data( 'nfis-main', 'async', true );

	// FAQ JavaScript (only on pages with FAQ).
	if ( is_page() ) {
		global $post;
		$post_content = $post ? $post->post_content : '';
		if ( strpos( $post_content, 'faq__item' ) !== false || has_block( 'nfis-mortgage/faq-accordion', $post ) ) {
			wp_enqueue_script( 'nfis-faq', NFIS_THEME_URI . '/assets/js/faq.js', array(), NFIS_THEME_VERSION, true );
		}
	}

	// Comment reply script.
	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
		wp_enqueue_script( 'comment-reply' );
	}
}
add_action( 'wp_enqueue_scripts', 'nfis_theme_scripts' );
endif;

/**
 * Register widget areas.
 */
if ( ! function_exists( 'nfis_theme_widgets_init' ) ) :
function nfis_theme_widgets_init() {
	register_sidebar( array(
		'name'          => __( 'Sidebar', 'nfis-mortgage' ),
		'id'            => 'sidebar-1',
		'description'   => __( 'Add widgets here to appear in your sidebar.', 'nfis-mortgage' ),
		'before_widget' => '<section id="%1$s" class="widget %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<h3 class="widget-title">',
		'after_title'   => '</h3>',
	) );

	register_sidebar( array(
		'name'          => __( 'Footer', 'nfis-mortgage' ),
		'id'            => 'footer-1',
		'description'   => __( 'Add widgets here to appear in your footer.', 'nfis-mortgage' ),
		'before_widget' => '<section id="%1$s" class="widget %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<h3 class="widget-title">',
		'after_title'   => '</h3>',
	) );
}
add_action( 'widgets_init', 'nfis_theme_widgets_init' );
endif;

/**
 * Breadcrumb navigation.
 */
if ( ! function_exists( 'nfis_breadcrumbs' ) ) :
function nfis_breadcrumbs() {
	if ( is_front_page() ) {
		return;
	}

	$home = __( 'Home', 'nfis-mortgage' );
	$delimiter = '<span class="breadcrumb__sep" aria-hidden="true">/</span>';

	echo '<nav class="breadcrumb" aria-label="' . esc_attr__( 'Breadcrumb', 'nfis-mortgage' ) . '">';
	echo '<div class="container">';
	echo '<a href="' . esc_url( home_url( '/' ) ) . '">' . esc_html( $home ) . '</a>' . $delimiter;

	if ( is_singular() ) {
		$categories = get_the_category();
		if ( ! empty( $categories ) ) {
			$category = $categories[0];
			echo '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '">' . esc_html( $category->name ) . '</a>' . $delimiter;
		}
		echo '<span class="breadcrumb__current">' . esc_html( get_the_title() ) . '</span>';
	} elseif ( is_archive() ) {
		echo '<span class="breadcrumb__current">' . esc_html( get_the_archive_title() ) . '</span>';
	} elseif ( is_search() ) {
		echo '<span class="breadcrumb__current">' . __( 'Search Results', 'nfis-mortgage' ) . '</span>';
	} elseif ( is_404() ) {
		echo '<span class="breadcrumb__current">' . __( '404 Not Found', 'nfis-mortgage' ) . '</span>';
	}

	echo '</div></nav>';
}
endif;

/**
 * Return business info for footer.
 */
if ( ! function_exists( 'nfis_get_business_info' ) ) :
function nfis_get_business_info() {
	return array(
		'name'         => 'NFIS Mortgage Brokers Pty Ltd',
		'abn'          => '49 666 814 768',
		'acl'          => 'Authorised under QED Credit Services Pty Ltd ACL 387856',
		'phone'        => '0405 069 441',
		'email'        => 'james.galliers@nfis.net.au',
		'address'      => '6-42 Commerce Ave, Rivervale, WA, 6112',
	);
}
endif;

/**
 * Reduce embed size heights to match block width.
 */
if ( ! function_exists( 'nfis_theme_embed_dimensions' ) ) :
function nfis_theme_embed_dimensions( $dimensions ) {
	$dimensions[0] = 1280;
	$dimensions[1] = 720;
	return $dimensions;
}
add_filter( 'embed_dimensions', 'nfis_theme_embed_dimensions', 20 );
endif;

/**
 * Add custom image sizes attribute for responsive images.
 */
if ( ! function_exists( 'nfis_theme_responsive_image_sizes' ) ) :
function nfis_theme_responsive_image_sizes( $sizes, $size ) {
	$width = $size[0];

	if ( $width < 640 ) {
		$sizes = '(max-width: ' . $width . 'px) 100vw, ' . $width . 'px';
	} elseif ( $width <= 1200 ) {
		$sizes = '(max-width: 768px) 100vw, (max-width: 1200px) 50vw, ' . $width . 'px';
	} else {
		$sizes = '(max-width: 768px) 100vw, (max-width: 1024px) 50vw, (max-width: 1200px) 40vw, ' . $width . 'px';
	}

	return $sizes;
}
add_filter( 'wp_calculate_image_sizes', 'nfis_theme_responsive_image_sizes', 10, 2 );
endif;

/**
 * Include template functions.
 */
require get_template_directory() . '/inc/template-tags.php';
require get_template_directory() . '/inc/template-functions.php';<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="https://testinsil987.insil.dev/wp-sitemap-index.xsl" ?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><sitemap><loc>https://testinsil987.insil.dev/wp-sitemap-posts-post-1.xml</loc></sitemap><sitemap><loc>https://testinsil987.insil.dev/wp-sitemap-posts-page-1.xml</loc></sitemap><sitemap><loc>https://testinsil987.insil.dev/wp-sitemap-taxonomies-category-1.xml</loc></sitemap><sitemap><loc>https://testinsil987.insil.dev/wp-sitemap-users-1.xml</loc></sitemap></sitemapindex>
