Disable Comments - Version 1.10.0

Version Description

  • Disable "recent comments" Gutenberg block.
Download this release

Release Info

Developer solarissmoke
Plugin Icon 128x128 Disable Comments
Version 1.10.0
Comparing to
See all releases

Code changes from version 1.9.0 to 1.10.0

assets/disable-comments.js ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ "use strict";
2
+
3
+ wp.domReady(function () {
4
+ if (wp.blocks) {
5
+ wp.blocks.getBlockTypes().forEach(function (block) {
6
+ if (disable_comments.disabled_blocks.includes(block.name)) {
7
+ wp.blocks.unregisterBlockType(block.name);
8
+ }
9
+ });
10
+ }
11
+ });
12
+ //# sourceMappingURL=disable-comments.js.map
assets/disable-comments.js.map ADDED
@@ -0,0 +1 @@
 
1
+ {"version":3,"sources":["../src/disable-comments.js"],"names":["wp","domReady","blocks","getBlockTypes","forEach","block","disable_comments","disabled_blocks","includes","name","unregisterBlockType"],"mappings":";;AAAAA,EAAE,CAACC,QAAH,CAAY,YAAM;AACjB,MAAGD,EAAE,CAACE,MAAN,EAAc;AACbF,IAAAA,EAAE,CAACE,MAAH,CAAUC,aAAV,GAA0BC,OAA1B,CAAkC,UAACC,KAAD,EAAW;AAC5C,UAAIC,gBAAgB,CAACC,eAAjB,CAAiCC,QAAjC,CAA0CH,KAAK,CAACI,IAAhD,CAAJ,EAA2D;AAC1DT,QAAAA,EAAE,CAACE,MAAH,CAAUQ,mBAAV,CAA8BL,KAAK,CAACI,IAApC;AACA;AACD,KAJD;AAKA;AACD,CARD","sourcesContent":["wp.domReady(() => {\n\tif(wp.blocks) {\n\t\twp.blocks.getBlockTypes().forEach((block) => {\n\t\t\tif (disable_comments.disabled_blocks.includes(block.name)) {\n\t\t\t\twp.blocks.unregisterBlockType(block.name);\n\t\t\t}\n\t\t});\n\t}\n});\n"],"file":"disable-comments.js"}
disable-comments.php CHANGED
@@ -3,7 +3,7 @@
3
  Plugin Name: Disable Comments
4
  Plugin URI: https://wordpress.org/plugins/disable-comments/
5
  Description: Allows administrators to globally disable comments on their site. Comments can be disabled according to post type.
6
- Version: 1.9.0
7
  Author: Samir Shah
8
  Author URI: http://www.rayofsolaris.net/
9
  License: GPL2
@@ -11,11 +11,12 @@ Text Domain: disable-comments
11
  Domain Path: /languages/
12
  */
13
 
14
- if( !defined( 'ABSPATH' ) )
15
  exit;
 
16
 
