diff --git a/library/App.php b/library/App.php index 637151421..3439e0b7b 100644 --- a/library/App.php +++ b/library/App.php @@ -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; @@ -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)); /** diff --git a/library/Comment/OptionalDisableDiscussionFeature.php b/library/Comment/OptionalDisableDiscussionFeature.php new file mode 100644 index 000000000..5a1814939 --- /dev/null +++ b/library/Comment/OptionalDisableDiscussionFeature.php @@ -0,0 +1,157 @@ +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'); + } +}