<?php
/**
 * The template for displaying comments
 *
 * The area of the page that contains both current comments
 * and the comment form.
 *
 * @package WordPress
 * @subpackage Campoal
 */

global $campoal_options;

/*
 * 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;
}

$ajax_comment_setting = isset($campoal_options['conikal_ajax_comment_field']) ? $campoal_options['conikal_ajax_comment_field'] : 0;
$comments_per_page = get_option('comments_per_page');
$commnet_count = wp_count_comments(get_the_ID());
$thread_comments_depth = get_option( 'thread_comments_depth' );
?>

<?php if ($ajax_comment_setting == 0) { ?>
    <div id="comments" class="comments-area">
        <?php if ( have_comments() ) : ?>
            <h2 class="ui dividing header">
                <?php
                    $comments_number = get_comments_number();
                    if ( '1' === $comments_number ) {
                        /* translators: %s: post title */
                        printf( _x( 'One comment on &ldquo;%s&rdquo;', 'comments title', 'campoal' ), get_the_title() );
                    } else {
                        printf(
                            /* translators: 1: number of comments, 2: post title */
                            _nx(
                                '%1$s comment on &ldquo;%2$s&rdquo;',
                                '%1$s comments on &ldquo;%2$s&rdquo;',
                                $comments_number,
                                'comments title',
                                'campoal'
                            ),
                            number_format_i18n( $comments_number ),
                            get_the_title()
                        );
                    }
                ?>
            </h2>
            <?php conikal_comment_nav(); ?>
            <ul class="ui comments" id="comments-list">
                <?php wp_list_comments( 'type=all&max_depth=' . $thread_comments_depth . '&callback=conikal_comment_callback' ); ?>
            </ul>
            <?php conikal_comment_nav(); ?>

        <?php endif; // have_comments() ?>
        <?php
            // If comments are closed and there are comments, let's leave a little note, shall we?
            if ( ! comments_open() && get_comments_number() && post_type_supports( get_post_type(), 'comments' ) ) :
        ?>
            <p class="no-comments"><?php _e('Comments are closed.', 'campoal'); ?></p>
        <?php endif; ?>

        <?php
            $commenter  = wp_get_current_commenter();
            $req        = get_option( 'require_name_email' );
            $fields     =  array(
                'author'    => '<div class="comment-form-author field ' . ( $req ? 'required' : '' ) . '"><label>' . esc_html__('Name', 'campoal') . '</label> ' .
                '<input id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) .
                '" ' . ( $req ? " aria-required='true'" : '' ) . ' /></div>',

                'email'     => '<div class="comment-form-email field ' . ( $req ? 'required' : '' ) . '"><label>' . esc_html__('Email', 'campoal') . '</label> ' .
                '<input id="email" name="email" type="text" value="' . esc_attr(  $commenter['comment_author_email'] ) .
                '" ' . ( $req ? " aria-required='true'" : '' ) . ' /></div>',

                'url'       => '<div class="comment-form-url field"><label>' . esc_html__('Website', 'campoal') . '</label>' .
                '<input id="url" name="url" type="text" value="' . esc_attr( $commenter['comment_author_url'] ) .
                '" /></div>',

            );

            if ( get_option( 'show_comments_cookies_opt_in' ) ) {
                $fields['cookies']   = '<div class="comment-form-cookies field"><div class="ui checkbox"><input type="checkbox" tabindex="0" class="hidden" id="wp-comment-cookies-consent" name="wp-comment-cookies-consent" value="yes"' . (empty( $commenter['comment_author_email'] ) ? '' : ' checked=&quot;checked&quot;') . 
                '><label>' . 
                esc_html__('Save my name for the next time I comment.', 'campoal') . 
                '</label></div></div>';
            }

            $comments_args = array(
            'class_form'            => 'ui large form',
            'fields'                => apply_filters( 'comment_form_default_fields', $fields ),
            // change the title of send button
            'label_submit'          => esc_html__('Submit', 'campoal'),
            // change the title of the reply section
            'title_reply'           => esc_html__('Write a Reply or Comment', 'campoal'),
            // remove "Text or HTML to be displayed after the set of comment fields"
            'comment_notes_before'  => '<p class="comment-notes">' . esc_html__('Your email address will not be published.', 'campoal') . ( $req ? esc_html__('Required fields are marked *', 'campoal') : '' ) . '</p>',
            'comment_notes_after'   => '',
            // redefine your own textarea (the comment body)
            'comment_field'         => '<div class="comment-form-comment field ' . ( $req ? 'required' : '' ) . '"><label>' . esc_html__('Comment', 'campoal') . '</label><textarea id="comment" name="comment" aria-required="true"></textarea></div>',
            'class_submit'          => 'field',
            'submit_button'         => '<input name="%1$s" type="submit" id="%2$s" class="%3$s ui primary large submit button" value="%4$s" />'
            );

            comment_form($comments_args);
        ?>
        <?php wp_nonce_field('comment_ajax_nonce', 'securityComment', true); ?>

    </div><!-- .comments-area -->
<?php } else {
    get_template_part('templates/ajax-comments');
} ?>
