<?php
/**
 * Theme custom hooks and markup callbacks.
 *
 * @package Eronex
 * @since   0.0.1
 */
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Adds the Eronex theme body class.
 *
 * Hooked to `body_class`.
 *
 * @since 0.0.1
 *
 * @param string[] $classes CSS classes applied to the body element.
 * @return string[] Filtered body classes.
 */
function eronex_theme_body_class( $classes ) {
	$classes[] = 'eronex-theme';

	return $classes;
}

add_filter( 'body_class', 'eronex_theme_body_class' );

/**
 * Outputs the skip-to-content accessibility link markup.
 *
 * Hooked to `eronex_skip_to_content`.
 *
 * @since 0.0.1
 *
 * @return void
 */
function eronex_skip_to_content_markup() {
	?>
	<a class="skip-link screen-reader-text" href="#eronex-content">
		<?php echo esc_html__( 'Skip to content', 'eronex' ); ?>
	</a>
	<?php
}

add_action( 'eronex_skip_to_content', 'eronex_skip_to_content_markup' );

/**
 * Outputs the theme preloader markup.
 *
 * Hooked to `eronex_preloader`.
 *
 * @since 0.0.1
 *
 * @return void
 */
function eronex_preloader_markup() {
	get_template_part( 'template-parts/preloader' );
}

add_action( 'eronex_preloader', 'eronex_preloader_markup' );

/**
 * Outputs the theme header markup.
 *
 * Hooked to `eronex_header`.
 *
 * @since 0.0.1
 *
 * @return void
 */
function eronex_header_markup() {
	get_template_part( 'template-parts/header' );
}

add_action( 'eronex_header', 'eronex_header_markup' );

/**
 * Outputs the theme footer markup.
 *
 * Hooked to `eronex_footer`.
 *
 * @since 0.0.1
 *
 * @return void
 */
function eronex_footer_markup() {
	get_template_part( 'template-parts/footer' );
}

add_action( 'eronex_footer', 'eronex_footer_markup' );


/**
 * Outputs the scroll-to-top button markup.
 *
 * Hooked to `eronex_content_bottom`.
 *
 * @since 0.0.1
 *
 * @return void
 */
function eronex_scroll_top_markup() {
	get_template_part( 'template-parts/scroll-top' );
}

add_action( 'eronex_content_bottom', 'eronex_scroll_top_markup' );


/**
 * Outputs the page banner markup when enabled for the current context.
 *
 * Hooked to `eronex_banner`. Respects theme options and per-post meta overrides.
 *
 * @since 0.0.1
 *
 * @return void
 */
function eronex_banner_markup() {

	$banner_enable = eronex_option( 'eronex_banner_enable', true );

	if ( eronex_is_woocommerce_archive() ) {
		$banner_enable = eronex_option( 'eronex_shop_archive_banner_enable', $banner_enable );
	} elseif ( is_front_page() || is_archive() ) {
		$banner_enable = eronex_option( 'eronex_archive_banner_enable', $banner_enable );
	} elseif ( is_page() ) {
		$banner_enable = eronex_option( 'eronex_page_banner_enable', $banner_enable );
	} elseif ( eronex_is_woocommerce_single() ) {
		$banner_enable = eronex_option( 'eronex_product_single_banner_enable', $banner_enable );
	} elseif ( is_singular() ) {
		$banner_enable = eronex_option( 'eronex_single_banner_enable', $banner_enable );
	} elseif ( is_404() ) {
		$banner_enable = eronex_option( 'eronex_not_found_enable', $banner_enable );
	}

	if ( is_singular() ) {
		$meta_banner = get_post_meta( get_the_ID(), 'eronex_banner', true );
		if ( ! empty( $meta_banner ) ) {
			$banner_enable = 'yes' === $meta_banner;
		}
	}

	if ( ! $banner_enable ) {
		return;
	}

	get_template_part( 'template-parts/banner' );
}

add_action( 'eronex_banner', 'eronex_banner_markup' );

/**
 * Outputs the breadcrumb trail markup.
 *
 * Hooked to `eronex_breadcrumb`.
 *
 * @since 0.0.1
 *
 * @return void
 */
function eronex_breadcrumb_markup() {
	get_template_part( 'template-parts/breadcrumb' );
}
add_action( 'eronex_breadcrumb', 'eronex_breadcrumb_markup' );

