<?php

/**
 * AJAX request handlers for the frontend gallery.
 *
 * @package MediaTasker
 */
// Prevent direct file access.
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
/**
 * Class Mediat_Ajax
 *
 * Handles AJAX requests for filtering gallery tasks.
 */
class Mediat_Ajax {
    /**
     * Hook into WordPress.
     *
     * @return void
     */
    public static function init() {
        // All five endpoints require a logged-in user; we deliberately do NOT register
        // wp_ajax_nopriv_* counterparts so anonymous requests are rejected at the action level.
        add_action( 'wp_ajax_mediat_filter_tasks', array(__CLASS__, 'filter_tasks') );
        add_action( 'wp_ajax_mediat_get_task_details', array(__CLASS__, 'get_task_details') );
        add_action( 'wp_ajax_mediat_add_comment', array(__CLASS__, 'add_comment') );
        add_action( 'wp_ajax_mediat_edit_comment', array(__CLASS__, 'edit_comment') );
        add_action( 'wp_ajax_mediat_delete_comment', array(__CLASS__, 'delete_comment') );
        // Free version endpoints that apply to multiple items or depend on 'bulk' functions.
        add_action( 'wp_ajax_mediat_bulk_status_change', array(__CLASS__, 'bulk_status_change') );
        add_action( 'wp_ajax_mediat_bulk_delete', array(__CLASS__, 'bulk_delete') );
        add_action( 'wp_ajax_mediat_bulk_download', array(__CLASS__, 'bulk_download') );
    }

    /**
     * AJAX handler: filter gallery tasks by status and/or tag.
     *
     * Expected POST parameters:
     *   - nonce  (string) Verified against 'mediat_frontend_nonce'.
     *   - status (string) Optional custom post status key.
     *   - tag    (int)    Optional mediat_tag term ID.
     *
     * @return void Sends JSON response and dies.
     */
    public static function filter_tasks() {
        // Verify nonce.
        check_ajax_referer( 'mediat_frontend_nonce', 'nonce' );
        // Capability check — at minimum the user must be able to read.
        if ( is_user_logged_in() && !current_user_can( 'read' ) ) {
            wp_send_json_error( array(
                'message' => esc_html__( 'You do not have permission to perform this action.', 'mediatasker' ),
            ), 403 );
        }
        // Sanitize inputs.
        $status = ( isset( $_POST['status'] ) ? sanitize_key( wp_unslash( $_POST['status'] ) ) : '' );
        $tag_id = ( isset( $_POST['tag'] ) ? absint( wp_unslash( $_POST['tag'] ) ) : 0 );
        // Reuse the shortcode's gallery builder.
        $html = Mediat_Shortcode::get_gallery_html( $status, $tag_id );
        wp_send_json_success( array(
            'html' => $html,
        ) );
    }

    /**
     * AJAX handler: get details for a single task (mainly comments).
     *
     * @return void Sends JSON response and dies.
     */
    public static function get_task_details() {
        check_ajax_referer( 'mediat_frontend_nonce', 'nonce' );
        if ( is_user_logged_in() && !current_user_can( 'read' ) ) {
            wp_send_json_error( array(
                'message' => esc_html__( 'Permission denied.', 'mediatasker' ),
            ), 403 );
        }
        $post_id = ( isset( $_POST['post_id'] ) ? absint( $_POST['post_id'] ) : 0 );
        if ( !$post_id || get_post_type( $post_id ) !== 'mediat_task' ) {
            wp_send_json_error( array(
                'message' => esc_html__( 'Invalid task.', 'mediatasker' ),
            ), 404 );
        }
        $post = get_post( $post_id );
        $attachment_id = get_post_thumbnail_id( $post_id );
        $metadata = wp_get_attachment_metadata( $attachment_id );
        $original_path = wp_get_original_image_path( $attachment_id );
        if ( !$original_path || !file_exists( $original_path ) ) {
            $original_path = get_attached_file( $attachment_id );
        }
        $dim_str = '';
        if ( $original_path && file_exists( $original_path ) ) {
            $dims = @getimagesize( $original_path );
            if ( $dims ) {
                $dim_str = $dims[0] . ' × ' . $dims[1] . ' px';
            }
        }
        $terms = wp_get_object_terms( $post_id, 'mediat_view_source', array(
            'fields' => 'slugs',
        ) );
        $view_type = ( !is_wp_error( $terms ) && !empty( $terms ) ? $terms[0] : 'original' );
        $image_info = array(
            'filename'   => get_the_title( $post_id ),
            'dimensions' => $dim_str,
            'size'       => ( $original_path && file_exists( $original_path ) ? size_format( filesize( $original_path ) ) : '' ),
            'date'       => get_the_date( 'M j, Y', $post_id ),
            'status'     => get_post_status( $post_id ),
            'view_type'  => $view_type,
        );
        $response_data = array(
            'image_info' => $image_info,
        );
        $comments = get_comments( array(
            'post_id' => $post_id,
            'status'  => 'approve',
            'order'   => 'ASC',
        ) );
        $comments_html = '';
        if ( empty( $comments ) ) {
            $comments_html = '<p class="mediat_no-comments" style="padding:15px; text-align:center; color:#646970;">' . esc_html__( 'No comments yet. Be the first to start the discussion!', 'mediatasker' ) . '</p>';
        } else {
            foreach ( $comments as $comment ) {
                $comments_html .= self::get_comment_html( $comment );
            }
        }
        $response_data['comments_html'] = $comments_html;
        wp_send_json_success( $response_data );
    }

