Skip to content

Commit 038f91a

Browse files
authored
Merge pull request #123 from alleyinteractive/feature/issue-48/disable-comments-blocks
Issue-48: `Disable_Comments` should disable its blocks
2 parents 97060a9 + bc30da5 commit 038f91a

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ admin stability. For technical details on how WP core implements preloading, ref
6060

6161
### `disable_comments`
6262

63-
This feature disables WordPress comments entirely, including the ability to post, view, edit, list, count, modify settings for, or access URLs that are related to comments completely.
63+
This feature disables WordPress comments entirely, including the ability to post, view, edit, list, count, modify settings for, or access URLs that are related to comments completely. The blocks are also removed from the Gutenberg block editor.
6464

6565
### `disable_custom_fields_meta_box`
6666

src/alley/wp/alleyvate/features/class-disable-comments.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ final class Disable_Comments implements Feature {
2424
public function boot(): void {
2525
add_action( 'add_meta_boxes', [ self::class, 'action__add_meta_boxes' ], 9999 );
2626
add_action( 'admin_bar_menu', [ self::class, 'action__admin_bar_menu' ], 9999 );
27+
add_action( 'admin_footer', [ self::class, 'action__admin_footer' ], 9999 );
2728
add_action( 'admin_init', [ self::class, 'action__admin_init' ], 0 );
2829
add_action( 'admin_menu', [ self::class, 'action__admin_menu' ], 9999 );
2930
add_action( 'init', [ self::class, 'action__init' ], 9999 );
@@ -54,6 +55,31 @@ public static function action__admin_bar_menu( \WP_Admin_Bar $wp_admin_bar ): vo
5455
$wp_admin_bar->remove_node( 'comments' );
5556
}
5657

58+
/**
59+
* Removes blocks related to core/comments from the admin block selector.
60+
*
61+
* JavaScript is used to selectively remove blocks from the editor.
62+
* The PHP filter for allowed blocks passes ‘true’ to allow all blocks by default,
63+
* so you can’t get the full list of blocks and selectively remove them.
64+
* https://developer.wordpress.org/news/2024/01/how-to-disable-specific-blocks-in-wordpress/#disable-blocks-with-php
65+
*/
66+
public static function action__admin_footer(): void {
67+
echo "
68+
<script>
69+
if (typeof wp?.domReady === 'function') {
70+
wp.domReady(() => {
71+
if (typeof wp?.blocks?.unregisterBlockType === 'function') {
72+
// Unregister blocks related to core comments.
73+
wp.blocks.unregisterBlockType('core/comments');
74+
wp.blocks.unregisterBlockType('core/post-comments-form');
75+
wp.blocks.unregisterBlockType('core/comments-query-loop');
76+
wp.blocks.unregisterBlockType('core/latest-comments');
77+
}
78+
});
79+
}
80+
</script>";
81+
}
82+
5783
/**
5884
* Redirects direct requests for the comments list and discussion settings page to the admin dashboard.
5985
*/

0 commit comments

Comments
 (0)