<?php
/**
 * Comment structure.
 *
 * @package Sunset
 */

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

if ( ! function_exists( 'sunset_comment' ) ) {
	/**
	 * Template for comments and pingbacks.
	 * Used as a callback by wp_list_comments() for displaying the comments.
	 *
	 * @param object $comment The comment object.
	 * @param array  $args The existing args.
	 * @param int    $depth The thread depth.
	 */
	function sunset_comment( $comment, $args, $depth ) {
		$args['avatar_size'] = apply_filters( 'sunset_comment_avatar_size', 90 );

		if ( 'pingback' === $comment->comment_type || 'trackback' === $comment->comment_type ) : ?>

		<li id="comment-<?php comment_ID(); ?>" <?php comment_class(); ?>>
			<div class="comment-body">
				<?php esc_html_e( 'Pingback:', 'my-sunset' ); ?> <?php comment_author_link(); ?> <?php edit_comment_link( __( 'Edit', 'my-sunset' ), '<span class="edit-link">', '</span>' ); ?>
			</div>

		<?php else : ?>

		<li id="comment-<?php comment_ID(); ?>" <?php comment_class( empty( $args['has_children'] ) ? '' : 'parent' ); ?>>
			<article class="comment-body" id="<?php echo esc_attr(get_comment_ID()); ?>">
				<footer class='comment-meta'>
					<?php
					if ( 0 != $args['avatar_size'] ) { // phpcs:ignore
						echo get_avatar( $comment, $args['avatar_size'] );
					}
					?>
					<div class="comment-author-info">
						<div class='comment-author'>
							<?php printf( '<cite itemprop="name" class="fn">%s</cite>', get_comment_author_link() ); ?>
						</div>

						<?php
						/**
						 * sunset_after_comment_author_name hook.
						 */
						do_action( 'sunset_after_comment_author_name' );

						if ( apply_filters( 'sunset_show_comment_entry_meta', true ) ) :
							?>
							<div class="entry-meta comment-metadata">
								<a href="<?php echo esc_url( get_comment_link( $comment->comment_ID ) ); ?>">
									<time datetime="<?php comment_time( 'c' ); ?>" itemprop="datePublished">
										<?php
											printf(
												/* translators: 1: date, 2: time */
												_x( '%1$s at %2$s', '1: date, 2: time', 'my-sunset' ), // phpcs:ignore
												get_comment_date(), // phpcs:ignore
												get_comment_time() // phpcs:ignore
											);
										?>
									</time>
								</a>
								<?php edit_comment_link( __( 'Edit', 'my-sunset' ), '<span class="edit-link">/ ', '</span>' ); ?>
							</div>
							<?php
						endif;
						?>
					</div>

					<?php if ( '0' == $comment->comment_approved ) : // phpcs:ignore ?>
						<p class="comment-awaiting-moderation"><?php esc_html_e( 'Your comment is awaiting moderation.', 'my-sunset' ); ?></p>
					<?php endif; ?>
				</footer>

				<div class="comment-content" itemprop="text">
					<?php
					/**
					 * sunset_before_comment_content hook.
					 */
					do_action( 'sunset_before_comment_text', $comment, $args, $depth );

					comment_text();

					/**
					 * sunset_after_comment_content hook.
					 */
					do_action( 'sunset_after_comment_text', $comment, $args, $depth );
					?>
				</div>
			</article>
			<?php
		endif;
	}
}

add_action( 'sunset_after_comment_text', 'sunset_do_comment_reply_link', 10, 3 );
/**
 * Add our comment reply link after the comment text.
 *
 * @param object $comment The comment object.
 * @param array  $args The existing args.
 * @param int    $depth The thread depth.
 */
function sunset_do_comment_reply_link( $comment, $args, $depth ) {
	comment_reply_link(
		array_merge(
			$args,
			array(
				//'add_below' => 'div-comment',
				'depth'     => $depth,
				'max_depth' => $args['max_depth'],
				'before'    => '<span class="reply">',
				'after'     => '</span>',
			)
		)
	);
}

add_filter( 'comment_form_defaults', 'sunset_set_comment_form_defaults' );
/**
 * Set the default settings for our comments.
 *
 * @param array $defaults The existing defaults.
 * @return array
 */