    /**
     * AJAX handler: add a comment to a task.
     *
     * @return void Sends JSON response and dies.
     */
    public static function add_comment() {
        check_ajax_referer( 'mediat_frontend_nonce', 'nonce' );
        if ( !is_user_logged_in() || !current_user_can( 'read' ) ) {
            wp_send_json_error( array(
                'message' => esc_html__( 'Permission denied.', 'mediatasker' ),
            ), 403 );
        }
        $post_id = ( isset( $_POST['post_id'] ) ? absint( $_POST['post_id'] ) : 0 );
        $content = ( isset( $_POST['comment'] ) ? trim( sanitize_textarea_field( wp_unslash( $_POST['comment'] ) ) ) : '' );
        if ( !$post_id || get_post_type( $post_id ) !== 'mediat_task' ) {
            wp_send_json_error( array(
                'message' => esc_html__( 'Invalid task.', 'mediatasker' ),
            ), 404 );
        }
        if ( empty( $content ) ) {
            wp_send_json_error( array(
                'message' => esc_html__( 'Comment cannot be empty.', 'mediatasker' ),
            ), 400 );
        }
        $user = wp_get_current_user();
        // Build comment data and let WordPress run it through the full pipeline
        // (preprocess_comment, pre_comment_approved → Akismet, disallowed-list check, etc.).
        // We intentionally omit comment_approved so WordPress decides based on
        // discussion settings and any installed anti-spam plugins.
        $comment_data = array(
            'comment_post_ID'      => $post_id,
            'comment_author'       => ( isset( $user->display_name ) ? $user->display_name : 'Guest' ),
            'comment_author_email' => ( isset( $user->user_email ) ? $user->user_email : '' ),
            'comment_author_url'   => ( isset( $user->user_url ) ? $user->user_url : '' ),
            'comment_content'      => $content,
            'comment_type'         => 'comment',
            'comment_parent'       => 0,
            'user_id'              => $user->ID,
        );
        $comment_id = wp_new_comment( $comment_data, true );
        if ( is_wp_error( $comment_id ) ) {
            wp_send_json_error( array(
                'message' => $comment_id->get_error_message(),
            ), 400 );
        }
        if ( !$comment_id ) {
            wp_send_json_error( array(
                'message' => esc_html__( 'Failed to add comment.', 'mediatasker' ),
            ), 500 );
        }
        $comment = get_comment( $comment_id );
        // Only log to the work log and render HTML when the comment is actually approved.
        // If it's held for moderation, surface that to the user instead of rendering an empty thread item.
        if ( $comment && '1' === (string) $comment->comment_approved ) {
            if ( class_exists( 'Mediat_Work_Log' ) ) {
                Mediat_Work_Log::insert_log( $post_id, 'comment_added', array(
                    'comment_id' => $comment_id,
                ) );
            }
            do_action( 'mediat_task_comment_added', $comment_id );
            $comment_html = self::get_comment_html( $comment );
            wp_send_json_success( array(
                'comment_html' => $comment_html,
            ) );
        }
        wp_send_json_success( array(
            'comment_html' => '',
            'pending'      => true,
            'message'      => esc_html__( 'Your comment is awaiting moderation.', 'mediatasker' ),
        ) );
    }

