PluginProbe
Simply Disable Comments / trunk
Simply Disable Comments vtrunk
0.3.2 trunk 0.1.0 0.1.1 0.1.2 0.1.3 0.1.4 0.2 0.3 0.3.1
simply-disable-comments / plugin.php

plugin.php in Simply Disable Comments trunk, at plugin.php

330 lines 8.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: Simply Disable Comments
4 * Plugin URI: https://wordpress.org/plugins/simply-disable-comments/
5 * Description: A simple way to fully disable comments on your WordPress.
6 * Author: HandyPlugins
7 * Author URI: https://handyplugins.co/
8 * Text Domain: simply-disable-comments
9 * Domain Path: /languages
10 * Version: 0.3.2
11 * Requires at least: 5.0
12 * Requires PHP: 7.2
13 * License: GPLv2 or later
14 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
15 *
16 * @package SimplyDisableComments
17 */
18
19 namespace SimplyDisableComments;
20
21 if ( ! defined( 'ABSPATH' ) ) {
22 exit; // Exit if accessed directly.
23 }
24
25 define( 'SIMPLY_DISABLE_COMMENTS_VERSION', '0.3.2' );
26 define( 'SIMPLY_DISABLE_COMMENTS_PLUGIN_FILE', __FILE__ );
27 define( 'SIMPLY_DISABLE_COMMENTS_URL', plugin_dir_url( __FILE__ ) );
28 define( 'SIMPLY_DISABLE_COMMENTS_PATH', plugin_dir_path( __FILE__ ) );
29
30 $simply_disable_comments_network_activated = is_network_wide( __FILE__ );
31
32 if ( ! defined( 'SIMPLY_DISABLE_COMMENTS_IS_NETWORK' ) ) {
33 define( 'SIMPLY_DISABLE_COMMENTS_IS_NETWORK', $simply_disable_comments_network_activated );
34 }
35
36 /**
37 * Setup routine.
38 *
39 * @return void
40 */
41 function setup() {
42 add_action( 'init', __NAMESPACE__ . '\\i18n' );
43 add_action( 'admin_init', __NAMESPACE__ . '\\remove_from_post_type_support' );
44 add_action( 'admin_menu', __NAMESPACE__ . '\\remove_admin_menu' );
45 add_action( 'admin_print_styles-index.php', __NAMESPACE__ . '\\admin_styles' );
46 add_action( 'admin_print_styles-profile.php', __NAMESPACE__ . '\\admin_styles' );
47 add_action( 'wp_dashboard_setup', __NAMESPACE__ . '\\remove_dashboard_widget' );
48 add_filter( 'pre_option_default_pingback_flag', '__return_zero' );
49 add_filter( 'xmlrpc_methods', __NAMESPACE__ . '\\disable_xmlrpc' );
50 add_filter( 'rest_endpoints', __NAMESPACE__ . '\\remove_rest_api_endpoints' );
51 add_filter( 'rest_pre_insert_comment', __NAMESPACE__ . '\\disable_rest_api_comments', 10, 2 );
52 add_filter( 'comments_open', '__return_false', 20, 2 );
53 add_filter( 'pings_open', '__return_false', 20, 2 );
54 add_filter( 'comments_array', '__return_empty_array', 10, 2 );
55 add_action( 'template_redirect', __NAMESPACE__ . '\\remove_from_admin_bar' );
56 add_action( 'admin_init', __NAMESPACE__ . '\\remove_from_admin_bar' );
57 add_action( 'init', __NAMESPACE__ . '\\remove_comments_blocks', 20 );
58 add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\\enqueue_block_editor_scripts', 20 );
59 }
60
61 /**
62 * Registers the default textdomain.
63 *
64 * @return void
65 */
66 function i18n() {
67 $locale = apply_filters( 'simply_disable_comments_locale', get_locale(), 'simply-disable-comments' );
68 load_textdomain( 'simply-disable-comments', WP_LANG_DIR . '/simply-disable-comments/simply-disable-comments-' . $locale . '.mo' );
69 }
70
71 /**
72 * Unregister comments blocks
73 *
74 * @return void
75 */
76 function remove_comments_blocks() {
77 if ( ! class_exists( '\WP_Block_Type_Registry' ) ) {
78 return;
79 }
80
81 $registered_blocks = \WP_Block_Type_Registry::get_instance()->get_all_registered();
82
83 $comment_blocks = [
84 'core/comment-author-name',
85 'core/comment-content',
86 'core/comment-date',
87 'core/comment-edit-link',
88 'core/comment-reply-link',
89 'core/comment-template',
90 'core/comments',
91 'core/comments-pagination',
92 'core/comments-pagination-next',
93 'core/comments-pagination-numbers',
94 'core/comments-pagination-previous',
95 'core/comments-title',
96 'core/latest-comments',
97 'core/post-comments-form',
98 'core/post-comments',
99 'core/post-comments-count',
100 'core/post-comments-link',
101 ];
102
103 foreach ( $comment_blocks as $block ) {
104 if ( isset( $registered_blocks[ $block ] ) ) {
105 unregister_block_type( $block );
106 }
107 }
108 }
109
110 /**
111 * Remove registered comments blocks from the editor
112 *
113 * @return void
114 */
115 function enqueue_block_editor_scripts() {
116
117 $assets_data = include_once SIMPLY_DISABLE_COMMENTS_PATH . '/dist/js/editor.asset.php';
118
119 wp_enqueue_script(
120 'simply-disable-comments-block-editor',
121 SIMPLY_DISABLE_COMMENTS_URL . 'dist/js/editor.js',
122 $assets_data['dependencies'],
123 $assets_data['version'],
124 true
125 );
126 }
127
128 /**
129 * Remove comment item from admin bar
130 *
131 * @return void
132 */
133 function remove_from_admin_bar() {
134 if ( is_admin_bar_showing() ) {
135 remove_action( 'admin_bar_menu', 'wp_admin_bar_comments_menu', 60 );
136 if ( is_multisite() ) {
137 add_action( 'admin_bar_menu', __NAMESPACE__ . '\\remove_from_network_admin_bar', 500 );
138 }
139 }
140 }
141
142 /**
143 * Remove from network admin bar
144 *
145 * @param \WP_Admin_Bar $wp_admin_bar Admin bar instance.
146 *
147 * @return void
148 */
149 function remove_from_network_admin_bar( $wp_admin_bar ) {
150 $wp_admin_bar->remove_menu( 'blog-' . get_current_blog_id() . '-c' );
151 }
152
153 /**
154 * Disable support for comments and trackbacks in post types
155 *
156 * @return void
157 */
158 function remove_from_post_type_support() {
159 foreach ( get_post_types() as $post_type ) {
160 if ( post_type_supports( $post_type, 'comments' ) ) {
161 \remove_post_type_support( $post_type, 'comments' );
162 \remove_post_type_support( $post_type, 'trackbacks' );
163 }
164 }
165 }
166
167 /**
168 * Remove admin menu
169 *
170 * @return void
171 */
172 function remove_admin_menu() {
173 global $pagenow;
174
175 if ( 'comment.php' === $pagenow || 'edit-comments.php' === $pagenow ) {
176 wp_die( esc_html__( 'Comments are closed.', 'simply-disable-comments' ), '', [ 'response' => 403 ] );
177 }
178
179 remove_menu_page( 'edit-comments.php' );
180
181 if ( 'options-discussion.php' === $pagenow ) {
182 wp_die( esc_html__( 'Comments are closed.', 'simply-disable-comments' ), '', [ 'response' => 403 ] );
183 }
184
185 remove_submenu_page( 'options-general.php', 'options-discussion.php' );
186 }
187
188 /**
189 * Hide on dashboard
190 *
191 * @return void
192 */
193 function admin_styles() {
194 ?>
195 <style>
196 #dashboard_right_now .comment-count,
197 #dashboard_right_now .comment-mod-count,
198 #latest-comments,
199 #welcome-panel .welcome-comments,
200 .user-comment-shortcuts-wrap {
201 display: none !important;
202 }
203 </style>
204 <?php
205 }
206
207 /**
208 * Remove dashboard widget
209 *
210 * @return void
211 */
212 function remove_dashboard_widget() {
213 remove_meta_box( 'dashboard_recent_comments', 'dashboard', 'normal' );
214 }
215
216 /**
217 * Remove newComment method from XMLRPC
218 *
219 * @param array $methods XMLRPC methods.
220 *
221 * @return mixed
222 */
223 function disable_xmlrpc( $methods ) {
224 unset( $methods['wp.newComment'] );
225
226 return $methods;
227 }
228
229
230 /**
231 * Remove the comments endpoint for the REST API
232 *
233 * @param array $endpoints Registered endpoints.
234 */
235 function remove_rest_api_endpoints( $endpoints ) {
236 unset( $endpoints['comments'] );
237
238 return $endpoints;
239 }
240
241 /**
242 * Remove the comments endpoint for the REST API
243 *
244 * @param array|\WP_Error $prepared_comment The prepared comment data for wp_insert_comment().
245 * @param \WP_REST_Request $request Request used to insert the comment.
246 *
247 * @return \WP_Error
248 */
249 function disable_rest_api_comments( $prepared_comment, $request ) {
250 return new \WP_Error( 'rest_comments_disabled', esc_html__( 'Comments are closed.', 'simply-disable-comments' ), [ 'status' => 403 ] );
251 }
252
253 /**
254 * Register general settings on multisite (to give site based control)
255 *
256 * @return void
257 */
258 function register_general_settings_field() {
259 register_setting( 'general', 'simply_disable_comments_enable_comments', 'boolval' );
260
261 add_settings_field(
262 'simply-disable-comments-enable-comments',
263 esc_html__( 'Enable Comments', 'simply-disable-comments' ),
264 __NAMESPACE__ . '\\render_general_settings_field',
265 'general'
266 );
267 }
268
269 /**
270 * Settings field
271 *
272 * @return void
273 */
274 function render_general_settings_field() {
275 $value = get_option( 'simply_disable_comments_enable_comments', false );
276 ?>
277 <fieldset>
278 <legend class="screen-reader-text">
279 <span><?php esc_html_e( 'Enable Comments', 'simply-disable-comments' ); ?></span>
280 </legend>
281 <label for="simply_disable_comments_enable_comments">
282 <input <?php checked( true, $value ); ?>name="simply_disable_comments_enable_comments" type="checkbox" id="simply_disable_comments_enable_comments" value="1">
283 <?php esc_html_e( 'Enable commenting on this site', 'simply-disable-comments' ); ?>
284 </label>
285 </fieldset>
286 <?php
287 }
288
289 /**
290 * Is plugin activated network wide?
291 *
292 * @param string $plugin_file file path
293 *
294 * @return bool
295 * @since 1.0
296 */
297 function is_network_wide( $plugin_file ) {
298 if ( ! is_multisite() ) {
299 return false;
300 }
301
302 if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
303 require_once ABSPATH . '/wp-admin/includes/plugin.php';
304 }
305
306 return is_plugin_active_for_network( plugin_basename( $plugin_file ) );
307 }
308
309 add_action( 'plugins_loaded', __NAMESPACE__ . '\\init' );
310
311 /**
312 * Bootstrap the plugin
313 *
314 * @return void
315 */
316 function init() {
317 $comments_enabled = false;
318
319 if ( SIMPLY_DISABLE_COMMENTS_IS_NETWORK ) {
320 add_filter( 'admin_init', __NAMESPACE__ . '\\register_general_settings_field' );
321 $comments_enabled = get_option( 'simply_disable_comments_enable_comments', false );
322 }
323
324 $comment_status = apply_filters( 'simply_disable_comments_enable_comments', $comments_enabled );
325
326 if ( ! $comment_status ) {
327 setup();
328 }
329 }
330