function sunset_set_comment_form_defaults( $defaults ) {

	$defaults['comment_field'] = sprintf(
		'<p class="comment-form-comment"><label for="comment" class="screen-reader-text">%1$s</label><textarea id="comment" name="comment" cols="45" rows="8" required placeholder="%1$s *"></textarea></p>',
		esc_html__( 'Comment', 'my-sunset' )
	);

	$defaults['fields']['cookies'] = sprintf(
		'<p class="comment-form-cookies-consent"><label for="wp-comment-cookies-consent"><input id="wp-comment-cookies-consent" name="wp-comment-cookies-consent" type="checkbox" value="yes">%1$s</label></p>',
		esc_html__( 'Save my name, email, and website in this browser for the next time I comment.', 'my-sunset' )
	);

	$defaults['comment_notes_before'] = null;
	$defaults['comment_notes_after']  = null;
	$defaults['id_form']              = 'commentform';
	$defaults['id_submit']            = 'submit';
	$defaults['title_reply']          = apply_filters( 'sunset_leave_comment', __( 'Leave a Comment', 'my-sunset' ) );
	$defaults['label_submit']         = apply_filters( 'sunset_post_comment', __( 'Post Comment', 'my-sunset' ) );

	return $defaults;
}

add_filter( 'comment_form_default_fields', 'sunset_filter_comment_fields' );
/**
 * Customizes the existing comment fields.
 *
 * @param array $fields The existing fields.
 * @return array
 */
function sunset_filter_comment_fields( $fields ) {
	$commenter = wp_get_current_commenter();
	$required = get_option( 'require_name_email' );

	$fields['author'] = sprintf(
		'<p class="comment-form-author"><label for="author" class="screen-reader-text">%1$s</label><input placeholder="%1$s%3$s" id="author" name="author" type="text" value="%2$s" size="30"%4$s /></p>',
		esc_html__( 'Name', 'my-sunset' ),
		esc_attr( $commenter['comment_author'] ),
		$required ? ' *' : '',
		$required ? ' required' : ''
	);

	$fields['email'] = sprintf(
		'<p class="comment-form-email"><label for="email" class="screen-reader-text">%1$s</label><input placeholder="%1$s%3$s" id="email" name="email" type="email" value="%2$s" size="30"%4$s /></p>',
		esc_html__( 'Email', 'my-sunset' ),
		esc_attr( $commenter['comment_author_email'] ),
		$required ? ' *' : '',
		$required ? ' required' : ''
	);

	$fields['url'] = sprintf(
		'<p class="comment-form-url"><label for="url" class="screen-reader-text">%1$s</label><input placeholder="%1$s" id="url" name="url" type="url" value="%2$s" size="30" /></p>',
		esc_html__( 'Website', 'my-sunset' ),
		esc_attr( $commenter['comment_author_url'] )
	);

	return $fields;
}

add_filter( 'comment_form_fields', 'sunset_sort_comment_fields' );
/**
 * Re-order comment form fields
 * 
 * @param array $fields The existing fields.
 * @return array
 */
function sunset_sort_comment_fields( $fields ) {
    $comment_field = $fields['comment'];
    $author_field = $fields['author'];
    $email_field = $fields['email'];
    $url_field = $fields['url'];
    $cookies_field = $fields['cookies'];
	
    unset( $fields['comment'] );
    unset( $fields['author'] );
    unset( $fields['email'] );
    unset( $fields['url'] );
    unset( $fields['cookies'] );

    $fields['author'] = $author_field;
    $fields['email'] = $email_field;
    $fields['comment'] = $comment_field;
    $fields['url'] = $url_field;
    $fields['cookies'] = $cookies_field;

    return $fields;
}

add_action( 'sunset_after_site_content', 'sunset_do_comments_template', 5 );
/**
 * Add the comments template to pages and single posts.
 */
function sunset_do_comments_template() {
	// If comments are open or we have at least one comment, load up the comment template.
	// phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison -- Intentionally loose.
	if ( comments_open() || '0' != get_comments_number() ) :
		/**
		 * sunset_before_comments_container hook.
		 */
		do_action( 'sunset_before_comments_container' );
		?>

		<div class="comments-area">
			<?php comments_template(); ?>
		</div>

		<?php
	endif;
}
