This repository has been archived by the owner on May 16, 2023. It is now read-only.
/
remove-comments-absolute.php
executable file
·703 lines (597 loc) · 18.8 KB
/
remove-comments-absolute.php
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
<?php
/**
* This is plugin for WordPress to remove the comment functions and his views.
*
* Plugin Name: Remove Comments Absolutely
* Plugin URI: https://github.com/bueltge/Remove-Comments-Absolutely
* Text Domain: remove_comments_absolute
* Domain Path: /languages
* Description: Deactivate comments functions and remove areas absolutely from the WordPress install
* Author: Frank Bültge
* Version: 1.5.3
* Last access: 2017-01-08
* License: GPLv3+
* Author URI: http://bueltge.de/
*
* @package WordPress
*/
if ( ! class_exists( 'Remove_Comments_Absolute' ) ) {
add_action( 'plugins_loaded', array( 'Remove_Comments_Absolute', 'get_object' ) );
/**
* Class Remove_Comments_Absolute.
*
* Hint: It is a God class, but a newer version is a lot of work.
* Maybe other users will refactoring the plugin for better code, that we can also include tests.
*/
class Remove_Comments_Absolute {
/**
* Class object.
*
* @var null
*/
static private $classobj;
/**
* Back end pages for the hint, that comment are inactive.
*
* @var array
*/
private static $comment_pages = array(
'comment.php',
'edit-comments.php',
'moderation.php',
'options-discussion.php',
);
/**
* Constructor, init on defined hooks of WP and include second class.
*
* @access public
* @since 0.0.1
* @uses add_filter, add_action
*/
public function __construct() {
// Remove update check.
add_filter( 'site_transient_update_plugins', array( $this, 'remove_update_nag' ) );
add_filter( 'the_posts', array( $this, 'set_comment_status' ) );
add_filter( 'comments_open', array( $this, 'close_comments' ), 20, 2 );
add_filter( 'pings_open', array( $this, 'close_comments' ), 20, 2 );
add_action( 'admin_init', array( $this, 'remove_comments' ) );
add_action( 'admin_menu', array( $this, 'remove_menu_items' ) );
add_filter( 'add_menu_classes', array( $this, 'add_menu_classes' ) );
// Remove items in dashboard.
add_action( 'admin_footer-index.php', array( $this, 'remove_dashboard_comments_areas' ) );
// Change admin bar items.
add_action( 'admin_bar_menu', array( $this, 'remove_admin_bar_comment_items' ), 999 );
add_action( 'admin_bar_menu', array( $this, 'remove_network_comment_items' ), 999 );
// Replace the theme's or the core comments template with an empty one.
add_filter( 'comments_template', array( $this, 'comments_template' ) );
// Remove comment feed.
remove_action( 'wp_head', 'feed_links', 2 );
remove_action( 'wp_head', 'feed_links_extra', 3 );
add_action( 'wp_head', array( $this, 'feed_links' ), 2 );
add_action( 'wp_head', array( $this, 'feed_links_extra' ), 3 );
add_action( 'template_redirect', array( $this, 'filter_query' ), 9 );
add_filter( 'wp_headers', array( $this, 'filter_wp_headers' ) );
// Remove default comment widget.
add_action( 'widgets_init', array( $this, 'unregister_default_wp_widgets' ), 1 );
// Remove comment options in profile page.
add_action( 'personal_options', array( $this, 'remove_profile_items' ) );
// Replace xmlrpc methods.
add_filter( 'xmlrpc_methods', array( $this, 'xmlrpc_replace_methods' ) );
// Set content of <wfw:commentRss> to empty string.
add_filter( 'post_comments_feed_link', '__return_empty_string' );
// Set content of <slash:comments> to empty string.
add_filter( 'get_comments_number', '__return_empty_string' );
// Return empty string for post comment link, which takes care of <comments>.
add_filter( 'get_comments_link', '__return_empty_string' );
// Remove comments popup.
add_filter( 'query_vars', array( $this, 'filter_query_vars' ) );
// Remove 'Discussion Settings' help tab from post edit screen.
add_action( 'admin_head-post.php', array( $this, 'remove_help_tabs' ), 10, 3 );
// Remove rewrite rules used for comment feed archives.
add_filter( 'comments_rewrite_rules', '__return_empty_array', 99 );
// Remove rewrite rules for the legacy comment feed and post type comment pages.
add_filter( 'rewrite_rules_array', array( $this, 'filter_rewrite_rules_array' ), 99 );
// Return an object with each comment stat set to zero.
add_filter( 'wp_count_comments', array( $this, 'filter_count_comments' ) );
}
/**
* Handler for the action 'init'. Instantiates this class.
*
* @access public
* @since 0.0.1
* @return null|Remove_Comments_Absolute $classobj object
*/
public static function get_object() {
if ( NULL === self::$classobj ) {
self::$classobj = new self;
}
return self::$classobj;
}
/**
* Disable plugin update notifications.
*
* @since 1.0.0 04/02/2012
* @link http://dd32.id.au/2011/03/01/disable-plugin-update-notification-for-a-specific-plugin-in-wordpress-3-1/
*
* @param array|string $value
*
* @return array|string $value
*/
public function remove_update_nag( $value ) {
if ( isset( $value->response[ plugin_basename( __FILE__ ) ] )
&& ! empty( $value->response[ plugin_basename( __FILE__ ) ] )
) {
unset( $value->response[ plugin_basename( __FILE__ ) ] );
}
return $value;
}
/**
* Set the status on posts and pages - is_singular().
*
* @access public
* @since 0.0.1
* @uses is_singular
*
* @param string $posts
*
* @return string $posts
*/
public function set_comment_status( $posts ) {
if ( ! empty( $posts ) && is_singular() ) {
$posts[ 0 ]->comment_status = 'closed';
$posts[ 0 ]->ping_status = 'closed';
}
return $posts;
}
/**
* Close comments, if open.
*
* @access public
* @since 0.0.1
*
* @param string|boolean $open
* @param string|integer $post_id
*
* @return bool|string $open
*/
public function close_comments( $open, $post_id ) {
// If not open, than back.
if ( ! $open ) {
return $open;
}
$post = get_post( $post_id );
// For all post types.
if ( $post->post_type ) {
return FALSE;
} // 'closed' don`t work; @see http://codex.wordpress.org/Option_Reference#Discussion
return $open;
}
/**
* Return default closed comment status.
*
* @since 04/08/2013
* @return string
*/
public function return_closed() {
return 'closed';
}
/**
* Change options for don't use comments.
*
* Remove meta boxes on edit pages.
* Remove support on all post types for comments.
* Remove menu-entries.
* Disallow comments pages direct access.
*
* @access public
* @since 0.0.1
* @return void
*/
public function remove_comments() {
global $pagenow;
// For integer values.
foreach ( array( 'comments_notify', 'default_pingback_flag' ) as $option ) {
add_filter( 'pre_option_' . $option, '__return_zero' );
}
// For string false.
foreach ( array( 'default_comment_status', 'default_ping_status' ) as $option ) {
add_filter( 'pre_option_' . $option, array( $this, 'return_closed' ) );
}
// For all post types.
// As alternative define an array( 'post', 'page' ).
foreach ( get_post_types() as $post_type ) {
// Remove the comment status meta box.
remove_meta_box( 'commentstatusdiv', $post_type, 'normal' );
// Remove the trackbacks meta box.
remove_meta_box( 'trackbacksdiv', $post_type, 'normal' );
// Remove all comments/trackbacks from tables.
if ( post_type_supports( $post_type, 'comments' ) ) {
remove_post_type_support( $post_type, 'comments' );
remove_post_type_support( $post_type, 'trackbacks' );
}
}
// Filter for different pages.
if ( in_array( $pagenow, self::$comment_pages, FALSE ) ) {
wp_die(
esc_html__( 'Comments are disabled on this site.', 'remove_comments_absolute' ),
'',
array( 'response' => 403 )
);
exit();
}
// Remove dashboard meta box for recents comments.
remove_meta_box( 'dashboard_recent_comments', 'dashboard', 'normal' );
}
/**
* Remove menu-entries.
*
* @access public
* @since 0.0.3
* @uses remove_meta_box, remove_post_type_support
* @return void
*/
public function remove_menu_items() {
remove_menu_page( 'edit-comments.php' );
remove_submenu_page( 'options-general.php', 'options-discussion.php' );
}
/**
* Add class for last menu entry with no 20.
*
* @access public
* @since 0.0.1
*
* @param array|string $menu
*
* @return array|string $menu
*/
public function add_menu_classes( $menu ) {
if ( isset( $menu[ 20 ][ 4 ] ) ) {
$menu[ 20 ][ 4 ] .= ' menu-top-last';
}
return $menu;
}
/**
* Remove comment related elements from the admin dashboard via JS.
*
* @access public
* @since 0.0.1
* $return string with js
*/
public function remove_dashboard_comments_areas() {
?>
<script type="text/javascript">
//<![CDATA[
jQuery( document ).ready( function( $ ) {
// Welcome screen
$( '.welcome-comments' ).parent().remove();
// 'Right Now' dashboard widget
$( 'div.table_discussion:first' ).remove();
// 'Right Now' dashbaord widget since WP version 3.8, second ID since WP 4.0
$( 'div#dash-right-now, #dashboard_right_now' ).find( '.comment-count' ).remove();
// 'Activity' dashboard widget, since WP version 3.8
$( 'div#dashboard_activity' ).find( '#latest-comments' ).remove();
} );
//]]>
</script>
<?php
}
/**
* Remove comment entry in Admin Bar.
*
* @access public
* @since 0.0.1
*
* @param WP_Admin_Bar $wp_admin_bar WP_Admin_Bar instance, passed by reference.
*
* @return null
*/
public function remove_admin_bar_comment_items( $wp_admin_bar ) {
if ( ! is_admin_bar_showing() ) {
return NULL;
}
// Remove comment item in blog list for "My Sites" in Admin Bar.
if ( isset( $GLOBALS[ 'blog_id' ] ) ) {
$wp_admin_bar->remove_node( 'blog-' . $GLOBALS[ 'blog_id' ] . '-c' );
}
// Remove entry in admin bar.
$wp_admin_bar->remove_node( 'comments' );
}
/**
* Remove comments item on network admin bar.
*
* @since 04/08/2013
* @internal param Array $wp_admin_bar
* @return void
*/
public function remove_network_comment_items() {
if ( ! is_admin_bar_showing() ) {
return NULL;
}
global $wp_admin_bar;
if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
}
if ( is_multisite() && is_plugin_active_for_network( plugin_basename( __FILE__ ) ) ) {
foreach ( (array) $wp_admin_bar->user->blogs as $blog ) {
$wp_admin_bar->remove_node( 'blog-' . $blog->userblog_id . '-c' );
}
}
}
/**
* Display the links to the general feeds, without comments.
*
* @access public
* @since 0.0.4
* @uses current_theme_supports, wp_parse_args, feed_content_type, get_bloginfo, esc_attr, get_feed_link, _x, __
*
* @param array $args Optional arguments
*
* @return string
*/
public function feed_links( $args ) {
if ( ! current_theme_supports( 'automatic-feed-links' ) ) {
return NULL;
}
$defaults = array(
// Translators: Separator between blog name and feed type in feed links.
'separator' => _x(
'»',
'feed link',
'remove_comments_absolute'
),
// Translators: 1: blog title, 2: separator (raquo).
'feedtitle' => __( '%1$s %2$s Feed', 'remove_comments_absolute' ),
);
$args = wp_parse_args( $args, $defaults );
echo '<link rel="alternate" type="' . esc_attr( feed_content_type() ) . '" title="' .
esc_attr(
sprintf(
$args[ 'feedtitle' ],
get_bloginfo( 'name' ),
$args[ 'separator' ]
)
) . '" href="' . esc_attr( get_feed_link() ) . '"/>' . "\n";
}
/**
* Display the links to the extra feeds such as category feeds.
*
* Copy from WP default, but without comment feed; no filter available.
*
* @since 04/08/2013
*
* @param array $args Optional argument.
*/
public function feed_links_extra( $args ) {
$defaults = array(
/* Translators: Separator between blog name and feed type in feed links. */
'separator' => _x( '»', 'feed link' ),
/* Translators: 1: blog name, 2: separator(raquo), 3: category name. */
'cattitle' => __( '%1$s %2$s %3$s Category Feed' ),
/* Translators: 1: blog name, 2: separator(raquo), 3: tag name. */
'tagtitle' => __( '%1$s %2$s %3$s Tag Feed' ),
/* Translators: 1: blog name, 2: separator(raquo), 3: author name. */
'authortitle' => __( '%1$s %2$s Posts by %3$s Feed' ),
/* Translators: 1: blog name, 2: separator(raquo), 3: search phrase. */
'searchtitle' => __( '%1$s %2$s Search Results for “%3$s” Feed' ),
/* Translators: 1: blog name, 2: separator(raquo), 3: post type name. */
'posttypetitle' => __( '%1$s %2$s %3$s Feed' ),
);
$args = wp_parse_args( $args, $defaults );
if ( is_category() ) {
$term = get_queried_object();
$title = sprintf( $args[ 'cattitle' ], get_bloginfo( 'name' ), $args[ 'separator' ], $term->name );
$href = get_category_feed_link( $term->term_id );
} elseif ( is_tag() ) {
$term = get_queried_object();
$title = sprintf( $args[ 'tagtitle' ], get_bloginfo( 'name' ), $args[ 'separator' ], $term->name );
$href = get_tag_feed_link( $term->term_id );
} elseif ( is_author() ) {
$author_id = (int) get_query_var( 'author' );
$title = sprintf(
$args[ 'authortitle' ], get_bloginfo( 'name' ), $args[ 'separator' ],
get_the_author_meta( 'display_name', $author_id )
);
$href = get_author_feed_link( $author_id );
} elseif ( is_search() ) {
$title = sprintf(
$args[ 'searchtitle' ], get_bloginfo( 'name' ), $args[ 'separator' ], get_search_query( FALSE )
);
$href = get_search_feed_link();
} elseif ( is_post_type_archive() ) {
$title = sprintf(
$args[ 'posttypetitle' ], get_bloginfo( 'name' ), $args[ 'separator' ],
post_type_archive_title( '', FALSE )
);
$href = get_post_type_archive_feed_link( get_queried_object()->name );
}
if ( isset( $title, $href ) ) {
echo '<link rel="alternate" type="' . esc_attr( feed_content_type() ) . '" title="' . esc_attr(
$title
) . '" href="' . esc_url( $href ) . '" />' . "\n";
}
}
/**
* Redirect on comment feed, set status 301.
*
* @since 04/08/2013
* @return NULL
*/
public function filter_query() {
if ( ! is_comment_feed() ) {
return NULL;
}
if ( isset( $_GET[ 'feed' ] ) ) {
wp_redirect( remove_query_arg( 'feed' ), 301 );
exit();
}
// Redirect_canonical will do the rest.
set_query_var( 'feed', '' );
}
/**
* Unset additional HTTP headers for pingback.
*
* @since 04/07/2013
*
* @param array $headers
*
* @return array $headers
*/
public function filter_wp_headers( $headers ) {
unset( $headers[ 'X-Pingback' ] );
return $headers;
}
/**
* Unregister default comment widget.
*
* @since 07/16/2012
*/
public function unregister_default_wp_widgets() {
unregister_widget( 'WP_Widget_Recent_Comments' );
}
/**
* Remove options for Keyboard Shortcuts on profile page.
*
* @since 09/03/2012
*
* @return void
*/
public function remove_profile_items() {
?>
<script type="text/javascript">
//<![CDATA[
jQuery( document ).ready( function( $ ) {
$( '#your-profile' ).find( '.form-table' ).first().find( 'tr:nth-child(3)' ).remove();
} );
//]]>
</script>
<?php
}
/**
* Replace the theme's or the core comments template with an empty one.
*
* @since 2016-02-16
* @return string The path to the empty template file.
*/
public function comments_template() {
return plugin_dir_path( __FILE__ ) . 'comments.php';
}
/**
* Replace comment related XML_RPC methods.
*
* @access public
* @since 09/21/2013
*
* @param array $methods
*
* @return array $methods
*/
public function xmlrpc_replace_methods( $methods ) {
$comment_methods = array(
'wp.getCommentCount',
'wp.getComment',
'wp.getComments',
'wp.deleteComment',
'wp.editComment',
'wp.newComment',
'wp.getCommentStatusList',
);
foreach ( $comment_methods as $method_name ) {
if ( isset( $methods[ $method_name ] ) ) {
$methods[ $method_name ] = array( $this, 'xmlrpc_placeholder_method' );
}
}
return $methods;
}
/**
* XML_RPC placeholder method.
*
* @access public
* @since 09/21/2013
* @return IXR_Error object
*/
public function xmlrpc_placeholder_method() {
return new IXR_Error(
403,
esc_attr__( 'Comments are disabled on this site.', 'remove_comments_absolute' )
);
}
/**
* Remove comments popup.
*
* @see https://core.trac.wordpress.org/ticket/28617
*
* @since 12/14/2015
*
* @param array $public_query_vars The array of whitelisted query variables.
*
* @return array
*/
public function filter_query_vars( $public_query_vars ) {
$key = array_search( 'comments_popup', $public_query_vars, FALSE );
if ( FALSE !== $key ) {
unset( $public_query_vars[ $key ] );
}
return $public_query_vars;
}
/**
* Remove 'Discussion Settings' help tab from post edit screen.
*
* @since 01/01/2016
*
* @access private
*/
public function remove_help_tabs() {
$current_screen = get_current_screen();
if ( $current_screen->get_help_tab( 'discussion-settings' ) ) {
$current_screen->remove_help_tab( 'discussion-settings' );
}
}
/**
* Remove rewrite rules for the legacy comment feed and post type comment pages.
*
* @since 2016-02-16
*
* @param array $rules The compiled array of rewrite rules.
*
* @return array The filtered array of rewrite rules.
*/
public function filter_rewrite_rules_array( $rules ) {
if ( is_array( $rules ) ) {
// Remove the legacy comment feed rule.
foreach ( $rules as $k => $v ) {
if ( FALSE !== strpos( $k, '|commentsrss2' ) ) {
$new_k = str_replace( '|commentsrss2', '', $k );
unset( $rules[ $k ] );
$rules[ $new_k ] = $v;
}
}
// Remove all other comment related rules.
foreach ( $rules as $k => $v ) {
if ( FALSE !== strpos( $k, 'comment-page-' ) ) {
unset( $rules[ $k ] );
}
}
}
return $rules;
}
/**
* Return an object with each comment stat set to zero.
*
* Prevents 'wp_count_comments' form performing a database query.
*
* @since 2016-08-29
* @see wp_count_comments
* @return object Comment stats.
*/
public function filter_count_comments() {
return (object) array( 'approved' => 0,
'spam' => 0,
'trash' => 0,
'post-trashed' => 0,
'total_comments' => 0,
'all' => 0,
'moderated' => 0
);
}
} // end class
} // end if class exists