/**
 * Registers the theme widget areas.
 *
 * Hooked to `widgets_init`. Registers the main sidebar and, when WooCommerce
 * is active, the product sidebar.
 *
 * @since 0.0.1
 *
 * @return void
 */
function eronex_widgets_init() {

	$default_args = array(
		'before_widget' => '<div id="%1$s" class="widget %2$s">',
		'after_widget'  => '</div>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	);

	/**
	 * Filters the main sidebar registration arguments.
	 *
	 * @since 0.0.1
	 *
	 * @param array $args Sidebar arguments passed to register_sidebar().
	 */
	register_sidebar(
		apply_filters(
			'eronex_main_widgets_init',
			array(
				'name' => esc_html__( 'Main Sidebar', 'eronex' ),
				'id'   => 'sidebar-1',
			) + $default_args
		)
	);

	if ( class_exists( 'WooCommerce' ) ) {
		/**
		 * Filters the product sidebar registration arguments.
		 *
		 * @since 0.0.1
		 *
		 * @param array $args Sidebar arguments passed to register_sidebar().
		 */
		register_sidebar(
			apply_filters(
				'eronex_product_widgets_init',
				array(
					'name' => esc_html__( 'Product Sidebar', 'eronex' ),
					'id'   => 'product-sidebar',
				) + $default_args
			)
		);
	}
}

add_action( 'widgets_init', 'eronex_widgets_init' );

/**
 * Loads the theme sidebar template.
 *
 * Hooked to `eronex_sidebar`.
 *
 * @since 0.0.1
 *
 * @return void
 */
function eronex_get_sidebar() {
	get_sidebar();
}

add_action( 'eronex_sidebar', 'eronex_get_sidebar' );

/**
 * Uses the product sidebar on WooCommerce shop and product pages.
 *
 * Hooked to `eronex_get_sidebar`.
 *
 * @since 0.0.1
 *
 * @param string $sidebar_id Sidebar ID.
 * @return string Filtered sidebar ID.
 */
function eronex_woocommerce_sidebar_id( $sidebar_id ) {
	if ( eronex_is_woocommerce_archive() || eronex_is_woocommerce_single() ) {
		return 'product-sidebar';
	}

	return $sidebar_id;
}

add_filter( 'eronex_get_sidebar', 'eronex_woocommerce_sidebar_id' );

/**
 * Outputs single post category list markup.
 *
 * Hooked to `eronex_post_categories`.
 *
 * @since 0.0.1
 *
 * @return void
 */
function eronex_post_categories_markup() {
	$categories = get_the_category_list( ', ' );

	if ( empty( $categories ) ) {
		return;
	}
	?>
	<div class="eronex-post-categories">
		<?php echo wp_kses_post( $categories ); ?>
	</div>
	<?php
}

add_action( 'eronex_post_categories', 'eronex_post_categories_markup' );

/**
 * Outputs single post title markup.
 *
 * Hooked to `eronex_post_title`.
 *
 * @since 0.0.1
 *
 * @return void
 */
function eronex_post_title_markup() {
	?>
	<h2 class="eronex-post-title"><?php the_title(); ?></h2>
	<?php
}

add_action( 'eronex_post_title', 'eronex_post_title_markup' );

/**
 * Outputs single post meta markup (author, comments, date).
 *
 * Hooked to `eronex_post_meta`.
 *
 * @since 0.0.1
 *
 * @return void
 */
function eronex_post_meta_markup() {
	?>
	<ul class="eronex-post-meta">
		<li class="eronex-post-meta eronex-post-author-name">
			<?php echo esc_html__( 'By', 'eronex' ); ?>
			<a href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ), get_the_author_meta( 'user_nicename' ) ) ); ?>">
				<?php echo esc_html( get_the_author() ); ?>
			</a>
		</li>

		<li class="eronex-post-meta eronex-post-commnets">
			<a href="<?php comments_link(); ?>">
				<?php
				comments_number(
					esc_html__( 'No comments', 'eronex' ),
					esc_html__( '1 comment', 'eronex' ),
					esc_html__( '% comments', 'eronex' )
				);
				?>
			</a>
		</li>

		<li class="eronex-post-meta eronex-post-date">
			<?php the_time( get_option( 'date_format' ) ); ?>
		</li>
	</ul>
	<?php
}

