<?php
if ('comments.php' == basename($_SERVER['SCRIPT_FILENAME'])) {
    die(__('Please do not load this page directly. Thanks!', 'newmoment'));
}

// check if post is pwd protected
if ( post_password_required() )
    return;

    if ( have_comments() ) { ?>
    <section id="comments">
        <h4><?php comments_number(__('No Comment', 'newmoment'), __('1 Comment', 'newmoment'), __('% Comments', 'newmoment')); ?></h4>
        <ol class="comments-list clearfix">
            <?php
            wp_list_comments(array(
                'walker' => null,
                'style' => 'li',
                'callback' => 'kopa_comments_callback',
                'end-callback' => null,
                'type' => 'all'
            ));
            ?>
        </ol>

        <?php
        // whether or not display paginate comments link
        $prev_comments_link = get_previous_comments_link();
        $next_comments_link = get_next_comments_link();

        if ( '' !== $prev_comments_link . $next_comments_link ) { ?>                
            <div class="pagination kopa-comment-pagination">
                <?php 
                $args = array(
                 'prev_text'    => __('Previous','newmoment'),
                 'next_text'    => __('Next','newmoment')
                );

                paginate_comments_links($args); ?>
            </div>
            <!-- pagination -->
        <?php } // endif ?>
    </section>
    <?php } elseif ( ! comments_open() && post_type_supports(get_post_type(), 'comments') ) {

    // Get template about author & related post
    get_template_part( 'layout/template', 'about-author' ); 
    get_template_part( 'layout/template', 'related-posts' );

    return;
    } // endif

    // Get template about author & related post
    get_template_part( 'layout/template', 'about-author' ); 
    get_template_part( 'layout/template', 'related-posts' );

    // Get comment form
    kopa_comment_form();

/*
 * Comments call back function
 */
function kopa_comments_callback($comment, $args, $depth) {
    $GLOBALS['comment'] = $comment;

    if ( 'pingback' == get_comment_type() || 'trackback' == get_comment_type() ) { ?>

    <li id="comment-<?php echo comment_ID(); ?>" <?php comment_class( 'comment' ); ?>>
        <article class="comment-wrap clearfix">
            <div class="comment-avatar">
                <?php if ( get_comment_author_url() ) { ?>
                        <a href="<?php comment_author_url(); ?>">
                <?php } ?>

                <?php echo get_avatar( $comment->comment_author_email, 50 ); ?>

                <?php if ( get_comment_author_url() ) { ?>
                        </a>
                <?php } ?>
            </div>
            <div class="comment-body clearfix">
                <header class="clearfix">
                    <div class="pull-left">
                        <h6><?php _e( 'Pingback', 'newmoment' ); ?></h6>
                        <span class="entry-date pull-left"><i class="fa fa-calendar"></i><span><?php comment_date( get_option( 'date_format' ) ); ?></span></span>
                    </div>
                    <div class="comment-button pull-right">
                        <?php if ( current_user_can( 'moderate_comments' ) ) {
                        edit_comment_link( __( 'Edit', 'newmoment' ) );
                        }?>
                    </div>
                </header>
                <p><a href="<?php if ( get_comment_author_url() ) { echo get_comment_author_url(); }?>" target="_blank" title="<?php _e('Pingback', 'newmoment');?>"><?php if ( get_comment_author_url() ) { echo get_comment_author_url(); }?></a></p>
            </div><!--comment-body -->
        </article>
    </li>

    <?php } elseif ( 'comment' == get_comment_type() ) { ?>

    <li id="comment-<?php echo comment_ID(); ?>" <?php comment_class( 'comment clearfix' ); ?> >
        <article class="comment-wrap clearfix">
            <div class="comment-avatar">
                <?php if ( get_comment_author_url() ) { ?>
                        <a href="<?php comment_author_url(); ?>">
                <?php } ?>

                <?php echo get_avatar( $comment->comment_author_email, 50 ); ?>

                <?php if ( get_comment_author_url() ) { ?>
                        </a>
                <?php } ?>
            </div>

            <div class="comment-body clearfix">
                <header class="clearfix">
                    <div class="pull-left">
                        <h6><?php comment_author_link(); ?></h6>
                        <span class="entry-date pull-left"><i class="fa fa-calendar"></i><span><?php comment_date( get_option( 'date_format' ) ); ?></span></span>
                    </div>
                    <div class="comment-button pull-right">
                        <?php if ( current_user_can( 'moderate_comments' ) ) {
                        edit_comment_link( __( 'Edit', 'newmoment' ) );
                        echo '<span>&nbsp;/&nbsp;</span>';
                    } comment_reply_link(array_merge($args, array('depth' => $depth, 'max_depth' => $args['max_depth']))); ?>
                    
                    </div>
                </header>
                <?php comment_text(); ?>
            </div><!--comment-body -->
        </article>
    

    <?php
    } // endif check comment type
}

