<?php
/**
 * Name: Disable Comments
 * Description: Completely disable comments functionality site-wide across all post types, pages, and media attachments to prevent spam and remove discussion features entirely
 * Category: Content
 * Icon: -
 * UseCases: [
 *   {
 *     "title": "Business Website",
 *     "description": "Completely disable comments on corporate website where blog posts are announcements that don't need discussion."
 *   },
 *   {
 *     "title": "Spam Prevention",
 *     "description": "Turn off comments sitewide to eliminate comment spam and moderation workload on portfolio or brochure sites."
 *   },
 *   {
 *     "title": "Portfolio Showcase",
 *     "description": "Remove comment functionality from design portfolio where feedback comes through contact form, not public comments."
 *   }
 * ]
 * Added: 2025-06-18
 * Tags: comments, disable, remove, spam, discussion, pingback, trackback
 * WhereToFind: {
 *   "location": "Works automatically site-wide",
 *   "url": "",
 *   "instructions": "Comments disabled on all posts and pages",
 *   "newTab": false
 * }
 */

// Prevent direct access
if (!defined('ABSPATH')) exit;

class SwitchboardDisableComments {

    public function __construct() {
        add_action('admin_init', [$this, 'disable_comments_post_types_support']);
        add_filter('comments_open', '__return_false', 20, 2);
        add_filter('pings_open', '__return_false', 20, 2);
        add_filter('comments_array', '__return_empty_array', 10, 2);
        add_action('admin_menu', [$this, 'disable_comments_admin_menu']);
        add_action('admin_init', [$this, 'disable_comments_admin_menu_redirect']);
        add_action('admin_init', [$this, 'disable_comments_dashboard']);
        add_action('init', [$this, 'disable_comments_admin_bar']);
        add_action('wp_before_admin_bar_render', [$this, 'disable_comments_admin_bar_render']);
    }

    /**
     * Register data locations for cleanup system
     */
    public function register_data_locations() {
        return [
            'options' => [],
            'option_patterns' => [],
            'tables' => [],
            'post_meta' => [],
            'user_meta' => [],
        ];
    }
    
    public function disable_comments_post_types_support() {
        $post_types = get_post_types();
        foreach ($post_types as $post_type) {
            if (post_type_supports($post_type, 'comments')) {
                remove_post_type_support($post_type, 'comments');
                remove_post_type_support($post_type, 'trackbacks');
            }
        }
    }
    
    public function disable_comments_admin_menu() {
        remove_menu_page('edit-comments.php');
    }
    
    public function disable_comments_admin_menu_redirect() {
        global $pagenow;
        if ($pagenow === 'edit-comments.php') {
            wp_safe_redirect(admin_url());
            exit;
        }
    }
    
    public function disable_comments_dashboard() {
        remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');
    }
    
    public function disable_comments_admin_bar() {
        if (is_admin_bar_showing()) {
            remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60);
        }
    }
    
    public function disable_comments_admin_bar_render() {
        global $wp_admin_bar;
        if ($wp_admin_bar && is_object($wp_admin_bar)) {
            $wp_admin_bar->remove_menu('comments');
        }
    }
}

$switchboard_instance = new SwitchboardDisableComments();
Switchboard_Data_Registry::get_instance()->auto_register($switchboard_instance, __FILE__);