17
  class Disable_Comments {
18
- const DB_VERSION = 6;
19
  private static $instance = null;
20
  private $options;
21
  private $networkactive;
@@ -33,15 +34,14 @@ class Disable_Comments {
33
  $this->networkactive = ( is_multisite() && array_key_exists( plugin_basename( __FILE__ ), (array) get_site_option( 'active_sitewide_plugins' ) ) );
34
 
35
  // Load options
36
- if( $this->networkactive ) {
37
  $this->options = get_site_option( 'disable_comments_options', array() );
38
- }
39
- else {
40
  $this->options = get_option( 'disable_comments_options', array() );
41
  }
42
 
43
  // If it looks like first run, check compat
44
- if( empty( $this->options ) ) {
45
  $this->check_compatibility();
46
  }
47
 
@@ -63,22 +63,23 @@ class Disable_Comments {
63
 
64
  private function check_db_upgrades() {
65
  $old_ver = isset( $this->options['db_version'] ) ? $this->options['db_version'] : 0;
66
- if( $old_ver < self::DB_VERSION ) {
67
- if( $old_ver < 2 ) {
68
  // upgrade options from version 0.2.1 or earlier to 0.3
69
  $this->options['disabled_post_types'] = get_option( 'disable_comments_post_types', array() );
70
  delete_option( 'disable_comments_post_types' );
71
  }
72
- if( $old_ver < 5 ) {
73
  // simple is beautiful - remove multiple settings in favour of one
74
  $this->options['remove_everywhere'] = isset( $this->options['remove_admin_menu_comments'] ) ? $this->options['remove_admin_menu_comments'] : false;
75
- foreach( array( 'remove_admin_menu_comments', 'remove_admin_bar_comments', 'remove_recent_comments', 'remove_discussion', 'remove_rc_widget' ) as $v )
76
- unset( $this->options[$v] );
 
77
  }
78
 
79
- foreach( array( 'remove_everywhere', 'extra_post_types' ) as $v ) {
80
- if( !isset( $this->options[$v] ) ) {
81
- $this->options[$v] = false;
82
  }
83
  }
84
 
@@ -88,10 +89,9 @@ class Disable_Comments {
88
  }
89
 
90
  private function update_options() {
91
- if( $this->networkactive ) {
92
  update_site_option( 'disable_comments_options', $this->options );
93
- }
94
- else {
95
  update_option( 'disable_comments_options', $this->options );
96
  }
97
  }
@@ -102,9 +102,9 @@ class Disable_Comments {
102
  private function get_disabled_post_types() {
103
  $types = $this->options['disabled_post_types'];
104
  // Not all extra_post_types might be registered on this particular site
105
- if( $this->networkactive ) {
106
- foreach( (array) $this->options['extra_post_types'] as $extra ) {
107
- if( post_type_exists( $extra ) ) {
108
  $types[] = $extra;
109
  }
110
  }
@@ -121,10 +121,10 @@ class Disable_Comments {
121
 
122
  private function init_filters() {
123
  // These need to happen now
124
- if( $this->options['remove_everywhere'] ) {
125
  add_action( 'widgets_init', array( $this, 'disable_rc_widget' ) );
126
  add_filter( 'wp_headers', array( $this, 'filter_wp_headers' ) );
127
- add_action( 'template_redirect', array( $this, 'filter_query' ), 9 ); // before redirect_canonical
128
 
129
  // Admin bar filtering has to happen here since WP 3.6
130
  add_action( 'template_redirect', array( $this, 'filter_admin_bar' ) );
@@ -134,18 +134,21 @@ class Disable_Comments {
134
  // These can happen later
135
  add_action( 'plugins_loaded', array( $this, 'register_text_domain' ) );
136
  add_action( 'wp_loaded', array( $this, 'init_wploaded_filters' ) );
 
 
 
137
  }
138
 
139
  public function register_text_domain() {
140
- load_plugin_textdomain( 'disable-comments', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
141
  }
142
 
143
- public function init_wploaded_filters(){
144
  $disabled_post_types = $this->get_disabled_post_types();
145
- if( !empty( $disabled_post_types ) ) {
146
- foreach( $disabled_post_types as $type ) {
147
  // we need to know what native support was for later
148
- if( post_type_supports( $type, 'comments' ) ) {
149
  $this->modified_types[] = $type;
150
  remove_post_type_support( $type, 'comments' );
151
  remove_post_type_support( $type, 'trackbacks' );
@@ -154,8 +157,8 @@ class Disable_Comments {
154
  add_filter( 'comments_array', array( $this, 'filter_existing_comments' ), 20, 2 );
155
  add_filter( 'comments_open', array( $this, 'filter_comment_status' ), 20, 2 );
156
  add_filter( 'pings_open', array( $this, 'filter_comment_status' ), 20, 2 );
157
- }
158
- elseif( is_admin() && !$this->options['remove_everywhere'] ) {
159
  // It is possible that $disabled_post_types is empty if other
160
  // plugins have disabled comments. Hence we also check for
161
  // remove_everywhere. If you still get a warning you probably
@@ -164,25 +167,25 @@ class Disable_Comments {
164
  }
165
 
166
  // Filters for the admin only
167
- if( is_admin() ) {
168
- if( $this->networkactive ) {
169
  add_action( 'network_admin_menu', array( $this, 'settings_menu' ) );
170
  add_action( 'network_admin_menu', array( $this, 'tools_menu' ) );
171
- add_filter( 'network_admin_plugin_action_links', array( $this, 'plugin_actions_links'), 10, 2 );
172
- }
173
- else {
174
  add_action( 'admin_menu', array( $this, 'settings_menu' ) );
175
  add_action( 'admin_menu', array( $this, 'tools_menu' ) );
176
- add_filter( 'plugin_action_links', array( $this, 'plugin_actions_links'), 10, 2 );
177
- if( is_multisite() ) // We're on a multisite setup, but the plugin isn't network activated.
178
  register_deactivation_hook( __FILE__, array( $this, 'single_site_deactivate' ) );
 
179
  }
180
 
181
  add_action( 'admin_notices', array( $this, 'discussion_notice' ) );
182
  add_filter( 'plugin_row_meta', array( $this, 'set_plugin_meta' ), 10, 2 );
183
 
184
- if( $this->options['remove_everywhere'] ) {
185
- add_action( 'admin_menu', array( $this, 'filter_admin_menu' ), 9999 ); // do this as late as possible
186
  add_action( 'admin_print_styles-index.php', array( $this, 'admin_css' ) );
187
  add_action( 'admin_print_styles-profile.php', array( $this, 'admin_css' ) );
188
  add_action( 'wp_dashboard_setup', array( $this, 'filter_dashboard' ) );
@@ -193,7 +196,7 @@ class Disable_Comments {
193
  else {
194
  add_action( 'template_redirect', array( $this, 'check_comment_template' ) );
195
 
196
- if( $this->options['remove_everywhere'] ) {
197
  add_filter( 'feed_links_show_comments_feed', '__return_false' );
198
  }
199
  }
@@ -205,8 +208,8 @@ class Disable_Comments {
205
  * and set it to True
206
  */
207
  public function check_comment_template() {
208
- if( is_singular() && ( $this->options['remove_everywhere'] || $this->is_post_type_disabled( get_post_type() ) ) ) {
209
- if( !defined( 'DISABLE_COMMENTS_REMOVE_COMMENTS_TEMPLATE' ) || DISABLE_COMMENTS_REMOVE_COMMENTS_TEMPLATE == true ) {
210
  // Kill the comments template.
211
  add_filter( 'comments_template', array( $this, 'dummy_comments_template' ), 20 );
212
  }
@@ -234,7 +237,7 @@ class Disable_Comments {
234
  * Issue a 403 for all comment feed requests.
235
  */
236
  public function filter_query() {
237
- if( is_comment_feed() ) {
238
  wp_die( __( 'Comments are closed.' ), '', array( 'response' => 403 ) );
239
  }
240
  }
@@ -243,25 +246,49 @@ class Disable_Comments {
243
  * Remove comment links from the admin bar.
244
  */
245
  public function filter_admin_bar() {
246
- if( is_admin_bar_showing() ) {
247
  // Remove comments links from admin bar
248
  remove_action( 'admin_bar_menu', 'wp_admin_bar_comments_menu', 60 );
249
- if( is_multisite() ) {
250
  add_action( 'admin_bar_menu', array( $this, 'remove_network_comment_links' ), 500 );
251
  }
252
  }
253
  }
254
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
255
  /*
256
  * Remove comment links from the admin bar in a multisite network.
257
  */
258
  public function remove_network_comment_links( $wp_admin_bar ) {
259
- if( $this->networkactive && is_user_logged_in() ) {
260
- foreach( (array) $wp_admin_bar->user->blogs as $blog ) {
261
  $wp_admin_bar->remove_menu( 'blog-' . $blog->userblog_id . '-c' );
262
  }
263
- }
264
- else {
265
  // We have no way to know whether the plugin is active on other sites, so only remove this one
266
  $wp_admin_bar->remove_menu( 'blog-' . get_current_blog_id() . '-c' );
267
  }
@@ -269,10 +296,11 @@ class Disable_Comments {
269
 
270
  public function discussion_notice() {
271
  $disabled_post_types = $this->get_disabled_post_types();
272
- if( get_current_screen()->id == 'options-discussion' && !empty( $disabled_post_types ) ) {
273
  $names = array();
274
- foreach( $disabled_post_types as $type )
275
- $names[$type] = get_post_type_object( $type )->labels->name;
 
276
 
277
  echo '<div class="notice notice-warning"><p>' . sprintf( __( 'Note: The <em>Disable Comments</em> plugin is currently active, and comments are completely disabled on: %s. Many of the settings below will not be applicable for those post types.', 'disable-comments' ), implode( __( ', ' ), $names ) ) . '</p></div>';
278
  }
@@ -282,7 +310,7 @@ class Disable_Comments {
282
  * Return context-aware settings page URL
283
  */
284
  private function settings_page_url() {
285
- $base = $this->networkactive ? network_admin_url( 'settings.php' ) : admin_url( 'options-general.php' );
286
  return add_query_arg( 'page', 'disable_comments_settings', $base );
287
  }
288
 
@@ -290,40 +318,43 @@ class Disable_Comments {
290
  * Return context-aware tools page URL
291
  */
292
  private function tools_page_url() {
293
- $base = $this->networkactive ? network_admin_url( 'settings.php' ) : admin_url( 'tools.php' );
294
  return add_query_arg( 'page', 'disable_comments_tools', $base );
295
  }
296
 
297
- public function setup_notice(){
298
- if( strpos( get_current_screen()->id, 'settings_page_disable_comments_settings' ) === 0 )
299
  return;
 
300
  $hascaps = $this->networkactive ? is_network_admin() && current_user_can( 'manage_network_plugins' ) : current_user_can( 'manage_options' );
301
- if( $hascaps ) {
302
- echo '<div class="updated fade"><p>' . sprintf( __( 'The <em>Disable Comments</em> plugin is active, but isn\'t configured to do anything yet. Visit the <a href="%s">configuration page</a> to choose which post types to disable comments on.', 'disable-comments'), esc_attr( $this->settings_page_url() ) ) . '</p></div>';
303
  }
304
  }
305
 
306
- public function filter_admin_menu(){
307
  global $pagenow;
308
 
309
- if ( $pagenow == 'comment.php' || $pagenow == 'edit-comments.php' )
310
  wp_die( __( 'Comments are closed.' ), '', array( 'response' => 403 ) );
 
311
 
312
  remove_menu_page( 'edit-comments.php' );
313
 
314
  if ( ! $this->discussion_settings_allowed() ) {
315
- if ( $pagenow == 'options-discussion.php' )
316
  wp_die( __( 'Comments are closed.' ), '', array( 'response' => 403 ) );
 
317
 
318
  remove_submenu_page( 'options-general.php', 'options-discussion.php' );
319
  }
320
  }
321
 
322
- public function filter_dashboard(){
323
  remove_meta_box( 'dashboard_recent_comments', 'dashboard', 'normal' );
324
  }
325
 
326
- public function admin_css(){
327
  echo '<style>
328
  #dashboard_right_now .comment-count,
329
  #dashboard_right_now .comment-mod-count,
@@ -335,7 +366,7 @@ class Disable_Comments {
335
  </style>';
336
  }
337
 
338
- public function filter_existing_comments($comments, $post_id) {
339
  $post = get_post( $post_id );
340
  return ( $this->options['remove_everywhere'] || $this->is_post_type_disabled( $post->post_type ) ) ? array() : $comments;
341
  }
@@ -345,11 +376,16 @@ class Disable_Comments {
345
  return ( $this->options['remove_everywhere'] || $this->is_post_type_disabled( $post->post_type ) ) ? false : $open;
346
  }
347
 
 
 
 
 
 
348
  public function disable_rc_widget() {
349
  unregister_widget( 'WP_Widget_Recent_Comments' );
350
  // The widget has added a style action when it was constructed - which will
351
  // still fire even if we now unregister the widget... so filter that out
352
- add_filter( 'show_recent_comments_widget_style', '__return_false');
353
  }
354
 
355
  public function set_plugin_meta( $links, $file ) {
@@ -363,11 +399,11 @@ class Disable_Comments {
363
 
364
  /**
365
  * Add links to Settings page
366
- */
367
  public function plugin_actions_links( $links, $file ) {
368
  static $plugin;
369
  $plugin = plugin_basename( __FILE__ );
370
- if( $file == $plugin && current_user_can('manage_options') ) {
371
  array_unshift(
372
  $links,
373
  sprintf( '<a href="%s">%s</a>', esc_attr( $this->settings_page_url() ), __( 'Settings' ) ),
@@ -380,10 +416,11 @@ class Disable_Comments {
380
 
381
  public function settings_menu() {
382
  $title = _x( 'Disable Comments', 'settings menu title', 'disable-comments' );
383
- if( $this->networkactive )
384
  add_submenu_page( 'settings.php', $title, $title, 'manage_network_plugins', 'disable_comments_settings', array( $this, 'settings_page' ) );
385
- else
386
  add_submenu_page( 'options-general.php', $title, $title, 'manage_options', 'disable_comments_settings', array( $this, 'settings_page' ) );
 
387
  }
388
 
389
  public function settings_page() {
@@ -392,10 +429,11 @@ class Disable_Comments {
392
 
393
  public function tools_menu() {
394
  $title = __( 'Delete Comments', 'disable-comments' );
395
- if( $this->networkactive )
396
  add_submenu_page( 'settings.php', $title, $title, 'manage_network_plugins', 'disable_comments_tools', array( $this, 'tools_page' ) );
397
- else
398
  add_submenu_page( 'tools.php', $title, $title, 'manage_options', 'disable_comments_tools', array( $this, 'tools_page' ) );
 
399
  }
400
 
401
  public function tools_page() {
@@ -403,7 +441,7 @@ class Disable_Comments {
403
  }
404
 
405
  private function discussion_settings_allowed() {
406
- if( defined( 'DISABLE_COMMENTS_ALLOW_DISCUSSION_SETTINGS' ) && DISABLE_COMMENTS_ALLOW_DISCUSSION_SETTINGS == true ) {
407
  return true;
408
  }
409
  }
3
  Plugin Name: Disable Comments
4
  Plugin URI: https://wordpress.org/plugins/disable-comments/
5
  Description: Allows administrators to globally disable comments on their site. Comments can be disabled according to post type.
6
+ Version: 1.10.2
7
  Author: Samir Shah
8
  Author URI: http://www.rayofsolaris.net/
9
  License: GPL2
11
  Domain Path: /languages/
12
  */
13
 
14
+ if ( ! defined( 'ABSPATH' ) ) {
15
  exit;
16
+ }
17
 
18
  class Disable_Comments {
19
+ const DB_VERSION = 6;
20
  private static $instance = null;
21
  private $options;
22
  private $networkactive;
34
  $this->networkactive = ( is_multisite() && array_key_exists( plugin_basename( __FILE__ ), (array) get_site_option( 'active_sitewide_plugins' ) ) );
35
 
36
  // Load options
37
+ if ( $this->networkactive ) {
38
  $this->options = get_site_option( 'disable_comments_options', array() );
39
+ } else {
 
40
  $this->options = get_option( 'disable_comments_options', array() );
41
  }
42
 
43
  // If it looks like first run, check compat
44
+ if ( empty( $this->options ) ) {
45
  $this->check_compatibility();
46
  }
47
 
63
 
64
  private function check_db_upgrades() {
65
  $old_ver = isset( $this->options['db_version'] ) ? $this->options['db_version'] : 0;
66
+ if ( $old_ver < self::DB_VERSION ) {
67
+ if ( $old_ver < 2 ) {
68
  // upgrade options from version 0.2.1 or earlier to 0.3
69
  $this->options['disabled_post_types'] = get_option( 'disable_comments_post_types', array() );
70
  delete_option( 'disable_comments_post_types' );
71
  }
72
+ if ( $old_ver < 5 ) {
73
  // simple is beautiful - remove multiple settings in favour of one
74
  $this->options['remove_everywhere'] = isset( $this->options['remove_admin_menu_comments'] ) ? $this->options['remove_admin_menu_comments'] : false;
75
+ foreach ( array( 'remove_admin_menu_comments', 'remove_admin_bar_comments', 'remove_recent_comments', 'remove_discussion', 'remove_rc_widget' ) as $v ) {
76
+ unset( $this->options[ $v ] );
77
+ }
78
  }
79
 
80
+ foreach ( array( 'remove_everywhere', 'extra_post_types' ) as $v ) {
81
+ if ( ! isset( $this->options[ $v ] ) ) {
82
+ $this->options[ $v ] = false;
83
  }
84
  }
85
 
89
  }
90
 
91
  private function update_options() {
92
+ if ( $this->networkactive ) {
93
  update_site_option( 'disable_comments_options', $this->options );
94
+ } else {
 
95
  update_option( 'disable_comments_options', $this->options );
96
  }
97
  }
102
  private function get_disabled_post_types() {
103
  $types = $this->options['disabled_post_types'];
104
  // Not all extra_post_types might be registered on this particular site
105
+ if ( $this->networkactive ) {
106
+ foreach ( (array) $this->options['extra_post_types'] as $extra ) {
107
+ if ( post_type_exists( $extra ) ) {
108
  $types[] = $extra;
109
  }
110
  }
121
 
122
  private function init_filters() {
123
  // These need to happen now
124
+ if ( $this->options['remove_everywhere'] ) {
125
  add_action( 'widgets_init', array( $this, 'disable_rc_widget' ) );
126
  add_filter( 'wp_headers', array( $this, 'filter_wp_headers' ) );
127
+ add_action( 'template_redirect', array( $this, 'filter_query' ), 9 ); // before redirect_canonical
128
 
129
  // Admin bar filtering has to happen here since WP 3.6
130
  add_action( 'template_redirect', array( $this, 'filter_admin_bar' ) );
134
  // These can happen later
135
  add_action( 'plugins_loaded', array( $this, 'register_text_domain' ) );
136
  add_action( 'wp_loaded', array( $this, 'init_wploaded_filters' ) );
137
+
138
+ // Disable "Latest comments" block in Gutenberg
139
+ add_action( 'enqueue_block_editor_assets', array( $this, 'filter_gutenberg_blocks') );
140
  }
141
 
142
  public function register_text_domain() {
143
+ load_plugin_textdomain( 'disable-comments', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
144
  }
145
 
146
+ public function init_wploaded_filters() {
147
  $disabled_post_types = $this->get_disabled_post_types();
148
+ if ( ! empty( $disabled_post_types ) ) {
149
+ foreach ( $disabled_post_types as $type ) {
150
  // we need to know what native support was for later
151
+ if ( post_type_supports( $type, 'comments' ) ) {
152
  $this->modified_types[] = $type;
153
  remove_post_type_support( $type, 'comments' );
154
  remove_post_type_support( $type, 'trackbacks' );
157
  add_filter( 'comments_array', array( $this, 'filter_existing_comments' ), 20, 2 );
158
  add_filter( 'comments_open', array( $this, 'filter_comment_status' ), 20, 2 );
159
  add_filter( 'pings_open', array( $this, 'filter_comment_status' ), 20, 2 );
160
+ add_filter( 'get_comments_number', array( $this, 'filter_comments_number' ), 20, 2 );
161
+ } elseif ( is_admin() && ! $this->options['remove_everywhere'] ) {
162
  // It is possible that $disabled_post_types is empty if other
163
  // plugins have disabled comments. Hence we also check for
164
  // remove_everywhere. If you still get a warning you probably
167
  }
168
 
169
  // Filters for the admin only
170
+ if ( is_admin() ) {
171
+ if ( $this->networkactive ) {
172
  add_action( 'network_admin_menu', array( $this, 'settings_menu' ) );
173
  add_action( 'network_admin_menu', array( $this, 'tools_menu' ) );
174
+ add_filter( 'network_admin_plugin_action_links', array( $this, 'plugin_actions_links' ), 10, 2 );
175
+ } else {
 
176
  add_action( 'admin_menu', array( $this, 'settings_menu' ) );
177
  add_action( 'admin_menu', array( $this, 'tools_menu' ) );
178
+ add_filter( 'plugin_action_links', array( $this, 'plugin_actions_links' ), 10, 2 );
179
+ if ( is_multisite() ) { // We're on a multisite setup, but the plugin isn't network activated.
180
  register_deactivation_hook( __FILE__, array( $this, 'single_site_deactivate' ) );
181
+ }
182
  }
183
 
184
  add_action( 'admin_notices', array( $this, 'discussion_notice' ) );
185
  add_filter( 'plugin_row_meta', array( $this, 'set_plugin_meta' ), 10, 2 );
186
 
187
+ if ( $this->options['remove_everywhere'] ) {
188
+ add_action( 'admin_menu', array( $this, 'filter_admin_menu' ), 9999 ); // do this as late as possible
189
  add_action( 'admin_print_styles-index.php', array( $this, 'admin_css' ) );
190
  add_action( 'admin_print_styles-profile.php', array( $this, 'admin_css' ) );
191
  add_action( 'wp_dashboard_setup', array( $this, 'filter_dashboard' ) );
196
  else {
197
  add_action( 'template_redirect', array( $this, 'check_comment_template' ) );
198
 
199
+ if ( $this->options['remove_everywhere'] ) {
200
  add_filter( 'feed_links_show_comments_feed', '__return_false' );
201
  }
202
  }
208
  * and set it to True
209
  */
210
  public function check_comment_template() {
211
+ if ( is_singular() && ( $this->options['remove_everywhere'] || $this->is_post_type_disabled( get_post_type() ) ) ) {
212
+ if ( ! defined( 'DISABLE_COMMENTS_REMOVE_COMMENTS_TEMPLATE' ) || DISABLE_COMMENTS_REMOVE_COMMENTS_TEMPLATE == true ) {
213
  // Kill the comments template.
214
  add_filter( 'comments_template', array( $this, 'dummy_comments_template' ), 20 );
215
  }
237
  * Issue a 403 for all comment feed requests.
238
  */
239
  public function filter_query() {
240
+ if ( is_comment_feed() ) {
241
  wp_die( __( 'Comments are closed.' ), '', array( 'response' => 403 ) );
242
  }
243
  }
246
  * Remove comment links from the admin bar.
247
  */
248
  public function filter_admin_bar() {
249
+ if ( is_admin_bar_showing() ) {
250
  // Remove comments links from admin bar
251
  remove_action( 'admin_bar_menu', 'wp_admin_bar_comments_menu', 60 );
252
+ if ( is_multisite() ) {
253
  add_action( 'admin_bar_menu', array( $this, 'remove_network_comment_links' ), 500 );
254
  }
255
  }
256
  }
257
 
258
+ /**
259
+ * Determines if scripts should be enqueued
260
+ */
261
+ public function filter_gutenberg_blocks( $hook ) {
262
+ global $post;
263
+
264
+ if ( $this->options['remove_everywhere'] || ( isset( $post->post_type ) && in_array( $post->post_type, $this->get_disabled_post_types(), true ) ) ) {
265
+ return $this->disable_comments_script();
266
+ }
267
+ }
268
+
269
+ /**
270
+ * Enqueues scripts
271
+ */
272
+ public function disable_comments_script() {
273
+ wp_enqueue_script( 'disable-comments-gutenberg', plugin_dir_url( __FILE__ ) . 'assets/disable-comments.js', array(), false, true );
274
+ wp_localize_script(
275
+ 'disable-comments-gutenberg',
276
+ 'disable_comments',
277
+ array(
278
+ 'disabled_blocks' => array( 'core/latest-comments' ),
279
+ )
280
+ );
281
+ }
282
+
283
  /*
284
  * Remove comment links from the admin bar in a multisite network.
285
  */
286
  public function remove_network_comment_links( $wp_admin_bar ) {
287
+ if ( $this->networkactive && is_user_logged_in() ) {
288
+ foreach ( (array) $wp_admin_bar->user->blogs as $blog ) {
289
  $wp_admin_bar->remove_menu( 'blog-' . $blog->userblog_id . '-c' );
290
  }
291
+ } else {
 
292
  // We have no way to know whether the plugin is active on other sites, so only remove this one
293
  $wp_admin_bar->remove_menu( 'blog-' . get_current_blog_id() . '-c' );
294
  }
296
 
297
  public function discussion_notice() {
298
  $disabled_post_types = $this->get_disabled_post_types();
299
+ if ( get_current_screen()->id == 'options-discussion' && ! empty( $disabled_post_types ) ) {
300
  $names = array();
301
+ foreach ( $disabled_post_types as $type ) {
302
+ $names[ $type ] = get_post_type_object( $type )->labels->name;
303
+ }
304
 
305
  echo '<div class="notice notice-warning"><p>' . sprintf( __( 'Note: The <em>Disable Comments</em> plugin is currently active, and comments are completely disabled on: %s. Many of the settings below will not be applicable for those post types.', 'disable-comments' ), implode( __( ', ' ), $names ) ) . '</p></div>';
306
  }
310
  * Return context-aware settings page URL
311
  */
312
  private function settings_page_url() {
313
+ $base = $this->networkactive ? network_admin_url( 'settings.php' ) : admin_url( 'options-general.php' );
314
  return add_query_arg( 'page', 'disable_comments_settings', $base );
315
  }
316
 
318
  * Return context-aware tools page URL
319
  */
320
  private function tools_page_url() {
321
+ $base = $this->networkactive ? network_admin_url( 'settings.php' ) : admin_url( 'tools.php' );
322
  return add_query_arg( 'page', 'disable_comments_tools', $base );
323
  }
324
 
325
+ public function setup_notice() {
326
+ if ( strpos( get_current_screen()->id, 'settings_page_disable_comments_settings' ) === 0 ) {
327
  return;
328
+ }
329
  $hascaps = $this->networkactive ? is_network_admin() && current_user_can( 'manage_network_plugins' ) : current_user_can( 'manage_options' );
330
+ if ( $hascaps ) {
331
+ echo '<div class="updated fade"><p>' . sprintf( __( 'The <em>Disable Comments</em> plugin is active, but isn\'t configured to do anything yet. Visit the <a href="%s">configuration page</a> to choose which post types to disable comments on.', 'disable-comments' ), esc_attr( $this->settings_page_url() ) ) . '</p></div>';
332
  }
333
  }
334
 
335
+ public function filter_admin_menu() {
336
  global $pagenow;
337
 
338
+ if ( $pagenow == 'comment.php' || $pagenow == 'edit-comments.php' ) {
339
  wp_die( __( 'Comments are closed.' ), '', array( 'response' => 403 ) );
340
+ }
341
 
342
  remove_menu_page( 'edit-comments.php' );
343
 
344
  if ( ! $this->discussion_settings_allowed() ) {
345
+ if ( $pagenow == 'options-discussion.php' ) {
346
  wp_die( __( 'Comments are closed.' ), '', array( 'response' => 403 ) );
347
+ }
348
 
349
  remove_submenu_page( 'options-general.php', 'options-discussion.php' );
350
  }
351
  }
352
 
353
+ public function filter_dashboard() {
354
  remove_meta_box( 'dashboard_recent_comments', 'dashboard', 'normal' );
355
  }
356
 
357
+ public function admin_css() {
358
  echo '<style>
359
  #dashboard_right_now .comment-count,
360
  #dashboard_right_now .comment-mod-count,
366
  </style>';
367
  }
368
 
369
+ public function filter_existing_comments( $comments, $post_id ) {
370
  $post = get_post( $post_id );
371
  return ( $this->options['remove_everywhere'] || $this->is_post_type_disabled( $post->post_type ) ) ? array() : $comments;
372
  }
376
  return ( $this->options['remove_everywhere'] || $this->is_post_type_disabled( $post->post_type ) ) ? false : $open;
377
  }
378
 
379
+ public function filter_comments_number( $count, $post_id ) {
380
+ $post = get_post( $post_id );
381
+ return ( $this->options['remove_everywhere'] || $this->is_post_type_disabled( $post->post_type ) ) ? 0 : $count;
382
+ }
383
+
384
  public function disable_rc_widget() {
385
  unregister_widget( 'WP_Widget_Recent_Comments' );
386
  // The widget has added a style action when it was constructed - which will
387
  // still fire even if we now unregister the widget... so filter that out
388
+ add_filter( 'show_recent_comments_widget_style', '__return_false' );
389
  }
390
 
391
  public function set_plugin_meta( $links, $file ) {
399
 
400
  /**
401
  * Add links to Settings page
402
+ */
403
  public function plugin_actions_links( $links, $file ) {
404
  static $plugin;
405
  $plugin = plugin_basename( __FILE__ );
406
+ if ( $file == $plugin && current_user_can( 'manage_options' ) ) {
407
  array_unshift(
408
  $links,
409
  sprintf( '<a href="%s">%s</a>', esc_attr( $this->settings_page_url() ), __( 'Settings' ) ),
416
 
417
  public function settings_menu() {
418
  $title = _x( 'Disable Comments', 'settings menu title', 'disable-comments' );
419
+ if ( $this->networkactive ) {
420
  add_submenu_page( 'settings.php', $title, $title, 'manage_network_plugins', 'disable_comments_settings', array( $this, 'settings_page' ) );
421
+ } else {
422
  add_submenu_page( 'options-general.php', $title, $title, 'manage_options', 'disable_comments_settings', array( $this, 'settings_page' ) );
423
+ }
424
  }
425
 
426
  public function settings_page() {
429
 
430
  public function tools_menu() {
431
  $title = __( 'Delete Comments', 'disable-comments' );
432
+ if ( $this->networkactive ) {
433
  add_submenu_page( 'settings.php', $title, $title, 'manage_network_plugins', 'disable_comments_tools', array( $this, 'tools_page' ) );
434
+ } else {
435
  add_submenu_page( 'tools.php', $title, $title, 'manage_options', 'disable_comments_tools', array( $this, 'tools_page' ) );
436
+ }
437
  }
438
 
439
  public function tools_page() {
441
  }
442
 
443
  private function discussion_settings_allowed() {
444
+ if ( defined( 'DISABLE_COMMENTS_ALLOW_DISCUSSION_SETTINGS' ) && DISABLE_COMMENTS_ALLOW_DISCUSSION_SETTINGS == true ) {
445
  return true;
446
  }
447
  }
includes/comments-template.php CHANGED
@@ -1,4 +1,5 @@
1
  <?php
2
- /* Dummy comments template file.
 
3
  * This replaces the theme's comment template when comments are disabled everywhere
4
- */
1
  <?php
2
+ /*
3
+ Dummy comments template file.
4
  * This replaces the theme's comment template when comments are disabled everywhere
5
+ */
includes/settings-page.php CHANGED
@@ -1,35 +1,37 @@
1
  <?php
2
- if( !defined( 'ABSPATH' ) ) {
3
  exit;
4
  }
5
 
6
  $typeargs = array( 'public' => true );
7
- if( $this->networkactive ) {
8
- $typeargs['_builtin'] = true; // stick to known types for network
9
  }
10
  $types = get_post_types( $typeargs, 'objects' );
11
- foreach( array_keys( $types ) as $type ) {
12
- if( ! in_array( $type, $this->modified_types ) && ! post_type_supports( $type, 'comments' ) ) // the type doesn't support comments anyway
13
- unset( $types[$type] );
 
14
  }
15
 
16
  if ( isset( $_POST['submit'] ) ) {
17
  check_admin_referer( 'disable-comments-admin' );
18
  $this->options['remove_everywhere'] = ( $_POST['mode'] == 'remove_everywhere' );
19
 
20
- if( $this->options['remove_everywhere'] )
21
  $disabled_post_types = array_keys( $types );
22
- else
23
- $disabled_post_types = empty( $_POST['disabled_types'] ) ? array() : (array) $_POST['disabled_types'];
 
24
 
25
  $disabled_post_types = array_intersect( $disabled_post_types, array_keys( $types ) );
26
 
27
  $this->options['disabled_post_types'] = $disabled_post_types;
28
 
29
  // Extra custom post types
30
- if( $this->networkactive && !empty( $_POST['extra_post_types'] ) ) {
31
- $extra_post_types = array_filter( array_map( 'sanitize_key', explode( ',', $_POST['extra_post_types'] ) ) );
32
- $this->options['extra_post_types'] = array_diff( $extra_post_types, array_keys( $types ) ); // Make sure we don't double up builtins
33
  }
34
 
35
  $this->update_options();
@@ -39,33 +41,38 @@ if ( isset( $_POST['submit'] ) ) {
39
  ?>
40
  <style> .indent {padding-left: 2em} </style>
41
  <div class="wrap">
42
- <h1><?php _ex( 'Disable Comments', 'settings page title', 'disable-comments') ?></h1>
43
  <?php
44
- if( $this->networkactive )
45
- echo '<div class="updated"><p>' . __( '<em>Disable Comments</em> is Network Activated. The settings below will affect <strong>all sites</strong> in this network.', 'disable-comments') . '</p></div>';
46
- if( WP_CACHE )
47
- echo '<div class="updated"><p>' . __( "It seems that a caching/performance plugin is active on this site. Please manually invalidate that plugin's cache after making any changes to the settings below.", 'disable-comments') . '</p></div>';
 
 
48
  ?>
49
  <form action="" method="post" id="disable-comments">
50
  <ul>
51
- <li><label for="remove_everywhere"><input type="radio" id="remove_everywhere" name="mode" value="remove_everywhere" <?php checked( $this->options['remove_everywhere'] );?> /> <strong><?php _e( 'Everywhere', 'disable-comments') ?></strong>: <?php _e( 'Disable all comment-related controls and settings in WordPress.', 'disable-comments') ?></label>
52
- <p class="indent"><?php printf( __( '%s: This option is global and will affect your entire site. Use it only if you want to disable comments <em>everywhere</em>. A complete description of what this option does is <a href="%s" target="_blank">available here</a>.', 'disable-comments' ), '<strong style="color: #900">' . __('Warning', 'disable-comments') . '</strong>', 'https://wordpress.org/plugins/disable-comments/other_notes/' ); ?></p>
53
  </li>
54
- <li><label for="selected_types"><input type="radio" id="selected_types" name="mode" value="selected_types" <?php checked( ! $this->options['remove_everywhere'] );?> /> <strong><?php _e( 'On certain post types', 'disable-comments') ?></strong>:</label>
55
  <p></p>
56
  <ul class="indent" id="listoftypes">
57
- <?php foreach( $types as $k => $v ) echo "<li><label for='post-type-$k'><input type='checkbox' name='disabled_types[]' value='$k' ". checked( in_array( $k, $this->options['disabled_post_types'] ), true, false ) ." id='post-type-$k'> {$v->labels->name}</label></li>";?>
 
 
 
58
  </ul>
59
- <?php if( $this->networkactive ) :?>
60
  <p class="indent" id="extratypes"><?php _e( 'Only the built-in post types appear above. If you want to disable comments on other custom post types on the entire network, you can supply a comma-separated list of post types below (use the slug that identifies the post type).', 'disable-comments' ); ?>
61
  <br /><label><?php _e( 'Custom post types:', 'disable-comments' ); ?> <input type="text" name="extra_post_types" size="30" value="<?php echo implode( ', ', (array) $this->options['extra_post_types'] ); ?>" /></label></p>
62
  <?php endif; ?>
63
- <p class="indent"><?php _e( 'Disabling comments will also disable trackbacks and pingbacks. All comment-related fields will also be hidden from the edit/quick-edit screens of the affected posts. These settings cannot be overridden for individual posts.', 'disable-comments') ?></p>
64
  </li>
65
  </ul>
66
 
67
  <?php wp_nonce_field( 'disable-comments-admin' ); ?>
68
- <p class="submit"><input class="button-primary" type="submit" name="submit" value="<?php _e( 'Save Changes', 'disable-comments') ?>"></p>
69
  </form>
70
  </div>
71
  <script>
1
  <?php
2
+ if ( ! defined( 'ABSPATH' ) ) {
3
  exit;
4
  }
5
 
6
  $typeargs = array( 'public' => true );
7
+ if ( $this->networkactive ) {
8
+ $typeargs['_builtin'] = true; // stick to known types for network
9
  }
10
  $types = get_post_types( $typeargs, 'objects' );
11
+ foreach ( array_keys( $types ) as $type ) {
12
+ if ( ! in_array( $type, $this->modified_types ) && ! post_type_supports( $type, 'comments' ) ) { // the type doesn't support comments anyway
13
+ unset( $types[ $type ] );
14
+ }
15
  }
16
 
17
  if ( isset( $_POST['submit'] ) ) {
18
  check_admin_referer( 'disable-comments-admin' );
19
  $this->options['remove_everywhere'] = ( $_POST['mode'] == 'remove_everywhere' );
20
 
21
+ if ( $this->options['remove_everywhere'] ) {
22
  $disabled_post_types = array_keys( $types );
23
+ } else {
24
+ $disabled_post_types = empty( $_POST['disabled_types'] ) ? array() : (array) $_POST['disabled_types'];
25
+ }
26
 
27
  $disabled_post_types = array_intersect( $disabled_post_types, array_keys( $types ) );
28
 
29
  $this->options['disabled_post_types'] = $disabled_post_types;
30
 
31
  // Extra custom post types
32
+ if ( $this->networkactive && ! empty( $_POST['extra_post_types'] ) ) {
33
+ $extra_post_types = array_filter( array_map( 'sanitize_key', explode( ',', $_POST['extra_post_types'] ) ) );
34
+ $this->options['extra_post_types'] = array_diff( $extra_post_types, array_keys( $types ) ); // Make sure we don't double up builtins
35
  }
36
 
37
  $this->update_options();
41
  ?>
42
  <style> .indent {padding-left: 2em} </style>
43
  <div class="wrap">
44
+ <h1><?php _ex( 'Disable Comments', 'settings page title', 'disable-comments' ); ?></h1>
45
  <?php
46
+ if ( $this->networkactive ) {
47
+ echo '<div class="updated"><p>' . __( '<em>Disable Comments</em> is Network Activated. The settings below will affect <strong>all sites</strong> in this network.', 'disable-comments' ) . '</p></div>';
48
+ }
49
+ if ( WP_CACHE ) {
50
+ echo '<div class="updated"><p>' . __( "It seems that a caching/performance plugin is active on this site. Please manually invalidate that plugin's cache after making any changes to the settings below.", 'disable-comments' ) . '</p></div>';
51
+ }
52
  ?>
53
  <form action="" method="post" id="disable-comments">
54
  <ul>
55
+ <li><label for="remove_everywhere"><input type="radio" id="remove_everywhere" name="mode" value="remove_everywhere" <?php checked( $this->options['remove_everywhere'] ); ?> /> <strong><?php _e( 'Everywhere', 'disable-comments' ); ?></strong>: <?php _e( 'Disable all comment-related controls and settings in WordPress.', 'disable-comments' ); ?></label>
56
+ <p class="indent"><?php printf( __( '%1$s: This option is global and will affect your entire site. Use it only if you want to disable comments <em>everywhere</em>. A complete description of what this option does is <a href="%2$s" target="_blank">available here</a>.', 'disable-comments' ), '<strong style="color: #900">' . __( 'Warning', 'disable-comments' ) . '</strong>', 'https://wordpress.org/plugins/disable-comments/other_notes/' ); ?></p>
57
  </li>
58
+ <li><label for="selected_types"><input type="radio" id="selected_types" name="mode" value="selected_types" <?php checked( ! $this->options['remove_everywhere'] ); ?> /> <strong><?php _e( 'On certain post types', 'disable-comments' ); ?></strong>:</label>
59
  <p></p>
60
  <ul class="indent" id="listoftypes">
61
+ <?php
62
+ foreach ( $types as $k => $v ) {
63
+ echo "<li><label for='post-type-$k'><input type='checkbox' name='disabled_types[]' value='$k' " . checked( in_array( $k, $this->options['disabled_post_types'] ), true, false ) . " id='post-type-$k'> {$v->labels->name}</label></li>";}
64
+ ?>
65
  </ul>
66
+ <?php if ( $this->networkactive ) : ?>
67
  <p class="indent" id="extratypes"><?php _e( 'Only the built-in post types appear above. If you want to disable comments on other custom post types on the entire network, you can supply a comma-separated list of post types below (use the slug that identifies the post type).', 'disable-comments' ); ?>
68
  <br /><label><?php _e( 'Custom post types:', 'disable-comments' ); ?> <input type="text" name="extra_post_types" size="30" value="<?php echo implode( ', ', (array) $this->options['extra_post_types'] ); ?>" /></label></p>
69
  <?php endif; ?>
70
+ <p class="indent"><?php _e( 'Disabling comments will also disable trackbacks and pingbacks. All comment-related fields will also be hidden from the edit/quick-edit screens of the affected posts. These settings cannot be overridden for individual posts.', 'disable-comments' ); ?></p>
71
  </li>
72
  </ul>
73
 
74
  <?php wp_nonce_field( 'disable-comments-admin' ); ?>
75
+ <p class="submit"><input class="button-primary" type="submit" name="submit" value="<?php _e( 'Save Changes', 'disable-comments' ); ?>"></p>
76
  </form>
77
  </div>
78
  <script>
includes/tools-page.php CHANGED
@@ -1,109 +1,113 @@
1
  <?php
2
- if( !defined( 'ABSPATH' ) ) {
3
  exit;
4
  }
5
  ?>
6
  <div class="wrap">
7
- <h1><?php _e( 'Delete Comments', 'disable-comments') ?></h1>
8
  <?php
9
 
10
  global $wpdb;
11
- $comments_count = $wpdb->get_var("SELECT count(comment_id) from $wpdb->comments");
12
- if ( $comments_count <= 0 ) { ?>
13
- <p><strong><?php _e( 'No comments available for deletion.', 'disable-comments') ?></strong></p>
 
14
  </div>
15
- <?php
16
  return;
17
  }
18
 
19
  $typeargs = array( 'public' => true );
20
- if( $this->networkactive ) {
21
- $typeargs['_builtin'] = true; // stick to known types for network
22
  }
23
  $types = get_post_types( $typeargs, 'objects' );
24
- foreach( array_keys( $types ) as $type ) {
25
- if( ! in_array( $type, $this->modified_types ) && ! post_type_supports( $type, 'comments' ) ) // the type doesn't support comments anyway
26
- unset( $types[$type] );
 
27
  }
28
 
29
  if ( isset( $_POST['delete'] ) && isset( $_POST['delete_mode'] ) ) {
30
  check_admin_referer( 'delete-comments-admin' );
31
 
32
- if( $_POST['delete_mode'] == 'delete_everywhere' ) {
33
- if ( $wpdb->query( "TRUNCATE $wpdb->commentmeta" ) != FALSE ){
34
- if ( $wpdb->query( "TRUNCATE $wpdb->comments" ) != FALSE ){
35
- $wpdb->query( "UPDATE $wpdb->posts SET comment_count = 0 WHERE post_author != 0" );
36
- $wpdb->query( "OPTIMIZE TABLE $wpdb->commentmeta" );
37
- $wpdb->query( "OPTIMIZE TABLE $wpdb->comments" );
38
- echo "<p style='color:green'><strong>" . __( 'All comments have been deleted.', 'disable-comments' ) . "</strong></p>";
39
- } else {
40
- echo "<p style='color:red'><strong>" . __( 'Internal error occured. Please try again later.', 'disable-comments' ) . "</strong></p>";
41
- }
42
- } else {
43
- echo "<p style='color:red'><strong>" . __( 'Internal error occured. Please try again later.', 'disable-comments' ) . "</strong></p>";
44
- }
45
  } else {
46
- $delete_post_types = empty( $_POST['delete_types'] ) ? array() : (array) $_POST['delete_types'];
47
  $delete_post_types = array_intersect( $delete_post_types, array_keys( $types ) );
48
 
49
  // Extra custom post types
50
- if( $this->networkactive && !empty( $_POST['delete_extra_post_types'] ) ) {
51
  $delete_extra_post_types = array_filter( array_map( 'sanitize_key', explode( ',', $_POST['delete_extra_post_types'] ) ) );
52
- $delete_extra_post_types = array_diff( $delete_extra_post_types, array_keys( $types ) ); // Make sure we don't double up builtins
53
- $delete_post_types = array_merge($delete_post_types, $delete_extra_post_types);
54
  }
55
 
56
  if ( ! empty( $delete_post_types ) ) {
57
  // Loop through post_types and remove comments/meta and set posts comment_count to 0
58
  foreach ( $delete_post_types as $delete_post_type ) {
59
- $wpdb->query( "DELETE cmeta FROM $wpdb->commentmeta cmeta INNER JOIN $wpdb->comments comments ON cmeta.comment_id=comments.comment_ID INNER JOIN $wpdb->posts posts ON comments.comment_post_ID=posts.ID WHERE posts.post_type = '$delete_post_type'" );
60
- $wpdb->query( "DELETE comments FROM $wpdb->comments comments INNER JOIN $wpdb->posts posts ON comments.comment_post_ID=posts.ID WHERE posts.post_type = '$delete_post_type'" );
61
- $wpdb->query( "UPDATE $wpdb->posts SET comment_count = 0 WHERE post_author != 0 AND post_type = '$delete_post_type'" );
62
 
63
- $post_type_object = get_post_type_object( $delete_post_type );
64
- $post_type_label = $post_type_object ? $post_type_object->labels->name : $delete_post_type;
65
- echo "<p style='color:green'><strong>" . sprintf( __( 'All comments have been deleted for %s.', 'disable-comments' ), $post_type_label ) . "</strong></p>";
66
  }
67
 
68
- $wpdb->query( "OPTIMIZE TABLE $wpdb->commentmeta" );
69
- $wpdb->query( "OPTIMIZE TABLE $wpdb->comments" );
70
 
71
- echo "<h4 style='color:green'><strong>" . __( 'Comment Deletion Complete', 'disable-comments') . "</strong></h4>";
72
  }
73
-
74
  }
75
 
76
- $comments_count = $wpdb->get_var("SELECT count(comment_id) from $wpdb->comments");
77
- if ( $comments_count <= 0 ) { ?>
78
- <p><strong><?php _e( 'No comments available for deletion.', 'disable-comments') ?></strong></p>
 
79
  </div>
80
- <?php
81
  return;
82
  }
83
-
84
  }
85
  ?>
86
  <form action="" method="post" id="delete-comments">
87
  <ul>
88
- <li><label for="delete_everywhere"><input type="radio" id="delete_everywhere" name="delete_mode" value="delete_everywhere" <?php checked( $this->options['remove_everywhere'] );?> /> <strong><?php _e( 'Everywhere', 'disable-comments') ?></strong>: <?php _e( 'Delete all comments in WordPress.', 'disable-comments') ?></label>
89
- <p class="indent"><?php printf( __( '%s: This function and will affect your entire site. Use it only if you want to delete comments <em>everywhere</em>.', 'disable-comments' ), '<strong style="color: #900">' . __('Warning', 'disable-comments') . '</strong>' ); ?></p>
90
  </li>
91
- <li><label for="selected_delete_types"><input type="radio" id="selected_delete_types" name="delete_mode" value="selected_delete_types" <?php checked( ! $this->options['remove_everywhere'] );?> /> <strong><?php _e( 'For certain post types', 'disable-comments') ?></strong>:</label>
92
  <p></p>
93
  <ul class="indent" id="listofdeletetypes">
94
- <?php foreach( $types as $k => $v ) echo "<li><label for='post-type-$k'><input type='checkbox' name='delete_types[]' value='$k' ". checked( in_array( $k, $this->options['disabled_post_types'] ), true, false ) ." id='post-type-$k'> {$v->labels->name}</label></li>";?>
 
 
 
95
  </ul>
96
- <?php if( $this->networkactive ) :?>
97
  <p class="indent" id="extradeletetypes"><?php _e( 'Only the built-in post types appear above. If you want to disable comments on other custom post types on the entire network, you can supply a comma-separated list of post types below (use the slug that identifies the post type).', 'disable-comments' ); ?>
98
  <br /><label><?php _e( 'Custom post types:', 'disable-comments' ); ?> <input type="text" name="delete_extra_post_types" size="30" value="<?php echo implode( ', ', (array) $this->options['extra_post_types'] ); ?>" /></label></p>
99
  <?php endif; ?>
100
- <p class="indent"><?php printf( __( '%s: Deleting comments will remove existing comment entries in the database and cannot be reverted without a database backup.', 'disable-comments'), '<strong style="color: #900">' . __('Warning', 'disable-comments') . '</strong>' ); ?></p>
101
  </li>
102
  </ul>
103
 
104
  <?php wp_nonce_field( 'delete-comments-admin' ); ?>
105
  <h4><?php _e( 'Total Comments:', 'disable-comments' ); ?> <?php echo $comments_count; ?></h4>
106
- <p class="submit"><input class="button-primary" type="submit" name="delete" value="<?php _e( 'Delete Comments', 'disable-comments') ?>"></p>
107
  </form>
108
  </div>
109
  <script>
1
  <?php
2
+ if ( ! defined( 'ABSPATH' ) ) {
3
  exit;
4
  }
5
  ?>
6
  <div class="wrap">
7
+ <h1><?php _e( 'Delete Comments', 'disable-comments' ); ?></h1>
8
  <?php
9
 
10
  global $wpdb;
11
+ $comments_count = $wpdb->get_var( "SELECT count(comment_id) from $wpdb->comments" );
12
+ if ( $comments_count <= 0 ) {
13
+ ?>
14
+ <p><strong><?php _e( 'No comments available for deletion.', 'disable-comments' ); ?></strong></p>
15
  </div>
16
+ <?php
17
  return;
18
  }
19
 
20
  $typeargs = array( 'public' => true );
21
+ if ( $this->networkactive ) {
22
+ $typeargs['_builtin'] = true; // stick to known types for network
23
  }
24
  $types = get_post_types( $typeargs, 'objects' );
25
+ foreach ( array_keys( $types ) as $type ) {
26
+ if ( ! in_array( $type, $this->modified_types ) && ! post_type_supports( $type, 'comments' ) ) { // the type doesn't support comments anyway
27
+ unset( $types[ $type ] );
28
+ }
29
  }
30
 
31
  if ( isset( $_POST['delete'] ) && isset( $_POST['delete_mode'] ) ) {
32
  check_admin_referer( 'delete-comments-admin' );
33
 
34
+ if ( $_POST['delete_mode'] == 'delete_everywhere' ) {
35
+ if ( $wpdb->query( "TRUNCATE $wpdb->commentmeta" ) != false ) {
36
+ if ( $wpdb->query( "TRUNCATE $wpdb->comments" ) != false ) {
37
+ $wpdb->query( "UPDATE $wpdb->posts SET comment_count = 0 WHERE post_author != 0" );
38
+ $wpdb->query( "OPTIMIZE TABLE $wpdb->commentmeta" );
39
+ $wpdb->query( "OPTIMIZE TABLE $wpdb->comments" );
40
+ echo "<p style='color:green'><strong>" . __( 'All comments have been deleted.', 'disable-comments' ) . '</strong></p>';
41
+ } else {
42
+ echo "<p style='color:red'><strong>" . __( 'Internal error occured. Please try again later.', 'disable-comments' ) . '</strong></p>';
43
+ }
44
+ } else {
45
+ echo "<p style='color:red'><strong>" . __( 'Internal error occured. Please try again later.', 'disable-comments' ) . '</strong></p>';
46
+ }
47
  } else {
48
+ $delete_post_types = empty( $_POST['delete_types'] ) ? array() : (array) $_POST['delete_types'];
49
  $delete_post_types = array_intersect( $delete_post_types, array_keys( $types ) );
50
 
51
  // Extra custom post types
52
+ if ( $this->networkactive && ! empty( $_POST['delete_extra_post_types'] ) ) {
53
  $delete_extra_post_types = array_filter( array_map( 'sanitize_key', explode( ',', $_POST['delete_extra_post_types'] ) ) );
54
+ $delete_extra_post_types = array_diff( $delete_extra_post_types, array_keys( $types ) ); // Make sure we don't double up builtins
55
+ $delete_post_types = array_merge( $delete_post_types, $delete_extra_post_types );
56
  }
57
 
58
  if ( ! empty( $delete_post_types ) ) {
59
  // Loop through post_types and remove comments/meta and set posts comment_count to 0
60
  foreach ( $delete_post_types as $delete_post_type ) {
61
+ $wpdb->query( "DELETE cmeta FROM $wpdb->commentmeta cmeta INNER JOIN $wpdb->comments comments ON cmeta.comment_id=comments.comment_ID INNER JOIN $wpdb->posts posts ON comments.comment_post_ID=posts.ID WHERE posts.post_type = '$delete_post_type'" );
62
+ $wpdb->query( "DELETE comments FROM $wpdb->comments comments INNER JOIN $wpdb->posts posts ON comments.comment_post_ID=posts.ID WHERE posts.post_type = '$delete_post_type'" );
63
+ $wpdb->query( "UPDATE $wpdb->posts SET comment_count = 0 WHERE post_author != 0 AND post_type = '$delete_post_type'" );
64
 
65
+ $post_type_object = get_post_type_object( $delete_post_type );
66
+ $post_type_label = $post_type_object ? $post_type_object->labels->name : $delete_post_type;
67
+ echo "<p style='color:green'><strong>" . sprintf( __( 'All comments have been deleted for %s.', 'disable-comments' ), $post_type_label ) . '</strong></p>';
68
  }
69
 
70
+ $wpdb->query( "OPTIMIZE TABLE $wpdb->commentmeta" );
71
+ $wpdb->query( "OPTIMIZE TABLE $wpdb->comments" );
72
 
73
+ echo "<h4 style='color:green'><strong>" . __( 'Comment Deletion Complete', 'disable-comments' ) . '</strong></h4>';
74
  }
 
75
  }
76
 
77
+ $comments_count = $wpdb->get_var( "SELECT count(comment_id) from $wpdb->comments" );
78
+ if ( $comments_count <= 0 ) {
79
+ ?>
80
+ <p><strong><?php _e( 'No comments available for deletion.', 'disable-comments' ); ?></strong></p>
81
  </div>
82
+ <?php
83
  return;
84
  }
 
85
  }
86
  ?>
87
  <form action="" method="post" id="delete-comments">
88
  <ul>
89
+ <li><label for="delete_everywhere"><input type="radio" id="delete_everywhere" name="delete_mode" value="delete_everywhere" <?php checked( $this->options['remove_everywhere'] ); ?> /> <strong><?php _e( 'Everywhere', 'disable-comments' ); ?></strong>: <?php _e( 'Delete all comments in WordPress.', 'disable-comments' ); ?></label>
90
+ <p class="indent"><?php printf( __( '%s: This function and will affect your entire site. Use it only if you want to delete comments <em>everywhere</em>.', 'disable-comments' ), '<strong style="color: #900">' . __( 'Warning', 'disable-comments' ) . '</strong>' ); ?></p>
91
  </li>
92
+ <li><label for="selected_delete_types"><input type="radio" id="selected_delete_types" name="delete_mode" value="selected_delete_types" <?php checked( ! $this->options['remove_everywhere'] ); ?> /> <strong><?php _e( 'For certain post types', 'disable-comments' ); ?></strong>:</label>
93
  <p></p>
94
  <ul class="indent" id="listofdeletetypes">
95
+ <?php
96
+ foreach ( $types as $k => $v ) {
97
+ echo "<li><label for='post-type-$k'><input type='checkbox' name='delete_types[]' value='$k' " . checked( in_array( $k, $this->options['disabled_post_types'] ), true, false ) . " id='post-type-$k'> {$v->labels->name}</label></li>";}
98
+ ?>
99
  </ul>
100
+ <?php if ( $this->networkactive ) : ?>
101
  <p class="indent" id="extradeletetypes"><?php _e( 'Only the built-in post types appear above. If you want to disable comments on other custom post types on the entire network, you can supply a comma-separated list of post types below (use the slug that identifies the post type).', 'disable-comments' ); ?>
102
  <br /><label><?php _e( 'Custom post types:', 'disable-comments' ); ?> <input type="text" name="delete_extra_post_types" size="30" value="<?php echo implode( ', ', (array) $this->options['extra_post_types'] ); ?>" /></label></p>
103
  <?php endif; ?>
104
+ <p class="indent"><?php printf( __( '%s: Deleting comments will remove existing comment entries in the database and cannot be reverted without a database backup.', 'disable-comments' ), '<strong style="color: #900">' . __( 'Warning', 'disable-comments' ) . '</strong>' ); ?></p>
105
  </li>
106
  </ul>
107
 
108
  <?php wp_nonce_field( 'delete-comments-admin' ); ?>
109
  <h4><?php _e( 'Total Comments:', 'disable-comments' ); ?> <?php echo $comments_count; ?></h4>
110
+ <p class="submit"><input class="button-primary" type="submit" name="delete" value="<?php _e( 'Delete Comments', 'disable-comments' ); ?>"></p>
111
  </form>
112
  </div>
113
  <script>
readme.txt CHANGED
@@ -1,9 +1,9 @@
1
  === Disable Comments ===
2
- Contributors: solarissmoke
3
  Donate link: http://www.rayofsolaris.net/donate/
4
  Tags: comments, disable, global
5
  Requires at least: 5.0
6
- Tested up to: 5.1
7
  Stable tag: trunk
8
 
9
  Allows administrators to globally disable comments on their site. Comments can be disabled according to post type. Multisite friendly. Provides tool to delete all comments or according to post type.
@@ -46,7 +46,7 @@ You can also bulk-edit the comment status of multiple posts from the [posts scre
46
 
47
  = I want to delete comments from my database. What do I do? =
48
 
49
- Go to the settings page for the disable comments plugin and utlize the Delete Comments tool to delete all comments or according to the specified post types from your database.
50
 
51
  == Details ==
52
 
@@ -74,6 +74,9 @@ These definitions can be made either in your main `wp-config.php` or in your the
74
 
75
  == Changelog ==
76
 
 
 
 
77
  = 1.9.0 =
78
  * Fix compatibility with WordPress 5.0 and above.
79
  * Remove deprecated "persistent mode" feature.
1
  === Disable Comments ===
2
+ Contributors: solarissmoke, garrett-eclipse
3
  Donate link: http://www.rayofsolaris.net/donate/
4
  Tags: comments, disable, global
5
  Requires at least: 5.0
6
+ Tested up to: 5.3
7
  Stable tag: trunk
8
 
9
  Allows administrators to globally disable comments on their site. Comments can be disabled according to post type. Multisite friendly. Provides tool to delete all comments or according to post type.
46
 
47
  = I want to delete comments from my database. What do I do? =
48
 
49
+ Go to the tools page for the Disable Comments plugin and utlize the Delete Comments tool to delete all comments or according to the specified post types from your database.
50
 
51
  == Details ==
52
 
74
 
75
  == Changelog ==
76
 
77
+ = 1.10.0 =
78
+ * Disable "recent comments" Gutenberg block.
79
+
80
  = 1.9.0 =
81
  * Fix compatibility with WordPress 5.0 and above.
82
  * Remove deprecated "persistent mode" feature.
src/disable-comments.js ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
1
+ wp.domReady(() => {
2
+ if(wp.blocks) {
3
+ wp.blocks.getBlockTypes().forEach((block) => {
4
+ if (disable_comments.disabled_blocks.includes(block.name)) {
5
+ wp.blocks.unregisterBlockType(block.name);
6
+ }
7
+ });
8
+ }
9
+ });
uninstall.php CHANGED
@@ -1,5 +1,6 @@
1
- <?php
2
- if( !defined('ABSPATH') && !defined('WP_UNINSTALL_PLUGIN') )
3
  exit;
 
4
 
5
- delete_site_option( 'disable_comments_options' );
1
+ <?php
2
+ if ( ! defined( 'ABSPATH' ) && ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
3
  exit;
4
+ }
5
 
6
+ delete_site_option( 'disable_comments_options' );