add_action( 'eronex_post_meta', 'eronex_post_meta_markup' );

/**
 * Outputs single post featured image markup.
 *
 * Hooked to `eronex_post_thumbnail`.
 *
 * @since 0.0.1
 *
 * @return void
 */
function eronex_post_thumbnail_markup() {
	if ( ! has_post_thumbnail() ) {
		return;
	}
	?>
	<figure class="eronex-post-thumbnail">
		<?php the_post_thumbnail( 'large' ); ?>
	</figure>
	<?php
}

add_action( 'eronex_post_thumbnail', 'eronex_post_thumbnail_markup' );

/**
 * Outputs single post content markup.
 *
 * Hooked to `eronex_post_content`.
 *
 * @since 0.0.1
 *
 * @return void
 */
function eronex_post_content_markup() {
	?>
	<div class="eronex-post-content">
		<?php the_content(); ?>
	</div>
	<?php
}

add_action( 'eronex_post_content', 'eronex_post_content_markup' );

/**
 * Outputs single post tags markup.
 *
 * Hooked to `eronex_post_tags`.
 *
 * @since 0.0.1
 *
 * @return void
 */
function eronex_post_tags_markup() {
	$tags = get_the_tag_list();

	if ( empty( $tags ) ) {
		return;
	}
	?>
	<div class="eronex-post-tags-wrapper">
		<h6 class="eronex-post-tag-title">
			<?php echo esc_html__( 'Tags:', 'eronex' ); ?>
		</h6>
		<div class="eronex-post-tags">
			<?php echo wp_kses_post( $tags ); ?>
		</div>
	</div>
	<?php
}

add_action( 'eronex_post_tags', 'eronex_post_tags_markup' );

/**
 * Outputs single post comments template markup.
 *
 * Hooked to `eronex_post_comments`.
 *
 * @since 0.0.1
 *
 * @return void
 */
function eronex_post_comments_markup() {
	if ( comments_open() || get_comments_number() ) {
		comments_template();
	}
}

add_action( 'eronex_post_comments', 'eronex_post_comments_markup' );

/**
 * Outputs single page content markup.
 *
 * Hooked to `eronex_page_content`.
 *
 * @since 0.0.1
 *
 * @return void
 */
function eronex_page_content_markup() {
	?>
	<div class="eronex-post-content">
		<?php the_content(); ?>
	</div>
	<?php
}

add_action( 'eronex_page_content', 'eronex_page_content_markup' );

/**
 * Outputs single page comments template markup.
 *
 * Hooked to `eronex_page_comments`.
 *
 * @since 0.0.1
 *
 * @return void
 */
function eronex_page_comments_markup() {
	if ( comments_open() || get_comments_number() ) {
		comments_template();
	}
}

add_action( 'eronex_page_comments', 'eronex_page_comments_markup' );

/**
 * Outputs multipage post/page navigation markup.
 *
 * Hooked to `eronex_post_pagination` and `eronex_page_pagination`.
 *
 * @since 0.0.1
 *
 * @return void
 */
function eronex_post_pagination_markup() {
	wp_link_pages(
		array(
			'before'      => '<div class="eronex-pagination"><span class="pagination-title">' . esc_html__( 'Pages:', 'eronex' ) . '</span>',
			'after'       => '</div>',
			'link_before' => '<span class="page-number">',
			'link_after'  => '</span>',
		)
	);
}

add_action( 'eronex_post_pagination', 'eronex_post_pagination_markup' );
add_action( 'eronex_page_pagination', 'eronex_post_pagination_markup' );

/**
 * Outputs the archive content loop markup.
 *
 * Hooked to `eronex_archive_loop`. Renders posts or the no-content template,
 * and fires pagination when posts exist.
 *
 * @since 0.0.1
 *
 * @return void
 */
function eronex_archive_loop_markup() {
	if ( have_posts() ) {
		while ( have_posts() ) {
			the_post();
			get_template_part( 'template-parts/archive/post' );
		}
		/**
		 * Fires after the archive post loop to display pagination.
		 *
		 * @since 0.0.1
		 */
		do_action( 'eronex_pagination' );
	} else {
		get_template_part( 'template-parts/no-content' );
	}
}

add_action( 'eronex_archive_loop', 'eronex_archive_loop_markup' );


