<?php
/**
 * Common some functions for this theme
 *
 * The file loading under functions.php
 *
 * @package RB_Themes
 * @subpackage RB_Blog_Two
 */

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

/*
=======================================
Table of Common Functions List Start Here
=========================================
	01. Skip Link Focus
	02. Header Menu Navwalker
	03. Custom Comment Form
	04. Custom Comment List
*/

if ( ! function_exists( 'rb_blog_two_focus_fix' ) ) {
	/**
	 * Skip Link Focus.
	 */
	function rb_blog_two_focus_fix() {
		// If SCRIPT_DEBUG is defined and true, print the unminified file.
		if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) {
			echo '<script>';
			include get_template_directory() . '/assets/js/skip-link-focus-fix.js';
			echo '</script>';
		}

		// The following is minified via `npx terser --compress --mangle -- assets/js/skip-link-focus-fix.js`.
		?>
		<script>
		/(trident|msie)/i.test(navigator.userAgent)&&document.getElementById&&window.addEventListener&&window.addEventListener("hashchange",(function(){var t,e=location.hash.substring(1);/^[A-z0-9_-]+$/.test(e)&&(t=document.getElementById(e))&&(/^(?:a|select|input|button|textarea)$/i.test(t.tagName)||(t.tabIndex=-1),t.focus())}),!1);
		</script>
		<?php
	}
	add_action( 'wp_print_footer_scripts', 'rb_blog_two_focus_fix' );
}

if ( ! function_exists( 'rb_blog_two_header_menu_custom' ) ) {
	/**
	 *  Header Menu Navwalker.
	 */
	function rb_blog_two_header_menu_custom() {
		if ( has_nav_menu( 'header_menu' ) ) {
			wp_nav_menu(
				array(
					'theme_location'  => 'header_menu',
					'container'       => 'nav',
					'container_id'    => 'site-navigation',
					'container_class' => 'header-menu-container',
					'menu'            => 'ul',
					'menu_class'      => 'header-menu',
					'fallback_cb'     => 'RB_Blog_Two_Navwalker_Class::fallback',
					'walker'          => new RB_Blog_Two_Navwalker_Class(),
				)
			);
		}
	}
	add_action( 'rb_blog_two_header_menu', 'rb_blog_two_header_menu_custom' );
}

if ( ! function_exists( 'rb_blog_two_custom_comment_form' ) ) {
	/**
	 * Reorder comment form fields.
	 *
	 * @param array $fields Comment form fields.
	 * @return array Modified comment form fields.
	 */
	function rb_blog_two_custom_comment_form( $fields ) {
		// What fields you want to control.
		$comment_field_author  = $fields['author'];
		$comment_field_email   = $fields['email'];
		$comment_field_url     = $fields['url'];
		$comment_field_massage = $fields['comment'];
		$comment_field_cookies = $fields['cookies'];

		// The fields you want to unset (remove).
		unset( $fields['author'] );
		unset( $fields['email'] );
		unset( $fields['url'] );
		unset( $fields['comment'] );
		unset( $fields['cookies'] );

		// Display the fields to your own taste.
		// The order in which you place them will determine in what order they are displayed.
		$fields['author']  = $comment_field_author;
		$fields['email']   = $comment_field_email;
		$fields['url']     = $comment_field_url;
		$fields['comment'] = $comment_field_massage;
		$fields['cookies'] = $comment_field_cookies;

		return $fields;
	}
	add_filter( 'comment_form_fields', 'rb_blog_two_custom_comment_form' );
}

if ( ! function_exists( 'rb_blog_two_comment_list' ) ) {
	/**
	 * Custom comment list callback.
	 *
	 * @param WP_Comment $comment Comment object.
	 * @param array      $args    Comment arguments.
	 * @param int        $depth   Comment depth.
	 * @return void
	 */
	function rb_blog_two_comment_list( $comment, $args, $depth ) {
		$args['reply_text'] = esc_html__( 'Reply', 'rb-blog-two' );
		$replay_class       = 'comment-depth-' . (int) $depth;
		?>
			<li id="comment-<?php comment_ID(); ?>" <?php comment_class( 'comment-item ' . esc_attr( $replay_class ) ); ?>>
				<div class="comments-box">
					<div class="comments-avatar">
						<?php echo wp_kses_post( get_avatar( $comment, 90 ) ); ?>
					</div>
					<div class="comments-text">

						<h5 class="avatar-name">
							<?php echo wp_kses_post( get_comment_author_link( $comment ) ); ?>
						</h5>

						<div class="comments-meta">
							<span class="comments-meta-icon">
								<i class="fa-regular fa-calendar-days"></i>
							</span>
							<span>
								<?php echo esc_html( get_comment_date( '', $comment ) ); ?>
							</span>
							<span>
								<?php echo esc_html( get_comment_time( '', $comment ) ); ?>
							</span>
						</div>

						<div class="comments-content">
							<?php comment_text(); ?>
						</div>

						<div class="comments-replay">
							<?php
							comment_reply_link(
								array_merge(
									$args,
									array(
										'depth'     => (int) $depth,
										'max_depth' => (int) $args['max_depth'],
									)
								)
							);
							?>
						</div>

					</div>
				</div>
		<?php
	}
}