<?php
/**
 * Disable_Comments Class
 *
 * @package dvcrs-core
 */

namespace DVCRS\Core;

/**
 * Disable_Comments Class
 */
class Disable_Comments {

	/**
	 * Disable_Comments constructor
	 */
	public function __construct() {

		\add_action( 'admin_head', [ $this, 'admin_css' ] );
		\add_action( 'admin_menu', [ $this, 'filter_admin_menu' ] );
		\add_action( 'admin_menu', [ $this, 'remove_meta_boxes' ] );
		\add_action( 'admin_bar_menu', [ $this, 'filter_admin_bar' ], 100 );
		\add_action( 'init', [ $this, 'remove_comment_support' ] );
		\add_action( 'template_redirect', [ $this, 'disable_comment_feed' ], 9 );
		\add_action( 'widgets_init', [ $this, 'disable_widget' ] );

		\add_filter( 'comment_link', '__return_false' );
		\add_filter( 'comments_link_feed', '__return_false' );
		\add_filter( 'comments_open', '__return_false' );
		\add_filter( 'comments_template', [ $this, 'comments_template' ] );
		\add_filter( 'feed_links_show_comments_feed', '__return_false' );
		\add_filter( 'get_comments_number', '__return_false' );
		\add_filter( 'pings_open', '__return_false' );
		\add_filter( 'post_comments_feed_link', '__return_false' );
		\add_filter( 'wp', [ $this, 'remove_pingback_header' ], 1000 );

	}

	/**
	 * Admin CSS for removing commenty things.
	 */
	public function admin_css() {
		?>
		<style>
			#dashboard_right_now .comment-count,
			#dashboard_right_now .comment-mod-count,
			#latest-comments,
			#welcome-panel .welcome-comments {
				display: none !important;
			}

			#welcome-panel .welcome-panel-last li:nth-child(2) {
				height: 0 !important;
				margin: 0 !important;;
				padding: 0 !important;;
			}
		</style>
		<?php
	}

	/**
	 * Send disabled message and 403 response.
	 */
	public function closed() {

		\wp_die( \__( 'Comments are disabled.', 'dvcrs' ), '', array( 'response' => 403 ) );

	}

	/**
	 * Load dummy template to stop posting of comments.
	 *
	 * @return string
	 */
	public function comments_template() {

		return dirname( __FILE__ ) . '/../assets/templates/comments-template.php';
	}

	/**
	 * Unregisters the Recent Comments widget.
	 */
	public function disable_widget() {

		\unregister_widget( 'WP_Widget_Recent_Comments' );

	}

	/**
	 * Disable comments feed.
	 */
	public function disable_comment_feed() {

		if ( \is_comment_feed() ) {
			$this->closed();
		}

	}

	/**
	 * Remove comments from admin menus.
	 */
	public function filter_admin_menu() {

		global $pagenow;

		if ( in_array( $pagenow, array( 'comment.php', 'edit-comments.php', 'options-discussion.php' ), true ) ) {
			$this->closed();
		}

		\remove_menu_page( 'edit-comments.php' );
		\remove_submenu_page( 'options-general.php', 'options-discussion.php' );

	}

	/**
	 * Remove comment menu from admin bar.
	 *
	 * @param \WP_Admin_Bar $wp_admin_bar The admin bar object.
	 */
	public function filter_admin_bar( $wp_admin_bar ) {

		$wp_admin_bar->remove_node( 'comments' );

	}

	/**
	 * Remove comments support from post types.
	 */
	public function remove_comment_support() {

		$types = array_keys( \get_post_types( array( 'public' => true ), 'objects' ) );

		if ( ! empty( $types ) ) {
			foreach ( $types as $type ) {
				if ( \post_type_supports( $type, 'comments' ) ) {
					\remove_post_type_support( $type, 'comments' );
					\remove_post_type_support( $type, 'trackbacks' );
				}
			}
		}

	}

	/**
	 * Remove admin meta boxes.
	 */
	public function remove_meta_boxes() {

		\remove_meta_box( 'commentsdiv', 'post', 'normal' );
		\remove_meta_box( 'commentstatusdiv', 'post', 'normal' );
		\remove_meta_box( 'trackbacksdiv', 'post', 'normal' );

	}

	/**
	 * Removes pingback header.
	 */
	public function remove_pingback_header() {

		header_remove( 'X-Pingback' );

	}
}

new Disable_Comments();
