1+ <?php
2+
3+ /**
4+ * Plugin loader.
5+ *
6+ * @package OnePlace\Swissknife
7+ * @copyright 2019 Verein onePlace
8+ * @license https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html GNU General Public License, version 2
9+ * @link https://1plc.ch/wordpress-plugins/swissknife
10+ */
11+
12+ namespace OnePlace \Swissknife \Modules ;
13+
14+ use OnePlace \Swissknife \Plugin ;
15+
16+ final class Comments {
17+ /**
18+ * Main instance of the module
19+ *
20+ * @since 0.1-stable
21+ * @var Plugin|null
22+ */
23+ private static $ instance = null ;
24+
25+ /**
26+ * Disable wordpress comments entirely
27+ *
28+ * @since 0.1-stable
29+ */
30+ public function register () {
31+ // Close comments on the front-end
32+ add_filter ('comments_open ' , '__return_false ' , 20 , 2 );
33+ add_filter ('pings_open ' , '__return_false ' , 20 , 2 );
34+
35+ // Hide existing comments
36+ add_filter ('comments_array ' , '__return_empty_array ' , 10 , 2 );
37+
38+ // Remove comments page in menu
39+ add_action ('admin_menu ' , function () {
40+ remove_menu_page ('edit-comments.php ' );
41+ });
42+
43+ // Remove comments links from admin bar
44+ add_action ('init ' , function () {
45+ if (is_admin_bar_showing ()) {
46+ remove_action ('admin_bar_menu ' , 'wp_admin_bar_comments_menu ' , 60 );
47+ }
48+ });
49+
50+ add_action ('admin_init ' , function () {
51+ // Redirect any user trying to access comments page
52+ global $ pagenow ;
53+
54+ if ($ pagenow === 'edit-comments.php ' ) {
55+ wp_redirect (admin_url ());
56+ exit ;
57+ }
58+ // Remove comments metabox from dashboard
59+ remove_meta_box ('dashboard_recent_comments ' , 'dashboard ' , 'normal ' );
60+ // Disable support for comments and trackbacks in post types
61+ foreach (get_post_types () as $ post_type ) {
62+ if (post_type_supports ($ post_type , 'comments ' )) {
63+ remove_post_type_support ($ post_type , 'comments ' );
64+ remove_post_type_support ($ post_type , 'trackbacks ' );
65+ }
66+ }
67+ });
68+ }
69+
70+ /**
71+ * Loads the plugin main instance and initializes it.
72+ *
73+ * @since 0.1-stable
74+ *
75+ * @param string $main_file Absolute path to the plugin main file.
76+ * @return bool True if the plugin main instance could be loaded, false otherwise.
77+ */
78+ public static function load () {
79+ if ( null !== static ::$ instance ) {
80+ return false ;
81+ }
82+ static ::$ instance = new self ();
83+ static ::$ instance ->register ();
84+ return true ;
85+ }
86+ }
0 commit comments