<?php
/**
 * HaPantheon theme functions.
 *
 * @package HaPantheon
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

define( 'HAPANTHEON_VERSION', '2.1.0' );
define( 'HAPANTHEON_DIR', get_template_directory() );
define( 'HAPANTHEON_URI', get_template_directory_uri() );

require_once HAPANTHEON_DIR . '/inc/template-tags.php';
require_once HAPANTHEON_DIR . '/inc/customizer.php';
require_once HAPANTHEON_DIR . '/inc/meta.php';
require_once HAPANTHEON_DIR . '/inc/forms.php';
require_once HAPANTHEON_DIR . '/inc/importer.php';
require_once HAPANTHEON_DIR . '/inc/blocks.php';
require_once HAPANTHEON_DIR . '/inc/source-blocks.php';
require_once HAPANTHEON_DIR . '/inc/block-styles.php';
require_once HAPANTHEON_DIR . '/inc/patterns.php';

/**
 * Register theme supports and menu locations.
 */
function hapantheon_setup() {
	load_theme_textdomain( 'hapantheon', HAPANTHEON_DIR . '/languages' );

	add_theme_support( 'automatic-feed-links' );
	add_theme_support( 'title-tag' );
	add_theme_support( 'post-thumbnails' );
	add_theme_support( 'responsive-embeds' );
	add_theme_support( 'align-wide' );
	add_theme_support( 'wp-block-styles' );
	add_theme_support( 'editor-styles' );
	add_theme_support( 'customize-selective-refresh-widgets' );
	add_theme_support(
		'html5',
		array(
			'search-form',
			'comment-form',
			'comment-list',
			'gallery',
			'caption',
			'style',
			'script',
			'navigation-widgets',
		)
	);
	add_theme_support(
		'custom-logo',
		array(
			'height'      => 120,
			'width'       => 420,
			'flex-height' => true,
			'flex-width'  => true,
		)
	);
	add_theme_support(
		'custom-background',
		array(
			'default-color' => 'F6F1E3',
		)
	);

	add_post_type_support( 'page', 'excerpt' );

	register_nav_menus(
		array(
			'primary'            => __( 'Primary Menu', 'hapantheon' ),
			'footer_navigation'  => __( 'Footer — Navigation', 'hapantheon' ),
			'footer_halls'       => __( 'Footer — Halls / Categories', 'hapantheon' ),
			'footer_information' => __( 'Footer — Information and Legal', 'hapantheon' ),
			'footer_legal'       => __( 'Footer — Bottom Legal Links', 'hapantheon' ),
		)
	);

	add_image_size( 'hapantheon-card', 900, 620, true );
	add_image_size( 'hapantheon-hero', 1800, 1040, true );

	add_editor_style(
		array(
			'assets/css/pantheon.css',
			'assets/css/wordpress.css',
			'assets/css/editor.css',
		)
	);
}
add_action( 'after_setup_theme', 'hapantheon_setup' );

/**
 * Set the content width for embeds and media.
 */
function hapantheon_content_width() {
	$GLOBALS['content_width'] = apply_filters( 'hapantheon_content_width', 1240 );
}
add_action( 'after_setup_theme', 'hapantheon_content_width', 0 );

/**
 * Enqueue public assets.
 */
function hapantheon_enqueue_assets() {
	$load_fonts = (bool) get_theme_mod( 'hapantheon_load_google_fonts', false );

	if ( $load_fonts ) {
		wp_enqueue_style(
			'hapantheon-fonts',
			'https://fonts.googleapis.com/css2?family=Noto+Sans+Hebrew:wght@400;500;600;700;900&family=Poppins:wght@400;500;600;700&family=Outfit:wght@400;500;600;700&display=swap',
			array(),
			null
		);
	}

	wp_enqueue_style(
		'hapantheon-design',
		HAPANTHEON_URI . '/assets/css/pantheon.css',
		$load_fonts ? array( 'hapantheon-fonts' ) : array(),
		HAPANTHEON_VERSION
	);
	wp_enqueue_style(
		'hapantheon-wordpress',
		HAPANTHEON_URI . '/assets/css/wordpress.css',
		array( 'hapantheon-design' ),
		HAPANTHEON_VERSION
	);

	$custom_css = hapantheon_get_customizer_css();
	if ( $custom_css ) {
		wp_add_inline_style( 'hapantheon-wordpress', $custom_css );
	}

	wp_enqueue_script(
		'hapantheon',
		HAPANTHEON_URI . '/assets/js/pantheon.js',
		array(),
		HAPANTHEON_VERSION,
		true
	);
	wp_script_add_data( 'hapantheon', 'strategy', 'defer' );
	wp_localize_script(
		'hapantheon',
		'hapantheonData',
		array(
			'homeUrl'          => home_url( '/' ),
			'archiveUrl'       => hapantheon_get_archive_url(),
			'accessibilityUrl' => hapantheon_get_page_url_by_slug( 'accessibility' ),
			'copySuccess'      => __( 'הקישור הועתק', 'hapantheon' ),
		)
	);

	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
		wp_enqueue_script( 'comment-reply' );
	}
}
add_action( 'wp_enqueue_scripts', 'hapantheon_enqueue_assets' );

/**
 * Add useful body classes.
 *
 * @param array $classes Existing classes.
 * @return array
 */
function hapantheon_body_classes( $classes ) {
	$classes[] = 'hapantheon-theme';
	$classes[] = 'hapantheon-direction-' . get_theme_mod( 'hapantheon_direction', 'rtl' );

	if ( is_front_page() ) {
		$classes[] = 'hapantheon-front-page';
	}
	if ( is_singular( 'post' ) ) {
		$classes[] = 'hapantheon-single';
	}

	return $classes;
}
add_filter( 'body_class', 'hapantheon_body_classes' );

/**
 * Load the bundled favicon when WordPress Site Icon is not configured.
 */
function hapantheon_fallback_site_icon() {
	if ( has_site_icon() ) {
		return;
	}
	?>
	<link rel="icon" href="<?php echo esc_url( HAPANTHEON_URI . '/assets/images/favicon.svg' ); ?>" type="image/svg+xml">
	<link rel="icon" href="<?php echo esc_url( HAPANTHEON_URI . '/assets/images/favicon-32.png' ); ?>" sizes="32x32" type="image/png">
	<link rel="apple-touch-icon" href="<?php echo esc_url( HAPANTHEON_URI . '/assets/images/favicon-192.png' ); ?>">
	<?php
}
add_action( 'wp_head', 'hapantheon_fallback_site_icon', 3 );

/**
 * Use a restrained title separator.
 *
 * @return string
 */
function hapantheon_document_title_separator() {
	return '—';
}
add_filter( 'document_title_separator', 'hapantheon_document_title_separator' );
