Betiok
6/17/2017 - 10:47 AM

Update comment custom field with AJAX

Update comment custom field with AJAX

jQuery(document).ready(function($){
	$(document).on('click', '.btn-featured', function(e) {
		e.preventDefault();

		var element = $(this);
		var comment_id = $(this).data('comment');
		var comment_value = $(this).data('value');

		$.ajax({
			url : object.ajax_url,
			type : 'post',
			data : {
				action : 'update_featured_comment',
				comment_id : comment_id,
				comment_value : comment_value
			},
			success : function(response) {
				location.reload(true);
			}
		});
	});

	$(document).on('click', '.btn-thumb', function(e) {
		e.preventDefault();

		var element = $(this);
		var comment_id = $(this).data('comment');

		$.ajax({
			url : object.ajax_url,
			type : 'post',
			data : {
				action : 'update_viewed_comment',
				comment_id : comment_id
			},
			success : function(response) {
				location.reload(true);
			}
		});
	});

	$(document).on('mouseover, mouseleave', '.btn-thumb', function(e) {
		$(this).parent().toggleClass('show');
	});

	$(document).on({
		mouseenter: function() {
			$(this).parent().addClass('show');
		},
		mouseleave: function() {
			$(this).parent().removeClass('show');
		}
	}, '.btn-thumb');
});
/* Update Comment Fields */

function update_featured_comment() {
    update_field('featured', $_POST['comment_value'], $_POST['comment_id']);
    die();
}

add_action('wp_ajax_nopriv_update_featured_comment', 'update_featured_comment');
add_action('wp_ajax_update_featured_comment', 'update_featured_comment');

function update_viewed_comment() {
    $user_id = get_current_user_id();
    $viewed = get_field('viewed_by', $_POST['comment_id'], false);

    if (!in_array($user_id, $viewed)) {
        $viewed[] = $user_id;
    }

    update_field('viewed_by', $viewed, $_POST['comment_id']);
    die();
}

add_action('wp_ajax_nopriv_update_viewed_comment', 'update_viewed_comment');
add_action('wp_ajax_update_viewed_comment', 'update_viewed_comment');
<div class="reply">
    <?php
        $id = 'comment_' . $comment->comment_ID;
        $user_id = apply_filters('determine_current_user', false);

        $views = get_field('viewed_by', $comment, false);

        if (in_array($user_id, $views)) {
            $class = 'active';
        }
    ?>

    <div class="seen-by">
        <p>
            <?php _e('Seen via web: '); ?>
        </p>

        <?php foreach ($views as $view) : ?>
            <img src="<?php the_field('user_avatar', 'user_' . $view); ?>" />
        <?php endforeach; ?>
    </div>

    <?php if(get_field('featured', $comment) == 'No'): ?>
        <?php $featured = 'Yes'; ?>
    <?php else: ?>
        <?php $featured = 'No'; ?>
    <?php endif; ?>

    <span class="btn-featured <?php the_field('featured', $comment); ?>" data-comment="<?php echo $id; ?>" data-value="<?php echo $featured; ?>"></span>

    <span class="sep"></span>

    <span class="btn-thumb <?php echo $class; ?>" data-comment="<?php echo $id; ?>"></span>

    <span class="sep"></span>

    <?php comment_reply_link(array_merge( $args, array('add_below' => $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
</div>