<?php
/**
 * The template for displaying comments
 *
 * This is the template that displays the area of the page that contains both the current comments
 * and the comment form.
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/
 *
 * @package lively
 */

/*
 * If the current post is protected by a password and
 * the visitor has not yet entered the password we will
 * return early without loading the comments.
 */

if ( post_password_required() ) {
	return;
}
$lively_commenter     = wp_get_current_commenter();
$lively_name_required = (bool) get_option( 'require_name_email' );
$lively_required_attr = $lively_name_required ? ' required="required" aria-required="true"' : '';
$lively_required_mark = $lively_name_required ? ' <span class="required">*</span>' : '';
$lively_current_user  = wp_get_current_user();

$fields = array(

	'author' => '<p class="comment-form-author contact-item">'
		. '<label for="author">' . esc_html__( 'Name', 'lively' ) . $lively_required_mark . '</label>'
		. '<input id="author" name="author" type="text" class="input-form" autocomplete="name" maxlength="245"'
		. ' value="' . esc_attr( $lively_commenter['comment_author'] ) . '"' . $lively_required_attr . ' />'
		. '</p>',

	'email'  => '<p class="comment-form-email contact-item">'
		. '<label for="email">' . esc_html__( 'Email', 'lively' ) . $lively_required_mark . '</label>'
		. '<input id="email" name="email" type="email" class="input-form" autocomplete="email" maxlength="100"'
		. ' value="' . esc_attr( $lively_commenter['comment_author_email'] ) . '"' . $lively_required_attr . ' />'
		. '</p>',

	'url'    => '<p class="comment-form-url contact-item">'
		. '<label for="url">' . esc_html__( 'Website', 'lively' ) . '</label>'
		. '<input id="url" name="url" type="url" class="input-form" autocomplete="url" maxlength="200"'
		. ' value="' . esc_attr( $lively_commenter['comment_author_url'] ) . '" />'
		. '</p>',
);

if ( function_exists( 'wp_get_current_commenter' ) && get_option( 'show_comments_cookies_opt_in' ) ) {
	$lively_consent = empty( $lively_commenter['comment_author_email'] ) ? '' : ' checked="checked"';

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

$custom_comment_form = array(
	'fields'               => apply_filters( 'comment_form_default_fields', $fields ),
	'comment_field'        => '<p class="comment-form-comment contact-item">'
		. '<label for="comment">' . esc_html__( 'Comment', 'lively' ) . ' <span class="required">*</span></label>'
		. '<textarea id="comment" name="comment" class="textarea-form" rows="6" maxlength="65525" required="required" aria-required="true"></textarea>'
		. '</p>',
	'logged_in_as'         => '<p class="logged-in-as">'
		. esc_html__( 'Logged in as', 'lively' ) . ' '
		. '<a href="' . esc_url( admin_url( 'profile.php' ) ) . '">' . esc_html( $lively_current_user->display_name ) . '</a> '
		. '<a href="' . esc_url( wp_logout_url( apply_filters( 'the_permalink', get_permalink(), get_the_ID() ) ) ) . '">' . esc_html__( 'Log out?', 'lively' ) . '</a>'
		. '</p>',
	'cancel_reply_link'    => esc_html__( 'Cancel', 'lively' ),
	'comment_notes_before' => '<p class="comment-notes">' . esc_html__( 'Your email address will not be published. Required fields are marked *', 'lively' ) . '</p>',
	'comment_notes_after'  => '',
	'title_reply'          => esc_html__( 'Leave a Reply', 'lively' ),
	'label_submit'         => esc_html__( 'Post Comment', 'lively' ),
	'id_submit'            => 'submit',
);
?>
<?php if ( have_comments() ) : ?>
<div id="comments" class="comments-area">
    <?php if ( comments_open() ) : ?>
        <h2 class="title">
            <?php
            printf(
                /* translators: %s: comment count number. */
                esc_html( _nx( '%s Comment', '%s Comments', get_comments_number(), 'comments title', 'lively' ) ),
                esc_html( number_format_i18n( get_comments_number() ) )
            );
            ?>
        </h2>
   	<?php endif; ?>
	<?php if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) : ?>
	<nav id="comment-nav-above" class="navigation comment-navigation" role="navigation">
		<h2 class="screen-reader-text"><?php esc_html_e( 'Comment navigation', 'lively' ); ?></h2>
		<div class="nav-previous"><?php previous_comments_link( esc_html__( 'Older Comments', 'lively' ) ); ?></div>
		<div class="nav-next"><?php next_comments_link( esc_html__( 'Newer Comments', 'lively' ) ); ?></div>
	</nav>
	<?php endif; ?>    
	<ul class="comment-list">
		<?php
			wp_list_comments( array(
				'style'       => 'ul',
				'short_ping'  => true,
			) );
		?>
	</ul>    
	<?php if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) : ?>
	<nav id="comment-nav-below" class="navigation comment-navigation" role="navigation">
		<h2 class="screen-reader-text"><?php esc_html_e( 'Comment navigation', 'lively' ); ?></h2>
		<div class="nav-previous"><?php previous_comments_link( esc_html__( 'Older Comments', 'lively' ) ); ?></div>
		<div class="nav-next"><?php next_comments_link( esc_html__( 'Newer Comments', 'lively' ) ); ?></div>
	</nav>
	<?php endif; ?>    
	<?php if ( ! comments_open() ) : ?>
	<p class="no-comments"><?php esc_html_e( 'Comments are closed.', 'lively' ); ?></p>
	<?php endif; ?>
</div>
<?php endif; ?>
<?php comment_form($custom_comment_form); ?>
