<?php
if (!defined('ABSPATH')) {
    die('You are not allowed to call this page directly.');
}

/**
 * FormShield Comments Control Class
 * 
 * Handles global comment disabling functionality
 * 
 * @since 1.0.5
 */
class FormShieldCommentsControl {
    
    private static $instance = null;
    
    public static function get_instance() {
        if (null === self::$instance) {
            self::$instance = new self();
        }
        return self::$instance;
    }
    
    private function __construct() {
        // Add filters to disable comments globally (works for all post types including attachments/images)
        add_filter('comments_open', array($this, 'disable_comments'), 20, 2);
        add_filter('pings_open', array($this, 'disable_comments'), 20, 2);
        add_filter('get_comments_number', array($this, 'hide_comments_count'), 20, 2);
        
        // Block comment submission at the earliest point
        add_filter('pre_comment_approved', array($this, 'block_comment_submission'), 1, 2);
        
        // Remove comment form from frontend
        add_action('comment_form_before', array($this, 'remove_comment_form_before'));
        add_action('comment_form_after', array($this, 'remove_comment_form_after'));
        add_filter('comment_form_defaults', array($this, 'remove_comment_form'), 20);
        add_action('wp_head', array($this, 'hide_comment_form_css'));
        
        // Disable comments for attachments in admin
        add_action('admin_init', array($this, 'disable_attachment_comments_admin'));
    }
    
    /**
     * Check if comments are disabled globally
     * 
     * @return bool
     */
    private function is_comments_disabled() {
        $options = form_shield()->get_options();
        return !empty($options['disable_all_comments']);
    }
    
    /**
     * Disable comments for all posts/pages/attachments/images
     * This filter works for ALL post types including attachments (images/media)
     * 
     * @param bool $open Whether comments are open
     * @param int $post_id Post ID
     * @return bool
     */
    public function disable_comments($open, $post_id) {
        if ($this->is_comments_disabled()) {
            // This works for all post types: posts, pages, attachments (images), custom post types, etc.
            return false;
        }
        return $open;
    }
    
    /**
     * Disable comments for attachments in admin area
     */
    public function disable_attachment_comments_admin() {
        if ($this->is_comments_disabled()) {
            // Close comments for all existing attachments
            add_action('admin_footer', array($this, 'close_attachment_comments_js'));
        }
    }
    
    /**
     * JavaScript to close comments for attachments in admin
     */
    public function close_attachment_comments_js() {
        if (get_current_screen() && get_current_screen()->id === 'attachment') {
            echo '<script type="text/javascript">
                jQuery(document).ready(function($) {
                    $("#comment_status").prop("checked", false).val("closed");
                    $("#ping_status").prop("checked", false).val("closed");
                });
            </script>';
        }
    }
    
    /**
     * Hide comments count
     * 
     * @param int $count Number of comments
     * @param int $post_id Post ID
     * @return int
     */
    public function hide_comments_count($count, $post_id) {
        if ($this->is_comments_disabled()) {
            return 0;
        }
        return $count;
    }
    
    /**
     * Block comment submission at WordPress level
     * 
     * @param int|string $approved Comment approval status
     * @param array $commentdata Comment data
     * @return string|int
     */
    public function block_comment_submission($approved, $commentdata) {
        if ($this->is_comments_disabled()) {
            // Return 'spam' to prevent comment from being saved
            return 'spam';
        }
        return $approved;
    }
    
    /**
     * Remove comment form before it renders
     */
    public function remove_comment_form_before() {
        if ($this->is_comments_disabled()) {
            ob_start();
        }
    }
    
    /**
     * Remove comment form after it renders
     */
    public function remove_comment_form_after() {
        if ($this->is_comments_disabled()) {
            ob_end_clean();
        }
    }
    
    /**
     * Remove comment form from frontend
     * 
     * @param array $defaults Comment form defaults
     * @return array
     */
    public function remove_comment_form($defaults) {
        if ($this->is_comments_disabled()) {
            // Return empty defaults to effectively hide the form
            return array();
        }
        return $defaults;
    }
    
    /**
     * Add CSS to hide comment form and related elements
     */
    public function hide_comment_form_css() {
        if ($this->is_comments_disabled()) {
            echo '<style type="text/css">
                /* Hide comment forms */
                #respond,
                #commentform,
                .comment-form,
                .comments-area,
                .comment-respond,
                .comment-form-comment,
                .comment-form-author,
                .comment-form-email,
                .comment-form-url,
                .comment-form-cookies-consent,
                .form-submit,
                .comment-reply-link,
                .comments-title,
                .comment-list,
                .comment-navigation,
                .comment-reply-title,
                .comment-form textarea,
                .comment-form input,
                .comment-form button,
                .comment-form .form-submit,
                /* Hide comment counts */
                .comments-link,
                .entry-comments,
                .post-comments,
                .comment-count,
                /* Hide comment sections */
                section.comments,
                div.comments,
                .entry-comments-link,
                /* Hide comments on attachment pages (images/media) */
                .attachment .comments-area,
                .attachment #respond,
                .attachment #commentform,
                .attachment .comment-form,
                .attachment .comment-list,
                .attachment .comments-title,
                .wp-attachment .comments-area,
                .wp-attachment #respond,
                .attachment-comments,
                /* Hide comment links on images */
                .wp-caption .comment-link,
                .attachment-comments-link,
                .post-attachment .comments-link {
                    display: none !important;
                    visibility: hidden !important;
                    height: 0 !important;
                    overflow: hidden !important;
                }
            </style>';
        }
    }
}

// Initialize the class
FormShieldCommentsControl::get_instance();

