-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcomment-free-zone.php
More file actions
215 lines (192 loc) · 5.74 KB
/
comment-free-zone.php
File metadata and controls
215 lines (192 loc) · 5.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/**
* A plugin to fully disable comments, trackbacks and all related features on your WordPress site.
*
* @package Comment_Free_Zone
*
* Plugin name: Comment-free zone
* Plugin URI: https://progressplanner.com/plugins/comment-free-zone/#utm_medium=readme&utm_source=w.org&utm_campaign=comment-free-zone
* Description: A plugin to fully disable comments, trackbacks and all related features on your WordPress site.
* Requires at least: 6.7
* Requires PHP: 7.4
* Version: 1.0.2
* Author: Team Progress Planner
* Author URI: https://prpl.fyi/about
* License: GPL-3.0+
* License URI: https://www.gnu.org/licenses/gpl-3.0.html
* Text Domain: comment-free-zone
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Disable comments and trackbacks in post types.
*/
class Comment_Free_Zone {
/**
* Constructor.
*/
public function __construct() {
add_action( 'init', [ $this, 'disable_comments' ] );
add_action( 'admin_init', [ $this, 'disable_comments' ] );
}
/**
* Disable comments and trackbacks in post types.
*
* @return void
*/
public function disable_comments() {
add_action(
'admin_menu',
function () {
remove_submenu_page( 'options-general.php', 'options-discussion.php' ); // Comments settings.
remove_menu_page( 'edit-comments.php' ); // Comments page.
},
999
);
add_action(
'admin_bar_menu',
function ( $wp_admin_bar ) {
$wp_admin_bar->remove_node( 'comments' );
},
999
);
// Disable comments REST API endpoint.
add_filter(
'rest_endpoints',
function ( $endpoints ) {
if ( isset( $endpoints['/wp/v2/comments'] ) ) {
unset( $endpoints['/wp/v2/comments'] );
}
if ( isset( $endpoints['/wp/v2/comments/(?P<id>[\d]+)'] ) ) {
unset( $endpoints['/wp/v2/comments/(?P<id>[\d]+)'] );
}
return $endpoints;
}
);
add_filter( 'manage_pages_columns', [ $this, 'remove_comments_column_from_pages' ] );
add_filter( 'comments_open', '__return_false', 20 );
add_filter( 'pings_open', '__return_false', 20 );
// Disable comment feeds.
add_action( 'do_feed_rss2', [ $this, 'disable_comment_feeds' ], 1 );
add_action( 'do_feed_rss', [ $this, 'disable_comment_feeds' ], 1 );
add_filter( 'feed_links_show_comments_feed', '__return_false' );
// Disable default comment & ping status.
add_filter(
'get_default_comment_status',
function () {
return 'closed';
},
999
);
// Disable comments on the frontend.
add_filter(
'comments_template',
function () {
return __DIR__ . '/templates/blank.php';
},
20
);
add_filter( 'comments_number', '__return_empty_string' );
add_filter( 'get_comments_number', '__return_zero' );
// Unregister the wp-block-comments block.
$comment_blocks = [
'comment',
'comment-author-name',
'comment-content',
'comment-date',
'comment-edit-link',
'comment-reply-link',
'comment-template',
'comments-pagination',
'comments-pagination-next',
'comments-pagination-numbers',
'comments-pagination-previous',
'comments-title',
'comments',
'latest-comments',
'post-comments-form',
];
$registry = WP_Block_Type_Registry::get_instance();
foreach ( $comment_blocks as $block ) {
if ( ! $registry->get_registered( 'core/' . $block ) ) {
continue;
}
$registry->unregister( 'core/' . $block );
// Filter the output of the block to be empty.
add_filter( 'render_block_core/' . $block, '__return_empty_string' );
}
// Disable outgoing pings.
add_action(
'pre_ping',
function ( &$links ) {
$links = [];
}
);
// Disable incoming pingbacks.
add_filter(
'xmlrpc_methods',
function ( $methods ) {
unset( $methods['pingback.ping'] );
return $methods;
}
);
// Disable support for comments and trackbacks in post types.
$post_types = get_post_types();
foreach ( $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' );
}
add_filter( "rest_{$post_type}_item_schema", [ $this, 'cleanup_rest_api_schema' ] );
// Remove relpies link from REST API responses.
add_filter( 'rest_prepare_' . $post_type, [ $this, 'cleanup_rest_prepare_post_type' ] );
}
}
/**
* Remove comment_status and ping_status from the REST API schema.
*
* @param string[][] $schema The schema.
*
* @return string[][] The modified schema.
*/
public function cleanup_rest_api_schema( $schema ) {
unset( $schema['properties']['comment_status'] );
unset( $schema['properties']['ping_status'] );
return $schema;
}
/**
* Remove the replies link from the REST API response - should only be present for posts and pages normally, but runs for all post types.
*
* @param WP_REST_Response $response The response object.
*
* @return WP_REST_Response The modified response object.
*/
public function cleanup_rest_prepare_post_type( $response ) {
$response->remove_link( 'replies' );
return $response;
}
/**
* Disable comment feeds.
*
* @return void
*/
public function disable_comment_feeds() {
if ( isset( $_SERVER['REQUEST_URI'] ) && strpos( esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ), 'comments' ) !== false ) {
wp_die( esc_html__( 'Comments are disabled.', 'comment-free-zone' ), '', [ 'response' => 403 ] );
}
}
/**
* Remove the Comments column from the Pages list table.
*
* @param string[] $columns The columns of the Pages list table.
*
* @return string[] The modified columns.
*/
public function remove_comments_column_from_pages( $columns ) {
unset( $columns['comments'] ); // Removes the Comments column.
return $columns;
}
}
new Comment_Free_Zone();