    /**
     * AJAX handler: edit a comment.
     *
     * @return void Sends JSON response and dies.
     */
    public static function edit_comment() {
        check_ajax_referer( 'mediat_frontend_nonce', 'nonce' );
        if ( !is_user_logged_in() ) {
            wp_send_json_error( array(
                'message' => esc_html__( 'You must be logged in.', 'mediatasker' ),
            ), 403 );
        }
        $comment_id = ( isset( $_POST['comment_id'] ) ? absint( $_POST['comment_id'] ) : 0 );
        $content = ( isset( $_POST['comment'] ) ? trim( sanitize_textarea_field( wp_unslash( $_POST['comment'] ) ) ) : '' );
        if ( !$comment_id || empty( $content ) ) {
            wp_send_json_error( array(
                'message' => esc_html__( 'Invalid parameters.', 'mediatasker' ),
            ), 400 );
        }
        $comment = get_comment( $comment_id );
        if ( !$comment ) {
            wp_send_json_error( array(
                'message' => esc_html__( 'Comment not found.', 'mediatasker' ),
            ), 404 );
        }
        $user_id = get_current_user_id();
        if ( $comment->user_id != $user_id && !current_user_can( 'manage_options' ) ) {
            wp_send_json_error( array(
                'message' => esc_html__( 'You do not have permission to edit this comment.', 'mediatasker' ),
            ), 403 );
        }
        $updated = wp_update_comment( array(
            'comment_ID'      => $comment_id,
            'comment_content' => $content,
        ), true );
        if ( is_wp_error( $updated ) || !$updated ) {
            wp_send_json_error( array(
                'message' => esc_html__( 'Failed to update comment.', 'mediatasker' ),
            ), 500 );
        }
        if ( class_exists( 'Mediat_Work_Log' ) ) {
            Mediat_Work_Log::insert_log( $comment->comment_post_ID, 'comment_edited', array(
                'comment_id' => $comment_id,
            ) );
        }
        $comment = get_comment( $comment_id );
        $comment_html = self::get_comment_html( $comment );
        wp_send_json_success( array(
            'comment_html' => $comment_html,
        ) );
    }

    /**
     * AJAX handler: delete a comment.
     *
     * @return void Sends JSON response and dies.
     */
    public static function delete_comment() {
        check_ajax_referer( 'mediat_frontend_nonce', 'nonce' );
        if ( !is_user_logged_in() ) {
            wp_send_json_error( array(
                'message' => esc_html__( 'You must be logged in.', 'mediatasker' ),
            ), 403 );
        }
        $comment_id = ( isset( $_POST['comment_id'] ) ? absint( $_POST['comment_id'] ) : 0 );
        if ( !$comment_id ) {
            wp_send_json_error( array(
                'message' => esc_html__( 'Invalid comment ID.', 'mediatasker' ),
            ), 400 );
        }
        $comment = get_comment( $comment_id );
        if ( !$comment ) {
            wp_send_json_error( array(
                'message' => esc_html__( 'Comment not found.', 'mediatasker' ),
            ), 404 );
        }
        $user_id = get_current_user_id();
        if ( $comment->user_id != $user_id && !current_user_can( 'manage_options' ) ) {
            wp_send_json_error( array(
                'message' => esc_html__( 'You do not have permission to delete this comment.', 'mediatasker' ),
            ), 403 );
        }
        $deleted = wp_delete_comment( $comment_id, true );
        if ( !$deleted ) {
            wp_send_json_error( array(
                'message' => esc_html__( 'Failed to delete comment.', 'mediatasker' ),
            ), 500 );
        }
        if ( class_exists( 'Mediat_Work_Log' ) ) {
            Mediat_Work_Log::insert_log( $comment->comment_post_ID, 'comment_deleted', array(
                'comment_id' => $comment_id,
            ) );
        }
        wp_send_json_success();
    }

