55 * @package Comment_Free_Zone
66 *
77 * Plugin name: Comment-free zone
8- * Plugin URI: https://prpl.fyi/ comment-free-zone
8+ * Plugin URI: https://progressplanner.com/plugins/comment-free-zone/#utm_medium=readme&utm_source=w.org&utm_campaign= comment-free-zone
99 * Description: A plugin to fully disable comments, trackbacks and all related features on your WordPress site.
1010 * Requires at least: 6.3
1111 * Requires PHP: 7.4
@@ -57,6 +57,20 @@ function ( $wp_admin_bar ) {
5757 999
5858 );
5959
60+ // Disable comments REST API endpoint.
61+ add_filter (
62+ 'rest_endpoints ' ,
63+ function ( $ endpoints ) {
64+ if ( isset ( $ endpoints ['/wp/v2/comments ' ] ) ) {
65+ unset( $ endpoints ['/wp/v2/comments ' ] );
66+ }
67+ if ( isset ( $ endpoints ['/wp/v2/comments/(?P<id>[\d]+) ' ] ) ) {
68+ unset( $ endpoints ['/wp/v2/comments/(?P<id>[\d]+) ' ] );
69+ }
70+ return $ endpoints ;
71+ }
72+ );
73+
6074 add_filter ( 'manage_pages_columns ' , [ $ this , 'remove_comments_column_from_pages ' ] );
6175 add_filter ( 'comments_open ' , '__return_false ' , 20 );
6276 add_filter ( 'pings_open ' , '__return_false ' , 20 );
@@ -66,6 +80,15 @@ function ( $wp_admin_bar ) {
6680 add_action ( 'do_feed_rss ' , [ $ this , 'disable_comment_feeds ' ], 1 );
6781 add_filter ( 'feed_links_show_comments_feed ' , '__return_false ' );
6882
83+ // Disable default comment & ping status.
84+ add_filter (
85+ 'get_default_comment_status ' ,
86+ function () {
87+ return 'closed ' ;
88+ },
89+ 999
90+ );
91+
6992 // Disable comments on the frontend.
7093 add_filter (
7194 'comments_template ' ,
@@ -103,10 +126,7 @@ function () {
103126 }
104127 $ registry ->unregister ( 'core/ ' . $ block );
105128 // Filter the output of the block to be empty.
106- add_filter (
107- 'render_block_core/ ' . $ block ,
108- '__return_empty_string '
109- );
129+ add_filter ( 'render_block_core/ ' . $ block , '__return_empty_string ' );
110130 }
111131
112132 // Disable outgoing pings.
@@ -133,9 +153,40 @@ function ( $methods ) {
133153 remove_post_type_support ( $ post_type , 'comments ' );
134154 remove_post_type_support ( $ post_type , 'trackbacks ' );
135155 }
156+
157+ add_filter ( "rest_ {$ post_type }_item_schema " , [ $ this , 'cleanup_rest_api_schema ' ] );
158+
159+ // Remove relpies link from REST API responses.
160+ add_filter ( 'rest_prepare_ ' . $ post_type , [ $ this , 'cleanup_rest_prepare_post_type ' ] );
136161 }
137162 }
138163
164+ /**
165+ * Remove comment_status and ping_status from the REST API schema.
166+ *
167+ * @param string[][] $schema The schema.
168+ *
169+ * @return string[][] The modified schema.
170+ */
171+ public function cleanup_rest_api_schema ( $ schema ) {
172+ unset( $ schema ['properties ' ]['comment_status ' ] );
173+ unset( $ schema ['properties ' ]['ping_status ' ] );
174+ return $ schema ;
175+ }
176+
177+ /**
178+ * Remove the replies link from the REST API response - should only be present for posts and pages normally, but runs for all post types.
179+ *
180+ * @param WP_REST_Response $response The response object.
181+ *
182+ * @return WP_REST_Response The modified response object.
183+ */
184+ public function cleanup_rest_prepare_post_type ( $ response ) {
185+ $ response ->remove_link ( 'replies ' );
186+
187+ return $ response ;
188+ }
189+
139190 /**
140191 * Disable comment feeds.
141192 *
0 commit comments