Skip to content

Commit

Permalink
feat: add option to disable discussions feature completely
Browse files Browse the repository at this point in the history
  • Loading branch information
thorbrink committed Oct 22, 2024
1 parent c5008aa commit a63b46d
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 0 deletions.
2 changes: 2 additions & 0 deletions library/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Municipio\BrandedEmails\ApplyMailHtmlTemplate;
use Municipio\BrandedEmails\HtmlTemplate\Config\HtmlTemplateConfigService;
use Municipio\BrandedEmails\HtmlTemplate\DefaultHtmlTemplate;
use Municipio\Comment\OptionalDisableDiscussionFeature;
use Municipio\Comment\OptionalHideDiscussionWhenLoggedOut;
use Municipio\Config\Features\SchemaData\SchemaDataConfigInterface;
use Municipio\Content\ResourceFromApi\Api\ResourceFromApiRestController;
Expand Down Expand Up @@ -218,6 +219,7 @@ public function __construct(
new \Municipio\Comment\Likes();
new \Municipio\Comment\Filters();
new \Municipio\Comment\Form();
$this->hooksRegistrar->register(new OptionalDisableDiscussionFeature($this->wpService, $this->acfService));
$this->hooksRegistrar->register(new OptionalHideDiscussionWhenLoggedOut($this->wpService, $this->acfService));

/**
Expand Down
157 changes: 157 additions & 0 deletions library/Comment/OptionalDisableDiscussionFeature.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?php

namespace Municipio\Comment;

use AcfService\Contracts\GetField;
use Municipio\HooksRegistrar\Hookable;
use WP_Admin_Bar;
use WpService\WpService;

/**
* Optional disable discussion feature.
*/
class OptionalDisableDiscussionFeature implements Hookable
{
/**
* Constructor.
*/
public function __construct(private WpService $wpService, private GetField $acfService)
{
}

/**
* @inheritDoc
*/
public function addHooks(): void
{
if (!$this->isDisabled()) {
return;
}

$this->wpService->addFilter('render_block', [$this, 'preventLatestCommentsBlockFromRendering'], 10, 2);
$this->wpService->addAction('admin_init', [$this, 'disableSupportForPostTypes']);
$this->wpService->addAction('admin_init', [$this, 'redirectUserIfOnEditCommentsPage']);
$this->wpService->addAction('admin_init', [$this, 'redirectUserIfOnSettingsPage']);
$this->wpService->addAction('admin_init', [$this, 'removeMetaBox']);
$this->wpService->addAction('admin_menu', [$this, 'removeSettingsPage']);
$this->wpService->addAction('admin_menu', [$this, 'removeFromAdminMenu']);
$this->wpService->addAction('admin_bar_menu', [$this, 'removeFromAdminBar'], 61);
$this->wpService->addAction('widgets_init', [$this, 'disableWidget']);

// Close comments on frontend
$this->wpService->addFilter('comments_open', '__return_false', 20, 2);
$this->wpService->addFilter('pings_open', '__return_false', 20, 2);
$this->wpService->addFilter('comments_array', '__return_empty_array', 10, 2);
}

/**
* Checks if the discussion feature is disabled.
*
* @return bool
*/
private function isDisabled(): bool
{
return $this->acfService->getField('disable_discussion_feature', 'option') === true;
}

/**
* Prevents the latest comments block from rendering.
*/
public function preventLatestCommentsBlockFromRendering(string $block_content, array $block): string
{
if ($block['blockName'] === 'core/latest-comments') {
return '';
}

if ($block['blockName'] === 'core/group') {
foreach ($block['innerBlocks'] as $innerBlock) {
if ($innerBlock['blockName'] === 'core/latest-comments') {
return '';
}
}
}

return $block_content;
}

/**
* Disables support for comments and trackbacks on all post types.
*/
public function disableSupportForPostTypes(): void
{
foreach ($this->wpService->getPostTypes() as $post_type) {
if ($this->wpService->postTypeSupports($post_type, 'comments')) {
$this->wpService->removePostTypeSupport($post_type, 'comments');
$this->wpService->removePostTypeSupport($post_type, 'trackbacks');
}
}
}

/**
* Removes the recent comments meta box.
*/
public function removeMetaBox(): void
{
$this->wpService->removeMetaBox('dashboard_recent_comments', 'dashboard', 'normal');
}

/**
* Redirects the user if they are on the edit comments page.
*/
public function redirectUserIfOnEditCommentsPage(): void
{
global $pagenow;

if ($pagenow === 'edit-comments.php') {
$this->wpService->wpSafeRedirect($this->wpService->adminUrl());
exit;
}
}

/**
* Redirects the user if they are on the discussion settings page.
*/
public function redirectUserIfOnSettingsPage(): void
{
global $pagenow;

if ($pagenow === 'options-discussion.php') {
$this->wpService->wpSafeRedirect($this->wpService->adminUrl());
exit;
}
}

/**
* Removes the discussion settings page.
*/
public function removeSettingsPage(): void
{
$this->wpService->removeSubmenuPage('options-general.php', 'options-discussion.php');
}

/**
* Removes the comments link from the admin menu.
*/
public function removeFromAdminMenu()
{
$this->wpService->removeMenuPage('edit-comments.php');
}

/**
* Removes the comments link from the admin bar.
*
* @param WP_Admin_Bar $wpAdminBar
*/
public function removeFromAdminBar(WP_Admin_Bar $wpAdminBar): void
{
$wpAdminBar->remove_node('comments');
}

/**
* Disables the recent comments widget.
*/
public function disableWidget(): void
{
$this->wpService->unregisterWidget('WP_Widget_Recent_Comments');
}
}

0 comments on commit a63b46d

Please sign in to comment.