function kopa_comment_form($args = array(), $post_id = null) {
    if (null === $post_id)
        $post_id = get_the_ID();
    $commenter = wp_get_current_commenter();
    $commeter_author = esc_attr($commenter['comment_author']);
    $commenter_author_email = esc_attr($commenter['comment_author_email']);
    $commenter_author_url = esc_attr($commenter['comment_author_url']);

    $user = wp_get_current_user();
    $user_identity = $user->exists() ? $user->display_name : '';
    $args = wp_parse_args($args);
    if (!isset($args['format']))
        $args['format'] = current_theme_supports('html5', 'comment-form') ? 'html5' : 'xhtml';
    $req = get_option('require_name_email');
    $aria_req = ( $req ? " aria-required='true'" : '' );
    $html5 = 'html5' === $args['format'];
    $fields = array();

    $fields['author'] = '<div class="comment-left pull-left">';
    $fields['author'].= '<p class="input-block">';
    $fields['author'].= '<label for="comment_name" class="required">'.__('Name *','newmoment').'<span>*</span></label>';
    $fields['author'].= '<input id="comment_name" name="author" value="'. $commeter_author . '" placeholder="' . __('Name (required)', 'newmoment') . '" type="text">';
    $fields['author'].= '</p>';


    $fields['email'] = '<p class="input-block">';
    $fields['email'].= '<label for="comment_email" class="required">'.__('Email *','newmoment').'<span> *</span></label>';
    $fields['email'].= '<input id="comment_email" name="email" value="'. $commenter_author_email . '"  type="email" placeholder="' . __('Email (required)', 'newmoment') .'">';
    $fields['email'].= '</p>';

    $fields['url'] = '<p class="input-block">';
    $fields['url'].= '<label for="comment_url" class="required">'.__('Name *','newmoment').'</label>';
    $fields['url'].= '<input id="comment_url" type="text" name="url" value="' . $commenter_author_url .'" placeholder="' . __('Website', 'newmoment') . '">';
    $fields['url'].= '</p>';
    $fields['url'].= '</div>';

    if ( ! is_user_logged_in() ) {
        $comment_field = '<div class="comment-right pull-right">';
        $comment_field.= '<p class="textarea-block">';
        $comment_field.= '<label for="comment_message" class="required">'.__('Your comment','newmoment').' <span>*</span></label>';
        $comment_field.= '<textarea value="" placeholder="' . __('Your comment (required)', 'newmoment') .'" name="comment" id="comment_message" cols="88" rows="6"></textarea>';
        $comment_field.= '</p>';
        $comment_field.= '</div>';
    } else {
        $comment_field= '<p class="textarea-block">';
        $comment_field.= '<label for="comment_message" class="required">'.__('Your comment','newmoment').'<span>*</span></label>';
        $comment_field.= '<textarea value="" placeholder="' . __('Your comment (required)', 'newmoment') .'" name="comment" id="comment_message" cols="88" rows="6"></textarea>';
        $comment_field.= '</p>';
    }

    


    $fields = apply_filters('comment_form_default_fields', $fields);

    $defaults = array(
        'fields' => $fields,
        'comment_field' => $comment_field,
        'must_log_in' => '<p class="must-log-in">' . sprintf(__('You must be <a href="%s">logged in</a> to post a comment.', 'newmoment'), wp_login_url(apply_filters('the_permalink', get_permalink($post_id)))) . '</p>',
        'logged_in_as' => '<p class="logged-in-as">' . sprintf(__('Logged in as <a href="%1$s">%2$s</a>. <a href="%3$s" title="Log out of this account">Log out?</a>', 'newmoment'), get_edit_user_link(), $user_identity, wp_logout_url(apply_filters('the_permalink', get_permalink($post_id)))) . '</p>',
        'comment_notes_before' => 'Your email address will not be published. Required fields are marked *',
        'comment_notes_after' => '',
        'id_form' => 'comments-form',
        'id_submit' => 'submit-contact',
        'title_reply' => __('POST YOUR COMMENTS', 'newmoment'),
        'title_reply_to' => __('POST YOUR COMMENTS TO %s', 'newmoment'),
        'cancel_reply_link' => __('(Cancel)', 'newmoment'),
        'label_submit' => __('Post Comment', 'newmoment'),
        'format' => 'xhtml',
    );
    $args = wp_parse_args($args, apply_filters('comment_form_defaults', $defaults));
    ?>
    <?php if (comments_open($post_id)) : ?>
        <?php
        do_action('comment_form_before');
        ?>
        <div id="respond">            
            <h4 class="contact-title widget-title style3">
                <?php comment_form_title($args['title_reply'], $args['title_reply_to']); ?>
                <?php cancel_comment_reply_link($args['cancel_reply_link']); ?>
            </h4>                        

            <?php if (get_option('comment_registration') && !is_user_logged_in()) : ?>
                <?php echo $args['must_log_in']; ?>
                <?php
                do_action('comment_form_must_log_in_after');
                ?>
            <?php else : ?>            
                <form action="<?php echo site_url('/wp-comments-post.php'); ?>" method="post" id="<?php echo esc_attr($args['id_form']); ?>" class="comments-form clearfix" <?php echo $html5 ? ' novalidate' : ''; ?>>                    
                    <?php
                    do_action('comment_form_top');
                    ?>
                    <?php if (is_user_logged_in()) : ?>
                        <?php
                        echo apply_filters('comment_form_logged_in', $args['logged_in_as'], $commenter, $user_identity);
                        ?>
                        <?php
                        do_action('comment_form_logged_in_after', $commenter, $user_identity);
                        ?>
                    <?php else : ?>                        
                        <?php
                        do_action('comment_form_before_fields');
                        foreach ((array) $args['fields'] as $name => $field) {
                            echo apply_filters("comment_form_field_{$name}", $field) . "\n";
                        }
                        do_action('comment_form_after_fields');
                        ?>
                    <?php endif; ?>
                    <?php
                    echo apply_filters('comment_form_field_comment', $args['comment_field']);
                    ?>
                    <?php echo $args['comment_notes_after']; ?>

                    <div class="clear"></div>
                    <p class="comment-button clearfix">   
                        <input type="submit" name="submit"  value="<?php echo esc_attr($args['label_submit']); ?>" id="<?php echo esc_attr($args['id_submit']); ?>" class="input-submit">                                                                                        
                        <?php comment_id_fields($post_id); ?>                                                                     
                    </p>

                    <?php
                    do_action('comment_form', $post_id);
                    ?>
                </form>
            <?php endif; ?>
        </div><!-- #respond -->
        <?php
        do_action('comment_form_after');
    else :
        do_action('comment_form_comments_closed');
    endif;
}