/**
 * Outputs the singular content loop markup.
 *
 * Hooked to `eronex_single_content`. Loads the page or post single template part.
 *
 * @since 0.0.1
 *
 * @return void
 */
function eronex_single_content_markup() {
	if ( have_posts() ) {
		$type = is_page() ? 'page' : 'post';
		while ( have_posts() ) {
			the_post();
			get_template_part( 'template-parts/single', $type );
		}
	} else {
		get_template_part( 'template-parts/no-content' );
	}
}

add_action( 'eronex_single_content', 'eronex_single_content_markup' );

/**
 * Fallback callback for the navigation menu when no menu is assigned.
 *
 * Displays a link prompting the user to assign a menu when they can edit
 * theme options; otherwise links to the home page.
 *
 * @since 0.0.1
 *
 * @param array $args wp_nav_menu() arguments.
 * @return void
 */
function eronex_nav_menu_fallback( $args ) {
	ob_start();
	$url = current_user_can( 'edit_theme_options' ) ? admin_url( 'nav-menus.php' ) : home_url( '/' );
	/**
	 * Filters the navigation menu fallback URL.
	 *
	 * @since 0.0.1
	 *
	 * @param string $url Fallback menu link URL.
	 */
	$url  = apply_filters( 'eronex_nav_menu_fallback_url', $url );
	$text = esc_html__( 'Home', 'eronex' );

	if ( current_user_can( 'edit_theme_options' ) ) {
		$text = sprintf(
		/* translators: %s: Menu location name */
			esc_html__( 'Set %s menu', 'eronex' ),
			esc_html( $args['theme_location'] )
		);
	}

	?>
	<nav class="eronex-nav">
		<ul class="eronex-menu eronex-alt-font">
			<li>
				<a class="menu-item no-menu" href="<?php echo esc_url( $url ); ?>"><?php echo esc_html( $text ); ?></a>
			</li>
		</ul>
	</nav>
	<?php
	$output = ob_get_clean();
	echo wp_kses_post( $output );
}

/**
 * Filters the excerpt "more" string.
 *
 * Hooked to `excerpt_more`.
 *
 * @since 0.0.1
 *
 * @return string Excerpt more text.
 */
function eronex_excerpt_more() {
	/**
	 * Filters the excerpt more string used by the theme.
	 *
	 * @since 0.0.1
	 *
	 * @param string $more Excerpt more text. Default '...'.
	 */
	return apply_filters( 'eronex_excerpt_more', '...' );
}

add_filter( 'excerpt_more', 'eronex_excerpt_more' );


/**
 * Outputs numbered pagination markup for the given query.
 *
 * Hooked to `eronex_pagination`.
 *
 * @since 0.0.1
 *
 * @param WP_Query|null $query Optional. Query object. Defaults to the main query.
 * @return void
 */
function eronex_pagination_markup( $query = null ) {
	$query = $query ? $query : $GLOBALS['wp_query'];

	$pagination = paginate_links(
		array(
			'current'   => max( 1, get_query_var( 'paged' ) ),
			'total'     => $query->max_num_pages ?? 1,
			'mid_size'  => 1,
			'end_size'  => 1,
			'prev_text' => esc_html__( 'Previous', 'eronex' ),
			'next_text' => esc_html__( 'Next', 'eronex' ),
			'type'      => 'list',
		)
	);

	if ( $pagination ) {
		echo '<div class="eronex-pagination" aria-label="' . esc_attr__( 'Pagination', 'eronex' ) . '">';
		echo wp_kses_post( $pagination );
		echo '</div>';
	}
}

add_action( 'eronex_pagination', 'eronex_pagination_markup' );

/**
 * Sets up WooCommerce theme compatibility.
 *
 * Hooked to `woocommerce_init`. Removes default WooCommerce wrappers, breadcrumb,
 * sidebar, and shop loop header output.
 *
 * @since 0.0.1
 *
 * @return void
 */
function eronex_woocommerce_setup() {
	remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10 );
	remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10 );
	remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20 );
	remove_action( 'woocommerce_sidebar', 'woocommerce_get_sidebar', 20 );
	remove_action( 'woocommerce_shop_loop_header', 'woocommerce_product_taxonomy_archive_header', 10 );

	add_filter( 'woocommerce_get_breadcrumb', '__return_empty_array' );
}

add_action( 'woocommerce_init', 'eronex_woocommerce_setup' );


