<?php

namespace ProudNerds\Admin;

use ProudNerds\Trait\Initializable;

/**
 * Changes the default WordPress comments behavior.
 */
class Comments
{
    use Initializable;

    private function __construct()
    {
        add_action('admin_menu', [$this, 'removeComments'], 10, 0);
    }

    /**
     * Remove support for certain post types.
     *
     * @return void
     */
    public function removeComments(): void
    {
        global $pagenow;

        if ($pagenow === 'edit-comments.php') {
            wp_redirect(admin_url());
            exit;
        }

        // Remove comments page in menu
        remove_menu_page('edit-comments.php');
        remove_submenu_page('options-general.php', 'options-discussion.php');

        // Remove comments metabox from dashboard
        remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');

        // Disable support for comments and trackbacks in post types
        foreach (get_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');
            }
        }
    }
}