    /**
     * AJAX handler: Update status for multiple tasks.
     *
     * @return void Sends JSON response and dies.
     */
    public static function bulk_status_change() {
        check_ajax_referer( 'mediat_frontend_nonce', 'nonce' );
        if ( !current_user_can( 'edit_posts' ) ) {
            wp_send_json_error( array(
                'message' => esc_html__( 'Permission denied.', 'mediatasker' ),
            ), 403 );
        }
        $task_ids = ( isset( $_POST['task_ids'] ) ? array_map( 'absint', (array) $_POST['task_ids'] ) : array() );
        $new_status = ( isset( $_POST['new_status'] ) ? sanitize_key( wp_unslash( $_POST['new_status'] ) ) : '' );
        if ( empty( $task_ids ) || empty( $new_status ) ) {
            wp_send_json_error( array(
                'message' => esc_html__( 'Missing parameters.', 'mediatasker' ),
            ), 400 );
        }
        $valid_statuses = array_keys( Mediat_Post_Types::get_task_statuses() );
        if ( !in_array( $new_status, $valid_statuses ) ) {
            wp_send_json_error( array(
                'message' => esc_html__( 'Invalid status.', 'mediatasker' ),
            ), 400 );
        }
        $updated_ids = array();
        foreach ( $task_ids as $id ) {
            $id = absint( $id );
            if ( get_post_type( $id ) === 'mediat_task' ) {
                wp_update_post( array(
                    'ID'          => $id,
                    'post_status' => $new_status,
                ) );
                if ( class_exists( 'Mediat_Work_Log' ) ) {
                    Mediat_Work_Log::insert_log( $id, 'status_change', array(
                        'new_status' => $new_status,
                    ) );
                }
                do_action( 'mediat_task_status_changed', $id, $new_status );
                $updated_ids[] = $id;
            }
        }
        wp_send_json_success( array(
            'updated'     => count( $updated_ids ),
            'updated_ids' => $updated_ids,
        ) );
    }

    /**
     * AJAX handler: Delete multiple tasks and their attachments.
     *
     * @return void Sends JSON response and dies.
     */
    public static function bulk_delete() {
        check_ajax_referer( 'mediat_frontend_nonce', 'nonce' );
        if ( !current_user_can( 'delete_posts' ) ) {
            wp_send_json_error( array(
                'message' => esc_html__( 'Permission denied.', 'mediatasker' ),
            ), 403 );
        }
        $task_ids = ( isset( $_POST['task_ids'] ) ? array_map( 'absint', (array) $_POST['task_ids'] ) : array() );
        if ( empty( $task_ids ) ) {
            wp_send_json_error( array(
                'message' => esc_html__( 'No tasks selected.', 'mediatasker' ),
            ), 400 );
        }
        $count = 0;
        foreach ( $task_ids as $id ) {
            $id = absint( $id );
            if ( get_post_type( $id ) === 'mediat_task' ) {
                // Delete attachments first if we want to be thorough,
                // but usually WP doesn't auto-delete attachments of a post.
                // However, for this plugin, tasks ARE the media.
                wp_delete_post( $id, true );
                if ( class_exists( 'Mediat_Work_Log' ) ) {
                    Mediat_Work_Log::insert_log( $id, 'media_deleted', array(
                        'task_id' => $id,
                    ) );
                }
                $count++;
            }
        }
        wp_send_json_success( array(
            'deleted' => $count,
        ) );
    }