/**
 * Registers the Eronex meta box for public post types.
 *
 * Hooked to `add_meta_boxes`.
 *
 * @since 0.0.1
 *
 * @return void
 */
function eronex_setup_meta_box() {

	$post_types = get_post_types( array( 'public' => true ) );

	$excluded = array( 'attachment' );

	/**
	 * Filters the public post types excluded from the Eronex meta box.
	 *
	 * @since 0.0.1
	 *
	 * @param string[] $excluded Post type names to exclude. Default array( 'attachment' ).
	 */
	$excluded = apply_filters( 'eronex_meta_post_type_excluded', $excluded );

	$post_types = array_diff( $post_types, $excluded );

	foreach ( $post_types as $post_type ) {
		add_meta_box(
			'eronex_post_meta_options',
			esc_html__( 'Eronex', 'eronex' ),
			'eronex_meta_box_markup',
			$post_type,
			'side'
		);
	}
}

add_action( 'add_meta_boxes', 'eronex_setup_meta_box' );

/**
 * Enqueues meta box styles on post edit screens.
 *
 * Hooked to `admin_enqueue_scripts`.
 *
 * @since 0.0.1
 *
 * @param string $hook_suffix Current admin page hook suffix.
 * @return void
 */
function eronex_admin_enqueue_scripts( $hook_suffix ) {
	if ( ! in_array( $hook_suffix, array( 'post.php', 'post-new.php' ), true ) ) {
		return;
	}

	wp_enqueue_style(
		'eronex-meta-box',
		ERONEX_URI . 'assets/admin/css/meta-box.css',
		array(),
		ERONEX_VERSION
	);
}

add_action( 'admin_enqueue_scripts', 'eronex_admin_enqueue_scripts' );

/**
 * Renders the Eronex post meta box fields.
 *
 * Callback for the `eronex_post_meta_options` meta box.
 *
 * @since 0.0.1
 *
 * @param WP_Post $post Current post object.
 * @return void
 */
