<?php

if (!class_exists('acf_form_comment')) {
    class acf_form_comment
    {
        function __construct()
        {
            add_action('admin_enqueue_scripts', array($this, 'admin_enqueue_scripts'));
            add_filter('comment_form_field_comment', array($this, 'comment_form_field_comment'), 999, 1);
            add_action('edit_comment', array($this, 'save_comment'), 10, 1);
            add_action('comment_post', array($this, 'save_comment'), 10, 1);
        }
        function validate_page()
        {
            global $pagenow;
            if ($pagenow == 'comment.php') {
                return true;
            }
            return false;
        }
        function admin_enqueue_scripts()
        {
            if (!$this->validate_page()) {
                return;
            }
            acf_enqueue_scripts();
            add_action('admin_footer', array($this, 'admin_footer'), 10, 1);
            add_action('add_meta_boxes_comment', array($this, 'edit_comment'), 10, 1);
        }
        function edit_comment($comment)
        {
            $post_id = "comment_{$comment->comment_ID}";
            $field_groups = acf_get_field_groups(array('comment' => get_post_type($comment->comment_post_ID)));
            if (!empty($field_groups)) {
                acf_form_data(array('screen' => 'comment', 'post_id' => $post_id));
                foreach ($field_groups as $field_group) {
                    $fields = acf_get_fields($field_group);
                    $o = array('id' => 'acf-' . $field_group['ID'], 'key' => $field_group['key'], 'label' => $field_group['label_placement'], 'edit_url' => '', 'edit_title' => __('Edit field group', 'acf'));
                    if ($field_group['ID'] && acf_current_user_can_admin()) {
                        $o['edit_url'] = admin_url('post.php?post=' . $field_group['ID'] . '&action=edit');
                    }
                    ?>
				<div id="acf-<?php 
                    echo $field_group['ID'];
                    ?>" class="stuffbox">
					<h3 class="hndle"><?php 
                    echo $field_group['title'];
                    ?></h3>
					<div class="inside">
							<?php 
                    acf_render_fields($fields, $post_id, 'div', $field_group['instruction_placement']);
                    ?>
						<script type="text/javascript">
						if( typeof acf !== 'undefined' ) {
								
							acf.newPostbox(<?php 
                    echo json_encode($o);
                    ?>);	
						
						}
						</script>
					</div>
				</div>
							<?php 
                }
            }
        }
        function comment_form_field_comment($html)
        {
            global $post;
            $post_id = false;
            $field_groups = acf_get_field_groups(array('comment' => $post->post_type));
            if (!$field_groups) {
                return $html;
            }
            acf_enqueue_scripts();
            ob_start();
            acf_form_data(array('screen' => 'comment', 'post_id' => $post_id));
            echo '<div class="acf-comment-fields acf-fields -clear">';
            foreach ($field_groups as $field_group) {
                $fields = acf_get_fields($field_group);
                acf_render_fields($fields, $post_id, 'p', $field_group['instruction_placement']);
            }
            echo '</div>';
            $html .= ob_get_contents();
            ob_end_clean();
            return $html;
        }
        function save_comment($comment_id)
        {
            if (!acf_verify_nonce('comment')) {
                return $comment_id;
            }
            if (isset($_POST['acf'])) {
                $_POST['acf'] = wp_kses_post_deep($_POST['acf']);
            }
            if (acf_validate_save_post(true)) {
                acf_save_post("comment_{$comment_id}");
            }
        }
        function admin_footer()
        {
            ?>
<script type="text/javascript">
(function($) {
	
	// vars
	var $spinner = $('#publishing-action .spinner');
	
	
	// create spinner if not exists (may exist in future WP versions)
	if( !$spinner.exists() ) {
		
		// create spinner
		$spinner = $('<span class="spinner"></span>');
		
		
		// append
		$('#publishing-action').prepend( $spinner );
		
	}
	
})(jQuery);	
</script>
			<?php 
        }
    }
    new acf_form_comment();
}