<?php
/**
 * @package WordPress
 * @subpackage Campoal
 */

/**
* Custom comment list display
*
* @since Campoal 1.0.0
*/
if ( ! function_exists( 'conikal_comment_callback' ) ) :
function conikal_comment_callback($comment, $args, $depth) {
    if ( 'li' === $args['style'] ) {
        $tag       = 'li';
        $add_below = 'comment';
    } else {
        $tag       = 'div';
        $add_below = 'div-comment';
    }

    $author_data   = get_user_by('email', get_comment_author_email());

    if ($author_data) {
        $author_name    = $author_data->display_name;
        $author_avatar  = conikal_get_avatar_url( $author_data->ID, array('size' => 64));
        $author_link    = get_author_posts_url($author_data->ID, $author_data->user_login);

        if (class_exists('Give') && function_exists('conikal_contribution_level_badge')) {
            $level_badge = conikal_contribution_level_badge($author_data->ID);
        } else {
            $level_badge = '';
        }
    } else {
        $author_name    = get_comment_author();
        $author_avatar  = conikal_get_avatar_url( get_comment_author_email(), array('size' => 64));
        $author_link    = get_comment_author_url();
        $level_badge    = '';
    }

    $comment_id     = get_comment_ID();
    $comment_type   = get_comment_type($comment_id);
    $votes          = get_comment_meta($comment_id, 'comment_vote', true);
    ?>
    <?php if ( 'div' != $args['style'] ) : ?>
        <li id="comment-<?php echo esc_attr($comment_id) ?>" class="comment">
    <?php endif; ?>
        <?php if ($comment_type === 'comment') : ?>
            <a href="<?php echo esc_url($author_link) ?>" class="avatar <?php echo esc_attr($level_badge) ?>" data-bjax>
                <img src="<?php echo esc_url($author_avatar) ?>" alt="<?php echo esc_attr($author_name) ?>" />
            </a>
        <?php endif; ?>
        <div class="content">
            <a class="author" href="<?php echo esc_url($author_link) ?>" data-bjax><?php echo esc_html($author_name) ?></a>
            <div class="metadata">
                <div class="date">
                    <?php echo sprintf(esc_html__('%s ago', 'campoal'), human_time_diff(strtotime(get_comment_date(), current_time('timestamp')), current_time('timestamp'))); ?>
                </div>
                <?php if (function_exists('conikal_register_petition_type')) : ?>
                    <div class="date"> · </div>
                    <div class="rating">
                        <span id="vote-num-<?php echo esc_attr($comment_id) ?>"><?php echo esc_html($votes ? count($votes) : 0) ?></span> <?php echo _n('like', 'likes', $votes, 'campoal'); ?>
                    </div>
                <?php endif; ?>
            </div>
            <div class="text" id="comment-content-<?php echo esc_attr($comment_id) ?>">
                <?php comment_text(); ?>
            </div>
            <div class="actions">
                <?php if (function_exists('conikal_register_petition_type')) : ?>
                    <?php if (is_array($votes) && in_array(get_current_user_id(), $votes)) { ?>
                        <a href="javascript:void(0)" class="vote down" id="vote-<?php echo esc_attr($comment_id) ?>" data-author="<?php echo esc_attr(comment_author()) ?>" data-id="<?php echo esc_attr($comment_id) ?>"><i class="thumbs down icon"></i><?php esc_html_e('Unlike', 'campoal') ?></a>
                    <?php } else { ?>
                        <a href="javascript:void(0)" class="vote up" id="vote-<?php echo esc_attr($comment_id) ?>" data-author="<?php echo esc_attr(comment_author()) ?>" data-id="<?php echo esc_attr($comment_id) ?>"><i class="thumbs up icon"></i><?php esc_html_e('Like', 'campoal') ?></a>
                    <?php } ?>
                <?php endif; ?>
                <?php comment_reply_link( array_merge( $args, array( 'reply_text' => '<i class="reply icon"></i>' . esc_html__('Reply', 'campoal'), 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
                <?php if ( is_user_logged_in() && function_exists('conikal_register_petition_type') ) { ?>
                <?php if ( ($comment->user_id == get_current_user_id()) || current_user_can('administrator') ) { ?>
                    <a href="javascript:void(0)" class="edit" id="edit-<?php echo esc_attr($comment_id) ?>" data-author="<?php echo esc_attr(comment_author()) ?>" data-id="<?php echo esc_attr($comment_id) ?>"><i class="pencil icon"></i><?php esc_html_e('Edit', 'campoal') ?></a>
                    <a href="javascript:void(0)" class="delete" id="delete-<?php echo esc_attr($comment_id) ?>" data-author="<?php echo esc_attr(comment_author()) ?>" data-id="<?php echo esc_attr($comment_id) ?>"><i class="delete icon"></i><?php esc_html_e('Delete', 'campoal') ?></a>
                <?php }
                    }
                 ?>
            </div>
        </div>
    <?php if ( $comment->comment_approved == '0' ) : ?>
        <em class="comment-awaiting-moderation"><?php esc_html_e('Your comment is awaiting moderation.', 'campoal'); ?></em>
        <br />
    <?php endif; ?>
    <?php
}
endif;


/**
* Display navigation next and previous comment
*
* @since Campoal 1.0.0
*/
if ( ! function_exists( 'conikal_comment_nav' ) ) :
function conikal_comment_nav() {
    // Are there comments to navigate through?
    if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) :
    ?>
    <nav class="navigation comment-navigation">
        <h2 class="screen-reader-text"><?php esc_html_e('Comment navigation', 'campoal'); ?></h2>
        <div class="nav-links">
            <?php
                if ( $prev_link = get_previous_comments_link( esc_html__('Older Comments', 'campoal') ) ) :
                    printf( '<div class="nav-previous">%s</div>', $prev_link );
                endif;

                if ( $next_link = get_next_comments_link( esc_html__('Newer Comments', 'campoal') ) ) :
                    printf( '<div class="nav-next">%s</div>', $next_link );
                endif;
            ?>
        </div><!-- .nav-links -->
    </nav><!-- .comment-navigation -->
    <?php
    endif;
}
endif;


?>