function eronex_meta_box_markup( $post ) {

	if ( empty( $post->ID ) ) {
		return;
	}

	// Prevent CSRF when saving the Eronex meta box.
	wp_nonce_field( 'eronex_save_meta_box', 'eronex_meta_box_nonce' );

	$container   = get_post_meta( $post->ID, 'eronex_container_layout', true );
	$padding     = get_post_meta( $post->ID, 'eronex_content_padding', true );
	$sidebar     = get_post_meta( $post->ID, 'eronex_sidebar_layout', true );
	$header      = get_post_meta( $post->ID, 'eronex_header_style', true );
	$transparent = get_post_meta( $post->ID, 'eronex_header_transparent', true );
	$menu_scheme = get_post_meta( $post->ID, 'eronex_header_menu_scheme', true );
	$banner      = get_post_meta( $post->ID, 'eronex_banner', true );
	?>
	<div class="eronex-meta-box-wrapper">

		<div class="eronex-meta-box eronex-meta-header-layout">
			<label class="eronex-label" for="eronex-header-layout"><?php echo esc_html__( 'Header Layout', 'eronex' ); ?></label>
			<select id="eronex-header-layout" name="eronex_header_style" class="eronex-field eronex-select">
				<option value="" <?php selected( $header, '' ); ?> > <?php esc_html_e( 'Default', 'eronex' ); ?></option>
				<option value="1" <?php selected( $header, '1' ); ?> > <?php esc_html_e( 'Style 1', 'eronex' ); ?></option>
				<option value="2" <?php selected( $header, '2' ); ?> > <?php esc_html_e( 'Style 2', 'eronex' ); ?></option>
				<option value="3" <?php selected( $header, '3' ); ?> > <?php esc_html_e( 'Style 3', 'eronex' ); ?></option>
				<option value="4" <?php selected( $header, '4' ); ?> > <?php esc_html_e( 'Style 4', 'eronex' ); ?></option>
				<option value="5" <?php selected( $header, '5' ); ?> > <?php esc_html_e( 'Style 5', 'eronex' ); ?></option>
			</select>
		</div>

		<div class="eronex-meta-box eronex-meta-header-transparent">
			<label class="eronex-label" for="eronex-header-transparent"><?php echo esc_html__( 'Header Transparent', 'eronex' ); ?></label>
			<select id="eronex-header-transparent" name="eronex_header_transparent" class="eronex-field eronex-select">
				<option value="" <?php selected( $transparent, '' ); ?> > <?php esc_html_e( 'Default', 'eronex' ); ?></option>
				<option value="yes" <?php selected( $transparent, 'yes' ); ?> > <?php esc_html_e( 'Yes', 'eronex' ); ?></option>
				<option value="no" <?php selected( $transparent, 'no' ); ?> > <?php esc_html_e( 'No', 'eronex' ); ?></option>
			</select>
		</div>

		<div class="eronex-meta-box eronex-meta-header-menu_scheme">
			<label class="eronex-label" for="eronex-header-menu-scheme"><?php echo esc_html__( 'Header Menu Scheme', 'eronex' ); ?></label>
			<select id="eronex-header-menu-scheme" name="eronex_header_menu_scheme" class="eronex-field eronex-select">
				<option value="" <?php selected( $menu_scheme, '' ); ?> > <?php esc_html_e( 'Default', 'eronex' ); ?></option>
				<option value="dark" <?php selected( $menu_scheme, 'dark' ); ?> > <?php esc_html_e( 'Dark', 'eronex' ); ?></option>
				<option value="light" <?php selected( $menu_scheme, 'light' ); ?> > <?php esc_html_e( 'Light', 'eronex' ); ?></option>
			</select>
		</div>

		<div class="eronex-meta-box eronex-meta-banner">
			<label class="eronex-label" for="eronex-banner"><?php echo esc_html__( 'Banner', 'eronex' ); ?></label>
			<select id="eronex-banner" name="eronex_banner" class="eronex-field eronex-select">
				<option value="" <?php selected( $banner, '' ); ?> > <?php esc_html_e( 'Default', 'eronex' ); ?></option>
				<option value="yes" <?php selected( $banner, 'yes' ); ?> > <?php esc_html_e( 'Yes', 'eronex' ); ?></option>
				<option value="no" <?php selected( $banner, 'no' ); ?> > <?php esc_html_e( 'No', 'eronex' ); ?></option>
			</select>
		</div>

		<div class="eronex-meta-box eronex-meta-container-layout">
			<label class="eronex-label" for="eronex-container-layout"><?php echo esc_html__( 'Container Layout', 'eronex' ); ?></label>
			<select id="eronex-container-layout" name="eronex_container_layout" class="eronex-field eronex-select">
				<option value="" <?php selected( $container, '' ); ?> > <?php esc_html_e( 'Default', 'eronex' ); ?></option>
				<option value="container" <?php selected( $container, 'container' ); ?> > <?php esc_html_e( 'Container', 'eronex' ); ?></option>
				<option value="container-fluid" <?php selected( $container, 'container-fluid' ); ?> > <?php esc_html_e( 'Container Fluid', 'eronex' ); ?></option>
				<option value="full-width" <?php selected( $container, 'full-width' ); ?> > <?php esc_html_e( 'Full Width', 'eronex' ); ?></option>
			</select>
		</div>

		<div class="eronex-meta-box eronex-meta-content-padding">
			<label class="eronex-label" for="eronex-content-padding"><?php echo esc_html__( 'Content Padding', 'eronex' ); ?></label>
			<select id="eronex-content-padding" name="eronex_content_padding" class="eronex-field eronex-select">
				<option value="" <?php selected( $padding, '' ); ?> > <?php esc_html_e( 'Default', 'eronex' ); ?></option>
				<option value="yes" <?php selected( $padding, 'yes' ); ?> > <?php esc_html_e( 'Yes', 'eronex' ); ?></option>
				<option value="no" <?php selected( $padding, 'no' ); ?> > <?php esc_html_e( 'No', 'eronex' ); ?></option>
			</select>
		</div>

		<div class="eronex-meta-box eronex-meta-sidebar-layout">
			<label class="eronex-label" for="eronex-sidebar-layout"><?php echo esc_html__( 'Sidebar Layout', 'eronex' ); ?></label>
			<select id="eronex-sidebar-layout" name="eronex_sidebar_layout" class="eronex-field eronex-select">
				<option value="" <?php selected( $sidebar, '' ); ?> > <?php esc_html_e( 'Default', 'eronex' ); ?></option>
				<option value="none" <?php selected( $sidebar, 'none' ); ?> > <?php esc_html_e( 'No Sidebar', 'eronex' ); ?></option>
				<option value="left" <?php selected( $sidebar, 'left' ); ?> > <?php esc_html_e( 'Left Sidebar', 'eronex' ); ?></option>
				<option value="right" <?php selected( $sidebar, 'right' ); ?> > <?php esc_html_e( 'Right Sidebar', 'eronex' ); ?></option>
			</select>
		</div>

	</div>
	<?php
}