    /**
     * AJAX handler: Prepare bulk download.
     *
     * @return void Sends JSON response and dies.
     */
    public static function bulk_download() {
        check_ajax_referer( 'mediat_frontend_nonce', 'nonce' );
        if ( !is_user_logged_in() || !current_user_can( 'read' ) ) {
            wp_send_json_error( array(
                'message' => esc_html__( 'Permission denied.', 'mediatasker' ),
            ), 403 );
        }
        $task_ids = ( isset( $_POST['task_ids'] ) ? array_map( 'absint', (array) $_POST['task_ids'] ) : array() );
        if ( empty( $task_ids ) ) {
            wp_send_json_error( array(
                'message' => esc_html__( 'No tasks selected.', 'mediatasker' ),
            ), 400 );
        }
        // Create temporary directory for the ZIP if it doesn't exist.
        $upload_dir = wp_upload_dir();
        $temp_dir = $upload_dir['basedir'] . '/media-tasker-downloads';
        if ( !file_exists( $temp_dir ) ) {
            wp_mkdir_p( $temp_dir );
        }
        // Clean up old ZIP files (older than 1 hour).
        $files = glob( $temp_dir . '/*.zip' );
        if ( $files ) {
            foreach ( $files as $file ) {
                if ( is_file( $file ) && time() - filemtime( $file ) > 3600 ) {
                    wp_delete_file( $file );
                }
            }
        }
        $date_str = current_time( 'd-m-Y' );
        $time_str = current_time( 'H-i-s' );
        $zip_filename = "MediaTasker-images-{$date_str}-{$time_str}.zip";
        $zip_path = $temp_dir . '/' . $zip_filename;
        if ( !class_exists( 'ZipArchive' ) ) {
            wp_send_json_error( array(
                'message' => esc_html__( 'ZipArchive not available on this server.', 'mediatasker' ),
            ), 500 );
        }
        $zip = new ZipArchive();
        if ( $zip->open( $zip_path, ZipArchive::CREATE | ZipArchive::OVERWRITE ) !== true ) {
            wp_send_json_error( array(
                'message' => esc_html__( 'Could not create ZIP file.', 'mediatasker' ),
            ), 500 );
        }
        $info_content = "Media Tasker Export\n";
        $info_content .= "Date: " . current_time( 'd-m-Y H:i:s' ) . "\n";
        $info_content .= "----------------------------------------\n\n";
        $added_filesCount = 0;
        $debug_info = array();
        foreach ( $task_ids as $task_id ) {
            $task_id = absint( $task_id );
            $post = get_post( $task_id );
            if ( !$post || $post->post_type !== 'mediat_task' ) {
                continue;
            }
            $attachment_id = get_post_thumbnail_id( $task_id );
            if ( !$attachment_id ) {
                continue;
            }
            $original_path = wp_get_original_image_path( $attachment_id );
            $image_path = ( $original_path && file_exists( $original_path ) ? $original_path : get_attached_file( $attachment_id ) );
            if ( !$image_path || !file_exists( $image_path ) ) {
                continue;
            }
            // Get Tags
            $tags = wp_get_post_terms( $task_id, 'mediat_tag', array(
                'fields' => 'names',
            ) );
            $tags_raw = ( is_wp_error( $tags ) ? $tags->get_error_message() : $tags );
            if ( is_wp_error( $tags ) ) {
                $tags = array();
            }
            $tags = array_filter( $tags, function ( $t ) {
                return !empty( trim( $t ) );
            } );
            $tag_str = ( !empty( $tags ) ? implode( ', ', $tags ) : '' );
            // Build filename: original_name-tag1-tag2.ext
            $original_name = get_the_title( $task_id );
            $ext = pathinfo( $original_name, PATHINFO_EXTENSION );
            $name_without_ext = pathinfo( $original_name, PATHINFO_FILENAME );
            $image_name = $name_without_ext;
            if ( !empty( $tags ) ) {
                foreach ( $tags as $tag ) {
                    $sanitized_tag = trim( $tag );
                    if ( $sanitized_tag !== '' ) {
                        $image_name .= '-' . $sanitized_tag;
                    }
                }
            }
            if ( $ext ) {
                $image_name .= '.' . $ext;
            }
            $debug_info[] = array(
                'task_id'       => $task_id,
                'title'         => $original_name,
                'tags_raw'      => $tags_raw,
                'tags_filtered' => array_values( $tags ),
                'filename'      => $image_name,
            );
            $zip->addFile( $image_path, $image_name );
            $added_filesCount++;
            // Get Comments
            $comments = get_comments( array(
                'post_id' => $task_id,
                'status'  => 'approve',
                'order'   => 'ASC',
            ) );
            $comment_str = "";
            foreach ( $comments as $comment ) {
                $comment_date = get_comment_date( 'd-m-Y H:i:s', $comment );
                $comment_str .= "- {$comment_date} by {$comment->comment_author}: {$comment->comment_content}\n";
            }
            $info_content .= "Image: " . $image_name . "\n";
            $info_content .= "Tags: " . $tag_str . "\n";
            $info_content .= "Comments:\n" . $comment_str;
            $info_content .= "----------------------------------------\n\n";
        }
        if ( $added_filesCount === 0 ) {
            $zip->close();
            wp_delete_file( $zip_path );
            wp_send_json_error( array(
                'message' => esc_html__( 'No images found for the selected tasks.', 'mediatasker' ),
            ), 400 );
        }
        $zip->addFromString( 'info.txt', $info_content );
        $zip->close();
        $updated_ids = array();
        if ( $added_filesCount > 0 ) {
            global $wpdb;
            foreach ( $task_ids as $task_id ) {
                $task_id = absint( $task_id );
                $terms = wp_get_object_terms( $task_id, 'mediat_view_source', array(
                    'fields' => 'slugs',
                ) );
                $view_type = ( !is_wp_error( $terms ) && !empty( $terms ) ? $terms[0] : 'original' );
                $current_status = get_post_status( $task_id );
                if ( $view_type === 'edited' && $current_status === 'mediat_not_dl' ) {
                    wp_update_post( array(
                        'ID'          => $task_id,
                        'post_status' => 'mediat_downloaded',
                    ) );
                    // wp_update_post handles cache cleaning.
                    if ( class_exists( 'Mediat_Work_Log' ) ) {
                        Mediat_Work_Log::insert_log( $task_id, 'status_change', array(
                            'new_status' => 'mediat_downloaded',
                        ) );
                    }
                    do_action( 'mediat_task_status_changed', $task_id, 'mediat_downloaded' );
                    $updated_ids[] = $task_id;
                } elseif ( $current_status === 'mediat_pending' ) {
                    wp_update_post( array(
                        'ID'          => $task_id,
                        'post_status' => 'mediat_in_progress',
                    ) );
                    if ( class_exists( 'Mediat_Work_Log' ) ) {
                        Mediat_Work_Log::insert_log( $task_id, 'status_change', array(
                            'new_status' => 'mediat_in_progress',
                        ) );
                    }
                    do_action( 'mediat_task_status_changed', $task_id, 'mediat_in_progress' );
                    $updated_ids[] = $task_id;
                }
            }
        }
        $download_url = $upload_dir['baseurl'] . '/media-tasker-downloads/' . $zip_filename;
        /* translators: %d: Number of files included in the ZIP. */
        $msg = sprintf( esc_html__( 'ZIP file created with %d items.', 'mediatasker' ), $added_filesCount );
        wp_send_json_success( array(
            'message'      => $msg,
            'download_url' => $download_url,
            'updated_ids'  => $updated_ids,
            'debug_tags'   => $debug_info,
        ) );
    }