/**
 * Persists Eronex meta box fields on post save.
 *
 * Hooked to `save_post`.
 *
 * @since 0.0.1
 *
 * @param int     $post_id Post ID.
 * @param WP_Post $post    Post object.
 * @return void
 */
function eronex_save_meta_box( $post_id, $post ) {
	// Only handle actual posts/types with IDs.
	if ( empty( $post_id ) || ! $post instanceof WP_Post ) {
		return;
	}

	// Ignore autosaves/revisions.
	if ( wp_is_post_autosave( $post_id ) || wp_is_post_revision( $post_id ) ) {
		return;
	}

	// Check nonce.
	if ( ! isset( $_POST['eronex_meta_box_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['eronex_meta_box_nonce'] ) ), 'eronex_save_meta_box' ) ) {
		return;
	}

	// Capability check.
	if ( ! current_user_can( 'edit_post', $post_id ) ) {
		return;
	}

	// Only save for the same public post types that we register meta boxes for.
	$post_type       = get_post_type( $post_id );
	$meta_post_types = get_post_types(
		array(
			'public' => true,
		)
	);

	$excluded = array( 'attachment' );
	/**
	 * Filters the public post types excluded from the Eronex meta box.
	 *
	 * @since 0.0.1
	 *
	 * @param string[] $excluded Post type names to exclude. Default array( 'attachment' ).
	 */
	$excluded        = apply_filters( 'eronex_meta_post_type_excluded', $excluded );
	$meta_post_types = array_diff( $meta_post_types, $excluded );

	if ( empty( $post_type ) || ! in_array( $post_type, $meta_post_types, true ) ) {
		return;
	}

	// Whitelist + sanitize each field. "Default" maps to empty string => delete meta.
	$allowed_container_layout = array( '', 'container', 'container-fluid', 'full-width' );
	$allowed_content_padding  = array( '', 'yes', 'no' );
	$allowed_sidebar_layout   = array( '', 'none', 'left', 'right' );
	$allowed_header_layout    = array( '', '1', '2', '3', '4', '5' );
	$allowed_transparent      = array( '', 'yes', 'no' );
	$allowed_menu_scheme      = array( '', 'dark', 'light' );
	$allowed_banner           = array( '', 'yes', 'no' );

	$fields = array(
		'eronex_container_layout'   => array(
			'allowed'  => $allowed_container_layout,
			'post_key' => 'eronex_container_layout',
		),
		'eronex_content_padding'    => array(
			'allowed'  => $allowed_content_padding,
			'post_key' => 'eronex_content_padding',
		),
		'eronex_sidebar_layout'     => array(
			'allowed'  => $allowed_sidebar_layout,
			'post_key' => 'eronex_sidebar_layout',
		),
		'eronex_header_style'       => array(
			'allowed'  => $allowed_header_layout,
			'post_key' => 'eronex_header_style',
		),
		'eronex_header_transparent' => array(
			'allowed'  => $allowed_transparent,
			'post_key' => 'eronex_header_transparent',
		),
		'eronex_header_menu_scheme' => array(
			'allowed'  => $allowed_menu_scheme,
			'post_key' => 'eronex_header_menu_scheme',
		),
		'eronex_banner'             => array(
			'allowed'  => $allowed_banner,
			'post_key' => 'eronex_banner',
		),
	);

	foreach ( $fields as $meta_key => $config ) {
		$raw_value = isset( $_POST[ $config['post_key'] ] ) ? sanitize_text_field( wp_unslash( $_POST[ $config['post_key'] ] ) ) : '';
		$value     = in_array( $raw_value, $config['allowed'], true ) ? $raw_value : '';

		if ( '' === $value ) {
			delete_post_meta( $post_id, $meta_key );
		} else {
			update_post_meta( $post_id, $meta_key, $value );
		}
	}
}

add_action( 'save_post', 'eronex_save_meta_box', 10, 2 );