    /**
     * Helper to render a single comment block.
     *
     * @param WP_Comment $comment The WordPress comment object.
     * @return string HTML for the comment.
     */
    public static function get_comment_html( $comment ) {
        $current_user_id = get_current_user_id();
        $is_mine = $comment->user_id && $comment->user_id == $current_user_id;
        $classes = 'mediat_thread-item';
        if ( $is_mine ) {
            $classes .= ' is-mine';
        }
        $author_name = $comment->comment_author;
        if ( empty( $author_name ) && $comment->user_id ) {
            $u = get_userdata( $comment->user_id );
            $author_name = ( $u ? $u->display_name : 'Unknown' );
        }
        if ( empty( $author_name ) ) {
            $author_name = 'Guest';
        }
        $date = get_comment_date( 'M j, g:i a', $comment->comment_ID );
        $html = '<div class="' . esc_attr( $classes ) . '" data-comment-id="' . esc_attr( $comment->comment_ID ) . '">';
        $html .= '  <div class="mediat_thread-meta">';
        $html .= '      <strong class="mediat_thread-author">' . esc_html( $author_name ) . '</strong> ';
        $html .= '      <span style="font-size:12px;color:#8c8f94;">' . esc_html( $date ) . '</span>';
        if ( $is_mine || current_user_can( 'manage_options' ) ) {
            $html .= '  <div class="mediat_thread-actions">';
            $html .= '      <button type="button" class="mediat_thread-btn mediat_edit-comment" title="' . esc_attr__( 'Edit', 'mediatasker' ) . '"><span class="dashicons dashicons-edit"></span></button>';
            $html .= '      <button type="button" class="mediat_thread-btn mediat_delete-comment" title="' . esc_attr__( 'Delete', 'mediatasker' ) . '"><span class="dashicons dashicons-trash"></span></button>';
            $html .= '  </div>';
        }
        $html .= '  </div>';
        $html .= '  <div class="mediat_thread-text">' . wp_kses_post( wpautop( $comment->comment_content ) ) . '</div>';
        $html .= '</div>';
        return $html;
    }

}
